transform-caret.xqm 11.2 KB
Newer Older
Mathias Goebel's avatar
Mathias Goebel committed
1
2
3
xquery version "3.1";
(:~
 : This module provides function corresponding to @tei:rend describing carets.
Mathias Goebel's avatar
Mathias Goebel committed
4
5
6
7
8
9
10
11
12
13
14
 :
 : Types currently used are:
 : [x] "caret:bow"
 : [x] "caret:curved_V"
 : [x] "caret:slash"
 : [ ] "caret:backslash"
 : [x] "caret:half-bow"
 : [x] "caret:V"
 : [ ] "caret:semicircle"
 : [ ] "caret:quartercircle"
 : [ ] "caret:looped_arc"
Mathias Goebel's avatar
Mathias Goebel committed
15
 : [>] "caret:funnel"
Mathias Goebel's avatar
Mathias Goebel committed
16
17
 : [ ] "caret:3/4-circle"
 : [ ] "caret:retraced_half-bow"
Mathias Goebel's avatar
Mathias Goebel committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 :)

module namespace caret="http://fontane-nb.dariah.eu/Transfo/caret";

declare namespace svg="http://www.w3.org/2000/svg";
declare namespace tei="http://www.tei-c.org/ns/1.0";

declare function caret:slash() {
<svg xmlns="http://www.w3.org/2000/svg"
    class="teicaret slash"
    style="right: -8px;"
    height="20"
    width="20">
    <line y2="0" x2="20" y1="20" x1="5"/>
</svg>
};

declare function caret:v($node) {
let
    $parameter := substring-before( substring-after( $node/string(@rend), '('), ')'),
    $parameter := tokenize(replace($parameter, 'cm', ''), ','),
    $l := number($parameter[1]) * 100,
    $r := number($parameter[2]) * 100,
    $height := '100',
    $viewbox := '0 0 ' || string($l+ $r) || ' ' || $height,
Mathias Goebel's avatar
Mathias Goebel committed
43
    $style := string-join(($node/tokenize(@style)[not(contains(., "margin-left"))], 'margin-left:-' || $parameter[1] || 'cm'), ";")
Mathias Goebel's avatar
Mathias Goebel committed
44
45
46
47
48
49
50

return
    <svg xmlns="http://www.w3.org/2000/svg"
        class="caretV"
        style="{$style}"
        width="{string(($l + $r) div 100)}cm"
        height="0.6cm"
Mathias Goebel's avatar
caret:V    
Mathias Goebel committed
51
52
        viewBox="{$viewbox}"
        preserveAspectRatio="none">
Mathias Goebel's avatar
Mathias Goebel committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
        <polyline
            points="0,0 {string($l)},{$height} {string($l + $r)},0"
            fill="none"
            stroke-width="3"/>
    </svg>
};

declare function caret:half-bow($node, $lineHeight) {
let $params:= tokenize(substring-before(substring-after($node/@rend, '('), ')'), ',')
let $width := $params[1]
let $pos := $params[2]

let $seq := tokenize($node/string(@style), ':|\s')
let $mlnum := (for $i at $pos in $seq where contains($i, 'margin-left') return $pos)+1,
    $mlnum := number( substring-before( $seq[$mlnum], 'cm') )
let $viewW := number( substring-before($width, 'cm') ) * 100

return
    switch($pos)
        case 'pos-right' return
        <svg xmlns="http://www.w3.org/2000/svg"
Mathias Goebel's avatar
Mathias Goebel committed
74
            class="caret_half-bow_pos-right"
Mathias Goebel's avatar
Mathias Goebel committed
75
76
77
78
            width="{$width}"
            height="{$lineHeight - 0.2}cm"
            viewBox="0 0 {$viewW} 100"
            preserveAspectRatio="none"
Mathias Goebel's avatar
Mathias Goebel committed
79
            style="left:{string(if($mlnum lt -0.19) then -0.2 else $mlnum)}cm;">
Mathias Goebel's avatar
Mathias Goebel committed
80
81
82
83
84
85
86
            <path
                d="M30,95 C30,95 5,100 5,60 M 5,60 C 5,20 {$viewW div 2},10 {$viewW - 5},5"
                fill="none"
                stroke-width="2"/>
        </svg>
        case 'pos-left' return
          <svg xmlns="http://www.w3.org/2000/svg"
Mathias Goebel's avatar
Mathias Goebel committed
87
88
89
90
91
            class="caret_half-bow_pos-left"
            width="{$width}" height="{$lineHeight - 0.2}cm"
            viewBox="0 0 100 100"
            preserveAspectRatio="none"
            style="left:{$mlnum}cm;">
Mathias Goebel's avatar
Mathias Goebel committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
            <path
                fill="none"
                stroke-width="2"
                d="M 78.36,37.00 C 96.91,33.55 108.45,20.36 86.00,10.45 86.18,10.55 72.27,2.73 4.55,9.09" />
          </svg>
    default return
        error(QName("https://sade.textgrid.de/ns/error/fontane", "CARET-HALF-BOW"), "unsupported position: " || $pos)
};

declare function caret:retraced-half-bow($node, $lineHeight) {
let $params:= tokenize(substring-before(substring-after($node/@rend, '('), ')'), ',')
let $width := $params[1]
let $pos := $params[2]

let $seq := tokenize($node/string(@style), ':|\s')
let $mlnum := (for $i at $pos in $seq where contains($i, 'margin-left') return $pos)+1,
    $mlnum := number( substring-before( $seq[$mlnum], 'cm') )
let $viewW := number( substring-before($width, 'cm') ) * 100

return
    switch($pos)
        case 'pos-right' return
        <svg xmlns="http://www.w3.org/2000/svg"
            width="{$width}"
            height="{$lineHeight}cm"
            viewBox="0 0 {$viewW} 100"
            preserveAspectRatio="none"
            style="left:{string($mlnum - 0.3)}cm;top: 0;">
            <path
                d="M30,95 C30,95 5,100 5,60 M 5,60 C 5,20 {$viewW div 2},10 {$viewW - 5},5"
                fill="none"
                stroke-width="2"/>
            <path
                d="M35,95 C35,95 10,100 10,60 M 10,60 C 10,20 {$viewW div 2},10 {$viewW - 5},10"
                fill="none"
                stroke-width="2"/>
        </svg>
    default return
        <xhtml:div>{$pos} no example in Gesamtdoku</xhtml:div>
};

declare function caret:bow($node as element(*), $lineHeight) {
let $params:= tokenize(substring-before(substring-after($node/@rend, '('), ')'), ',')
let $width := $params[1]
let $pos := $params[2]

let $seq := tokenize($node/string(@style), ':|\s')
let $mlnum := (for $i at $pos in $seq where contains($i, 'margin-left') return $pos)+1,
    $mlnum := number( substring-before( $seq[$mlnum], 'cm') )

let $viewW := number( substring-before($width, 'cm') ) * 100,
    $p := $viewW - ($viewW * 0.05),
    $q := $viewW * (3 div 8)

return

switch($pos)
    case 'pos-right' return
        <svg xmlns="http://www.w3.org/2000/svg"
            class="caret_bow_pos-right"
            width="{$width}"
            height="{$lineHeight}cm"
            viewBox="0 0 {$viewW} 80"
            preserveAspectRatio="none"
            style="left:-0.1cm;">
            <path
                d="M{$p}, 5 C{$p},  5 {$viewW}, 10 {$p}, 18 M{$p}, 18 C{$p}, 18   0, 10   5, 60 M  5, 60 C {$viewW div 10},100 {$viewW div 3}, 60  {$q - 15}, 70"
                fill="none"
                stroke-width="2"/>
        </svg>

    case 'pos-left' return
        <svg xmlns="http://www.w3.org/2000/svg"
            class="caret_bow_pos-left"
            width="{$width}"
            height="{$lineHeight}cm"
            viewBox="0 0 {$viewW} 80"
            preserveAspectRatio="none"
            style="left:{string($mlnum - 0.3)}cm;">
            <path
                d="M10, 5 C10,5 0,15 10,20 M10,20 C{$viewW div 3},35 {$viewW},30 {$viewW - 5},65 M{$viewW - 5},65 C{$viewW - 5},80 {($viewW div 2) + ($viewW * 0.25)},75 {($viewW div 2) + ($viewW * 0.25)},75"
                fill="none"
                stroke-width="2"/>
        </svg>

    default return
        error(QName("https://sade.textgrid.de/ns/error/fontane", "CARET-BOW"), "unsupported position: " || $pos)
};

declare function caret:backslash($node as element()) {
let $seq := tokenize($node/string(@style), ':|\s')
let $mlnum := (for $i at $pos in $seq where contains($i, 'margin-left') return $pos)+1

return
    <svg xmlns="http://www.w3.org/2000/svg"
        style="left: calc({$seq[$mlnum]} - 6px);"
        height="20"
        width="20">
        <line x1="0" y1="0" x2="15" y2="20"/>
    </svg>
};

declare function caret:semicircle($node, $lineHeight) {
let $pos := substring-before(substring-after($node/@rend, '('), ')')
return
    switch($pos)
        case 'right' return
            <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="{$lineHeight}cm" viewBox="0 0 100 100" preserveAspectRatio="none">
                <path d="M75,5 C 75,5 145,100 5,90" fill="none" stroke-width="2"/>
            </svg>
        case 'left' return
            <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="{$lineHeight}cm" viewBox="0 0 100 100" preserveAspectRatio="none">
                <path d="M25,5 C 25,5 -45,100 95,90" fill="none" stroke-width="2"/>
            </svg>
        default return ()
};

declare function caret:quartercircle($node, $lineHeight) {
let $pos := substring-before(substring-after($node/@rend, '('), ')')

let $seq := tokenize($node/string(@style), ':|\s')
let $mlnum := (for $i at $pos in $seq where contains($i, 'margin-left') return $pos)+1,
    $mlnum := number( substring-before( $seq[$mlnum], 'cm') )
let $width := if($mlnum lt 0) then $mlnum * -1 else $mlnum
let $width := if($width = 0) then 0.7 else $width
let $width := if(string($width) = "NaN" ) then 1.2 else $width
return
    switch($pos)
    case 'upper-right' return
    <svg xmlns="http://www.w3.org/2000/svg" class="upper-right" width="{$width}cm" height="{$lineHeight - 0.2}cm" viewBox="0 0 100 50" preserveAspectRatio="none" data-pos="{$pos}" style="position: absolute;right: 0;top: 5px;">
                <path d="M0,10 C 0,10 95,-10 95,45" fill="none" stroke-width="2"/>
            </svg>
    case 'upper-left' return
    <svg xmlns="http://www.w3.org/2000/svg" class="upper-left" width="1cm" height="{$lineHeight - 0.2}cm" viewBox="0 0 100 50" preserveAspectRatio="none" data-pos="{$pos}" style="position: absolute;right: -0.8cm;top: 0;">
                <path d="M3,95 C 3,95 0,35 60,20 M60,20 C60,20 90,15 100,20" fill="none" stroke-width="2"/>
            </svg>
    case 'lower-left' return
        <svg xmlns="http://www.w3.org/2000/svg"
            class="lower-left"
            width="{$width * 3}cm"
            height="{$lineHeight - 0.2}cm"
            viewBox="0 0 100 50"
            preserveAspectRatio="none"
            data-pos="{$pos}"
            style="position: absolute;right: -40px;top: 0;">
            <path
                id="quartercircle_lower-left"
                fill="none"
                stroke="black"
                stroke-width="1"
                d="M 3.30,48.00 C 3.30,48.00 35.09,65.78 63.43,4.74" />
        </svg>
    default return <xhtml:div>unknown parameter in function 'caret'</xhtml:div>
};

Mathias Goebel's avatar
Mathias Goebel committed
247
declare function caret:funnel($node) {
Mathias Goebel's avatar
Mathias Goebel committed
248
let $params as xs:decimal+ := tokenize(substring-before(substring-after($node/replace(@rend, "cm", ""), '('), ')'), ',') ! xs:decimal(.)
Mathias Goebel's avatar
Mathias Goebel committed
249
return
Mathias Goebel's avatar
Mathias Goebel committed
250
251
<svg xmlns="http://www.w3.org/2000/svg"
    style="margin-left: -1cm;margin-bottom: 0.2cm;"
Mathias Goebel's avatar
Mathias Goebel committed
252
    width="{$params[1] + $params[2]}cm"
Mathias Goebel's avatar
Mathias Goebel committed
253
254
255
256
257
258
259
260
261
262
263
264
    viewBox="0 0 350 231"
    height="0.5cm">
    <path
        id="right"
        fill="none"
        stroke="darkslategrey"
        d="M 222.50,210.75 C 222.50,210.75 345.00,11.25 345.00,11.25"
        stroke-width="5"></path>
    <path
        id="left"
        fill="none"
        stroke="darkslategrey"
Mathias Goebel's avatar
Mathias Goebel committed
265
        d="M 33.00,12.50 C 33.00,12.50 172.25,205.75 172.25,205.75 .75,167.50 -68.00,46.25"
Mathias Goebel's avatar
Mathias Goebel committed
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
        stroke-width="5"></path>
  </svg>
};

declare function caret:curved-v($node, $lineHeight) {
let $seq := tokenize($node/string(@style), ':|\s')
let $mlnum := (for $i at $pos in $seq where contains($i, 'margin-left') return $pos)+1,
    $mlnum := string(number( substring-before( $seq[$mlnum], 'cm') ) - 0.1) || 'cm'
let $width:= tokenize(substring-before(substring-after($node/@rend, '('), ')'), ','),
    $w1:=number(substring-before($width[1], 'cm')),
    $w2:=number(substring-before($width[2], 'cm')),
    $x:=($w1 * 100) div ($w1 + $w2),
    $width:= $w1 + $w2,
    $viewBoxHeight := 100,
    $viewBoxWidth := 300,
    $turn1 := round( (($viewBoxWidth div 2) * ($w1 div $w2)) ),
Mathias Goebel's avatar
Mathias Goebel committed
282
    $turn := ($viewBoxWidth div $width * $w1) + 10
Mathias Goebel's avatar
Mathias Goebel committed
283
284
285
286
287
288
289
290

return
<svg xmlns="http://www.w3.org/2000/svg"
    class="curved_V"
    height="100%"
    width="{$width}cm"
    viewBox="0 0 {$viewBoxWidth} {$viewBoxHeight}"
    preserveAspectRatio="none"
Mathias Goebel's avatar
Mathias Goebel committed
291
    style="left:{ 0 - $width div $viewBoxWidth * $turn }cm;">
Mathias Goebel's avatar
Mathias Goebel committed
292
293
    <path
        d="M  10,40 C   0, 45   0, 55  10,60 M  10,60 C 10, 60 {$turn + 5}, 60 {$turn + 5},80 M {$turn + 5},80 C {$turn + 5}, 80 {$turn + 2}, 87 {$turn},80 M {$turn},80 C {$turn + 2}, 50 {290 - (290 - $turn) div 2}, 60 290,60 M 290,60 C 300, 55 300, 45 290,40"
Mathias Goebel's avatar
caret:V    
Mathias Goebel committed
294
        stroke-width="4"
Mathias Goebel's avatar
Mathias Goebel committed
295
296
297
        fill="none" />
</svg>
};