Debian Codegraph @

lib/ChordPro/res/abc/abc2svg/page-1.js

raw symbols SCIP
1 // page.js - module to generate pages
2 //
3 // Copyright (C) 2018-2025 Jean-Francois Moine
4 //
5 // This file is part of abc2svg.
6 //
7 // abc2svg is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // abc2svg is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // This module is loaded when "%%pageheight" appears in a ABC source.
21 //
22 // Parameters
23 // %%pageheight <unit>
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.page = {
29
30 // function called at end of generation
31 abc_end: function(of) {
32 var page = this.page
33 if (page && page.in_page)
34 abc2svg.page.close_page(page)
35
36 // restore user.img_out and abc2svg.abc_end (needed when more generation)
37 if (abc2svg.page.user_out) {
38 this.get_user().img_out = abc2svg.page.user_out
39 abc2svg.page.user_out = null
40 abc2svg.abc_end = of
41 }
42 of()
43 }, // abc_end()
44
45 // output the SVG tag
46 svg_tag: function(w, h, ty, user) {
47 w = Math.ceil(w)
48 h = Math.ceil(h)
49 return '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"\n\
50 xmlns:xlink="http://www.w3.org/1999/xlink"\n\
51 class="'
52 + ty + '" '
53 + (user.imagesize != undefined
54 ? (user.imagesize)
55 : ('width="' + w + 'px" height="' + h + 'px"')
56 )
57 + ' viewBox="0 0 ' + w + ' ' + h + '">'
58 }, // svg_tag()
59
60 // generate a header or a footer in page.hf and return its height
61 gen_hf: function(page, ty) {
62 var a, i, j, k, x, y, y0, s, str,
63 font = page.abc.get_font(ty.substr(0, 6)),
64 cfmt = page.abc.cfmt(),
65 fh = font.size * 1.1,
66 pos = [ '">',
67 '" text-anchor="middle">',
68 '" text-anchor="end">' ]
69
70 // replace <>& by XML character references
71 function clean_txt(txt) {
72 return txt.replace(/<|>|&.*?;|&/g, function(c) {
73 switch (c) {
74 case '<': return "&lt;"
75 case '>': return "&gt;"
76 case '&': return "&amp;"
77 }
78 return c
79 })
80 } // clean_txt()
81
82 // clear a field if $x said so
83 function clr(str) {
84 return str.indexOf('\u00ff') >= 0 ? '' : str
85 } //clr()
86
87 // create the text of a header or a footer
88 function header_footer(o_font, str) {
89 var c, d, i, k, t, n_font, s, noc,
90 c_font = o_font,
91 nl = 1,
92 j = 0,
93 r = ["", "", ""]
94
95 if (str[0] == '"')
96 str = str.slice(1, -1)
97 while (1) {
98 i = str.indexOf('$', j)
99 if (i < 0)
100 break
101 c = str[++i]
102 s = '$' + c // string to replace
103 switch (c) {
104 case 'd':
105 if (!abc2svg.get_mtime)
106 break // cannot know the change time of the file
107 d = abc2svg.get_mtime(abc.get_parse().fname)
108 // fall thru
109 case 'D':
110 if (c == 'D')
111 d = new Date()
112 if (cfmt.dateformat[0] == '"')
113 cfmt.dateformat = cfmt.dateformat.slice(1, -1)
114 d = strftime(cfmt.dateformat, d)
115 break
116 case 'F':
117 d = typeof document != "undefined"
118 ? window.location.href
119 : page.abc.get_parse().fname
120 break
121 case 'I':
122 c = str[++i]
123 s += c
124 // fall thru
125 case 'T':
126 t = page.abc.info()[c]
127 d = t ? t.split('\n', 1)[0] : ''
128 break
129 case 'P': // current page number
130 case 'Q': // absolute page number
131 j = str.indexOf('\t', i)
132 noc = str.indexOf('$P', i)
133 noc = noc > 0 && noc < j // if two $P's in the same cell
134 ? '' // don't remove the cell
135 : '\u00ff', // remove the whole cell
136 t = c == 'P' ? page.pn : page.pna
137 switch (str[i + 1]) {
138 case '0':
139 s += '0'
140 d = (t & 1) ? noc : t
141 break
142 case '1':
143 s += '1'
144 d = (t & 1) ? t : noc
145 break
146 default:
147 d = t
148 break
149 }
150 break
151 case 'V':
152 d = "abc2svg-" + abc2svg.version
153 break
154 default:
155 d = ''
156 if (c == '0')
157 n_font = o_font
158 else if (c >= '1' && c < '9')
159 n_font = page.abc.get_font("u" + c)
160 else
161 break
162
163 // handle the font changes
164 if (n_font == c_font)
165 break
166 if (c_font != o_font)
167 d += "</tspan>"
168 c_font = n_font
169 if (c_font == o_font)
170 break
171 d += '<tspan class="' +
172 font_class(n_font) + '">'
173 break
174 }
175 str = str.replace(s, d)
176 j = i
177 }
178 if (c_font != o_font)
179 str += "</tspan>";
180
181 str = str.split('\n')
182 r[4] = str.length // number of lines
183 for (j = 0; j < str.length; j++) {
184 if (j != 0)
185 for (i = 0; i < 3; i++)
186 r[i] += '\n'
187 t = str[j].split('\t')
188 if (t.length == 1) {
189 r[1] += clr(t[0])
190 } else {
191 for (i = 0; i < 3; i++) {
192 if (t[i])
193 r[i] += clr(t[i])
194 }
195 }
196 }
197 return r
198 } // header_footer()
199
200 function font_class(font) {
201 if (font.class)
202 return 'f' + font.fid + cfmt.fullsvg + ' ' + font.class
203 return 'f' + font.fid + cfmt.fullsvg
204 }
205
206 // gen_hf
207
208 if (!(page.pn & 1))
209 str = page[ty + '2'] || page[ty]
210 else
211 str = page[ty]
212
213 if (str[0] == '-') { // not on 1st page
214 if (page.pn == 1)
215 return 0
216 str = str.slice(1)
217 }
218
219 a = header_footer(font, clean_txt(str))
220 y0 = font.size * .8
221 for (i = 0; i < 3; i++) {
222 str = a[i]
223 if (!str)
224 continue
225 if (i == 0)
226 x = cfmt.leftmargin
227 else if (i == 1)
228 x = cfmt.pagewidth / 2
229 else
230 x = cfmt.pagewidth - cfmt.rightmargin
231 y = y0
232 k = 0
233 while (1) {
234 j = str.indexOf('\n', k)
235 if (j >= 0)
236 s = str.slice(k, j)
237 else
238 s = str.slice(k)
239 if (s)
240 page.hf += '<text class="' +
241 font_class(font) +
242 '" x="' + x.toFixed(1) +
243 '" y="' + y.toFixed(1) +
244 pos[i] +
245 s + '</text>\n'
246 if (j < 0)
247 break
248 k = j + 1
249 y += fh
250 }
251 }
252 return fh * a[4]
253 }, // gen_hf()
254
255 // start a new page
256 open_page: function(page,
257 ht) { // spacing under the header
258 var h,
259 abc = page.abc,
260 cfmt = abc.cfmt(),
261 sty = '<div style="line-height:0'
262
263 page.pn++
264 page.pna++
265
266 // start a new page
267 if (page.first)
268 page.first = false
269 else
270 sty += ";page-break-before:always"
271 if (page.gutter)
272 sty += ";margin-left:" +
273 ((page.pn & 1) ? page.gutter : -page.gutter).toFixed(1) + "px"
274 abc2svg.page.user_out(sty + '">')
275 page.in_page = true
276
277 ht += page.topmargin
278 page.hmax = cfmt.pageheight - page.botmargin - ht
279
280 // define the header/footer
281 page.hf = ''
282 if (page.header) {
283 abc.clr_sty()
284 if (!cfmt.headerfont)
285 abc.param_set_font("headerfont", "text,serif 16")
286 h = abc2svg.page.gen_hf(page, "header")
287 if (!h && page.pn == 1 && page.header1)
288 h = abc2svg.page.gen_hf(page, "header1")
289 sty = abc.get_font_style() // new style(s)
290 if (cfmt.fullsvg || sty != page.hsty) {
291 page.hsty = sty
292 sty = '<style>' + sty + '\n</style>\n'
293 } else {
294 sty = ''
295 }
296 if (ht + h)
297 abc2svg.page.user_out(abc2svg.page.svg_tag(
298 cfmt.pagewidth, ht + h, "header", abc.get_user())
299 + sty +
300 '<g transform="translate(0,' +
301 page.topmargin.toFixed(1) + ')">\n' +
302 page.hf + '</g>\n</svg>')
303 page.hmax -= h;
304 page.hf = ''
305 } else if (ht) {
306 abc2svg.page.user_out(abc2svg.page.svg_tag(cfmt.pagewidth, ht,
307 "header", abc.get_user())
308 + '\n</svg>')
309 }
310 if (page.footer) {
311 abc.clr_sty()
312 if (!cfmt.footerfont)
313 abc.param_set_font("footerfont", "text,serif 16")
314 page.fh = abc2svg.page.gen_hf(page, "footer")
315 sty = abc.get_font_style() // new style(s)
316 if (cfmt.fullsvg || sty != page.fsty) {
317 page.fsty = sty
318 page.ffsty = '<style>' + sty + '\n</style>\n'
319 } else {
320 page.ffsty = ''
321 }
322 page.hmax -= page.fh
323 }
324
325 page.h = 0
326 }, // open_page()
327
328 close_page: function(page) {
329 var h,
330 cfmt = page.abc.cfmt()
331
332 page.in_page = false
333 if (page.footer) {
334 h = page.hmax + page.fh - page.h
335 if (h)
336 abc2svg.page.user_out(
337 abc2svg.page.svg_tag(cfmt.pagewidth, h,
338 "footer", page.abc.get_user()) +
339 page.ffsty +
340 '<g transform="translate(0,' +
341 (h - page.fh).toFixed(1) + ')">\n' +
342 page.hf + '</g>\n</svg>')
343 }
344 abc2svg.page.user_out('</div>')
345 page.h = 0
346 }, // close_page()
347
348 // handle the output flow of the abc2svg generator
349 img_in: function(p) {
350 var h, ht, nh,
351 page = this.page
352
353 // copy a block
354 function blkcpy(page) {
355 while (page.blk.length)
356 abc2svg.page.user_out(page.blk.shift())
357 page.blk = null // direct output
358 } // blkcpy()
359
360 // img_in()
361 switch (p.slice(0, 4)) {
362 case "<div": // block of new tune
363 if (p.indexOf('newpage') > 0
364 || (page.oneperpage && this.info().X)
365 || !page.h) { // empty page
366 if (page.in_page)
367 abc2svg.page.close_page(page)
368 abc2svg.page.open_page(page, 0)
369 }
370 page.blk = [] // in block
371 page.hb = page.h // keep the offset of the start of tune
372 break
373 case "<svg": // SVG image
374 h = Number(p.match(/viewBox="0 0 [\d.]+ ([\d.]+)"/)[1])
375 while (h + page.h >= page.hmax) { // if (still) page overflow
376 ht = page.blk ? 0 :
377 this.cfmt().topspace // tune continuation
378
379 if (page.blk) {
380 if (!page.hb) { // overflow on the first page
381 blkcpy(page)
382 nh = 0
383 } else {
384 nh = page.h - page.hb
385 page.h = page.hb
386 }
387 }
388 abc2svg.page.close_page(page)
389 abc2svg.page.open_page(page, ht)
390
391 if (page.blk) { // if inside a block
392 blkcpy(page) // output the beginning of the tune
393 page.h = nh
394 }
395 if (h > page.hmax)
396 break // error
397 }
398
399 // if no overflow yet, keep the block
400 if (page.blk)
401 page.blk.push(p)
402 else
403 abc2svg.page.user_out(p)
404 page.h += h
405 break
406 case "</di": // end of block
407 if (page.blk)
408 blkcpy(page)
409 break
410 // default:
411 ////fixme: %%beginml cannot be treated (no information about its height)
412 // break
413 }
414 }, // img_in()
415
416 // handle the page related parameters
417 set_fmt: function(of, cmd, parm) {
418 var v,
419 user = this.get_user(),
420 cfmt = this.cfmt(),
421 page = this.page
422
423 if (cmd == "pageheight") {
424 v = this.get_unit(parm)
425 if (isNaN(v)) {
426 this.syntax(1, this.errs.bad_val, '%%' + cmd)
427 return
428 }
429 if (!user.img_out || !abc2svg.abc_end)
430 v = 0
431 cfmt.pageheight = v
432 if (!v) {
433 if (abc2svg.page.user_out) {
434 user.img_out = abc2svg.page.user_out
435 abc2svg.page.user_out = null
436 abc2svg.page.abc_end = abc2svg.page.abc_end_o
437 }
438 delete this.page
439 return
440 }
441
442 // if first definition, install the hook
443 if (!page || !abc2svg.page.user_out) {
444 this.page = page = {
445 abc: this,
446 topmargin: 38, // 1cm
447 botmargin: 38, // 1cm
448 // gutter: 0,
449 h: 0, // current page height
450 pn: 0, // page number
451 pna: 0, // absolute page number
452 ffsty: '', // style of the footer
453 first: true // no skip to next page
454 }
455
456 // don't let the backend handle the header/footer
457 if (cfmt.header) {
458 page.header = cfmt.header;
459 cfmt.header = null
460 }
461 if (cfmt.footer) {
462 page.footer = cfmt.footer;
463 cfmt.footer = null
464 }
465 if (cfmt.header1) {
466 page.header1 = cfmt.header1
467 cfmt.header1 = null
468 }
469 if (cfmt.header2) {
470 page.header2 = cfmt.header2
471 cfmt.header2 = null
472 }
473 if (cfmt.footer2) {
474 page.footer2 = cfmt.footer2
475 cfmt.footer2 = null
476 }
477
478 // get the previously defined page parameters
479 if (cfmt.botmargin != undefined) {
480 v = this.get_unit(cfmt.botmargin)
481 if (!isNaN(v))
482 page.botmargin = v
483 }
484 if (cfmt.topmargin != undefined) {
485 v = this.get_unit(cfmt.topmargin)
486 if (!isNaN(v))
487 page.topmargin = v
488 }
489 if (cfmt.gutter != undefined) {
490 v = this.get_unit(cfmt.gutter)
491 if (!isNaN(v))
492 page.gutter = v
493 }
494 if (cfmt.oneperpage)
495 page.oneperpage = this.get_bool(cfmt.oneperpage)
496 if (!cfmt.dateformat)
497 cfmt.dateformat = "%b %e, %Y %H:%M"
498
499 // set the hooks
500 if (!abc2svg.page.user_out) {
501 abc2svg.page.user_out = user.img_out
502 abc2svg.page.abc_end_o = abc2svg.abc_end
503 }
504 abc2svg.abc_end = abc2svg.page.abc_end.bind(this,
505 abc2svg.abc_end)
506 user.img_out = abc2svg.page.img_in.bind(this)
507 }
508 return
509 }
510 if (page) {
511 switch (cmd) {
512 case "header":
513 case "footer":
514 case "header1":
515 case "header2":
516 case "footer2":
517 page[cmd] = parm
518 return
519 case "newpage":
520 if (!parm)
521 break
522 v = Number(parm)
523 if (isNaN(v)) {
524 this.syntax(1, this.errs.bad_val, '%%' + cmd)
525 return
526 }
527 page.pn = v - 1
528 return
529 case "gutter":
530 case "botmargin":
531 case "topmargin":
532 v = this.get_unit(parm)
533 if (isNaN(v)) {
534 this.syntax(1, this.errs.bad_val, '%%' + cmd)
535 return
536 }
537 page[cmd] = v
538 return
539 case "oneperpage":
540 page[cmd] = this.get_bool(parm)
541 return
542 }
543 }
544 of(cmd, parm)
545 }, // set_fmt()
546
547 set_hooks: function(abc) {
548 abc.set_format("page-format", 1) // do page formatting
549 abc.set_format = abc2svg.page.set_fmt.bind(abc, abc.set_format)
550 abc.set_pagef()
551 }
552 } // page
553
554 if (!abc2svg.mhooks)
555 abc2svg.mhooks = {}
556 abc2svg.mhooks.page = abc2svg.page.set_hooks