Debian Codegraph @

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

raw symbols SCIP
1 // abc2svg - abc2svg.js
2 //
3 // Copyright (C) 2014-2025 Jean-François Moine
4 //
5 // This file is part of abc2svg-core.
6 //
7 // abc2svg-core 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-core 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-core. If not, see <http://www.gnu.org/licenses/>.
19
20 // define the abc2svg object is not yet done
21 if (typeof abc2svg == "undefined")
22 var abc2svg = {};
23
24 // constants
25 abc2svg.C = {
26 BLEN: 1536,
27
28 // symbol types
29 BAR: 0,
30 CLEF: 1,
31 CUSTOS: 2,
32 SM: 3, // sequence marker (transient)
33 GRACE: 4,
34 KEY: 5,
35 METER: 6,
36 MREST: 7,
37 NOTE: 8,
38 PART: 9,
39 REST: 10,
40 SPACE: 11,
41 STAVES: 12,
42 STBRK: 13,
43 TEMPO: 14,
44 BLOCK: 16,
45 REMARK: 17,
46
47 // note heads
48 FULL: 0,
49 EMPTY: 1,
50 OVAL: 2,
51 OVALBARS: 3,
52 SQUARE: 4,
53
54 // position types
55 SL_ABOVE: 0x01, // position (3 bits)
56 SL_BELOW: 0x02,
57 SL_AUTO: 0x03,
58 SL_HIDDEN: 0x04,
59 SL_DOTTED: 0x08, // modifiers
60 SL_ALI_MSK: 0x70, // align
61 SL_ALIGN: 0x10,
62 SL_CENTER: 0x20,
63 SL_CLOSE: 0x40
64 };
65
66 // !! tied to the symbol types in abc2svg.js !!
67 abc2svg.sym_name = ['bar', 'clef', 'custos', 'smark', 'grace',
68 'key', 'meter', 'Zrest', 'note', 'part',
69 'rest', 'yspace', 'staves', 'Break', 'tempo',
70 '', 'block', 'remark']
71
72 // key table - index = number of accidentals + 7
73 abc2svg.keys = [
74 new Int8Array([-1,-1,-1,-1,-1,-1,-1 ]), // 7 flat signs
75 new Int8Array([-1,-1,-1, 0,-1,-1,-1 ]), // 6 flat signs
76 new Int8Array([ 0,-1,-1, 0,-1,-1,-1 ]), // 5 flat signs
77 new Int8Array([ 0,-1,-1, 0, 0,-1,-1 ]), // 4 flat signs
78 new Int8Array([ 0, 0,-1, 0, 0,-1,-1 ]), // 3 flat signs
79 new Int8Array([ 0, 0,-1, 0, 0, 0,-1 ]), // 2 flat signs
80 new Int8Array([ 0, 0, 0, 0, 0, 0,-1 ]), // 1 flat signs
81 new Int8Array([ 0, 0, 0, 0, 0, 0, 0 ]), // no accidental
82 new Int8Array([ 0, 0, 0, 1, 0, 0, 0 ]), // 1 sharp signs
83 new Int8Array([ 1, 0, 0, 1, 0, 0, 0 ]), // 2 sharp signs
84 new Int8Array([ 1, 0, 0, 1, 1, 0, 0 ]), // 3 sharp signs
85 new Int8Array([ 1, 1, 0, 1, 1, 0, 0 ]), // 4 sharp signs
86 new Int8Array([ 1, 1, 0, 1, 1, 1, 0 ]), // 5 sharp signs
87 new Int8Array([ 1, 1, 1, 1, 1, 1, 0 ]), // 6 sharp signs
88 new Int8Array([ 1, 1, 1, 1, 1, 1, 1 ]) // 7 sharp signs
89 ]
90
91 // base-40 representation of musical pitch
92 // (http://www.ccarh.org/publications/reprints/base40/)
93 abc2svg.p_b40 = new Int8Array( // staff pitch to base-40
94 // C D E F G A B
95 [ 2, 8, 14, 19, 25, 31, 37 ])
96 abc2svg.b40_p = new Int8Array( // base-40 to staff pitch
97 // C D
98 [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
99 // E F G
100 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4,
101 // A B
102 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6 ])
103 abc2svg.b40_a = new Int8Array( // base-40 to accidental
104 // C D
105 [-2, -1, 0, 1, 2, -3, -2, -1, 0, 1, 2, -3,
106 // E F G
107 -2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -3, -2, -1, 0, 1, 2, -3,
108 // A B
109 -2, -1, 0, 1, 2, -3, -2, -1, 0, 1, 2 ])
110 abc2svg.b40_m = new Int8Array( // base-40 to midi
111 // C D
112 [-2, -1, 0, 1, 2, 0, 0, 1, 2, 3, 4, 0,
113 // E F G
114 2, 3, 4, 5, 6, 3, 4, 5, 6, 7, 0, 5, 6, 7, 8, 9, 0,
115 // A B
116 7, 8, 9, 10, 11, 0, 9, 10, 11, 12, 13 ])
117 abc2svg.b40l5 = new Int8Array([ // base-40 to line of fifth
118 // C D
119 -14, -7, 0, 7, 14, 0,-12, -5, 2, 9, 16, 0,
120 // E F G
121 -10, -3, 4, 11, 18,-15, -8, -1, 6, 13, 0,-13, -6, 1, 8, 15, 0,
122 // A B
123 -11, -4, 3, 10, 17, 0, -9, -2, 5, 12, 19 ])
124
125 abc2svg.isb40 = new Int8Array( // interval with sharp to base-40 interval
126 [0, 1, 6,7,12,17,18,23,24,29,30,35])
127
128 abc2svg.pab40 = function(p, a) {
129 p += 19 // staff pitch from C-1
130 var b40 = ((p / 7) | 0) * 40 + abc2svg.p_b40[p % 7]
131 if (a && a != 3) // if some accidental, but not natural
132 b40 += a
133 return b40
134 } // pit2b40()
135 abc2svg.b40p = function(b) {
136 return ((b / 40) | 0) * 7 + abc2svg.b40_p[b % 40] - 19
137 } // b40p()
138 abc2svg.b40a = function(b) {
139 return abc2svg.b40_a[b % 40]
140 } // b40a()
141 abc2svg.b40m = function(b) {
142 return ((b / 40) | 0) * 12 + abc2svg.b40_m[b % 40]
143 } // b40m()
144
145 // chord table
146 // This table is used in various modules
147 // to convert the types of chord symbols to a minimum set.
148 // More chord types may be added by the command %%chordalias.
149 abc2svg.ch_alias = {
150 "maj": "",
151 "min": "m",
152 "-": "m",
153 "°": "dim",
154 "+": "aug",
155 "+5": "aug",
156 "maj7": "M7",
157 "Δ7": "M7",
158 "Δ": "M7",
159 "min7": "m7",
160 "-7": "m7",
161 "ø7": "m7b5",
162 "°7": "dim7",
163 "min+7": "m+7",
164 "aug7": "+7",
165 "7+5": "+7",
166 "7#5": "+7",
167 "sus": "sus4",
168 "7sus": "7sus4"
169 } // ch_alias
170
171 // global fonts
172 abc2svg.font_tb = [] // fonts - index = font.fid
173 abc2svg.font_st = {} // font style => font_tb index for incomplete user fonts
174
175 // cache for converting a duration into [head, dots, nflags]
176 abc2svg.hdn = {}
177
178 // font weight
179 // reference:
180 // https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
181 abc2svg.ft_w = {
182 thin: 100,
183 extralight: 200,
184 light: 300,
185 regular: 400,
186 medium: 500,
187 semi: 600,
188 demi: 600,
189 semibold: 600,
190 demibold: 600,
191 bold: 700,
192 extrabold: 800,
193 ultrabold: 800,
194 black: 900,
195 heavy: 900
196 }
197 abc2svg.ft_re = new RegExp('\
198 -?Thin|-?Extra Light|-?Light|-?Regular|-?Medium|\
199 -?[DS]emi|-?[DS]emi[ -]?Bold|\
200 -?Bold|-?Extra[ -]?Bold|-?Ultra[ -]?Bold|-?Black|-?Heavy/',
201 "i")
202
203 // lyric prefix
204 abc2svg.lypre = /^\d.+\.|^[\d-]+\.?|^\w+:|^\(|^\)/;
205
206 // simplify a rational number n/d
207 abc2svg.rat = function(n, d) {
208 var a, t,
209 n0 = 0,
210 d1 = 0,
211 n1 = 1,
212 d0 = 1
213 while (1) {
214 if (d == 0)
215 break
216 t = d
217 a = (n / d) | 0
218 d = n % d
219 n = t
220 t = n0 + a * n1
221 n0 = n1
222 n1 = t
223 t = d0 + a * d1
224 d0 = d1
225 d1 = t
226 }
227 return [n1, d1]
228 } // rat()
229
230 // compare pitches
231 // This function is used to sort the note pitches
232 abc2svg.pitcmp = function(n1, n2) { return n1.pit - n2.pit }
233
234 // start of the Abc object
235 abc2svg.Abc = function(user) {
236 "use strict";
237
238 // constants
239 var C = abc2svg.C;
240
241 // mask some unsafe functions
242 var require = empty_function,
243 system = empty_function,
244 write = empty_function,
245 XMLHttpRequest = empty_function,
246 std = null,
247 os = null
248
249 // -- constants --
250
251 // staff system
252 var OPEN_BRACE = 0x01,
253 CLOSE_BRACE = 0x02,
254 OPEN_BRACKET = 0x04,
255 CLOSE_BRACKET = 0x08,
256 OPEN_PARENTH = 0x10,
257 CLOSE_PARENTH = 0x20,
258 STOP_BAR = 0x40,
259 FL_VOICE = 0x80,
260 OPEN_BRACE2 = 0x0100,
261 CLOSE_BRACE2 = 0x0200,
262 OPEN_BRACKET2 = 0x0400,
263 CLOSE_BRACKET2 = 0x0800,
264 MASTER_VOICE = 0x1000,
265
266 IN = 96, // resolution 96 PPI
267 CM = 37.8, // 1 inch = 2.54 centimeter
268 YSTEP // number of steps for y offsets
269
270 // error texts
271 var errs = {
272 bad_char: "Bad character '$1'",
273 bad_grace: "Bad character in grace note sequence",
274 bad_transp: "Bad transpose value",
275 bad_val: "Bad value in $1",
276 bar_grace: "Cannot have a bar in grace notes",
277 ignored: "$1: inside tune - ignored",
278 misplaced: "Misplaced '$1' in %%score",
279 must_note: "!$1! must be on a note",
280 must_note_rest: "!$1! must be on a note or a rest",
281 nonote_vo: "No note in voice overlay",
282 not_ascii: "Not an ASCII character",
283 not_enough_n: 'Not enough notes/rests for %%repeat',
284 not_enough_m: 'Not enough measures for %%repeat',
285 not_enough_p: "Not enough parameters in %%map",
286 not_in_tune: "Cannot have '$1' inside a tune",
287 notransp: "Cannot transpose with a temperament"
288 }
289
290 var self = this, // needed for modules
291 glovar = {
292 meter: {
293 type: C.METER, // meter in tune header
294 wmeasure: 1, // no M:
295 a_meter: [] // default: none
296 },
297 },
298 info = {}, // information fields
299 parse = {
300 ctx: {},
301 prefix: '%',
302 state: 0,
303 ottava: [],
304 line: new scanBuf
305 },
306 tunes = [], // first time symbol and voice array per tune for playing
307 psvg // PostScript
308
309 // utilities
310 function clone(obj, lvl) {
311 if (!obj)
312 return obj
313 var tmp = new obj.constructor
314 for (var k in obj)
315 if (obj.hasOwnProperty(k)) {
316 if (lvl && typeof obj[k] == "object")
317 tmp[k] = clone(obj[k], lvl - 1)
318 else
319 tmp[k] = obj[k]
320 }
321 return tmp
322 }
323
324 function errbld(sev, txt, fn, idx) {
325 var i, j, l, c, h
326
327 if (user.errbld) {
328 switch (sev) {
329 case 0: sev = "warn"; break
330 case 1: sev = "error"; break
331 default: sev= "fatal"; break
332 }
333 user.errbld(sev, txt, fn, idx)
334 return
335 }
336 if (idx != undefined && idx >= 0) {
337 i = l = 0
338 while (1) {
339 j = parse.file.indexOf('\n', i)
340 if (j < 0 || j > idx)
341 break
342 l++;
343 i = j + 1
344 }
345 c = idx - i
346 }
347 h = ""
348 if (fn) {
349 h = fn
350 if (l)
351 h += ":" + (l + 1) + ":" + (c + 1);
352 h += " "
353 }
354 switch (sev) {
355 case 0: h += "Warning: "; break
356 case 1: h += "Error: "; break
357 default: h += "Internal bug: "; break
358 }
359 user.errmsg(h + txt, l, c)
360 }
361
362 function error(sev, s, msg, a1, a2, a3, a4) {
363 var i, j, regex, tmp
364
365 if (sev < cfmt.quiet)
366 return
367 if (s) {
368 if (s.err) // only one error message per symbol
369 return
370 s.err = true
371 }
372 if (user.textrans) {
373 tmp = user.textrans[msg]
374 if (tmp)
375 msg = tmp
376 }
377 if (arguments.length > 3)
378 msg = msg.replace(/\$./g, function(a) {
379 switch (a) {
380 case '$1': return a1
381 case '$2': return a2
382 case '$3': return a3
383 default : return a4
384 }
385 })
386 if (s && s.fname)
387 errbld(sev, msg, s.fname, s.istart)
388 else
389 errbld(sev, msg)
390 }
391
392 // scanning functions
393 function scanBuf() {
394 // this.buffer = buffer
395 this.index = 0;
396
397 scanBuf.prototype.char = function() {
398 return this.buffer[this.index]
399 }
400 scanBuf.prototype.next_char = function() {
401 return this.buffer[++this.index]
402 }
403 scanBuf.prototype.get_int = function() {
404 var val = 0,
405 c = this.buffer[this.index]
406 while (c >= '0' && c <= '9') {
407 val = val * 10 + Number(c);
408 c = this.next_char()
409 }
410 return val
411 }
412 }
413
414 function syntax(sev, msg, a1, a2, a3, a4) {
415 var s = {
416 fname: parse.fname,
417 istart: parse.istart + parse.line.index
418 }
419
420 error(sev, s, msg, a1, a2, a3, a4)
421 }
422
423 // inject javascript code
424 function js_inject(js) {
425 eval('"use strict";\n' + js)
426 }
427 // abc2svg - deco.js - decorations
428 //
429 // Copyright (C) 2014-2025 Jean-Francois Moine
430 //
431 // This file is part of abc2svg-core.
432 //
433 // abc2svg-core is free software: you can redistribute it and/or modify
434 // it under the terms of the GNU Lesser General Public License as published by
435 // the Free Software Foundation, either version 3 of the License, or
436 // (at your option) any later version.
437 //
438 // abc2svg-core is distributed in the hope that it will be useful,
439 // but WITHOUT ANY WARRANTY; without even the implied warranty of
440 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
441 // GNU Lesser General Public License for more details.
442 //
443 // You should have received a copy of the GNU Lesser General Public License
444 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
445
446 // Decoration objects
447 // dd { // decoration definition (static)
448 // dd_en, // definition of the ending decoration
449 // dd_st, // definition of the starting decoration
450 // func, // function
451 // glyph, // glyph
452 // h, // height / ascent
453 // hd, // descent
454 // inv, // inverted glyph
455 // name, // name
456 // str, // string
457 // wl, // left width
458 // wr, // right width
459 // }
460 // de { // decoration elements (in an array - one list per music line)
461 // dd, // definition of the decoration
462 // defl { // flags
463 // noen, // no end of this decoration
464 // nost, // no start of this decoration
465 // },
466 // has_val, // defined value
467 // ix, // index of the decoration in the 'de' list
468 // lden, // end of a long decoration
469 // ldst, // start of a long decoration if true
470 // m, // note index when note decoration
471 // prev, // previous decoration (hack for 'tr~~~~~')
472 // s, // symbol
473 // start, // start of the decoration (in the ending element)
474 // up, // above the symbol
475 // val, // value
476 // x, // x offset
477 // y, // y offset
478 // }
479
480 var dd_tb = {}, // definition of the decorations
481 a_de, // array of the decoration elements
482 cross // cross voice decorations
483
484 // decorations - populate with standard decorations
485 var decos = {
486 dot: "0 stc 6 .7 1",
487 tenuto: "0 emb 6 4 3",
488 slide: "1 sld 5,5 7 1",
489 arpeggio: "2 arp 12 10 3",
490 roll: "3 roll 5,4 5 6",
491 lowermordent: "3 lmrd 6,5 4 6",
492 uppermordent: "3 umrd 6,5 4 6",
493 trill: "3 trl 14 5 8",
494 upbow: "3 upb 12,2 3 7",
495 downbow: "3 dnb 8,2 4 6",
496 gmark: "3 grm 7 4 6",
497 wedge: "0 wedge 8 1.5 1", // (staccatissimo or spiccato)
498 longphrase: "5 lphr 0 1 16",
499 mediumphrase: "5 mphr 0 1 16",
500 shortphrase: "5 sphr 0 1 16",
501 turnx: "3 turnx 7,2.5 5 6",
502 invertedturn: "3 turn 7,2 5 6",
503 // "0": "3 fng 5,5 3 3 0", // dynamic - see deco_def()
504 // "1": "3 fng 5,5 3 3 1",
505 // "2": "3 fng 5,5 3 3 2",
506 // "3": "3 fng 5,5 3 3 3",
507 // "4": "3 fng 5,5 3 3 4",
508 // "5": "3 fng 5,5 3 3 5",
509 plus: "3 dplus 8,2 2 4",
510 "+": "3 dplus 8,2 2 4",
511 ">": "5 accent 3.5,3.5 4 4",
512 accent: "5 accent 3.5,3.5 4 4",
513 emphasis: "5 accent 3.5,3.5 4 4",
514 marcato: "3 marcato 9 5 5",
515 "^": "3 marcato 9 5 5",
516 mordent: "3 lmrd 6,5 4 6",
517 open: "3 opend 8 3 3",
518 snap: "3 snap 10 3 3",
519 thumb: "3 thumb 10 3 3",
520 turn: "3 turn 7,2.5 5 6",
521 "trill(": "5 ltr 8 0 0",
522 "trill)": "5 ltr 8 0 0",
523 "8va(": "5 8va 12 6 6",
524 "8va)": "5 8va 12 6 6",
525 "8vb(": "4 8vb 10,5 6 6",
526 "8vb)": "4 8vb 10,5 6 6",
527 "15ma(": "5 15ma 12 9 9",
528 "15ma)": "5 15ma 12 9 9",
529 "15mb(": "4 15mb 12 9 9",
530 "15mb)": "4 15mb 12 9 9",
531 breath: "5 brth 0 1 16",
532 caesura: "5 caes 0 1 20",
533 short: "5 short 0 1 16",
534 tick: "5 tick 0 1 16",
535 coda: "5 coda 22,5 10 10",
536 dacapo: "5 dacs 16 20 20 Da Capo",
537 dacoda: "5 dacs 16 20 20 Da Coda",
538 "D.C.": "5 dcap 16,3 6 6",
539 "D.S.": "5 dsgn 16,3 6 6",
540 "D.C.alcoda": "5 dacs 16 32 32 D.C. al Coda",
541 "D.S.alcoda": "5 dacs 16 32 32 D.S. al Coda",
542 "D.C.alfine": "5 dacs 16 32 32 D.C. al Fine",
543 "D.S.alfine": "5 dacs 16 32 32 D.S. al Fine",
544 fermata: "5 hld 12 7.5 7.5",
545 fine: "5 dacs 16 12 12 Fine",
546 invertedfermata: "7 hld 12 8 8",
547 segno: "5 sgno 22,2 5 5",
548 f: "6 f 12,5 3 4",
549 ff: "6 ff 12,5 8 5",
550 fff: "6 fff 12,5 11 9",
551 ffff: "6 ffff 12,5 15 12",
552 mf: "6 mf 12,5 8 10",
553 mp: "6 mp 12,5 9 10",
554 p: "6 p 12,5 3 6",
555 pp: "6 pp 12,5 8 9",
556 ppp: "6 ppp 12,5 14 11",
557 pppp: "6 pppp 12,5 14 17",
558 pralltriller: "3 umrd 6,5 4 6",
559 sfz: "6 sfz 12,5 9 9",
560 ped: "6 ped 9 6 10",
561 "ped-up": "6 pedoff 9 4 4",
562 "ped(": "7 lped 14 1 1",
563 "ped)": "7 lped 14 1 1",
564 "crescendo(": "6 cresc 15,2 0 0",
565 "crescendo)": "6 cresc 15,2 0 0",
566 "<(": "6 cresc 15,2 0 0",
567 "<)": "6 cresc 15,2 0 0",
568 "diminuendo(": "6 dim 15,2 0 0",
569 "diminuendo)": "6 dim 15,2 0 0",
570 ">(": "6 dim 15,2 0 0",
571 ">)": "6 dim 15,2 0 0",
572 "-(": "8 gliss 0 0 0",
573 "-)": "8 gliss 0 0 0",
574 "~(": "8 glisq 0 0 0",
575 "~)": "8 glisq 0 0 0",
576 // internal
577 // color: "10 0 0 0 0",
578 invisible: "32 0 0 0 0",
579 beamon: "33 0 0 0 0",
580 trem1: "34 0 0 0 0",
581 trem2: "34 0 0 0 0",
582 trem3: "34 0 0 0 0",
583 trem4: "34 0 0 0 0",
584 xstem: "35 0 0 0 0",
585 beambr1: "36 0 0 0 0",
586 beambr2: "36 0 0 0 0",
587 rbstop: "37 0 0 0 0",
588 "/": "38 0 0 6 6",
589 "//": "38 0 0 6 6",
590 "///": "38 0 0 6 6",
591 "beam-accel": "39 0 0 0 0",
592 "beam-rall": "39 0 0 0 0",
593 stemless: "40 0 0 0 0",
594 rbend: "41 0 0 0 0",
595 editorial: "42 0 0 0 0",
596 "sacc-1": "3 sacc-1 6,4 4 4",
597 sacc3: "3 sacc3 6,5 4 4",
598 sacc1: "3 sacc1 6,4 4 4",
599 courtesy: "43 0 0 0 0",
600 "cacc-1": "3 cacc-1 0 0 0",
601 cacc3: "3 cacc3 0 0 0",
602 cacc1: "3 cacc1 0 0 0",
603 "tie(": "44 0 0 0 0",
604 "tie)": "44 0 0 0 0",
605 fg: "45 0 0 0 0"},
606
607 // types of decoration per function
608 f_near = [
609 d_near, // 0 - near the note
610 d_slide, // 1 - slide or tied to the note stem
611 d_arp // 2 - arpeggio
612 ],
613 f_note = [
614 null, null, null, null,
615 d_upstaff // 4 (below the staff)
616 ],
617 f_staff = [
618 null, null, null,
619 d_upstaff, // 3 - tied to note
620 null,
621 d_upstaff, // 5 (above the staff)
622 d_upstaff, // 6 - tied to staff (dynamic marks)
623 d_upstaff // 7 (below the staff)
624 ]
625
626 /* -- get the max/min vertical offset -- */
627 function y_get(st, up, x, w) {
628 var y,
629 p_staff = staff_tb[st],
630 i = (x / 2) | 0,
631 j = ((x + w) / 2) | 0
632
633 if (i < 0) {
634 i = 0
635 if (j < 0)
636 j = 0
637 }
638 if (j >= YSTEP) {
639 j = YSTEP - 1
640 if (i > j)
641 i = j
642 }
643 if (up) {
644 y = p_staff.top[i++]
645 while (i <= j) {
646 if (y < p_staff.top[i])
647 y = p_staff.top[i];
648 i++
649 }
650 } else {
651 y = p_staff.bot[i++]
652 while (i <= j) {
653 if (y > p_staff.bot[i])
654 y = p_staff.bot[i];
655 i++
656 }
657 }
658 return y
659 }
660
661 /* -- adjust the vertical offsets -- */
662 function y_set(st, up, x, w, y) {
663 var p_staff = staff_tb[st],
664 i = (x / 2) | 0,
665 j = ((x + w) / 2) | 0
666
667 /* (may occur when annotation on 'y' at start of an empty staff) */
668 if (i < 0) {
669 i = 0
670 if (j < 0)
671 j = 0
672 }
673 if (j >= YSTEP) {
674 j = YSTEP - 1
675 if (i > j)
676 i = j
677 }
678 if (up) {
679 while (i <= j) {
680 if (p_staff.top[i] < y)
681 p_staff.top[i] = y;
682 i++
683 }
684 } else {
685 while (i <= j) {
686 if (p_staff.bot[i] > y)
687 p_staff.bot[i] = y;
688 i++
689 }
690 }
691 }
692
693 // get the staff position
694 // - of the ornaments
695 function up3(s, pos) {
696 switch (pos & 0x07) {
697 case C.SL_ABOVE:
698 return 1 // true
699 case C.SL_BELOW:
700 return 0 // false
701 }
702 return s.multi > 0 || !s.second
703 } // up3()
704
705 // - of the dynamic and volume marks
706 function up6(s, pos) {
707 switch (pos & 0x07) {
708 case C.SL_ABOVE:
709 return true
710 case C.SL_BELOW:
711 return false
712 }
713 if (s.multi)
714 return s.multi > 0
715 if (!s.p_v.have_ly)
716 return false
717
718 /* above if the lyrics are below the staff */
719 return (s.pos.voc & 0x07) != C.SL_ABOVE
720 }
721
722 /* -- drawing functions -- */
723 /* 2: special case for arpeggio */
724 function d_arp(de) {
725 var m, h, dx,
726 s = de.s,
727 dd = de.dd,
728 xc = dd.wr
729
730 if (s.type == C.NOTE) {
731 for (m = 0; m <= s.nhd; m++) {
732 if (s.notes[m].acc) {
733 dx = s.notes[m].shac
734 } else {
735 dx = 1 - s.notes[m].shhd
736 switch (s.head) {
737 case C.SQUARE:
738 dx += 3.5
739 break
740 case C.OVALBARS:
741 case C.OVAL:
742 dx += 2
743 break
744 }
745 }
746 if (dx > xc)
747 xc = dx
748 }
749 }
750 h = 3 * (s.notes[s.nhd].pit - s.notes[0].pit) + 4;
751 m = dd.h /* minimum height */
752 if (h < m)
753 h = m;
754
755 de.has_val = true;
756 de.val = h;
757 // de.x = s.x - xc;
758 de.x -= xc;
759 de.y = 3 * ((s.notes[0].pit + s.notes[s.nhd].pit) / 2 - 18) - h / 2 - 3
760 }
761
762 /* 0: near the note (dot, tenuto) */
763 function d_near(de) {
764 var y,
765 up = de.up,
766 s = de.s,
767 dd = de.dd
768
769 y = up ? s.ymx : s.ymn
770 if (y > 0 && y < 24) {
771 y = (((y + 9) / 6) | 0) * 6 - 6 // between lines
772 }
773 if (up) {
774 if (s.ys > 27
775 && dd.name[0] == 'd' // if dot (staccato)
776 && s.a_dd[0].name == "dot" // as the first decoration
777 && s.stem > 0 && s.nflags >= 0
778 && s.beam_st && s.beam_end)
779 y -= 6 // put the dot a bit lower
780 else
781 y += dd.hd
782 if (s.ymx < y + dd.h)
783 s.ymx = y + dd.h
784 } else if (dd.name[0] == 'w') { // wedge (no descent)
785 de.inv = true
786 y -= dd.h
787 s.ymn = y
788 } else {
789 y -= dd.h
790 s.ymn = y - dd.hd
791 }
792 de.x -= dd.wl
793 de.y = y
794 if (s.type == C.NOTE)
795 de.x += s.notes[s.stem >= 0 ? 0 : s.nhd].shhd
796 }
797
798 /* 1: special case for slide */
799 function d_slide(de) {
800 var m, dx, xc, yc,
801 s = de.s
802
803 if (s.decstm != null) { // decoration tied to the stem
804 if (de.s.stem >= 0) {
805 if (s.nflags >= -1) {
806 xc = 3.5
807 yc = s.ys
808 if (s.nflags > 1)
809 yc -= 4 * (s.nflags - 1)
810 } else {
811 xc = 0
812 yc = s.y + 21
813 }
814 de.y = (yc + 3 * (s.notes[s.nhd].pit - 18)) / 2
815 } else {
816 de.rotpi = 1//true // rotate pi (180°)
817 if (s.nflags >= -1) {
818 xc = -3.5
819 yc = s.ys
820 if (s.nflags > 1)
821 yc += 4 * (s.nflags - 1)
822 } else {
823 xc = 0
824 yc = s.y - 21
825 }
826 de.y = (yc + 3 * (s.notes[0].pit - 18)) / 2
827 }
828 } else {
829 xc = -5
830 de.y = 3 * (s.notes[0].pit - 18)
831 if (de.dd.glyph == "sld") { // !slide!
832 xc = -10
833 for (m = 0; m <= s.nhd; m++) {
834 if (s.notes[m].acc) {
835 dx = -7 - s.notes[m].shac
836 } else {
837 dx = -10 + s.notes[m].shhd
838 switch (s.head) {
839 case C.SQUARE:
840 dx -= 3.5
841 break
842 case C.OVALBARS:
843 case C.OVAL:
844 dx -= 2
845 break
846 }
847 }
848 if (dx < xc)
849 xc = dx
850 }
851 }
852 }
853 de.x += xc
854
855 if (de.y < 0)
856 y_set(s.st, 0, de.x, de.dd.wl, de.y - de.dd.h)
857 }
858
859 // special case for long decoration
860 function d_trill(de) {
861 if (de.ldst)
862 return
863 var y, w, tmp,
864 dd = de.dd,
865 de2 = de.prev,
866 up = de.start.up,
867 s2 = de.s,
868 st = s2.st,
869 s = de.start.s,
870 x = s.x
871
872 // shift the starting point of a long decoration
873 // in the cases "T!trill(!" and "!pp!!<(!"
874 // (side effect on x)
875 function sh_st() {
876 var de3,
877 de2 = de.start, // start of the decoration
878 s = de2.s,
879 i = de2.ix // index of the current decoration
880
881 while (--i >= 0) {
882 de3 = a_de[i]
883 if (!de3 || de3.s != s)
884 break
885 }
886 while (1) { // loop on the decorations of the symbol
887 i++
888 de3 = a_de[i]
889 if (!de3 || de3.s != s)
890 break
891 if (de3 == de2)
892 continue
893 if (!(up ^ de3.up)
894 && (de3.dd.name == "trill"
895 || de3.dd.func == 6)) { // dynamic
896 x += de3.dd.wr + 2
897 break
898 }
899 }
900 } // sh_st()
901
902 // shift the ending point of a long decoration
903 // (side effect on w)
904 function sh_en() {
905 var de3,
906 i = de.ix // index of the current decoration
907
908 while (--i > 0) {
909 de3 = a_de[i]
910 if (!de3 || de3.s != s2)
911 break
912 }
913 while (1) { // loop on the decorations of the symbol
914 i++
915 de3 = a_de[i]
916 if (!de3 || de3.s != s2)
917 break
918 // if (de3 == de || de3 == de2)
919 if (de3 == de)
920 continue
921 if (!(up ^ de3.up)
922 && de3.dd.func == 6) { // if dynamic mark
923 w -= de3.dd.wl
924 break
925 }
926 }
927 } //sh_en()
928
929 // d_trill()
930 if (de2) { // same height
931 x = de2.s.x + de.dd.wl + 2
932 de2.val -= de2.dd.wr
933 if (de2.val < 8)
934 de2.val = 8
935 }
936 de.st = st
937 de.up = up
938
939 sh_st() // shift the starting point?
940
941 if (de.defl.noen) { /* if no decoration end */
942 w = de.x - x
943 if (w < 20) {
944 x = de.x - 20 - 3;
945 w = 20
946 }
947 } else {
948 w = s2.x - x - 4
949 sh_en(de) // shift the ending point?
950 if (w < 20)
951 w = 20
952 }
953 y = y_get(st, up, x - dd.wl, w)
954 if (up) {
955 tmp = staff_tb[s.st].topbar + 2
956 if (y < tmp)
957 y = tmp
958 } else {
959 tmp = staff_tb[s.st].botbar - 2
960 if (y > tmp)
961 y = tmp
962 y -= dd.h
963 }
964 if (de2) { // if same height
965 if (up) {
966 if (y < de2.y)
967 y = de2.y // (only on one note)
968 } else {
969 if (y >= de2.y) {
970 y = de2.y
971 } else {
972 do {
973 de2.y = y
974 de2 = de2.prev // go backwards
975 } while (de2)
976 }
977 }
978 }
979
980 de.lden = false;
981 de.has_val = true;
982 de.val = w;
983 de.x = x;
984 de.y = y
985 if (up)
986 y += dd.h;
987 else
988 y -= dd.hd
989 y_set(st, up, x, w, y)
990 if (up)
991 s.ymx = s2.ymx = y
992 else
993 s.ymn = s2.ymn = y
994 }
995
996 /* 3, 4, 5, 7: above (or below) the staff */
997 function d_upstaff(de) {
998
999 // don't treat here the long decorations
1000 if (de.ldst) // if long deco start
1001 return
1002 if (de.start) { // if long decoration
1003 d_trill(de)
1004 return
1005 }
1006
1007 var y, inv,
1008 up = de.up,
1009 s = de.s,
1010 dd = de.dd,
1011 x = de.x,
1012 w = dd.wl + dd.wr
1013
1014 // glyphs inside the staff
1015 switch (dd.glyph) {
1016 case "lphr":
1017 case "mphr":
1018 case "sphr":
1019 case "short":
1020 case "tick":
1021 if (s.type == C.BAR)
1022 s.invis = 1
1023 // fall thru
1024 case "brth":
1025 case "caes":
1026 y = staff_tb[s.st].topbar + 2 + dd.hd
1027 if (!s.invis) {
1028 if (dd.glyph == "brth" && y < s.ymx)
1029 y = s.ymx
1030 for (s = s.ts_next; s; s = s.ts_next)
1031 if (s.seqst)
1032 break
1033 x += ((s ? s.x : realwidth) - x) * .45
1034 }
1035 de.x = x
1036 de.y = y
1037 return
1038 }
1039
1040 if (s.nhd)
1041 x += s.notes[s.stem >= 0 ? 0 : s.nhd].shhd;
1042
1043 switch (dd.ty) {
1044 case '@':
1045 case '<':
1046 case '>':
1047 y = de.y
1048 break
1049 }
1050 if (y == undefined) {
1051 if (up) {
1052 y = y_get(s.st, true, x - dd.wl, w)
1053 + dd.hd
1054 if (de.y > y)
1055 y = de.y
1056 s.ymx = y + dd.h
1057 } else {
1058 y = y_get(s.st, false, x - dd.wl, w)
1059 - dd.h
1060 if (de.y < y)
1061 y = de.y
1062 if (dd.name == "fermata"
1063 || dd.glyph == "accent"
1064 || dd.glyph == "roll")
1065 de.inv = 1
1066 s.ymn = y - dd.hd
1067 }
1068 }
1069
1070 if (dd.wr > 5 && x > realwidth - dd.wr)
1071 de.x = x = realwidth - dd.wr
1072
1073 // if (dd.func == 6
1074 // && ((de.pos & C.SL_ALI_MSK) == C.SL_ALIGN
1075 // || ((de.pos & C.SL_ALI_MSK) == 0
1076 // && de.s.fmt.dynalign > 0))) // if align
1077 // ;
1078 // else
1079 if (up)
1080 y_set(s.st, 1, x - dd.wl, w, y + dd.h)
1081 else
1082 y_set(s.st, 0, x - dd.wl, w, y - dd.hd)
1083
1084 de.y = y
1085 }
1086
1087 // add a decoration
1088 /* syntax:
1089 * %%deco <name> <c_func> <glyph> <h> <wl> <wr> [<str>]
1090 * "<h>" may be followed by ",<hd>" (descent)
1091 */
1092 function deco_add(param) {
1093 var dv = param.match(/(\S*)\s+(.*)/);
1094 decos[dv[1]] = dv[2]
1095 }
1096
1097 // define a decoration
1098 // nm is the name of the decoration
1099 // nmd is the name of the definition in the table 'decos'
1100 function deco_def(nm, nmd) {
1101 if (!nmd)
1102 nmd = nm
1103 var a, dd, dd2, nm2, c, i, elts, str, hd,
1104 text = decos[nmd]
1105
1106 // check if a long decoration with number
1107 if (!text) {
1108 if (/\d[()]$/.test(nmd))
1109 text = decos[nmd.replace(/\d/, '')]
1110 else if (/^\d$/.test(nmd)) // or some fingering/string number
1111 text = "3 fng 5,5 3 3 " + nmd
1112 }
1113 if (!text) {
1114 if (cfmt.decoerr)
1115 error(1, null, "Unknown decoration '$1'", nm)
1116 return //undefined
1117 }
1118
1119 // extract the values
1120 a = text.match(/(\d+)\s+(.+?)\s+([0-9.,]+)\s+([0-9.]+)\s+([0-9.]+)/)
1121 if (!a) {
1122 error(1, null, "Invalid decoration '$1'", nm)
1123 return //undefined
1124 }
1125 var c_func = Number(a[1]),
1126 // glyph = a[2],
1127 h = a[3],
1128 wl = parseFloat(a[4]),
1129 wr = parseFloat(a[5])
1130
1131 if (isNaN(c_func)) {
1132 error(1, null, "%%deco: bad C function value '$1'", a[1])
1133 return //undefined
1134 }
1135 if (c_func > 10
1136 && (c_func < 32 || c_func > 45)) {
1137 error(1, null, "%%deco: bad C function index '$1'", c_func)
1138 return //undefined
1139 }
1140 // if (c_func == 5) // old !trill(!
1141 // c_func = 3
1142 // if (c_func == 7) // old !cresc(!
1143 // c_func = 6
1144
1145 if (h.indexOf(',') > 0) {
1146 h = h.split(',')
1147 hd = h[1]
1148 h = h[0]
1149 } else {
1150 hd = 0
1151 }
1152 if (h > 50 || wl > 80 || wr > 80) {
1153 error(1, null, "%%deco: abnormal h/wl/wr value '$1'", text)
1154 return //undefined
1155 }
1156
1157 // create/redefine the decoration
1158 dd = dd_tb[nm]
1159 if (!dd) {
1160 dd = {
1161 name: nm
1162 }
1163 dd_tb[nm] = dd
1164 }
1165
1166 /* set the values */
1167 dd.func = nm.indexOf("head-") == 0 ? 9 : c_func;
1168 dd.glyph = a[2];
1169 dd.h = Number(h)
1170 dd.hd = Number(hd)
1171 dd.wl = wl;
1172 dd.wr = wr;
1173 str = text.replace(a[0], '').trim()
1174 if (str) { // optional string
1175 if (str[0] == '"')
1176 str = str.slice(1, -1);
1177 if (str[0] == '@') {
1178 c = str.match(/^@([0-9.-]+),([0-9.-]+);?/)
1179 if (!c) {
1180 error(1, null, "%%deco: bad position '$1'", str)
1181 return
1182 }
1183 dd.dx = +c[1] // x and y offsets
1184 dd.dy = +c[2]
1185 str = str.replace(c[0], '')
1186 }
1187 dd.str = str
1188 }
1189
1190 /* compatibility */
1191 if (dd.func == 6 && dd.str == undefined)
1192 dd.str = nm
1193
1194 // link the start and end of long decorations
1195 c = nm.slice(-1)
1196 if (c == '(' ||
1197 (c == ')' && nm.indexOf('(') < 0)) { // not (#)
1198 dd.str = null; // (no string)
1199 nm2 = nm.slice(0, -1) + (c == '(' ? ')' : '(');
1200 dd2 = dd_tb[nm2]
1201 if (dd2) {
1202 if (c == '(') {
1203 dd.dd_en = dd2;
1204 dd2.dd_st = dd
1205 } else {
1206 dd.dd_st = dd2;
1207 dd2.dd_en = dd
1208 }
1209 }
1210 }
1211 return dd
1212 }
1213
1214 // define a cross-voice tie
1215 // @nm = decoration name
1216 // @s = note symbol
1217 // @nt1 = note
1218 function do_ctie(nm, s, nt1) {
1219 var nt2 = cross[nm],
1220 nm2 = nm.slice(0, -1) + (nm.slice(-1) == '(' ? ')' : '(')
1221
1222 if (nt2) {
1223 error(1, s, "Conflict on !$1!", nm)
1224 return
1225 }
1226 if (nt1.tie_ty) // if normal '-'
1227 curvoice.tie_s = null
1228
1229 nt1.s = s
1230 nt2 = cross[nm2]
1231 if (!nt2) {
1232 cross[nm] = nt1 // keep the start/end
1233 return
1234 }
1235 if (nm.slice(-1) == ')') {
1236 nt2 = nt1
1237 nt1 = cross[nm2]
1238 }
1239 cross[nm2] = null
1240 if (nt1.midi != nt2.midi
1241 || nt1.s.time + nt1.dur != nt2.s.time) {
1242 error(1, s, "Bad tie")
1243 } else {
1244 if (!nt1.tie_ty) // if not normal '-'
1245 nt1.tie_ty = C.SL_AUTO
1246 nt1.tie_e = nt2
1247 nt2.tie_s = nt1
1248 nt1.s.ti1 = nt2.s.ti2 = true
1249 }
1250 } // do_ctie()
1251
1252 // get/create the definition of a decoration
1253 function get_dd(nm) {
1254 var ty, p,
1255 dd = dd_tb[nm]
1256
1257 if (dd)
1258 return dd
1259 if ("<>^_@".indexOf(nm[0]) >= 0 // if position
1260 && !/^([>^]|[<>]\d?[()])$/.test(nm)) {
1261 ty = nm[0]
1262 if (ty == '@') {
1263 p = nm.match(/@([-\d]+),([-\d]+)/)
1264 if (p)
1265 ty = p[0]
1266 else
1267 ty = '' // accept decorations starting with '@'
1268 }
1269 dd = deco_def(nm, nm.replace(ty, ''))
1270 } else {
1271 dd = deco_def(nm)
1272 }
1273 if (!dd)
1274 return
1275 if (ty) {
1276 if (ty[0] == '@') { // if with x,y
1277 dd.x = Number(p[1])
1278 dd.y = Number(p[2])
1279 ty = '@'
1280 }
1281 dd.ty = ty
1282 }
1283 return dd
1284 } // get_dd()
1285
1286 /* -- convert the decorations -- */
1287 function deco_cnv(s, prev) {
1288 var i, j, dd, nm, note, s1, court, fg
1289
1290 // mark a finger glissando
1291 function sav_fg() {
1292 var i,
1293 s1 = prev
1294
1295 if (s.type != C.NOTE)
1296 return 1
1297 while (s1 && s1.type != C.NOTE)
1298 s1 = s1.prev
1299 if (!s1)
1300 return 1
1301 for (i = 0; i < s1.a_dd.length; i++) {
1302 if (s1.a_dd[i].name == dd.name) {
1303 if (!s.fg)
1304 s.fg = []
1305 s.fg.push({
1306 ty: 1, // end of glissando
1307 s: s1,
1308 nm: dd.name
1309 })
1310 if (!s1.fg)
1311 s1.fg = []
1312 s1.fg.push({
1313 ty: 0, // start of glissando
1314 s: s,
1315 nm: dd.name
1316 })
1317 return 0
1318 }
1319 }
1320 return 1
1321 } // sav_fg()
1322
1323 while (1) {
1324 nm = a_dcn.shift()
1325 if (!nm)
1326 break
1327 dd = get_dd(nm)
1328 if (!dd)
1329 continue
1330
1331 /* special decorations */
1332 switch (dd.func) {
1333 case 0: // near
1334 if (s.type == C.BAR && nm == "dot") {
1335 s.bar_dotted = true
1336 continue
1337 }
1338 // fall thru
1339 case 1: // slide & deco on stem
1340 if (dd.glyph[0] == '|')
1341 s.decstm = dd.h // deco on stem
1342 // fall thru
1343 case 2: // arp
1344 // if (s.type != C.NOTE && s.type != C.REST) {
1345 if (!s.notes) {
1346 error(1, s, errs.must_note_rest, nm)
1347 continue
1348 }
1349 break
1350 case 3:
1351 if (fg && dd.glyph == "fng") { // move the fingers out of staves
1352 for (i = 0; i <= 5; i++) {
1353 decos[i.toString()] = "5 fng 5,5 3 3 " + i
1354 if (dd_tb[i.toString()])
1355 dd_tb[i.toString()].func = 5
1356 }
1357 }
1358 break
1359 case 4: // below the staff
1360 case 5: // above the staff
1361 i = nm.match(/1?[85]([vm])([ab])([()])/)
1362 if (i) { // if ottava
1363 j = i[1] == 'v' ? 1 : 2
1364 if (i[2] == 'b')
1365 j = -j
1366 if (!s.ottava)
1367 s.ottava = []
1368 s.ottava[i[3] == '(' ? 0 : 1] = j
1369 glovar.ottava = 1 //true
1370 }
1371 break
1372 case 8: // gliss
1373 if (s.type != C.NOTE) {
1374 error(1, s, errs.must_note, nm)
1375 continue
1376 }
1377 note = s.notes[s.nhd] // move to the upper note of the chord
1378 if (!note.a_dd)
1379 note.a_dd = []
1380 note.a_dd.push(dd)
1381 continue
1382 case 9: // alternate head
1383 if (!s.notes) {
1384 error(1, s, errs.must_note_rest, nm)
1385 continue
1386 }
1387
1388 // move the alternate head of the chord to the notes
1389 for (j = 0; j <= s.nhd; j++) {
1390 note = s.notes[j]
1391 note.invis = true
1392 if (!note.a_dd)
1393 note.a_dd = []
1394 note.a_dd.push(dd)
1395 }
1396 continue
1397 case 10: /* color */
1398 if (s.notes) {
1399 for (j = 0; j <= s.nhd; j++)
1400 s.notes[j].color = nm
1401 } else {
1402 s.color = nm
1403 }
1404 break
1405 case 32: /* invisible */
1406 s.invis = true
1407 break
1408 case 33: /* beamon */
1409 if (s.type != C.BAR) {
1410 error(1, s, "!beamon! must be on a bar")
1411 continue
1412 }
1413 s.beam_on = true
1414 break
1415 case 34: /* trem1..trem4 */
1416 if (s.type != C.NOTE
1417 || !prev
1418 || prev.type != C.NOTE
1419 || s.dur != prev.dur) {
1420 error(1, s,
1421 "!$1! must be on the last of a couple of notes",
1422 nm)
1423 continue
1424 }
1425 s.trem2 = true;
1426 s.beam_end = true;
1427 s.beam_st = false;
1428 prev.beam_st = true;
1429 prev.beam_end = false;
1430 s.ntrem = prev.ntrem = Number(nm[4]);
1431 for (j = 0; j <= s.nhd; j++)
1432 s.notes[j].dur *= 2;
1433 for (j = 0; j <= prev.nhd; j++)
1434 prev.notes[j].dur *= 2
1435 break
1436 case 35: /* xstem */
1437 if (s.type != C.NOTE) {
1438 error(1, s, errs.must_note, nm)
1439 continue
1440 }
1441 s.xstem = true;
1442 break
1443 case 36: /* beambr1 / beambr2 */
1444 if (s.type != C.NOTE) {
1445 error(1, s, errs.must_note, nm)
1446 continue
1447 }
1448 if (nm[6] == '1')
1449 s.beam_br1 = true
1450 else
1451 s.beam_br2 = true
1452 break
1453 case 37: /* rbstop */
1454 s.rbstop = 1 // open
1455 break
1456 case 38: /* /, // and /// = tremolo */
1457 if (s.type != C.NOTE) {
1458 error(1, s, errs.must_note, nm)
1459 continue
1460 }
1461 s.trem1 = true;
1462 s.ntrem = nm.length /* 1, 2 or 3 */
1463 break
1464 case 39: /* beam-accel/beam-rall */
1465 if (s.type != C.NOTE) {
1466 error(1, s, errs.must_note, nm)
1467 continue
1468 }
1469 s.feathered_beam = nm[5] == 'a' ? 1 : -1;
1470 break
1471 case 40: /* stemless */
1472 s.stemless = true
1473 break
1474 case 41: /* rbend */
1475 s.rbstop = 2 // with end
1476 break
1477 case 42: // editorial
1478 if (s.type != C.NOTE) {
1479 error(1, s, errs.must_note, nm)
1480 continue
1481 }
1482 if (!s.notes[0].acc)
1483 continue
1484 nm = "sacc" + s.notes[0].acc.toString() // small accidental
1485 dd = dd_tb[nm]
1486 if (!dd) {
1487 dd = deco_def(nm)
1488 if (!dd) {
1489 error(1, s, errs.bad_val, "!editorial!")
1490 continue
1491 }
1492 }
1493 delete s.notes[0].acc
1494 curvoice.acc[s.notes[0].pit + 19] = 0 // ignore the accidental
1495 break
1496 case 43: // courtesy
1497 if (s.type != C.NOTE) {
1498 error(1, s, errs.must_note, nm)
1499 continue
1500 }
1501 j = curvoice.acc[s.notes[0].pit + 19]
1502 if (s.notes[0].acc || !j)
1503 continue
1504 court = 1 // defer
1505 break
1506 case 44: // cross-voice ties
1507 if (s.type != C.NOTE) {
1508 error(1, s, errs.must_note, nm)
1509 continue
1510 }
1511 do_ctie(nm, s, s.notes[0]) // (only one note for now)
1512 continue
1513 case 45: // finger glissando
1514 fg = 1 //true
1515 continue
1516 // default:
1517 // break
1518 }
1519
1520 // handle the fingering in case finger glissando
1521 if (fg && dd.glyph == 'fng') {
1522 fg = 0 //false
1523 if (sav_fg()) {
1524 error(1, s,
1525 "!$1! must be on the last of a couple of notes",
1526 nm)
1527 continue
1528 }
1529 }
1530
1531 // add the decoration in the symbol
1532 if (!s.a_dd)
1533 s.a_dd = []
1534 s.a_dd.push(dd)
1535 }
1536 // handle the possible courtesy accidental
1537 if (court) {
1538 a_dcn.push("cacc" + j)
1539 dh_cnv(s, s.notes[0])
1540 }
1541 }
1542
1543 // -- convert head decorations --
1544 // The decorations are in the global array a_dcn
1545 function dh_cnv(s, nt) {
1546 var k, nm, dd
1547
1548 while (1) {
1549 nm = a_dcn.shift()
1550 if (!nm)
1551 break
1552 dd = get_dd(nm)
1553 if (!dd)
1554 continue
1555
1556 switch (dd.func) {
1557 case 1: // slide
1558 case 3:
1559 case 8: // gliss
1560 break
1561 default:
1562 error(1, s, "Cannot have !$1! on a head", nm)
1563 continue
1564 case 9: // head replacement
1565 nt.invis = true
1566 break
1567 case 32: // invisible
1568 nt.invis = true
1569 continue
1570 case 10: // color
1571 nt.color = nm
1572 continue
1573 case 40: // stemless chord (abcm2ps behaviour)
1574 s.stemless = true
1575 continue
1576 case 44: // cross-voice ties
1577 do_ctie(nm, s, nt)
1578 continue
1579 }
1580
1581 // add the decoration in the note
1582 if (!nt.a_dd)
1583 nt.a_dd = []
1584 nt.a_dd.push(dd)
1585 }
1586 } // dh_cnv()
1587
1588 /* -- update the x position of a decoration -- */
1589 // used to center the rests
1590 function deco_update(s, dx) {
1591 var i, de,
1592 nd = a_de.length
1593
1594 for (i = 0; i < nd; i++) {
1595 de = a_de[i]
1596 if (de.s == s)
1597 de.x += dx
1598 }
1599 }
1600
1601 /* -- adjust the symbol width -- */
1602 function deco_width(s, wlnt) {
1603 var dd, i, w,
1604 wl = wlnt,
1605 wr = s.wr,
1606 a_dd = s.a_dd,
1607 nd = a_dd.length
1608
1609 for (i = 0; i < nd; i++) {
1610 dd = a_dd[i]
1611 switch (dd.func) {
1612 case 1: /* slide */
1613 case 2: /* arpeggio */
1614 if (wl < 12)
1615 wl = 12
1616 break
1617 case 3:
1618 switch (dd.glyph) {
1619 case "brth":
1620 case "lphr":
1621 case "mphr":
1622 case "sphr":
1623 if (s.wr < 20)
1624 s.wr = 20
1625 break
1626 default:
1627 w = dd.wl + 2
1628 if (wl < w)
1629 wl = w
1630 break
1631 }
1632 // fall thru
1633 default:
1634 switch (dd.ty) {
1635 case '<':
1636 w = wlnt + dd.wl + dd.wr + 6
1637 if (wl < w)
1638 wl = w
1639 break
1640 case '>':
1641 w = wr + dd.wl + dd.wr + 6
1642 if (s.wr < w)
1643 s.wr = w
1644 break
1645 }
1646 break
1647 }
1648 }
1649 return wl
1650 }
1651
1652 // compute the width of decorations in chord
1653 function deco_wch(nt) {
1654 var i, w, dd,
1655 wl = 0,
1656 n = nt.a_dd.length
1657
1658 for (i = 0; i < n; i++) {
1659 dd = nt.a_dd[i]
1660 w = dd.wl + dd.wr
1661 if (nt.shac)
1662 w += nt.shac
1663 if (w > wl)
1664 wl = w
1665 }
1666 return wl
1667 } // deco_wch()
1668
1669 /* -- draw the decorations -- */
1670 /* (the staves are defined) */
1671 Abc.prototype.draw_all_deco = function() {
1672 if (!a_de.length)
1673 return
1674 var de, dd, s, note, f, st, x, y, y2, ym, uf, i, str, a,
1675 new_de = [],
1676 ymid = []
1677
1678 // display a finger glissando
1679 function out_fg() {
1680 var k, l, de2, fg, fg2, x2,
1681 j = s.fg.length
1682
1683 while (--j >= 0) {
1684 fg = s.fg[j]
1685 if (fg.nm == dd.name)
1686 break
1687 }
1688 if (j < 0)
1689 return
1690
1691 if (fg.ty) { // end
1692 if (fg.ty == 1) // no start (not treated yet)
1693 out_wln(x - 19, y, 12)
1694 return
1695 }
1696
1697 x2 = x + 7 // start
1698 for (k = 0; k < a_de.length; k++) {
1699 de2 = a_de[k]
1700 if (de2.s != fg.s
1701 || de2.dd.name != dd.name)
1702 continue
1703 for (l = 0; l < de2.s.fg.length; l++) {
1704 fg2 = de2.s.fg[l]
1705 if (fg2.nm == fg.nm)
1706 break
1707 }
1708 if (fg2.nm == fg.nm) { // if same finger
1709 fg2.ty = 2 // end done
1710 xypath(x2, y + 1)
1711 output += 'l' + (de2.x - 7 - x2).toFixed(1)
1712 + ' ' + (y - de2.y
1713 - staff_tb[s.st].y).toFixed(1)
1714 + '" stroke-width=".7"/>\n'
1715 return
1716 }
1717 }
1718 out_wln(x2, y, 12) // start without end
1719 } // out_fg()
1720
1721 st = nstaff;
1722 y = staff_tb[st].y
1723 while (--st >= 0) {
1724 y2 = staff_tb[st].y;
1725 ymid[st] = (y + 24 + y2) * .5;
1726 y = y2
1727 }
1728
1729 while (1) {
1730 de = a_de.shift()
1731 if (!de)
1732 break
1733 dd = de.dd
1734 if (!dd)
1735 continue // deleted
1736
1737 if (dd.dd_en) // start of long decoration
1738 continue
1739
1740 // handle the stem direction
1741 s = de.s
1742 f = dd.glyph;
1743 i = f.indexOf('/')
1744 if (i > 0) {
1745 if (s.stem >= 0)
1746 f = f.slice(0, i)
1747 else
1748 f = f.slice(i + 1)
1749 }
1750
1751 // no voice scale if staff decoration
1752 if (f_staff[dd.func])
1753 set_sscale(s.st)
1754 else
1755 set_scale(s);
1756
1757 st = de.st;
1758 if (!staff_tb[st].topbar)
1759 continue // invisible staff
1760 x = de.x + (dd.dx || 0)
1761 y = de.y + staff_tb[st].y + (dd.dy || 0)
1762
1763 /* center the dynamic marks between two staves */
1764 /*fixme: KO when deco on other voice and same direction*/
1765 if (dd.func == 6
1766 && ((de.pos & C.SL_ALI_MSK) == C.SL_CENTER
1767 || ((de.pos & C.SL_ALI_MSK) == 0
1768 && !s.fmt.dynalign))
1769 && ((de.up && st > 0)
1770 || (!de.up && st < nstaff))) {
1771 if (de.up)
1772 ym = ymid[--st]
1773 else
1774 ym = ymid[st++];
1775 ym -= dd.h * .5
1776 if ((de.up && y < ym)
1777 || (!de.up && y > ym)) {
1778 // if (s.st > st) {
1779 // while (s.st != st)
1780 // s = s.ts_prev
1781 // } else if (s.st < st) {
1782 // while (s.st != st)
1783 // s = s.ts_next
1784 // }
1785 y2 = y_get(st, !de.up, de.x, de.val)
1786 + staff_tb[st].y
1787 if (de.up)
1788 y2 -= dd.h
1789 //fixme: y_set is not used later!
1790 if ((de.up && y2 > ym)
1791 || (!de.up && y2 < ym)) {
1792 y = ym;
1793 // y_set(st, de.up, de.x, de.val,
1794 // (de.up ? y + dd.h : y)
1795 // - staff_tb[st].y)
1796 if (stv_g.scale != 1)
1797 y += stv_g.dy / 2
1798 }
1799 }
1800 }
1801
1802 // check if user JS decoration
1803 if (user.deco) {
1804 uf = user.deco[f]
1805 if (uf && typeof(uf) == "function") {
1806 uf.call(self, x, y, de)
1807 continue
1808 }
1809 }
1810
1811 // check if user PS definition
1812 if (self.psdeco(x, y, de))
1813 continue
1814
1815 anno_start(s, 'deco')
1816 // if (de.flags.grace) {
1817 // g_open(x, y, 0, .7, de.inv ? -.7 : 0);
1818 // x = y = 0
1819 // } else
1820 if (de.inv) {
1821 y = y + dd.h - dd.hd
1822 g_open(x, y, 0, 1, -1);
1823 x = y = 0
1824 } else if (de.rotpi) {
1825 g_open(x, y, 180)
1826 x = y = 0
1827 }
1828 if (de.has_val) {
1829 if (dd.func != 2 // if not !arpeggio!
1830 || stv_g.st < 0) // or not staff scale
1831 // || voice_tb[s.v].scale != 1)
1832 out_deco_val(x, y, f, de.val / stv_g.scale, de.defl)
1833 else
1834 out_deco_val(x, y, f, de.val, de.defl)
1835 if (de.cont)
1836 new_de.push(de.start) // to be continued next line
1837 } else if (dd.str != undefined // string
1838 && !tgls[dd.glyph]
1839 && !glyphs[dd.glyph]) { // with a class
1840 if (s.fg) // if finger glissando
1841 out_fg() // (may change y)
1842 out_deco_str(x, y, // - dd.h * .2,
1843 de)
1844 } else if (de.lden) {
1845 out_deco_long(x, y, de)
1846 } else {
1847 xygl(x, y, f)
1848 }
1849 if (stv_g.g)
1850 g_close();
1851 anno_stop(s, 'deco')
1852 }
1853
1854 // keep the long decorations which continue on the next line
1855 a_de = new_de
1856 }
1857
1858 /* -- create the decorations and define the ones near the notes -- */
1859 /* (the staves are not yet defined) */
1860 /* (delayed output) */
1861 /* this function must be called first as it builds the deco element table */
1862 function draw_deco_near() {
1863 var s, g
1864
1865 // update starting old decorations
1866 function ldeco_update(s) {
1867 var i, de,
1868 // x = s.ts_prev.x + s.ts_prev.wr
1869 x = s.x - s.wl,
1870 nd = a_de.length
1871
1872 for (i = 0; i < nd; i++) {
1873 de = a_de[i];
1874 de.ix = i;
1875 de.s.x = de.x = x;
1876 de.defl.nost = true
1877 }
1878 }
1879
1880 /* -- create the deco elements, and treat the near ones -- */
1881 function create_deco(s) {
1882 var dd, k, pos, de, x, y, up,
1883 nd = s.a_dd.length
1884
1885 if (s.y == undefined)
1886 s.y = 0 // (no y in measure bars)
1887
1888 /*fixme:pb with decorations above the staff*/
1889 for (k = 0; k < nd; k++) {
1890 dd = s.a_dd[k]
1891
1892 // adjust the position
1893 x = s.x
1894 y = s.y
1895 switch (dd.func) {
1896 default:
1897 if (dd.func >= 10)
1898 continue
1899 pos = 0
1900 break
1901 case 3: /* d_upstaff */
1902 case 4:
1903 case 5: // after slurs
1904 pos = s.pos.orn
1905 break
1906 case 6: /* dynamic */
1907 pos = s.pos.dyn
1908 break
1909 }
1910
1911 switch (dd.ty) { // explicit position
1912 case '^':
1913 pos = (pos & ~0x07) | C.SL_ABOVE
1914 break
1915 case '_':
1916 pos = (pos & ~0x07) | C.SL_BELOW
1917 break
1918 case '<':
1919 case '>':
1920 pos = (pos & 0x07) | C.SL_CLOSE
1921 if (dd.ty == '<') {
1922 x -= dd.wr + 8
1923 if (s.notes[0].acc)
1924 x -= 8
1925 } else {
1926 x += dd.wl + 8
1927 }
1928 y = 3 * (s.notes[0].pit - 18)
1929 - (dd.h - dd.hd) / 2
1930 break
1931 case '@':
1932 x += dd.x
1933 y += dd.y
1934 break
1935 }
1936
1937 if ((pos & 0x07) == C.SL_HIDDEN)
1938 continue
1939
1940 de = {
1941 s: s,
1942 dd: dd,
1943 st: s.st,
1944 ix: a_de.length,
1945 defl: {},
1946 x: x,
1947 y: y
1948 }
1949 if (pos)
1950 de.pos = pos
1951
1952 up = 0 //false
1953 if (dd.ty == '^') {
1954 up = 1 //true
1955 } else if (dd.ty == '_') {
1956 ;
1957 } else {
1958 switch (dd.func) {
1959 case 0:
1960 if (s.multi)
1961 up = s.multi > 0
1962 else
1963 up = s.stem < 0
1964 break
1965 case 3:
1966 case 5:
1967 up = up3(s, pos)
1968 break
1969 case 6:
1970 up = up6(s, pos)
1971 break
1972 }
1973 }
1974 de.up = up
1975
1976 if (dd.name.indexOf("inverted") >= 0)
1977 de.inv = 1
1978 if (s.type == C.BAR && !dd.ty)
1979 de.x -= s.wl / 2 - 2
1980 a_de.push(de)
1981 if (dd.dd_en) {
1982 de.ldst = true
1983 } else if (dd.dd_st) {
1984 //fixme: pb with "()"
1985 de.lden = true;
1986 de.defl.nost = true
1987 }
1988
1989 if (f_near[dd.func])
1990 f_near[dd.func](de)
1991 }
1992 } // create_deco()
1993
1994 // create the decorations of note heads
1995 function create_dh(s, m) {
1996 var de, k, dd,
1997 note = s.notes[m],
1998 nd = note.a_dd.length,
1999 x = s.x
2000
2001 for (k = 0; k < nd; k++) {
2002 dd = note.a_dd[k]
2003
2004 //fixme: check if hidden?
2005 de = {
2006 s: s,
2007 dd: dd,
2008 st: s.st,
2009 m: m,
2010 ix: 0,
2011 defl: {},
2012 x: x,
2013 y: 3 * (note.pit - 18) - (dd.h - dd.hd) / 2
2014 // dy: 0
2015 }
2016
2017 if (dd.ty) { // if explicit position
2018 if (dd.ty == '@') {
2019 de.x += dd.x
2020 de.y += dd.y
2021 } else {
2022 de.y -= (dd.h - dd.hd) / 2 // center
2023 if (dd.ty == '<') {
2024 de.x -= dd.wr + 8
2025 if (s.notes[m].acc)
2026 x -= 8
2027 } else if (dd.ty == '>') {
2028 de.x += dd.wl + 8
2029 }
2030 }
2031 } else {
2032 if (note.shhd)
2033 de.x += note.shhd * stv_g.scale
2034 if (note.shac)
2035 de.x -= note.shac
2036 if (dd.func != 8) // if not glissendo
2037 de.x -= dd.wl + dd.wr + 3
2038 }
2039
2040 a_de.push(de)
2041 if (dd.dd_en) {
2042 de.ldst = true
2043 } else if (dd.dd_st) {
2044 de.lden = true;
2045 de.defl.nost = true
2046 }
2047 }
2048 } // create_dh()
2049
2050 // create all decorations of a note (chord and heads)
2051 function create_all(s) {
2052 if (s.invis && s.play) // play sequence: no decoration
2053 return
2054 if (s.a_dd)
2055 create_deco(s)
2056 if (s.notes) {
2057 for (var m = 0; m < s.notes.length; m++) {
2058 if (s.notes[m].a_dd)
2059 create_dh(s, m)
2060 }
2061 }
2062 } // create_all()
2063
2064 // link the long decorations
2065 function ll_deco() {
2066 var i, j, de, de2, de3, dd, dd2, v, s, st,
2067 n_de = a_de.length
2068
2069 // add ending decorations
2070 for (i = 0; i < n_de; i++) {
2071 de = a_de[i]
2072 if (!de.ldst) // not the start of long decoration
2073 continue
2074 dd = de.dd;
2075 dd2 = dd.dd_en;
2076 s = de.s;
2077 v = s.v // search later in the voice
2078 for (j = i + 1; j < n_de; j++) {
2079 de2 = a_de[j]
2080 if (!de2.start
2081 && de2.dd == dd2 && de2.s.v == v)
2082 break
2083 }
2084 if (j == n_de) { // no end, search in the staff
2085 st = s.st;
2086 for (j = i + 1; j < n_de; j++) {
2087 de2 = a_de[j]
2088 if (!de2.start
2089 && de2.dd == dd2 && de2.s.st == st)
2090 break
2091 }
2092 }
2093 if (j == n_de) { // no end, insert one
2094 de2 = {
2095 s: s,
2096 st: de.st,
2097 dd: dd2,
2098 ix: a_de.length - 1,
2099 x: realwidth - 6,
2100 y: s.y,
2101 cont: true, // keep for next line
2102 lden: true,
2103 defl: {
2104 noen: true
2105 }
2106 }
2107 if (de2.x < s.x + 10)
2108 de2.x = s.x + 10
2109 if (de.m != undefined)
2110 de2.m = de.m;
2111 a_de.push(de2)
2112 }
2113 de2.start = de;
2114 de2.defl.nost = de.defl.nost
2115
2116 // handle same decoration ending at a same time
2117 j = i
2118 while (--j >= 0) {
2119 de3 = a_de[j]
2120 if (!de3.start)
2121 continue
2122 if (de3.s.time < s.time)
2123 break
2124 if (de3.dd.name == de2.dd.name) {
2125 de2.prev = de3
2126 break
2127 }
2128 }
2129 }
2130
2131 // add starting decorations
2132 for (i = 0; i < n_de; i++) {
2133 de2 = a_de[i]
2134 if (!de2.lden // not the end of long decoration
2135 || de2.start) // start already found
2136 continue
2137 s = de2.s;
2138 de = {
2139 s: prev_scut(s),
2140 st: de2.st,
2141 dd: de2.dd.dd_st,
2142 ix: a_de.length - 1,
2143 // x: s.x - s.wl - 4,
2144 y: s.y,
2145 ldst: true
2146 }
2147 de.x = de.s.x + de.s.wr
2148 if (de2.m != undefined)
2149 de.m = de2.m;
2150 a_de.push(de);
2151 de2.start = de
2152 }
2153 } // ll_deco
2154
2155 // update the long decorations started in the previous line
2156 for (s = tsfirst ; s; s = s.ts_next) {
2157 switch (s.type) {
2158 case C.CLEF:
2159 case C.KEY:
2160 case C.METER:
2161 continue
2162 }
2163 break
2164 }
2165 if (a_de.length)
2166 ldeco_update(s)
2167
2168 for ( ; s; s = s.ts_next) {
2169 switch (s.type) {
2170 case C.BAR:
2171 case C.MREST:
2172 case C.NOTE:
2173 case C.REST:
2174 case C.SPACE:
2175 break
2176 case C.GRACE:
2177 for (g = s.extra; g; g = g.next)
2178 create_all(g)
2179 break
2180 default:
2181 continue
2182 }
2183 create_all(s)
2184 }
2185 ll_deco() // link the long decorations
2186 }
2187
2188 /* -- define the decorations tied to a note -- */
2189 /* (the staves are not yet defined) */
2190 /* (delayed output) */
2191 function draw_deco_note() {
2192 var i, de, dd, f,
2193 nd = a_de.length
2194
2195 for (i = 0; i < nd; i++) {
2196 de = a_de[i];
2197 dd = de.dd;
2198 f = dd.func
2199 if (f_note[f]
2200 && de.m == undefined)
2201 f_note[f](de)
2202 }
2203 }
2204
2205 // -- define the music elements tied to the staff --
2206 // - decoration tied to the staves
2207 // - chord symbols
2208 // - repeat brackets
2209 /* (the staves are not yet defined) */
2210 /* (unscaled delayed output) */
2211 function draw_deco_staff() {
2212 var s, p_voice, y, i, v, de, dd, w,
2213 minmax = new Array(nstaff + 1),
2214 nd = a_de.length
2215
2216 /* draw the repeat brackets */
2217 function draw_repbra(p_voice) {
2218 var s, s1, x, y, y2, i, p, w, wh, first_repeat;
2219
2220 // search the max y offset of the line
2221 y = staff_tb[p_voice.st].topbar + 15 // 10 (vert bar) + 5 (room)
2222 for (s = p_voice.sym; s; s = s.next) {
2223 if (s.type != C.BAR)
2224 continue
2225 if (!s.rbstart || s.norepbra)
2226 continue
2227 /*fixme: line cut on repeat!*/
2228 if (!s.next)
2229 break
2230 if (!first_repeat) {
2231 first_repeat = s;
2232 set_font("repeat")
2233 }
2234 s1 = s
2235 for (;;) {
2236 if (!s.next)
2237 break
2238 s = s.next
2239 if (s.rbstop)
2240 break
2241 }
2242 x = s1.x
2243 if (s1.xsh) // volta shift
2244 x += s1.xsh
2245 y2 = y_get(p_voice.st, true, x, s.x - x) + 2
2246 if (y < y2)
2247 y = y2
2248
2249 // have room for the vertical lines and the repeat numbers
2250 if (s1.rbstart == 2) {
2251 y2 = y_get(p_voice.st, true, x, 3) + 10
2252 if (y < y2)
2253 y = y2
2254 }
2255 if (s.rbstop == 2) {
2256 y2 = y_get(p_voice.st, true, s.x - 3, 3) + 10
2257 if (y < y2)
2258 y = y2
2259 }
2260 if (s1.text) {
2261 wh = strwh(s1.text);
2262 y2 = y_get(p_voice.st, true, x + 4, wh[0]) +
2263 wh[1]
2264 if (y < y2)
2265 y = y2
2266 }
2267 if (s.rbstart)
2268 s = s.prev
2269 }
2270
2271 /* draw the repeat indications */
2272 s = first_repeat
2273 if (!s)
2274 return
2275 set_dscale(p_voice.st, true);
2276 y2 = y * staff_tb[p_voice.st].staffscale
2277 for ( ; s; s = s.next) {
2278 if (!s.rbstart || s.norepbra)
2279 continue
2280 s1 = s
2281 while (1) {
2282 if (!s.next)
2283 break
2284 s = s.next
2285 if (s.rbstop)
2286 break
2287 }
2288 if (s1 == s)
2289 break
2290 x = s1.x
2291 if (s1.xsh) // volta shift
2292 x += s1.xsh
2293 if (cfmt.measurenb > 0 & s.bar_num
2294 && s.bar_num % cfmt.measurenb)
2295 x += 6
2296 if (s.type != C.BAR) {
2297 w = s.rbstop ? 0 : s.x - realwidth + 4
2298 } else if ((s.bar_type.length > 1 // if complex bar
2299 && s.bar_type != "[]")
2300 || s.bar_type == "]") {
2301 // if (s.bar_type == "]")
2302 // s.invis = true
2303 //fixme:%%staves: cur_sy moved?
2304 if (s1.st > 0
2305 && !(cur_sy.staves[s1.st - 1].flags & STOP_BAR))
2306 w = s.wl
2307 else if (s.bar_type.slice(-1) == ':')
2308 w = 12
2309 else if (s.bar_type[0] != ':')
2310 // || s.bar_type == "]")
2311 w = 0 /* explicit repeat end */
2312 else
2313 w = 8
2314 } else {
2315 w = (s.rbstop && !s.rbstart) ? 0 : 8
2316 }
2317 w = (s.x - x - w) // / staff_tb[p_voice.st].staffscale;
2318
2319 if (!s.next // 2nd ending at end of line
2320 && !s.rbstop
2321 && !p_voice.bar_start) { // continue on next line
2322 p_voice.bar_start = _bar(s)
2323 p_voice.bar_start.bar_type = ""
2324 p_voice.bar_start.rbstart = 1
2325 }
2326 if (s1.text)
2327 xy_str(x + 4, y2 - gene.curfont.size,
2328 s1.text);
2329 xypath(x, y2);
2330 if (s1.rbstart == 2)
2331 output += 'm0 10v-10';
2332 output+= 'h' + w.toFixed(1)
2333 if (s.rbstop == 2)
2334 output += 'v10';
2335 output += '"/>\n';
2336 y_set(s1.st, true, x, w, y + 2)
2337
2338 if (s.rbstart)
2339 s = s.prev
2340 }
2341 } // draw_repbra()
2342
2343 /* create the decorations tied to the staves */
2344 for (i = 0; i <= nstaff; i++)
2345 minmax[i] = {
2346 ymin: 0,
2347 ymax: 0
2348 }
2349 for (i = 0; i < nd; i++) {
2350 de = a_de[i];
2351 dd = de.dd
2352 if (!dd) // if error
2353 continue
2354 if (!f_staff[dd.func] /* if not tied to the staff */
2355 || de.m != undefined // or head decoration
2356 || dd.ty == '<' || dd.ty == '>' || dd.ty == '@')
2357 continue
2358
2359 f_staff[dd.func](de)
2360 if (dd.func != 6
2361 || dd.dd_en) // if start
2362 continue
2363
2364 if ((de.pos & C.SL_ALI_MSK) == C.SL_ALIGN
2365 || ((de.pos & C.SL_ALI_MSK) == 0
2366 && de.s.fmt.dynalign > 0)) { // if align
2367 if (de.up) {
2368 if (de.y > minmax[de.st].ymax)
2369 minmax[de.st].ymax = de.y
2370 } else {
2371 if (de.y < minmax[de.st].ymin)
2372 minmax[de.st].ymin = de.y
2373 }
2374 }
2375 }
2376
2377 // set the same vertical offset of the dynamic marks
2378 for (i = 0; i < nd; i++) {
2379 de = a_de[i];
2380 dd = de.dd
2381 if (!dd) // if error
2382 continue
2383
2384 // if @x,y offsets, update the top and bottom of the staff
2385 if (dd.ty == '@') {
2386 var y2
2387
2388 y = de.y
2389 if (y > 0) {
2390 y2 = y + dd.h + 2
2391 if (y2 > staff_tb[de.st].ann_top)
2392 staff_tb[de.st].ann_top = y2
2393 } else {
2394 y2 = y - dd.hd - 2
2395 if (y2 < staff_tb[de.st].ann_bot)
2396 staff_tb[de.st].ann_bot = y2
2397
2398 }
2399 continue
2400 }
2401 if (dd.func != 6
2402 || dd.ty == '<' || dd.ty == '>'
2403 || dd.dd_en) // if start
2404 continue
2405
2406 w = de.val || (dd.wl + dd.wr)
2407 if ((de.pos & C.SL_ALI_MSK) == C.SL_ALIGN
2408 || ((de.pos & C.SL_ALI_MSK) == 0
2409 && de.s.fmt.dynalign > 0)) { // if align
2410 if (de.up)
2411 y = minmax[de.st].ymax
2412 else
2413 y = minmax[de.st].ymin;
2414 de.y = y
2415 } else {
2416 y = de.y
2417 }
2418 if (de.up)
2419 y += dd.h;
2420 else
2421 y -= dd.hd
2422 y_set(de.st, de.up, de.x, w, y)
2423 }
2424
2425 // second pass for pedal (under the staff)
2426 for (i = 0; i < nd; i++) {
2427 de = a_de[i]
2428 dd = de.dd
2429 if (!dd) // if error
2430 continue
2431 if (dd.dd_en // if start
2432 || dd.name.slice(0, 3) != "ped")
2433 continue
2434 w = de.val || 10
2435 de.y = y_get(de.st, 0, de.x, w)
2436 - (dd.dd_st && cfmt.pedline ? 10 : dd.h)
2437 y_set(de.st, 0, de.x, w, de.y) // (no descent)
2438 }
2439
2440 draw_all_chsy() // draw all chord symbols
2441
2442 /* draw the repeat brackets */
2443 for (v = 0; v < voice_tb.length; v++) {
2444 p_voice = voice_tb[v]
2445 if (p_voice.second || !p_voice.sym || p_voice.ignore)
2446 continue
2447 draw_repbra(p_voice)
2448 }
2449 }
2450
2451 /* -- draw the measure bar numbers -- */
2452 /* (scaled delayed output) */
2453 function draw_measnb() {
2454 var s, st, bar_num, x, y, w, any_nb, font_size, w0,
2455 sy = cur_sy
2456
2457 /* search the top staff */
2458 for (st = 0; st <= nstaff; st++) {
2459 if (sy.st_print[st])
2460 break
2461 }
2462 if (st > nstaff)
2463 return /* no visible staff */
2464 set_dscale(st)
2465
2466 /* leave the measure numbers as unscaled */
2467 if (staff_tb[st].staffscale != 1) {
2468 font_size = get_font("measure").size;
2469 param_set_font("measurefont", "* " +
2470 (font_size / staff_tb[st].staffscale).toString())
2471 }
2472 set_font("measure");
2473 w0 = cwidf('0'); // (greatest) width of a number
2474
2475 s = tsfirst; /* clef */
2476 bar_num = gene.nbar
2477 if (bar_num > 1) {
2478 if (cfmt.measurenb == 0) {
2479 any_nb = true;
2480 y = y_get(st, true, 0, 20)
2481 if (y < staff_tb[st].topbar + 14)
2482 y = staff_tb[st].topbar + 14;
2483 xy_str(0, y - gene.curfont.size * .2, bar_num.toString())
2484 y_set(st, true, 0, 20, y + gene.curfont.size + 2)
2485 } else if (bar_num % cfmt.measurenb == 0) {
2486 for ( ; ; s = s.ts_next) {
2487 switch (s.type) {
2488 case C.CLEF:
2489 case C.KEY:
2490 case C.METER:
2491 case C.STBRK:
2492 continue
2493 }
2494 break
2495 }
2496
2497 // don't display the number twice
2498 if (s.type != C.BAR || !s.bar_num) {
2499 any_nb = true;
2500 w = w0
2501 if (bar_num >= 10)
2502 w *= bar_num >= 100 ? 3 : 2
2503 if (gene.curfont.pad)
2504 w += gene.curfont.pad * 2
2505 x = (s.prev
2506 ? s.prev.x + s.prev.wr / 2
2507 : s.x - s.wl) - w
2508 y = y_get(st, true, x, w) + 5
2509 if (y < staff_tb[st].topbar + 6)
2510 y = staff_tb[st].topbar + 6;
2511 y += gene.curfont.pad
2512 xy_str(x, y - gene.curfont.size * .2, bar_num.toString())
2513 y += gene.curfont.size + gene.curfont.pad
2514 y_set(st, true, x, w, y);
2515 // s.ymx = y
2516 }
2517 }
2518 }
2519
2520 for ( ; s; s = s.ts_next) {
2521 switch (s.type) {
2522 case C.STAVES:
2523 sy = s.sy
2524 for (st = 0; st < nstaff; st++) {
2525 if (sy.st_print[st])
2526 break
2527 }
2528 set_dscale(st)
2529 continue
2530 default:
2531 continue
2532 case C.BAR:
2533 if (!s.bar_num || s.bar_num <= 1)
2534 continue
2535 break
2536 }
2537
2538 bar_num = s.bar_num
2539 if (cfmt.measurenb == 0
2540 || (bar_num % cfmt.measurenb) != 0
2541 || !s.next
2542 || s.bar_mrep)
2543 continue
2544 if (!any_nb)
2545 any_nb = true;
2546 w = w0
2547 if (bar_num >= 10)
2548 w *= bar_num >= 100 ? 3 : 2
2549 if (gene.curfont.pad)
2550 w += gene.curfont.pad * 2
2551 x = s.x
2552 y = y_get(st, true, x, w)
2553 if (y < staff_tb[st].topbar + 6)
2554 y = staff_tb[st].topbar + 6
2555 if (s.next.type == C.NOTE) {
2556 if (s.next.stem > 0) {
2557 if (y < s.next.ys - gene.curfont.size)
2558 y = s.next.ys - gene.curfont.size
2559 } else {
2560 if (y < s.next.y)
2561 y = s.next.y
2562 }
2563 }
2564 y += 2 + gene.curfont.pad
2565 xy_str(x, y - gene.curfont.size * .2, bar_num.toString())
2566 y += gene.curfont.size + gene.curfont.pad
2567 y_set(st, true, x, w, y);
2568 // s.ymx = y
2569 }
2570 gene.nbar = bar_num
2571
2572 if (font_size)
2573 param_set_font("measurefont", "* " + font_size.toString());
2574 }
2575
2576 /* -- draw the parts and the tempo information -- */
2577 // (unscaled delayed output)
2578 function draw_partempo() {
2579 var s, s2, some_part, some_tempo, h, w, y, st, p,
2580 sy = cur_sy
2581
2582 // search the top staff
2583 for (st = 0; st <= nstaff; st++) {
2584 if (sy.st_print[st])
2585 break
2586 }
2587 if (st > nstaff)
2588 return // no visible staff
2589 set_dscale(st, 1) // no scale
2590
2591 /* get the minimal y offset */
2592 var ymin = staff_tb[st].topbar + 2,
2593 dosh = 0,
2594 shift = 1,
2595 x = -100 // (must be negative for %%soloffs)
2596
2597 // output the parts
2598 for (s = tsfirst; s; s = s.ts_next) {
2599 s2 = s.part
2600 if (!s2 || s2.invis)
2601 continue
2602 if (!some_part) {
2603 some_part = s;
2604 set_font("parts");
2605 h = gene.curfont.size + 2 +
2606 gene.curfont.pad * 2
2607 }
2608 if (s2.x == undefined)
2609 s2.x = s.x - 10
2610 p = s2.text
2611 if (cfmt.partname)
2612 s2.ntxt = p = partname(p)[2]
2613 w = strwh(p)[0]
2614 y = y_get(st, true, s2.x, w + 3)
2615 if (ymin < y)
2616 ymin = y
2617 }
2618 if (some_part) {
2619 set_sscale(-1)
2620 ymin *= staff_tb[st].staffscale
2621 for (s = some_part; s; s = s.ts_next) {
2622 s2 = s.part
2623 if (!s2 || s2.invis)
2624 continue
2625 p = s2.ntxt || s2.text
2626 w = strwh(p)[0]
2627 if (user.anno_start || user.anno_stop) {
2628 s2.wl = 0
2629 s2.wr = w
2630 s2.ymn = ymin
2631 s2.ymx = s2.ymn + h
2632 anno_start(s2)
2633 }
2634 xy_str(s2.x,
2635 ymin + gene.curfont.pad + gene.curfont.size * .22,
2636 p)
2637 y_set(st, 1, s2.x, w + 3,
2638 (ymin + h) / staff_tb[st].staffscale)
2639 anno_stop(s2)
2640 }
2641 }
2642
2643 // output the tempos
2644 ymin = staff_tb[st].topbar + 6
2645 for (s = tsfirst; s; s = s.ts_next) {
2646 if (s.type != C.TEMPO || s.invis)
2647 continue
2648 if (!some_tempo)
2649 some_tempo = s
2650 w = s.tempo_wh[0]
2651 // if (s.time == 0 && s.x > 40) // at start of tune and no %%soloffs,
2652 // s.x = 40 // shift the tempo over the key signature
2653 y = y_get(st, true, s.x - 16, w)
2654 if (y > ymin)
2655 ymin = y
2656 if (x >= s.x - 16 && !(dosh & (shift >> 1)))
2657 dosh |= shift
2658 shift <<= 1
2659 x = s.x - 16 + w
2660 }
2661 if (some_tempo) {
2662 set_sscale(-1)
2663 set_font("tempo")
2664 ymin += 2
2665 ymin *= staff_tb[st].staffscale
2666
2667 /* draw the tempo indications */
2668 for (s = some_tempo; s; s = s.ts_next) {
2669 if (s.type != C.TEMPO
2670 || s.invis) // (displayed by %%titleformat)
2671 continue
2672 w = s.tempo_wh[0]
2673 h = s.tempo_wh[1]
2674 y = ymin
2675 if (dosh & 1)
2676 y += h
2677 if (user.anno_start || user.anno_stop) {
2678 s.wl = 16
2679 // s.wr = 30
2680 s.wr = w - 16
2681 s.ymn = y
2682 s.ymx = s.ymn + 14
2683 anno_start(s)
2684 }
2685 writempo(s, s.x - 16, y)
2686 anno_stop(s)
2687 y_set(st, 1, s.x - 16, w,
2688 (y + h + 2) / staff_tb[st].staffscale)
2689 dosh >>= 1
2690 }
2691 }
2692 }
2693 // abc2svg - draw.js - draw functions
2694 //
2695 // Copyright (C) 2014-2025 Jean-Francois Moine
2696 //
2697 // This file is part of abc2svg-core.
2698 //
2699 // abc2svg-core is free software: you can redistribute it and/or modify
2700 // it under the terms of the GNU Lesser General Public License as published by
2701 // the Free Software Foundation, either version 3 of the License, or
2702 // (at your option) any later version.
2703 //
2704 // abc2svg-core is distributed in the hope that it will be useful,
2705 // but WITHOUT ANY WARRANTY; without even the implied warranty of
2706 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2707 // GNU Lesser General Public License for more details.
2708 //
2709 // You should have received a copy of the GNU Lesser General Public License
2710 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
2711
2712 // constants
2713 var STEM_MIN = 16, /* min stem height under beams */
2714 STEM_MIN2 = 14, /* ... for notes with two beams */
2715 STEM_MIN3 = 12, /* ... for notes with three beams */
2716 STEM_MIN4 = 10, /* ... for notes with four beams */
2717 STEM_CH_MIN = 14, /* min stem height for chords under beams */
2718 STEM_CH_MIN2 = 10, /* ... for notes with two beams */
2719 STEM_CH_MIN3 = 9, /* ... for notes with three beams */
2720 STEM_CH_MIN4 = 9, /* ... for notes with four beams */
2721 BEAM_DEPTH = 3.2, /* width of a beam stroke */
2722 BEAM_OFFSET = .25, /* pos of flat beam relative to staff line */
2723 BEAM_SHIFT = 5, /* shift of second and third beams */
2724 BEAM_STUB = 7, /* length of stub for flag under beam */
2725 SLUR_SLOPE = .7, // max slope of a slur
2726 GSTEM = 15, /* grace note stem length */
2727 GSTEM_XOFF = 2.3 /* x offset for grace note stem */
2728
2729 var cache,
2730 anno_a = [] // symbols with annotations
2731
2732 /* -- compute the best vertical offset for the beams -- */
2733 function b_pos(grace, stem, nflags, b) {
2734 var top, bot, d1, d2,
2735 shift = !grace ? BEAM_SHIFT : 3.5,
2736 depth = !grace ? BEAM_DEPTH : 1.8
2737
2738 /* -- up/down shift needed to get k*6 -- */
2739 function rnd6(y) {
2740 var iy = Math.round((y + 12) / 6) * 6 - 12
2741 return iy - y
2742 } // rnd6()
2743
2744 if (stem > 0) {
2745 bot = b - (nflags - 1) * shift - depth
2746 if (bot > 26)
2747 return 0
2748 top = b
2749 } else {
2750 top = b + (nflags - 1) * shift + depth
2751 if (top < -2)
2752 return 0
2753 bot = b
2754 }
2755
2756 d1 = rnd6(top - BEAM_OFFSET);
2757 d2 = rnd6(bot + BEAM_OFFSET)
2758 return d1 * d1 > d2 * d2 ? d2 : d1
2759 }
2760
2761 /* duplicate a note for beaming continuation */
2762 function sym_dup(s) {
2763 var m, note
2764
2765 s = clone(s)
2766 s.invis = true
2767 delete s.extra;
2768 delete s.text
2769 delete s.a_gch
2770 delete s.a_ly
2771 delete s.a_dd;
2772 delete s.tp
2773 s.notes = clone(s.notes)
2774 for (m = 0; m <= s.nhd; m++) {
2775 note = s.notes[m] = clone(s.notes[m])
2776 delete note.a_dd
2777 }
2778 return s
2779 }
2780
2781 /* -- calculate a beam -- */
2782 /* (the staves may be defined or not) */
2783 var min_tb = [
2784 [STEM_MIN, STEM_MIN,
2785 STEM_MIN2, STEM_MIN3, STEM_MIN4, STEM_MIN4],
2786 [STEM_CH_MIN, STEM_CH_MIN,
2787 STEM_CH_MIN2, STEM_CH_MIN3, STEM_CH_MIN4, STEM_CH_MIN4]
2788 ]
2789
2790 // (possible hook)
2791 Abc.prototype.calculate_beam = function(bm, s1) {
2792 var s, s2, g, notes, nflags, st, v, two_staves, two_dir,
2793 n, x, y, ys, a, b, stem_err, max_stem_err,
2794 p_min, p_max, s_closest,
2795 stem_xoff, scale,
2796 visible, dy
2797
2798 if (!s1.beam_st) { /* beam from previous music line */
2799 s = sym_dup(s1);
2800 lkvsym(s, s1);
2801 lktsym(s, s1);
2802 s.x -= 12
2803 if (s.x > s1.prev.x + 12)
2804 s.x = s1.prev.x + 12;
2805 s.beam_st = true
2806 delete s.beam_end;
2807 s.tmp = true
2808 delete s.sls;
2809 s1 = s
2810 }
2811
2812 /* search last note in beam */
2813 notes = nflags = 0; /* set x positions, count notes and flags */
2814 two_staves = two_dir = false;
2815 st = s1.st;
2816 v = s1.v;
2817 stem_xoff = s1.grace ? GSTEM_XOFF : 3.5
2818 for (s2 = s1; ;s2 = s2.next) {
2819 if (s2.type == C.NOTE) {
2820 if (s2.nflags > nflags)
2821 nflags = s2.nflags;
2822 notes++
2823 if (s2.st != st)
2824 two_staves = true
2825 if (s2.stem != s1.stem)
2826 two_dir = true
2827 if (!visible && !s2.invis
2828 && (!s2.stemless || s2.trem2))
2829 visible = true
2830 if (s2.beam_end)
2831 break
2832 }
2833 if (!s2.next) { /* beam towards next music line */
2834 for (; ; s2 = s2.prev) {
2835 if (s2.type == C.NOTE)
2836 break
2837 }
2838 s = sym_dup(s2);
2839 s.next = s2.next
2840 if (s.next)
2841 s.next.prev = s;
2842 s2.next = s;
2843 s.prev = s2;
2844 s.ts_next = s2.ts_next
2845 if (s.ts_next)
2846 s.ts_next.ts_prev = s;
2847 s2.ts_next = s;
2848 s.ts_prev = s2
2849 delete s.beam_st;
2850 s.beam_end = true;
2851 s.tmp = true
2852 delete s.sls;
2853 s.x += 12
2854 if (s.x < realwidth - 12)
2855 s.x = realwidth - 12;
2856 s2 = s;
2857 notes++
2858 break
2859 }
2860 }
2861
2862 // at least, must have a visible note with a stem
2863 if (!visible)
2864 return false;
2865
2866 bm.s2 = s2 /* (don't display the flags) */
2867
2868 if (staff_tb[st].y == 0) { /* staves not defined */
2869 if (two_staves)
2870 return false
2871 } else { /* staves defined */
2872 // if (!two_staves && !s1.grace) {
2873 if (!two_staves) {
2874 bm.s1 = s1; /* beam already calculated */
2875 bm.a = (s1.ys - s2.ys) / (s1.xs - s2.xs);
2876 bm.b = s1.ys - s1.xs * bm.a + staff_tb[st].y;
2877 bm.nflags = nflags
2878 return true
2879 }
2880 }
2881
2882 s_closest = s1;
2883 p_min = 100;
2884 p_max = 0
2885 for (s = s1; ; s = s.next) {
2886 if (s.type != C.NOTE)
2887 continue
2888 if ((scale = s.p_v.scale) == 1)
2889 scale = staff_tb[s.st].staffscale
2890 if (s.stem >= 0) {
2891 x = stem_xoff + s.notes[0].shhd
2892 if (s.notes[s.nhd].midi > p_max) {
2893 p_max = s.notes[s.nhd].midi
2894 s_closest = s
2895 }
2896 } else {
2897 x = -stem_xoff + s.notes[s.nhd].shhd
2898 if (s.notes[0].midi < p_min) {
2899 p_min = s.notes[0].midi
2900 s_closest = s
2901 }
2902 }
2903 s.xs = s.x + x * scale;
2904 if (s == s2)
2905 break
2906 }
2907
2908 // have flat beams on grace notes when asked
2909 if (s.grace && s1.fmt.flatbeams)
2910 a = 0
2911
2912 // if a note inside the beam is the closest to the beam, the beam is flat
2913 else if (!two_dir
2914 && notes >= 3
2915 && s_closest != s1 && s_closest != s2)
2916 a = 0
2917
2918 y = s1.ys + staff_tb[st].y
2919 if (a == undefined) {
2920 if (two_dir
2921 && s1.stem != s2.stem
2922 && s1.st == s2.st) { // if inverted stems,
2923 y -= 5 * s1.stem // remove the beam depth
2924 s2.ys -= 5 * s2.stem
2925 }
2926 a = (s2.ys + staff_tb[s2.st].y - y) / (s2.xs - s1.xs)
2927 }
2928
2929 if (a != 0) {
2930 a = s1.fmt.beamslope * a /
2931 (s1.fmt.beamslope + Math.abs(a)) // max steepness for beam
2932 if (a > -.04 && a < .04)
2933 a = 0 // slope too low
2934 }
2935
2936 // center the main beam
2937 b = (y + s2.ys + staff_tb[s2.st].y) / 2 - a * (s2.xs + s1.xs) / 2
2938
2939 /* have room for the symbols in the staff */
2940 max_stem_err = 0; /* check stem lengths */
2941
2942 // when 2 directions, check if all beams are on the same side of the main beam
2943 s = s1
2944 if (two_dir) {
2945 n = 1
2946 while (1) {
2947 if (s.stem != s1.stem
2948 && (s.nflags == 1
2949 || s.beam_br1 || s.beam_br2)) {
2950 n = 0
2951 break
2952 }
2953 if (s == s2)
2954 break
2955 s = s.next
2956 }
2957 if (n) // same side
2958 n = (s1.nflags + s2.nflags)
2959 * (s1.nflags >= s2.nflags ? s1.stem : s2.stem)
2960 / 4
2961 else // different sides
2962 n = -(s1.nflags * s1.stem + s2.nflags * s2.stem)
2963 / 2
2964 b += ((s1.grace ? 3.5 : BEAM_SHIFT) * n
2965 + BEAM_DEPTH * s1.stem) / 2
2966 } else if (!s1.grace) { /* normal notes */
2967 var beam_h = BEAM_DEPTH + BEAM_SHIFT * (nflags - 1)
2968 //--fixme: added for abc2svg
2969 while (s.ts_prev
2970 && s.ts_prev.type == C.NOTE
2971 && s.ts_prev.time == s.time
2972 && s.ts_prev.x > s1.xs)
2973 s = s.ts_prev
2974
2975 for (; s && s.time <= s2.time; s = s.ts_next) {
2976 if (s.type != C.NOTE
2977 || s.invis
2978 || (s.st != st
2979 && s.v != v)) {
2980 continue
2981 }
2982 x = s.v == v ? s.xs : s.x;
2983 ys = a * x + b - staff_tb[s.st].y
2984 if (s.v == v) {
2985 stem_err = min_tb[s.nhd == 0 ? 0 : 1][s.nflags]
2986 if (s.stem > 0) {
2987 if (s.notes[s.nhd].pit > 26) {
2988 stem_err -= 2
2989 if (s.notes[s.nhd].pit > 28)
2990 stem_err -= 2
2991 }
2992 stem_err -= ys - 3 * (s.notes[s.nhd].pit - 18)
2993 } else {
2994 if (s.notes[0].pit < 18) {
2995 stem_err -= 2
2996 if (s.notes[0].pit < 16)
2997 stem_err -= 2
2998 }
2999 stem_err -= 3 * (s.notes[0].pit - 18) - ys
3000 }
3001 stem_err += BEAM_DEPTH + BEAM_SHIFT * (s.nflags - 1)
3002 } else {
3003 /*fixme: KO when two_staves*/
3004 if (s1.stem > 0) {
3005 if (s.stem > 0) {
3006 /*fixme: KO when the voice numbers are inverted*/
3007 if (s.ymn > ys + 4
3008 || s.ymx < ys - beam_h - 2)
3009 continue
3010 if (s.v > v)
3011 stem_err = s.ymx - ys
3012 else
3013 stem_err = s.ymn + 8 - ys
3014 } else {
3015 stem_err = s.ymx - ys
3016 }
3017 } else {
3018 if (s.stem < 0) {
3019 if (s.ymx < ys - 4
3020 || s.ymn > ys - beam_h - 2)
3021 continue
3022 if (s.v < v)
3023 stem_err = ys - s.ymn
3024 else
3025 stem_err = ys - s.ymx + 8
3026 } else {
3027 stem_err = ys - s.ymn
3028 }
3029 }
3030 stem_err += 2 + beam_h
3031 }
3032 if (stem_err > max_stem_err)
3033 max_stem_err = stem_err
3034 }
3035 } else { /* grace notes */
3036 for ( ; ; s = s.next) {
3037 ys = a * s.xs + b - staff_tb[s.st].y;
3038 stem_err = GSTEM - 2
3039 if (s.stem > 0)
3040 stem_err -= ys - (3 * (s.notes[s.nhd].pit - 18))
3041 else
3042 stem_err += ys - (3 * (s.notes[0].pit - 18));
3043 stem_err += 3 * (s.nflags - 1)
3044 if (stem_err > max_stem_err)
3045 max_stem_err = stem_err
3046 if (s == s2)
3047 break
3048 }
3049 }
3050
3051 if (max_stem_err > 0) /* shift beam if stems too short */
3052 b += s1.stem * max_stem_err
3053
3054 // have room for the gracenotes and clefs
3055 if (!two_staves && !two_dir)
3056 for (s = s1.next; ; s = s.next) {
3057 switch (s.type) {
3058 case C.REST: /* cannot move rests in multi-voices */
3059 if (!s.multi)
3060 break
3061 g = s.ts_next
3062 if (!g || g.st != st
3063 || (g.type != C.NOTE && g.type != C.REST))
3064 break
3065 //fixme:too much vertical shift if some space above the note
3066 //fixme:this does not fix rest under beam in second voice (ts_prev)
3067 /*fall thru*/
3068 // case C.BAR:
3069 if (s.invis)
3070 break
3071 /*fall thru*/
3072 case C.CLEF:
3073 y = a * s.x + b
3074 if (s1.stem > 0) {
3075 y = s.ymx - y
3076 + BEAM_DEPTH + BEAM_SHIFT * (nflags - 1)
3077 + 2
3078 if (y > 0)
3079 b += y
3080 } else {
3081 y = s.ymn - y
3082 - BEAM_DEPTH - BEAM_SHIFT * (nflags - 1)
3083 - 2
3084 if (y < 0)
3085 b += y
3086 }
3087 break
3088 case C.GRACE:
3089 for (g = s.extra; g; g = g.next) {
3090 y = a * g.x + b
3091 if (s1.stem > 0) {
3092 y = g.ymx - y
3093 + BEAM_DEPTH + BEAM_SHIFT * (nflags - 1)
3094 if (y > 0)
3095 b += y
3096 } else {
3097 y = g.ymn - y
3098 - BEAM_DEPTH - BEAM_SHIFT * (nflags - 1)
3099 if (y < 0)
3100 b += y
3101 }
3102 }
3103 break
3104 }
3105 if (s == s2)
3106 break
3107 }
3108
3109 if (a == 0) /* shift flat beams onto staff lines */
3110 b += b_pos(s1.grace, s1.stem, nflags, b - staff_tb[st].y)
3111
3112 /* adjust final stems and rests under beam */
3113 for (s = s1; ; s = s.next) {
3114 switch (s.type) {
3115 case C.NOTE:
3116 s.ys = a * s.xs + b - staff_tb[s.st].y
3117 if (s.stem > 0) {
3118 s.ymx = s.ys + 2.5
3119 } else {
3120 s.ymn = s.ys - 2.5
3121 }
3122 break
3123 case C.REST:
3124 y = a * s.x + b - staff_tb[s.st].y
3125 dy = BEAM_DEPTH + BEAM_SHIFT * (nflags - 1)
3126 + (s.head != C.FULL ? 4 : 9)
3127 if (s1.stem > 0) {
3128 y -= dy
3129 if (s1.multi == 0 && y > 12)
3130 y = 12
3131 if (s.y <= y)
3132 break
3133 } else {
3134 y += dy
3135 if (s1.multi == 0 && y < 12)
3136 y = 12
3137 if (s.y >= y)
3138 break
3139 }
3140 if (s.head != C.FULL)
3141 y = (((y + 3 + 12) / 6) | 0) * 6 - 12;
3142 s.y = y
3143 break
3144 }
3145 if (s == s2)
3146 break
3147 }
3148
3149 /* save beam parameters */
3150 if (staff_tb[st].y == 0) /* if staves not defined */
3151 return false
3152 bm.s1 = s1;
3153 bm.a = a;
3154 bm.b = b;
3155 bm.nflags = nflags
3156 return true
3157 }
3158
3159 /* -- draw the beams for one word -- */
3160 /* (the staves are defined) */
3161 function draw_beams(bm) {
3162 var s, i, beam_dir, shift, bshift, bstub, bh, da, bsh,
3163 k, k1, k2, x1,
3164 osh = 0, // shift other side
3165 s1 = bm.s1,
3166 s2 = bm.s2
3167
3168 /* -- draw a single beam -- */
3169 function draw_beam(x1, x2, dy, h, bm,
3170 n) { /* beam number (1..n) */
3171 var y1, dy2,
3172 s = bm.s1,
3173 nflags = s.nflags
3174
3175 if (s.ntrem)
3176 nflags -= s.ntrem
3177 if (s.trem2 && n > nflags) {
3178 if (s.dur >= C.BLEN / 2) {
3179 x1 = s.x + 6;
3180 x2 = bm.s2.x - 6
3181 } else if (s.dur < C.BLEN / 4) {
3182 var dx = x2 - x1
3183 if (dx < 16) {
3184 x1 += dx / 4
3185 x2 -= dx / 4
3186 } else {
3187 x1 += 5
3188 x2 -= 6
3189 }
3190 }
3191 }
3192
3193 y1 = bm.a * x1 + bm.b - dy;
3194 x2 -= x1;
3195 x2 /= stv_g.scale;
3196 dy2 = bm.a * x2 * stv_g.stsc
3197 xypath(x1, y1, true);
3198 output += 'l' + x2.toFixed(1) + ' ' + (-dy2).toFixed(1) +
3199 'v' + h.toFixed(1) +
3200 'l' + (-x2).toFixed(1) + ' ' + dy2.toFixed(1) +
3201 'z"/>\n'
3202 } // draw_beam()
3203
3204 anno_start(s1, 'beam')
3205 /*fixme: KO if many staves with different scales*/
3206 // set_scale(s1)
3207 if (!s1.grace) {
3208 bshift = BEAM_SHIFT;
3209 bstub = BEAM_STUB;
3210 shift = .34; /* (half width of the stem) */
3211 bh = BEAM_DEPTH
3212 } else {
3213 bshift = 3.5;
3214 bstub = 3.2;
3215 shift = .29;
3216 bh = 1.8
3217 }
3218 bh /= stv_g.scale
3219
3220 /*fixme: quick hack for stubs at end of beam and different stem directions*/
3221 beam_dir = s1.stem
3222 if (s1.stem != s2.stem
3223 && s1.nflags > s2.nflags)
3224 beam_dir = s2.stem
3225 if (beam_dir < 0)
3226 bh = -bh;
3227
3228 /* make first beam over whole word and adjust the stem lengths */
3229 draw_beam(s1.xs - shift, s2.xs + shift, 0, bh, bm, 1);
3230 da = 0
3231 for (s = s1; ; s = s.next) {
3232 if (s.type == C.NOTE
3233 && s.stem != beam_dir)
3234 s.ys = bm.a * s.xs + bm.b
3235 - staff_tb[s.st].y
3236 + bshift * (s.nflags - 1) * s.stem
3237 - bh
3238 if (s == s2)
3239 break
3240 }
3241
3242 if (s1.feathered_beam) {
3243 da = bshift / (s2.xs - s1.xs)
3244 if (s1.feathered_beam > 0) {
3245 da = -da;
3246 bshift = da * s1.xs
3247 } else {
3248 bshift = da * s2.xs
3249 }
3250 da = da * beam_dir
3251 }
3252
3253 /* other beams with two or more flags */
3254 shift = 0
3255 for (i = 2; i <= bm.nflags; i++) {
3256 shift += bshift
3257 if (da != 0)
3258 bm.a += da
3259 for (s = s1; ; s = s.next) {
3260 if (s.type != C.NOTE
3261 || s.nflags < i) {
3262 if (s == s2)
3263 break
3264 continue
3265 }
3266 if (s.trem1
3267 && i > s.nflags - s.ntrem) {
3268 x1 = (s.dur >= C.BLEN / 2) ? s.x : s.xs;
3269 draw_beam(x1 - 5, x1 + 5,
3270 (shift + 2.5) * beam_dir,
3271 bh, bm, i)
3272 if (s == s2)
3273 break
3274 continue
3275 }
3276 k1 = s
3277 while (1) {
3278 if (s == s2)
3279 break
3280 k = s.next
3281 if (k.type == C.NOTE || k.type == C.REST) {
3282 if (k.trem1){
3283 if (k.nflags - k.ntrem < i)
3284 break
3285 } else if (k.nflags < i) {
3286 break
3287 }
3288 }
3289 if (k.beam_br1
3290 || (k.beam_br2 && i > 2))
3291 break
3292 s = k
3293 }
3294 k2 = s
3295 while (k2.type != C.NOTE)
3296 k2 = k2.prev;
3297 x1 = k1.xs
3298 bsh = shift * beam_dir
3299 if (k1 == k2) {
3300 if (k1 == s1) {
3301 x1 += bstub
3302 } else if (k1 == s2) {
3303 x1 -= bstub
3304 } else if (k1.beam_br1
3305 || (k1.beam_br2
3306 && i > 2)) {
3307 x1 += bstub
3308 } else {
3309 k = k1.next
3310 while (k.type != C.NOTE)
3311 k = k.next
3312 if (k.beam_br1
3313 || (k.beam_br2 && i > 2)) {
3314 x1 -= bstub
3315 } else {
3316 k1 = k1.prev
3317 while (k1.type != C.NOTE)
3318 k1 = k1.prev
3319 if (k1.nflags < k.nflags
3320 || (k1.nflags == k.nflags
3321 && k1.dots < k.dots))
3322 x1 += bstub
3323 else
3324 x1 -= bstub
3325 }
3326 }
3327 if (k1.stem != beam_dir) {
3328 osh -= bshift
3329 bsh = osh * beam_dir
3330 k1.ys = bm.a * k1.xs + bm.b
3331 - staff_tb[k1.st].y - bh
3332 }
3333 } else if (k1.stem == k2.stem && k1.stem != beam_dir) {
3334
3335 // inverted stems: put the beam on the other side
3336 osh -= bshift
3337 bsh = osh * beam_dir
3338 for (s = k1; ; s = s.next) {
3339 if (s.type == C.NOTE)
3340 s.ys = bm.a * s.xs + bm.b
3341 - staff_tb[s.st].y
3342 - bh
3343 if (s == k2)
3344 break
3345 }
3346 }
3347 draw_beam(x1, k2.xs,
3348 bsh,
3349 bh, bm, i)
3350 if (s == s2)
3351 break
3352 }
3353 }
3354 if (s1.tmp)
3355 unlksym(s1)
3356 else if (s2.tmp)
3357 unlksym(s2)
3358 anno_stop(s1, 'beam')
3359 }
3360
3361 /* -- draw the left side of the staves -- */
3362 function draw_lstaff(x) {
3363 var i, j, yb, h, fl,
3364 nst = cur_sy.nstaff,
3365 l = 0
3366
3367 /* -- draw a system brace or bracket -- */
3368 function draw_sysbra(x, st, flag) {
3369 var i, st_end, yt, yb
3370
3371 while (!cur_sy.st_print[st]) {
3372 if (cur_sy.staves[st].flags & flag)
3373 return
3374 st++
3375 }
3376 i = st_end = st
3377 while (1) {
3378 if (cur_sy.st_print[i])
3379 st_end = i
3380 if (cur_sy.staves[i].flags & flag)
3381 break
3382 i++
3383 }
3384 yt = staff_tb[st].y + staff_tb[st].topbar
3385 * staff_tb[st].staffscale;
3386 yb = staff_tb[st_end].y + staff_tb[st_end].botbar
3387 * staff_tb[st_end].staffscale
3388 if (flag & (CLOSE_BRACE | CLOSE_BRACE2))
3389 out_brace(x, yb, yt - yb)
3390 else
3391 out_bracket(x, yt, yt - yb)
3392 }
3393
3394 for (i = 0; ; i++) {
3395 fl = cur_sy.staves[i].flags
3396 if (fl & (OPEN_BRACE | OPEN_BRACKET))
3397 l++
3398 if (cur_sy.st_print[i])
3399 break
3400 if (fl & (CLOSE_BRACE | CLOSE_BRACKET))
3401 l--
3402 if (i == nst)
3403 break
3404 }
3405 for (j = nst; j > i; j--) {
3406 if (cur_sy.st_print[j])
3407 break
3408 }
3409 if (i == j && l == 0)
3410 return
3411 yb = staff_tb[j].y + staff_tb[j].botbar * staff_tb[j].staffscale;
3412 h = staff_tb[i].y + staff_tb[i].topbar * staff_tb[i].staffscale - yb;
3413 xypath(x, yb);
3414 output += "v" + (-h).toFixed(1) + '"/>\n'
3415 for (i = 0; i <= nst; i++) {
3416 fl = cur_sy.staves[i].flags
3417 if (fl & OPEN_BRACE)
3418 draw_sysbra(x, i, CLOSE_BRACE)
3419 if (fl & OPEN_BRACKET)
3420 draw_sysbra(x, i, CLOSE_BRACKET)
3421 if (fl & OPEN_BRACE2)
3422 draw_sysbra(x - 6, i, CLOSE_BRACE2)
3423 if (fl & OPEN_BRACKET2)
3424 draw_sysbra(x - 6, i, CLOSE_BRACKET2)
3425 }
3426 }
3427
3428 /* -- draw the time signature -- */
3429 function draw_meter(s) {
3430 if (!s.a_meter)
3431 return
3432 var i, m, meter, x, x0, yt,
3433 p_staff = staff_tb[s.st],
3434 y = p_staff.y
3435
3436 // adjust the vertical offset according to the staff definition
3437 if (p_staff.stafflines != '|||||')
3438 y += (p_staff.topbar + p_staff.botbar) / 2 - 12 // bottom
3439
3440 for (i = 0; i < s.a_meter.length; i++) {
3441 meter = s.a_meter[i];
3442 x = s.x + s.x_meter[i]
3443 yt = y + (meter.bot ? 18 : 12)
3444 if (s.a_meter[i + 1]
3445 && (s.a_meter[i + 1].top == '|'
3446 || s.a_meter[i + 1].top == '.')) {
3447 xygl(x, yt, "mtr" + meter.top[0] + s.a_meter[i + 1].top)
3448 i++
3449 continue
3450 }
3451 xygl(x, yt, "mtr" + meter.top[0])
3452 if (meter.top.length > 1) {
3453 m = 0
3454 x0 = x
3455 while (1) {
3456 switch (meter.top[m]) {
3457 case '(':
3458 case ')':
3459 x += 4
3460 break
3461 case '1':
3462 x += 8
3463 break
3464 case ' ':
3465 x += 4
3466 break
3467 case '+':
3468 x += 2
3469 // fall thru
3470 default:
3471 x += 10
3472 break
3473 }
3474 if (++m >= meter.top.length)
3475 break
3476 xygl(x, yt, "mtr" + meter.top[m])
3477 }
3478 x = (x0 + x) / 2 - 5
3479 }
3480 if (meter.bot) {
3481 if (meter.bot[1]) {
3482 if (meter.bot[0] == 1) {
3483 x0 = x - 4
3484 x += 4
3485 } else {
3486 x0 = x - 5
3487 x += 5
3488 }
3489 xygl(x0, y + 6, "mtr" + meter.bot[0])
3490 xygl(x, y + 6, "mtr" + meter.bot[1])
3491 } else {
3492 xygl(x, y + 6, "mtr" + meter.bot[0])
3493 }
3494 }
3495 }
3496 }
3497
3498 var acc_nd = {} // cache of the microtonal accidentals
3499
3500 /* -- draw an accidental -- */
3501 function draw_acc(x, y, a) {
3502 if (typeof a == "object") { // if microtone
3503 var c,
3504 n = a[0],
3505 d = a[1]
3506
3507 c = n + '_' + d
3508 a = acc_nd[c]
3509 if (!a) {
3510 a = abc2svg.rat(Math.abs(n), d)
3511 d = a[1]
3512 a = (n < 0 ? -a[0] : a[0]).toString()
3513 if (d != 1)
3514 a += '_' + d
3515 acc_nd[c] = a
3516 }
3517 }
3518 xygl(x, y, "acc" + a)
3519 }
3520
3521 // memorize the helper/ledger lines
3522 function set_hl(p_st, n, x, dx1, dx2) {
3523 var i, hl
3524
3525 if (n >= 0) {
3526 hl = p_st.hlu[n]
3527 if (!hl)
3528 hl = p_st.hlu[n] = []
3529 } else {
3530 hl = p_st.hld[-n]
3531 if (!hl)
3532 hl = p_st.hld[-n] = []
3533 }
3534
3535 for (i = 0; i < hl.length; i++) {
3536 if (x >= hl[i][0])
3537 break
3538 }
3539 if (i == hl.length) {
3540 hl.push([x, dx1, dx2])
3541 } else if (x > hl[i][0]) {
3542 hl.splice(++i, 0, [x, dx1, dx2])
3543 } else {
3544 if (dx1 < hl[i][1])
3545 hl[i][1] = dx1
3546 if (dx2 > hl[i][2])
3547 hl[i][2] = dx2
3548 }
3549 } // set_hl()
3550
3551 // draw helper lines
3552 // (possible hook)
3553 Abc.prototype.draw_hl = function(s) {
3554 var i, j, n, note,
3555 hla = [],
3556 st = s.st,
3557 p_staff = staff_tb[st]
3558
3559 // check if any helper line
3560 if (!p_staff.hll
3561 || s.invis)
3562 return // no helper line (no line)
3563 for (i = 0; i <= s.nhd; i++) {
3564 note = s.notes[i]
3565 if (!p_staff.hlmap[note.pit - p_staff.hll])
3566 hla.push([note.pit - 18,
3567 note.shhd * s.p_v.scale])
3568 }
3569 n = hla.length
3570 if (!n)
3571 return // no
3572
3573 // handle the helper lines out of the staff
3574 var dx1, dx2, hl, shhd,hlp,
3575 stafflines = cur_sy.staves[st].stafflines,
3576 top = stafflines.length - 1,
3577 yu = top,
3578 bot = (p_staff.hll - 17) / 2,
3579 yl = bot,
3580 dx = (s.grace ? 4 : hw_tb[s.head] * 1.3) * s.p_v.scale
3581
3582 // get the x start and x stop of the intermediate helper lines
3583 note = s.notes[s.stem < 0 ? s.nhd : 0]
3584 shhd = note.shhd
3585
3586 for (i = 0; i < hla.length; i++) {
3587 hlp = hla[i][0]
3588 dx1 = (hla[i][1] < shhd ? hla[i][1] : shhd) - dx
3589 dx2 = (hla[i][1] > shhd ? hla[i][1] : shhd) + dx
3590 if (hlp < bot * 2) {
3591 if (++hlp < yl * 2)
3592 yl = hlp >> 1
3593 n--
3594 } else if (hlp > top * 2) {
3595 yu = hlp >> 1
3596 n--
3597 }
3598 set_hl(p_staff, hlp >> 1, s.x, dx1, dx2)
3599 }
3600
3601 dx1 = shhd - dx
3602 dx2 = shhd + dx
3603 while (++yl < bot)
3604 set_hl(p_staff, yl,
3605 s.x, dx1, dx2)
3606 while (--yu > top)
3607 set_hl(p_staff, yu,
3608 s.x, dx1, dx2)
3609 if (!n)
3610 return // no more helper lines
3611
3612 // draw the helper lines inside the staff
3613 i = yl;
3614 j = yu
3615 while (i > bot && stafflines[i] == '-')
3616 i--
3617 while (j < top && stafflines[j] == '-')
3618 j++
3619 for ( ; i < j; i++) {
3620 if (stafflines[i] == '-')
3621 set_hl(p_staff, i, s.x, dx1, dx2)
3622 }
3623 }
3624
3625 /* -- draw a key signature -- */
3626 // (possible hook)
3627 var sharp_cl = new Int8Array([24, 9, 15, 21, 6, 12, 18]),
3628 flat_cl = new Int8Array([12, 18, 24, 9, 15, 21, 6]),
3629 sharp1 = new Int8Array([-9, 12, -9, -9, 12, -9]),
3630 sharp2 = new Int8Array([12, -9, 12, -9, 12, -9]),
3631 flat1 = new Int8Array([9, -12, 9, -12, 9, -12]),
3632 flat2 = new Int8Array([-12, 9, -12, 9, -12, 9])
3633
3634 Abc.prototype.draw_keysig = function(x, s) {
3635 var old_sf = s.k_old_sf,
3636 st = s.st,
3637 staffb = staff_tb[st].y,
3638 i, shift, p_seq,
3639 clef_ix = s.k_y_clef,
3640 a_acc = s.k_a_acc // accidental list [pit, acc]
3641
3642 // set the accidentals when K: with modified accidentals
3643 function set_k_acc(a_acc, sf) {
3644 var i, j, n, nacc, p_acc,
3645 accs = [],
3646 pits = []
3647
3648 if (sf > 0) {
3649 for (nacc = 0; nacc < sf; nacc++) {
3650 accs[nacc] = 1 // sharp
3651 pits[nacc] = [26, 23, 27, 24, 21, 25, 22][nacc]
3652 }
3653 } else {
3654 for (nacc = 0; nacc < -sf; nacc++) {
3655 accs[nacc] = -1 // flat
3656 pits[nacc] = [22, 25, 21, 24, 20, 23, 26][nacc]
3657 }
3658 }
3659 n = a_acc.length
3660 for (i = 0; i < n; i++) {
3661 p_acc = a_acc[i]
3662 for (j = 0; j < nacc; j++) {
3663 if (pits[j] == p_acc.pit) {
3664 accs[j] = p_acc.acc
3665 break
3666 }
3667 }
3668 if (j == nacc) {
3669 accs[j] = p_acc.acc
3670 pits[j] = p_acc.pit
3671 nacc++
3672 }
3673 }
3674 for (i = 0; i < nacc; i++) {
3675 p_acc = a_acc[i]
3676 if (!p_acc)
3677 p_acc = a_acc[i] = {}
3678 p_acc.acc = accs[i]
3679 p_acc.pit = pits[i]
3680 }
3681 } // set_k_acc()
3682
3683 // ---- draw_keysig ---
3684 if (clef_ix & 1)
3685 clef_ix += 7;
3686 clef_ix /= 2
3687 while (clef_ix < 0)
3688 clef_ix += 7;
3689 clef_ix %= 7
3690
3691 /* normal accidentals */
3692 if (a_acc && !s.exp) // if added accidentals
3693 set_k_acc(a_acc, s.k_sf) // merge them into the key
3694
3695 if (!a_acc) {
3696
3697 /* put neutrals if 'accidental cancel' */
3698 if (s.fmt.cancelkey || s.k_sf == 0) {
3699
3700 /* when flats to sharps, or sharps to flats, */
3701 if (s.k_sf == 0
3702 || old_sf * s.k_sf < 0) {
3703
3704 /* old sharps */
3705 shift = sharp_cl[clef_ix];
3706 p_seq = shift > 9 ? sharp1 : sharp2
3707 for (i = 0; i < old_sf; i++) {
3708 xygl(x, staffb + shift, "acc3");
3709 shift += p_seq[i];
3710 x += 5.5
3711 }
3712
3713 /* old flats */
3714 shift = flat_cl[clef_ix];
3715 p_seq = shift < 18 ? flat1 : flat2
3716 for (i = 0; i > old_sf; i--) {
3717 xygl(x, staffb + shift, "acc3");
3718 shift += p_seq[-i];
3719 x += 5.5
3720 }
3721 if (s.k_sf != 0)
3722 x += 3 /* extra space */
3723 }
3724 }
3725
3726 /* new sharps */
3727 if (s.k_sf > 0) {
3728 shift = sharp_cl[clef_ix];
3729 p_seq = shift > 9 ? sharp1 : sharp2
3730 for (i = 0; i < s.k_sf; i++) {
3731 xygl(x, staffb + shift, "acc1");
3732 shift += p_seq[i];
3733 x += 5.5
3734 }
3735 if (s.fmt.cancelkey && i < old_sf) {
3736 x += 2
3737 for (; i < old_sf; i++) {
3738 xygl(x, staffb + shift, "acc3");
3739 shift += p_seq[i];
3740 x += 5.5
3741 }
3742 }
3743 if (s.k_bagpipe == 'p') { // K:Hp - add the g natural
3744 xygl(x, staffb + 27, "acc3")
3745 x += 5.5
3746 }
3747 }
3748
3749 /* new flats */
3750 if (s.k_sf < 0) {
3751 shift = flat_cl[clef_ix];
3752 p_seq = shift < 18 ? flat1 : flat2
3753 for (i = 0; i > s.k_sf; i--) {
3754 xygl(x, staffb + shift, "acc-1");
3755 shift += p_seq[-i];
3756 x += 5.5
3757 }
3758 if (s.fmt.cancelkey && i > old_sf) {
3759 x += 2
3760 for (; i > old_sf; i--) {
3761 xygl(x, staffb + shift, "acc3");
3762 shift += p_seq[-i];
3763 x += 5.5
3764 }
3765 }
3766 }
3767 } else if (a_acc.length) {
3768
3769 /* explicit accidentals */
3770 var acc,
3771 last_acc = a_acc[0].acc,
3772 last_shift = 100,
3773 s2 = {
3774 st: st,
3775 nhd: 0,
3776 notes: [{}]
3777 }
3778
3779 for (i = 0; i < a_acc.length; i++) {
3780 acc = a_acc[i];
3781 shift = (s.k_y_clef // clef shift
3782 + acc.pit - 18) * 3
3783 while (shift < -3) // let the accidentals inside the staff
3784 shift += 21
3785 while (shift > 27)
3786 shift -= 21
3787 if (i != 0
3788 && (shift > last_shift + 18
3789 || shift < last_shift - 18))
3790 x -= 5.5 // no clash
3791 else if (acc.acc != last_acc)
3792 x += 3;
3793 last_acc = acc.acc;
3794 s2.x = x
3795 s2.notes[0].pit = shift / 3 + 18;
3796 // is this useful?
3797 // s2.head = C.FULL
3798 // s2.notes[0].shhd = 0
3799 // self.draw_hl(s2)
3800 last_shift = shift;
3801 draw_acc(x, staffb + shift, acc.acc)
3802 x += 5.5
3803 }
3804 }
3805 }
3806
3807 // output the measure repeat number
3808 function nrep_out(x, y, n) {
3809 y -= 3
3810 if (n < 10) {
3811 xygl(x - 4, y, "mtr" + n)
3812 } else {
3813 xygl(x - 10, y, "mtr" + ((n / 10) | 0))
3814 xygl(x - 2, y, "mtr" + (n % 10))
3815 }
3816 } // nrep_out()
3817
3818 // if rest alone in the measure or measure repeat,
3819 // change the head and center
3820 function center_rest(s) {
3821 var s2, x
3822
3823 if (s.dur < C.BLEN * 2)
3824 s.nflags = -2 // semibreve / whole
3825 else if (s.dur < C.BLEN * 4)
3826 s.nflags = -3
3827 else
3828 s.nflags = -4
3829 s.dots = 0
3830
3831 /* don't use next/prev: there is no bar in voice overlay */
3832 s2 = s.ts_next
3833 while (s2.time != s.time + s.dur
3834 && s2.ts_next)
3835 s2 = s2.ts_next
3836 x = s2.x - s2.wl
3837 s2 = s
3838 while (!s2.seqst)
3839 s2 = s2.ts_prev
3840 s2 = s2.ts_prev
3841 x = (x + s2.x + s2.wr) / 2
3842
3843 /* center the associated decorations */
3844 if (s.a_dd)
3845 deco_update(s, x - s.x)
3846 s.x = x
3847 } // center_rest()
3848
3849 /* -- draw a rest -- */
3850 /* (the staves are defined) */
3851 var rest_tb = [
3852 "r128", "r64", "r32", "r16", "r8",
3853 "r4",
3854 "r2", "r1", "r0", "r00"]
3855
3856 function draw_rest(s) {
3857 var s2, i, j, y, bx,
3858 p_staff = staff_tb[s.st],
3859 yb = p_staff.y, // bottom of staff
3860 x = s.x
3861
3862 if (s.notes[0].shhd)
3863 x += s.notes[0].shhd * stv_g.scale
3864
3865 if (s.rep_nb) {
3866 set_sscale(s.st);
3867 anno_start(s);
3868 if (p_staff.stafflines == '|||||')
3869 yb += 12
3870 else
3871 yb += (p_staff.topbar + p_staff.botbar) / 2
3872 if (s.rep_nb < 0) {
3873 xygl(x, yb, "srep")
3874 } else {
3875 xygl(x, yb, "mrep")
3876 if (s.rep_nb > 2 && s.v == cur_sy.top_voice
3877 && s.fmt.measrepnb > 0
3878 && !(s.rep_nb % s.fmt.measrepnb))
3879 nrep_out(x, yb + p_staff.topbar, s.rep_nb)
3880 }
3881 anno_a.push(s)
3882 return
3883 }
3884
3885 set_scale(s);
3886 anno_start(s);
3887
3888 if (s.notes[0].color)
3889 set_color(s.notes[0].color);
3890
3891 y = s.y;
3892
3893 i = 5 - s.nflags /* rest_tb index (5 = C_XFLAGS) */
3894 if (i == 7 && y == 12
3895 && p_staff.stafflines.length <= 2)
3896 y -= 6 /* semibreve a bit lower */
3897
3898 // draw the rest
3899 if (!s.notes[0].invis) // if not head replacement
3900 xygl(x, y + yb, rest_tb[i])
3901
3902 if (s.dots) {
3903 x += 8;
3904 y += yb + 3
3905 j = s.dots
3906 i = (s.dur_orig / 12) >> ((5 - s.nflags) - j)
3907 while (j-- > 0) {
3908 xygl(x, y, (i & (1 << j)) ? "dot" : "dot+")
3909 x += 3.5
3910 }
3911 }
3912 set_color();
3913 anno_a.push(s)
3914 }
3915
3916 // -- draw a multi-measure rest --
3917 // (the staves are defined)
3918 function draw_mrest(s) {
3919 var x1, x2, s2,
3920 p_st = staff_tb[s.st],
3921 y = p_st.y + (p_st.topbar + p_st.botbar) / 2,
3922 p = s.nmes.toString()
3923
3924 // output an old multimeasure rest
3925 function omrest() {
3926 var x = s.x,
3927 y = p_st.y + 12,
3928 n = s.nmes,
3929 k = n >> 2 // number of rests
3930
3931 if (n & 3) {
3932 k++
3933 if (n & 3 == 3)
3934 k++
3935 }
3936 //dx = 6
3937 x -= 3 * (k - 1)
3938 while (n >= 4) {
3939 xygl(x, y, "r00")
3940 n -= 4
3941 x += 6
3942 }
3943 if (n >= 2) {
3944 xygl(x, y, "r0")
3945 n -= 2
3946 x += 6
3947 }
3948 if (n)
3949 xygl(x + 2, y, "r1")
3950 } // omrest()
3951
3952 set_scale(s)
3953
3954 s2 = s // search the start of the previous time sequence
3955 while (!s2.seqst)
3956 s2 = s2.ts_prev
3957 s2 = s2.ts_prev
3958 while (!s2.seqst)
3959 s2 = s2.ts_prev
3960 x1 = s2.x + 20
3961
3962 s2 = s.ts_next // search the next symbol on the same staff
3963 if (s2.staff != s.staff)
3964 s2 = s.next
3965 x2 = s2.x - 20
3966
3967 s.x = (x1 + x2) / 2
3968 anno_start(s)
3969 if (!cfmt.oldmrest || s.nmes > cfmt.oldmrest) {
3970 out_XYAB('<path d="mX Y', x1 + .6, y - 2.7)
3971 output += 'v2.7h-1.4v-10.8h1.4v2.7h'
3972 + ((x2 - x1 - 2.8) / stv_g.scale).toFixed(1)
3973 + 'v-2.7h1.4v10.8h-1.4v-2.7z"/>\n'
3974 } else {
3975 omrest() // old multirest
3976 }
3977 if (s.tacet)
3978 out_XYAB('<text x ="X" y="Y" style="font-size:12px;font-weight:700"\
3979 text-anchor="middle">A</text>\n',
3980 s.x, y + 18, s.tacet)
3981 else
3982 out_XYAB('<text x ="X" y="Y" text-anchor="middle">A</text>\n',
3983 s.x, y + 22, m_gl(p))
3984 anno_a.push(s)
3985 } // draw_mrest()
3986
3987 function grace_slur(s) {
3988 var yy, x0, y0, x3, y3, bet1, bet2, dy1, dy2, last, below,
3989 so = s,
3990 g = s.extra
3991
3992 while (1) {
3993 if (!g.next)
3994 break /* (keep the last note) */
3995 g = g.next
3996 }
3997 last = g
3998
3999 below = ((g.stem >= 0 || s.multi < 0) && g.notes[0].pit <= 28)
4000 || g.notes[0].pit < 16
4001 if (below) {
4002 yy = 127
4003 for (g = s.extra; g; g = g.next) {
4004 if (g.y < yy) {
4005 yy = g.y;
4006 last = g
4007 }
4008 }
4009 x0 = last.x;
4010 y0 = last.y - 5
4011 if (s.extra != last) {
4012 x0 -= 4;
4013 y0 += 1
4014 }
4015 s = s.next;
4016 x3 = s.x - 1
4017 if (s.stem < 0 && s.nflags > -2)
4018 x3 -= 4;
4019 y3 = 3 * (s.notes[0].pit - 18) - 5;
4020 dy1 = (x3 - x0) * .4
4021 if (dy1 > 3)
4022 dy1 = 3;
4023 dy2 = dy1;
4024 bet1 = .2;
4025 bet2 = .8
4026 if (y0 > y3 + 7) {
4027 x0 = last.x - 1;
4028 y0 += .5;
4029 y3 += 6.5;
4030 x3 = s.x - 5.5;
4031 dy1 = (y0 - y3) * .8;
4032 dy2 = (y0 - y3) * .2;
4033 bet1 = 0
4034 } else if (y3 > y0 + 4) {
4035 y3 = y0 + 4;
4036 x0 = last.x + 2;
4037 y0 = last.y - 4
4038 }
4039 } else { // slur above
4040 yy = -127
4041 for (g = s.extra; g; g = g.next) {
4042 if (g.y > yy) {
4043 yy = g.y;
4044 last = g
4045 }
4046 }
4047 x0 = last.x;
4048 y0 = last.y + 5
4049 if (s.extra != last) {
4050 x0 -= 4;
4051 y0 -= 1
4052 }
4053 s = s.next;
4054 x3 = s.x - 1
4055 if (s.stem >= 0 && s.nflags > -2)
4056 x3 -= 2;
4057 y3 = 3 * (s.notes[s.nhd].pit - 18) + 5;
4058 dy1 = (x0 - x3) * .4
4059 if (dy1 < -3)
4060 dy1 = -3;
4061 dy2 = dy1;
4062 bet1 = .2;
4063 bet2 = .8
4064 if (y0 < y3 - 7) {
4065 x0 = last.x - 1;
4066 y0 -= .5;
4067 y3 -= 6.5;
4068 x3 = s.x - 5.5;
4069 dy1 = (y0 - y3) * .8;
4070 dy2 = (y0 - y3) * .2;
4071 bet1 = 0
4072 } else if (y3 < y0 - 4) {
4073 y3 = y0 - 4;
4074 x0 = last.x + 2;
4075 y0 = last.y + 4
4076 }
4077 }
4078
4079 so.slur = {
4080 x0: x0,
4081 y0: y0,
4082 x1: bet1 * x3 + (1 - bet1) * x0 - x0,
4083 y1: y0 - bet1 * y3 - (1 - bet1) * y0 + dy1,
4084 x2: bet2 * x3 + (1 - bet2) * x0 - x0,
4085 y2: y0 - bet2 * y3 - (1 - bet2) * y0 + dy2,
4086 x3: x3 - x0,
4087 y3: y0 - y3
4088 }
4089 y0 -= so.slur.y1
4090 g = so.extra
4091 //fixme: useless?
4092 // y_set(s.st, !below, x0, x3 - x0, y0)
4093 if (below) {
4094 if (y0 < g.ymn)
4095 g.ymn = y0
4096 } else {
4097 if (y0 > g.ymx)
4098 g.ymx = y0
4099 }
4100 } // grace_slur()
4101
4102 /* -- draw grace notes -- */
4103 /* (the staves are defined) */
4104 function draw_gracenotes(s) {
4105 var x1, y1,
4106 last, note,
4107 bm = {},
4108 g = s.extra
4109
4110 /* draw the notes */
4111 // bm.s2 = undefined /* (draw flags) */
4112 while (1) {
4113 if (g.beam_st && !g.beam_end) {
4114 if (self.calculate_beam(bm, g))
4115 draw_beams(bm)
4116 }
4117 anno_start(g)
4118 draw_note(g, !bm.s2)
4119 if (g == bm.s2)
4120 bm.s2 = null /* (draw flags again) */
4121 anno_a.push(s)
4122 // if (g.sls || g.sl2)
4123 // slur = true
4124 if (!g.next)
4125 break /* (keep the last note) */
4126 g = g.next
4127 }
4128 last = g
4129
4130 // if an acciaccatura, draw a bar
4131 if (s.sappo) {
4132 g = s.extra
4133 if (!g.next) { /* if one note */
4134 x1 = 9
4135 y1 = g.stem > 0 ? 5 : -5
4136 } else { /* many notes */
4137 x1 = (g.next.x - g.x) * .5 + 4
4138 y1 = (g.ys + g.next.ys) * .5 - g.y
4139 if (g.stem > 0)
4140 y1 -= 1
4141 else
4142 y1 += 1
4143 }
4144 note = g.notes[g.stem < 0 ? 0 : g.nhd]
4145 out_acciac(g.x, y_head(g, note),
4146 x1, y1, g.stem > 0)
4147 }
4148
4149 /* slur */
4150 g = s.slur
4151 if (g) {
4152 anno_start(s, 'slur')
4153 xypath(g.x0, g.y0 + staff_tb[s.st].y)
4154 output += 'c' + g.x1.toFixed(1) + ' ' + g.y1.toFixed(1) +
4155 ' ' + g.x2.toFixed(1) + ' ' + g.y2.toFixed(1) +
4156 ' ' + g.x3.toFixed(1) + ' ' + g.y3.toFixed(1) + '"/>\n'
4157 anno_stop(s, 'slur')
4158 }
4159 }
4160
4161 /* -- set the y offset of the dots -- */
4162 function setdoty(s, y_tb) {
4163 var m, m1, y
4164
4165 /* set the normal offsets */
4166 for (m = 0; m <= s.nhd; m++) {
4167 y = 3 * (s.notes[m].pit - 18) /* note height on staff */
4168 if ((y % 6) == 0) {
4169 if (s.dot_low)
4170 y -= 3
4171 else
4172 y += 3
4173 }
4174 y_tb[m] = y
4175 }
4176 /* dispatch and recenter the dots in the staff spaces */
4177 for (m = 0; m < s.nhd; m++) {
4178 if (y_tb[m + 1] > y_tb[m])
4179 continue
4180 m1 = m
4181 while (m1 > 0) {
4182 if (y_tb[m1] > y_tb[m1 - 1] + 6)
4183 break
4184 m1--
4185 }
4186 if (3 * (s.notes[m1].pit - 18) - y_tb[m1]
4187 < y_tb[m + 1] - 3 * (s.notes[m + 1].pit - 18)) {
4188 while (m1 <= m)
4189 y_tb[m1++] -= 6
4190 } else {
4191 y_tb[m + 1] = y_tb[m] + 6
4192 }
4193 }
4194 }
4195
4196 // get the y offset of a note head
4197 // (when the staves are defined)
4198 function y_head(s, note) {
4199 return staff_tb[s.st].y + 3 * (note.pit - 18)
4200 }
4201
4202 /* -- draw m-th head with accidentals and dots -- */
4203 /* (the staves are defined) */
4204 // sets {x,y}_note
4205 function draw_basic_note(s, m, y_tb) {
4206 var i, p, yy, dotx, doty, inv, head, dots, nflags,
4207 old_color = false,
4208 note = s.notes[m],
4209 staffb = staff_tb[s.st].y, /* bottom of staff */
4210 x = s.x,
4211 y = 3 * (note.pit - 18), /* note height on staff */
4212 shhd = note.shhd * stv_g.scale,
4213 x_note = x + shhd,
4214 y_note = y + staffb
4215
4216 // /* special case for voice unison */
4217 // if (s.nohdi1 != undefined
4218 // && m >= s.nohdi1 && m < s.nohdi2)
4219 // return
4220
4221 if (note.dur == s.dur) {
4222 head = s.head
4223 dots = s.dots
4224 nflags = s.nflags
4225 } else {
4226 i = identify_note(s, note.dur)
4227 head = i[0]
4228 dots = i[1]
4229 nflags = i[2]
4230 }
4231
4232 /* draw the head */
4233 if (note.invis) {
4234 ;
4235 } else if (note.map && note.map[0]) {
4236 i = head;
4237 p = note.map[0][i] // heads
4238 if (!p)
4239 p = note.map[0][note.map[0].length - 1]
4240 i = p.indexOf('/')
4241 if (i >= 0) { // stem dependant
4242 if (s.stem >= 0)
4243 p = p.slice(0, i)
4244 else
4245 p = p.slice(i + 1)
4246 }
4247 } else if (s.type == C.CUSTOS) {
4248 p = "custos"
4249 } else {
4250 switch (head) {
4251 case C.OVAL:
4252 p = "HD"
4253 break
4254 case C.OVALBARS:
4255 if (s.head != C.SQUARE) {
4256 p = "HDD"
4257 break
4258 }
4259 // fall thru
4260 case C.SQUARE:
4261 if (nflags > -4) {
4262 p = "breve"
4263 } else {
4264 p = "longa"
4265 inv = s.stem > 0
4266 }
4267
4268 /* don't display dots on last note of the tune */
4269 if (!tsnext && s.next
4270 && s.next.type == C.BAR && !s.next.next)
4271 dots = 0
4272 x_note += 1
4273 break
4274 case C.EMPTY:
4275 p = "Hd" // white note
4276 break
4277 default: // black note
4278 p = "hd"
4279 break
4280 }
4281 }
4282 if (note.color != undefined)
4283 old_color = set_color(note.color)
4284 if (p) {
4285 if (s.grace || inv) {
4286 if (s.grace)
4287 g_open(x_note, y_note, 0, .66, 0)
4288 else
4289 g_open(x_note, y_note, 0, 1, -1)
4290 x_note = y_note = 0
4291 }
4292 if (!self.psxygl(x_note, y_note, p))
4293 xygl(x_note, y_note, p)
4294 if (s.grace || inv)
4295 g_close()
4296 }
4297
4298 /* draw the dots */
4299 /*fixme: to see for grace notes*/
4300 // (s.dots may be removed in tablatures - see strtab)
4301 if (dots && (s.dots || note.dur != s.dur)) {
4302 dotx = x + (6.6 + s.xmx) * stv_g.scale
4303 if (y_tb[m] == undefined) {
4304 y_tb[m] = 3 * (s.notes[m].pit - 18)
4305 if ((s.notes[m].pit & 1) == 0)
4306 y_tb[m] += 3
4307 }
4308 doty = y_tb[m] + staffb
4309 i = (note.dur / 12) >> ((5 - nflags) - dots)
4310 while (dots-- > 0) {
4311 xygl(dotx, doty, (i & (1 << dots)) ? "dot" : "dot+")
4312 dotx += 3.5
4313 }
4314 }
4315
4316 /* draw the accidental */
4317 if (note.acc) {
4318 x -= note.shac * stv_g.scale
4319 if (!s.grace) {
4320 draw_acc(x, y + staffb, note.acc)
4321 } else {
4322 g_open(x, y + staffb, 0, .75);
4323 draw_acc(0, 0, note.acc)
4324 g_close()
4325 }
4326 }
4327 if (old_color != false)
4328 set_color(old_color)
4329 }
4330
4331 /* -- draw a note or a chord -- */
4332 /* (the staves are defined) */
4333 function draw_note(s,
4334 fl) { // draw flags
4335 var s2, i, m, y, slen, c, nflags,
4336 y_tb = new Array(s.nhd + 1),
4337 note = s.notes[s.stem < 0 ? s.nhd : 0], // master note head
4338 x = s.x,
4339 x_st = s.x + note.shhd * stv_g.scale,
4340 y = y_head(s, note),
4341 staffb = staff_tb[s.st].y
4342
4343 if (s.dots)
4344 setdoty(s, y_tb)
4345
4346 /* draw the stem and flags */
4347 if (!s.stemless) {
4348 slen = s.ys - s.y;
4349 nflags = s.nflags
4350 if (s.ntrem)
4351 nflags -= s.ntrem
4352 if (!fl || nflags <= 0) { /* stem only */
4353 if (s.nflags > 0) { /* (fix for PS low resolution) */
4354 if (s.stem >= 0)
4355 slen -= 1
4356 else
4357 slen += 1
4358 }
4359 out_stem(x_st, y, slen, s.grace)
4360 } else { /* stem and flags */
4361 out_stem(x_st, y, slen, s.grace,
4362 nflags, s.fmt.straightflags)
4363 }
4364 } else if (s.xstem) { /* cross-staff stem */
4365 s2 = s.ts_prev;
4366 slen = (s2.stem > 0 ? s2.y : s2.ys) - s.y;
4367 slen += staff_tb[s2.st].y - staffb;
4368 out_stem(x_st, y, slen)
4369 }
4370
4371 /* draw the tremolo bars */
4372 if (fl && s.trem1) {
4373 var ntrem = s.ntrem || 0,
4374 x1 = x;
4375 slen = 3 * (s.notes[s.stem > 0 ? s.nhd : 0].pit - 18)
4376 if (s.head == C.FULL || s.head == C.EMPTY) {
4377 x1 += (s.grace ? GSTEM_XOFF : 3.5) * s.stem
4378 if (s.stem > 0)
4379 slen += 6 + 5.4 * ntrem
4380 else
4381 slen -= 6 + 5.4
4382 } else {
4383 if (s.stem > 0)
4384 slen += 5 + 5.4 * ntrem
4385 else
4386 slen -= 5 + 5.4
4387 }
4388 slen /= s.p_v.scale;
4389 out_trem(x1, staffb + slen, ntrem)
4390 }
4391
4392 /* draw the note heads */
4393 for (m = 0; m <= s.nhd; m++)
4394 draw_basic_note(s, m, y_tb)
4395 }
4396
4397 // find where to start a long decoration
4398 function prev_scut(s) {
4399 while (s.prev) {
4400 s = s.prev
4401 if (s.rbstart)
4402 return s
4403 }
4404
4405 /* return a symbol of any voice starting before the start of the voice */
4406 s = s.p_v.sym
4407 while (s.type != C.CLEF)
4408 s = s.ts_prev /* search a main voice */
4409 if (s.next && s.next.type == C.KEY)
4410 s = s.next
4411 if (s.next && s.next.type == C.METER)
4412 return s.next
4413 return s
4414 }
4415
4416 /* -- decide whether a slur goes up or down (same voice) -- */
4417 function slur_direction(k1, k2) {
4418 var s, some_upstem, low, dir
4419
4420 // check if slur sequence in a multi-voice staff
4421 function slur_multi(s1, s2) {
4422 // while (1) {
4423 // if (s1.multi) // if multi voice
4424 // //fixme: may change
4425 // return s1.multi
4426 // if (s1 == s2)
4427 // break
4428 // s1 = s1.next
4429 // }
4430 if (s1.multi)
4431 return s1.multi
4432 if (s2.multi)
4433 return s2.multi
4434 return 0
4435 } // slur_multi()
4436
4437 if (k1.grace && k1.stem > 0)
4438 return -1
4439
4440 dir = slur_multi(k1, k2)
4441 if (dir)
4442 return dir
4443
4444 for (s = k1; ; s = s.next) {
4445 if (s.type == C.NOTE) {
4446 if (!s.stemless) {
4447 if (s.stem < 0)
4448 return 1
4449 some_upstem = true
4450 }
4451 if (s.notes[0].pit < 22) /* if under middle staff */
4452 low = true
4453 }
4454 // if (s == k2)
4455 if (s.time == k2.time) // (k2 may be a grace note)
4456 break
4457 }
4458 if (!some_upstem && !low)
4459 return 1
4460 return -1
4461 }
4462
4463 /* -- output a slur / tie -- */
4464 function slur_out(x1, y1, x2, y2, dir, height, dotted) {
4465 var dx, dy, dz,
4466 alfa = .3,
4467 beta = .45;
4468
4469 /* for wide flat slurs, make shape more square */
4470 dy = y2 - y1
4471 if (dy < 0)
4472 dy = -dy;
4473 dx = x2 - x1
4474 if (dx > 40. && dy / dx < .7) {
4475 alfa = .3 + .002 * (dx - 40.)
4476 if (alfa > .7)
4477 alfa = .7
4478 }
4479
4480 /* alfa, beta, and height determine Bezier control points pp1,pp2
4481 *
4482 * X====alfa===|===alfa=====X
4483 * / | \
4484 * pp1 | pp2
4485 * / height \
4486 * beta | beta
4487 * / | \
4488 * p1 m p2
4489 *
4490 */
4491
4492 var mx = .5 * (x1 + x2),
4493 my = .5 * (y1 + y2),
4494 xx1 = mx + alfa * (x1 - mx),
4495 yy1 = my + alfa * (y1 - my) + height;
4496 xx1 = x1 + beta * (xx1 - x1);
4497 yy1 = y1 + beta * (yy1 - y1)
4498
4499 var xx2 = mx + alfa * (x2 - mx),
4500 yy2 = my + alfa * (y2 - my) + height;
4501 xx2 = x2 + beta * (xx2 - x2);
4502 yy2 = y2 + beta * (yy2 - y2);
4503
4504 // dy = 1.6 * dir
4505 dy = 2 * dir;
4506 dz = .2 + .001 * dx
4507 if (dz > .6)
4508 dz = .6;
4509 dz *= dir
4510 dx *= .03
4511 // if (dx > 10.)
4512 // dx = 10.
4513
4514 // var scale_y = stv_g.st < 0 ? stv_g.scale : 1
4515 var scale_y = 1 // (see set_dscale())
4516 if (!dotted)
4517 output += '<path d="M'
4518 else
4519 output += '<path class="stroke" stroke-dasharray="5,5" d="M';
4520 out_sxsy(x1, ' ', y1);
4521 output += 'c' +
4522 ((xx1 - x1) / stv_g.scale).toFixed(1) + ' ' +
4523 ((y1 - yy1) / scale_y).toFixed(1) + ' ' +
4524 ((xx2 - x1) / stv_g.scale).toFixed(1) + ' ' +
4525 ((y1 - yy2) / scale_y).toFixed(1) + ' ' +
4526 ((x2 - x1) / stv_g.scale).toFixed(1) + ' ' +
4527 ((y1 - y2) / scale_y).toFixed(1)
4528
4529 if (!dotted)
4530 output += '\n\tv' +
4531 (-dz).toFixed(1) + 'c' +
4532 ((xx2 - dx - x2) / stv_g.scale).toFixed(1) + ' ' +
4533 ((y2 + dz - yy2 - dy) / scale_y).toFixed(1) + ' ' +
4534 ((xx1 + dx - x2) / stv_g.scale).toFixed(1) + ' ' +
4535 ((y2 + dz - yy1 - dy) / scale_y).toFixed(1) + ' ' +
4536 ((x1 - x2) / stv_g.scale).toFixed(1) + ' ' +
4537 ((y2 - y1) / scale_y).toFixed(1)
4538 output += '"/>\n'
4539 }
4540
4541 // draw a slur between two chords / notes
4542 /* (the staves are not yet defined) */
4543 /* (delayed output) */
4544 /* (not a pretty routine, this) */
4545 function draw_slur(path, // list of symbols under the slur
4546 sl, // slur variables: type, end symbol, note
4547 recurr) { // recurrent call when slur on two staves
4548 var i,
4549 k, g, x1, y1, x2, y2, height, addy, s_st2,
4550 a, y, z, h, dx, dy,
4551 ty = sl.ty,
4552 dir = (ty & 0x07) == C.SL_ABOVE ? 1 : -1,
4553 n = path.length,
4554 i1 = 0,
4555 i2 = n - 1,
4556 not1 = sl.nts, // if the slur starts on a note
4557 k1 = path[0],
4558 k2 = path[i2],
4559 nn = 1
4560
4561 set_dscale(k1.st)
4562
4563 for (i = 1; i < n; i++) {
4564 k = path[i]
4565 if (k.type == C.NOTE || k.type == C.REST) {
4566 nn++
4567 if (k.st != k1.st
4568 && !s_st2)
4569 s_st2 = k
4570 }
4571 }
4572
4573 // if slur on 2 staves, define it, but don't output it now
4574 // this will be done in draw_sl2()
4575 if (s_st2 && !recurr) { // if not 2nd call to draw_slur()
4576 if (!gene.a_sl)
4577 gene.a_sl = []
4578
4579 // replace the symbols of the other staff
4580 // by symbols in the current staff but with updated y offsets
4581 h = 24 + k1.fmt.sysstaffsep // delta y
4582 if (s_st2.st > k1.st)
4583 h = -h
4584 for (i = 0; i < n; i++) {
4585 k = path[i]
4586 if (k.st == k1.st) {
4587 if (k.dur)
4588 a = k // (used for types // and \\)
4589 continue
4590 }
4591 k = clone(k)
4592 if (path[i] == s_st2)
4593 s_st2 = k
4594 path[i] = k
4595 if (k.dur) {
4596 k.notes = clone(k.notes)
4597 k.notes[0] = clone(k.notes[0])
4598 if (sl.ty & C.SL_CENTER) {
4599 if (k.st != a.st) {
4600 sl.ty = (sl.ty & ~0x07)
4601 | (a.st < k.st
4602 ? C.SL_BELOW
4603 : C.SL_ABOVE)
4604 z = k1.ymn
4605 h = k2.ymx
4606 if (k.st < a.st) {
4607 for (i1 = 1; i1 < i; i1++) {
4608 a = path[i1]
4609 if (a.ymn < z)
4610 z = a.ymn
4611 }
4612 for (i1 = i; i1 < i2; i1++) {
4613 a = path[i1]
4614 if (a.ymx > h)
4615 h = a.ymx
4616 }
4617 } else {
4618 for (i1 = 1; i1 < i; i1++) {
4619 a = path[i1]
4620 if (a.ymx > h)
4621 h = a.ymx
4622 }
4623 for (i1 = i; i1 < i2; i1++) {
4624 a = path[i1]
4625 if (a.ymn < z)
4626 z = a.ymn
4627 }
4628 }
4629 h += z
4630 a = k
4631 }
4632 k.y = h - k.y
4633 k.notes[0].pit = (k.y / 3 | 0) + 18
4634 k.ys = h - k.ys
4635 y = k.ymx
4636 k.ymx = h - k.ymn
4637 k.ymn = h - y
4638 k.stem = -k.stem
4639 } else {
4640 k.notes[0].pit += h / 3 | 0
4641 k.ys += h
4642 k.y += h
4643 k.ymx += h
4644 k.ymn += h
4645 }
4646 }
4647 // k.st = k1.st // keep the staff number for draw_sl2()
4648 }
4649
4650 ty = k1.st > s_st2.st ? '/' : '\\'
4651 if (sl.ty & C.SL_CENTER)
4652 ty = ty + ty // type = // or \\
4653 else if (k1.st == k2.st)
4654 ty = ty == '/' ? '/\\' : '\\/' // type = /\ or \/
4655 else
4656 ty += dir > 0 ? '+' : '-' // type = .+ or .-
4657
4658 var savout = output
4659 output = ""
4660 draw_slur(path, sl, 1 /*true*/)
4661 gene.a_sl.push([k1, s_st2, ty, output])
4662 output = savout
4663 return
4664 }
4665
4666 /* fix endpoints */
4667 x1 = k1.x
4668 if (k1.notes && k1.notes[0].shhd)
4669 x1 += k1.notes[0].shhd;
4670 x2 = k2.x
4671 if (k2.notes)
4672 x2 += k2.notes[0].shhd
4673
4674 if (not1) { // start on a note
4675 y1 = 3 * (not1.pit - 18) + 2 * dir
4676 x1 += 3
4677 } else { // start on a chord
4678 y1 = dir > 0 ? k1.ymx + 2 : k1.ymn - 2
4679 if (k1.type == C.NOTE) {
4680 if (dir > 0) {
4681 if (k1.stem > 0) {
4682 x1 += 5
4683 if (k1.beam_end
4684 && k1.nflags >= -1 /* if with a stem */
4685 //fixme: check if at end of tuplet
4686 && !k1.in_tuplet) {
4687 if (k1.nflags > 0) {
4688 x1 += 2;
4689 y1 = k1.ys - 3
4690 } else {
4691 y1 = k1.ys - 6
4692 }
4693 } else {
4694 y1 = k1.ys + 3
4695 }
4696 } else {
4697 y1 = k1.y + 8
4698 }
4699 } else {
4700 if (k1.stem < 0) {
4701 x1 -= 1
4702 if (k2.grace) {
4703 y1 = k1.y - 8
4704 } else if (k1.beam_end
4705 && k1.nflags >= -1
4706 && (!k1.in_tuplet
4707 || k1.ys < y1 + 3)) {
4708 if (k1.nflags > 0) {
4709 x1 += 2;
4710 y1 = k1.ys + 3
4711 } else {
4712 y1 = k1.ys + 6
4713 }
4714 } else {
4715 y1 = k1.ys - 3
4716 }
4717 } else {
4718 y1 = k1.y - 5
4719 }
4720 }
4721 }
4722 }
4723
4724 if (sl.nte) { // slur ending on a note
4725 y2 = 3 * (sl.nte.pit - 18) + 2 * dir
4726 x2 -= 3
4727 } else { // end on a chord
4728 y2 = dir > 0 ? k2.ymx + 2 : k2.ymn - 2
4729 if (k2.type == C.NOTE) {
4730 if (dir > 0) {
4731 if (k2.stem > 0) {
4732 x2 += 1
4733 if (k2.beam_st
4734 && k2.nflags >= -1
4735 && !k2.in_tuplet)
4736 y2 = k2.ys - 6
4737 else
4738 y2 = k2.ys + 3
4739 } else {
4740 y2 = k2.y + 8
4741 }
4742 } else {
4743 if (k2.stem < 0) {
4744 x2 -= 5
4745 if (k2.beam_st
4746 && k2.nflags >= -1
4747 && !k2.in_tuplet)
4748 // || k2.ys < y2 + 3))
4749 y2 = k2.ys + 6
4750 else
4751 y2 = k2.ys - 3
4752 } else {
4753 y2 = k2.y - 5
4754 }
4755 }
4756 }
4757 }
4758
4759 if (k1.type != C.NOTE) {
4760 y1 = y2 + 1.2 * dir;
4761 x1 = k1.x + k1.wr * .5
4762 if (x1 > x2 - 12)
4763 x1 = x2 - 12
4764 }
4765
4766 if (k2.type != C.NOTE) {
4767 if (k1.type == C.NOTE)
4768 y2 = y1 + 1.2 * dir
4769 else
4770 y2 = y1
4771 if (k1 != k2)
4772 x2 = k2.x - k2.wl * .3
4773 }
4774
4775 if (nn >= 3) {
4776 k = path[1]
4777 if (k.type != C.BAR
4778 && k.x < x1 + 48) {
4779 if (dir > 0) {
4780 y = k.ymx - 2
4781 if (y1 < y)
4782 y1 = y
4783 } else {
4784 y = k.ymn + 2
4785 if (y1 > y)
4786 y1 = y
4787 }
4788 }
4789 k = path[i2 - 1]
4790 if (k.type != C.BAR
4791 && k.x > x2 - 48) {
4792 if (dir > 0) {
4793 y = k.ymx - 2
4794 if (y2 < y)
4795 y2 = y
4796 } else {
4797 y = k.ymn + 2
4798 if (y2 > y)
4799 y2 = y
4800 }
4801 }
4802 }
4803
4804 a = (y2 - y1) / (x2 - x1) /* slur steepness */
4805 if (a > SLUR_SLOPE || a < -SLUR_SLOPE) {
4806 a = a > SLUR_SLOPE ? SLUR_SLOPE : -SLUR_SLOPE
4807 if (a * dir > 0)
4808 y1 = y2 - a * (x2 - x1)
4809 else
4810 y2 = y1 + a * (x2 - x1)
4811 }
4812
4813 /* for big vertical jump, shift endpoints */
4814 y = y2 - y1
4815 if (y > 8)
4816 y = 8
4817 else if (y < -8)
4818 y = -8
4819 z = y
4820 if (z < 0)
4821 z = -z;
4822 dx = .5 * z;
4823 dy = .3 * y
4824 if (y * dir > 0) {
4825 x2 -= dx;
4826 y2 -= dy
4827 } else {
4828 x1 += dx;
4829 y1 += dy
4830 }
4831
4832 /* special case for grace notes */
4833 if (k1.grace)
4834 x1 = k1.x - GSTEM_XOFF * .5
4835 if (k2.grace)
4836 x2 = k2.x + GSTEM_XOFF * .5
4837
4838 h = 0;
4839 a = (y2 - y1) / (x2 - x1)
4840 if (k1 != k2
4841 && k1.v == k2.v) {
4842 addy = y1 - a * x1
4843 for (i = 1; i < i2; i++) {
4844 k = path[i]
4845 switch (k.type) {
4846 case C.NOTE:
4847 case C.REST:
4848 if (dir > 0) {
4849 y = 3 * (k.notes[k.nhd].pit - 18) + 6
4850 if (y < k.ymx)
4851 y = k.ymx;
4852 y -= a * k.x + addy
4853 if (y > h)
4854 h = y
4855 } else {
4856 y = 3 * (k.notes[0].pit - 18) - 6
4857 if (y > k.ymn)
4858 y = k.ymn;
4859 y -= a * k.x + addy
4860 if (y < h)
4861 h = y
4862 }
4863 break
4864 case C.GRACE:
4865 for (g = k.extra; g; g = g.next) {
4866 if (dir > 0) {
4867 // y = 3 * (g.notes[g.nhd].pit - 18) + 6
4868 // if (y < g.ymx)
4869 y = g.ymx;
4870 y -= a * g.x + addy
4871 if (y > h)
4872 h = y
4873 } else {
4874 // y = 3 * (g.notes[0].pit - 18) - 6
4875 // if (y > g.ymn)
4876 y = g.ymn;
4877 y -= a * g.x + addy
4878 if (y < h)
4879 h = y
4880 }
4881 }
4882 break
4883 }
4884 }
4885 y1 += .45 * h;
4886 y2 += .45 * h;
4887 h *= .65
4888 }
4889
4890 if (nn > 3)
4891 height = (.08 * (x2 - x1) + 12) * dir
4892 else
4893 height = (.03 * (x2 - x1) + 8) * dir
4894 if (dir > 0) {
4895 if (height < 3 * h)
4896 height = 3 * h
4897 if (height > 40)
4898 height = 40
4899 } else {
4900 if (height > 3 * h)
4901 height = 3 * h
4902 if (height < -40)
4903 height = -40
4904 }
4905
4906 y = y2 - y1
4907 if (y < 0)
4908 y = -y
4909 if (dir > 0) {
4910 if (height < .8 * y)
4911 height = .8 * y
4912 } else {
4913 if (height > -.8 * y)
4914 height = -.8 * y
4915 }
4916 height *= k1.fmt.slurheight;
4917
4918 // anno_start(k1_o, 'slur');
4919 slur_out(x1, y1, x2, y2, dir, height, ty & C.SL_DOTTED);
4920 // anno_stop(k1_o, 'slur');
4921
4922 /* have room for other symbols */
4923 dx = x2 - x1;
4924 a = (y2 - y1) / dx;
4925 /*fixme: it seems to work with .4, but why?*/
4926 // addy = y1 - a * x1 + .4 * height
4927 //fixme: the following code seems better!
4928 addy = y1 - a * x1
4929 if (height > 0)
4930 addy += 3 * Math.sqrt(height) - 2
4931 else
4932 addy -= 3 * Math.sqrt(-height) - 2
4933 for (i = 0; i <= i2; i++) {
4934 k = path[i]
4935 if (k.st != k1.st || k.type == C.BAR)
4936 continue
4937 y = a * k.x + addy
4938 if (k.ymx < y)
4939 k.ymx = y
4940 else if (k.ymn > y)
4941 k.ymn = y
4942 if (recurr) // no room when slur on 2 staves
4943 continue
4944 if (i == i2) {
4945 dx = x2
4946 if (sl.nte)
4947 dx -= 5;
4948 } else {
4949 dx = k.x + k.wr
4950 }
4951 if (i != 0)
4952 x1 = k.x
4953 if (!i || i == i2)
4954 y -= height / 3
4955 dx -= x1 - k.wl
4956 y_set(k1.st, dir > 0, x1 - k.wl, dx, y)
4957 }
4958 }
4959
4960 /* -- draw the slurs between 2 symbols --*/
4961 function draw_slurs(s, last) {
4962 var gr1, i, m, note, sls, nsls
4963
4964 // draw a slur knowing the start and stop elements
4965 function draw_sls(s, // start symbol
4966 sl) { // slur variables
4967 var k, v, i, dir, s3,
4968 path = [],
4969 s2 = sl.se // slur end
4970
4971 if (last && s2.time > last.time)
4972 return // will be drawn next time
4973
4974 // handle slurs without start or end
4975 switch (sl.loc) {
4976 case 'i': // no start
4977 s = prev_scut(s)
4978 break
4979 case 'o': // no end
4980 for (s3 = gr1 || s; s3.ts_next; s3 = s3.ts_next)
4981 ;
4982 s2 = s3
4983 for (; s3; s3 = s3.ts_prev) {
4984 if (s3.v == s.v) {
4985 s2 = s3
4986 break
4987 }
4988 if (s3.st == s.st)
4989 s2 = s3
4990 if (s3.ts_prev.time != s2.time)
4991 break
4992 }
4993 break
4994 }
4995
4996 // if the slur continues on the next music line,
4997 // stop it at the end of the current line
4998 if (s.p_v.s_next && s2.time >= tsnext.time) {
4999 if (s2.time == tsnext.time) {
5000 if (s2.grace) {
5001 for (s3 = tsnext; s3 && s3.time == s2.time; s3 = s3.ts_next) {
5002 if (s3.type == C.GRACE) {
5003 s3 = null
5004 break
5005 }
5006 }
5007 } else {
5008 for (s3 = tsnext; s3.time == s2.time; s3 = s3.ts_next) {
5009 if (s3 == s2) {
5010 s3 = null // end of slur in next line
5011 break
5012 }
5013 }
5014 }
5015 } else {
5016 s3 = null
5017 }
5018 if (!s3) {
5019 s.p_v.sls.push(sl); // continuation on next line
5020 s2 = s.p_v.s_next.prev // one voice
5021 while (s2.next)
5022 s2 = s2.next; // search the ending bar
5023 sl = Object.create(sl); // new slur
5024 }
5025 }
5026
5027 // set the slur position
5028 switch (sl.ty & 0x07) {
5029 case C.SL_ABOVE: dir = 1; break
5030 case C.SL_BELOW: dir = -1; break
5031 default:
5032 dir = s.v != s2.v ?
5033 1 : // always above ?
5034 slur_direction(s, s2)
5035 sl.ty &= ~0x07
5036 sl.ty |= dir > 0 ? C.SL_ABOVE : C.SL_BELOW
5037 break
5038 }
5039
5040 // build the path of the symbols under the slur
5041 if (s.v == s2.v) {
5042 v = s.v
5043 } if (!cur_sy.voices[s.v] || !cur_sy.voices[s2.v]) {
5044 v = s.v > s2.v ? s.v : s2.v
5045 } else if (dir * // if slur on first voice
5046 (cur_sy.voices[s.v].range <= cur_sy.voices[s2.v].range ?
5047 1 : -1) > 0)
5048 v = s.v
5049 else
5050 v = s2.v
5051
5052 if (gr1 // if start on a grace note
5053 && !(s2.grace // and not end in the same
5054 && s.v == s2.v // grace note sequence
5055 && s.time == s2.time)) {
5056 do {
5057 path.push(s); // add all grace notes
5058 s = s.next
5059 } while (s);
5060 s = gr1.next
5061 } else {
5062 path.push(s);
5063 if (s.grace)
5064 s = s.next
5065 else
5066 s = s.ts_next
5067 }
5068
5069 if (!s2.grace) { // if end on a normal note
5070 while (s) {
5071 if (s.v == v)
5072 path.push(s)
5073 if (s == s2)
5074 break
5075 s = s.ts_next
5076 }
5077 } else if (s.grace) { // if start/end in the same sequence
5078 while (1) {
5079 // if (s.v == v)
5080 path.push(s)
5081 if (s == s2)
5082 break
5083 s = s.next
5084 }
5085 } else { // end on a grace note
5086 k = s2
5087 while (k.prev)
5088 k = k.prev // .extra pointer
5089 while (1) {
5090 if (s.v == v)
5091 path.push(s)
5092 if (s.extra == k)
5093 break
5094 s = s.ts_next
5095 }
5096 s = k
5097 while (1) {
5098 path.push(s)
5099 if (s == s2)
5100 break
5101 s = s.next
5102 }
5103 }
5104
5105 // if some nested slurs/tuplets, draw them
5106 for (i = 1; i < path.length - 1; i++) {
5107 s = path[i]
5108 if (s.sls)
5109 draw_slurs(s, last)
5110 if (s.tp)
5111 draw_tuplet(s)
5112 }
5113 draw_slur(path, sl)
5114 return 1 // slur drawn, remove it
5115 } // draw_sls()
5116
5117 // code of draw_slurs()
5118 while (1) {
5119 if (!s || s == last) {
5120 if (!gr1 // if end of grace notes
5121 || !(s = gr1.next) // return to normal notes
5122 || s == last)
5123 break
5124 gr1 = null
5125 }
5126 if (s.type == C.GRACE) { // if start of grace notes
5127 gr1 = s; // continue in the grace note sequence
5128 s = s.extra
5129 continue
5130 }
5131 if (s.sls) { // slurs from the chord
5132 sls = s.sls
5133 s.sls = null
5134 nsls = []
5135 for (i = 0; i < sls.length; i++) {
5136 if (!draw_sls(s, sls[i]))
5137 nsls.push(sls[i])
5138 }
5139 if (nsls.length)
5140 s.sls = nsls
5141 }
5142 s = s.next
5143 }
5144 }
5145
5146 /* -- draw a tuplet -- */
5147 /* (the staves are not yet defined) */
5148 /* (delayed output) */
5149 /* See http://moinejf.free.fr/abcm2ps-doc/tuplets.html
5150 * for the value of 'tp.f' */
5151 function draw_tuplet(s1) {
5152 var s2, s3, g, stu, std, nb_only,
5153 x1, x2, y1, y2, xm, ym, a, s0, yy, yx, dy, a, dir, r,
5154 tp = s1.tp.shift() // tuplet parameters
5155
5156 if (!s1.tp.length)
5157 delete s1.tp // last tuplet
5158
5159 // treat the slurs and the nested tuplets
5160 stu = std = s1.st
5161 for (s2 = s1; s2; s2 = s2.next) {
5162 switch (s2.type) {
5163 case C.GRACE:
5164 if (!s2.sl1)
5165 continue
5166 for (g = s2.extra; g; g = g.next) {
5167 if (g.sls)
5168 draw_slurs(g)
5169 }
5170 // fall thru
5171 default:
5172 continue
5173 case C.NOTE:
5174 case C.REST:
5175 break
5176 }
5177 if (s2.sls)
5178 draw_slurs(s2)
5179 if (s2.st < stu) {
5180 std = stu
5181 stu = s2.st
5182 } else if (s2.st > std) {
5183 std = s2.st
5184 }
5185 if (s2.tp)
5186 draw_tuplet(s2)
5187 if (s2.tpe)
5188 break
5189 }
5190
5191 if (s2)
5192 s2.tpe--
5193
5194 if (tp.f[0] == 1) // if 'when' == never
5195 return // accept tuplets on many lines
5196
5197 if (!s2) {
5198 error(1, s1, "No end of tuplet in this music line")
5199 return
5200 }
5201
5202 dir = tp.f[3] // 'where'
5203 if (!dir) { // if auto
5204 s3 = s1
5205 while (s3 && !s3.stem) // (may have tuplets of rests!)
5206 s3 = s3.next
5207 dir = (s3 && s3.stem < 0) ? C.SL_BELOW : C.SL_ABOVE
5208 }
5209 set_dscale(dir == C.SL_ABOVE ? stu : std)
5210
5211 if (s1 == s2 // tuplet with 1 note (!)
5212 || tp.f[1] == 2) { // what == nothing
5213 nb_only = true
5214 } else if (tp.f[1] == 1) { /* 'what' == slur */
5215 nb_only = true;
5216 draw_slur([s1, s2], {ty: dir})
5217 } else {
5218
5219 /* search if a bracket is needed */
5220 if (tp.f[0] != 2 // if 'when' != always
5221 && s1.type == C.NOTE && s2.type == C.NOTE) {
5222 nb_only = true
5223 for (s3 = s1; ; s3 = s3.next) {
5224 if (s3.type != C.NOTE
5225 && s3.type != C.REST) {
5226 if (s3.type == C.GRACE
5227 || s3.type == C.SPACE)
5228 continue
5229 nb_only = false
5230 break
5231 }
5232 if (s3 == s2)
5233 break
5234 if (s3.beam_end) {
5235 nb_only = false
5236 break
5237 }
5238 }
5239 if (nb_only
5240 && !s1.beam_st
5241 && !s1.beam_br1
5242 && !s1.beam_br2) {
5243 for (s3 = s1.prev; s3; s3 = s3.prev) {
5244 if (s3.type == C.NOTE
5245 || s3.type == C.REST) {
5246 if (s3.nflags >= s1.nflags)
5247 nb_only = false
5248 break
5249 }
5250 }
5251 }
5252 if (nb_only && !s2.beam_end) {
5253 for (s3 = s2.next; s3; s3 = s3.next) {
5254 if (s3.type == C.NOTE
5255 || s3.type == C.REST) {
5256 if (!s3.beam_br1
5257 && !s3.beam_br2
5258 && s3.nflags >= s2.nflags)
5259 nb_only = false
5260 break
5261 }
5262 }
5263 }
5264 }
5265 }
5266
5267 /* if number only, draw it */
5268 if (nb_only) {
5269 if (tp.f[2] == 1) /* if 'which' == none */
5270 return
5271 set_font("tuplet")
5272 xm = (s2.x + s1.x) / 2
5273 if (dir == C.SL_ABOVE) // 8 = width around the middle
5274 ym = y_get(stu, 1, xm - 4, 8)
5275 else
5276 ym = y_get(std, 0, xm - 4, 8) -
5277 gene.curfont.size
5278
5279 if (s1.stem * s2.stem > 0) {
5280 if (s1.stem > 0)
5281 xm += 4
5282 else
5283 xm -= 4
5284 }
5285
5286 yy = ym + gene.curfont.size * .22
5287 if (tp.f[2] == 0) // if 'which' == number
5288 xy_str(xm, yy, tp.p.toString(), 'c')
5289 else
5290 xy_str(xm, yy, tp.p + ':' + tp.q, 'c')
5291
5292 for (s3 = s1; ; s3 = s3.next) {
5293 if (s3.x >= xm)
5294 break
5295 }
5296 if (dir == C.SL_ABOVE) {
5297 ym += gene.curfont.size
5298 if (s3.ymx < ym)
5299 s3.ymx = ym;
5300 y_set(stu, 1, xm - 3, 6, ym)
5301 } else {
5302 if (s3.ymn > ym)
5303 s3.ymn = ym;
5304 y_set(std, 0, xm - 3, 6, ym)
5305 }
5306 return
5307 }
5308
5309 // here, 'what' is square bracket
5310
5311 /*fixme: two staves not treated*/
5312 /*fixme: to optimize*/
5313
5314 // first, get the x offsets
5315 x1 = s1.x - 4
5316
5317 // end the bracket according to the last note duration
5318 if (s2.dur > s2.prev.dur) {
5319 s3 = s2.next
5320 if (!s3 // maybe a note in an overlay voice
5321 || s3.time != s2.time + s2.dur) {
5322 for (s3 = s2.ts_next; s3; s3 = s3.ts_next) {
5323 if (s3.seqst
5324 && s3.time >= s2.time + s2.dur)
5325 break
5326 }
5327 }
5328 //fixme: s3 cannot be null (bar at end of staff)
5329 x2 = s3 ? s3.x - s3.wl - 5 : realwidth - 6
5330 } else {
5331 x2 = s2.x + 4
5332 r = s2.stem >= 0 ? 0 : s2.nhd
5333 if (s2.notes[r].shhd > 0)
5334 x2 += s2.notes[r].shhd
5335 if (s2.st == stu
5336 && s2.stem > 0)
5337 x2 += 3.5
5338 }
5339
5340 // above
5341 if (dir == C.SL_ABOVE) {
5342 if (s1.st >= s2.st) {
5343 if (s1.stem > 0)
5344 x1 += 3
5345 ym = y_get(s1.st, 1, x1 - 4, 8)
5346 y1 = ym > staff_tb[s1.st].topbar + 2
5347 ? ym
5348 : staff_tb[s1.st].topbar + 2
5349 } else {
5350 y1 = staff_tb[s1.st].topbar + 2
5351 }
5352 if (s2.st >= s1.st) {
5353 ym = y_get(s2.st, 1, x2 - 4, 8)
5354 y2 = ym > staff_tb[s2.st].topbar + 2
5355 ? ym
5356 : staff_tb[s2.st].topbar + 2
5357 } else {
5358 y2 = staff_tb[s2.st].topbar + 2
5359 }
5360
5361 xm = .5 * (x1 + x2);
5362 ym = .5 * (y1 + y2);
5363
5364 a = (y2 - y1) / (x2 - x1);
5365 s0 = 3 * (s2.notes[s2.nhd].pit - s1.notes[s1.nhd].pit) / (x2 - x1)
5366 if (s0 > 0) {
5367 if (a < 0)
5368 a = 0
5369 else if (a > s0)
5370 a = s0
5371 } else {
5372 if (a > 0)
5373 a = 0
5374 else if (a < s0)
5375 a = s0
5376 }
5377 a = s1.fmt.beamslope * a / (s1.fmt.beamslope + Math.abs(a))
5378 if (a * a < .1 * .1)
5379 a = 0
5380
5381 /* shift up bracket if needed */
5382 dy = 0
5383 for (s3 = s1; ; s3 = s3.next) {
5384 if (!s3.dur /* not a note or a rest */
5385 || s3.st != stu) {
5386 if (s3 == s2)
5387 break
5388 continue
5389 }
5390 yy = ym + (s3.x - xm) * a;
5391 yx = y_get(stu, 1, s3.x - 4, 8) + 2
5392 if (yx - yy > dy)
5393 dy = yx - yy
5394 if (s3 == s2)
5395 break
5396 }
5397
5398 ym += dy;
5399 y1 = ym + a * (x1 - xm);
5400 y2 = ym + a * (x2 - xm);
5401
5402 /* shift the slurs / decorations */
5403 ym += 6
5404 for (s3 = s1; ; s3 = s3.next) {
5405 if (s3.st == stu) {
5406 yy = ym + (s3.x - xm) * a
5407 if (s3.ymx < yy)
5408 s3.ymx = yy
5409 y_set(stu, 1, s3.x - 3, 6, yy)
5410 }
5411 if (s3 == s2)
5412 break
5413 }
5414
5415 // below
5416 } else { /* lower voice of the staff: the bracket is below the staff */
5417 if (s1.st <= s2.st) {
5418 ym = y_get(s1.st, 0, x1 - 4, 8)
5419 y1 = ym < -2
5420 ? ym
5421 : -2
5422 } else {
5423 y1 = -2
5424 }
5425 if (s2.st <= s1.st) {
5426 if (s2.stem < 0)
5427 x2 -= 3
5428 ym = y_get(s2.st, 0, x2 - 4, 8)
5429 y2 = ym < -2
5430 ? ym
5431 : -2
5432 } else {
5433 y2 = -2
5434 }
5435
5436 xm = .5 * (x1 + x2);
5437 ym = .5 * (y1 + y2);
5438
5439 a = (y2 - y1) / (x2 - x1);
5440 s0 = 3 * (s2.notes[0].pit - s1.notes[0].pit) / (x2 - x1)
5441 if (s0 > 0) {
5442 if (a < 0)
5443 a = 0
5444 else if (a > s0)
5445 a = s0
5446 if (a > .35)
5447 a = .35
5448 } else {
5449 if (a > 0)
5450 a = 0
5451 else if (a < s0)
5452 a = s0
5453 if (a < -.35)
5454 a = -.35
5455 }
5456 if (a * a < .1 * .1)
5457 a = 0
5458
5459 /* shift down the bracket if needed */
5460 dy = 0
5461 for (s3 = s1; ; s3 = s3.next) {
5462 if (!s3.dur /* not a note nor a rest */
5463 || s3.st != std) {
5464 if (s3 == s2)
5465 break
5466 continue
5467 }
5468 yy = ym + (s3.x - xm) * a;
5469 yx = y_get(std, 0, s3.x - 4, 8)
5470 if (yx - yy < dy)
5471 dy = yx - yy
5472 if (s3 == s2)
5473 break
5474 }
5475
5476 ym += dy - 8
5477 y1 = ym + a * (x1 - xm);
5478 y2 = ym + a * (x2 - xm);
5479
5480 /* shift the slurs / decorations */
5481 ym -= 2
5482 for (s3 = s1; ; s3 = s3.next) {
5483 if (s3.st == std) {
5484 yy = ym + (s3.x - xm) * a
5485 if (s3.ymn > yy)
5486 s3.ymn = yy;
5487 y_set(std, 0, s3.x - 3, 6, yy)
5488 }
5489 if (s3 == s2)
5490 break
5491 }
5492 } /* lower voice */
5493
5494 if (tp.f[2] == 1) { /* if 'which' == none */
5495 out_tubr(x1, y1 + 4, x2 - x1, y2 - y1, dir == C.SL_ABOVE);
5496 return
5497 }
5498 out_tubrn(x1, y1, x2 - x1, y2 - y1, dir == C.SL_ABOVE,
5499 tp.f[2] == 0 ? tp.p.toString() : tp.p + ':' + tp.q);
5500
5501 if (dir == C.SL_ABOVE)
5502 y_set(stu, 1, xm - 3, 6, yy + 2)
5503 else
5504 y_set(std, 0, xm - 3, 6, yy)
5505 }
5506
5507 // -- draw a ties --
5508 function draw_tie(not1, not2,
5509 job) { // -: start and end
5510 // 1: no starting note
5511 // 2: no ending note
5512 // 3: no start for clef or staff change
5513 var m, x1, s, y, h, time,
5514 p = job == 2 ? not1.pit : not2.pit,
5515 dir = (not1.tie_ty & 0x07) == C.SL_ABOVE ? 1 : -1,
5516 s1 = not1.s,
5517 st = s1.st,
5518 s2 = not2.s,
5519 x2 = s2.x,
5520 sh = not1.shhd // head shift
5521
5522 for (m = 0; m < s1.nhd; m++)
5523 if (s1.notes[m] == not1)
5524 break
5525 if (dir > 0) {
5526 if (m < s1.nhd && p + 1 == s1.notes[m + 1].pit)
5527 if (s1.notes[m + 1].shhd > sh)
5528 sh = s1.notes[m + 1].shhd
5529 } else {
5530 if (m > 0 && p == s1.notes[m - 1].pit + 1)
5531 if (s1.notes[m - 1].shhd > sh)
5532 sh = s1.notes[m - 1].shhd
5533 }
5534 x1 = s1.x + sh // * .6
5535
5536 if (job != 2) {
5537 for (m = 0; m < s2.nhd; m++)
5538 if (s2.notes[m] == not2)
5539 break
5540 sh = s2.notes[m].shhd
5541 if (dir > 0) {
5542 if (m < s2.nhd && p + 1 == s2.notes[m + 1].pit)
5543 if (s2.notes[m + 1].shhd < sh)
5544 sh = s2.notes[m + 1].shhd
5545 } else {
5546 if (m > 0 && p == s2.notes[m - 1].pit + 1)
5547 if (s2.notes[m - 1].shhd < sh)
5548 sh = s2.notes[m - 1].shhd
5549 }
5550 x2 += sh // * .6
5551 }
5552
5553 switch (job) {
5554 default:
5555 if (p < not2.pit || dir < 0)
5556 p = not1.pit
5557 break
5558 case 3:
5559 dir = -dir
5560 case 1: /* no starting note */
5561 x1 = s2.prev ? (s2.prev.x + s2.wr) : s1.x
5562 if (s1.st != s2.st)
5563 st = s2.st
5564 x1 += (x2 - x1) * .4
5565 if (x1 > x2 - 20)
5566 x1 = x2 - 20
5567 break
5568 case 2: // no ending note
5569 x2 = s1.next ? s1.next.x : realwidth
5570 if (x2 != realwidth)
5571 x2 -= (x2 - x1) * .4
5572 if (x2 < x1 + 16)
5573 x2 = x1 + 16
5574 break
5575 }
5576 if (x2 - x1 > 20) {
5577 x1 += 3.5
5578 x2 -= 3.5
5579 } else {
5580 x1 += 1.5
5581 x2 -= 1.5
5582 }
5583
5584 if (s1.dots && !(not1.pit & 1)
5585 && ((dir > 0 && !s1.dot_low)
5586 || (dir < 0 && s1.dot_low)))
5587 x1 += 5
5588
5589 y = staff_tb[st].y + 3 * (p - 18) + .8 * dir
5590 h = (.15 * (x2 - x1) + 14) * dir * s1.fmt.tieheight
5591 // anno_start(k1, 'slur')
5592 slur_out(x1, y, x2, y, dir, h, not1.tie_ty & C.SL_DOTTED)
5593 // anno_stop(k1, 'slur')
5594 } // draw_tie()
5595
5596 /* -- draw all ties between neighboring notes -- */
5597 function draw_all_ties(p_voice) {
5598 var s, s1, s2, clef_chg, x, dx, m, not1, not2,
5599 tim2 = 0
5600
5601 /* search the start of ties */
5602 // clef_chg = false
5603 s1 = p_voice.sym
5604 set_color(s1.color)
5605 for ( ; s1; s1 = s1.next) {
5606 if (s1.ti2 // if end of tie
5607 && !s1.invis
5608 && s1.time != tim2) { // and new end
5609 for (m = 0; m <= s1.nhd; m++) {
5610 not2 = s1.notes[m]
5611 not1 = not2.tie_s
5612 if (!not1
5613 || not1.s.v != s1.v) // (already done)
5614 continue
5615 draw_tie(not1, not2, 1)
5616 }
5617 }
5618 if (!s1.ti1 // if not start of tie
5619 || s1.invis)
5620 continue
5621
5622 // get the end of the tie(s)
5623 if (s1.type == C.GRACE) {
5624 for (s = s1.extra; s; s = s.next) {
5625 for (m = 0; m <= s1.nhd; m++) {
5626 not1 = s.notes[m]
5627 not2 = not1.tie_e
5628 if (!not2)
5629 continue
5630 draw_tie(not1, not2)
5631 tim2 = not2.s.time
5632 }
5633 }
5634 continue
5635 }
5636
5637 for (m = 0; m <= s1.nhd; m++) {
5638 not1 = s1.notes[m]
5639 not2 = not1.tie_e
5640 if (!not2) {
5641 if (not1.tie_ty)
5642 draw_tie(not1, not1, 2)
5643 continue
5644 }
5645 s2 = not2.s
5646 if (tsnext && s2.time >= tsnext.time) { // if end in next line
5647 draw_tie(not1, not2, 2)
5648 continue
5649 }
5650 tim2 = s2.time
5651
5652 // check if some clef changes (can occur in an other voice)
5653 for (s = s1.ts_next; s != s2; s = s.ts_next) {
5654 if (s.st != s1.st)
5655 continue
5656 if (s.type == C.CLEF) {
5657 clef_chg = true
5658 break
5659 }
5660 }
5661
5662 /* ties with clef or staff change */
5663 if (clef_chg || s1.st != s2.st) {
5664 draw_tie(not1, not2, 2)
5665 draw_tie(not1, not2, 3)
5666 clef_chg = false
5667 } else {
5668 draw_tie(not1, not2)
5669 }
5670 }
5671 }
5672 }
5673
5674 /* -- draw the symbols near the notes -- */
5675 /* (the staves are not yet defined) */
5676 /* order:
5677 * - scaled
5678 * - beams
5679 * - decorations near the notes
5680 * - decorations tied to the notes
5681 * - tuplets and slurs
5682 * - not scaled
5683 * - measure numbers
5684 * - lyrics
5685 * - staff decorations
5686 * - chord symbols
5687 * - repeat brackets
5688 * - parts and tempos
5689 * The buffer output is delayed until the definition of the staff system
5690 */
5691 function draw_sym_near() {
5692 var p_voice, p_st, s, v, st, y, g, w, i, st, dx, top, bot, ymn,
5693 output_sav = output;
5694
5695 // set the staff offsets of a beam
5696 function set_yab(s1, s2) {
5697 var y,
5698 k = realwidth / YSTEP,
5699 i = (s1.x / k) | 0,
5700 j = (s2.x / k) | 0,
5701 a = (s1.ys - s2.ys) / (s1.xs - s2.xs),
5702 b = s1.ys - s1.xs * a,
5703 p_st = staff_tb[s1.st]
5704
5705 k *= a
5706 if (s1.stem > 0) {
5707 while (i <= j) {
5708 y = k * i + b
5709 if (p_st.top[i] < y)
5710 p_st.top[i] = y
5711 i++
5712 }
5713 } else {
5714 while (i <= j) {
5715 y = k * i + b
5716 if (p_st.bot[i] > y)
5717 p_st.bot[i] = y
5718 i++
5719 }
5720 }
5721 } // set_yab()
5722
5723 output = ""
5724 YSTEP = Math.ceil(realwidth / 2)
5725
5726 // initialize the min/max vertical offsets in the staves
5727 for (st = 0; st <= nstaff; st++) {
5728 p_st = staff_tb[st]
5729 p_st.top = new Float32Array(YSTEP)
5730 p_st.bot = new Float32Array(YSTEP)
5731 for (i = 0; i < YSTEP; i++) {
5732 p_st.top[i] = 0
5733 p_st.bot[i] = 24
5734 }
5735 // p_st.top.fill(0.)
5736 // p_st.bot.fill(24.)
5737 }
5738
5739 /* calculate the beams but don't draw them (the staves are not yet defined) */
5740 for (v = 0; v < voice_tb.length; v++) {
5741 var bm = {},
5742 first_note = true;
5743
5744 p_voice = voice_tb[v]
5745 for (s = p_voice.sym; s; s = s.next) {
5746 switch (s.type) {
5747 case C.GRACE:
5748 for (g = s.extra; g; g = g.next) {
5749 if (g.beam_st && !g.beam_end) {
5750 self.calculate_beam(bm, g)
5751 if (bm.s2)
5752 set_yab(g, bm.s2)
5753 }
5754 }
5755 if (!s.p_v.ckey.k_bagpipe // no slur when bagpipe
5756 && s.fmt.graceslurs
5757 && !s.gr_shift // tied to previous note
5758 && !s.sl1 // explicit slur
5759 && !s.ti1 // some tie
5760 && s.next
5761 && s.next.type == C.NOTE)
5762 grace_slur(s)
5763 break
5764 }
5765 }
5766 for (s = p_voice.sym; s; s = s.next) {
5767 switch (s.type) {
5768 case C.NOTE:
5769 if ((s.beam_st && !s.beam_end)
5770 || (first_note && !s.beam_st)) {
5771 first_note = false;
5772 self.calculate_beam(bm, s)
5773 if (bm.s2)
5774 set_yab(s, bm.s2)
5775 }
5776 break
5777 }
5778 }
5779 }
5780
5781 set_tie_room();
5782 draw_deco_near()
5783
5784 /* set the min/max vertical offsets */
5785 for (s = tsfirst; s; s = s.ts_next) {
5786 if (s.invis)
5787 continue
5788 switch (s.type) {
5789 case C.GRACE:
5790 for (g = s.extra; g; g = g.next) {
5791 y_set(s.st, true, g.x - 2, 4, g.ymx + 1);
5792 y_set(s.st, false, g.x - 2, 4,
5793 //fixme: hack for slur/accidental far under the staff
5794 // g.ymn - 1)
5795 g.ymn - 5)
5796 }
5797 continue
5798 case C.MREST:
5799 y_set(s.st, true, s.x + 16, 32, s.ymx + 2)
5800 continue
5801 default:
5802 y_set(s.st, true, s.x - s.wl, s.wl + s.wr, s.ymx + 2);
5803 y_set(s.st, false, s.x - s.wl, s.wl + s.wr, s.ymn - 2)
5804 // fall thru
5805 case C.PART:
5806 case C.TEMPO:
5807 case C.STAVES:
5808 continue
5809 case C.NOTE:
5810 break
5811 }
5812
5813 // (permit closer staves)
5814 if (s.stem > 0) {
5815 if (s.stemless) {
5816 dx = -5;
5817 w = 10
5818 } else if (s.beam_st) {
5819 dx = 3;
5820 w = s.beam_end ? 4 : 10
5821 } else {
5822 dx = -8;
5823 w = s.beam_end ? 11 : 16
5824 }
5825 y_set(s.st, true, s.x + dx, w, s.ymx);
5826 ymn = s.ymn
5827 if (s.notes[0].acc // set room for the accidental
5828 && ymn > 3 * (s.notes[0].pit - 18) - 9)
5829 ymn = 3 * (s.notes[0].pit - 18) - 9
5830 y_set(s.st, false, s.x - s.wl, s.wl + s.wr, ymn)
5831 } else {
5832 y_set(s.st, true, s.x - s.wl, s.wl + s.wr, s.ymx);
5833 if (s.stemless) {
5834 dx = -5;
5835 w = 10
5836 } else if (s.beam_st) {
5837 dx = -6;
5838 w = s.beam_end ? 4 : 10
5839 } else {
5840 dx = -8;
5841 w = s.beam_end ? 5 : 16
5842 }
5843 dx += s.notes[0].shhd;
5844 y_set(s.st, false, s.x + dx, w, s.ymn)
5845 }
5846
5847 /* have room for the accidentals */
5848 if (s.notes[s.nhd].acc) {
5849 y = 3 * (s.notes[s.nhd].pit - 18)
5850 + (s.notes[s.nhd].acc == -1 // flat
5851 ? 11 : 10)
5852 y_set(s.st, true, s.x - 10, 10, y)
5853 }
5854 if (s.notes[0].acc) {
5855 y = 3 * (s.notes[0].pit - 18)
5856 - (s.notes[0].acc == -1 // flat
5857 ? 5 : 10)
5858 y_set(s.st, false, s.x - 10, 10, y)
5859 }
5860 }
5861
5862 draw_deco_note()
5863
5864 for (v = 0; v < voice_tb.length; v++) {
5865 p_voice = voice_tb[v];
5866 s = p_voice.sym
5867 if (!s)
5868 continue
5869 set_color(s.color);
5870 st = p_voice.st;
5871 // if (st == undefined) {
5872 //error(1, s, "BUG: no staff for voice " + p_voice.id)
5873 // continue
5874 // }
5875
5876 // draw the slurs and tuplets
5877 for ( ; s; s = s.next) {
5878 if (s.play)
5879 continue
5880 if (s.tp)
5881 draw_tuplet(s)
5882 if (s.sls || s.sl1)
5883 draw_slurs(s)
5884 }
5885 }
5886 set_color()
5887
5888 /* set the top and bottom out of the staves */
5889 for (st = 0; st <= nstaff; st++) {
5890 p_st = staff_tb[st];
5891 top = p_st.topbar + 2;
5892 bot = p_st.botbar - 2
5893 /*fixme:should handle stafflines changes*/
5894 for (i = 0; i < YSTEP; i++) {
5895 if (top > p_st.top[i])
5896 p_st.top[i] = top
5897 if (bot < p_st.bot[i])
5898 p_st.bot[i] = bot
5899 }
5900 }
5901
5902 if (cfmt.measurenb >= 0)
5903 draw_measnb();
5904
5905 /* if any lyric, draw them now as unscaled */
5906 set_dscale(-1)
5907 // set_sscale(-1)
5908 for (v = 0; v < voice_tb.length; v++) {
5909 p_voice = voice_tb[v]
5910 if (p_voice.have_ly) {
5911 draw_all_lyrics()
5912 break
5913 }
5914 }
5915
5916 draw_deco_staff()
5917
5918 draw_partempo() // draw the parts and tempo indications if any
5919
5920 set_dscale(-1);
5921 output = output_sav
5922 }
5923
5924 /* -- draw the name/subname of the voices -- */
5925 function draw_vname(indent, stl) {
5926 var p_voice, n, st, v, a_p, p, y, h, h2,
5927 staff_d = []
5928
5929 if (!gene.vnt)
5930 return
5931
5932 // for (st = stl.length; st >= 0; st--) {
5933 for (st = stl.length; --st >= 0; ) {
5934 if (stl[st])
5935 break
5936 }
5937 if (st < 0)
5938 return
5939
5940 for (v = 0; v < voice_tb.length; v++) {
5941 p_voice = voice_tb[v]
5942 if (!cur_sy.voices[v])
5943 continue
5944 st = cur_sy.voices[v].st
5945 if (!stl[st])
5946 continue
5947 p = gene.vnt == 1 ? p_voice.nm : p_voice.snm
5948 if (!p)
5949 continue
5950 delete p_voice.new_name
5951 if (!staff_d[st])
5952 staff_d[st] = p
5953 else
5954 staff_d[st] += "\n" + p
5955 }
5956 if (!staff_d.length)
5957 return
5958 set_font("voice");
5959 h = gene.curfont.size
5960 h2 = h / 2
5961 indent = -indent * .5 /* center */
5962 for (st = 0; st < staff_d.length; st++) {
5963 if (!staff_d[st])
5964 continue
5965 a_p = staff_d[st].split("\n");
5966 y = staff_tb[st].y
5967 + staff_tb[st].topbar * .5
5968 * staff_tb[st].staffscale
5969 + h2 * (a_p.length - 2) + h *.22
5970
5971 // if instrument with 2 staves, center the voice name
5972 if ((cur_sy.staves[st].flags & OPEN_BRACE)
5973 && st + 1 < staff_tb.length
5974 && (cur_sy.staves[st + 1].flags & CLOSE_BRACE)
5975 && !staff_d[st + 1])
5976 y -= (staff_tb[st].y - staff_tb[st + 1].y) * .5
5977 for (n = 0; n < a_p.length; n++) {
5978 p = a_p[n];
5979 xy_str(indent, y, p, "c");
5980 y -= h
5981 }
5982 }
5983 }
5984
5985 // -- set the y offset of the staves and return the height of the whole system --
5986 function set_staff() {
5987 var i, st, prev_staff, v, fmt, s,
5988 y, staffsep, dy, maxsep, mbot, val, p_voice, p_staff,
5989 sy = cur_sy
5990
5991 // the last values of {,max}{,sys}staffsep are in the last format
5992 fmt = tsnext ? tsnext.fmt : cfmt
5993
5994 /* set the scale of the voices */
5995 for (v = 0; v < voice_tb.length; v++) {
5996 p_voice = voice_tb[v]
5997 if (p_voice.scale != 1)
5998 p_voice.scale_str =
5999 'transform="scale(' + p_voice.scale + ')"'
6000 }
6001
6002 // search the top staff
6003 for (st = 0; st <= nstaff; st++) {
6004 if (gene.st_print[st])
6005 break
6006 }
6007 y = 0
6008 if (st > nstaff)
6009 return y
6010
6011 p_staff = staff_tb[st]
6012 for (i = 0; i < YSTEP; i++) {
6013 val = p_staff.top[i]
6014 if (y < val)
6015 y = val
6016 }
6017
6018 /* set the vertical offset of the 1st staff */
6019 y *= p_staff.staffscale;
6020 staffsep = tsfirst.fmt.staffsep / 2 +
6021 p_staff.topbar * p_staff.staffscale
6022 if (y < staffsep)
6023 y = staffsep
6024 if (y < p_staff.ann_top) // absolute annotation
6025 y = p_staff.ann_top;
6026 p_staff.y = -y;
6027
6028 /* set the offset of the other staves */
6029 for (prev_staff = 0; prev_staff < st; prev_staff++)
6030 staff_tb[prev_staff].y = -y
6031 if (!gene.st_print[st]) // no staff
6032 return y
6033
6034 var sy_staff_prev = sy.staves[prev_staff]
6035 for (st++; st <= nstaff; st++) {
6036 if (!gene.st_print[st])
6037 continue
6038 p_staff = staff_tb[st]
6039 staffsep = sy_staff_prev.sep || fmt.sysstaffsep;
6040 maxsep = sy_staff_prev.maxsep || fmt.maxsysstaffsep;
6041
6042 dy = 0
6043 if (p_staff.staffscale == staff_tb[prev_staff].staffscale) {
6044 for (i = 0; i < YSTEP; i++) {
6045 val = p_staff.top[i] -
6046 staff_tb[prev_staff].bot[i]
6047 if (dy < val)
6048 dy = val
6049 }
6050 dy *= p_staff.staffscale
6051 } else {
6052 for (i = 0; i < YSTEP; i++) {
6053 val = p_staff.top[i] * p_staff.staffscale
6054 - staff_tb[prev_staff].bot[i]
6055 * staff_tb[prev_staff].staffscale
6056 if (dy < val)
6057 dy = val
6058 }
6059 }
6060 staffsep += p_staff.topbar * p_staff.staffscale
6061 if (dy < staffsep)
6062 dy = staffsep;
6063 maxsep += p_staff.topbar * p_staff.staffscale
6064 if (dy > maxsep)
6065 dy = maxsep;
6066 y += dy;
6067 p_staff.y = -y;
6068
6069 while (!gene.st_print[++prev_staff])
6070 staff_tb[prev_staff].y = -y
6071 while (1) {
6072 sy_staff_prev = sy.staves[prev_staff]
6073 if (sy_staff_prev)
6074 break
6075 sy = sy.next
6076 }
6077 }
6078 mbot = 0
6079 for (i = 0; i < YSTEP; i++) {
6080 val = staff_tb[prev_staff].bot[i]
6081 if (mbot > val)
6082 mbot = val
6083 }
6084 if (mbot > p_staff.ann_bot) // absolute annotation
6085 mbot = p_staff.ann_bot;
6086 mbot *= staff_tb[prev_staff].staffscale
6087
6088 /* output the staff offsets */
6089 for (st = 0; st <= nstaff; st++) {
6090 p_staff = staff_tb[st];
6091 dy = p_staff.y
6092 if (p_staff.staffscale != 1) {
6093 p_staff.scale_str =
6094 'transform="translate(0,' +
6095 (posy - dy).toFixed(1) + ') ' +
6096 'scale(' + p_staff.staffscale + ')"'
6097 }
6098 }
6099
6100 if (mbot == 0) {
6101 for (st = nstaff; st >= 0; st--) {
6102 if (gene.st_print[st])
6103 break
6104 }
6105 if (st < 0) /* no symbol in this system ! */
6106 return y
6107 }
6108 dy = -mbot;
6109 staffsep = fmt.staffsep * .5
6110 if (dy < staffsep)
6111 dy = staffsep;
6112 maxsep = fmt.maxstaffsep * .5
6113 if (dy > maxsep)
6114 dy = maxsep;
6115
6116 // return the height of the whole staff system
6117 return y + dy
6118 }
6119
6120 /* -- draw the staff systems and the measure bars -- */
6121 function draw_systems(indent) {
6122 var s, s2, st, x, x2, res, sy,
6123 xstaff = [],
6124 stl = [], // all staves in the line
6125 bar_bot = [],
6126 bar_height = [],
6127 bar_ng = [], // number of gaps
6128 ba = [], // bars [symbol, bottom, height]
6129 sb = "",
6130 thb = ""
6131
6132 /* -- set the bottom and height of the measure bars -- */
6133 function bar_set() {
6134 var st, sc, i, j, l, stlines, b, hlmap,
6135 dy = 0
6136
6137 for (st = 0; st <= cur_sy.nstaff; st++) {
6138 if (xstaff[st] < 0) {
6139 bar_bot[st] = bar_height[st] = 0
6140 continue
6141 }
6142 sc = staff_tb[st].staffscale;
6143 stlines = cur_sy.staves[st].stafflines
6144 l = stlines.length
6145 for (i = 0; i < l; i++) {
6146 if (stlines[i] != '.' && stlines[i] != '-')
6147 break
6148 }
6149 bar_bot[st] = staff_tb[st].y + 6 * (
6150 i == l - 1 ? (l - 2) :
6151 i >= l - 2 ? (l - 3) :
6152 i) * sc
6153 if (!dy) {
6154 dy = staff_tb[st].y + 6 * (
6155 i == l ? (l + 1) :
6156 i >= l - 2 ? l :
6157 (l - 1)) * sc
6158 }
6159 bar_height[st] = dy - bar_bot[st];
6160 bar_ng[st] = l - i // number of gaps
6161 && (l - 1 - i || 2)
6162
6163 // define the helper lines
6164 if (stlines[l-1]!= '.') { // if any staff line
6165 staff_tb[st].hll = 17 + i * 2 // pitch of lowest note
6166 // without helper line
6167 // ('D' when standard staff)
6168 if (i == l)
6169 staff_tb[st].hll -= 2
6170 staff_tb[st].hlmap =
6171 hlmap = new Int8Array((l - i + 1) * 2 + 2)
6172 for (j = 1; i < l; i++, j += 2) {
6173 switch (stlines[i]) {
6174 case '|':
6175 case '[':
6176 case "'":
6177 // case ':':
6178 hlmap[j - 1] = 1 // no helper line
6179 hlmap[j] = 1
6180 hlmap[j + 1] = 1
6181 break
6182 }
6183 }
6184 }
6185
6186 dy = (cur_sy.staves[st].flags & STOP_BAR) ?
6187 0 : bar_bot[st]
6188 }
6189
6190 // if in the middle of the tune, check the previous bar(s)
6191 i = ba.length
6192 if (!i)
6193 return
6194 while (--i >= 0) {
6195 b = ba[i]
6196 st = b[0].st
6197 if (b[1] > bar_bot[st])
6198 b[1] = bar_bot[st]
6199 if (b[2] < bar_height[st])
6200 b[2] = bar_height[st]
6201 if (b[3] < bar_ng[st])
6202 b[3] = bar_ng[st]
6203 if (b[0].seqst) // end of time sequence
6204 break
6205 }
6206 } // bar_set()
6207
6208 /* -- draw a staff -- */
6209 function draw_staff(st, x1, x2) {
6210 var w, i, dy, ty,
6211 y = 0,
6212 ln = "",
6213 tycl = {
6214 "|": "slW", // normal
6215 "[": "slthW", // thick
6216 "'": "sltnW", // thin
6217 ":": "sldW" // dash
6218 },
6219 stafflines = cur_sy.staves[st].stafflines,
6220 l = stafflines.length,
6221 il = 6 * staff_tb[st].staffscale // interline
6222
6223 if (!/[\[|':]/.test(stafflines)) // '
6224 return // no line
6225 w = x2 - x1;
6226 set_sscale(-1)
6227
6228 // check if default staff
6229 if (cache && cache.st_l == stafflines
6230 && staff_tb[st].staffscale == 1
6231 && cache.st_w == (w | 0)) {
6232 xygl(x1, staff_tb[st].y, 'stdef' + cfmt.fullsvg)
6233 return
6234 }
6235 for (i = 0; i < l; i++, y -= il) {
6236 if (stafflines[i] == '.')
6237 continue
6238 dy = 0
6239 for (; i < l; i++, y -= il, dy -= il) {
6240 switch (stafflines[i]) {
6241 case '.':
6242 case '-':
6243 continue
6244 case ty:
6245 ln += 'm-' + w.toFixed(1) +
6246 ' ' + dy.toFixed(2) +
6247 'h' + w.toFixed(1);
6248 dy = 0
6249 continue
6250 }
6251 if (ty != undefined)
6252 ln += '"/>\n';
6253 ty = stafflines[i]
6254 ln += '<path class="' + tycl[ty] +
6255 '" d="m0 ' + y + 'h' + w.toFixed(1);
6256 dy = 0
6257 }
6258 ln += '"/>'
6259 }
6260 y = staff_tb[st].y
6261 if (!cache
6262 && w > get_lwidth() - 10
6263 && staff_tb[st].staffscale == 1) {
6264 cache = {
6265 st_l: stafflines,
6266 st_w: w | 0
6267 }
6268 i = 'stdef' + cfmt.fullsvg;
6269 if (ln.indexOf('<path', 1) < 0)
6270 glyphs[i] = ln.replace('path', 'path id="' + i + '"')
6271 else
6272 glyphs[i] = '<g id="' + i + '">\n' + ln + '\n</g>';
6273 xygl(x1, y, i)
6274 return
6275 }
6276 out_XYAB('<g transform="translate(X, Y)">\n' + ln + '\n</g>\n', x1, y)
6277 } // draw_staff()
6278
6279 // draw a measure bar
6280 function draw_bar(s, bot, h, ng) {
6281 var i, s2, yb, w,
6282 bar_type = s.bar_type,
6283 st = s.st,
6284 p_staff = staff_tb[st],
6285 top = ng >= 3 ? 6 * ng : (4 - ng) * 6,
6286 x = s.x
6287
6288 // don't put a line between the staves if there is no bar above
6289 if (st != 0
6290 && s.ts_prev
6291 //fixme: 's.ts_prev.st != st - 1' when floating voice in lower staff
6292 // && (s.ts_prev.type != C.BAR || s.ts_prev.st != st - 1))
6293 && s.ts_prev.type != C.BAR)
6294 h = top * p_staff.staffscale
6295
6296 s.ymx = s.ymn + h;
6297
6298 set_sscale(-1)
6299 anno_start(s)
6300 if (s.color)
6301 set_color(s.color);
6302
6303 // compute the middle vertical offset of the staff
6304 yb = bot + top / 2
6305
6306 // if measure repeat, draw the '%' like glyphs
6307 if (s.bar_mrep) {
6308 set_sscale(st)
6309 if (s.bar_mrep == 1) {
6310 for (s2 = s.prev; s2.type != C.REST; s2 = s2.prev)
6311 ;
6312 xygl(s2.x, yb, "mrep")
6313 } else {
6314 xygl(x, yb, "mrep2")
6315 if (s.v == cur_sy.top_voice)
6316 nrep_out(x, yb + p_staff.topbar, s.bar_mrep)
6317 }
6318 set_sscale(-1)
6319 }
6320
6321 if (bar_type == '||:')
6322 bar_type = '[|:'
6323
6324 for (i = bar_type.length; --i >= 0; ) {
6325 switch (bar_type[i]) {
6326 case "|":
6327 if (s.bar_dotted) {
6328 w = top / 6 <= 9
6329 ? [0, 0, 4, 3.6, 4.8, 4.3, 4, 4.7, 4.4, 4.9]
6330 [top / 6]
6331 : 5
6332 out_XYAB(
6333 '<path class="bW" stroke-dasharray="A,A" d="MX YvG"/>\n',
6334 x, bot, w * p_staff.staffscale, -h)
6335 } else if (s.color) {
6336 out_XYAB('<path class="bW" d="MX YvF"/>\n',
6337 x, bot, -h)
6338 } else {
6339 sb += 'M' + sx(x).toFixed(1)
6340 + ' ' + self.sy(bot).toFixed(1)
6341 + 'v' + (-h).toFixed(1)
6342 }
6343 break
6344 default:
6345 // case "[":
6346 // case "]":
6347 x -= 3;
6348 if (s.color)
6349 out_XYAB('<path class="bthW" d="MX YvF"/>\n',
6350 x + 1.5, bot, -h)
6351 else
6352 thb += 'M' + sx(x + 1.5).toFixed(1)
6353 + ' ' + self.sy(bot).toFixed(1)
6354 + 'v' + (-h).toFixed(1)
6355 break
6356 case ":":
6357 x -= 2;
6358 set_sscale(st);
6359 if (ng & 1) {
6360 xygl(x, yb + 6, "rdot")
6361 xygl(x, yb - 6, "rdot")
6362 } else {
6363 xygl(x, yb - 12, "rdots")
6364 }
6365 set_sscale(-1)
6366 break
6367 }
6368 x -= 3
6369 }
6370 set_color();
6371 anno_stop(s)
6372 } // draw_bar()
6373
6374 // output all the bars
6375 function out_bars() {
6376 var i, b,
6377 l = ba.length
6378
6379 for (i = 0; i < l; i++) {
6380 b = ba[i] // symbol, bottom, height, top
6381 draw_bar(b[0], b[1], b[2], b[3])
6382 }
6383
6384 set_sscale(-1)
6385 if (sb) // single bars
6386 output += '<path class="bW" d="'
6387 + sb
6388 + '"/>\n'
6389
6390 if (thb) // thick bars [x, y, h]
6391 output += '<path class="bthW" d="'
6392 + thb
6393 + '"/>\n'
6394 } // out_bars()
6395
6396 // set the helper lines of rests
6397 function hl_rest(s) {
6398 var j,
6399 stlines = cur_sy.staves[s.st].stafflines,
6400 p_st = staff_tb[s.st],
6401 i = 5 - s.nflags, // rest_tb index (5 = C_XFLAGS)
6402 x = s.x,
6403 y = s.y
6404
6405 if (i < 6) // no ledger line if rest smaller than minim
6406 return
6407
6408 if (i == 7 && y == 12
6409 && stlines.length <= 2)
6410 y -= 6 // semibreve a bit lower
6411
6412 j = y / 6
6413 switch (i) {
6414 default:
6415 switch (stlines[j + 1]) {
6416 case '|':
6417 case '[':
6418 case "'":
6419 case ':':
6420 break
6421 default:
6422 set_hl(p_st, j + 1, x, -7, 7)
6423 break
6424 }
6425 if (i == 9) { // longa
6426 y -= 6
6427 j--
6428 }
6429 break
6430 case 7: // semibreve
6431 y += 6
6432 j++
6433 case 6: // minim
6434 break
6435 }
6436 switch (stlines[j]) {
6437 case '|':
6438 case '[':
6439 case "'":
6440 case ':':
6441 break
6442 default:
6443 set_hl(p_st, j, x, -7, 7)
6444 break
6445 }
6446 } // hl_rest()
6447
6448 // return the left x offset of a new staff
6449 // s is the %%staves
6450 function st1(st, s) {
6451 var tim = s.time
6452
6453 do { // search a voice of this staff
6454 s = s.ts_next
6455 } while (s.st != st)
6456 while (s.prev // search the first symbol of this voice
6457 && s.prev.time >= tim)
6458 s = s.prev
6459 if (s.bar_type)
6460 return s.x
6461 return s.x - s.wl
6462 } // st1()
6463
6464 // ---- draw_systems() ----
6465
6466 /* draw the staff, skipping the staff breaks */
6467 for (st = 0; st <= nstaff; st++) {
6468 stl[st] = cur_sy.st_print[st] // staff at start of line
6469 xstaff[st] = !stl[st] ? -1 : 0;
6470 }
6471 bar_set();
6472 draw_lstaff(0)
6473 for (s = tsfirst; s; s = s.ts_next) {
6474 switch (s.type) {
6475 case C.STAVES:
6476 sy = s.sy
6477 for (st = 0; st <= nstaff; st++) {
6478 x = xstaff[st]
6479 if (x < 0) { // no staff yet
6480 if (sy.st_print[st]) {
6481 xstaff[st] = st1(st, s)
6482 stl[st] = true
6483 }
6484 continue
6485 }
6486 if (sy.st_print[st] // if not staff stop
6487 && cur_sy.staves[st]
6488 && sy.staves[st].stafflines ==
6489 cur_sy.staves[st].stafflines)
6490 continue
6491 if (s.ts_prev.bar_type) {
6492 x2 = s.ts_prev.x
6493 if (sy.staves[st].stafflines.length >
6494 cur_sy.staves[st].stafflines.length)
6495 x2 -= s.ts_prev.wl - 4
6496 } else {
6497 x2 = (s.ts_prev.x + s.x) / 2
6498 xstaff[st] = -1
6499 }
6500 draw_staff(st, x, x2)
6501 xstaff[st] = sy.st_print[st] ? x2 : -1
6502 }
6503 cur_sy = sy;
6504 bar_set()
6505 continue
6506 case C.BAR: // display the bars after the staves
6507 if (s.invis || !s.bar_type
6508 || !cur_sy.st_print[s.st])
6509 break
6510 if (s.second
6511 && (!s.ts_prev
6512 || (s.ts_prev.type == C.BAR
6513 && s.ts_prev.st == s.st)))
6514 break
6515 ba.push([s, bar_bot[s.st], bar_height[s.st], bar_ng[s.st]])
6516 break
6517 case C.STBRK:
6518 if (cur_sy.voices[s.v]
6519 && cur_sy.voices[s.v].range == 0) {
6520 if (s.xmx > 14
6521 && s.next // if not at end of line
6522 && s.next.type == C.CLEF) { // and before a clef
6523
6524 /* draw the left system if stbrk in all voices */
6525 var nv = 0
6526 for (var i = 0; i < voice_tb.length; i++) {
6527 if (cur_sy.voices[i]
6528 && cur_sy.voices[i].range > 0)
6529 nv++
6530 }
6531 for (s2 = s.ts_next; s2; s2 = s2.ts_next) {
6532 if (s2.type != C.STBRK)
6533 break
6534 nv--
6535 }
6536 if (nv == 0)
6537 draw_lstaff(s.x)
6538 }
6539 }
6540 st = s.st;
6541 x = xstaff[st]
6542 if (x >= 0) {
6543 s2 = s.prev
6544 if (!s2)
6545 break
6546 x2 = s2.type == C.BAR ?
6547 s2.x :
6548 s.x - s.xmx
6549 if (x >= x2)
6550 break
6551 draw_staff(st, x, x2)
6552 xstaff[st] = s.x
6553 }
6554 break
6555 case C.GRACE:
6556 for (s2 = s.extra; s2; s2 = s2.next)
6557 self.draw_hl(s2)
6558 break
6559 case C.NOTE:
6560 if (!s.invis)
6561 self.draw_hl(s)
6562 break
6563 case C.REST:
6564 if (s.fmr // if full measure rest
6565 || (s.rep_nb && s.rep_nb >= 0))
6566 center_rest(s)
6567 if (!s.invis)
6568 hl_rest(s)
6569 break
6570 // default:
6571 //fixme:does not work for "%%staves K: M: $" */
6572 //removed for K:/M: in empty staves
6573 // if (!cur_sy.st_print[st])
6574 // s.invis = true
6575 // break
6576 }
6577 }
6578
6579 // draw the end of the staves
6580 for (st = 0; st <= nstaff; st++) {
6581 x = xstaff[st]
6582 if (x < 0 || x >= realwidth)
6583 continue
6584 draw_staff(st, x, realwidth)
6585 }
6586
6587 // the ledger lines
6588 draw_all_hl()
6589
6590 // and the bars
6591 out_bars()
6592
6593 draw_vname(indent, stl)
6594
6595 // set_sscale(-1)
6596 }
6597
6598 /* -- draw remaining symbols when the staves are defined -- */
6599 // (possible hook)
6600 Abc.prototype.draw_symbols = function(p_voice) {
6601 var bm = {},
6602 s, x, y, st;
6603
6604 // bm.s2 = undefined
6605 for (s = p_voice.sym; s; s = s.next) {
6606 if (s.invis) {
6607 switch (s.type) {
6608 case C.CLEF:
6609 if (s.time >= staff_tb[s.st].clef.time)
6610 staff_tb[s.st].clef = s
6611 continue
6612 case C.KEY:
6613 p_voice.ckey = s
6614 default:
6615 continue
6616 case C.NOTE: // (beams may start on invisible notes)
6617 break
6618 }
6619 }
6620 st = s.st
6621 x = s.x;
6622 set_color(s.color)
6623 switch (s.type) {
6624 case C.NOTE:
6625 //--fixme: recall set_scale if different staff
6626 set_scale(s)
6627 if (s.beam_st && !s.beam_end) {
6628 if (self.calculate_beam(bm, s))
6629 draw_beams(bm)
6630 }
6631 if (!s.invis) {
6632 anno_start(s);
6633 draw_note(s, !bm.s2);
6634 anno_a.push(s)
6635 }
6636 if (s == bm.s2)
6637 bm.s2 = null
6638 break
6639 case C.REST:
6640 if (!gene.st_print[st])
6641 break
6642 draw_rest(s);
6643 break
6644 case C.BAR:
6645 break /* drawn in draw_systems */
6646 case C.CLEF:
6647 if (s.time >= staff_tb[st].clef.time) {
6648 if (s.x == staff_tb[st].clef.x
6649 && s.v != staff_tb[st].clef.v)
6650 break
6651 staff_tb[st].clef = s
6652 }
6653 if (!gene.st_print[st])
6654 break
6655 set_color();
6656 set_sscale(st);
6657 anno_start(s);
6658 y = staff_tb[st].y
6659 if (s.clef_name)
6660 xygl(x, y + s.y, s.clef_name)
6661 else if (!s.clef_small)
6662 xygl(x, y + s.y, s.clef_type + "clef")
6663 else
6664 xygl(x, y + s.y, "s" + s.clef_type + "clef")
6665 if (s.clef_octave) {
6666 /*fixme:break the compatibility and avoid strange numbers*/
6667 if (s.clef_octave > 0) {
6668 y += s.ymx + 1
6669 if (s.clef_small)
6670 y -= 2
6671 } else {
6672 y += s.ymn - 2 //+2
6673 // if (s.clef_small)
6674 // y += 1
6675 }
6676 xygl(x - 2, y, (s.clef_octave == 7
6677 || s.clef_octave == -7)
6678 ? "oct" : "oct2")
6679 }
6680 anno_a.push(s)
6681 break
6682 case C.METER:
6683 p_voice.meter = s
6684 if (s.second
6685 || !staff_tb[s.st].topbar)
6686 break
6687 set_color();
6688 set_sscale(s.st);
6689 anno_start(s);
6690 draw_meter(s);
6691 anno_a.push(s)
6692 break
6693 case C.KEY:
6694 p_voice.ckey = s
6695 if (s.second
6696 || !staff_tb[s.st].topbar)
6697 break
6698 set_color();
6699 set_sscale(s.st);
6700 anno_start(s);
6701 self.draw_keysig(x, s);
6702 anno_a.push(s)
6703 break
6704 case C.MREST:
6705 draw_mrest(s)
6706 break
6707 case C.GRACE:
6708 set_scale(s);
6709 draw_gracenotes(s)
6710 break
6711 case C.SPACE:
6712 case C.STBRK:
6713 break /* nothing */
6714 case C.CUSTOS:
6715 set_scale(s);
6716 draw_note(s, 0)
6717 break
6718 case C.BLOCK: // no width
6719 case C.REMARK:
6720 case C.STAVES:
6721 case C.TEMPO:
6722 break
6723 default:
6724 error(2, s, "draw_symbols - Cannot draw symbol " + s.type)
6725 break
6726 }
6727 }
6728 set_scale(p_voice.sym);
6729 }
6730
6731 /* -- draw all symbols -- */
6732 function draw_all_sym() {
6733 var p_voice, v,
6734 n = voice_tb.length
6735
6736 // draw the slurs on 2 staves
6737 // sl = [symbol of 1st staff, symbol of 2nd staff, slur type, <path .../>
6738 function draw_sl2() {
6739 var i, a, d, dy, dy2, dy2o, dz, n, sl
6740
6741 while (1) {
6742 sl = gene.a_sl.shift()
6743 if (!sl)
6744 break
6745
6746 // extract the path header and the values
6747 i = sl[3].indexOf('d="M') + 4
6748 output += sl[3].slice(0, i) // <path .. >d="M
6749
6750 a = new Float32Array(sl[3].slice(i).match(/[\d.-]+/g))
6751
6752 // update the starting point of the slur
6753 a[1] -= staff_tb[sl[0].st].y // absolute vertical offset
6754
6755 // [0][1] = M
6756 // [2][3] [4][5] [6][7] = c
6757 // [8] = v second curve if not dotted
6758 // [9][10] [11][12] [13][14] = c
6759 //
6760 // y: 3-------5
6761 // / 12---10 \
6762 // / / \ \
6763 // / / \ \ 7
6764 // 1 14 \| 8
6765 //
6766 // x: 0 2 4 6
6767 // 13 11 9
6768
6769 // deltas between staves, original and now
6770 dy2o = sl[0].fmt.sysstaffsep + 24
6771 dy2 = staff_tb[sl[1].st].y - staff_tb[sl[0].st].y
6772
6773 switch (sl[2]) { // slur type
6774 case "//": // '~' like
6775 case "\\\\":
6776
6777 // get the middle of the '~' slur (* 2)
6778 d = -(sl[1].prev.prev.y + staff_tb[sl[0].st].y
6779 + sl[1].prev.next.y + staff_tb[sl[1].st].y)
6780 - 2 * (a[1] - posy)
6781 a[5] = d - a[5]
6782 a[7] = d - a[7]
6783 if (a.length > 8) {
6784 d = sl[2][0] == '/' ? 3 : -3
6785 a[8] = -a[8]
6786 a[10] = -a[3] + d
6787 a[12] = -a[5] + d
6788 a[14] = -a[7]
6789 }
6790 break
6791 case "/\\":
6792 case "\\/":
6793 d = sl[2][0] == '/'
6794 ? dy2 - dy2o - 10
6795 : dy2 + dy2o + 10
6796 a[3] += d
6797 a[5] += d
6798 if (a.length > 8) {
6799 a[10] += d
6800 a[12] += d
6801 }
6802 break
6803 default: // /+, /-, \+ or \-
6804 d = sl[2][0] == '/' ? dy2 - dy2o : -dy2 - dy2o
6805 a[5] += d
6806 a[7] += d
6807 if (a.length > 8) {
6808 a[12] -= d
6809 a[14] -= d
6810 }
6811 break
6812 }
6813
6814 // output the slur
6815 output += a[0].toFixed(1) + ' ' + a[1].toFixed(1)
6816 + 'c' + a[2].toFixed(1) + ' ' + a[3].toFixed(1)
6817 + ' ' + a[4].toFixed(1) + ' ' + a[5].toFixed(1)
6818 + ' ' + a[6].toFixed(1) + ' ' + a[7].toFixed(1)
6819 if (a.length > 8)
6820 output += 'v' + a[8].toFixed(1)
6821 + 'c' + a[9].toFixed(1)
6822 + ' ' + a[10].toFixed(1)
6823 + ' ' + a[11].toFixed(1)
6824 + ' ' + a[12].toFixed(1)
6825 + ' ' + a[13].toFixed(1)
6826 + ' ' + a[14].toFixed(1)
6827 output += '"/>\n'
6828 }
6829 } // draw_sl2()
6830
6831 for (v = 0; v < n; v++) {
6832 p_voice = voice_tb[v]
6833 if (p_voice.sym
6834 && p_voice.sym.x != undefined) {
6835 self.draw_symbols(p_voice)
6836 draw_all_ties(p_voice);
6837 // no need to reset the scale as in abcm2ps
6838 set_color()
6839 }
6840 }
6841
6842 self.draw_all_deco()
6843 glout() // output the symbols
6844 anno_put() // before outputting the symbol annotations
6845
6846 set_sscale(-1) /* restore the scale */
6847
6848 if (gene.a_sl) // if slurs on two staves
6849 draw_sl2()
6850 }
6851
6852 /* -- set the tie directions for one voice -- */
6853 function set_tie_dir(s) {
6854 var i, ntie, dir, sec, pit, ty, s2
6855
6856 for ( ; s; s = s.next) {
6857 if (!s.ti1)
6858 continue
6859
6860 sec = ntie = 0;
6861 pit = 128
6862 for (i = 0; i <= s.nhd; i++) {
6863 if (s.notes[i].tie_ty) {
6864 ntie++
6865 if (pit < 128
6866 && s.notes[i].pit <= pit + 1)
6867 sec++;
6868 pit = s.notes[i].pit
6869 s2 = s.notes[i].tie_e
6870 }
6871 }
6872
6873 if (s2 && s.stem * s2.stem < 0)
6874 dir = pit >= 22 // up if above middle staff
6875 ? C.SL_ABOVE : C.SL_BELOW
6876 else if (s.multi)
6877 dir = s.multi > 0 ? C.SL_ABOVE : C.SL_BELOW
6878 else
6879 dir = s.stem < 0 ? C.SL_ABOVE : C.SL_BELOW
6880
6881 // if other voice, set the ties in opposite direction
6882 if (s.multi) {
6883 for (i = 0; i <= s.nhd; i++) {
6884 ty = s.notes[i].tie_ty
6885 if (!((ty & 0x07) == C.SL_AUTO))
6886 continue
6887 s.notes[i].tie_ty = (ty & C.SL_DOTTED) | dir
6888 }
6889 continue
6890 }
6891
6892 /* if one note, set the direction according to the stem */
6893 if (ntie <= 1) {
6894 for (i = 0; i <= s.nhd; i++) {
6895 ty = s.notes[i].tie_ty
6896 if (ty) {
6897 if ((ty & 0x07) == C.SL_AUTO)
6898 s.notes[i].tie_ty =
6899 (ty & C.SL_DOTTED) | dir
6900 break
6901 }
6902 }
6903 continue
6904 }
6905 if (!sec) {
6906 if (ntie & 1) {
6907 /* in chords with an odd number of notes, the outer noteheads are paired off
6908 * center notes are tied according to their position in relation to the
6909 * center line */
6910 ntie = (ntie - 1) / 2;
6911 dir = C.SL_BELOW
6912 for (i = 0; i <= s.nhd; i++) {
6913 ty = s.notes[i].tie_ty
6914 if (!ty)
6915 continue
6916 if (ntie == 0) { /* central tie */
6917 if (s.notes[i].pit >= 22)
6918 dir = C.SL_ABOVE
6919 }
6920 if ((ty & 0x07) == C.SL_AUTO)
6921 s.notes[i].tie_ty =
6922 (ty & C.SL_DOTTED) | dir
6923 if (ntie-- == 0)
6924 dir = C.SL_ABOVE
6925 }
6926 continue
6927 }
6928 /* even number of notes, ties divided in opposite directions */
6929 ntie /= 2;
6930 dir = C.SL_BELOW
6931 for (i = 0; i <= s.nhd; i++) {
6932 ty = s.notes[i].tie_ty
6933 if (!ty)
6934 continue
6935 if ((ty & 0x07) == C.SL_AUTO)
6936 s.notes[i].tie_ty =
6937 (ty & C.SL_DOTTED) | dir
6938 if (--ntie == 0)
6939 dir = C.SL_ABOVE
6940 }
6941 continue
6942 }
6943 /*fixme: treat more than one second */
6944 /* if (nsec == 1) { */
6945 /* When a chord contains the interval of a second, tie those two notes in
6946 * opposition; then fill in the remaining notes of the chord accordingly */
6947 pit = 128
6948 for (i = 0; i <= s.nhd; i++) {
6949 if (s.notes[i].tie_ty) {
6950 if (pit < 128
6951 && s.notes[i].pit <= pit + 1) {
6952 ntie = i
6953 break
6954 }
6955 pit = s.notes[i].pit
6956 }
6957 }
6958 dir = C.SL_BELOW
6959 for (i = 0; i <= s.nhd; i++) {
6960 ty = s.notes[i].tie_ty
6961 if (!ty)
6962 continue
6963 if (ntie == i)
6964 dir = C.SL_ABOVE
6965 if ((ty & 0x07) == C.SL_AUTO)
6966 s.notes[i].tie_ty =
6967 (ty & C.SL_DOTTED) | dir
6968 }
6969 /*fixme..
6970 continue
6971 }
6972 ..*/
6973 /* if a chord contains more than one pair of seconds, the pair farthest
6974 * from the center line receives the ties drawn in opposition */
6975 }
6976 }
6977
6978 /* -- have room for the ties out of the staves -- */
6979 function set_tie_room() {
6980 var p_voice, s, s2, v, dx, y, dy
6981
6982 for (v = 0; v < voice_tb.length; v++) {
6983 p_voice = voice_tb[v];
6984 s = p_voice.sym
6985 if (!s)
6986 continue
6987 s = s.next
6988 if (!s)
6989 continue
6990 set_tie_dir(s)
6991 for ( ; s; s = s.next) {
6992 if (!s.ti1)
6993 continue
6994 if (s.notes[0].pit < 20
6995 && s.notes[0].tie_ty
6996 && (s.notes[0].tie_ty & 0x07) == C.SL_BELOW)
6997 ;
6998 else if (s.notes[s.nhd].pit > 24
6999 && s.notes[s.nhd].tie_ty
7000 && (s.notes[s.nhd].tie_ty & 0x07) == C.SL_ABOVE)
7001 ;
7002 else
7003 continue
7004 s2 = s.next
7005 while (s2 && s2.type != C.NOTE)
7006 s2 = s2.next
7007 if (s2) {
7008 if (s2.st != s.st)
7009 continue
7010 dx = s2.x - s.x - 10
7011 } else {
7012 dx = realwidth - s.x - 10
7013 }
7014 if (dx < 100)
7015 dy = 9
7016 else if (dx < 300)
7017 dy = 12
7018 else
7019 dy = 16
7020 if (s.notes[s.nhd].pit > 24) {
7021 y = 3 * (s.notes[s.nhd].pit - 18) + dy
7022 if (s.ymx < y)
7023 s.ymx = y
7024 if (s2 && s2.ymx < y)
7025 s2.ymx = y;
7026 y_set(s.st, true, s.x + 5, dx, y)
7027 }
7028 if (s.notes[0].pit < 20) {
7029 y = 3 * (s.notes[0].pit - 18) - dy
7030 if (s.ymn > y)
7031 s.ymn = y
7032 if (s2 && s2.ymn > y)
7033 s2.ymn = y;
7034 y_set(s.st, false, s.x + 5, dx, y)
7035 }
7036 }
7037 }
7038 }
7039 // abc2svg music font
7040 var musicfont = 'url("data:application/octet-stream;base64,\
7041 AAEAAAAOAIAAAwBgRkZUTZjuVTkAAFesAAAAHEdERUYAFQAUAABXkAAAABxPUy8yWLldDAAAAWgA\
7042 AABWY21hcI+tzq4AAAQEAAAD1mN2dCAAIgKIAAAH3AAAAARnYXNw//8AAwAAV4gAAAAIZ2x5Zjpw\
7043 qQYAAAkEAABFtGhlYWQZC66+AAAA7AAAADZoaGVhCWn/GgAAASQAAAAkaG10eNmK+y4AAAHAAAAC\
7044 RGxvY2GUUqVMAAAH4AAAASRtYXhwANgBEgAAAUgAAAAgbmFtZSB54KwAAE64AAADIXBvc3Rcker6\
7045 AABR3AAABasAAQAAAAEAALjJexZfDzz1AAsEAAAAAADRlyIXAAAAAOPSSjP/OPzvBUsEiAAAAAgA\
7046 AgAAAAAAAAABAAAEiPzvAFwEJf84/XQFSwABAAAAAAAAAAAAAAAAAAAAkQABAAAAkQDhAAUAAAAA\
7047 AAIAAAABAAEAAABAAC4AAAAAAAEBggGQAAUAAAKZAswAAACPApkCzAAAAesAMwEJAAACAAUDAAAA\
7048 AAAAAAAAARAAAAAAAAAAAAAAAFBmRWQAQAAA7LcDOP84AFwEiAMRAAAAAQAAAAAAAAF2ACIAAAAA\
7049 AVUAAAGQAAACWAAAAFcAAAAjAAAAJQAAACT//wBkAAAAZAAABCMAAAQlAAAB4P/cA7oAAAMLAAAC\
7050 0gAAAr//ugHWAAADCwAAAw4AAAMn/8gAyAAAAWgAAAGuAAABIgAAAZAAAAF8AAABkAAAAZAAAAGB\
7051 AAABkAAAAZAAAAGBAAABnwAAAZ///wH0AAABBAAUAQQACgJrACQCEgAAAcIAAAFCAAABQAAAAUr/\
7052 /gEsAAACMAAAAUoAAAFKAAAAZAAAAUAAAAFAAAABQAAAAUAAAABkAAABNgAAAOYAAAE2AAABOwAA\
7053 ATsAAAE7AAABOwAAATsAAAE7AAABOwAAATsAAAE7AAABOwAAAQ0AAADIAAAA/wAAAQsAFAFuAAAA\
7054 jAAAAIwAAAENADIBbv/1AKkAAAE6AAABQP/9AFAAAAFUAAAAZAAAARgAAAJYAAAAtgAAAZAABQCC\
7055 AAAAggAAASwAAAEsAAAA7gAAAP8AAAFJAAABjwAAAdgAAAHYAAACM//wAyD/4QF7/7QBuP/bARb/\
7056 fgET/9sA3AAAAOj/5AK//7QCM/+0Ar//tAMr/9sBX//bAmn/fgFf/34Caf9+AV8AAAH9AAUBtQAA\
7057 AbUAAAJEAA0CRAANARgAAAE2AAABLP//ASwAAAD6AAAAyAAAARj/OAD6AAAAyAAABA0AAAIcAAwB\
7058 9AAAAfQAAAH0AAAB9AAAAfQAAAH0AAACHAAAAPoAAAD6/+gBwgAAAUgAAAFAAAACCgAAAgoAAABk\
7059 AAAAAAADAAAAAwAAABwAAQAAAAAC0AADAAEAAAAcAAQCtAAAAFwAQAAFABwAAAAg4ADgMOA54Ejg\
7060 UOBc4GLgaeCM4JXgpOCp4LPhAeG74efiAOJJ4mTia+KD5KzkwOTR5OrlAeUx5TnlbeWC5dDl4uYY\
7061 5iTmMOZQ5lXpGOld6gLqpOyp7Lf//wAAAAAAIOAA4DDgOOBD4FDgXOBi4GngeuCU4KDgqeCz4QHh\
7062 ueHn4fLiQOJg4mrigOSg5MDkzuTh5QDlIOU55WblguXQ5eLmEOYk5jDmUOZV6RDpXeoC6qTsouy3\
7063 //8AA//kIAUf1gAAAAAfvx+0H68fqQAAAAAAAB+DH3ofLQAAHkoAAAAAAAAAAAAAAAAblAAAAAAA\
7064 AAAAGzgAABr1GqgalwAAGloaTxowGiwAABcrFocV5gAAE9kAAQAAAAAAAAAAAFQAVgAAAAAAAAAA\
7065 AFgAfAB+AAAAAAAAAIAAAACCAJ4AsAC4ALoAwAAAANYA3ADuAPAAAAEQAAAAAAAAARgAAAAAAAAA\
7066 AAEgAAAAAAAAASoAAAAAAAcACAAJAAoACwAMAA0ADgATABQAFQAWABcAAAAYABkAGgAbABwAHQAe\
7067 AB8AIAAhACIAIwAkACUAJgAnACgAKQAqACsALwAAADAAMgAAADMAAAAAADQAAAA1AAAAAAA2AAAA\
7068 NwA4ADkAOgA7ADwAPQA+AD8AQABBAEIAQwBEAEUARgBHAEgASQBKAEsATABNAE4ATwAAAFAAAABR\
7069 AAAAAAAAAFIAAAAAAAAAUwBVAAAAAABWAFcAWABZAFoAWwBcAF0AXgBfAGAAYQBiAGMAZABlAGYA\
7070 ZwBoAAAAAAAAAGkAagBrAGwAbQAAAG4AbwBwAHIAcwAAAHQAAAAAAHUAdgB6AAAAewAAAHwAAAAA\
7071 AAAAfQCCAIMAhAAAAIUAhgAAAAAAhwCLAIwAAACNAAAAjgAAAI8AAAEGAAADAAAAAAAAAAECAAAA\
7072 AgAAAAAAAAAAAAAAAAAAAAEAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
7073 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
7074 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
7075 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\
7076 AAAAAAAAAAAAAAAAAAAAAAAAIgKIAAAAKgAqACoANgA+AG4AegCGAJIAsADCATYBkgICAkYC0gNk\
7077 A8QD2AR2BQgFYAWgBiIGRAZYBqoG+gcaB2AHoAfaCCYIZgieCO4JAgkoCU4JgAmcCb4J5gn8ChYK\
7078 TApaCmYKcgqECqAKwgrQCuQK9gsGC1ILYgt8C5YLzgwGDFQMoA0GDWgN5g5eDoQOng7ODvgPUA9q\
7079 D4QPqhAEECQQYhCKEJ4QqhC4EMgQ8BEYEVARXBFoEXQRgBGiEcgR/hJMEqwTIBNGE3QTyBQ8FJAU\
7080 0BUUFWoWhBdcF/IYpBlmGfgayhveHModDB1QHZodtB3YHeweAh4yHkIeVB5wHoYeqh7aH7YgciCc\
7081 ILog6iEoIVohnCG4IdIh+CIaIkgiYiKMIsgi2gACACIAAAEyAqoAAwAHAC6xAQAvPLIHBADtMrEG\
7082 Bdw8sgMCAO0yALEDAC88sgUEAO0ysgcGAfw8sgECAO0yMxEhESczESMiARDuzMwCqv1WIgJmAAAB\
7083 AAAAAAGRAZAAAwAAMREhEQGRAZD+cAABAAAAAAAAAAAAAAAAMQAAAQAAAAAAVwQDACAAABE1NjU0\
7084 JyY1NDcGFRQXFhUUBxYVFAcGFRQXJjU0NzY1NDUjElc/FSVNTSUVP1cSIwIDAhhDNmA5NGY6Mksi\
7085 OWFNYRgYZkxgOSVKMjpmNDlgNkgAAQAAAAAAIwPoAAMAABEzESMjIwPo/BgAAQAAAfQAJQPoAAMA\
7086 ABEzESMlJQPo/gwAAf//A2QAJARgAAMAABEzByMkASQEYPwAAgAAAUAAZAKeAAcADwAAEiImNDYy\
7087 FhQCIiY0NjIWFEcqHR0qHR0qHR0qHQI6HSodHSr+6R0qHR0qAAAAAQAA/84AZAAyAAcAABYiJjQ2\
7088 MhYURyodHSodMh0qHR0qAAAABQAAAAAEJAGuAC8ANwA/AEcAUwAAITUzHgEzMjY1NCcuBDU0NjMy\
7089 Fhc3MxcjLgEjIgYVFB4DFx4BFRQGIyInByAiJjQ2MhYUBCImNDYyFhQBETMyNjQmIwM1MxEjNTMy\
7090 FhUUIwJOHhVPMik7lBkaKhYRWT0kJxkeHgceD0owHzkQIhkyCE5NW09FLiMBmyodHSod/eoqHR0q\
7091 Hf6JKDxGRjzcRkbccYn6oDxLICEtKAcIFBQjFUNNCw4ZmzpIKBsPFw8JCwIVNzM6TiAgHSodHSod\
7092 HSodHSoBaf6YYaZh/noeAWgeZ2vSAAUAAAAABCQBrgAaACIAKgAyAD4AACEiJjU0NjMyFhc3MxcH\
7093 JiMiBhQWMzI2NxcOATIiJjQ2MhYUBCImNDYyFhQBETMyNjQmIwM1MxEjNTMyFhUUIwMCZ3WCWiUp\
7094 Gx4eCCEkXjg2Njg2TREjFFmxKh0dKh396iodHSod/okoPEZGPNxGRtxxifprZ2V3DBIepgSMbZht\
7095 ST4KSlEdKh0dKh0dKh0dKgFp/phhpmH+eh4BaB5na9IAAAAD/9wAAgHeArMABwAPAE0AAAAiJjQ2\
7096 MhYUBCImNDYyFhQXNDYzMhYVFAcWMzI2NTQvAQMnEy4BNTQ+ATc2MzIWFRQGIyImNTQ3JiMiBhUU\
7097 HwETFwMeARUUDgEHBiMiJgGeIBgYIBj+fiAYGCAYWxsUEx4sFykmNiZ7zyvRWkgcExQmMzA4GxQT\
7098 HiwXKSY2JnrUK9VaSBwTFCYzMDgBUhggGBggVBggGBgg0xIcGhEdDhctJi0mZf7eIAElR3E4Fi0R\
7099 ERM4IRIcGhEdDhctJi0mZQEmH/7XR3I4Fi0RERM4AAUAAP9WA7wDYgAXABsAHwAjACcAAAEzFR4B\
7100 FzMVIw4BBxUjNS4BJyM1Mz4BNxEjFhcTETY3JzMmJwMRBgcBxDRwnwmsrAmfcDRwnwmsrAmecZQH\
7101 jTSOBpSUBo40jgYDYqsLtoA0f7cLq6sLt380grQL/ov0FAEI/vgU9DT0FP74AQoU9gAAAAAEAAD9\
7102 bwKnBIgACwBEAE4AYwAAAQYVFBc+ATU0Jw4BExcVFAYjIiY1NDYzMhYUBgcWMzI2PQEnBiMiJjU0\
7103 Nz4GPwEmNTQ2NxYVFAYHFzYzMhYVFCc0JiMiBiMTPgEnDgEVFBYXLgE1NDcnDgEHHgEzMjcBbAcF\
7104 SHU2OUJLF09NUl9AMi9BPy8tGC9AFxwNmNs6DSQgLh0xEhcWDWxJYlx5FBQLcYhOYlACBwIhW0Hi\
7105 NkYnHT0/phGPbAECoXkKFgNTNy0jWjGVSHINB177bOwDTFxTQS1IO1g3ARk8RgPpAsedhWYXMCUt\
7106 GicOERCWZoyhBzrliKlfzwKcc866PmYB/p0SXfILRjEgQhIORUepLr1yl2eNowIAAgAA/fwC0gIA\
7107 AGQAaAAAATI2NTQnJiMiBw4CByYnJicRIxEzETY3NjceAxcWMzI2NTQnJiMiBxYXFBYVFAYrASY1\
7108 NDc2NzYzMhcWFxUUBgcGIyInBxc2MzIWFxYdAQYHBiMiJjU0NzMyFhUUBhUGBxYBMxEjAd4+Sg0a\
7109 SUY8AgYKBCIaHi4cHC4eGiIGFAwZDyclMT0SJFIvMTINAjMhBUQFGlUnI15VNwhaSB8tND8iIj80\
7110 QmYeKAg3Vl1MckQFITMCDzA1/k17e/4geEotLWxJBQ4bCmInKx/+BAQA/hEfKydiCyoXHQkbe0Ix\
7111 NmIaECgDDQQeKxkyFAtEGQ1PNFMSTm8cDRdLShc+LDlDElM0UE48MhkrHgQOBCYQHAPe/AAAAAP/\
7112 uv2lAsMA/wAqADYAQgAANzQ2MzIWFxYVFAYHDgEHNjc+ATc2NTQmJy4BIyIGBz4BMzIWFRQHBiMi\
7113 JgUiJjU0NjMyFhUUBiciJjU0NjMyFhUUBhOLZ1VrKy9GVWbPleWFMTQTChEdHDQzO2EWGCMbLj0n\
7114 ITEzRQKCFh0aFBUeGhoXGxwVFBwbHWKANDk+cn+6T2BNCD2FMWNQK2ZHUCMiF09GHhdBLzIgHlGK\
7115 HBcWHB0VFh3yHhkVGhsUGR4AAAIAAP8GAXIA+gADAAcAADczEyMDMxMj3JQCltyUApb6/gwB9P4M\
7116 AAAEAAD+CgIfA6oACQAgAGMAbgAAJRYXPgE1NCYjIgMCJw4BFRQXLgE1NDY3JicOAQceATMyFx4B\
7117 HwEwHQEUIyImNTQ2MzIWFRQGBxYzMjY1NC8BBiMiJjU0Nz4BNz4CNyY1NDY3HgEVFAYHHgEXNjMy\
7118 FxYVFAcGAwYVFBc+ATU0JwYBSRMGTUdWQg4NGAEsOR8gKkw7BQlxVQEFbYIDIgMGAgJ6NlMzKCU1\
7119 MScVIiMsAQ0JFY2ZLg5MHgUjJxIOWUAvGUlhAggEEghcOTJjNmQDBjdeKV91xFsSTzM2Vv7gAQsQ\
7120 CTQnKSYSQSk4ThA+Wlp5U3F+GiBAFxcbB39IMSQzNyQiKAEMMzUPCY0BkopqURxQGAQgIQ3CB25/\
7121 EzNiW22HTBFuJAJDNmJ3MBsDVh4iOSQlezNGJiYAAAAAAgAA/mMCQgGaAGMAZwAAATI2NTQnJiMi\
7122 Bw4BByYnJicRIxEzETY3NjceAxcWMzI2NTQnJiMiBxYXFBYVFAYrASY1NDc2NzYzMhcWFxUUBgcG\
7123 IyInBxc2MzIWFxYdAQYHBiMiJjU0NzMyFhUUBhUGBxYBMxEjAX4yOwoVOjgwAgwEHRMYJRYWJRgT\
7124 HQQRChMMIB0nMQ4cQyUnKAoCKhoENgQVRCMYSUYtBkg6GCUrMRsbNCg1UhggBi1GST1bNgQaKgIM\
7125 Jiv+o2Ji/oBgOyQkVzsFHQtSHCIZ/moDM/50GSIcUggiEhcIFmI1KyhOFQ0gAgsDGCIUKBAJNRUL\
7126 QCtBDj5aFgoSPDsTMiMuNQ9BK0A/MCgUIxgDCwMfDRYDGPzNAAAAAAP/yP4eAjYAzAAmAC8AOwAA\
7127 NzQ2MzIXFhUUBw4BBzY3Njc2NTQnLgEjIgYHPgEzMhYVFAcGIyImBSImNDYyFhQGJyImNTQ2MzIW\
7128 FRQGD3BSgjwjeEPGacBeRx4JLRgtIDBSChIWFiU5IBklLDkCAhIXFCIYFRUSFhUSERYVIU1eVzNa\
7129 xmw7VwYzZEqMLylmMxoUQjYXDTwmKBoYU3UXJBYXIhjCGBQRFBUQExkAAAAAAwAAAAAAyADwAAkA\
7130 FQArAAA3BhUUFjMyNjU0JyIGFRQeARc2NTQmByImNTQ2Ny4BNTQ2MzIWFRQHFhUUBlYsHhARFAIO\
7131 EwoJECUUNCU1IyIRDCgeKTE3IzR0EB4UHhsRF4URDgoQCAwLHQ4X3CgeFxoLDxMQGiIfHSUOHB8g\
7132 JgACAAD//wFnAQMAMABiAAAXIjU0MzIVFAYVFDMyNjU0IyIHBiMiNTQ3NjM2MzIWFRQHBgcOAQcG\
7133 Fjc2MzIWFRQGJyI0OwEyNzI0MzY3PgE1NiMiBwYnJjc+ATc2OwIyFxYzMjc2BwYHBhUUMzIVFCsC\
7134 1D4YGxIdFyweChQWCAsmBgkjSQ8FFhpICgYBAgUIDA4gI0P6CQkJFAUBAQ0XAQIIDgkIDA0KDwYR\
7135 AjIEAgMGDgwIAggXBkcKAhUMDCcnATchGwcWBg5CIxwMDQ8WVAsBAgUQBwkQAgYLCgUCAyQgKDkB\
7136 EgwEHz0BBQIVCQ4GBhIHGAJABwYCCBKtHQUFCAoIAAAAAAIAAP8GAa4A+gALABQAADMUFjMyNjU0\
7137 JiMiBgc0NjIWFAYiJooqIyIrJyYlKIp9tH19tH1ieHlhZXV2YWeQkdKRkgAAAQAA/wYBIgD6AAkA\
7138 ADE3MxEXFSM1NxFkfUHwQfr+Ph4UFB4BLAAAAAEAAP8GAY8A+gA8AAA3MhUUBw4DBzYzMhYzMjc+\
7139 AjMOAgcGBwYjIiYjIgYjIjU0Jz4FNTQnIgcyFhUUBiMiNTQ+AcfIBQ02QG82EyAbZBwYHgUQDAEB\
7140 BQUBBxAaKRp0FR9WAgcBAiw+RzwoU04aHCk3Hkw8WPp+Gg4hLh1ELQwjDgMNCwUWFgMpDhgnJhAB\
7141 AiFFODwyOBhiATUlHh8pZyg5GQAAAQAA/wYBdQD6ADkAADcyFhUUBiMiJjU0NzYzMhcWFRQGBx4B\
7142 FRQHBiMiJicmNDYzMhYVFAYjFjMyNjU0JicmNDc+ATQmIyJmGyInIRsyHzNZRiZERj0+UUskTSdX\
7143 GCMyICIqJRsMPyQrSC4WFi9LKSQ8qhwXGyMrIy8aKhMiSC5ECwtFLUMnExYUHUwuIRsZHikxJyY6\
7144 CAQiBAk3UDAAAAEAAP8GAZAA+gARAAAFFyM3NSM1NjUzATM/AREzFSMBRTLIMuGTo/77sAFjS0vR\
7145 KSkxKPKA/o6Wkf7ZKAAAAAABAAD/BwF+APoALwAAFzYzMhYVFAYjFjMyNz4BNTQnJiMiBxMhDgEr\
7146 AQc2MzIXHgEVFAcOAiMiJy4BNTQSHCEbKiAcGiQxHBMJHhwoTkgKAWILNSXVBjlCUzEhK0MWQCsl\
7147 PCsQHl8gIBccICEeFB8gORwaNQEiJDp5Hh8VQSVPLxAQAhQJMhIjAAAAAAIAAP8GAYEA+gAJACwA\
7148 ABcyNjU0JiMiBxYTFhUUBiMiJjU0NjMmIyIGFT4CMzIWFRQGIyImJz4BMzIWyCktKigsMAfdGyMY\
7149 HiIbEBY3NS8VGC0eTE9xSGFmAQFsWzA/0kUsIjAlngGfGiYZKB4bDB4jeF8LCglANkRZgnhpkRIA\
7150 AQAA/wYBkAD7ACgAADciDgMHNz4IMzIWMzI2Nw4EFSM2NzY3NjcGIyImYRQaFAkRBQoBDAILBQsJ\
7151 DRAJL3UjGjsRG0UaHgiCAQgRaB4tER4lYKEFDgkbBnQBCwIJAQYBAwEmFw5Dp0FcQitFGzaHJzkK\
7152 KAADAAD/BgGEAPoADgAcADQAABcOARUUFjMyNjU0LgM3PgE1NCYiBhUUHgMHLgE1NDY3MhYVFAYH\
7153 HgEVFAYjIiY1NDaaNixYLCo/DyAdMT4zI0RSMwocEjFwMSlmSktlKjA6MnVNTHY5KhkkGx0wKR8O\
7154 FxINE1oaIhwdMCggDxcTChU/GDw1M00BRjInNBcaOjU3SkgwJDUAAAIAAP8GAYEA+gAJACwAADci\
7155 BhUUFjMyNyYDJjU0NjMyFhUUBiMWMzI2NQ4CIyImNTQ2MzIWFw4BIyImuSktKigsMAfdGyMYHiIb\
7156 EBY3NS8VGC0eTE9xSGFmAQFsWzA/0kUsIjAlnv5hGiYZKB4bDB4jeF8LCglANkRZgnhpkRIAAQAA\
7157 /wcBnwD+ACcAABcyNxcOASMiJjU0PgIzMhYVFAYjIiY1NDY3MjU0JiMiBhUUHgPveh4YEGVNWoMo\
7158 QU4pRGInHSAtJSgKOh8zThQbJRvZngVqT49qQGU7Hks+JDMuHBksAQYLIXBqOlYtGwcAAAL///6i\
7159 AZ8BXgAzADkAABcWMzI3NjcUFhUOASsBByM0Iy4BNTQ2NzUzFTMyFhUUBiMiJjU+ATsBMh4BMzY1\
7160 NCYjIg8BBhUUFxDbEgI9JC0KGAprSwMBLQFSXFtULQhOVScdIC0CJBoBBAYGAgVFHgQMKz081gMn\
7161 MEcBAgJfWmVrFI9XVokSZmJOOSQzLhwWJgECBwYRIQIWNI6RNgGJAAABAAD/BgH0APoACwAANTM1\
7162 MxUzFSMVIzUj10bX10bXI9fXRtfXAAAAAQAU/gYA5AIAABMAABMWBwYnJgI1NBI3NhcWBwYCFRQS\
7163 3AcNCQVJa2tJCQsHBzxGR/4WCAUDBlcBIH18ASJWCwcFCUn+54iG/uUAAAEACv4CANwB/QATAAAT\
7164 NhI1NAInJjc2FxYSFRQCBwYnJhQ7R0Y8CQwKB0lra0kICwb+FkkBG4aIARlJCwQECVb+3nx9/uBX\
7165 CQkEAAAEACT/VgJMAKoACwAPABMAHgAABTQmIyIGFRQWMzI2NzMRIwEzESMkFAYjIiY1NDYzMgGo\
7166 VDwgNFc9IS9yMjL+CjIyAfV7ZmV8eWhmIDVOJx81SyPq/qwBVP6s5nhGSTk/QwAAAgAA/yQCEgDc\
7167 AAMADwAANxUhNSUzFSE1MxEjNSEVIx4B1v4MHgHWHh7+Kh5BgoKbNzf+SDc3AAACAAD/fwHCAIEA\
7168 CwATAAAFNCYjIgYVFBYzMj4BFAYiJjQ2MgFRWjYgNF03IS9xfch9fcgeNE8nHzRMI3RsS0tsSwAA\
7169 AAIAAP92AUIAigAMABgAACU0JiMiBhUUHgEzMjYnMhYVFAYjIiY1NDYBIxocO5MEGxc7k2FAQHtH\
7170 QEB7Lg4ZWygEDhVbhEwpP2BMKT9gAAAAAQAA/3kBQACHAAsAACUUBiMiJjU0NjMyFgFAeVkyPHpY\
7171 MjwoRmk4J0VqOAAB//7/bwFMAJEACwAAJzcXNxcHFwcnByc3AhuMjBuGhhyLixyGcSB2dSBwcCF1\
7172 dSFwAAAABQAA/2oBLACWAAUACwARABcAHwAAFwcWMzI3LwEGFRQXPwEmIyIHHwE2NTQnBjQ2MhYU\
7173 BiKWRx0qKR9dRx0dXEgfKSodXEcdHfJYfFhYfBJIHR1aSR8pKh1YSB0dWkgfKSodhXxYWHxYAAAA\
7174 AQAA/wYCMAD6AAMAABUBMwEBuHj+R/oB9P4MAAEAAP90AUoAjAADAAAxNxcHpaWljIyMAAEAAP90\
7175 AUoAjAACAAAVGwGlpYwBGP7oAAEAAP/OAGQAMgAHAAAWIiY0NjIWFEcqHR0qHTIdKh0dKgAAAAEA\
7176 AP95AUACqAAPAAARIREUBiMiJjU0NjMyFxEhAUB5WTI8elgxH/7eAqj9gEZpOCdFahwBxQAAAQAA\
7177 /3kBQAKoABMAABEhERQGIyImNTQ2MzIXESE1ITUhAUB5WTI8elgxH/7eASL+3gKo/YBGaTgnRWoc\
7178 ARF4PAAAAAABAAACMAFAAqgAAwAAESEVIQFA/sACqHgAAAAAAgAAAXwBQAKoAAMABwAAESEVIREh\
7179 FSEBQP7AAUD+wAH0eAEseAAAAAEAAP/aAGQAPgAHAAAWIiY0NjIWFEcqHR0qHSYdKh0dKgAAAAEA\
7180 AAMCATYDwAAFAAARIRUhFSMBNv7oHgPAHqAAAAAAAQAAAyoA5gQ4ADkAABMiJjU0NjMyFhUUDwEU\
7181 MzI2NTQmKwEiNTQ7ATI2NTQmIyIVFBYVFCMiJjU0NjMyFhUUDgEVFBYVFAZQJykQDg8SCwwhFSQR\
7182 DxIUFCgXJQ4OFQIhDBIlKyMtIyMUMwMqHhgOFhAMEgcHDSofDxkOECUVDRMNAwgIHBEMFR4iGhgj\
7183 EwIBIg8jLQAAAAABAAADAgE2A8AABQAAARUjNSE1ATYe/ugDwL6gHgAAAAEAAPzvATsAAAAPAAAV\
7184 NTMeBBUUBzY1NCYnHgY/UVA3LhKRcO/vNXBlbIlJYGlBSY/fKgABAAAAAAE7AxEADwAAMTUzPgE1\
7185 NCcWFRQOAwcecJITLjdQUT8G7x/hk0dIZ15Iim1ncTUAAgAA/UQBPAAAABgAJgAAGQEzHgYVFAcW\
7186 FRQHNjU0LgMjNR4DFzQ2NTQuAx4GJzM7OC4cEhMeBSk+SEAWCURNVhUBKT5IQf6pAVcbOTM3Oj1I\
7187 JSwrKSs2ORkhN2RFNBirJFRDXSkEDAQ3ZUUzGQAAAAIAAP//ATwCvAAYACYAABURMzI+AzU0JxYV\
7188 FAcWFRQOBQc1Mj4DNTQmNQ4DHhZASD4pBR4TEhwuODszJwYWQUg+KQEVVk1EAQFXGDRFZDchGTk2\
7189 KykrLCVIPTo3MzkbqxkzRWU3BAwDKV1DUwAAAAADAAD9KgE8AJEAGwApADcAABkBMx4GFRQHFhUU\
7190 BxYVFAc2NTQuAiM1HgMXNDY1NC4CIyceAxc0NjU0LgIjHgYnMzs4LhwSEhITHgU9V1UcCURNVhUB\
7191 PFZVHQIJRE1WFQE8VlUd/pACARs5Mzc6PUglLCsnLSwrKSs2ORkhRHdIKakkVEFdKQQNA0R2SCmr\
7192 JFNCXSkEDQNEdkgpAAADAAD/VgE8Ar0AHAApADYAABEzMj4DNTQnFhUUBxYVFAcWFRQOBQcjNzI+\
7193 AjU0JjUOAycyPgI1NCY1DgMeFkBIPikFHhMSEhIcLjg7MycGHh4dVVc9ARVWTUQJHVVXPQEVVk1E\
7194 AVcYNEVkNyEZOTYrKSssLScrLCVIPTo3MzkbqilJeEQEDAQpXUNUhilJeEQDDQQpXUNUAAQAAP1C\
7195 ATwBVAAgAC4APABKAAAZATMeBhUUBxYVFAcWFRQHFhUUBzY1NC4DIzUeAxc0NjU0LgMnHgMXNDY1\
7196 NC4DJx4DFzQ2NTQuAx4GJzM7OC4cEhISEhITHgUpPkhAFglETVYVASk+SEEWCURNVhUBKT5IQRYJ\
7197 RE1WFQEpPkhB/qgCrBs5Mzc6PUglLCsnLSwrJy0sKykrNjkZITdkRTQYqyRUQ10pBAwEN2VFMxmr\
7198 JFRDXSkDDgM3ZUUzGaskVENdKQQMBDdlRTMZAAQAAP6OATwCoAAfACwAOQBGAAAZATMyPgI1NCcW\
7199 FRQHFhUUBxYVFAcWFRQOBQc1Mj4CNTQmNQ4DJzI+AjU0JjUOAycyPgI1NCY1DgMeHFVXPQUeExIS\
7200 EhISHC44OzMnBh1VVz0BFVZNRAkdVVc9ARVWTUQJHVVXPQEVVk1E/o4CrClId0QhGTk2KykrLC0n\
7201 KywtJyssJUg9OjczORuqKUl4RAMNBCldQ1SGKUl4RAMNBCldQ1SGKUl4RAMNBCldQ1QAAAAFAAD9\
7202 VQE8AhIAJAAyAEAATgBcAAAZATMeBhUUBxYVFAcWFRQHFhUUBxYVFAc2NTQuAyM1HgMXNDY1NC4D\
7203 Jx4DFzQ2NTQuAyceAxc0NjU0LgMnHgMXNDY1NC4DHgYnMzs4LhwSEhISEhISEx4FKT5IQBYJRE1W\
7204 FQEpPkhBFglETVYVASk+SEEWCURNVhUBKT5IQRYJRE1WFQEpPkhB/rsDVxs5Mzc6PUglLCsnLSwr\
7205 Jy0sKyctLCspKzY5GSE3ZEU0GKskVENdKQMNBDdlRTMZqyRUQ10pBAwEN2VFMxmrJFRDXSkDDgM3\
7206 ZUUzGaskVENdKQQMBDdlRTMZAAAFAAD9vAE8AnkAIwAwAD0ASgBXAAAZATMyPgI1NCcWFRQHFhUU\
7207 BxYVFAcWFRQHFhUUDgUHNTI+AjU0JjUOAycyPgI1NCY1DgMnMj4CNTQmNQ4DJzI+AjU0JjUOAx4c\
7208 VVc9BR4TEhISEhISEhwuODszJwYdVVc9ARVWTUQJHVVXPQEVVk1ECR1VVz0BFVZNRAkdVVc9ARVW\
7209 TUT9vANXKUh3RCEZOTYrKSssLScrLC0nKywtJyssJUg9OjczORuqKUl4RAMNBCldQ1SGKUl4RAMN\
7210 BCldQ1SGKUl4RAMNBCldQ1SGKUl4RAMNBCldQ1QAAAACAAD/ZADhAbAACgAWAAA3IgYdATY3NjU0\
7211 JjcyFhUUBwYjETMRNmcUKyImKx0HIzlLUkQoI3omE7kPODsvGyYmMiNJTFICTP68NAAAAgAA/oYA\
7212 xQF6AAMADAAAFzc1BxEVNxEjNQcRNxyQkKkZrAFlLZYtAUnoNP3A4jMCQwEAAAIAAP6YAP8BaAAD\
7213 AB8AADcVNzUDIzUHNTc1BzU3NTMVNzUzFTcVBxU3FQcVIzUHU1paHjU1NTUeWh01NTU1HVpGpxun\
7214 /jejD1wPpw9aD6ifHKujD1wPpw9aD6ifHAAAAAEAFP+EAQsAegAeAAAXNSYnBzAVIzUzNycwIzUz\
7215 FRYXNzA1MxUjBgcXMDMVwygMM0g5MzM5SCESNEg5IRM0OXw7Jg0zO0oyMkg5IhEzOUciETRIAAQA\
7216 AP9qAWwBsAAOABwAKwA6AAA3DgEdATI3Njc2NTQnJiM3MhYVFAcGBwYjETMRNhcOAR0BMjc2NzY1\
7217 NCcmIzcyFhUUBwYHDgEjETMRNk4RHg4eHwwEChARGR0rCRgrNS8fGdERHRAeHQsGCxAPFh8qCxko\
7218 FjcWHht9AR4QxikrNA0ZHhQVJjkhEiA5NEACRv7BMiYBHRHGKS8wExMcFhUmNiQWHD4vGyUCRv7B\
7219 MgAAAQAA/wYAjAD6AA4AADcVBhUUFxUuAzQ+AoxQUBolMhsbMiX6FEOjpkAUDx86WnBaOh8AAAEA\
7220 AP8GAIwA+gAOAAA1HgMUDgIHNTY1NCcaJTIbGzIlGlBQ+g8fOlpwWjofDxRApqNDAAACADL/ZAEN\
7221 AbAACgAWAAA3IgYVFBcWFzU0JicyFxEzESInJjU0NqsXIC4uGiozOiMjOlJPOXomGzBFQgPLEh4m\
7222 NAFE/bRSUEUjMgAABP/1/2oBbAGwAA4AHQAsADsAADcjIgcGFRQXFhcWMzU0JicyFxEzESImJyYn\
7223 JjU0NhcOAR0BMjc2NzY1NCcmIzcyFhUUBwYHDgEjETMRNlcBERAKBAwfHg4eKzAZHxY3FysYCSvp\
7224 ER0QHh0LBgsQDxYfKgsZKBY3Fh4bfRUUHhkNNCspxhAeJzIBP/26JRs0OSASITkmAR0RxikvMBMT\
7225 HBYVJjYkFhw+LxslAkb+wTIAAAAAAQAA/sAAqQFAABMAABMzFTcVBxU3FQcVIzUHNTc1BzU3RB5H\
7226 R0dHHkREREQBQKIOXA5/DloPqKIOXA5/DloPAAMAAP6YAToBaAAjACcAKwAANzUzFTcVBxU3FQcV\
7227 IzUHFSM1DwEjNQc1NzUHNTc1MxU3NTMVAzUHFTcVNzXpHjMzMzMePR48AR4zMzMzHj0eHj1bPcCo\
7228 ng9cD58PWg+1qhKspBCong9cD58PWg+1qhKspP79nhGfuJ4RnwAB//0AAAE/APQAGAAANwYjIiY1\
7229 ND8BNi8BJjU0NjMyMRcFFhUUBxICAwcJBs8ODs0ICwcBAgEfDg4BARAICgNJBwZPAwsKEgFrBg4N\
7230 BQAAAAEAAAAAAFAAUAAJAAA1NDYyFhQGIyImFyIXFxEQGCgRFxciFxgAAAABAAAAAAFUADIAAwAA\
7231 MTUhFQFUMjIAAAABAAAAAABkARgAAwAAMwMzAygoZCgBGP7oAAAAAQAAAAABGAE1AAUAADEbASMn\
7232 B4yMQVhaATX+y8bGAAACAAAAAAJYAUoADgAZAAAxNDYzMh4CFSMuASIGByEiJjQ2MzIWFRQGs3k5\
7233 a1UzDwui4KILARwXJSUXGSMjmLIsUYBNboaGbiQwJCQYGSMAAAEAAAAAALYBLQAYAAATMhYXFhUU\
7234 Bw4BIzAnJjU0NjU0Iy4BNTQ2VhsbEBoyGUQQBgFHFBsoLQEtDBEdMD08HS0DAQIIaxMPASYcHjEA\
7235 AAAAAgAFAAABjgH1ABEAIwAANxM2MzIXFhUUBwMGIyInJjU0JxM2MzIXFhUUBwMGIyInJjU0mM0L\
7236 CgYDCwXMCQsEBguLzQoLBgMLBcwIDAQGCyMBwBICBQoGDP4/EQMGDAcHAcASAgUKBgz+PxEDBgwH\
7237 AAEAAP8GAIIA+gADAAA1MxEjgoL6/gwAAAEAAAAAAIIA+gADAAA1MxUjgoL6+gAAAAEAAP+DASwA\
7238 AAADAAAxIRUhASz+1H0AAAEAAAAAASwAfQADAAA1IRUhASz+1H19AAEAAP5+AOsBhwATAAATFwcX\
7239 JiMiBhUUFyY1NDYzMhcnNym9Z2wyNB8mOHg0JSIih2QBh+XZzy4kHTU0S00jLRW8tAAAAQAA/w0B\
7240 AADAABYAADcOAiMiJjU0NjIWFRQHMjY3NjIXAyerAxkaEys3JjgpFyIzIQIVA5YwPAEHBCkoHyAe\
7241 GR0bISwCAv5vEAAAAAEAAP4MAUgAwAAkAAAXBiMiJjU0NjMyFhUUBzI/AQYjIiY1NDYzMhYVFAcy\
7242 NzYyFwMnqyghKzcnGxwpF0ELPDYYKzcnGxwpF0guAhUDxS3EDCgoICAfGR0bIsoMKSgfIB4ZHRtN\
7243 AgL9bgwAAAEAAP4MAY8BwAA2AAA3BiMiJjU0NjMyFhUUBzI/ASIOASMiJjU0NjMyFhUUBzI3NjIX\
7244 AScTBiMiJjU0NjMyFhUUBzI39igfKzcnGxwpFz8LOgEgHBMrNycbHCkXSC4BFgP+9C1VKCErNycb\
7245 HCkXQQs8DCgoICAfGR0bIssJBCkoHyAeGR0bTQIC/G4MASQMKCggIB8ZHRsiAAAAAAEAAP0MAdoB\
7246 wABFAAATBiMiJjU0NjMyFhUUBzI/AQYjIiY1NDYzMhYVFAcyPwEGIyImNTQ2MzIWFRQHMj8BIg4B\
7247 IyImNTQ2MhYVFAcyNzYyFwEnqyghKzcnGxwpF0ELOighKzcnGxwpF0ELOigfKzcnGxwpFz8LOgEg\
7248 HBMrNyY4KRdILgIVA/6pLf48DCgoICAfGR0bIsoMKCggIB8ZHRsiygwoKCAgHxkdGyLLCQQpKB8g\
7249 HhkdG00CAvtuDAAAAAEAAP0MAhkCrgBWAAAlBiMiJjU0NjMyFhUUBzI/AQYjIiY1NDYzMhYVFAcy\
7250 PwEiDgEjIiY1NDYzMhYVFAcyNzYyFwEnEwYjIiY1NDYzMhYVFAcyPwEGIyImNTQ2MzIWFRQHMjcB\
7251 PyghKzcnGxwpF0ELNigfKzcnGxwpFz8LNAEgHBMrNycbHCkXSC4BFgP+ai1VKCErNycbHCkXQQs6\
7252 KCErNycbHCkXQQs0DCgoICAfGR0bIsQMKCggIB8ZHRsixwkEKSgfIB4ZHRtNAgL6gAwBJAwoKCAg\
7253 HxkdGyLIDCgoICAfGR0bIgAD//D/BgImAPoABwAPABMAADYiJjQ2MhYUACImNDYyFhQFATMBUDIj\
7254 IzIjAYgyIyMyI/3SAbh+/kdLIzIjIzL+zyMyIyMyWgH0/gwABP/h/wYDBwD6AAcADwATABcAADYi\
7255 JjQ2MhYUACImNDYyFhQFATMBMwEzAUEyIyMyIwJ3MiMjMiP84wG4e/5HeQG4e/5HSyMyIyMy/s8j\
7256 MiMjMloB9P4MAfT+DAAC/7T/iAF8ARgAEQA7AAA3FjMyNjc2NTQnJiMiBgcGFRQXIicHMzIUKwEi\
7257 NDsBEzY1NCMiDgMHBiY3Njc2MzIWFz4BMzIWFRQGxQIFEjIODQ8CBBI1Cw8nKhktNAsL4QsLS2gG\
7258 CwgMDwsZCgUbBTEPFyUjJAcdJiMeLVkoATUkICYlBQEzHCckKS4geh4eAR0SDA8HFBIsEAgPCVgQ\
7259 GRMaHg80MElrAAAB/9v/9gG+ARgAUwAANwYHBisBIj8BNiYjIgYHBiY3PgMzMhc2MzIXPgEzMhYV\
7260 FA8BBhUUMzI3PgU3NhYHDgIjIiY1ND8BNjU0IyIPAQYHJwYmPwE2NTQjIgdQBwgEBDUNDUIEBggN\
7261 FiQFFQQUECIfEjcLJCQtCQspExkkBS8ECAEEBQsHDAMNAQYVBhIXLR8VGQU0ARUbCEEIDyYNCARD\
7262 ARUbCBURAwEapw0PGjkIDAklGjASKCgoEBgjGgsPfQsJDgIDCQYPBRIBCQ0LHx8aFhMNDYgCBA4U\
7263 qxYBAQEPCKsCBA4UAAAAAf9+/2ABXgG4AEEAAAciJjU0NjMyFhUUBwYVFDMyPgc3IyI1NDsBPgEz\
7264 MhYVFAYjIiY1NDc2NCMiDgcHMzIUKwEOATIgMBcTEhcSChkLEA8LDQoODRQKNRMRQRRpNCAwFxMS\
7265 FxIKGQcMCgcIBQYDBgE2ExM/IXagJiAaIhQPDgsHDQ4GERMlJDw3VScVE0tfJiAaIhQPDgsGHAUL\
7266 ChQNGg0eBijFwQAAAf/bAAABEwETACoAADc+ASYjIgYPAQYHBisBIj8BNiYjIgYHBiY3PgEzMhYX\
7267 NjMyFhUUIyImNTTQBQMDBRQkCT8HCAQENQ0NQgQGCA4XIgUVBCE4IhsdBB8kGiArDxvnAwcELBic\
7268 EQMBGqMNDxo1CAwJPEAaDSceGTcWDRQAAAABAAAAAADcARgAMQAAMyImNTQ2MhYVFAcWMzI2NTQu\
7269 AicmNTQ2MzIWFRQGIyImNTQ3JiMiBhUUHgIXFhUUUB4yFRoXEAwTFiEJCxgGOjctIjYWEA0WBw8R\
7270 DxkREh4FMCsbEBYQDBIQEBYSCw8HDQQlMCMoJBgQGBQOAw4TEQ0JEwwRAyArVQAAAf/k//wA5wEP\
7271 ADwAACcGLgE/ASIGIyImDgEHBicmNz4BNx4BMzI2MzIXFhQPAQYVFDMyNjMyFxY3NiciNTQ2MzIV\
7272 FAYrAS4BIyIKBgsBBbAFHwwDFAwfBQ8EAwgMCwEOLxUiKwQIBQsHngICAQ0GGiMNCAUHJBINJige\
7273 HRglCA4DBAgNBsEGBgIyBQ0LCRIeMAIBBgsBAREHpwYCAwMaCg4NAx0OFTceLwMYAAX/tP+IBUsB\
7274 GAAQACIAtQDGANcAACUWMzI2NzY0JyYjIgYHBhUUBRYzMjY3NjU0JyYjIgYHBhUUFyInBzMyFCsB\
7275 IjQ7ARM2NTQjIg4DBwYmNzY3NjMyFhc+ATMyFz4BMzIWFz4BMzIXPgEzMhYXPgEzMhc+ATMyFhc+\
7276 ATMyFhUUBiMiJwczMhQrASI0OwETNjU0IyIGBxUUBiMiJwczMhQrASI0OwETNjU0IyIGBxUUBiMi\
7277 JwczMhQrASI0OwETNjU0IyIGBxUUBiUWMzI2NzY0JyYjIgYHBhUUBRYzMjY3NjQnJiMiBgcGFRQC\
7278 CgIFEjIODQ8CBBI1Cw/+ywIFEjIODQ8CBBI1Cw8TFhktNAsL4QsLS2gGCwgMDwsZCgUbBTEPFyUj\
7279 JAcdJiMvExIiGyMkBx0mIy8TEiIbIyQHHSYjLxMSIhsjJAcdJiMeLWtJFhktNAsL4QsLS2gGCw4U\
7280 FGtJFhktNAsL4QsLS2gGCw4UFGtJFhktNAsL4QsLS2gGCw4UFGsDgwIFEjIODQ8CBBI1Cw/+ywIF\
7281 EjIODQ8CBBI1Cw8oATUkIUoFATMcJyQpBgE1JCAmJQUBMxwnJCkuIHoeHgEdEgwPBxQSLBAIDwlY\
7282 EBkTGh4PMhwWExoeDzIcFhMaHg8yHBYTGh4PNDBHbSB6Hh4BHRIMDxgjAUdtIHoeHgEdEgwPGCMB\
7283 R20geh4eAR0SDA8YIwFHbSgBNSQhSgUBMxwnJCkGATUkIUoFATMcJyQpAAT/tP+IBAYBGABvAIAA\
7284 kQCjAAAhIicHMzIUKwEiNDsBEzY1NCMiBgcVFAYjIicHMzIUKwEiNDsBEzY1NCMiBgcVFAYjIicH\
7285 MzIUKwEiNDsBEzY1NCMiDgMHBiY3Njc2MzIWFz4BMzIXPgEzMhYXPgEzMhc+ATMyFhc+ATMyFhUU\
7286 BicWMzI2NzY0JyYjIgYHBhUUBRYzMjY3NjQnJiMiBgcGFRQFFjMyNjc2NTQnJiMiBgcGFRQDUhYZ\
7287 LTQLC+ELC0toBgsOFBRrSRYZLTQLC+ELC0toBgsOFBRrSRYZLTQLC+ELC0toBgsIDA8LGQoFGwUx\
7288 DxclIyQHHSYjLxMSIhsjJAcdJiMvExIiGyMkBx0mIx4ta0wCBRIyDg0PAgQSNQsP/ssCBRIyDg0P\
7289 AgQSNQsP/ssCBRIyDg0PAgQSNQsPIHoeHgEdEgwPGCMBR20geh4eAR0SDA8YIwFHbSB6Hh4BHRIM\
7290 DwcUEiwQCA8JWBAZExoeDzIcFhMaHg8yHBYTGh4PNDBHbSgBNSQhSgUBMxwnJCkGATUkIUoFATMc\
7291 JyQpBgE1JCAmJQUBMxwnJCkAAAP/tP+IAsEBGABMAF4AbwAAMyInBzMyFCsBIjQ7ARM2NTQjIg4D\
7292 BwYmNzY3NjMyFhc+ATMyFz4BMzIWFz4BMzIWFRQGIyInBzMyFCsBIjQ7ARM2NTQjIgYHFRQGJxYz\
7293 MjY3NjU0JyYjIgYHBhUUBRYzMjY3NjQnJiMiBgcGFRTIFhktNAsL4QsLS2gGCwgMDwsZCgUbBTEP\
7294 FyUjJAcdJiMvExIiGyMkBx0mIx4ta0kWGS00CwvhCwtLaAYLDhQUa0wCBRIyDg0PAgQSNQsPAVUC\
7295 BRIyDg0PAgQSNQsPIHoeHgEdEgwPBxQSLBAIDwlYEBkTGh4PMhwWExoeDzQwR20geh4eAR0SDA8Y\
7296 IwFHbSgBNSQgJiUFATMcJyQpBgE1JCFKBQEzHCckKQAAAAL/2/+IAysBGAByAIMAACU2NzYzMhYX\
7297 PgEzMhYVFAYjIicHMzIUKwEiNDsBEzY1NCMiDgMHDgEjIiY1ND8BNjU0IyIPAQYHJwYmPwE2NTQj\
7298 Ig8BBgcGKwEiPwE2JiMiBgcGJjc+AzMyFzYzMhc+ATMyFhUUDwEGFRQzMjc+ARcWMzI2NzY0JyYj\
7299 IgYHBhUUAa0xFBgiIyQHHSYjHi1rSRYZLTQLC+ELC0toBgsHDxIOGAgaPC0VGQU0ARUbCEEIDyYN\
7300 CARDARUbCEQHCAQENQ0NQgQGCA0WJAUVBBQQIh8SNwskJC0JCykTGSQFLwQIAQQPJdECBRIyDg0P\
7301 AgQSNQsPd2wYHRMaHg80MEdtIHoeHgEdEgwPDR8bNRA2OBYTDQ2IAgQOFKsWAQEBDwirAgQOFKwR\
7302 AwEapw0PGjkIDAklGjASKCgoEBgjGgsPfQsJDgIINTkBNSQhSgUBMxwnJCkAAAAAAv/b/2ADGQG4\
7303 AFMAlQAANwYHBisBIj8BNiYjIgYHBiY3PgMzMhc2MzIXPgEzMhYVFA8BBhUUMzI3PgU3NhYHDgIj\
7304 IiY1ND8BNjU0IyIPAQYHJwYmPwE2NTQjIgcTIiY1NDYzMhYVFAcGFRQzMj4HNyMiNTQ7AT4BMzIW\
7305 FRQGIyImNTQ3NjQjIg4HBzMyFCsBDgFQBwgEBDUNDUIEBggNFiQFFQQUECIfEjcLJCQtCQspExkk\
7306 BS8ECAEEBQsHDAMNAQYVBhIXLR8VGQU0ARUbCEEIDyYNCARDARUbCPUgMBcTEhcSChkLEA8LDQoO\
7307 DRQKNRMRQRRpNCAwFxMSFxIKGQcMCgcIBQYDBgE2FBQ/IXYVEQMBGqcNDxo5CAwJJRowEigoKBAY\
7308 IxoLD30LCQ4CAwkGDwUSAQkNCx8fGhYTDQ2IAgQOFKsWAQEBDwirAgQOFP6fJiAaIhQPDgsHDQ4G\
7309 ERMlJDw3VScVE0tfJiAaIhQPDgsGHAULChQNGg0eBijFwQAAAAH/fv9gAmkBuAB0AAAlIw4BIyIm\
7310 NTQ2MzIWFRQHBhUUMzI+BzcjIjU0OwE+ATMyFhUUBiMiJjU0NzY0IyIHBgcXPgEzMhYVFAYjIiY1\
7311 NDc2NCMiDgcHMzIUKwEOASMiJjU0NjMyFhUUBwYVFDMyPgcBX5shdl8gMBcTEhcSChkLEA8LDQoO\
7312 DRQKNRMRQRRpNCAwFxMSFxIKGSYXAwGbFGk0IDAXExIXEgoZBwwKBwgFBgMGATYUFD8hdl8gMBcT\
7313 EhcSChkLEA8LDQoPDRTmxcEmIBoiFA8OCwcNDgYREyUkPDdVJxUTS18mIBoiFA8OCwYccwwGAUtf\
7314 JiAaIhQPDgsGHAULChQNGg0eBijFwSYgGiIUDw4LBw0OBhETJSQ8N1UAAAAB/37/YAN0AbgAqgAA\
7315 EzM+ATMyFhUUBiMiJjU0NzY0IyIHBgcXPgEzMhYVFAYjIiY1NDc2NCMiDgcHMzIUKwEOASMiJjU0\
7316 NjMyFhUUBwYVFDMyPgc3Iw4BIyImNTQ2MzIWFRQHBhUUMzI+BzcjDgEjIiY1NDYzMhYVFAcGFRQz\
7317 Mj4HNyMiNTQ7AT4BMzIWFRQGIyImNTQ3NjQjIg4CBwbMnBRpNCAwFxMSFxIKGSYXAwGbFGk0IDAX\
7318 ExIXEgoZBwwKBwgFBgMGATYTEz8hdl8gMBcTEhcSChkLEA8LDQoPDRQKmyF2XyAwFxMSFxIKGQsQ\
7319 DwsNCg4NFQqbIXZfIDAXExIXEgoZCxAPCw0KDg0UCjUTEUEUaTQgMBcTEhcSChkOFQ4IBgIBDktf\
7320 JiAaIhQPDgsGHHMMBgFLXyYgGiIUDw4LBhwFCwoUDRoNHgYoxcEmIBoiFA8OCwcNDgYREyUkPDdV\
7321 J8XBJiAaIhQPDgsHDQ4GERMlJDs4VSfFwSYgGiIUDw4LBw0OBhETJSQ8N1UnFRNLXyYgGiIUDw4L\
7322 BhwTJyIbCgAB/37/YASAAbgA4AAAARc+ATMyFhUUBiMiJjU0NzY0IyIHBgcXPgEzMhYVFAYjIiY1\
7323 NDc2NCMiDgcHMzIUKwEOASMiJjU0NjMyFhUUBwYVFDMyPgc3Iw4BIyImNTQ2MzIWFRQHBhUUMzI+\
7324 BzcjDgEjIiY1NDYzMhYVFAcGFRQzMj4HNyMOASMiJjU0NjMyFhUUBwYVFDMyPgc3IyI1NDsBPgEz\
7325 MhYVFAYjIiY1NDc2NCMiBwYHFz4BMzIWFRQGIyImNTQ3NjQjIg4HAdicFGk0IDAXExIXEgoZJhcD\
7326 AZsUaTQgMBcTEhcSChkHDAoHCAUGAwYBNhMTPyF2XyAwFxMSFxIKGQsQDwsNCg8NFAqbIXZfIDAX\
7327 ExIXEgoZCxAPCw0KDg0UCpshdl8gMBcTEhcSChkLEA8LDQoPDRQKmyF2XyAwFxMSFxIKGQsQDwsN\
7328 Cg4NFAo1ExFBFGk0IDAXExIXEgoZJhcDAZsUaTQgMBcTEhcSChkHDAoHCAUGAwYBDwFLXyYgGiIU\
7329 Dw4LBhxzDAYBS18mIBoiFA8OCwYcBQsKFA0aDR4GKMXBJiAaIhQPDgsHDQ4GERMlJDw3VSfFwSYg\
7330 GiIUDw4LBw0OBhETJSM8N1YnxcEmIBoiFA8OCwcNDgYREyUkPDdVJ8XBJiAaIhQPDgsHDQ4GERMl\
7331 JDw3VScVE0tfJiAaIhQPDgsGHHMMBgFLXyYgGiIUDw4LBhwFCgsTDhkOHQAAAAADAAD/YALfAbgA\
7332 NgBxALYAADMiJjU0NjMyFhUUBxYzMjY1NC4DJy4CNTQ2MzIWFRQGIyImNTQ3JiMiBhUUHgIXHgEV\
7333 FCUGLgE/ASYjIgYjIg4BBwYnJjc+ATcWMzI2MzIXFhQPAQYVFB4BFxY+AScuATU0NjMyFRQGKwEu\
7334 ASMiBSImNTQ2MzIWFRQHDgEeARUUFjI+BzcjIjU0OwE+ATMyFhUUBiMiJjU0NzY0IyIOBwczMhQr\
7335 AQ4BUB4yEgwOFwwGGBYhAwwFFwMUGBQ3LSI2FhANFgUQDg8ZERIeBRsXASYGCwEFrgoUBRoJBRAS\
7336 BRAFAwcLDAExGiAoBwIUCweaBBoqDQUJBQIDJBQMJisgBhc1CRP+tCAwFxMSFxIEAQECBhYQDwsN\
7337 Cg4NFAo1ExFBFGk0IDAXExIXEgoZBwwKBwgFBgMGATYTEz8hdisbEBYNCxgMEhYSCQwNBA4CDRIe\
7338 DyMtJBgQGBQOBgoUEQ0JEwwRAxIfFVoDBAgNBr8FARgfBA0LCBMeLgQDCAIBEQejBQUHAwcLAwQN\
7339 BggIFAsTNx4qAg2yJiAaIhQPDgsCBQQGAwgGBhETJSQ8N1UnFRNLXyYgGiIUDw4LBhwFCwoUDRoN\
7340 HgYoxcEAAgAF//sB/AGaAAkALAAAAQ8BBhUUMzI2NwcOASMiJjU0PwEjNTM/AQc3MhU2MzIWFRQG\
7341 IiY1NDcGDwEjATZ/OQIUGEQSDCkyHyIiAzdpcxZZJpgZIDUYHRggFgk3EEJIAQkHuAgDFRcPKBkU\
7342 JBoLC7MgSi54Ci0pHBUSGxEOEhMPJ9gAAQAA//0BtQDUAC8AACUyNTQnBiImNT4BMzIWFRQHBiMi\
7343 LwEmIyIVFBc2MzIWFRQGByInJjU0NzYzFh8BFgFwLRoQHBQBFwkkLSkXHiYeohoRLhkQDw0VFA0d\
7344 GBwoFiIqF6IcMTkgExAWDQ4WMzUzJBUVehI6IBIQFw4PEgIaIi0zJBMCEHoTAAEAAP/NAbUBAwA2\
7345 AAAXIiY1NDc2MxYfATUzFRcWMzI2NTQnBiMiNT4BMzIWFRQHBiMiLwEVIzUnJiMiBhUUFzYzMhUU\
7346 TSAtKBYgKBcuHlwiDxUcFhQSHAEPCSAtKRccJB4uHlwgDxYcFRQTHANDJjMkEwIQJGmARxcpHCcQ\
7347 DBsPFUImMyQVFSRsg0cWKB4nDwwdIAABAA0AAAJFAOAACwAANyc3FzcXNxcHJwcnIhWOZXhqTRaS\
7348 aXRpLhmZfHx8VBehfHx8AAAAAQAN/8sCRQERABMAACUHJwcnNxc3NTMXNxc3FwcnBxUjARZAaUsV\
7349 jmUWGwFGak8UkmkZG0ZGfE4ZmXwXlntKfFUYoXwblgAAAQAAAAABGAEYAAsAADM1IzUzNTMVMxUj\
7350 FXt7eyJ7e3sie3siewAAAAEAAP6OATYAAAAKAAARNT4ENzMUBiQxTDQ0Dx7A/o48Bw8rPW9JjtoA\
7351 AAAAAf//AAABLQCgAB0AADc+AjMyHgEXFjMyNzYWBw4CIyIuAScmIyIHBiYBCRApHBgmJg8JCh4Y\
7352 BA4CCBEpHBgkJRIHCB0dBQ1GGSEgITEKBiQGBwcZIh8hMQoEIwYIAAAAAQAAAAABLAEsAAcAADER\
7353 IREjNSMVASwj5gEs/tS0tAABAAAAAAD6AcIABgAAMwMzGwEzA2lpKFVVKGkBwv6YAWj+PgACAAAA\
7354 AADIAMgABwAPAAA2MjY0JiIGFBYiJjQ2MhYURT4sLD4sdFI7O1I7GSw+LCw+RTtSOztSAAH/OAAA\
7355 AMgAyAALAAAjNDYyFhUjNCYiBhXIdqR2HmCUYFJ2dlJKYGBKAAAAAgAAAAAAtAEsAAcAFQAANjI2\
7356 NCYiBhQXNS4BNTQ2MhYVFAYHFUseGxseGxgdKzdGNysdeDU2NTU2rWQINScoPDwoJzUIZAAAAgAA\
7357 AAAAyAEsAA8AHwAANy4BNTQ2MhYVFAYHHQEjNTc+ATU0JiIGFRQWFz0BMxVUJDA7UjswJCAgGSIs\
7358 PiwiGSBmBTglKTs7KSU4BQFlZRkGKRsfLCwfGykGAUlJAAAABAAA//wD9AJ/AIMAjQCZAKMAADc+\
7359 ATU0Jy4BNTQ+Aj8CDgEVFDMyNxcOASMiJjU0PgIzMhYVFAYjIiYnNx4BMzI1NC4CJwcGFRQeAhUU\
7360 Bg8BHgI7ATI3JjU0NzYzMhYVFAcGBx4BMzI2NTQ2Ny4CPQEeARUUBiMiJwYjIi4BJw4BIiYnJiMi\
7361 DgEHBiMiNTQ2BSImNDYzMhYUBiU+ATU0JyYjIgYVFAU2NTQmJw4BFRSYNkUFA1IDBAUBAj1iaiAe\
7362 HRoVKSUeLR8+cEh9cjIuHDYSGBEUFDMMHEAtGwkdIh0fEBAOKR8MDREiIRwiTBsjCh08EyAbFjFD\
7363 XSCBXrq0UUJBIyQ8ITMWDiwoIBscGhIKFBkGWiEKMANcCxMTCwwSEf4PMyQEBRUcLgE8WhcSPzBa\
7364 GD8gCgwIdCIFDg4MBASlBGMsIzwNQi8mGiFHRCxRNzA1MCsOHhEyChoiGgJCJBseNiEsFSZPFBQK\
7365 KRoeNSg1JS4rICETOTAXEioZUl8ZLFk0BgE8sWtGXzk6FRQSJhkWIC0LFQQ6Bw04TxIYEhIYEoAv\
7366 LxsHDhI5Kx50BYEiSRUdWEQ/AAACAAwACgHTAc8ACgCPAAAlNCYjIgYUFjMyNicOASMiJjQ2MzIW\
7367 FzY1NCcmIyImNDYzMhceARcWMzI1NCcuATU0NjMyFhUUBgcUMzI3PgE3NjMyFhUUBiMiBgcGFRQz\
7368 MjYzMhYUBiMiJiMiBhUUFx4BFxYVFAYjIicuAScmIyIVFBYVFAYjIiY0NjU0IyIHDgEHBiMiJjU0\
7369 NzYzMjc2NTQBFhgPEBUWDxAXiRkmDhkbGhkNKhglDBQYHRwZFxIQCwMUCxIUAQInHhQSGyMBFhAN\
7370 EQENDBoTHhsSGxUNECEbKw4cGx0ZDycUFxILFDgNDhkXExQMARMRChIqIBMSHSYXDQ4SAxESDBQa\
7371 DQwWIxIN7hAUEyIWFQoBJRkqGyUCAxYOChMcKB0MCz4WDRgNCBcnDxcaGxYRIxkqEBM4Dw4bFBEj\
7372 BgsODxYnHSgaJggKFgoSAwsNGRMbDgs5FQ8fHjATFBgaJDEXJA4SPQwHFhQYDg0SDQwaAAAAAwAA\
7373 /wYB9AD6AAcADwAXAAAEIiY0NjIWFCQUFjI2NCYiAjQ2MhYUBiIBEzIjIzIj/vd4qnh4qqWS0JKS\
7374 0DwjMiMjMm6qeHiqeP7L0JKS0JIAAAAAAgAA/wYB9AD6AAcADwAANhQWMjY0JiICNDYyFhQGIi14\
7375 qnh4qqWS0JKS0FWqeHiqeP7L0JKS0JIAAAAAAwAA/qIB9AFeAAUAFgAcAAAXEQ4BFBYTNTMVHgEU\
7376 BgcVIzUuATU0NhM+ATQmJ+NNaWlNLWGDg2EtYIODjU5paU7MAZgIdpx2Ab1lZQiPxI8IZWUJj2Fi\
7377 j/5DCHWedQgAAgAA/wYB0gD6AAcAKQAABCImNDYyFhQ3FhUUBwYjIicmIyIGFBYzMjc2MzIXFhUU\
7378 BwYjIiY0NjMyARMyIyMyI5kCCwUGDQozd1V4eFV4MgYSBwMMA0CVaJKSaJQ8IzIjIzKbBgQPBgMO\
7379 X3iqeF8NAgcMBgZ4ktCSAAAAAAEAAP8GAdIA+gAhAAAlFhUUBwYjIicmIyIGFBYzMjc2MzIXFhUU\
7380 BwYjIiY0NjMyAc8CCwUGDQozd1V4eFV4MgYSBwMMA0CVaJKSaJSCBgQPBgMOX3iqeF8NAgcMBgZ4\
7381 ktCSAAACAAD+ogHSAV4AJAAqAAAlFhUUBwYjIicmJxE2NzYzMhcWFRQHBgcVIzUuATU0Njc1MxUW\
7382 Bw4BFBYXAc8CCwUGDQouZmYuBhIHAwwDO4QtYIODYC2EsU1paU2CBgQPBgMOVAr+aAdXDQIHDAYG\
7383 cAdlZQmPYWKPCGVlCSQIdpx2CAAAAAACAAD9EgFKAL4AAwAPAAA3FSE1JTMVITUzESMRIRUjHgEO\
7384 /tQeAQ4eHv7yHkaMjHgyMvxUAmIyAAEAAAAAAlYBcgALAAA1Nxc3FzcXAScHJweJVFZSryL+/FRW\
7385 UzVBuXNzcekW/qR0dHBHAAAB/+gAvQESATcAFwAAJj4BMzIWMjc2MzIVFA4BIyImIgcGIyI1GDAh\
7386 HBRVKBQDBw4wIRwUVSgUBAcN9jARPRQDBwwwET0UBAgAAAAAAgAA/4kBwgCLAAsAEwAABTQmIyIG\
7387 FRQWMzI+ARQGIiY0NjIBUVo2IDRdNyEvcX3IfX3IFDRPJx80TCN0bEtLbEsAAAACAAD/bAFIAqgA\
7388 DQAcAAAlJiMiBhUUFxYzMjY1NBMzERQGIyInJjU0NjMyFwEkDSU8lwYLJjyXAh1+SE8kD35IQCRE\
7389 F2ErCgkXYSsJAm79bkNnQx4dQ2ctAAAAAAEAAP95AUACqAANAAABMxEUBiMiJjU0NjMyFwEiHnlZ\
7390 Mjx6WDEfAqj9gEZpOCdFahwAAAABAAD/eQIKAqgAGgAAAREUBiMiJjU0NjMyFxE1Mx4EFRQHNjU0\
7391 AUB5WTI8elgxHx4GLjk4JTISAbn+b0ZpOCdFahwBTu81XEdOdUlIckFJ7wAAAAACAAD/eQILAqgA\
7392 HwAoAAAlNREzHgQVFAcWFRQHNjU0JiMRFAYjIiY1NDYzMhMeARc0NjU0JgEiHgkwODYjFRYiBXY4\
7393 eVkyPHpYMT0OhRsBdmvmAVckRztCWDMlMjAkLkEZIV+e/tZGaTgnRWoBdjuiNQMOA1+fAAABAAD/\
7394 zgBkADIABwAAFiImNDYyFhRHKh0dKh0yHSodHSoAAAAAAA4ArgABAAAAAAAAAIMBCAABAAAAAAAB\
7395 AAcBnAABAAAAAAACAAcBtAABAAAAAAADACMCBAABAAAAAAAEAAcCOAABAAAAAAAFAAgCUgABAAAA\
7396 AAAGAAcCawADAAEECQAAAQYAAAADAAEECQABAA4BjAADAAEECQACAA4BpAADAAEECQADAEYBvAAD\
7397 AAEECQAEAA4CKAADAAEECQAFABACQAADAAEECQAGAA4CWwBDAG8AcAB5AHIAaQBnAGgAdAAgAFwA\
7398 MgA1ADEAIAAyADAAMQA4AC0AMgAwADIANQAgAEoAZQBhAG4ALQBGAHIAYQBuAGMAbwBpAHMAIABN\
7399 AG8AaQBuAGUALgAgAFQAaABpAHMAIABmAG8AbgB0ACAAaQBzACAAbABpAGMAZQBuAHMAZQBkACAA\
7400 dQBuAGQAZQByACAAdABoAGUAIABTAEkATAAgAE8AcABlAG4AIABGAG8AbgB0ACAATABpAGMAZQBu\
7401 AHMAZQAgAFwAKABoAHQAdABwADoALwAvAHMAYwByAGkAcAB0AHMALgBzAGkAbAAuAG8AcgBnAC8A\
7402 TwBGAEwAXAApAC4AAENvcHlyaWdodCBcMjUxIDIwMTgtMjAyNSBKZWFuLUZyYW5jb2lzIE1vaW5l\
7403 LiBUaGlzIGZvbnQgaXMgbGljZW5zZWQgdW5kZXIgdGhlIFNJTCBPcGVuIEZvbnQgTGljZW5zZSBc\
7404 KGh0dHA6Ly9zY3JpcHRzLnNpbC5vcmcvT0ZMXCkuAABhAGIAYwAyAHMAdgBnAABhYmMyc3ZnAABS\
7405 AGUAZwB1AGwAYQByAABSZWd1bGFyAABGAG8AbgB0AEYAbwByAGcAZQAgADIALgAwACAAOgAgAGEA\
7406 YgBjADIAcwB2AGcAIAA6ACAAMQAyAC0AMgAtADIAMAAyADUAAEZvbnRGb3JnZSAyLjAgOiBhYmMy\
7407 c3ZnIDogMTItMi0yMDI1AABhAGIAYwAyAHMAdgBnAABhYmMyc3ZnAABWAGUAcgBzAGkAbwBuACAA\
7408 AFZlcnNpb24gAABhAGIAYwAyAHMAdgBnAABhYmMyc3ZnAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAA\
7409 AAAAAAAAAAAAAAAAAJEAAAABAAIBAgADAQMBBAEFAQYBBwEIAQkBCgELAQwBDQEOAQ8BEAERARIB\
7410 EwEUARUBFgEXARgBGQEaARsBHAEdAR4BHwEgASEBIgEjASQBJQEmAScBKAEpASoBKwEsAS0BLgEv\
7411 ATABMQEyATMBNAE1ATYBNwE4ATkBOgE7ATwBPQE+AT8BQAFBAUIBQwFEAUUBRgFHAUgBSQFKAUsB\
7412 TAFNAU4BTwFQAVEBUgFTAVQBVQFWAVcBWAFZAVoBWwFcAV0BXgFfAWABYQFiAWMBZAFlAWYBZwFo\
7413 AWkBagFrAWwBbQFuAW8BcAFxAXIBcwF0AXUBdgF3AXgBeQF6AXsBfAF9AX4BfwGAAYEBggGDAYQB\
7414 hQGGAYcBiAGJAYoBiwGMAY0BjgYubm9kZWYHdW5pRTAwMAd1bmlFMDMwB3VuaUUwMzgHdW5pRTAz\
7415 OQd1bmlFMDQzB3VuaUUwNDQHdW5pRTA0NQd1bmlFMDQ2B3VuaUUwNDcHdW5pRTA0OAd1bmlFMDUw\
7416 B3VuaUUwNUMHdW5pRTA2Mgd1bmlFMDY5B3VuaUUwN0EHdW5pRTA3Qgd1bmlFMDdDB3VuaUUwN0QH\
7417 dW5pRTA3RQd1bmlFMDgwB3VuaUUwODEHdW5pRTA4Mgd1bmlFMDgzB3VuaUUwODQHdW5pRTA4NQd1\
7418 bmlFMDg2B3VuaUUwODcHdW5pRTA4OAd1bmlFMDg5B3VuaUUwOEEHdW5pRTA4Qgd1bmlFMDhDB3Vu\
7419 aUUwOTQHdW5pRTA5NQd1bmlFMEEwB3VuaUUwQTEHdW5pRTBBMgd1bmlFMEEzB3VuaUUwQTQHdW5p\
7420 RTBBOQd1bmlFMEIzB3VuaUUxMDEHdW5pRTFCOQd1bmlFMUJCB3VuaUUxRTcHdW5pRTFGMgd1bmlF\
7421 MUY0B3VuaUUxRjcHdW5pRTFGOQd1bmlFMUZDB3VuaUUxRkUHdW5pRTFGRgd1bmlFMjAwB3VuaUUy\
7422 NDAHdW5pRTI0MQd1bmlFMjQyB3VuaUUyNDMHdW5pRTI0NAd1bmlFMjQ1B3VuaUUyNDYHdW5pRTI0\
7423 Nwd1bmlFMjQ4B3VuaUUyNDkHdW5pRTI2MAd1bmlFMjYxB3VuaUUyNjIHdW5pRTI2Mwd1bmlFMjY0\
7424 B3VuaUUyNmEHdW5pRTI2Ygd1bmlFMjgwB3VuaUUyODEHdW5pRTI4Mgd1bmlFMjgzB3VuaUU0QTAH\
7425 dW5pRTRBMgd1bmlFNEE0B3VuaUU0QTgHdW5pRTRBQwd1bmlFNEMwB3VuaUU0Q0UHdW5pRTREMQd1\
7426 bmlFNEUxB3VuaUU0RTIHdW5pRTRFMwd1bmlFNEU0B3VuaUU0RTUHdW5pRTRFNgd1bmlFNEU3B3Vu\
7427 aUU0RTgHdW5pRTRFOQd1bmlFNEVBB3VuaUU1MDAHdW5pRTUwMQd1bmlFNTIwB3VuaUU1MjEHdW5p\
7428 RTUyMgd1bmlFNTIzB3VuaUU1MjQHdW5pRTUyNQd1bmlFNTI5B3VuaUU1MkEHdW5pRTUyQgd1bmlF\
7429 NTJDB3VuaUU1MkQHdW5pRTUyRgd1bmlFNTMwB3VuaUU1MzEHdW5pRTUzOQd1bmlFNTY2B3VuaUU1\
7430 NjcHdW5pRTU2OQd1bmlFNTZDB3VuaUU1NkQHdW5pRTU4Mgd1bmlFNUQwB3VuaUU1RTIHdW5pRTYx\
7431 MAd1bmlFNjEyB3VuaUU2MTQHdW5pRTYxOAd1bmlFNjI0B3VuaUU2MzAHdW5pRTY1MAd1bmlFNjU1\
7432 B3VuaUU5MTAHdW5pRTkxMQd1bmlFOTEyB3VuaUU5MTQHdW5pRTkxNQd1bmlFOTE4B3VuaUU5NUQH\
7433 dW5pRUEwMgd1bmlFQUE0B3VuaUVDQTIHdW5pRUNBMwd1bmlFQ0E1B3VuaUVDQTcHdW5pRUNBOQd1\
7434 bmlFQ0I3AAAAAAH//wACAAEAAAAAAAAADAAUAAQAAAACAAAAAQAAAAEAAAAAAAEAAAAA44To7gAA\
7435 AADRlyIXAAAAAOPSSjM=\
7436 ") format("truetype")'
7437 // abc2svg - format.js - formatting functions
7438 //
7439 // Copyright (C) 2014-2025 Jean-Francois Moine
7440 //
7441 // This file is part of abc2svg-core.
7442 //
7443 // abc2svg-core is free software: you can redistribute it and/or modify
7444 // it under the terms of the GNU Lesser General Public License as published by
7445 // the Free Software Foundation, either version 3 of the License, or
7446 // (at your option) any later version.
7447 //
7448 // abc2svg-core is distributed in the hope that it will be useful,
7449 // but WITHOUT ANY WARRANTY; without even the implied warranty of
7450 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7451 // GNU Lesser General Public License for more details.
7452 //
7453 // You should have received a copy of the GNU Lesser General Public License
7454 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
7455
7456 var font_scale_tb = {
7457 serif: 1,
7458 serifBold: 1,
7459 'sans-serif': 1,
7460 'sans-serifBold': 1,
7461 Palatino: 1.1,
7462 monospace: 1
7463 },
7464 txt_ff = "text,serif", // text font-family (serif for compatibility)
7465 ff = {}, // font-face's from %%beginsvg
7466 fmt_lock = {}
7467
7468 var cfmt = {
7469 "abc-version": "1", // default: old version
7470 annotationfont: {name: "text,sans-serif", size: 12},
7471 aligncomposer: 1,
7472 beamslope: .4, // max slope of a beam
7473 // botmargin: .7 * IN, // != 1.8 * CM,
7474 bardef: {
7475 "[": "", // invisible
7476 "[]": "",
7477 "|:": "[|:",
7478 "|::": "[|::",
7479 "|:::": "[|:::",
7480 ":|": ":|]",
7481 "::|": "::|]",
7482 ":::|": ":::|]",
7483 "::": ":][:"
7484 },
7485 breaklimit: .7,
7486 breakoneoln: true,
7487 cancelkey: true,
7488 composerfont: { name: txt_ff, style: "italic", size: 14 },
7489 composerspace: 6,
7490 // contbarnb: false,
7491 decoerr: true,
7492 dynalign: true,
7493 footerfont: { name: txt_ff, size: 16 },
7494 fullsvg: '',
7495 gchordfont: { name: "text,sans-serif", size: 12 },
7496 gracespace: new Float32Array([6, 8, 11]), // left, inside, right
7497 graceslurs: true,
7498 headerfont: { name: txt_ff, size: 16 },
7499 historyfont: { name: txt_ff, size: 16 },
7500 hyphencont: true,
7501 indent: 0,
7502 infofont: {name: txt_ff, style: "italic", size: 14 },
7503 infoname: 'R "Rhythm: "\n\
7504 B "Book: "\n\
7505 S "Source: "\n\
7506 D "Discography: "\n\
7507 N "Notes: "\n\
7508 Z "Transcription: "\n\
7509 H "History: "',
7510 infospace: 0,
7511 keywarn: true,
7512 leftmargin: 1.4 * CM,
7513 lineskipfac: 1.1,
7514 linewarn: true,
7515 maxshrink: .65, // nice scores
7516 maxstaffsep: 2000,
7517 maxsysstaffsep: 2000,
7518 measrepnb: 1,
7519 measurefont: {name: txt_ff, style: "italic", size: 10},
7520 measurenb: -1,
7521 musicfont: {name: "music", src: musicfont, size: 24},
7522 musicspace: 6,
7523 // notespacingfactor: "1.3, 38",
7524 partsfont: {name: txt_ff, size: 15},
7525 parskipfac: .4,
7526 partsspace: 8,
7527 // pageheight: 29.7 * CM,
7528 pagewidth: 21 * CM,
7529 "propagate-accidentals": "o", // octave
7530 printmargin: 0,
7531 rightmargin: 1.4 * CM,
7532 rbmax: 4,
7533 rbmin: 2,
7534 repeatfont: {name: txt_ff, size: 9},
7535 scale: 1,
7536 slurheight: 1.0,
7537 spatab: // spacing table (see "notespacingfactor" and set_space())
7538 new Float32Array([ // default = "1.3, 38"
7539 10.2, 13.3, 17.3, 22.48, 29.2,
7540 38,
7541 49.4, 64.2, 83.5, 108.5]),
7542 staffsep: 46,
7543 stemheight: 21, // one octave
7544 stretchlast: .25,
7545 stretchstaff: true,
7546 subtitlefont: {name: txt_ff, size: 16},
7547 subtitlespace: 3,
7548 sysstaffsep: 34,
7549 systnames: -1, // (for compatibility)
7550 systvoices: 3,
7551 tempofont: {name: txt_ff, weight: "bold", size: 12},
7552 textfont: {name: txt_ff, size: 16},
7553 // textoption: undefined,
7554 textspace: 14,
7555 tieheight: 1.0,
7556 titlefont: {name: txt_ff, size: 20},
7557 // titleleft: false,
7558 titlespace: 6,
7559 titletrim: true,
7560 // transp: 0, // global transpose
7561 // topmargin: .7 * IN,
7562 topspace: 22,
7563 tuplets: [0, 0, 0, 0],
7564 tupletfont: {name: txt_ff, style: "italic", size: 10},
7565 vocalfont: {name: txt_ff, weight: "bold", size: 13},
7566 vocalspace: 10,
7567 voicefont: {name: txt_ff, weight: "bold", size: 13},
7568 // voicescale: 1,
7569 writefields: "CMOPQsTWw",
7570 wordsfont: {name: txt_ff, size: 16},
7571 wordsspace: 5,
7572 "writeout-accidentals": "n"
7573 }
7574
7575 // parameters that are used in the symbols
7576 var sfmt = {
7577 bardef: true,
7578 barsperstaff: true,
7579 beamslope: true,
7580 breaklimit: true,
7581 bstemdown: true,
7582 cancelkey: true,
7583 dynalign: true,
7584 flatbeams: true,
7585 gracespace: true,
7586 hyphencont: true,
7587 keywarn: true,
7588 maxshrink: true,
7589 maxstaffsep: true,
7590 measrepnb: true,
7591 rbmax: true,
7592 rbmin: true,
7593 shiftunison: true,
7594 slurheight: true,
7595 squarebreve: true,
7596 staffsep: true,
7597 systvoices: 1, //true
7598 stemheight: true,
7599 stretchlast: true,
7600 stretchstaff: true,
7601 tieheight: true,
7602 timewarn: true,
7603 trimsvg: 1, //true
7604 vocalspace: true
7605 } // sfmt
7606
7607 function get_bool(param) {
7608 return !param || !/^(0|n|f)/i.test(param) // accept void as true !
7609 }
7610
7611 // %%font <font> [<encoding>] [<scale>]
7612 function get_font_scale(param) {
7613 var i, font,
7614 a = info_split(param) // a[0] = font name
7615
7616 if (a.length <= 1)
7617 return
7618 var scale = +a[a.length - 1]
7619
7620 if (isNaN(scale) || scale <= 0.5) {
7621 syntax(1, "Bad scale value in %%font")
7622 return
7623 }
7624 font_scale_tb[a[0]] = scale
7625 }
7626
7627 // set the width factor of a font
7628 function set_font_fac(font) {
7629 var scale = font_scale_tb[font.fname || font.name]
7630
7631 if (!scale)
7632 scale = 1.1;
7633 font.swfac = font.size * scale
7634 }
7635
7636 // %%xxxfont fontname|* [encoding] [size|*]
7637 function param_set_font(xxxfont, p) {
7638 var font, n, a, ft2, k
7639
7640 // "setfont-<n>" goes to "u<n>font"
7641 if (xxxfont[xxxfont.length - 2] == '-') {
7642 n = xxxfont[xxxfont.length - 1]
7643 if (n < '1' || n > '9')
7644 return
7645 xxxfont = "u" + n + "font"
7646 }
7647
7648 // fill the values
7649 font = {}
7650 a = p.match(/\s+(no)?box(\s|$)/)
7651 if (a) { // if box
7652 if (a[1]) {
7653 font.box = false // nobox
7654 font.pad = 0
7655 } else {
7656 font.box = true
7657 font.pad = 2.5
7658 }
7659 p = p.replace(a[0], a[2])
7660 }
7661 a = p.match(/\s+padding=([\d.]+)(\s|$)/)
7662 if (a) { // if padding
7663 font.pad = a[1] ? +a[1] : 0
7664 p = p.replace(a[0], a[2])
7665 }
7666
7667 a = p.match(/\s+class=(.*?)(\s|$)/)
7668 if (a) {
7669 font.class = a[1];
7670 p = p.replace(a[0], a[2])
7671 }
7672 a = p.match(/\s+wadj=(.*?)(\s|$)/)
7673 if (a) {
7674 if (typeof document == "undefined") // useless if in browser
7675 switch (a[1]) {
7676 case 'none':
7677 font.wadj = ''
7678 break
7679 case 'space':
7680 font.wadj = 'spacing'
7681 break
7682 case 'glyph':
7683 font.wadj = 'spacingAndGlyphs'
7684 break
7685 default:
7686 syntax(1, errs.bad_val, "%%" + xxxfont)
7687 break
7688 }
7689 p = p.replace(a[0], a[2])
7690 }
7691
7692 // the font size is the last item
7693 a = p.match(/\s+([0-9.]+|\*)$/)
7694 if (a) {
7695 if (a[1] != "*")
7696 font.size = +a[1]
7697 p = p.replace(a[0], "")
7698 }
7699
7700 // accept local(..) and url(...) as the font source
7701 if ((p[0] == 'u' && p.slice(0, 4) == "url(")
7702 || (p[0] == 'l' && p.slice(0, 6) == "local(")) {
7703 n = p.indexOf(')', 1)
7704 if (n < 0) {
7705 syntax(1, "No end of url in font family")
7706 return
7707 }
7708
7709 font.src = p.slice(0, n + 1)
7710 font.fid = abc2svg.font_tb.length
7711 abc2svg.font_tb.push(font)
7712 font.name = 'ft' + font.fid
7713 p = p.replace(font.src, '')
7714 }
7715
7716 // extract the font attributes
7717 a = p.match(/[- ]?[nN]ormal/)
7718 if (a) {
7719 font.normal = true
7720 p = p.replace(a[0], '')
7721 }
7722
7723 // font weight
7724 a = p.match(abc2svg.ft_re)
7725 if (a) {
7726 font.weight = abc2svg.ft_w[a[0].replace(/[ -]/, '')
7727 .toLowerCase()]
7728 p = p.replace(a[0], '')
7729 }
7730
7731 a = p.match(/[- ]?[iI]talic/)
7732 if (a) {
7733 font.style = "italic"
7734 p = p.replace(a[0], '')
7735 }
7736 a = p.match(/[- ]?[oO]blique/)
7737 if (a) {
7738 font.style = "oblique"
7739 p = p.replace(a[0], '')
7740 }
7741
7742 if (!font.src) { // if no url(...)
7743
7744 // here is the font family
7745 p = p.trim()
7746
7747 if (p == '*')
7748 p = ''
7749 p = p.replace(/Times-Roman|Times/, "serif")
7750 .replace("Helvetica", "sans-serif")
7751 .replace("Courier", "monospace")
7752 .replace("music", cfmt.musicfont.name)
7753 //hack: the font "Figurato" is used for figured bass
7754 if (p.indexOf("Fig") > 0)
7755 font.figb = true
7756 }
7757 if (p && !font.name)
7758 font.name = p
7759
7760 if (font.size)
7761 set_font_fac(font)
7762
7763 // keep the previous attributes if no font name or no size
7764 if (!font.name || !font.size) {
7765 ft2 = cfmt[xxxfont]
7766 for (k in ft2) {
7767 if (!ft2.hasOwnProperty(k)
7768 || font[k] != undefined)
7769 continue
7770 switch (k) {
7771 case "fid":
7772 case "used":
7773 case "src":
7774 break
7775 case "style":
7776 case "weight":
7777 if (font.normal)
7778 break
7779 // fall thru
7780 default:
7781 font[k] = ft2[k]
7782 break
7783 }
7784 }
7785 if (!font.swfac)
7786 set_font_fac(font)
7787 }
7788 if (font.pad == undefined)
7789 font.pad = 0
7790 font.fname = font.name
7791 if (font.weight >= 700)
7792 font.fname += 'Bold'
7793
7794 cfmt[xxxfont] = font
7795 }
7796
7797 // get a length with a unit - return the number of pixels
7798 function get_unit(param) {
7799 var v = param.toLowerCase().match(/(-?[\d.]+)(.*)/)
7800 if (!v)
7801 return NaN
7802
7803 v[1] = +v[1]
7804 switch (v[2]) {
7805 case "cm":
7806 return v[1] * CM
7807 case "in":
7808 return v[1] * IN
7809 case "pt": // paper point in 1/72 inch
7810 return v[1] / .75
7811 case "px": // screen pixel in 1/96 inch
7812 case "":
7813 return v[1]
7814 }
7815 return NaN
7816 }
7817
7818 // set the name of an info or a part
7819 function set_infoname(cmd, param) {
7820 //fixme: check syntax: '<letter> ["string"]'
7821 var tmp = cfmt[cmd] ? cfmt[cmd].split("\n") : "",
7822 letter = param[0]
7823
7824 for (var i = 0; i < tmp.length; i++) {
7825 var infoname = tmp[i]
7826 if (infoname[0] != letter)
7827 continue
7828 if (param.length == 1)
7829 tmp.splice(i, 1)
7830 else
7831 tmp[i] = param
7832 cfmt[cmd] = tmp.join('\n')
7833 return
7834 }
7835 if (cfmt[cmd])
7836 cfmt[cmd] += "\n" + param
7837 else
7838 cfmt[cmd] = param
7839 }
7840
7841 // get the text option
7842 var textopt = {
7843 align: 'j',
7844 center: 'c',
7845 fill: 'f',
7846 justify: 'j',
7847 obeylines: 'l',
7848 ragged: 'f',
7849 right: 'r',
7850 skip: 's',
7851 // abcm2ps compatibility
7852 "0": 'l',
7853 "1": 'j',
7854 "2": 'f',
7855 "3": 'c',
7856 "4": 's',
7857 "5": 'r'
7858 }
7859 function get_textopt(v) {
7860 var i = v.indexOf(' ')
7861 if (i > 0)
7862 v = v.slice(0, i)
7863 return textopt[v]
7864 }
7865
7866 /* -- position of a voice element -- */
7867 var posval = {
7868 above: C.SL_ABOVE,
7869 auto: 0, // !! not C.SL_AUTO !!
7870 below: C.SL_BELOW,
7871 down: C.SL_BELOW,
7872 hidden: C.SL_HIDDEN,
7873 opposite: C.SL_HIDDEN,
7874 under: C.SL_BELOW,
7875 up: C.SL_ABOVE
7876 }
7877
7878 /* -- set the position of elements in a voice -- */
7879 function set_pos(k, v) { // keyword, value
7880 k = k.slice(0, 3)
7881 if (k == "ste")
7882 k = "stm"
7883 set_v_param("pos", '"' + k + ' ' + v + '"')
7884 }
7885
7886 // set/unset the fields to write
7887 function set_writefields(parm) {
7888 var c, i,
7889 a = parm.split(/\s+/)
7890
7891 if (get_bool(a[1])) {
7892 for (i = 0; i < a[0].length; i++) { // set
7893 c = a[0][i]
7894 if (cfmt.writefields.indexOf(c) < 0)
7895 cfmt.writefields += c
7896 }
7897 } else {
7898 for (i = 0; i < a[0].length; i++) { // unset
7899 c = a[0][i]
7900 if (cfmt.writefields.indexOf(c) >= 0)
7901 cfmt.writefields = cfmt.writefields.replace(c, '')
7902 }
7903 }
7904 }
7905
7906 // set a voice specific parameter
7907 function set_v_param(k, v) {
7908 k = [k + '=', v]
7909 if (parse.state < 3)
7910 memo_kv_parm(curvoice ? curvoice.id : '*', k)
7911 else if (curvoice)
7912 set_kv_parm(k)
7913 else
7914 memo_kv_parm('*', k)
7915 }
7916
7917 function set_page() {
7918 if (!img.chg)
7919 return
7920 img.chg = false;
7921 img.lm = cfmt.leftmargin - cfmt.printmargin
7922 if (img.lm < 0)
7923 img.lm = 0;
7924 img.rm = cfmt.rightmargin - cfmt.printmargin
7925 if (img.rm < 0)
7926 img.rm = 0;
7927 img.width = cfmt.pagewidth - 2 * cfmt.printmargin
7928
7929 // must have 100pt at least as the staff width
7930 if (img.width - img.lm - img.rm < 100) {
7931 error(0, undefined, "Bad staff width");
7932 img.width = img.lm + img.rm + 150
7933 }
7934 set_posx()
7935 } // set_page()
7936
7937 // set a format parameter
7938 // (possible hook)
7939 Abc.prototype.set_format = function(cmd, param) {
7940 var f, f2, v, i
7941
7942 //fixme: should check the type and limits of the parameter values
7943 if (/.+font(-[\d])?$/.test(cmd)) {
7944 if (cmd == "soundfont")
7945 cfmt.soundfont = param
7946 else
7947 param_set_font(cmd, param)
7948 return
7949 }
7950
7951 // duplicate the global parameters if already used by symbols
7952 if (sfmt[cmd] && parse.ufmt)
7953 cfmt = Object.create(cfmt)
7954
7955 switch (cmd) {
7956 case "aligncomposer":
7957 case "barsperstaff":
7958 case "infoline":
7959 case "measurenb":
7960 case "rbmax":
7961 case "rbmin":
7962 case "measrepnb":
7963 case "shiftunison":
7964 case "systnames":
7965 case "systvoices":
7966 v = parseInt(param)
7967 if (isNaN(v)) {
7968 syntax(1, "Bad integer value");
7969 break
7970 }
7971 if (cmd == "systnames") { // compatibility
7972 switch (v) {
7973 case -1: v = 3; break
7974 case 1: v = 2; break
7975 case 2: v = 1; break
7976 }
7977 cmd = "systvoices"
7978 }
7979 cfmt[cmd] = v
7980 break
7981 case "abc-version":
7982 case "bgcolor":
7983 case "fgcolor":
7984 case "propagate-accidentals":
7985 case "writeout-accidentals":
7986 cfmt[cmd] = param
7987 break
7988 case "beamslope":
7989 case "breaklimit": // float values
7990 case "lineskipfac":
7991 case "maxshrink":
7992 case "pagescale":
7993 case "parskipfac":
7994 case "scale":
7995 case "slurheight":
7996 case "stemheight":
7997 case "tieheight":
7998 f = +param
7999 if (isNaN(f) || !param || f < 0) {
8000 syntax(1, errs.bad_val, '%%' + cmd)
8001 break
8002 }
8003 switch (cmd) {
8004 case "scale": // old scale
8005 f /= .75
8006 case "pagescale":
8007 if (f < .1)
8008 f = .1 // smallest scale
8009 cmd = "scale";
8010 img.chg = true
8011 break
8012 }
8013 cfmt[cmd] = f
8014 break
8015 case "annotationbox":
8016 case "gchordbox":
8017 case "measurebox":
8018 case "partsbox":
8019 param_set_font(cmd.replace("box", "font"), // font
8020 "* * " + (get_bool(param) ? "box" : "nobox"))
8021 break
8022 case "altchord":
8023 case "bstemdown":
8024 case "breakoneoln":
8025 case "cancelkey":
8026 case "checkbars":
8027 case "contbarnb":
8028 case "custos":
8029 case "decoerr":
8030 case "flatbeams":
8031 case "graceslurs":
8032 case "graceword":
8033 case "hyphencont":
8034 case "keywarn":
8035 case "linewarn":
8036 case "squarebreve":
8037 case "splittune":
8038 case "straightflags":
8039 case "stretchstaff":
8040 case "timewarn":
8041 case "titlecaps":
8042 case "titleleft":
8043 case "trimsvg":
8044 cfmt[cmd] = get_bool(param)
8045 break
8046 case "dblrepbar":
8047 param = ":: " + param
8048 // fall thru
8049 case "bardef": // %%bardef oldbar newbar
8050 v = param.split(/\s+/)
8051 if (v.length != 2) {
8052 syntax(1, errs.bad_val, "%%bardef")
8053 } else {
8054 if (parse.ufmt)
8055 cfmt.bardef = Object.create(cfmt.bardef) // new object
8056 cfmt.bardef[v[0]] = v[1]
8057 }
8058 break
8059 case "chordalias":
8060 v = param.split(/\s+/)
8061 if (!v.length)
8062 syntax(1, errs.bad_val, "%%chordalias")
8063 else
8064 abc2svg.ch_alias[v[0]] = v[1] || ""
8065 break
8066 case "composerspace":
8067 case "indent":
8068 case "infospace":
8069 case "maxstaffsep":
8070 case "maxsysstaffsep":
8071 case "musicspace":
8072 case "partsspace":
8073 case "staffsep":
8074 case "subtitlespace":
8075 case "sysstaffsep":
8076 case "textspace":
8077 case "titlespace":
8078 case "topspace":
8079 case "vocalspace":
8080 case "wordsspace":
8081 f = get_unit(param) // normally, unit in points - 72 DPI accepted
8082 if (isNaN(f) || f < 0)
8083 syntax(1, errs.bad_val, '%%' + cmd)
8084 else
8085 cfmt[cmd] = f
8086 break
8087 case "page-format":
8088 user.page_format = get_bool(param)
8089 break
8090 case "print-leftmargin": // to remove
8091 syntax(0, "$1 is deprecated - use %%printmargin instead", '%%' + cmd)
8092 cmd = "printmargin"
8093 // fall thru
8094 case "printmargin":
8095 // case "botmargin":
8096 case "leftmargin":
8097 // case "pageheight":
8098 case "pagewidth":
8099 case "rightmargin":
8100 // case "topmargin":
8101 f = get_unit(param) // normally unit in cm or in - 96 DPI
8102 if (isNaN(f)) {
8103 syntax(1, errs.bad_val, '%%' + cmd)
8104 break
8105 }
8106 cfmt[cmd] = f;
8107 img.chg = true
8108 break
8109 case "concert-score":
8110 if (cfmt.sound != "play")
8111 cfmt.sound = "concert"
8112 break
8113 case "writefields":
8114 set_writefields(param)
8115 break
8116 case "volume":
8117 cmd = "dynamic"
8118 // fall thru
8119 case "dynamic":
8120 case "gchord":
8121 case "gstemdir":
8122 case "ornament":
8123 case "stemdir":
8124 case "vocal":
8125 set_pos(cmd, param)
8126 break
8127 case "font":
8128 get_font_scale(param)
8129 break
8130 case "fullsvg":
8131 if (parse.state != 0) {
8132 syntax(1, errs.not_in_tune, "%%fullsvg")
8133 break
8134 }
8135 //fixme: should check only alpha, num and '_' characters
8136 cfmt[cmd] = param
8137 break
8138 case "gracespace":
8139 v = param.split(/\s+/)
8140 for (i = 0; i < 3; i++)
8141 if (isNaN(+v[i])) {
8142 syntax(1, errs.bad_val, "%%gracespace")
8143 break
8144 }
8145 if (parse.ufmt)
8146 cfmt[cmd] = new Float32Array(3)
8147 for (i = 0; i < 3; i++)
8148 cfmt[cmd][i] = +v[i]
8149 break
8150 case "tuplets":
8151 v = param.split(/\s+/)
8152 f = v[3]
8153 if (f) // if 'where'
8154 f = posval[f] // translate the keyword
8155 if (f)
8156 v[3] = f
8157 if (curvoice)
8158 curvoice.tup = v
8159 else
8160 cfmt[cmd] = v
8161 break
8162 case "infoname":
8163 case "partname":
8164 set_infoname(cmd, param)
8165 break
8166 case "notespacingfactor":
8167 v = param.match(/([.\d]+)[,\s]*(\d+)?/)
8168 if (v) {
8169 f = +v[1]
8170 if (isNaN(f) || f < 1 || f > 2) {
8171 f = 0
8172 } else if (v[2]) {
8173 f2 = +v[2]
8174 if (isNaN(f))
8175 f = 0
8176 } else {
8177 f2 = cfmt.spatab[5]
8178 }
8179 }
8180 if (!f) {
8181 syntax(1, errs.bad_val, "%%" + cmd)
8182 break
8183 }
8184 cfmt[cmd] = param // (for dump)
8185
8186 // in the table 'spatab',
8187 // the width of notes is indexed by log2(note_length)
8188 cfmt.spatab = new Float32Array(10)
8189 i = 5; // index of crotchet
8190 do {
8191 cfmt.spatab[i] = f2
8192 f2 /= f
8193 } while (--i >= 0)
8194 i = 5;
8195 f2 = cfmt.spatab[i]
8196 for ( ; ++i < cfmt.spatab.length; ) {
8197 f2 *= f;
8198 cfmt.spatab[i] = f2
8199 }
8200 break
8201 case "play":
8202 cfmt.sound = "play" // without clef
8203 break
8204 case "pos":
8205 cmd = param.match(/(\w*)\s+(.*)/)
8206 if (!cmd || !cmd[2]) {
8207 syntax(1, "Error in %%pos")
8208 break
8209 }
8210 if (cmd[1].slice(0, 3) == 'tup' // special case for %%pos tuplet
8211 && curvoice) { // inside tune
8212 if (!curvoice.tup)
8213 curvoice.tup = cfmt.tuplets
8214 else
8215 curvoice.tup = Object.create(curvoice.tup)
8216 v = posval[cmd[2]]
8217 switch (v) {
8218 case C.SL_ABOVE:
8219 curvoice.tup[3] = 1
8220 break
8221 case C.SL_BELOW:
8222 curvoice.tup[3] = 2
8223 break
8224 case C.SL_HIDDEN:
8225 curvoice.tup[2] = 1
8226 break
8227 }
8228 break
8229 }
8230 if (cmd[1].slice(0, 3) == "vol")
8231 cmd[1] = "dyn" // compatibility
8232 set_pos(cmd[1], cmd[2])
8233 break
8234 case "sounding-score":
8235 if (cfmt.sound != "play")
8236 cfmt.sound = "sounding"
8237 break
8238 case "staffwidth":
8239 v = get_unit(param)
8240 if (isNaN(v)) {
8241 syntax(1, errs.bad_val, '%%' + cmd)
8242 break
8243 }
8244 if (v < 100) {
8245 syntax(1, "%%staffwidth too small")
8246 break
8247 }
8248 v = cfmt.pagewidth - v - cfmt.leftmargin
8249 if (v < 2) {
8250 syntax(1, "%%staffwidth too big")
8251 break
8252 }
8253 cfmt.rightmargin = v;
8254 img.chg = true
8255 break
8256 case "textoption":
8257 cfmt[cmd] = get_textopt(param)
8258 break
8259 case "dynalign":
8260 case "quiet":
8261 case "singleline":
8262 case "stretchlast":
8263 case "titletrim":
8264 v = param == '' ? 1 : +param
8265 if (isNaN(v))
8266 v = +get_bool(param)
8267 if (cmd[1] == 't') { // stretchlast
8268 if (v < 0 || v > 1) {
8269 syntax(1, errs.bad_val, '%%' + cmd)
8270 break
8271 }
8272 }
8273 cfmt[cmd] = v
8274 break
8275 case "combinevoices":
8276 syntax(1, "%%combinevoices is deprecated - use %%voicecombine instead")
8277 break
8278 case "voicemap":
8279 set_v_param("map", param)
8280 break
8281 case "voicescale":
8282 set_v_param("scale", param)
8283 break
8284 case "unsizedsvg":
8285 if (get_bool(param))
8286 user.imagesize = ""
8287 else
8288 delete user.imagesize
8289 break
8290 // deprecated
8291 case "rbdbstop":
8292 v = get_bool(param)
8293 if (v && cfmt["abc-version"] >= "2.2")
8294 cfmt["abc-version"] = "1"
8295 else if (!v && cfmt["abc-version"] < "2.2")
8296 cfmt["abc-version"] = "2.2"
8297 break
8298 default: // memorize all global commands
8299 if (!parse.state) // (needed for modules)
8300 cfmt[cmd] = param
8301 break
8302 }
8303
8304 // check if already a same format
8305 if (sfmt[cmd] && parse.ufmt) {
8306 // to do...
8307 parse.ufmt = false
8308 }
8309 }
8310
8311 // font stuff
8312
8313 // build a font style
8314 function st_font(font) {
8315 var n = font.name,
8316 r = ""
8317
8318 if (font.weight)
8319 r += font.weight + " "
8320 if (font.style)
8321 r += font.style + " "
8322 if (n.indexOf('"') < 0 && n.indexOf(' ') > 0)
8323 n = '"' + n + '"'
8324 return r + font.size.toFixed(1) + 'px ' + n
8325 }
8326 function style_font(font) {
8327 return 'font:' + st_font(font)
8328 }
8329 Abc.prototype.style_font = style_font
8330
8331 // build a font class
8332 function font_class(font) {
8333 var f = 'f' + font.fid + cfmt.fullsvg
8334 if (font.class)
8335 f += ' ' + font.class
8336 if (font.box)
8337 f += ' ' + 'box'
8338 return f
8339 }
8340
8341 // use the font
8342 function use_font(font) {
8343 if (!font.used) {
8344 font.used = true;
8345 if (font.fid == undefined) { // if default font
8346 font.fid = abc2svg.font_tb.length
8347 abc2svg.font_tb.push(font)
8348 if (!font.swfac)
8349 set_font_fac(font)
8350 if (!font.pad)
8351 font.pad = 0
8352 }
8353
8354 // set the pointer to the width of the characters
8355 if (!font.cw_tb) {
8356 font.cw_tb = !font.name ? ssw_tb
8357 : font.name.indexOf("ans") > 0
8358 ? ssw_tb // sans-serif
8359 : font.name.indexOf("ono") > 0
8360 ? mw_tb // monospace
8361 : sw_tb // serif
8362 }
8363 add_fstyle(".f" + font.fid +
8364 (cfmt.fullsvg || "") +
8365 "{" + style_font(font) + "}")
8366 if (font.src)
8367 add_fstyle("@font-face{\n\
8368 font-family:" + font.name + ";\n\
8369 src:" + font.src + "}")
8370 if (font == cfmt.musicfont) // add more music font style
8371 add_fstyle(".f" + font.fid
8372 + (cfmt.fullsvg || "")
8373 + ' text,tspan{white-space:pre}')
8374 if (ff.text && !ff.used && font.name.indexOf("text") >= 0) {
8375 font_style += ff.text // add font-face's from %%beginsvg
8376 ff.used = 1 //true
8377 }
8378 }
8379 }
8380
8381 // get the font of the 'xxxfont' parameter
8382 function get_font(fn) {
8383 var font, font2, fid, st
8384
8385 fn += "font"
8386 font = cfmt[fn]
8387 if (!font) {
8388 syntax(1, "Unknown font $1", fn)
8389 return gene.curfont
8390 }
8391
8392 if (!font.name || !font.size) { // if incomplete user font
8393 font2 = Object.create(gene.deffont)
8394 if (font.name)
8395 font2.name = font.name
8396 if (font.normal) {
8397 if (font2.weight) // !! don't use delete !!
8398 font2.weight = null
8399 if (font2.style)
8400 font2.style = null
8401 }
8402 if (font.weight)
8403 font2.weight = font.weight
8404 if (font.style)
8405 font2.style = font.style
8406 if (font.src)
8407 font2.src = font.src
8408 if (font.size)
8409 font2.size = font.size
8410 st = st_font(font2)
8411 if (font.class) {
8412 font2.class = font.class
8413 st += ' '+ font.class
8414 }
8415 fid = abc2svg.font_st[st]
8416 if (fid != undefined)
8417 return abc2svg.font_tb[fid]
8418 abc2svg.font_st[st] = abc2svg.font_tb.length // will be the font id
8419 font2.fid = font2.used = undefined
8420 font = font2
8421 }
8422 use_font(font)
8423 return font
8424 }
8425 // abc2svg - front.js - ABC parsing front-end
8426 //
8427 // Copyright (C) 2014-2025 Jean-Francois Moine
8428 //
8429 // This file is part of abc2svg-core.
8430 //
8431 // abc2svg-core is free software: you can redistribute it and/or modify
8432 // it under the terms of the GNU Lesser General Public License as published by
8433 // the Free Software Foundation, either version 3 of the License, or
8434 // (at your option) any later version.
8435 //
8436 // abc2svg-core is distributed in the hope that it will be useful,
8437 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8438 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8439 // GNU Lesser General Public License for more details.
8440 //
8441 // You should have received a copy of the GNU Lesser General Public License
8442 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
8443
8444 var sav = {}, // save global (between tunes) definitions
8445 mac = {}, // macros (m:)
8446 maci = {}, // first letters of macros
8447 modone = {} // hooks done by module
8448
8449 // translation table from the ABC draft version 2.2
8450 var abc_utf = {
8451 "=D": "Đ",
8452 "=H": "Ħ",
8453 "=T": "Ŧ",
8454 "=d": "đ",
8455 "=h": "ħ",
8456 "=t": "ŧ",
8457 "/O": "Ø",
8458 "/o": "ø",
8459 // "/D": "Đ",
8460 // "/d": "đ",
8461 "/L": "Ł",
8462 "/l": "ł",
8463 "vL": "Ľ",
8464 "vl": "ľ",
8465 "vd": "ď",
8466 ".i": "ı",
8467 "AA": "Å",
8468 "aa": "å",
8469 "AE": "Æ",
8470 "ae": "æ",
8471 "DH": "Ð",
8472 "dh": "ð",
8473 // "ng": "ŋ",
8474 "OE": "Œ",
8475 "oe": "œ",
8476 "ss": "ß",
8477 "TH": "Þ",
8478 "th": "þ"
8479 }
8480
8481 // accidentals as octal values (abcm2ps compatibility)
8482 var oct_acc = {
8483 "1": "\u266f",
8484 "2": "\u266d",
8485 "3": "\u266e",
8486 "4": "&#x1d12a;",
8487 "5": "&#x1d12b;"
8488 }
8489
8490 // convert the escape sequences to utf-8
8491 function cnv_escape(src, flag) {
8492 var c, c2,
8493 dst = "",
8494 i, j = 0
8495
8496 while (1) {
8497 i = src.indexOf('\\', j)
8498 if (i < 0)
8499 break
8500 dst += src.slice(j, i);
8501 c = src[++i]
8502 if (!c)
8503 return dst + '\\'
8504 switch (c) {
8505 case '0':
8506 case '2':
8507 if (src[i + 1] != '0')
8508 break
8509 c2 = oct_acc[src[i + 2]]
8510 if (c2) {
8511 dst += c2;
8512 j = i + 3
8513 continue
8514 }
8515 break
8516 case 'u':
8517 j = Number("0x" + src.slice(i + 1, i + 5));
8518 if (isNaN(j) || j < 0x20) {
8519 dst += src[++i] + "\u0306" // breve accent
8520 j = i + 1
8521 continue
8522 }
8523 c = String.fromCharCode(j)
8524 if (c == '\\') {
8525 i += 4
8526 break
8527 }
8528 dst += c
8529 j = i + 5
8530 continue
8531 case 't': // TAB
8532 dst += '\t';
8533 j = i + 1
8534 continue
8535 case 'n': // new line (voice name)
8536 dst += '\n';
8537 j = i + 1
8538 continue
8539 default:
8540 c2 = abc_utf[src.slice(i, i + 2)]
8541 if (c2) {
8542 dst += c2;
8543 j = i + 2
8544 continue
8545 }
8546
8547 // try unicode combine characters
8548 c2 = src[i + 1]
8549 if (!c2)
8550 break // !! the next test is true if c2 is undefined !!
8551 if (!/[A-Za-z]/.test(c2))
8552 break
8553 switch (c) {
8554 case '`':
8555 dst += c2 + "\u0300" // grave
8556 j = i + 2
8557 continue
8558 case "'":
8559 dst += c2 + "\u0301" // acute
8560 j = i + 2
8561 continue
8562 case '^':
8563 dst += c2 + "\u0302" // circumflex
8564 j = i + 2
8565 continue
8566 case '~':
8567 dst += c2 + "\u0303" // tilde
8568 j = i + 2
8569 continue
8570 case '=':
8571 dst += c2 + "\u0304" // macron
8572 j = i + 2
8573 continue
8574 case '_':
8575 dst += c2 + "\u0305" // overline
8576 j = i + 2
8577 continue
8578 case '.':
8579 dst += c2 + "\u0307" // dot
8580 j = i + 2
8581 continue
8582 case '"':
8583 dst += c2 + "\u0308" // dieresis
8584 j = i + 2
8585 continue
8586 case 'o':
8587 dst += c2 + "\u030a" // ring
8588 j = i + 2
8589 continue
8590 case 'H':
8591 dst += c2 + "\u030b" // hungarumlaut
8592 j = i + 2
8593 continue
8594 case 'v':
8595 dst += c2 + "\u030c" // caron
8596 j = i + 2
8597 continue
8598 // case ',':
8599 // dst += c2 + "\u0326" // comma below
8600 // j = i + 2
8601 // continue
8602 case 'c':
8603 dst += c2 + "\u0327" // cedilla
8604 j = i + 2
8605 continue
8606 case ';':
8607 dst += c2 + "\u0328" // ogonek
8608 j = i + 2
8609 continue
8610 }
8611 break
8612 }
8613 if (flag == 'w') // if lyrics line (w:)
8614 dst += '\\' // keep the backslash
8615 dst += c
8616 j = i + 1
8617 }
8618 return dst + src.slice(j)
8619 }
8620
8621 // ABC include
8622 var include = 0
8623
8624 function do_include(fn) {
8625 var file, parse_sav
8626
8627 if (!user.read_file) {
8628 syntax(1, "No read_file support")
8629 return
8630 }
8631 if (include > 2) {
8632 syntax(1, "Too many include levels")
8633 return
8634 }
8635 file = user.read_file(fn)
8636 if (!file) {
8637 syntax(1, "Cannot read file '$1'", fn)
8638 return
8639 }
8640 include++;
8641 parse_sav = clone(parse);
8642 tosvg(fn, file);
8643 parse_sav.state = parse.state;
8644 parse_sav.ckey = parse.ckey
8645 parse = parse_sav;
8646 include--
8647 }
8648
8649 // parse ABC code
8650 function tosvg(in_fname, // file name
8651 file, // file content
8652 bol, eof) { // beginning/end of file
8653 var i, c, eol, end,
8654 select,
8655 line0, line1,
8656 last_info, opt, text, a, b, s,
8657 pscom,
8658 txt_add = '\n' // for "+:"
8659
8660 // check if a tune is selected
8661 function tune_selected() {
8662 var re, res,
8663 i = file.indexOf('K:', bol)
8664
8665 if (i < 0) {
8666 // syntax(1, "No K: in tune")
8667 return false
8668 }
8669 i = file.indexOf('\n', i)
8670 if (parse.select.test(file.slice(parse.bol, i)))
8671 return true
8672 re = /\n\w*\n/;
8673 re.lastIndex = i;
8674 res = re.exec(file)
8675 if (res)
8676 eol = re.lastIndex
8677 else
8678 eol = eof
8679 return false
8680 } // tune_selected()
8681
8682 // remove the comment at end of text
8683 // if flag, handle the escape sequences
8684 // if flag is 'w' (lyrics), keep the '\'s
8685 function uncomment(src, flag) {
8686 if (!src)
8687 return src
8688 var i = src.indexOf('%')
8689 if (i == 0)
8690 return ''
8691 if (i > 0)
8692 src = src.replace(/([^\\])%.*/, '$1')
8693 .replace(/\\%/g, '%');
8694 src = src.replace(/\s+$/, '')
8695 if (flag && src.indexOf('\\') >= 0)
8696 return cnv_escape(src, flag)
8697 return src
8698 } // uncomment()
8699
8700 // set the sequence showing the source and save it in sav.src
8701 function set_src(stag, se) {
8702 var r, t,
8703 etag = ""
8704
8705 if (!se)
8706 se = file.indexOf('\n\n', bol) // end of tune
8707 if (se < 0)
8708 se = eof
8709 if (typeof stag != "object") { // set the tag after source
8710 if (stag[0] != 'b' && stag[0] != 'a' && stag[0] != '+'
8711 && stag[0] != '*')
8712 stag = 'b' + stag // default: source before
8713 if (stag[1] != '<') // (if bool)
8714 stag = stag[0] + "<pre>"
8715 r = stag.match(/<\/?[^>]*>/g)
8716 while (1) {
8717 t = r.pop()
8718 if (!t)
8719 break
8720 if (t[1] == '/' || t.slice(-2) == '/>')
8721 r.pop() // skip this stop/start tag
8722 else
8723 etag += '</' + t.slice(1)
8724 }
8725 cfmt.show_source = stag = [stag, etag]
8726 }
8727 t = stag[0].slice(1)
8728 + clean_txt(file.slice(bol, se))
8729 + stag[1]
8730 if (stag[0][0] == '+' && sav.src)
8731 sav.src += t
8732 else
8733 sav.src = t
8734 } // set_src()
8735
8736 function end_tune() {
8737 parse.bol = bol // (for multi V:)
8738 generate()
8739 cfmt = sav.cfmt;
8740 info = sav.info;
8741 char_tb = sav.char_tb;
8742 glovar = sav.glovar;
8743 maps = sav.maps;
8744 mac = sav.mac;
8745 maci = sav.maci;
8746 parse.tune_v_opts = null;
8747 parse.scores = null;
8748 parse.ufmt = false
8749 delete parse.pq
8750 init_tune()
8751 img.chg = true;
8752 set_page();
8753 if (cfmt.show_source) {
8754 user.img_out("</div>")
8755 if (cfmt.show_source[0][0] == 'a')
8756 user.img_out(sav.src)
8757 }
8758 } // end_tune()
8759
8760 // get %%voice
8761 function do_voice(select, in_tune) {
8762 var opt, bol
8763 if (select == "end")
8764 return // end of previous %%voice
8765
8766 // get the options
8767 if (in_tune) {
8768 if (!parse.tune_v_opts)
8769 parse.tune_v_opts = {};
8770 opt = parse.tune_v_opts
8771 } else {
8772 if (!parse.voice_opts)
8773 parse.voice_opts = {};
8774 opt = parse.voice_opts
8775 }
8776 opt[select] = []
8777 while (1) {
8778 bol = ++eol
8779 if (file[bol] != '%')
8780 break
8781 eol = file.indexOf('\n', eol);
8782 if (file[bol + 1] != line1)
8783 continue
8784 bol += 2
8785 if (eol < 0)
8786 text = file.slice(bol)
8787 else
8788 text = file.slice(bol, eol);
8789 a = text.match(/\S+/)
8790 switch (a[0]) {
8791 default:
8792 opt[select].push(uncomment(text, true))
8793 continue
8794 case "score":
8795 case "staves":
8796 case "tune":
8797 case "voice":
8798 bol -= 2
8799 break
8800 }
8801 break
8802 }
8803 eol = parse.eol = bol - 1
8804 } // do_voice()
8805
8806 // apply the options to the current tune
8807 function tune_filter() {
8808 var o, opts, j, pc, h,
8809 i = file.indexOf('K:', bol)
8810
8811 i = file.indexOf('\n', i);
8812 h = file.slice(parse.bol, i) // tune header
8813
8814 for (i in parse.tune_opts) {
8815 if (!parse.tune_opts.hasOwnProperty(i))
8816 continue
8817 if (!(new RegExp(i)).test(h))
8818 continue
8819 opts = parse.tune_opts[i]
8820 for (j = 0; j < opts.t_opts.length; j++) {
8821 pc = opts.t_opts[j]
8822 switch (pc.match(/\S+/)[0]) {
8823 case "score":
8824 case "staves":
8825 if (!parse.scores)
8826 parse.scores = [];
8827 parse.scores.push(pc)
8828 break
8829 default:
8830 self.do_pscom(pc)
8831 break
8832 }
8833 }
8834 opts = opts.v_opts
8835 if (!opts)
8836 continue
8837 for (j in opts) {
8838 if (!opts.hasOwnProperty(j))
8839 continue
8840 if (!parse.tune_v_opts)
8841 parse.tune_v_opts = {};
8842 if (!parse.tune_v_opts[j])
8843 parse.tune_v_opts[j] = opts[j]
8844 else
8845 parse.tune_v_opts[j] =
8846 parse.tune_v_opts[j].
8847 concat(opts[j])
8848 }
8849 }
8850 } // tune_filter()
8851
8852 // export functions and/or set module hooks
8853 if (abc2svg.mhooks) {
8854 for (i in abc2svg.mhooks) {
8855 if (!modone[i]) {
8856 modone[i] = 1 //true
8857 abc2svg.mhooks[i](self)
8858 }
8859 }
8860 }
8861
8862 // initialize
8863 parse.file = file; // used for errors
8864 parse.fname = in_fname
8865
8866 // scan the file
8867 if (bol == undefined)
8868 bol = 0
8869 if (!eof)
8870 eof = file.length
8871 if (file.slice(bol, bol + 5) == "%abc-")
8872 cfmt["abc-version"] = /[1-9.]+/.exec(file.slice(bol + 5, bol + 10))
8873 for ( ; bol < eof; bol = parse.eol + 1) {
8874 eol = file.indexOf('\n', bol) // get a line
8875 if (eol < 0 || eol > eof)
8876 eol = eof;
8877 parse.eol = eol
8878
8879 // remove the ending white spaces
8880 while (1) {
8881 eol--
8882 switch (file[eol]) {
8883 case ' ':
8884 case '\t':
8885 continue
8886 }
8887 break
8888 }
8889 eol++
8890 if (eol == bol) { // empty line
8891 if (parse.state == 1) {
8892 parse.istart = bol;
8893 syntax(1, "Empty line in tune header - ignored")
8894 } else if (parse.state >= 2) {
8895 end_tune()
8896 parse.state = 0
8897 if (parse.select) { // skip to next tune
8898 eol = file.indexOf('\nX:', parse.eol)
8899 if (eol < 0)
8900 eol = eof
8901 parse.eol = eol
8902 }
8903 }
8904 continue
8905 }
8906 parse.istart = parse.bol = bol;
8907 parse.iend = eol;
8908 parse.line.index = 0;
8909
8910 // check if the line is a pseudo-comment or I:
8911 line0 = file[bol];
8912 line1 = file[bol + 1]
8913 if ((line0 == 'I' && line1 == ':')
8914 || line0 == '%') {
8915 if (line0 == '%' && parse.prefix.indexOf(line1) < 0)
8916 continue // comment
8917
8918 // change "%%abc xxxx" to "xxxx"
8919 if (file[bol + 2] == 'a'
8920 && file[bol + 3] == 'b'
8921 && file[bol + 4] == 'c'
8922 && file[bol + 5] == ' ') {
8923 bol += 6;
8924 line0 = file[bol];
8925 line1 = file[bol + 1]
8926 } else {
8927 pscom = true
8928 }
8929 }
8930
8931 // pseudo-comments
8932 if (pscom) {
8933 pscom = false;
8934 bol += 2 // skip %%/I:
8935 text = file.slice(bol, eol)
8936 a = text.match(/([^\s]+)\s*(.*)/)
8937 if (!a || a[1][0] == '%')
8938 continue
8939 switch (a[1]) {
8940 case "abcm2ps":
8941 case "ss-pref":
8942 parse.prefix = a[2] // may contain a '%'
8943 continue
8944 case "abc-include":
8945 do_include(uncomment(a[2]))
8946 continue
8947 }
8948
8949 // beginxxx/endxxx
8950 if (a[1].slice(0, 5) == 'begin') {
8951 b = a[1].substr(5);
8952 end = '\n' + line0 + line1 + "end" + b;
8953 i = file.indexOf(end, eol)
8954 if (i < 0) {
8955 syntax(1, "No $1 after %%$2",
8956 end.slice(1), a[1]);
8957 parse.eol = eof
8958 continue
8959 }
8960 self.do_begin_end(b, uncomment(a[2]),
8961 file.slice(eol + 1, i)
8962 .replace(/\n%[^%].*$/gm,'')
8963 .replace(/^%%/gm,''))
8964 parse.eol = file.indexOf('\n', i + 6)
8965 if (parse.eol < 0)
8966 parse.eol = eof
8967 continue
8968 }
8969 switch (a[1]) {
8970 case "show_source":
8971 b = uncomment(a[2])
8972 switch (b[0]) {
8973 case '*':
8974 i = file.indexOf('\n' + line0 + line1
8975 + "show_source", eol)
8976 bol -= 2 // keep %%show_.. in the source
8977 set_src(b, i)
8978 user.img_out(sav.src)
8979 // fall thru
8980 case '0':
8981 b = ""
8982 // fall thru
8983 default:
8984 cfmt[a[1]] = b
8985 // fall thru
8986 }
8987 continue
8988 case "select":
8989 if (parse.state != 0) {
8990 syntax(1, errs.not_in_tune, "%%select")
8991 continue
8992 }
8993 select = uncomment(a[2])
8994 if (select[0] == '"')
8995 select = select.slice(1, -1);
8996 if (!select) {
8997 delete parse.select
8998 continue
8999 }
9000 select = select.replace(/\(/g, '\\(');
9001 select = select.replace(/\)/g, '\\)');
9002 // select = select.replace(/\|/g, '\\|');
9003 parse.select = new RegExp(select, 'm')
9004 continue
9005 case "tune":
9006 if (parse.state != 0) {
9007 syntax(1, errs.not_in_tune, "%%tune")
9008 continue
9009 }
9010 select = uncomment(a[2])
9011
9012 // if void %%tune, free all tune options
9013 if (!select) {
9014 parse.tune_opts = {}
9015 continue
9016 }
9017
9018 if (select == "end")
9019 continue // end of previous %%tune
9020
9021 if (!parse.tune_opts)
9022 parse.tune_opts = {};
9023 parse.tune_opts[select] = opt = {
9024 t_opts: []
9025 // v_opts: {}
9026 };
9027 while (1) {
9028 bol = eol
9029 if (file[bol + 1] != '%')
9030 break
9031 eol = file.indexOf('\n', eol + 1)
9032 if (file[bol + 2] != line1)
9033 continue
9034 text = file.slice(bol + 3,
9035 eol < 0 ? undefined : eol)
9036 a = text.match(/([^\s]+)\s*(.*)/)
9037 switch (a[1]) {
9038 case "tune":
9039 break
9040 case "voice":
9041 do_voice(uncomment(a[2],
9042 true), true)
9043 continue
9044 default:
9045 opt.t_opts.push(
9046 uncomment(text, true))
9047 continue
9048 }
9049 break
9050 }
9051 if (parse.tune_v_opts) {
9052 opt.v_opts = parse.tune_v_opts;
9053 parse.tune_v_opts = null
9054 }
9055 parse.eol = bol
9056 continue
9057 case "voice":
9058 if (parse.state != 0) {
9059 syntax(1, errs.not_in_tune, "%%voice")
9060 continue
9061 }
9062 select = uncomment(a[2])
9063
9064 /* if void %%voice, free all voice options */
9065 if (!select) {
9066 parse.voice_opts = null
9067 continue
9068 }
9069
9070 do_voice(select)
9071 continue
9072 }
9073 self.do_pscom(uncomment(text, true))
9074 continue
9075 }
9076
9077 // music line (or free text)
9078 if (line1 != ':' || !/[A-Za-z+]/.test(line0)) {
9079 last_info = undefined;
9080 if (parse.state < 2)
9081 continue
9082 parse.line.buffer = uncomment(file.slice(bol, eol))
9083 if (parse.line.buffer)
9084 parse_music_line()
9085 continue
9086 }
9087
9088 // information fields
9089 bol += 2
9090 while (1) {
9091 switch (file[bol]) {
9092 case ' ':
9093 case '\t':
9094 bol++
9095 continue
9096 }
9097 break
9098 }
9099 if (line0 == '+') {
9100 if (!last_info) {
9101 syntax(1, "+: without previous info field")
9102 continue
9103 }
9104 txt_add = ' '; // concatenate
9105 line0 = last_info
9106 }
9107 text = uncomment(file.slice(bol, eol), line0)
9108
9109 switch (line0) {
9110 case 'X': // start of tune
9111 if (parse.state != 0) {
9112 syntax(1, errs.ignored, line0)
9113 continue
9114 }
9115 if (parse.select
9116 && !tune_selected()) { // skip to the next tune
9117 eol = file.indexOf('\nX:', parse.eol)
9118 if (eol < 0)
9119 eol = eof;
9120 parse.eol = eol
9121 continue
9122 }
9123
9124 sav.cfmt = clone(cfmt);
9125 sav.info = clone(info, 2) // (level 2 for info.V[])
9126 sav.char_tb = clone(char_tb);
9127 sav.glovar = clone(glovar);
9128 sav.maps = clone(maps, 1);
9129 sav.mac = clone(mac);
9130 sav.maci = clone(maci);
9131 if (cfmt.show_source) {
9132 bol -= 2
9133 set_src(cfmt.show_source)
9134 if (cfmt.show_source[0][0] == 'b')
9135 user.img_out(sav.src)
9136 user.img_out('<div class="source">')
9137 }
9138 info.X = text;
9139 parse.state = 1 // tune header
9140 if (parse.tune_opts)
9141 tune_filter()
9142 continue
9143 case 'T':
9144 switch (parse.state) {
9145 case 0:
9146 continue
9147 case 1:
9148 case 2:
9149 text = trim_title(text, info.T)
9150 if (info.T == undefined) // (keep empty T:)
9151 info.T = text
9152 else
9153 info.T += "\n" + text
9154 continue
9155 }
9156 s = new_block("title");
9157 s.text = text
9158 continue
9159 case 'K':
9160 switch (parse.state) {
9161 case 0:
9162 continue
9163 case 1: // tune header
9164 info.K = text
9165 break
9166 }
9167 do_info(line0, text)
9168 continue
9169 case 'W':
9170 if (parse.state == 0
9171 || cfmt.writefields.indexOf(line0) < 0)
9172 break
9173 if (info.W == undefined)
9174 info.W = text
9175 else
9176 info.W += txt_add + text
9177 break
9178
9179 case 'm':
9180 if (parse.state >= 2) {
9181 syntax(1, errs.ignored, line0)
9182 continue
9183 }
9184 a = text.match(/(.*?)[= ]+(.*)/)
9185 if (!a || !a[2]) {
9186 syntax(1, errs.bad_val, "m:")
9187 continue
9188 }
9189 mac[a[1]] = a[2];
9190 maci[a[1][0]] = true // first letter
9191 break
9192
9193 // info fields in tune body only
9194 case 's':
9195 if (parse.state != 3
9196 || cfmt.writefields.indexOf(line0) < 0)
9197 break
9198 get_sym(text, txt_add == ' ')
9199 break
9200 case 'w':
9201 if (parse.state != 3
9202 || cfmt.writefields.indexOf(line0) < 0)
9203 break
9204 get_lyrics(text, txt_add == ' ')
9205 break
9206 case '|': // "|:" starts a music line
9207 if (parse.state < 2)
9208 continue
9209 parse.line.buffer = text
9210 parse_music_line()
9211 continue
9212 default:
9213 if ("ABCDFGHNOSZ".indexOf(line0) >= 0) {
9214 if (parse.state >= 2) {
9215 syntax(1, errs.ignored, line0)
9216 continue
9217 }
9218 // if (cfmt.writefields.indexOf(c) < 0)
9219 // break
9220 if (!info[line0])
9221 info[line0] = text
9222 else
9223 info[line0] += txt_add + text
9224 break
9225 }
9226
9227 // info field which may be embedded
9228 do_info(line0, text)
9229 continue
9230 }
9231 txt_add = '\n';
9232 last_info = line0
9233 }
9234 if (include)
9235 return
9236 if (parse.state == 1) {
9237 syntax(1, "End of file in tune header")
9238 get_key("C")
9239 }
9240 if (parse.state >= 2)
9241 end_tune();
9242 if (sav.src && cfmt.show_source[0] == '+') {
9243 user.img_out(sav.src) // source of all tunes
9244 sav.src = null
9245 }
9246 parse.state = 0
9247 }
9248 Abc.prototype.tosvg = tosvg
9249 // abc2svg - music.js - music generation
9250 //
9251 // Copyright (C) 2014-2025 Jean-Francois Moine
9252 //
9253 // This file is part of abc2svg-core.
9254 //
9255 // abc2svg-core is free software: you can redistribute it and/or modify
9256 // it under the terms of the GNU Lesser General Public License as published by
9257 // the Free Software Foundation, either version 3 of the License, or
9258 // (at your option) any later version.
9259 //
9260 // abc2svg-core is distributed in the hope that it will be useful,
9261 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9262 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9263 // GNU Lesser General Public License for more details.
9264 //
9265 // You should have received a copy of the GNU Lesser General Public License
9266 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
9267
9268 var gene,
9269 staff_tb,
9270 nstaff, // current number of staves
9271 tsnext, // next line when cut
9272 realwidth, // real staff width while generating
9273 insert_meter, // insert the time signature
9274 spf_last, // spacing for last short line
9275
9276 smallest_duration
9277
9278 /* -- decide whether to shift heads to other side of stem on chords -- */
9279 /* this routine is called only once per tune */
9280
9281 // distance for no overlap - index: [prev acc][cur acc]
9282 //var dt_tb = [
9283 // [5, 5, 5, 5], /* dble sharp */
9284 // [5, 6, 6, 6], /* sharp */
9285 // [5, 6, 5, 6], /* natural */
9286 // [5, 5, 5, 5] /* flat / dble flat */
9287 //]
9288
9289 // accidental x offset - index = note head type
9290 var dx_tb = new Float32Array([
9291 9.5, // FULL
9292 9.5, // EMPTY
9293 13, // OVAL
9294 16, // OVALBARS
9295 16 // SQUARE
9296 ])
9297
9298 // head width - index = note head type
9299 var hw_tb = new Float32Array([
9300 4.7, // FULL
9301 5, // EMPTY
9302 6, // OVAL
9303 7.2, // OVALBARS
9304 7.5 // SQUARE
9305 ])
9306
9307 /* head width for voice overlap - index = note head type */
9308 var w_note = new Float32Array([
9309 2.5, // FULL
9310 3, // EMPTY
9311 4.5, // OVAL
9312 6, // OVALBARS
9313 6.5 // SQUARE
9314 ])
9315
9316 // get head type, dots, flags of note/rest for a duration
9317 function identify_note(s, dur_o) {
9318 var r = abc2svg.hdn[dur_o]
9319 if (r) // in cache?
9320 return r
9321
9322 var head, flags,
9323 dots = 0,
9324 dur = dur_o
9325
9326 if (dur % 12 != 0)
9327 error(1, s, "Invalid note duration $1", dur);
9328 dur /= 12 /* see C.BLEN for values */
9329 if (!dur)
9330 error(1, s, "Note too short")
9331 for (flags = 5; dur; dur >>= 1, flags--) {
9332 if (dur & 1)
9333 break
9334 }
9335 dur >>= 1
9336 while (dur >> dots > 0)
9337 dots++
9338
9339 flags -= dots
9340 if (flags >= 0) {
9341 head = C.FULL
9342 } else switch (flags) {
9343 default:
9344 error(1, s, "Note too long")
9345 flags = -4
9346 /* fall thru */
9347 case -4:
9348 head = C.SQUARE
9349 break
9350 case -3:
9351 head = s.fmt.squarebreve ? C.SQUARE : C.OVALBARS
9352 break
9353 case -2:
9354 head = C.OVAL
9355 break
9356 case -1:
9357 head = C.EMPTY
9358 break
9359 }
9360 abc2svg.hdn[dur_o] =
9361 r = [head, dots, flags]
9362 return r
9363 }
9364
9365 function set_head_shift(s) {
9366 var i, i1, i2, d, ps, dx,
9367 dx_head = dx_tb[s.head],
9368 dir = s.stem,
9369 n = s.nhd
9370
9371 if (!n)
9372 return // single note
9373
9374 /* set the head shifts */
9375 dx = dx_head * .74
9376 if (s.grace)
9377 dx *= .6
9378 if (dir >= 0) {
9379 i1 = 1;
9380 i2 = n + 1;
9381 ps = s.notes[0].pit
9382 } else {
9383 dx = -dx;
9384 i1 = n - 1;
9385 i2 = -1;
9386 ps = s.notes[n].pit
9387 }
9388 var shift = false,
9389 dx_max = 0
9390 for (i = i1; i != i2; i += dir) {
9391 d = s.notes[i].pit - ps;
9392 ps = s.notes[i].pit
9393 if (!d) {
9394 if (shift) { /* unison on shifted note */
9395 var new_dx = s.notes[i].shhd =
9396 s.notes[i - dir].shhd + dx
9397 if (dx_max < new_dx)
9398 dx_max = new_dx
9399 continue
9400 }
9401 if (i + dir != i2 /* second after unison */
9402 //fixme: should handle many unisons after second
9403 && ps + dir == s.notes[i + dir].pit) {
9404 s.notes[i].shhd = -dx
9405 if (dx_max < -dx)
9406 dx_max = -dx
9407 continue
9408 }
9409 }
9410 if (d < 0)
9411 d = -d
9412 if (d > 3 || (d >= 2 && s.head != C.SQUARE)) {
9413 shift = false
9414 } else {
9415 shift = !shift
9416 if (shift) {
9417 s.notes[i].shhd = dx
9418 if (dx_max < dx)
9419 dx_max = dx
9420 }
9421 }
9422 }
9423 s.xmx = dx_max /* shift the dots */
9424 }
9425
9426 // set the accidental shifts for a set of chords
9427 // @grh = head width when grace notes
9428 function acc_shift(notes, grh) {
9429 var i, i1, i2, dx, dx1, dx2, ps, p1, acc, dxh,
9430 n = notes.length
9431
9432 // set the shifts from the head shifts
9433 for (i = n - 1; --i >= 0; ) { // (no shift on top)
9434 dx = notes[i].shhd
9435 if (!dx || dx > 0)
9436 continue
9437 //fixme: '9' is the width of a quarter note
9438 dx = (grh || (notes[i].s ? dx_tb[notes[i].s.head] : 9))
9439 - dx
9440 ps = notes[i].pit
9441 for (i1 = n; --i1 >= 0; ) {
9442 if (!notes[i1].acc)
9443 continue
9444 p1 = notes[i1].pit
9445 if (p1 < ps - 3)
9446 break
9447 if (p1 > ps + 3)
9448 continue
9449 if (notes[i1].shac < dx)
9450 notes[i1].shac = dx
9451 }
9452 }
9453
9454 // set the shifts of the highest and lowest notes
9455 for (i1 = n; --i1 >= 0; ) {
9456 if (notes[i1].acc) {
9457 p1 = notes[i1].pit // top note with accidental
9458 dx1 = notes[i1].shac
9459 if (!dx1) {
9460 dx1 = notes[i1].shhd
9461 dxh = grh || dx_tb[notes[i1].s.head]
9462 if (dx1 < 0)
9463 dx1 = dxh - dx1
9464 else
9465 dx1 = dxh
9466 }
9467 break
9468 }
9469 }
9470 if (i1 < 0) // no accidental
9471 return
9472 for (i2 = 0; i2 < i1; i2++) {
9473 if (notes[i2].acc) {
9474 ps = notes[i2].pit // bottom note with accidental
9475 dx2 = notes[i2].shac
9476 if (!dx2) {
9477 dx2 = notes[i2].shhd
9478 dxh = grh || dx_tb[notes[i2].s.head]
9479 if (dx2 < 0)
9480 dx2 = dxh - dx2
9481 else
9482 dx2 = dxh
9483 }
9484 break
9485 }
9486 }
9487 if (i1 == i2) { // only one accidental
9488 notes[i1].shac = dx1
9489 return
9490 }
9491
9492 if (p1 > ps + 4) { // if interval greater than a sixth
9493 if (dx1 > dx2)
9494 dx2 = dx1 // align the accidentals
9495 notes[i1].shac = notes[i2].shac = dx2
9496 } else {
9497 notes[i1].shac = dx1
9498 if (notes[i1].pit != notes[i2].pit
9499 || notes[i1].acc != notes[i2].acc)
9500 dx1 += 7
9501 notes[i2].shac = dx2 = dx1
9502 }
9503 dx2 += 7
9504
9505 // shift the remaining accidentals
9506 for (i = i1; --i > i2; ) { // from top to bottom
9507 acc = notes[i].acc
9508 if (!acc)
9509 continue
9510 dx = notes[i].shac
9511 if (dx < dx2)
9512 dx = dx2
9513 ps = notes[i].pit
9514 for (i1 = n; --i1 > i; ) {
9515 if (!notes[i1].acc)
9516 continue
9517 p1 = notes[i1].pit
9518 if (p1 >= ps + 4) { // pitch far enough
9519 if (p1 > ps + 4 // if more than a fifth
9520 || acc < 0 // if flat/dble flat
9521 || notes[i1].acc < 0)
9522 continue
9523 }
9524 if (dx > notes[i1].shac - 6) {
9525 dx1 = notes[i1].shac + 7
9526 if (dx1 > dx)
9527 dx = dx1
9528 }
9529 }
9530 notes[i].shac = dx
9531 }
9532 }
9533
9534 /* set the horizontal shift of accidentals */
9535 /* this routine is called only once per tune */
9536 function set_acc_shft() {
9537 var s, s2, st, i, acc, st, t, notes
9538
9539 // search the notes with accidentals at the same time
9540 s = tsfirst
9541 while (s) {
9542 if (s.type != C.NOTE
9543 || s.invis) {
9544 s = s.ts_next
9545 continue
9546 }
9547 st = s.st;
9548 t = s.time;
9549 acc = false
9550 for (s2 = s; s2; s2 = s2.ts_next) {
9551 if (s2.time != t
9552 || s2.type != C.NOTE
9553 || s2.st != st)
9554 break
9555 for (i = 0; i <= s2.nhd; i++) {
9556 if (s2.notes[i].acc) {
9557 s2.notes[i].s = s2
9558 acc = true
9559 }
9560 }
9561 }
9562 if (!acc) {
9563 s = s2
9564 continue
9565 }
9566
9567 // build a pseudo chord and shift the accidentals
9568 notes = []
9569 for ( ; s != s2; s = s.ts_next) {
9570 if (!s.invis)
9571 Array.prototype.push.apply(notes, s.notes)
9572 }
9573 notes.sort(abc2svg.pitcmp)
9574 acc_shift(notes)
9575 }
9576 }
9577
9578 // link a symbol before an other one
9579 function lkvsym(s, next) { // voice linkage
9580 s.next = next;
9581 s.prev = next.prev
9582 if (s.prev)
9583 s.prev.next = s
9584 else
9585 s.p_v.sym = s;
9586 next.prev = s
9587 }
9588 function lktsym(s, next) { // time linkage
9589 var old_wl
9590
9591 s.ts_next = next
9592 if (next) {
9593 s.ts_prev = next.ts_prev
9594 if (s.ts_prev)
9595 s.ts_prev.ts_next = s;
9596 next.ts_prev = s
9597 } else {
9598 //fixme
9599 error(2, s, "Bad linkage")
9600 s.ts_prev = null
9601 }
9602 s.seqst = !s.ts_prev
9603 || s.time != s.ts_prev.time
9604 || (w_tb[s.ts_prev.type] != w_tb[s.type]
9605 && !!w_tb[s.ts_prev.type])
9606 if (!next || next.seqst)
9607 return
9608 next.seqst = next.time != s.time ||
9609 (w_tb[s.type] != w_tb[next.type]
9610 && !!w_tb[s.type])
9611 if (next.seqst) {
9612 old_wl = next.wl
9613 self.set_width(next)
9614 if (next.a_ly)
9615 ly_set(next)
9616 if (!next.shrink) {
9617 next.shrink = next.wl
9618 if (next.prev)
9619 next.shrink += next.prev.wr
9620 } else {
9621 next.shrink += next.wl - old_wl
9622 }
9623 next.space = 0
9624 }
9625 }
9626
9627 /* -- unlink a symbol -- */
9628 function unlksym(s) {
9629 if (s.next)
9630 s.next.prev = s.prev
9631 if (s.prev)
9632 s.prev.next = s.next
9633 else
9634 s.p_v.sym = s.next
9635 if (s.ts_next) {
9636 if (s.seqst) {
9637 if (s.ts_next.seqst) {
9638 s.ts_next.shrink += s.shrink;
9639 s.ts_next.space += s.space
9640 } else {
9641 s.ts_next.seqst = true;
9642 s.ts_next.shrink = s.shrink;
9643 s.ts_next.space = s.space
9644 }
9645 } else {
9646 if (s.ts_next.seqst
9647 && s.ts_prev && s.ts_prev.seqst
9648 && !w_tb[s.ts_prev.type]) {
9649 s.ts_next.seqst = false
9650 s.shrink = s.ts_next.shrink
9651 s.space = s.ts_next.space
9652 }
9653 }
9654 s.ts_next.ts_prev = s.ts_prev
9655 }
9656 if (s.ts_prev)
9657 s.ts_prev.ts_next = s.ts_next
9658 if (tsfirst == s)
9659 tsfirst = s.ts_next
9660 if (tsnext == s)
9661 tsnext = s.ts_next
9662 }
9663
9664 /* -- insert a clef change (treble or bass) before a symbol -- */
9665 function insert_clef(s, clef_type, clef_line) {
9666 var p_voice = s.p_v,
9667 new_s,
9668 st = s.st
9669
9670 /* don't insert the clef between two bars */
9671 if (s.type == C.BAR && s.prev && s.prev.type == C.BAR
9672 && s.prev.bar_type[0] != ':')
9673 s = s.prev;
9674
9675 /* create the symbol */
9676 p_voice.last_sym = s.prev
9677 if (!p_voice.last_sym)
9678 p_voice.sym = null;
9679 p_voice.time = s.time;
9680 new_s = sym_add(p_voice, C.CLEF);
9681 new_s.next = s;
9682 s.prev = new_s;
9683
9684 new_s.clef_type = clef_type;
9685 new_s.clef_line = clef_line;
9686 new_s.st = st;
9687 new_s.clef_small = true
9688 delete new_s.second;
9689 new_s.notes = []
9690 new_s.notes[0] = {
9691 pit: s.notes[0].pit
9692 }
9693 new_s.nhd = 0;
9694
9695 /* link in time */
9696 while (!s.seqst)
9697 s = s.ts_prev;
9698 lktsym(new_s, s)
9699 if (s.soln) { // move the start of line
9700 new_s.soln = true
9701 delete s.soln
9702 }
9703 return new_s
9704 }
9705
9706 /* -- set the staff of the floating voices -- */
9707 /* this function is called only once per tune */
9708 function set_float() {
9709 var p_voice, st, staff_chg, v, s, s1, up, down
9710
9711 for (v = 0; v < voice_tb.length; v++) {
9712 p_voice = voice_tb[v]
9713 // if (!p_voice.floating)
9714 // continue
9715 staff_chg = false;
9716 st = p_voice.st
9717 for (s = p_voice.sym; s; s = s.next) {
9718 if (!s.floating) {
9719 while (s && !s.floating)
9720 s = s.next
9721 if (!s)
9722 break
9723 staff_chg = false
9724 }
9725 if (!s.dur) {
9726 if (staff_chg)
9727 s.st++
9728 continue
9729 }
9730 if (s.notes[0].pit >= 19) { /* F */
9731 staff_chg = false
9732 continue
9733 }
9734 if (s.notes[s.nhd].pit <= 12) { /* F, */
9735 staff_chg = true
9736 s.st++
9737 continue
9738 }
9739 up = 127
9740 for (s1 = s.ts_prev; s1; s1 = s1.ts_prev) {
9741 if (s1.st != st
9742 || s1.v == s.v)
9743 break
9744 if (s1.type == C.NOTE)
9745 if (s1.notes[0].pit < up)
9746 up = s1.notes[0].pit
9747 }
9748 if (up == 127) {
9749 if (staff_chg)
9750 s.st++
9751 continue
9752 }
9753 if (s.notes[s.nhd].pit > up - 3) {
9754 staff_chg = false
9755 continue
9756 }
9757 down = -127
9758 for (s1 = s.ts_next; s1; s1 = s1.ts_next) {
9759 if (s1.st != st + 1
9760 || s1.v == s.v)
9761 break
9762 if (s1.type == C.NOTE)
9763 if (s1.notes[s1.nhd].pit > down)
9764 down = s1.notes[s1.nhd].pit
9765 }
9766 if (down == -127) {
9767 if (staff_chg)
9768 s.st++
9769 continue
9770 }
9771 if (s.notes[0].pit < down + 3) {
9772 staff_chg = true
9773 s.st++
9774 continue
9775 }
9776 up -= s.notes[s.nhd].pit
9777 down = s.notes[0].pit - down
9778 if (!staff_chg) {
9779 if (up < down + 3)
9780 continue
9781 staff_chg = true
9782 } else {
9783 if (up < down - 3) {
9784 staff_chg = false
9785 continue
9786 }
9787 }
9788 s.st++
9789 }
9790 }
9791 }
9792
9793 /* -- set the x offset of the grace notes -- */
9794 function set_graceoffs(s) {
9795 var next, m, dx, x,
9796 gspleft = s.fmt.gracespace[0],
9797 gspinside = s.fmt.gracespace[1],
9798 gspright = s.fmt.gracespace[2],
9799 g = s.extra;
9800
9801 if (s.prev && s.prev.type == C.BAR)
9802 gspleft -= 3;
9803 x = gspleft;
9804
9805 g.beam_st = true
9806 for ( ; ; g = g.next) {
9807 set_head_shift(g)
9808 acc_shift(g.notes, 6.5)
9809 dx = 0
9810 for (m = g.nhd; m >= 0; m--) {
9811 if (g.notes[m].shac - 2 > dx)
9812 dx = g.notes[m].shac - 2
9813 }
9814 x += dx;
9815 g.x = x
9816
9817 if (g.nflags <= 0)
9818 g.beam_st = g.beam_end = true
9819 next = g.next
9820 if (!next) {
9821 g.beam_end = true
9822 break
9823 }
9824 if (next.nflags <= 0)
9825 g.beam_end = true
9826 if (g.beam_end) {
9827 next.beam_st = true;
9828 x += gspinside / 4
9829 }
9830 if (g.nflags <= 0)
9831 x += gspinside / 4
9832 if (g.y > next.y + 8)
9833 x -= 1.5
9834 x += gspinside
9835 }
9836
9837 next = s.next
9838 if (next
9839 && next.type == C.NOTE) { /* if before a note */
9840 if (g.y >= 3 * (next.notes[next.nhd].pit - 18))
9841 gspright -= 1 // above, a bit closer
9842 else if (g.beam_st
9843 && g.y < 3 * (next.notes[next.nhd].pit - 18) - 4)
9844 gspright += 2 // below with flag, a bit further
9845 }
9846 x += gspright;
9847
9848 /* return the whole width */
9849 return x
9850 }
9851
9852 // Compute the smallest spacing between symbols according to chord symbols
9853 // so that they stay at the same offset
9854 // and, also, adjust the spacing due to the lyric words.
9855 // Constraints:
9856 // - assume the chord symbols are only in the first staff
9857 // - treat only the first chord symbol of each symbol
9858 // - the chord symbols under the staff are ignored
9859 function set_w_chs(s) {
9860 var i, ch, w0, s0, dw,
9861 x = 0,
9862 n = 0
9863
9864 set_font("vocal")
9865 for ( ; s; s = s.ts_next) {
9866 if (s.seqst) {
9867 x += s.shrink;
9868 n++
9869 }
9870 if (s.a_ly) // if some lyric
9871 ly_set(s)
9872
9873 if (!s.a_gch)
9874 continue
9875 for (i = 0; i < s.a_gch.length; i++) {
9876 ch = s.a_gch[i]
9877 if (ch.type != 'g' || ch.y < 0) // upper chord symbol only
9878 continue
9879 if (w0) { // width of the previous chord symbol
9880 if (w0 > x + ch.x) {
9881 if (s.prev // (if not at start of a secondary voice)
9882 && s.prev.seqst
9883 && s.prev.type == C.BAR) // don't move away
9884 n-- // the symbol from a bar
9885 dw = (w0 - x - ch.x) / n
9886 while (1) {
9887 s0 = s0.ts_next
9888 if (s0.shrink)
9889 s0.shrink += dw
9890 if (s0 == s
9891 || s0.type == C.BAR)
9892 break
9893 }
9894 }
9895 }
9896 s0 = s;
9897 w0 = ch.text.wh[0];
9898 n = 0;
9899 // x = ch.font.box ? -2 : 0
9900 x = 0
9901 break
9902 }
9903 }
9904 }
9905
9906 // compute the width needed by the left and right annotations
9907 function gchord_width(s, wlnote, wlw) {
9908 var gch, w, ix,
9909 arspc = 0
9910
9911 for (ix = 0; ix < s.a_gch.length; ix++) {
9912 gch = s.a_gch[ix]
9913 switch (gch.type) {
9914 case '<': /* left */
9915 w = gch.text.wh[0] + wlnote
9916 if (w > wlw)
9917 wlw = w
9918 break
9919 case '>': /* right */
9920 w = gch.text.wh[0] + s.wr
9921 if (w > arspc)
9922 arspc = w
9923 break
9924 }
9925 }
9926 if (s.wr < arspc)
9927 s.wr = arspc
9928
9929 return wlw
9930 }
9931
9932 /* -- set the width of a symbol -- */
9933 /* This routine sets the minimal left and right widths wl,wr
9934 * so that successive symbols are still separated when
9935 * no extra glue is put between them */
9936 // (possible hook)
9937 Abc.prototype.set_width = function(s) {
9938 var s2, i, m, xx, w, wlnote, wlw, acc, nt,
9939 bar_type, meter, last_acc, n1, n2, esp, tmp
9940
9941 if (s.play) { // if play symbol
9942 s.wl = s.wr = 0
9943 return
9944 }
9945
9946 switch (s.type) {
9947 case C.NOTE:
9948 case C.REST:
9949
9950 /* set the note widths */
9951 s.wr = wlnote = s.invis ? 0 : hw_tb[s.head]
9952
9953 /* room for shifted heads and accidental signs */
9954 if (s.xmx > 0)
9955 s.wr += s.xmx + 4;
9956 for (s2 = s.prev; s2; s2 = s2.prev) {
9957 if (w_tb[s2.type])
9958 break
9959 }
9960 if (s2) {
9961 switch (s2.type) {
9962 case C.BAR:
9963 case C.CLEF:
9964 case C.KEY:
9965 case C.METER:
9966 wlnote += 3
9967 break
9968 case C.STBRK:
9969 wlnote += 8
9970 break
9971 case C.NOTE:
9972
9973 // change the spacing when stems in reverse directions
9974 if (s.stem * s2.stem < 0)
9975 wlnote += s.stem < 0 ? 3 : -3
9976 break
9977 }
9978 }
9979 w = 0 // width of the chord decorations
9980 for (m = 0; m <= s.nhd; m++) {
9981 nt = s.notes[m]
9982 xx = nt.shhd
9983 if (xx < 0) {
9984 if (wlnote < -xx + 5)
9985 wlnote = -xx + 5
9986 }
9987 acc = nt.acc
9988 if (acc
9989 && (!s2
9990 || s2.type != C.NOTE
9991 || (s2.stem >= 0 && s2.notes[0].pit < nt.pit + 4)
9992 || (s2.stem < 0 && s2.notes[s2.nhd].pit > nt.pit - 4))) {
9993 tmp = nt.shac +
9994 (typeof acc == "object" ? 5.5 : 3.5)
9995 if (s2 && s2.stem < 0
9996 && s2.notes[s2.nhd].pit > nt.pit + 4)
9997 tmp -= 5
9998 if (wlnote < tmp)
9999 wlnote = tmp
10000 }
10001 if (nt.a_dd) { // if decoration in chord
10002 tmp = deco_wch(nt)
10003 if (w < tmp)
10004 w = tmp
10005 }
10006 }
10007 wlnote += w
10008 if (s2) {
10009 switch (s2.type) {
10010 case C.BAR:
10011 case C.CLEF:
10012 case C.KEY:
10013 case C.METER:
10014 wlnote -= 3
10015 break
10016 }
10017 }
10018
10019 /* room for the decorations */
10020 if (s.a_dd)
10021 wlnote = deco_width(s, wlnote)
10022
10023 /* space for flag if stem goes up on standalone note */
10024 if (s.beam_st && s.beam_end
10025 && s.stem > 0 && s.nflags > 0) {
10026 if (s.wr < s.xmx + 9)
10027 s.wr = s.xmx + 9
10028 }
10029
10030 /* leave room for dots and set their offset */
10031 if (s.dots) {
10032 if (s.wl == undefined) // don't recompute if new music line
10033 switch (s.head) {
10034 case C.SQUARE:
10035 case C.OVALBARS:
10036 s.xmx += 3
10037 break
10038 case C.OVAL:
10039 s.xmx += 1
10040 break
10041 }
10042 if (s.wr < s.xmx + 8)
10043 s.wr = s.xmx + 8
10044 if (s.dots >= 2)
10045 s.wr += 3.5 * (s.dots - 1)
10046 }
10047
10048 /* if a tremolo on 2 notes, have space for the small beam(s) */
10049 if (s.trem2 && s.beam_end
10050 && wlnote < 20)
10051 wlnote = 20
10052
10053 wlw = wlnote
10054
10055 if (s2) {
10056 switch (s2.type) {
10057 case C.NOTE:
10058
10059 /* make sure helper lines don't overlap */
10060 if ((s.y > 27 && s2.y > 27)
10061 || (s.y < -3 && s2.y < -3)) {
10062 if (wlw < 6)
10063 wlw = 6
10064 }
10065
10066 /* have ties wide enough */
10067 if (s2.tie) {
10068 if (wlw < 14)
10069 wlw = 14
10070 }
10071 break
10072 case C.CLEF: /* extra space at start of line */
10073 if (s2.second
10074 || s2.clef_small)
10075 break
10076 // fall thru
10077 case C.KEY:
10078 if (s.a_gch)
10079 wlw += 4 // have some room for the chord symbols
10080 // fall thru
10081 case C.METER:
10082 wlw += 3
10083 break
10084 }
10085 }
10086
10087 /* leave room for guitar chord */
10088 if (s.a_gch)
10089 wlw = gchord_width(s, wlnote, wlw)
10090
10091 // ignore the lyrics for now
10092
10093 /* if preceeded by a grace note sequence, adjust */
10094 if (s.prev && s.prev.type == C.GRACE) {
10095 s.wl = wlnote - 4.5
10096 } else {
10097 s.wl = wlw
10098 }
10099 return
10100 case C.SPACE:
10101 xx = s.width / 2;
10102 s.wr = xx
10103 if (s.a_gch)
10104 xx = gchord_width(s, xx, xx)
10105 if (s.a_dd)
10106 xx = deco_width(s, xx)
10107 s.wl = xx
10108 return
10109 case C.BAR:
10110 bar_type = s.bar_type
10111 switch (bar_type) {
10112 case "|":
10113 w = 5 // 3 + 2
10114 break
10115 case "[": // repeat number on secondary staff
10116 w = 0
10117 break
10118 default:
10119 w = 2 + 2.8 * bar_type.length
10120 for (i = 0; i < bar_type.length; i++) {
10121 switch (bar_type[i]) {
10122 case "[":
10123 case "]":
10124 w += 1
10125 // fall thru
10126 case ":":
10127 w += 2
10128 break
10129 }
10130 }
10131 if (bar_type[0] == ":" // if "c3 :|"
10132 && s.prev && s.prev.dots)
10133 w += 4
10134 break
10135 }
10136 s.wl = w
10137 if (s.next
10138 && s.next.type != C.METER)
10139 s.wr = 7
10140 else
10141 s.wr = 5
10142 // s.notes[0].shhd = (w - 5) * -.5
10143
10144 // special case for (mainly) "|| !invisible! |:"
10145 if (s.invis)
10146 //fixme
10147 // && s.prev && s.prev.bar_type)
10148 s.wl = s.wr = 2
10149
10150 /* if preceeded by a grace note sequence, adjust */
10151 s2 = s.prev
10152 if (s2 && s2.type == C.GRACE)
10153 s.wl -= 6
10154 for ( ; s2; s2 = s2.prev) {
10155 if (w_tb[s2.type]) {
10156 if (s2.type == C.STBRK)
10157 s.wl -= 12
10158 break
10159 }
10160 }
10161
10162 if (s.a_dd)
10163 s.wl = deco_width(s, s.wl)
10164
10165 /* have room for the repeat numbers / chord indication */
10166 if (s.text && s.text.length < 4
10167 && s.next && s.next.a_gch) {
10168 set_font("repeat");
10169 s.wr += strwh(s.text)[0] + 2
10170 }
10171 if (cfmt.measurenb > 0 && s.bar_num
10172 && !(s.bar_num % cfmt.measurenb))
10173 s.wr += 4
10174 return
10175 case C.CLEF:
10176 // (there may be invisible clefs in empty staves)
10177 if (s.invis) {
10178 s.wl = s.wr = 1 // (!! not 0 !!)
10179 return
10180 }
10181 if (s.prev && s.prev.type == C.STBRK) {
10182 s.wl = 6
10183 s.wr = 13
10184 delete s.clef_small
10185 return
10186 }
10187 s.wl = s.clef_small ? 11 : 12
10188 s.wr = s.clef_small ? 8 : 13
10189 return
10190 case C.KEY:
10191 if (s.invis)
10192 break // no width
10193 s.wl = 0
10194 esp = 3
10195 n1 = s.k_sf /* new key sig */
10196 if (s.k_old_sf && (s.fmt.cancelkey || n1 == 0))
10197 n2 = s.k_old_sf /* old key */
10198 else
10199 n2 = 0
10200 if (n1 * n2 >= 0) { /* if no natural */
10201 if (n1 < 0)
10202 n1 = -n1
10203 if (n2 < 0)
10204 n2 = -n2
10205 if (n2 > n1)
10206 n1 = n2
10207 } else {
10208 n1 -= n2
10209 if (n1 < 0)
10210 n1 = -n1;
10211 esp += 3 /* see extra space in draw_keysig() */
10212 }
10213 if (s.k_bagpipe == 'p') // K:Hp - add the g natural
10214 n1++
10215 if (s.k_a_acc) {
10216 n2 = s.k_a_acc.length
10217 if (s.exp)
10218 n1 = n2 // no key signature
10219 else
10220 n1 += n2
10221 if (n2)
10222 last_acc = s.k_a_acc[0].acc
10223 for (i = 1; i < n2; i++) {
10224 acc = s.k_a_acc[i]
10225 if (acc.pit > s.k_a_acc[i - 1].pit + 6
10226 || acc.pit < s.k_a_acc[i - 1].pit - 6)
10227 n1-- // no clash
10228 else if (acc.acc != last_acc)
10229 esp += 3;
10230 last_acc = acc.acc
10231 }
10232 }
10233 if (!n1)
10234 break // no width
10235 s.wr = 5.5 * n1 + esp
10236 if (s.prev && !s.prev.bar_type)
10237 s.wl += 2
10238 return
10239 case C.METER:
10240 s.x_meter = []
10241 if (!s.a_meter.length)
10242 break // no width
10243 wlw = 0
10244 for (i = 0; i < s.a_meter.length; i++) {
10245 meter = s.a_meter[i]
10246 switch (meter.top[0]) {
10247 case 'C':
10248 case 'c':
10249 case 'o':
10250 s.x_meter[i] = wlw
10251 wlw += 14
10252 break
10253 default:
10254 w = 0
10255 if (!meter.bot
10256 || meter.top.length > meter.bot.length)
10257 meter = meter.top
10258 else
10259 meter = meter.bot;
10260 for (m = 0; m < meter.length; m++) {
10261 switch (meter[m]) {
10262 case '(':
10263 wlw += 2
10264 w += 6
10265 break
10266 case ')':
10267 wlw -= 2
10268 w += 6
10269 break
10270 case '1':
10271 w += 6
10272 break
10273 case ' ':
10274 w += 2
10275 // fall thru
10276 case '.':
10277 case '|':
10278 break
10279 default:
10280 w += 12
10281 break
10282 }
10283 }
10284 s.x_meter[i] = wlw
10285 wlw += w
10286 break
10287 }
10288 }
10289 s.wl = 1
10290 s.wr = wlw + 4
10291 return
10292 case C.MREST:
10293 s.wl = 6;
10294 s.wr = 66
10295 return
10296 case C.GRACE:
10297 if (s.invis)
10298 break
10299 s.wl = set_graceoffs(s);
10300 s.wr = 0
10301 if (s.a_ly)
10302 ly_set(s)
10303 return
10304 case C.STBRK:
10305 s.wl = s.xmx
10306 s.wr = 8
10307 return
10308 case C.CUSTOS:
10309 s.wl = s.wr = 4
10310 return
10311 case C.TEMPO: // no width, but build the tempo string
10312 tempo_build(s)
10313 break
10314 case C.BLOCK: // no width
10315 case C.REMARK:
10316 case C.STAVES:
10317 break
10318 default:
10319 error(2, s, "set_width - Cannot set width for symbol $1", s.type)
10320 break
10321 }
10322 s.wl = s.wr = 0
10323
10324 // move the invisible symbol to the next time sequence
10325 if (s.seqst && s.ts_next)
10326 s.ts_next.seqst = 0
10327 }
10328
10329 // convert delta time to natural spacing
10330 function time2space(s, len) {
10331 var i, l, space
10332
10333 if (smallest_duration >= C.BLEN / 2) {
10334 if (smallest_duration >= C.BLEN)
10335 len /= 4
10336 else
10337 len /= 2
10338 } else if (!s.next && len >= C.BLEN) {
10339 len /= 2
10340 }
10341 if (len >= C.BLEN / 4) {
10342 if (len < C.BLEN / 2)
10343 i = 5
10344 else if (len < C.BLEN)
10345 i = 6
10346 else if (len < C.BLEN * 2)
10347 i = 7
10348 else if (len < C.BLEN * 4)
10349 i = 8
10350 else
10351 i = 9
10352 } else {
10353 if (len >= C.BLEN / 8)
10354 i = 4
10355 else if (len >= C.BLEN / 16)
10356 i = 3
10357 else if (len >= C.BLEN / 32)
10358 i = 2
10359 else if (len >= C.BLEN / 64)
10360 i = 1
10361 else
10362 i = 0
10363 }
10364 l = len - ((C.BLEN / 16 / 8) << i)
10365 space = cfmt.spatab[i]
10366 if (l) {
10367 if (l < 0) {
10368 space = cfmt.spatab[0] * len / (C.BLEN / 16 / 8)
10369 } else {
10370 if (i >= 9)
10371 i = 8
10372 space += (cfmt.spatab[i + 1] - cfmt.spatab[i]) *
10373 l / ((C.BLEN / 16 / 8) << i)
10374 }
10375 }
10376 return space
10377 }
10378
10379 // set the natural space
10380 function set_space(s, ptime) {
10381 var space, len, s2, stemdir
10382
10383 len = s.time - ptime // time skip
10384
10385 if (!len) {
10386 switch (s.type) {
10387 case C.MREST:
10388 return s.wl
10389 ///*fixme:do same thing at start of line*/
10390 // case C.NOTE:
10391 // case C.REST:
10392 // if (s.ts_prev.type == C.BAR) {
10393 // if (s.nflags < -2)
10394 // return cfmt.spatab[0]
10395 // return cfmt.spatab[2]
10396 // }
10397 // break
10398 }
10399 return 0
10400 }
10401 if (s.ts_prev.type == C.MREST)
10402 // return s.ts_prev.wr + 16
10403 // + 3 // (bar wl=5 wr=8)
10404 return 71 // 66 (mrest.wl) + 5 (bar.wl)
10405
10406 space = time2space(s, len)
10407
10408 while (!s.dur) {
10409 switch (s.type) {
10410 case C.BAR:
10411 // (hack to have quite the same note widths between measures)
10412 if (!s.next)
10413 space *= .9
10414 return space * .9 - 3
10415 case C.CLEF:
10416 return space - s.wl - s.wr
10417 case C.BLOCK: // no space
10418 case C.REMARK:
10419 case C.STAVES:
10420 case C.TEMPO:
10421 s = s.ts_next
10422 if (!s)
10423 return space
10424 continue
10425 case C.NOTE:
10426 break
10427 default:
10428 return space
10429 }
10430 break
10431 }
10432
10433 /* reduce spacing within a beam */
10434 if (len <= C.BLEN / 4) {
10435 s2 = s
10436 while (s2) {
10437 if (!s2.beam_st) {
10438 space *= .9 // ex fnnp
10439 break
10440 }
10441 s2 = s2.ts_next
10442 if (!s2 || s2.seqst)
10443 break
10444 }
10445 }
10446
10447 /* decrease spacing when stem down followed by stem up */
10448 /*fixme:to be done later, after x computed in sym_glue*/
10449 if (s.nflags >= -1
10450 && s.stem > 0) {
10451 stemdir = true
10452
10453 for (s2 = s.ts_prev;
10454 s2 && s2.time == ptime;
10455 s2 = s2.ts_prev) {
10456 if (s2.type == C.NOTE
10457 && (s2.nflags < -1 || s2.stem > 0)) {
10458 stemdir = false
10459 break
10460 }
10461 }
10462 if (stemdir) {
10463 for (s2 = s.ts_next;
10464 s2 && s2.time == s.time;
10465 s2 = s2.ts_next) {
10466 if (s2.type == C.NOTE
10467 && (s2.nflags < -1 || s2.stem < 0)) {
10468 stemdir = false
10469 break
10470 }
10471 }
10472 if (stemdir)
10473 space *= .9
10474 }
10475 }
10476 return space
10477 }
10478
10479 // set the spacing inside tuplets or L: factor
10480 function set_sp_tup(s, s_et) {
10481 var tim = s.time,
10482 ttim = s_et.time - tim,
10483 sp = time2space(s, ttim), // whole time spacing
10484 s2 = s,
10485 wsp = 0
10486
10487 // compute the whole spacing
10488 while (1) {
10489 s2 = s2.ts_next
10490 if (s2.seqst) {
10491 wsp += s2.space
10492 if (s2.bar_type)
10493 wsp += 10 // (fixme: not exact)
10494 }
10495 if (s2 == s_et)
10496 break
10497 }
10498 sp = (sp + wsp) / 2 / ttim // mean spacing per time unit
10499
10500 while (1) {
10501 s = s.ts_next
10502 if (s.seqst) {
10503 s.space = sp * (s.time - tim)
10504 tim = s.time
10505 }
10506 if (s == s_et)
10507 break
10508 }
10509 }
10510
10511 // return an empty bar
10512 function _bar(s) {
10513 return {
10514 type: C.BAR,
10515 bar_type: "|",
10516 fname: s.fname,
10517 istart: s.istart,
10518 iend: s.iend,
10519 v: s.v,
10520 p_v: s.p_v,
10521 st: s.st,
10522 dur: 0,
10523 time: s.time + (s.dur || 0),
10524 nhd: 0,
10525 notes: [{
10526 pit: s.notes ? s.notes[0].pit : 22
10527 }],
10528 seqst: true,
10529 invis: true,
10530 prev: s,
10531 fmt: s.fmt
10532 }
10533 } // _bar()
10534
10535 // create an invisible bar for end of music lines
10536 function add_end_bar(s) {
10537 if (s.type == C.KEY && !s.k_sf // if an invisible key
10538 && !s.k_old_sf && !s.k_a_acc
10539 && s.prev && s.prev.bar_type) { // before a bar
10540 unlksym(s) // remove it
10541 return
10542 }
10543
10544 var b = _bar(s),
10545 sn = s.ts_next // start of next line
10546
10547 b.wl = 0
10548 b.wr = 0
10549 b.ts_prev = s
10550 b.next = s.next
10551 b.ts_next = s.ts_next
10552
10553 if (s.next) // (must not be the end of the voice)
10554 s.next.prev = b
10555 // if (s.ts_next)
10556 s.ts_next.ts_prev = b
10557 s.next = s.ts_next = b
10558 b.shrink = sn.shrink
10559 sn.shrink = sn.wl + 10
10560 b.space = sn.space * .9 - 3
10561 }
10562
10563 /* -- set the width and space of all symbols -- */
10564 // this function is called once for the whole tune
10565 // and once more for each new music line
10566 function set_allsymwidth(first) {
10567 var val, st, s_chs, stup, itup,
10568 s = tsfirst,
10569 s2 = s,
10570 xa = 0,
10571 xl = [],
10572 wr = [],
10573 maxx = xa,
10574 tim = s.time
10575
10576 /* loop on all symbols */
10577 while (1) {
10578 itup = 0
10579 do {
10580 if ((s.a_gch || s.a_ly) && !s_chs)
10581 s_chs = s;
10582 self.set_width(s);
10583 st = s.st
10584 if (xl[st] == undefined)
10585 xl[st] = 0
10586 if (wr[st] == undefined)
10587 wr[st] = 0;
10588 if (s.prev && s.prev.st != st) {
10589 xl[st] = xl[s.prev.st]
10590 wr[st] = wr[s.prev.st]
10591 }
10592 val = xl[st] + wr[st] + s.wl
10593 if (val > maxx)
10594 maxx = val
10595 if (s.dur && s.dur != s.notes[0].dur) // if in tuplet
10596 itup = 1
10597 s = s.ts_next
10598 } while (s && !s.seqst);
10599
10600 // set the spaces of the time sequence
10601 s2.shrink = maxx - xa
10602 s2.space = s2.ts_prev ? set_space(s2, tim) : 0
10603
10604 // adjust the spacing when after a spacer (y)
10605 if (s2.space == 0 && s2.ts_prev
10606 && s2.ts_prev.type == C.SPACE && s2.ts_prev.seqst)
10607 s2.space = s2.ts_prev.space /= 2
10608
10609 if (itup) {
10610 if (!first)
10611 break
10612 if (!stup)
10613 stup = s2
10614 } else if (stup && stup.v == s2.v) {
10615 set_sp_tup(stup, s2)
10616 stup = null
10617 }
10618
10619 if (!s2.shrink) {
10620 if (s2.type == C.CLEF
10621 && !s2.ts_prev.bar_type) {
10622 delete s2.seqst; /* no space */
10623 s2.time = tim
10624 } else {
10625 s2.shrink = s2.wl // cannot be null
10626 maxx += s2.wl
10627 }
10628 }
10629 tim = s2.time
10630 if (!s)
10631 break
10632
10633 // update the min left space per staff
10634 s = s2
10635 do {
10636 wr[s.st] = 0
10637 s = s.ts_next
10638 } while (!s.seqst)
10639
10640 xa = maxx
10641 do {
10642 st = s2.st;
10643 xl[st] = xa
10644 if (s2.wr > wr[st])
10645 wr[st] = s2.wr
10646 s2 = s2.ts_next
10647 } while (!s2.seqst)
10648 }
10649
10650 if (stup)
10651 set_sp_tup(stup, s2)
10652
10653 // let the chord symbols at the same offset
10654 // and adjust the spacing due to the lyrics
10655 if (s_chs)
10656 set_w_chs(s_chs)
10657 }
10658
10659 // insert a rest, this one replacing a sequence or a measure
10660 function to_rest(so) {
10661 var s = clone(so)
10662
10663 s.prev.next = so.ts_prev = so.prev = s.ts_prev.ts_next = s
10664 s.next = s.ts_next = so
10665 so.seqst = false
10666 so.invis = so.play = true
10667
10668 s.type = C.REST
10669 // just keep nl and seqst
10670 delete s.in_tuplet
10671 delete s.tp
10672 delete s.a_dd
10673 delete s.a_gch
10674 delete s.sls
10675 //fixme: what if chord / slur in notes / ... ?
10676 /*fixme: should set many parameters for set_width*/
10677 // set_width(s)
10678 return s
10679 }
10680
10681 /* -- set the repeat sequences / measures -- */
10682 function set_repeat(s) { // first note
10683 var s2, s3, i, j, dur,
10684 n = s.repeat_n,
10685 k = s.repeat_k,
10686 st = s.st,
10687 v = s.v
10688
10689 s.repeat_n = 0 // treated
10690
10691 /* treat the sequence repeat */
10692 if (n < 0) { /* number of notes / measures */
10693 n = -n;
10694 i = n /* number of notes to repeat */
10695 for (s3 = s.prev; s3; s3 = s3.prev) {
10696 if (!s3.dur) {
10697 if (s3.type == C.BAR) {
10698 error(1, s3, "Bar in repeat sequence")
10699 return
10700 }
10701 continue
10702 }
10703 if (--i <= 0)
10704 break
10705 }
10706 if (!s3) {
10707 error(1, s, errs.not_enough_n)
10708 return
10709 }
10710 dur = s.time - s3.time;
10711
10712 i = k * n /* whole number of notes/rests to repeat */
10713 for (s2 = s; s2; s2 = s2.next) {
10714 if (!s2.dur) {
10715 if (s2.type == C.BAR) {
10716 error(1, s2, "Bar in repeat sequence")
10717 return
10718 }
10719 continue
10720 }
10721 if (--i <= 0)
10722 break
10723 }
10724 if (!s2
10725 || !s2.next) { /* should have some symbol */
10726 error(1, s, errs.not_enough_n)
10727 return
10728 }
10729 for (s2 = s.prev; s2 != s3; s2 = s2.prev) {
10730 if (s2.type == C.NOTE) {
10731 s2.beam_end = true
10732 break
10733 }
10734 }
10735 for (j = k; --j >= 0; ) {
10736 i = n /* number of notes/rests */
10737 if (s.dur)
10738 i--;
10739 s2 = s.ts_next
10740 while (i > 0) {
10741 if (s2.st == st) {
10742 s2.invis = s2.play = true
10743 if (s2.seqst && s2.ts_next.seqst)
10744 s2.seqst = false
10745 if (s2.v == v
10746 && s2.dur)
10747 i--
10748 }
10749 s2 = s2.ts_next
10750 }
10751 s = to_rest(s)
10752 s.dur = s.notes[0].dur = dur;
10753 s.rep_nb = -1; // single repeat
10754 s.beam_st = true;
10755 self.set_width(s)
10756 s.head = C.SQUARE;
10757 for (s = s2; s; s = s.ts_next) {
10758 if (s.st == st
10759 && s.v == v
10760 && s.dur)
10761 break
10762 }
10763 }
10764 return
10765 }
10766
10767 /* check the measure repeat */
10768 i = n /* number of measures to repeat */
10769 for (s2 = s.prev.prev ; s2; s2 = s2.prev) {
10770 if (s2.type == C.BAR
10771 || s2.time == tsfirst.time) {
10772 if (--i <= 0)
10773 break
10774 }
10775 }
10776 if (!s2) {
10777 error(1, s, errs.not_enough_m)
10778 return
10779 }
10780
10781 dur = s.time - s2.time /* repeat duration */
10782
10783 if (n == 1)
10784 i = k /* repeat number */
10785 else
10786 i = n /* check only 2 measures */
10787 for (s2 = s; s2; s2 = s2.next) {
10788 if (s2.type == C.BAR) {
10789 if (--i <= 0)
10790 break
10791 }
10792 }
10793 if (!s2) {
10794 error(1, s, errs.not_enough_m)
10795 return
10796 }
10797
10798 /* if many 'repeat 2 measures'
10799 * insert a new %%repeat after the next bar */
10800 i = k /* repeat number */
10801 if (n == 2 && i > 1) {
10802 s2 = s2.next
10803 if (!s2) {
10804 error(1, s, errs.not_enough_m)
10805 return
10806 }
10807 s2.repeat_n = n;
10808 s2.repeat_k = --i
10809 }
10810
10811 /* replace */
10812 dur /= n
10813 if (n == 2) { /* repeat 2 measures (once) */
10814 s3 = s
10815 for (s2 = s.ts_next; ; s2 = s2.ts_next) {
10816 if (s2.st != st)
10817 continue
10818 if (s2.type == C.BAR) {
10819 if (s2.v == v)
10820 break
10821 continue
10822 }
10823 s2.invis = s2.play = true
10824 if (s2.seqst && s2.ts_next.seqst)
10825 s2.seqst = false
10826 }
10827 s3 = to_rest(s3)
10828 s3.dur = s3.notes[0].dur = dur;
10829 s3.invis = true
10830 s2.bar_mrep = 2
10831 s3 = s2.next;
10832 for (s2 = s3.ts_next; ; s2 = s2.ts_next) {
10833 if (s2.st != st)
10834 continue
10835 if (s2.type == C.BAR) {
10836 if (s2.v == v)
10837 break
10838 continue
10839 }
10840 if (!s2.dur)
10841 continue
10842 s2.invis = s2.play = true
10843 if (s2.seqst && s2.ts_next.seqst)
10844 s2.seqst = false
10845 }
10846 s3 = to_rest(s3)
10847 s3.dur = s3.notes[0].dur = dur;
10848 s3.invis = true;
10849 self.set_width(s3)
10850 return
10851 }
10852
10853 /* repeat 1 measure */
10854 s3 = s
10855 for (j = k; --j >= 0; ) {
10856 for (s2 = s3.ts_next; ; s2 = s2.ts_next) {
10857 if (s2.st != st)
10858 continue
10859 if (s2.type == C.BAR) {
10860 if (s2.v == v)
10861 break
10862 continue
10863 }
10864 if (!s2.dur)
10865 continue
10866 s2.invis = s2.play = true
10867 if (s2.seqst && s2.ts_next.seqst)
10868 s2.seqst = false
10869 }
10870 s3 = to_rest(s3)
10871
10872 s3.dur = s3.notes[0].dur = dur;
10873 s3.beam_st = true
10874 if (k == 1) {
10875 s3.rep_nb = 1
10876 break
10877 }
10878 s3.rep_nb = k - j + 1; // number to print above the repeat rest
10879 s3 = s2.next
10880 }
10881 }
10882
10883 /* add a custos before the symbol of the next line */
10884 function custos_add(s) {
10885 var p_voice, new_s, i,
10886 s2 = s
10887
10888 while (1) {
10889 if (s2.type == C.NOTE)
10890 break
10891 s2 = s2.next
10892 if (!s2)
10893 return
10894 }
10895
10896 p_voice = s.p_v;
10897 p_voice.last_sym = s.prev;
10898 // if (!p_voice.last_sym)
10899 // p_voice.sym = null;
10900 p_voice.time = s.time;
10901 new_s = sym_add(p_voice, C.CUSTOS);
10902 new_s.next = s;
10903 s.prev = new_s;
10904 new_s.wl = 0 // (needed here for lktsym)
10905 new_s.wr = 4
10906 lktsym(new_s, s);
10907
10908 new_s.shrink = s.shrink
10909 if (new_s.shrink < 8 + 4)
10910 new_s.shrink = 8 + 4;
10911 new_s.space = s2.space;
10912
10913 new_s.head = C.FULL
10914 new_s.stem = s2.stem
10915 new_s.nhd = s2.nhd;
10916 new_s.notes = []
10917 for (i = 0; i < s2.notes.length; i++) {
10918 new_s.notes[i] = {
10919 pit: s2.notes[i].pit,
10920 shhd: 0,
10921 dur: C.BLEN / 4
10922 }
10923 }
10924 new_s.stemless = true
10925 }
10926
10927 /* -- define the beginning of a new music line -- */
10928 function set_nl(s) { // s = start of line
10929 var p_voice, done, tim, ptyp
10930
10931 // divide the left repeat (|:) or variant bars (|1)
10932 // the new bars go in the next line
10933 function bardiv(so) { // start of next line
10934 var s, s1, s2, t1, t2, i
10935
10936 function new_type(s) {
10937 var t = s.bar_type.match(/(:*)([^:]*)(:*)/)
10938 // [1] = starting ':'s, [2] = middle, [3] = ending ':'s
10939
10940 if (!t[3]) { // if start of variant
10941 // |1 -> | [1
10942 // :|]1 -> :|] [1
10943 t1 = t[1] + t[2]
10944 t2 = '['
10945 } else if (!t[1]) { // if left repeat only
10946 // x|: -> || [|:
10947 t1 = '||'
10948 t2 = '[|' + t[3]
10949 } else {
10950 // :][: -> :|] [|:
10951 i = (t[2].length / 2) | 0
10952 t1 = t[1] + '|' + t[2].slice(0, i)
10953 t2 = t[2].slice(i) +'|' + t[3]
10954 }
10955 } // new_typ()
10956
10957 // change or add a bar for the voice in the previous line
10958 function eol_bar(s, // bar |:
10959 so, // start of new line
10960 sst) { // first bar (for seqst)
10961 var s1, s2, s3
10962
10963 // check if a bar in the previous line
10964 for (s1 = so.ts_prev ; s1.time == s.time; s1 = s1.ts_prev) {
10965 if (s1.v != s.v)
10966 continue
10967 if (s1.bar_type) {
10968 if (s1.bar_type != '|')
10969 return // don't change
10970 s2 = s1 // last symbol in previous line
10971 break
10972 }
10973 if (!s3)
10974 s3 = s1.next // possible anchor for the new bar
10975 }
10976 if (!s2) { // if no symbol in previous line
10977 s2 = clone(s)
10978 if (!s3)
10979 s3 = s
10980 s2.next = s3
10981 s2.prev = s3.prev
10982 if (s2.prev)
10983 s2.prev.next = s2
10984 s3.prev = s2
10985 s2.ts_prev = so.ts_prev // time linkage
10986 s2.ts_prev.ts_next = s2
10987 s2.ts_next = so
10988 so.ts_prev = s2
10989 if (s == sst) // if first inserted bar
10990 s2.seqst = 1 //true
10991 if (s2.seqst) {
10992 for (s = s2.ts_next; !s.seqst; s = s.ts_next)
10993 ;
10994 s2.shrink = s.shrink
10995 s.shrink = s2.wr + s.wl
10996 s2.space = s.space
10997 s.space = 0
10998 }
10999 delete s2.part
11000 }
11001 s2.bar_type = "||"
11002 } // eol_bar()
11003
11004 // check if there is a left repeat bar at start of the new line
11005 s = so // start of new music line
11006 while (s && s.time == so.time) {
11007 if (s.bar_type && s.bar_type.slice(-1) == ':') {
11008 s2 = s
11009 break
11010 }
11011 s = s.ts_next
11012 }
11013 if (s2) {
11014 s = s2
11015 while (1) { // loop on all voices
11016 eol_bar(s2, so, s)
11017 s2 = s2.ts_next
11018 if (!s2 || s2.seqst)
11019 break
11020 }
11021 return so
11022 }
11023
11024 s = so
11025 while (s.ts_prev
11026 && s.ts_prev.time == so.time) {
11027 s = s.ts_prev
11028 if (s.bar_type)
11029 s1 = s // first previous bar
11030 else if (!s1 && s.type == C.GRACE && s.seqst)
11031 so = s // if grace note after a bar
11032 // move the start of line
11033 }
11034 if (!s1
11035 || !s1.bar_type
11036 || (s1.bar_type.slice(-1) != ':'
11037 && !s1.text))
11038 return so
11039
11040 // search the new start of the next line
11041 for (so = s1; so.time == s1.time; so = so.ts_prev) {
11042 switch (so.ts_prev.type) {
11043 case C.KEY:
11044 case C.METER:
11045 // case C.PART:
11046 case C.TEMPO:
11047 case C.STAVES:
11048 case C.STBRK:
11049 continue
11050 }
11051 break
11052 }
11053
11054 // put the new bar before the end of music line
11055 s = s1 // keep first bar
11056 while (1) {
11057 new_type(s1)
11058 s2 = clone(s1)
11059 s2.bar_type = t1
11060 s1.bar_type = t2
11061 s2.ts_prev = so.ts_prev
11062 s2.ts_prev.ts_next = s2
11063 s2.ts_next = so
11064 so.ts_prev = s2
11065 if (s1 == s)
11066 s2.seqst = 1 //true
11067 s2.next = s1
11068 if (s2.prev)
11069 s2.prev.next = s2
11070 s1.prev = s2
11071 if (s1.rbstop)
11072 s2.rbstop = s1.rbstop
11073 if (s1.text) {
11074 s1.invis = 1 //true
11075 delete s1.xsh
11076 delete s2.text
11077 delete s2.rbstart
11078 }
11079 delete s2.part
11080 delete s1.a_dd
11081 delete s1.a_gch
11082 do {
11083 s1 = s1.ts_next
11084 } while (!s1.seqst && !s1.bar_type)
11085 if (s1.seqst)
11086 break
11087 }
11088 return so
11089 } // bardiv()
11090
11091 // set the start of line marker
11092 function set_eol(s) {
11093 if (cfmt.custos && voice_tb.length == 1)
11094 custos_add(s)
11095 s.nl = true
11096 s = s.ts_prev
11097 if (s.type != C.BAR)
11098 add_end_bar(s)
11099 } // set_eol()
11100
11101 // put the warning symbols
11102 // the new symbols go in the previous line
11103 function do_warn(s) { // start of next line
11104 var s1, s2, s3, s4, w
11105
11106 // advance in the next line
11107 for (s2 = s; s2 && s2.time == s.time; s2 = s2.ts_next) {
11108 switch (s2.type) {
11109 case C.KEY:
11110 if (!s2.fmt.keywarn
11111 || s2.invis)
11112 continue
11113 for (s1 = s.ts_prev; s1 ;s1 = s1.ts_prev) {
11114 if (s1.type != C.METER)
11115 break
11116 }
11117 // fall thru
11118 case C.METER:
11119 if (s2.type == C.METER) {
11120 if (!s.fmt.timewarn)
11121 continue
11122 s1 = s.ts_prev
11123 }
11124 // fall thru
11125 case C.CLEF:
11126 if (!s2.prev) // start of voice
11127 continue
11128 if (s2.type == C.CLEF) {
11129 if (s2.clef_none) // if 'K: clef=none' after bar
11130 break
11131 for (s1 = s.ts_prev; s1; s1 = s1.ts_prev) {
11132 switch (s1.type) {
11133 case C.BAR:
11134 if (s1.bar_type[0] == ':')
11135 break
11136 // fall thru
11137 case C.KEY:
11138 case C.METER:
11139 continue
11140 }
11141 break
11142 }
11143 }
11144
11145 // put the warning symbol at end of line
11146 s3 = clone(s2) // duplicate the K:/M:/clef
11147
11148 lktsym(s3, s1.ts_next) // time link
11149
11150 s1 = s3
11151 while (1) {
11152 s1 = s1.ts_next
11153 if (s1.v == s2.v)
11154 break
11155 }
11156 lkvsym(s3, s1) // voice link
11157
11158 // care with spacing
11159 if (s3.seqst) {
11160 self.set_width(s3)
11161 s3.shrink = s3.wl
11162 s4 = s3.ts_prev
11163 w = 0
11164 while (1) {
11165 if (s4.wr > w)
11166 w = s4.wr
11167 if (s4.seqst)
11168 break
11169 s4 = s4.ts_prev
11170 }
11171 s3.shrink += w
11172 s3.space = 0
11173 s4 = s3
11174 while (1) {
11175 if (s4.ts_next.seqst)
11176 break
11177 s4 = s4.ts_next
11178 }
11179 w = 0
11180 while (1) {
11181 if (s4.wl > w)
11182 w = s4.wl
11183 s4 = s4.ts_next
11184 if (s4.seqst)
11185 break
11186 }
11187 s4.shrink = s3.wr + w
11188 }
11189 delete s3.part
11190 continue
11191 }
11192 if (w_tb[s2.type])
11193 break // symbol with a width
11194 }
11195 } // do_warn()
11196
11197 // divide the left repeat and variant bars
11198 s = bardiv(s)
11199
11200 // add the warning symbols at the end of the previous line
11201 do_warn(s)
11202
11203 /* if normal symbol, cut here */
11204 if (s.ts_prev.type != C.STAVES) {
11205 set_eol(s)
11206 return s
11207 }
11208
11209 /* go back to handle the staff breaks at end of line */
11210 for (s = s.ts_prev; s; s = s.ts_prev) {
11211 if (s.seqst && s.type != C.CLEF)
11212 break
11213 }
11214 done = 0
11215 ptyp = s.type
11216 for ( ; ; s = s.ts_next) {
11217 if (!s)
11218 return s
11219 if (s.type == ptyp)
11220 continue
11221 ptyp = s.type
11222 if (done < 0)
11223 break
11224 switch (s.type) {
11225 case C.STAVES:
11226 if (!s.ts_prev)
11227 return // null // no music yet
11228 if (s.ts_prev.type == C.BAR)
11229 break
11230 while (s.ts_next) {
11231 if (w_tb[s.ts_next.type]
11232 && s.ts_next.type != C.CLEF)
11233 break
11234 s = s.ts_next
11235 }
11236 if (!s.ts_next || s.ts_next.type != C.BAR)
11237 continue
11238 s = s.ts_next
11239 // fall thru
11240 case C.BAR:
11241 if (done)
11242 break
11243 done = 1;
11244 continue
11245 case C.STBRK:
11246 if (!s.stbrk_forced)
11247 unlksym(s) /* remove */
11248 else
11249 done = -1 // keep the next symbols on the next line
11250 continue
11251 case C.CLEF:
11252 if (done)
11253 break
11254 continue
11255 default:
11256 if (!done || (s.prev && s.prev.type == C.GRACE))
11257 continue
11258 break
11259 }
11260 break
11261 }
11262 set_eol(s)
11263 return s
11264 }
11265
11266 /* get the width of the starting clef and key signature */
11267 // return
11268 // r[0] = width of clef and key signature
11269 // r[1] = width of the meter
11270 function get_ck_width() {
11271 var r0, r1,
11272 p_voice = voice_tb[0]
11273
11274 self.set_width(p_voice.clef);
11275 self.set_width(p_voice.ckey);
11276 self.set_width(p_voice.meter)
11277 return [p_voice.clef.wl + p_voice.clef.wr +
11278 p_voice.ckey.wl + p_voice.ckey.wr,
11279 p_voice.meter.wl + p_voice.meter.wr]
11280 }
11281
11282 // get the width of the symbols up to the next soln or eof
11283 // also, set a x (nice spacing) to all symbols
11284 // two returned values: width of nice spacing, width with max shrinking
11285 function get_width(s, next) {
11286 var shrink, space,
11287 w = 0,
11288 wmx = 0,
11289 sp_fac = (1 - s.fmt.maxshrink)
11290
11291 while (s != next) {
11292 if (s.seqst) {
11293 shrink = s.shrink
11294 wmx += shrink
11295 if ((space = s.space) < shrink)
11296 w += shrink
11297 else
11298 w += shrink * s.fmt.maxshrink
11299 + space * sp_fac
11300 s.x = w
11301 }
11302 s = s.ts_next
11303 }
11304 if (next)
11305 wmx += next.wr // big key signatures may be wide enough
11306 return [w, wmx]
11307 }
11308
11309 /* -- search where to cut the lines according to the staff width -- */
11310 function set_lines( s, /* first symbol */
11311 next, /* symbol of the next line / null */
11312 lwidth, /* w - (clef & key sig) */
11313 indent) { /* for start of tune */
11314 var first, s2, s3, s4, s5, x, xmin, xmid, xmax, wwidth, shrink, space,
11315 nlines,
11316 last = next ? next.ts_prev : null,
11317 ws = get_width(s, next) // 2 widths: nice and shrunk
11318
11319 // split a big lyric word on two music lines
11320 function ly_split(s, wmax) {
11321 var i, wh,
11322 s2 = clone(s),
11323 p = s.a_ly[0].t, // lyric word
11324 w = 0,
11325 j = 0
11326
11327 gene.deffont = s.a_ly[0].font
11328 while (1) {
11329 i = p.indexOf(' ', j) + 1
11330 if (i <= 0)
11331 break
11332 wh = strwh(p.slice(j, i))
11333 w += wh[0]
11334 if (w > wmax)
11335 break
11336 j = i
11337 }
11338 s.a_ly[0].t = new String(p.slice(0, j - 1))
11339 s2.a_ly = clone(s.a_ly)
11340 s2.a_ly[0] = clone(s.a_ly[0])
11341 s2.a_ly[0].t = new String(p.slice(j))
11342 if (abc2svg.el) {
11343 strwh(s.a_ly[0].t)
11344 strwh(s2.a_ly[0].t)
11345 } else {
11346 s.a_ly[0].t.wh = strwh(s.a_ly[0].t)
11347 s2.a_ly[0].t.wh = strwh(s2.a_ly[0].t)
11348 }
11349 w = s.a_ly[0].t.wh[0] // new length of the words
11350 s.wr = wmax
11351 s2.wr -= w
11352
11353 lkvsym(s2, s2.next) // voice linkage
11354
11355 s2.time += .1
11356 // s2.dur -= .1
11357 s2.nl = 0 //false
11358 while (!s.seqst)
11359 s = s.ts_prev
11360 s2.x = s.x + w
11361 s2.shrink = w - s2.wl
11362 s = s2.ts_next
11363 while (s) {
11364 if (s.seqst) {
11365 s.wl -= w
11366 s.shrink -= w
11367 break
11368 }
11369 s = s.ts_next
11370 }
11371 lktsym(s2, s2.ts_next) // time linkage
11372 } // ly_split()
11373
11374 // -- set_lines --
11375
11376 // take care of big key signatures at end of line
11377 if (s.fmt.keywarn && next
11378 && next.type == C.KEY && !last.dur) {
11379 ws[0] += next.wr
11380 ws[1] += next.wr
11381 }
11382
11383 // check if the symbols can enter in one line (with max shrink)
11384 if (ws[1] + indent < lwidth) {
11385 if (next)
11386 next = set_nl(next)
11387 return next || last
11388 }
11389
11390 /* loop on cutting the tune into music lines */
11391 wwidth = ws[0] + indent
11392 while (1) {
11393 nlines = Math.ceil(wwidth / lwidth)
11394 if (nlines <= 1) {
11395 if (next)
11396 next = set_nl(next)
11397 return next || last
11398 }
11399
11400 s2 = first = s;
11401 xmin = s.x
11402 xmax = xmin + lwidth;
11403 xmid = xmin + wwidth / nlines;
11404 xmin += wwidth / nlines * s.fmt.breaklimit;
11405 for (s = s.ts_next; s != next ; s = s.ts_next) {
11406 if (!s.x)
11407 continue
11408 if (s.type == C.BAR)
11409 s2 = s
11410 if (s.x >= xmin)
11411 break
11412 }
11413 s4 = s // keep first symbol with x greater than xmin
11414 //fixme: can this occur?
11415 if (s == next) {
11416 if (s)
11417 s = set_nl(s)
11418 return s
11419 }
11420
11421 // if the width of the symbol is greater than
11422 // the remaining width in the staff
11423 // and if there are lyrics, split these lyrics on 2 staves
11424 if (s.x > xmax
11425 && s.prev.a_ly) {
11426 s3 = s = s.prev
11427 while (!s3.seqst)
11428 s3 = s3.ts_prev
11429 ly_split(s, xmax - s3.x)
11430 } else {
11431
11432 /* try to cut on a measure bar */
11433 s3 = null
11434 for ( ; s != next; s = s.ts_next) {
11435 x = s.x
11436 if (!x)
11437 continue
11438 if (x > xmax)
11439 break
11440 if (s.type != C.BAR)
11441 continue
11442
11443 // cut on the bar closest to the middle
11444 if (x < xmid) {
11445 s3 = s // closest bar before middle
11446 continue
11447 }
11448 if (!s3 || x - xmid < xmid - s3.x)
11449 s3 = s // closest bar after middle
11450 break
11451 }
11452
11453 // no bar, try to avoid to cut a beam or a tuplet */
11454 if (!s3) {
11455 s = s4 // restart after xmin
11456
11457 var beam = 0,
11458 bar_time = s2.time
11459
11460 xmax -= 8; // (left width of the inserted bar in set_allsymwidth)
11461 s5 = s
11462 for ( ; s != next; s = s.ts_next) {
11463 if (s.seqst) {
11464 x = s.x
11465 if (x + s.wr >= xmax)
11466 break
11467 if (!beam && !s.in_tuplet
11468 && (xmid - s5.x > x - xmid
11469 || (s.time - bar_time)
11470 % (C.BLEN / 4) == 0))
11471 s3 = s
11472 }
11473 if (s.beam_st)
11474 beam |= 1 << s.v
11475 if (s.beam_end)
11476 beam &= ~(1 << s.v)
11477 s5 = s // start of new time sequence
11478 }
11479 if (s3) {
11480 do { // cut on the previous sequence
11481 s3 = s3.ts_prev
11482 } while (!s3.seqst)
11483 }
11484 }
11485
11486 // cut anyhere
11487 if (!s3) {
11488 s3 = s = s4
11489 for ( ; s != next; s = s.ts_next) {
11490 x = s.x
11491 if (!x)
11492 continue
11493 if (x + s.wr >= xmax)
11494 break
11495 if (s3 && x >= xmid) {
11496 if (xmid - s3.x > x - xmid)
11497 s3 = s
11498 break
11499 }
11500 s3 = s
11501 }
11502 }
11503 s = s3
11504 } // ly_split call
11505 while (s.ts_next) {
11506 s = s.ts_next
11507 if (s.seqst)
11508 break
11509 }
11510
11511 if (s.nl) { /* already set here - advance */
11512 error(0, s,
11513 "Line split problem - adjust maxshrink and/or breaklimit");
11514 nlines = 2
11515 for (s = s.ts_next; s != next; s = s.ts_next) {
11516 if (!s.x)
11517 continue
11518 if (--nlines <= 0)
11519 break
11520 }
11521 }
11522 s = set_nl(s)
11523 if (!s
11524 || (next && s.time >= next.time))
11525 break
11526 wwidth -= s.x - first.x;
11527 indent = 0
11528 }
11529 return s
11530 }
11531
11532 /* -- cut the tune into music lines -- */
11533 function cut_tune(lwidth, lsh) {
11534 var s2, i, mc,
11535 pg_sav = { // save the page parameters
11536 leftmargin: cfmt.leftmargin,
11537 rightmargin: cfmt.rightmargin,
11538 pagewidth: cfmt.pagewidth,
11539 scale: cfmt.scale
11540 },
11541 indent = lsh[1] - lsh[2], // extra width of the first line
11542 ckw = get_ck_width(), // width of the starting symbols
11543 s = tsfirst
11544
11545 lwidth -= lsh[2] // width of the lines
11546 if (cfmt.indent && cfmt.indent > lsh[1])
11547 indent += cfmt.indent
11548
11549 // adjust the line width according to the starting symbols
11550 lwidth -= ckw[0]
11551 indent += ckw[1]
11552
11553 if (cfmt.custos && voice_tb.length == 1)
11554 lwidth -= 12
11555
11556 /* if asked, count the measures and set the EOLNs */
11557 i = s.fmt.barsperstaff
11558 if (i) {
11559 for (s2 = s; s2; s2 = s2.ts_next) {
11560 if (s2.type != C.BAR
11561 || !s2.bar_num
11562 || --i > 0)
11563 continue
11564 while (s2.ts_next && s2.ts_next.type == C.BAR)
11565 s2 = s2.ts_next
11566 if (s2.ts_next)
11567 s2.ts_next.soln = true
11568 i = s.fmt.barsperstaff
11569 }
11570 }
11571
11572 /* cut at explicit end of line, checking the line width */
11573 s2 = s
11574 for ( ; s; s = s.ts_next) {
11575 if (s.type == C.BLOCK) {
11576 switch (s.subtype) {
11577 case "leftmargin":
11578 case "rightmargin":
11579 case "pagescale":
11580 case "pagewidth":
11581 case "scale":
11582 case "staffwidth":
11583 if (!s.soln)
11584 self.set_format(s.subtype, s.param)
11585 break
11586 case "mc_start":
11587 mc = {
11588 lm: cfmt.leftmargin,
11589 rm: cfmt.rightmargin
11590 }
11591 break
11592 case "mc_new":
11593 case "mc_end":
11594 if (!mc)
11595 break
11596 cfmt.leftmargin = mc.lm
11597 cfmt.rightmargin = mc.rm
11598 img.chg = 1 //true
11599 break
11600 }
11601 }
11602 if (!s.ts_next) {
11603 s = null
11604 } else if (!s.soln) {
11605 continue
11606 } else {
11607 s.soln = false
11608 //fixme what if new line wanted?
11609 if (s.time == s2.time)
11610 continue // empty music line!
11611 while (!s.seqst)
11612 s = s.ts_prev
11613 }
11614 set_page()
11615 lwidth = get_lwidth() - lsh[1] - ckw[0]
11616 s2 = set_lines(s2, s, lwidth, indent)
11617 if (!s2)
11618 break
11619
11620 s = s2.type == C.BLOCK
11621 ? s2.ts_prev // don't miss a parameter
11622 : s
11623 indent = 0
11624 }
11625
11626 // restore the page parameters at start of line
11627 cfmt.leftmargin = pg_sav.leftmargin
11628 cfmt.rightmargin = pg_sav.rightmargin
11629 cfmt.pagewidth = pg_sav.pagewidth
11630 cfmt.scale = pg_sav.scale
11631 img.chg = 1
11632 set_page()
11633 }
11634
11635 /* -- set the y values of some symbols -- */
11636 function set_yval(s) {
11637 //fixme: staff_tb is not yet defined
11638 // var top = staff_tb[s.st].topbar
11639 // var bot = staff_tb[s.st].botbar
11640 switch (s.type) {
11641 case C.CLEF:
11642 s.y = (s.clef_line - 1) * 6
11643 if (s.second
11644 || s.invis) {
11645 // s.ymx = s.ymn = (top + bot) / 2
11646 s.ymx = s.ymn = 12
11647 break
11648 }
11649 switch (s.clef_type) {
11650 default: /* treble / perc */
11651 s.ymx = s.y + 25
11652 s.ymn = s.y - 14
11653 break
11654 case "c":
11655 s.ymx = s.y + 12
11656 s.ymn = s.y - 11
11657 break
11658 case "b":
11659 s.ymx = s.y + 4
11660 s.ymn = s.y - 11//12
11661 break
11662 }
11663 if (s.clef_small) {
11664 s.ymx -= 3
11665 s.ymn += 2
11666 }
11667 if (s.ymx < 24)
11668 s.ymx = 24
11669 // if (s.ymn > -1)
11670 // s.ymn = -1
11671 if (s.ymn > 0)
11672 s.ymn = 0
11673 // s.y += s.clef_line * 6
11674 // if (s.y > 0)
11675 // s.ymx += s.y
11676 // else if (s.y < 0)
11677 // s.ymn += s.y
11678 if (s.clef_octave) {
11679 if (s.clef_octave > 0)
11680 s.ymx += 4
11681 else
11682 s.ymn -= 4
11683 }
11684 break
11685 case C.KEY:
11686 if (s.k_sf > 2)
11687 s.ymx = 24 + 10
11688 else if (s.k_sf > 0)
11689 s.ymx = 24 + 6
11690 else
11691 s.ymx = 24 + 2;
11692 s.ymn = -2
11693 break
11694 default:
11695 // s.ymx = top;
11696 s.ymx = 24;
11697 s.ymn = 0
11698 break
11699 }
11700 }
11701
11702 // set the pitch of the notes under an ottava sequence
11703 function set_ottava() {
11704 var s, s1, st, o, d,
11705 m = nstaff + 1,
11706 staff_d = new Int8Array(m)
11707
11708 // update the pitches of a symbol
11709 function sym_ott(s, d) {
11710 var g, m, note
11711
11712 switch (s.type) {
11713 case C.REST:
11714 if (voice_tb.length == 1)
11715 break
11716 case C.NOTE:
11717 if (!s.p_v.ckey.k_drum) {
11718 for (m = s.nhd; m >= 0; m--) {
11719 note = s.notes[m];
11720 if (!note.opit)
11721 note.opit = note.pit;
11722 note.pit += d
11723 }
11724 }
11725 break
11726 case C.GRACE:
11727 for (g = s.extra; g; g = g.next) {
11728 if (!s.p_v.ckey.k_drum) {
11729 for (m = 0; m <= g.nhd; m++) {
11730 note = g.notes[m]
11731 if (!note.opit)
11732 note.opit = note.pit
11733 note.pit += d
11734 }
11735 }
11736 }
11737 break
11738 }
11739 } // sym_ott()
11740
11741 // remove the ottava decorations of a symbol
11742 function deco_rm(s) {
11743 for (var i = s.a_dd.length; --i >= 0;) {
11744 if (s.a_dd[i].name.match(/1?[85][vm][ab]/))
11745 s.a_dd.splice(i, 1)
11746 }
11747 } // deco_rm()
11748
11749 for (s = tsfirst; s; s = s.ts_next) {
11750 st = s.st
11751 o = s.ottava
11752 if (o) { // some ottava start or stop
11753 if (o[0]) {
11754 if (staff_d[st] && !o[1]) {
11755 sym_ott(s, staff_d[st])
11756 deco_rm(s)
11757 continue // same ottava
11758 }
11759 } else if (!staff_d[st]) {
11760 deco_rm(s)
11761 continue // already no ottava
11762 }
11763 s1 = s
11764 while (s1 && !s1.seqst)
11765 s1 = s1.ts_prev
11766 if (s1) { // update the previous symbols
11767 while (s1 != s) {
11768 if (s1.st == st) {
11769 if (o[1])
11770 sym_ott(s1, -staff_d[st])
11771 if (o[0])
11772 sym_ott(s1, -o[0] * 7)
11773 }
11774 s1 = s1.ts_next
11775 }
11776 }
11777 if (o[0]) { // ottava start
11778 staff_d[st] = -o[0] * 7
11779 } else {
11780 staff_d[st] = 0
11781 }
11782 }
11783 if (staff_d[st])
11784 sym_ott(s, staff_d[st])
11785 }
11786 }
11787
11788 // expand the multi-rests as needed
11789 function mrest_expand() {
11790 var s, s2
11791
11792 // expand a multi-rest into a set of rest + bar
11793 function mexp(s) {
11794 var bar, s3, s4, tim, nbar,
11795 nb = s.nmes,
11796 dur = s.dur / nb,
11797 s2 = s.next
11798
11799 // get the bar (there may be some other symbols before the bar)
11800 while (s2 && !s2.bar_type)
11801 s2 = s2.next
11802 if (!s2)
11803 return error(1, s, "Lack of bar after multi-measure rest")
11804 bar = s2
11805 while (!s2.bar_num) // get the bar number
11806 s2 = s2.ts_prev
11807 nbar = s2.bar_num - s.nmes
11808
11809 // change the multi-rest into a single rest
11810 s.type = C.REST
11811 s.notes[0].dur = s.dur = s.dur_orig = dur
11812 s.nflags = -2
11813 s.head = C.FULL
11814 s.fmr = 1 // full measure rest
11815
11816 /* add the bar(s) and rest(s) */
11817 tim = s.time + dur
11818 s3 = s
11819 while (--nb > 0) {
11820
11821 // add the bar
11822 s2 = clone(bar)
11823 delete s2.soln
11824 delete s2.a_gch
11825 delete s2.a_dd
11826 delete s2.text
11827 delete s2.rbstart
11828 delete s2.rbstop
11829 lkvsym(s2, s.next) // before symbol at end of rests
11830
11831 s2.time = tim
11832 while (s3.time < tim)
11833 s3 = s3.ts_next // bar at end of measure
11834 if (s3.time == tim)
11835 while (!s3.bar_type
11836 && s3.ts_next && s3.ts_next.time == tim)
11837 s3 = s3.ts_next
11838 while (s3 && s3.v < s.v && s3.type == C.BAR)
11839 s3 = s3.ts_next // keep in order
11840 if (s3) {
11841 if (s3.bar_type)
11842 s3.seqst = 0 //false
11843 lktsym(s2, s3)
11844 if (s3.type == C.BAR)
11845 delete s3.bar_num
11846 } else {
11847 s3 = s
11848 while (s3.ts_next)
11849 s3 = s3.ts_next
11850 s3.ts_next = s2
11851 s2.ts_prev = s3
11852 s2.ts_next = null
11853 }
11854 nbar++
11855 if (s2.seqst) {
11856 s2.bar_num = nbar
11857 s4 = s2.ts_next
11858 } else {
11859 delete s2.bar_num
11860 s4 = s2.ts_prev
11861 }
11862 s2.bar_type = s4.bar_type || "|"
11863 if (s4.bar_num && !s4.seqst)
11864 delete s4.bar_num
11865
11866 // add the rest
11867 s4 = clone(s)
11868 delete s4.a_dd
11869 delete s4.soln
11870 delete s4.a_gch
11871 delete s4.part
11872 if (s2.next) {
11873 s4.next = s2.next
11874 s4.next.prev = s4
11875 } else {
11876 s4.next = null
11877 }
11878 s2.next = s4
11879 s4.prev = s2
11880 s4.time = tim
11881
11882 while (s3 && !s3.dur && s3.time == tim)
11883 s3 = s3.ts_next
11884 while (s3 && s3.v < s.v) {
11885 s3 = s3.ts_next // keep in order
11886 if (s3 && s3.seqst)
11887 break
11888 }
11889 if (s3) {
11890 if (s3.dur)
11891 s3.seqst = 0 //false
11892 lktsym(s4, s3)
11893 } else {
11894 s3 = s
11895 while (s3.ts_next)
11896 s3 = s3.ts_next
11897 s3.ts_next = s4
11898 s4.ts_prev = s3
11899 s4.ts_next = null
11900 }
11901
11902 tim += dur
11903 s = s3 = s4
11904 }
11905 } // mexp()
11906
11907 for (s = tsfirst; s; s = s.ts_next) {
11908 if (s.type != C.MREST)
11909 continue
11910 if (!s.seqst && w_tb[s.ts_prev.type]) {
11911 s2 = s
11912 } else {
11913 s2 = s.ts_next
11914 while (!s2.seqst) {
11915 if (s2.type != C.MREST
11916 || s2.nmes != s.nmes)
11917 break
11918 s2 = s2.ts_next
11919 }
11920 }
11921 if (!s2.seqst) {
11922 while (s.type == C.MREST) {
11923 mexp(s)
11924 s = s.ts_next
11925 }
11926 } else {
11927 s = s2.ts_prev
11928 }
11929 }
11930 } // mrest_expand()
11931
11932 // set the clefs (treble or bass) in a 'auto clef' sequence
11933 // return the starting clef type
11934 function set_auto_clef(st, s_start, clef_type_start) {
11935 var s, time, s2, s3,
11936 max = 14, // "A,"
11937 min = 18 // "E"
11938
11939 /* get the max and min pitches in the sequence */
11940 for (s = s_start; s; s = s.ts_next) {
11941 if (s.type == C.STAVES && s != s_start)
11942 break
11943 if (s.st != st)
11944 continue
11945 if (s.type != C.NOTE) {
11946 if (s.type == C.CLEF) {
11947 if (s.clef_type != 'a')
11948 break
11949 unlksym(s)
11950 }
11951 continue
11952 }
11953 if (s.notes[0].pit < min)
11954 min = s.notes[0].pit
11955 if (s.notes[s.nhd].pit > max)
11956 max = s.notes[s.nhd].pit
11957 }
11958
11959 if (min >= 19 /* upper than 'F' */
11960 || (min >= 13 && clef_type_start != 'b')) /* or 'G,' */
11961 return 't'
11962 if (max <= 13 /* lower than 'G,' */
11963 || (max <= 19 && clef_type_start != 't')) /* or 'F' */
11964 return 'b'
11965
11966 /* set clef changes */
11967 if (clef_type_start == 'a') {
11968 if ((max + min) / 2 >= 16)
11969 clef_type_start = 't'
11970 else
11971 clef_type_start = 'b'
11972 }
11973 var clef_type = clef_type_start,
11974 s_last = s,
11975 s_last_chg = null
11976 for (s = s_start; s != s_last; s = s.ts_next) {
11977 if (s.type == C.STAVES && s != s_start)
11978 break
11979 if (s.st != st || s.type != C.NOTE)
11980 continue
11981
11982 /* check if a clef change may occur */
11983 time = s.time
11984 if (clef_type == 't') {
11985 if (s.notes[0].pit > 12 /* F, */
11986 || s.notes[s.nhd].pit > 20) { /* G */
11987 if (s.notes[0].pit > 20
11988 || s.notes[s.nhd].pit > 20)
11989 s_last_chg = s
11990 continue
11991 }
11992 s2 = s.ts_prev
11993 if (s2
11994 && s2.time == time
11995 && s2.st == st
11996 && s2.type == C.NOTE
11997 && s2.notes[0].pit >= 19) /* F */
11998 continue
11999 s2 = s.ts_next
12000 if (s2
12001 && s2.st == st
12002 && s2.time == time
12003 && s2.type == C.NOTE
12004 && s2.notes[0].pit >= 19) /* F */
12005 continue
12006 } else {
12007 if (s.notes[0].pit <= 12 /* F, */
12008 || s.notes[s.nhd].pit < 20) { /* G */
12009 if (s.notes[s.nhd].pit <= 12
12010 || s.notes[0].pit <= 12)
12011 s_last_chg = s
12012 continue
12013 }
12014 s2 = s.ts_prev
12015 if (s2
12016 && s2.time == time
12017 && s2.st == st
12018 && s2.type == C.NOTE
12019 && s2.notes[0].pit <= 13) /* G, */
12020 continue
12021 s2 = s.ts_next
12022 if (s2
12023 && s2.st == st
12024 && s2.time == time
12025 && s2.type == C.NOTE
12026 && s2.notes[0].pit <= 13) /* G, */
12027 continue
12028 }
12029
12030 /* if first change, change the starting clef */
12031 if (!s_last_chg) {
12032 clef_type = clef_type_start =
12033 clef_type == 't' ? 'b' : 't';
12034 s_last_chg = s
12035 continue
12036 }
12037
12038 /* go backwards and search where to insert a clef change */
12039 s3 = s
12040 for (s2 = s.ts_prev; s2 != s_last_chg; s2 = s2.ts_prev) {
12041 if (s2.st != st)
12042 continue
12043 if (s2.type == C.BAR) {
12044 s3 = s2.bar_type[0] != ':' ? s2 : s2.next
12045 break
12046 }
12047 if (s2.type != C.NOTE)
12048 continue
12049
12050 /* have a 2nd choice on beam start */
12051 if (s2.beam_st
12052 && !s2.p_v.second)
12053 s3 = s2
12054 }
12055
12056 /* no change possible if no insert point */
12057 if (s3.time == s_last_chg.time) {
12058 s_last_chg = s
12059 continue
12060 }
12061 s_last_chg = s;
12062
12063 /* insert a clef change */
12064 clef_type = clef_type == 't' ? 'b' : 't';
12065 s2 = insert_clef(s3, clef_type, clef_type == "t" ? 2 : 4);
12066 s2.clef_auto = true
12067 // s3.prev.st = st
12068 }
12069 return clef_type_start
12070 }
12071
12072 /* set the clefs */
12073 /* this function is called once at start of tune generation */
12074 /*
12075 * global variables:
12076 * - staff_tb[st].clef = clefs at start of line (here, start of tune)
12077 * (created here, updated on clef draw)
12078 * - voice_tb[v].clef = clefs at end of generation
12079 * (created on voice creation, updated here)
12080 */
12081 function set_clefs() {
12082 var s, s2, st, v, p_voice, g, new_type, new_line, p_staff, pit,
12083 staff_clef = new Array(nstaff + 1), // st -> { clef, autoclef }
12084 sy = cur_sy,
12085 mid = []
12086
12087 // create the staff table
12088 staff_tb = new Array(nstaff + 1)
12089 for (st = 0; st <= nstaff; st++) {
12090 staff_clef[st] = {
12091 autoclef: true
12092 }
12093 staff_tb[st] = {
12094 output: "",
12095 sc_out: ""
12096 }
12097 }
12098
12099 for (st = 0; st <= sy.nstaff; st++)
12100 mid[st] = (sy.staves[st].stafflines.length - 1) * 3
12101
12102 for (s = tsfirst; s; s = s.ts_next) {
12103 if (s.repeat_n)
12104 set_repeat(s)
12105
12106 switch (s.type) {
12107 case C.STAVES:
12108 sy = s.sy // new system
12109 for (st = 0; st <= nstaff; st++)
12110 staff_clef[st].autoclef = true
12111 for (v = 0; v < voice_tb.length; v++) {
12112 if (!sy.voices[v])
12113 continue
12114 p_voice = voice_tb[v];
12115 st = sy.voices[v].st
12116 if (!sy.voices[v].second) {
12117 sy.staves[st].staffnonote = p_voice.staffnonote
12118 if (p_voice.staffscale)
12119 sy.staves[st].staffscale = p_voice.staffscale
12120 if (sy.voices[v].sep)
12121 sy.staves[st].sep = sy.voices[v].sep
12122 if (sy.voices[v].maxsep)
12123 sy.staves[st].maxsep = sy.voices[v].maxsep
12124 }
12125 s2 = p_voice.clef
12126 if (!s2.clef_auto)
12127 staff_clef[st].autoclef = false
12128 }
12129 for (st = 0; st <= sy.nstaff; st++)
12130 mid[st] = (sy.staves[st].stafflines.length - 1) * 3
12131 for (v = 0; v < voice_tb.length; v++) {
12132 if (!sy.voices[v]
12133 || sy.voices[v].second) // main voices
12134 continue
12135 p_voice = voice_tb[v];
12136 st = sy.voices[v].st;
12137 s2 = p_voice.clef
12138 if (s2.clef_auto) {
12139 //fixme: the staff may have other voices with explicit clefs...
12140 // if (!staff_clef[st].autoclef)
12141 // ???
12142 new_type = set_auto_clef(st, s,
12143 staff_clef[st].clef ?
12144 staff_clef[st].clef.clef_type :
12145 'a');
12146 new_line = new_type == 't' ? 2 : 4
12147 set_yval(p_voice.clef)
12148 } else {
12149 new_type = s2.clef_type;
12150 new_line = s2.clef_line
12151 }
12152 if (!staff_clef[st].clef) { // new staff
12153 if (s2.clef_auto) {
12154 if (s2.clef_type != 'a')
12155 p_voice.clef =
12156 clone(p_voice.clef);
12157 p_voice.clef.clef_type = new_type;
12158 p_voice.clef.clef_line = new_line
12159 set_yval(p_voice.clef)
12160 }
12161 staff_tb[st].clef =
12162 staff_clef[st].clef = p_voice.clef
12163 continue
12164 }
12165 // old staff
12166 if (new_type == staff_clef[st].clef.clef_type
12167 && new_line == staff_clef[st].clef.clef_line)
12168 continue
12169 g = s.ts_prev
12170 while (g
12171 && g.time == s.time
12172 && (g.v != v || g.st != st))
12173 g = g.ts_prev
12174 if (!g || g.time != s.time) {
12175 g = s.ts_next
12176 while (g && (g.v != v || g.st != st))
12177 g = g.ts_next
12178 if (!g || g.time != s.time)
12179 g = s
12180 }
12181 if (g.type != C.CLEF) {
12182 g = insert_clef(g, new_type, new_line)
12183 if (s2.clef_auto)
12184 g.clef_auto = true
12185 }
12186 staff_clef[st].clef = p_voice.clef = g
12187 }
12188 continue
12189 default:
12190 s.mid = mid[s.st]
12191 continue
12192 case C.CLEF:
12193 break
12194 }
12195
12196 if (s.clef_type == 'a') {
12197 s.clef_type = set_auto_clef(s.st,
12198 s.ts_next,
12199 staff_clef[s.st].clef.clef_type);
12200 s.clef_line = s.clef_type == 't' ? 2 : 4
12201 set_yval(s)
12202 }
12203
12204 p_voice = s.p_v;
12205 p_voice.clef = s
12206 st = s.st
12207 // may have been inserted on %%staves
12208 // if (s.clef_auto) {
12209 // unlksym(s)
12210 // continue
12211 // }
12212
12213 if (staff_clef[st].clef) {
12214 if (s.clef_type == staff_clef[st].clef.clef_type
12215 && s.clef_line == staff_clef[st].clef.clef_line) {
12216 // unlksym(s)
12217 continue
12218 }
12219 } else {
12220
12221 // the voice moved to a new staff with a forced clef
12222 staff_tb[st].clef = s
12223 }
12224 staff_clef[st].clef = s
12225 }
12226
12227 /* set a pitch to the symbols of voices with no note */
12228 sy = cur_sy
12229 for (v = 0; v < voice_tb.length; v++) {
12230 if (!sy.voices[v])
12231 continue
12232 s2 = voice_tb[v].sym
12233 if (!s2 || s2.notes[0].pit != 127)
12234 continue
12235 st = sy.voices[v].st
12236 switch (staff_tb[st].clef.clef_type) {
12237 default:
12238 pit = 22 /* 'B' */
12239 break
12240 case "c":
12241 pit = 16 /* 'C' */
12242 break
12243 case "b":
12244 pit = 10 /* 'D,' */
12245 break
12246 }
12247 for (s = s2; s; s = s.next)
12248 s.notes[0].pit = pit
12249 }
12250 }
12251
12252 /* set the pitch of the notes according to the clefs
12253 * and set the vertical offset of the symbols */
12254 /* this function is called at start of tune generation and
12255 * then, once per music line up to the old sequence */
12256
12257 var delta_tb = {
12258 t: 0 - 2 * 2,
12259 c: 6 - 3 * 2,
12260 b: 12 - 4 * 2,
12261 p: 0 - 3 * 2
12262 }
12263
12264 /* upper and lower space needed by rests */
12265 var rest_sp = [
12266 [18, 18],
12267 [12, 18],
12268 [12, 12],
12269 [10, 12],
12270 [10, 10],
12271 [10, 10], /* crotchet */
12272 [8, 4],
12273 [9, 0],
12274 [9, 4],
12275 [6, 8]
12276 ]
12277
12278 // set the offsets of a rest
12279 function roffs(s) {
12280 s.ymx = s.y + rest_sp[5 - s.nflags][0]
12281 s.ymn = s.y - rest_sp[5 - s.nflags][1]
12282 } // roffs()
12283
12284 // (possible hook)
12285 Abc.prototype.set_pitch = function(last_s) {
12286 var s, s2, g, st, delta, pitch, note,
12287 dur = C.BLEN,
12288 m = nstaff + 1,
12289 staff_delta = new Int16Array(m * 2), // delta clef
12290 sy = cur_sy
12291
12292 // set the starting clefs of the staves
12293 for (st = 0; st <= nstaff; st++) {
12294 s = staff_tb[st].clef;
12295 staff_delta[st] = delta_tb[s.clef_type] + s.clef_line * 2
12296 if (s.clefpit)
12297 staff_delta[st] += s.clefpit
12298 if (cfmt.sound) {
12299 if (s.clef_octave && !s.clef_oct_transp)
12300 staff_delta[st] += s.clef_octave
12301 } else {
12302 if (s.clef_oct_transp)
12303 staff_delta[st] -= s.clef_octave
12304 }
12305 }
12306
12307 for (s = tsfirst; s != last_s; s = s.ts_next) {
12308 st = s.st
12309 switch (s.type) {
12310 case C.CLEF:
12311 staff_delta[st] = delta_tb[s.clef_type] +
12312 s.clef_line * 2
12313 if (s.clefpit)
12314 staff_delta[st] += s.clefpit
12315 if (cfmt.sound) {
12316 if (s.clef_octave && !s.clef_oct_transp)
12317 staff_delta[st] += s.clef_octave
12318 } else {
12319 if (s.clef_oct_transp)
12320 staff_delta[st] -= s.clef_octave
12321 }
12322 set_yval(s)
12323 break
12324 case C.GRACE:
12325 for (g = s.extra; g; g = g.next) {
12326 delta = staff_delta[g.st]
12327 if (delta
12328 && !s.p_v.ckey.k_drum) {
12329 for (m = 0; m <= g.nhd; m++) {
12330 note = g.notes[m];
12331 note.opit = note.pit
12332 note.pit += delta
12333 }
12334 }
12335 g.ymn = 3 * (g.notes[0].pit - 18) - 2;
12336 g.ymx = 3 * (g.notes[g.nhd].pit - 18) + 2
12337 }
12338 set_yval(s)
12339 break
12340 case C.KEY:
12341 s.k_y_clef = staff_delta[st] /* keep the y delta */
12342 /* fall thru */
12343 default:
12344 set_yval(s)
12345 break
12346 case C.MREST:
12347 if (s.invis)
12348 break
12349 s.y = 12;
12350 s.ymx = 24 + 15;
12351 s.ymn = -2
12352 break
12353 case C.REST:
12354 s.y = 12
12355 if (s.rep_nb > 1 // if measure repeat
12356 || s.bar_mrep) {
12357 s.ymx = 38 // (24 + 14)
12358 s.ymn = 0
12359 break
12360 }
12361 roffs(s)
12362 // fall thru
12363 case C.NOTE:
12364 delta = staff_delta[st]
12365 if (delta
12366 && !s.p_v.ckey.k_drum) {
12367 for (m = s.nhd; m >= 0; m--) {
12368 note = s.notes[m]
12369 note.opit = note.pit
12370 note.pit += delta
12371 }
12372 }
12373 if (s.dur < dur)
12374 dur = s.dur
12375 break
12376 }
12377 }
12378 if (!last_s)
12379 smallest_duration = dur
12380 }
12381
12382 /* -- set the stem direction when multi-voices -- */
12383 /* this function is called only once per tune */
12384 // (possible hook)
12385 Abc.prototype.set_stem_dir = function() {
12386 var t, u, i, st, rvoice, v,
12387 v_st, // voice -> staff 1 & 2
12388 st_v, vobj, // staff -> (v, ymx, ymn)*
12389 v_st_tb, // array of v_st
12390 st_v_tb = [], // array of st_v
12391 s = tsfirst,
12392 sy = cur_sy,
12393 nst = sy.nstaff
12394
12395 while (s) {
12396 for (st = 0; st <= nst; st++)
12397 st_v_tb[st] = []
12398 v_st_tb = []
12399
12400 /* get the max/min offsets in the delta time */
12401 /*fixme: the stem height is not calculated yet*/
12402 for (u = s; u; u = u.ts_next) {
12403 if (u.type == C.BAR)
12404 break;
12405 if (u.type == C.STAVES) {
12406 if (u != s)
12407 break
12408 sy = s.sy
12409 for (st = nst; st <= sy.nstaff; st++)
12410 st_v_tb[st] = []
12411 nst = sy.nstaff
12412 continue
12413 }
12414 if ((u.type != C.NOTE && u.type != C.REST)
12415 || u.invis)
12416 continue
12417 st = u.st;
12418 /*fixme:test*/
12419 if (st > nst) {
12420 var msg = "*** fatal set_stem_dir(): bad staff number " + st +
12421 " max " + nst;
12422 error(2, null, msg);
12423 throw new Error(msg)
12424 }
12425 v = u.v;
12426 v_st = v_st_tb[v]
12427 if (!v_st) {
12428 v_st = {
12429 st1: -1,
12430 st2: -1
12431 }
12432 v_st_tb[v] = v_st
12433 }
12434 if (v_st.st1 < 0) {
12435 v_st.st1 = st
12436 } else if (v_st.st1 != st) {
12437 if (st > v_st.st1) {
12438 if (st > v_st.st2)
12439 v_st.st2 = st
12440 } else {
12441 if (v_st.st1 > v_st.st2)
12442 v_st.st2 = v_st.st1;
12443 v_st.st1 = st
12444 }
12445 }
12446 st_v = st_v_tb[st];
12447 rvoice = sy.voices[v].range;
12448 for (i = st_v.length; --i >= 0; ) {
12449 vobj = st_v[i]
12450 if (vobj.v == rvoice)
12451 break
12452 }
12453 if (i < 0) {
12454 vobj = {
12455 v: rvoice,
12456 ymx: 0,
12457 ymn: 24
12458 }
12459 for (i = 0; i < st_v.length; i++) {
12460 if (rvoice < st_v[i].v) {
12461 st_v.splice(i, 0, vobj)
12462 break
12463 }
12464 }
12465 if (i == st_v.length)
12466 st_v.push(vobj)
12467 }
12468
12469 if (u.type != C.NOTE)
12470 continue
12471 if (u.ymx > vobj.ymx)
12472 vobj.ymx = u.ymx
12473 if (u.ymn < vobj.ymn)
12474 vobj.ymn = u.ymn
12475
12476 if (u.xstem) {
12477 if (u.ts_prev.st != st - 1
12478 || u.ts_prev.type != C.NOTE) {
12479 error(1, s, "Bad !xstem!");
12480 u.xstem = false
12481 /*fixme:nflags KO*/
12482 } else {
12483 u.ts_prev.multi = 1;
12484 u.multi = 1;
12485 u.stemless = true
12486 }
12487 }
12488 }
12489
12490 for ( ; s != u; s = s.ts_next) {
12491 if (s.multi)
12492 continue
12493 switch (s.type) {
12494 default:
12495 continue
12496 case C.REST:
12497 // handle %%voicecombine 0
12498 if ((s.combine != undefined && s.combine < 0)
12499 || !s.ts_next || s.ts_next.type != C.REST
12500 || s.ts_next.st != s.st
12501 || s.time != s.ts_next.time
12502 || s.dur != s.ts_next.dur
12503 || (s.a_dd && s.ts_next.a_dd)
12504 || (s.a_gch && s.ts_next.a_gch)
12505 || s.invis)
12506 break
12507 if (s.ts_next.a_dd)
12508 s.a_dd = s.ts_next.a_dd
12509 if (s.ts_next.a_gch)
12510 s.a_gch = s.ts_next.a_gch
12511 if (s.p_v.scale != 1
12512 && s.ts_next.p_v.scale > s.p_v.scale)
12513 unlksym(s)
12514 else
12515 unlksym(s.ts_next)
12516
12517 if ((!s.ts_prev.dur || s.ts_prev.time != s.time
12518 || s.ts_prev.st != s.st)
12519 && (s.ts_next.time != s.time
12520 || s.ts_next.st != s.st))
12521 continue // rest alone in the staff
12522 // fall thru
12523 case C.NOTE:
12524 case C.GRACE:
12525 break
12526 }
12527
12528 st = s.st;
12529 v = s.v;
12530 v_st = v_st_tb[v];
12531 st_v = st_v_tb[st]
12532 if (v_st && v_st.st2 >= 0) {
12533 if (st == v_st.st1)
12534 s.multi = -1
12535 else if (st == v_st.st2)
12536 s.multi = 1
12537 continue
12538 }
12539 if (st_v.length <= 1) { /* voice alone on the staff */
12540 // if (s.multi)
12541 // continue
12542 /*fixme:could be done in set_var()*/
12543 if (s.floating)
12544 s.multi = st == voice_tb[v].st ? -1 : 1
12545 continue
12546 }
12547 rvoice = sy.voices[v].range
12548 for (i = st_v.length; --i >= 0; ) {
12549 if (st_v[i].v == rvoice)
12550 break
12551 }
12552 if (i < 0)
12553 continue /* voice ignored */
12554 if (i == st_v.length - 1) {
12555 s.multi = -1 /* last voice */
12556 } else {
12557 s.multi = 1 /* first voice(s) */
12558
12559 /* if 3 voices, and vertical space enough,
12560 * have stems down for the middle voice */
12561 if (i && i + 2 == st_v.length) {
12562 if (st_v[i].ymn - s.fmt.stemheight
12563 >= st_v[i + 1].ymx)
12564 s.multi = -1;
12565
12566 /* special case for unison */
12567 t = s.ts_next
12568 //fixme: pb with ../lacerda/evol-7.5.5.abc
12569 if (s.ts_prev
12570 && s.ts_prev.time == s.time
12571 && s.ts_prev.st == s.st
12572 && s.notes[s.nhd].pit == s.ts_prev.notes[0].pit
12573 && s.beam_st
12574 && s.beam_end
12575 && (!t
12576 || t.st != s.st
12577 || t.time != s.time))
12578 s.multi = -1
12579 }
12580 }
12581 }
12582 while (s && s.type == C.BAR)
12583 s = s.ts_next
12584 }
12585 }
12586
12587 /* -- adjust the offset of the rests when many voices -- */
12588 /* this function is called only once per tune */
12589 function set_rest_offset() {
12590 var s, s2, v, v_s, ymax, ymin, d,
12591 v_s_tb = [],
12592 sy = cur_sy
12593
12594 // set a vertical offset on a line
12595 function loffs(d) {
12596 return d > 0
12597 ? Math.ceil(d / 6) * 6
12598 : -Math.ceil(-d / 6) * 6
12599 } // loffs()
12600
12601 // shift a rest to the right
12602 function rshift() {
12603 var dx = s2.dots ? 15 : 10
12604 s2.notes[0].shhd = dx
12605 s2.xmx = dx
12606 d = (d + v_s.d) / 2
12607 d = loffs(d)
12608 s2.y += d
12609 s2.ymx += d
12610 s2.ymn += d
12611 v_s.d = 0
12612 } // rshift()
12613
12614 // -- set_rest_off --
12615 for (s = tsfirst; s; s = s.ts_next) {
12616 if (s.invis)
12617 continue
12618 switch (s.type) {
12619 case C.STAVES:
12620 sy = s.sy
12621 // fall thru
12622 default:
12623 continue
12624 case C.REST:
12625 if (s.invis || !s.multi)
12626 continue
12627 v_s = v_s_tb[s.v]
12628 if (!v_s) {
12629 v_s_tb[s.v] = v_s = { d: 0}
12630 } else if (v_s.d) {
12631 s2 = v_s.s // set the offsets of the previous rest
12632 d = loffs(v_s.d)
12633 s2.y += d
12634 s2.ymx += d
12635 s2.ymn += d
12636 v_s.d = 0
12637 }
12638
12639 d = s.multi > 0 ? 0 : 24
12640 if (s.prev && s.prev.type == C.NOTE)
12641 d = (s.next && s.next.type == C.NOTE)
12642 ? (s.prev.y + s.next.y) / 2
12643 : s.prev.y
12644 else if (s.next && s.next.type == C.NOTE)
12645 d = s.next.y
12646 else if (s.prev && s.prev.type == C.REST)
12647 d = s.prev.y
12648 if (s.multi > 0) {
12649 if (d >= 12)
12650 v_s.d = d - s.y
12651 } else {
12652 if (d <= 12)
12653 v_s.d = d - s.y
12654 }
12655
12656 v_s.s = s
12657 v_s.st = s.st
12658 v_s.end_time = s.time + s.dur
12659 if (s.fmr) // if full meeasure rest
12660 v_s.end_time -= s.p_v.wmeasure * .3
12661 if (s.seqst)
12662 continue
12663 s2 = s.ts_prev
12664 if (s2.st != s.st
12665 || s2.invis)
12666 continue
12667 d = s2.ymn
12668 if (v_s_tb[s2.v] && v_s_tb[s2.v].d
12669 && v_s_tb[s2.v] >= s.time)
12670 d += v_s_tb[s2.v].d
12671 if (s.ymx <= d)
12672 continue
12673 if (s2.type == C.NOTE) {
12674 v_s.d = d - s.ymx
12675 break
12676 }
12677 if (s2.type == C.REST
12678 && s2.y < 18
12679 && s.y >= 6)
12680 v_s.d = (d - s.ymx) / 2
12681 break
12682 case C.NOTE:
12683 if (s.invis || !s.multi)
12684 continue
12685 break
12686 }
12687
12688 // check if any clash with a rest
12689 for (v = 0; v < v_s_tb.length; v++) {
12690 v_s = v_s_tb[v]
12691 if (!v_s
12692 || v_s.st != s.st
12693 || v == s.v
12694 || v_s.end_time <= s.time)
12695 continue
12696 s2 = v_s.s // rest
12697 if (sy.voices[v].range > sy.voices[s.v].range) {
12698 if (s2.ymx + v_s.d <= s.ymn)
12699 continue
12700 d = s.ymn - s2.ymx // rest must go down
12701 // if (s2.time < s.time) {
12702 if (s.type == C.REST) {
12703 if (!v_s_tb[s.v])
12704 v_s_tb[s.v] = {d: 0}
12705 if (v_s_tb[s.v].d < 6)
12706 v_s_tb[s.v].d = 6
12707 d = -6
12708 } else {
12709 d /= 2
12710 if (s2.fmr)
12711 d -= 6
12712 }
12713 // }
12714 if (v_s.d) {
12715 if (v_s.d > 0) { // if it was go up
12716 rshift() // shift the rest
12717 continue
12718 }
12719 if (d >= v_s.d)
12720 continue
12721 }
12722 } else {
12723 if (s2.ymn + v_s.d >= s.ymx)
12724 continue
12725 d = s.ymx - s2.ymn // rest must go up
12726 if (s.type == C.REST // if rest
12727 && s2 == s.ts_prev // just under a rest
12728 && s.y == s2.y) { // at a same offset
12729 if (!v_s_tb[s.v])
12730 v_s_tb[s.v] = {d: 0}
12731 if (v_s_tb[s.v].d > -6)
12732 v_s_tb[s.v].d = -6
12733 d = 6
12734 } else if (s2.time < s.time) {
12735 d = s.ymx - s2.y
12736 }
12737 if (v_s.d) {
12738 if (v_s.d < 0) { // if it was go down
12739 rshift() // shift the rest
12740 continue
12741 }
12742 if (d <= v_s.d)
12743 continue
12744 }
12745 }
12746 v_s.d = d
12747 }
12748 }
12749
12750 // update the offsets of the last rests
12751 for (v = 0; v < v_s_tb.length; v++) {
12752 v_s = v_s_tb[v]
12753 if (v_s && v_s.d) {
12754 s2 = v_s.s
12755 d = loffs(v_s.d)
12756 s2.y += d
12757 s2.ymx += d
12758 s2.ymn += d
12759 }
12760 }
12761 }
12762
12763 /* -- create a starting symbol -- */
12764 // last_s = symbol at same time
12765 function new_sym(s, p_v, last_s) {
12766 s.p_v = p_v
12767 s.v = p_v.v
12768 s.st = p_v.st
12769 s.time = last_s.time
12770
12771 if (p_v.last_sym) {
12772 s.next = p_v.last_sym.next
12773 if (s.next)
12774 s.next.prev = s;
12775 p_v.last_sym.next = s;
12776 s.prev = p_v.last_sym
12777 }
12778 p_v.last_sym = s;
12779
12780 lktsym(s, last_s)
12781 }
12782
12783 /* -- init the symbols at start of a music line -- */
12784 function init_music_line() {
12785 var p_voice, s, s1, s2, s3, last_s, v, st, shr, shrmx, shl,
12786 shlp, p_st, top,
12787 nv = voice_tb.length,
12788 fmt = tsfirst.fmt
12789
12790 /* initialize the voices */
12791 for (v = 0; v < nv; v++) {
12792 if (!cur_sy.voices[v])
12793 continue
12794 p_voice = voice_tb[v];
12795 p_voice.st = cur_sy.voices[v].st
12796 p_voice.second = cur_sy.voices[v].second;
12797 p_voice.last_sym = p_voice.sym;
12798
12799 // move the first clefs, key signatures and time signatures
12800 // to the staves
12801 for (s = p_voice.sym; s && s.time == tsfirst.time; s = s.next) {
12802 switch (s.type) {
12803 case C.CLEF:
12804 case C.KEY:
12805 case C.METER:
12806 switch (s.type) {
12807 case C.CLEF:
12808 staff_tb[s.st].clef = s
12809 break
12810 case C.KEY:
12811 s.p_v.ckey = s
12812 break
12813 case C.METER:
12814 s.p_v.meter = s
12815 insert_meter = cfmt.writefields.indexOf('M') >= 0
12816 && s.a_meter.length
12817 break
12818 }
12819 if (s.part)
12820 s.next.part = s.part
12821 unlksym(s)
12822 // fall thru
12823 case C.TEMPO:
12824 case C.BLOCK:
12825 case C.REMARK:
12826 continue
12827 }
12828 break
12829 }
12830 }
12831
12832 // generate the starting clefs, key signatures and time signatures
12833
12834 // add a clef at start of the main voices
12835 last_s = tsfirst
12836 for (v = 0; v < nv; v++) {
12837 p_voice = voice_tb[v]
12838 if (!cur_sy.voices[v]
12839 || (cur_sy.voices[v].second
12840 && !p_voice.bar_start)) // needed for correct linkage
12841 continue
12842 st = cur_sy.voices[v].st
12843 if (!staff_tb[st]
12844 || !staff_tb[st].clef)
12845 continue
12846 s = clone(staff_tb[st].clef);
12847 s.v = v;
12848 s.p_v = p_voice;
12849 s.st = st;
12850 s.time = tsfirst.time;
12851 s.prev = null;
12852 s.next = p_voice.sym
12853 if (s.next)
12854 s.next.prev = s;
12855 p_voice.sym = p_voice.last_sym = s
12856 s.ts_next = last_s;
12857 if (last_s)
12858 s.ts_prev = last_s.ts_prev
12859 else
12860 s.ts_prev = null
12861 if (!s.ts_prev) {
12862 tsfirst = s;
12863 } else {
12864 s.ts_prev.ts_next = s
12865 delete s.seqst
12866 }
12867 if (last_s)
12868 last_s.ts_prev = s
12869 delete s.clef_small;
12870 delete s.part
12871 s.second = cur_sy.voices[v].second
12872 // (fixme: needed for sample5 X:3 Fugue & staffnonote.html)
12873 if (!cur_sy.st_print[st])
12874 s.invis = true
12875 else if (!s.clef_none)
12876 delete s.invis
12877 s.fmt = fmt
12878 }
12879
12880 /* add keysig */
12881 for (v = 0; v < nv; v++) {
12882 if (!cur_sy.voices[v]
12883 || cur_sy.voices[v].second
12884 || !cur_sy.st_print[cur_sy.voices[v].st])
12885 continue
12886 p_voice = voice_tb[v]
12887 s2 = p_voice.ckey
12888 if (s2.k_sf || s2.k_a_acc) {
12889 s = clone(s2)
12890 new_sym(s, p_voice, last_s)
12891 delete s.invis
12892 delete s.part
12893 s.k_old_sf = s2.k_sf // no key cancel
12894 s.fmt = fmt
12895 }
12896 }
12897
12898 /* add time signature (meter) if needed */
12899 if (insert_meter) {
12900 for (v = 0; v < nv; v++) {
12901 p_voice = voice_tb[v];
12902 s2 = p_voice.meter
12903 if (!cur_sy.voices[v]
12904 || cur_sy.voices[v].second
12905 || !cur_sy.st_print[cur_sy.voices[v].st])
12906 // || !s2.a_meter.length)
12907 continue
12908 s = clone(s2)
12909 new_sym(s, p_voice, last_s)
12910 delete s.part
12911 s.fmt = fmt
12912 }
12913 insert_meter = false // no meter any more
12914 }
12915
12916 // add an invisible bar for the various continued elements
12917 for (v = 0; v < nv; v++) {
12918 p_voice = voice_tb[v]
12919 if (p_voice.sls.length) {
12920 s = {
12921 type: C.BAR,
12922 fname: last_s.fname,
12923 bar_type: "|",
12924 dur: 0,
12925 multi: 0,
12926 invis: true,
12927 sls: p_voice.sls,
12928 fmt: fmt
12929 }
12930 new_sym(s, p_voice, last_s)
12931 p_voice.sls = []
12932 }
12933 }
12934
12935 // add a bar for the continuation of repeat brackets
12936 for (v = 0; v < nv; v++) {
12937 p_voice = voice_tb[v];
12938 s2 = p_voice.bar_start;
12939 p_voice.bar_start = null
12940
12941 // check if bracket stop at this time
12942 for (s = last_s; s && s.time == last_s.time; s = s.ts_next) {
12943 if (s.rbstop) {
12944 s2 = null
12945 break
12946 }
12947 }
12948
12949 if (!s2)
12950 continue
12951 if (!cur_sy.voices[v]
12952 || !cur_sy.st_print[cur_sy.voices[v].st])
12953 continue
12954
12955 if (p_voice.last_sym.type == C.BAR) {
12956 if (!p_voice.last_sym.rbstop)
12957 p_voice.last_sym.rbstart = 1
12958 } else {
12959 new_sym(s2, p_voice, last_s)
12960 s2.fmt = fmt
12961 }
12962 }
12963
12964 // compute the spacing of the added symbols
12965 self.set_pitch(last_s);
12966
12967 s = tsfirst
12968 s.seqst = true
12969
12970 for (s = last_s; s.ts_next && !s.ts_next.seqst; s = s.ts_next)
12971 ;
12972 if (s.ts_next // a bit further in case different keys per voice
12973 && s.ts_next.type != C.CLEF // (the clef may move in allsymwidth)
12974 && !s.tp // (start of a tuplet)
12975 && !s.ts_next.a_ly) // (don't update next .shrink)
12976 for (s = s.ts_next; s.ts_next && !s.ts_next.seqst; s = s.ts_next)
12977 ;
12978 s2 = s.ts_next
12979 s.ts_next = null
12980 set_allsymwidth()
12981 s.ts_next = s2
12982 } // init_music_line()
12983
12984 // check if the tune ends on a measure bar
12985 function check_end_bar() {
12986 var s2,
12987 s = tsfirst
12988 while (s.ts_next)
12989 s = s.ts_next
12990 if (s.type != C.BAR) {
12991 s2 = _bar(s)
12992 s2.ts_prev = s
12993
12994 s.next = s.ts_next = s2
12995 }
12996 } // check_end_bar()
12997
12998 /* -- set a pitch in all symbols and the start/stop of the beams -- */
12999 // and sort the pitches in the chords
13000 // and build the chord symbols / annotations
13001 // this function is called only once per tune
13002 function set_words(p_voice) {
13003 var s, s2, nflags, lastnote, res,
13004 start_flag = true,
13005 pitch = 127 /* no note */
13006
13007 // adjust the duration of the notes in a decoration !trem1..4!
13008 function trem_adj(s) {
13009 s.prev.trem2 = true
13010 s.prev.head = ++s.head
13011 if (--s.nflags > 0) {
13012 s.nflags += s.ntrem
13013 } else {
13014 if (s.nflags <= -2) {
13015 s.stemless = true
13016 s.prev.stemless = true
13017 }
13018 s.nflags = s.ntrem
13019 }
13020 s.prev.nflags = s.nflags
13021 } // trem_adj()
13022
13023 for (s = p_voice.sym; s; s = s.next) {
13024 if (s.type == C.NOTE) {
13025 pitch = s.notes[0].pit
13026 break
13027 }
13028 }
13029 for (s = p_voice.sym; s; s = s.next) {
13030 if (s.a_gch)
13031 self.gch_build(s)
13032 switch (s.type) {
13033 case C.MREST:
13034 start_flag = true
13035 break
13036 case C.BAR:
13037 res = s.fmt.bardef[s.bar_type]
13038 if (res)
13039 s.bar_type = res
13040 if (!s.beam_on)
13041 start_flag = true
13042 if (!s.next && s.prev
13043 && !s.invis
13044 && s.prev.head == C.OVALBARS)
13045 s.prev.head = C.SQUARE
13046 break
13047 case C.GRACE:
13048 for (s2 = s.extra; s2; s2 = s2.next) {
13049 s2.notes.sort(abc2svg.pitcmp)
13050 res = identify_note(s2, s2.dur_orig)
13051 s2.head = res[0]
13052 s2.dots = res[1]
13053 s2.nflags = res[2]
13054 if (s2.trem2
13055 && (!s2.next || s2.next.trem2))
13056 trem_adj(s2)
13057 }
13058 break
13059 case C.NOTE:
13060 case C.REST:
13061 res = identify_note(s, s.dur_orig);
13062 s.head = res[0];
13063 s.dots = res[1];
13064 s.nflags = res[2]
13065 if (s.nflags <= -2)
13066 s.stemless = true
13067
13068 if (s.xstem)
13069 s.nflags = 0 // beam break
13070 if (s.trem1) {
13071 if (s.nflags > 0)
13072 s.nflags += s.ntrem
13073 else
13074 s.nflags = s.ntrem
13075 }
13076 if (s.next && s.next.trem2)
13077 break
13078 if (s.trem2) {
13079 trem_adj(s)
13080 break
13081 }
13082
13083 nflags = s.nflags
13084
13085 if (s.ntrem)
13086 nflags -= s.ntrem
13087 if (s.type == C.REST && s.beam_end
13088 && !s.beam_on) {
13089 // s.beam_end = false;
13090 start_flag = true
13091 }
13092 if (start_flag
13093 || nflags <= 0) {
13094 if (lastnote) {
13095 lastnote.beam_end = true;
13096 lastnote = null
13097 }
13098 if (nflags <= 0) {
13099 s.beam_st = s.beam_end = true
13100 } else if (s.type == C.NOTE || s.beam_on) {
13101 s.beam_st = true;
13102 start_flag = false
13103 }
13104 }
13105 if (s.beam_end)
13106 start_flag = true
13107 if (s.type == C.NOTE || s.beam_on)
13108 lastnote = s
13109 break
13110 }
13111 if (s.type == C.NOTE) {
13112 if (s.nhd)
13113 s.notes.sort(abc2svg.pitcmp)
13114 pitch = s.notes[0].pit
13115 // if (s.prev
13116 // && s.prev.type != C.NOTE) {
13117 // s.prev.notes[0].pit = (s.prev.notes[0].pit
13118 // + pitch) / 2
13119 for (s2 = s.prev; s2; s2 = s2.prev) {
13120 if (s2.type != C.REST)
13121 break
13122 s2.notes[0].pit = pitch
13123 }
13124 } else {
13125 if (!s.notes) {
13126 s.notes = []
13127 s.notes[0] = {}
13128 s.nhd = 0
13129 }
13130 s.notes[0].pit = pitch
13131 }
13132 }
13133 if (lastnote)
13134 lastnote.beam_end = true
13135 }
13136
13137 /* -- set the end of the repeat sequences -- */
13138 function set_rb(p_voice) {
13139 var s2, n,
13140 s = p_voice.sym
13141
13142 while (s) {
13143 if (s.type != C.BAR || !s.rbstart || s.norepbra) {
13144 s = s.next
13145 continue
13146 }
13147 n = 0;
13148 s2 = null
13149 for (s = s.next; s; s = s.next) {
13150 if (s.type != C.BAR)
13151 continue
13152 if (s.rbstop)
13153 break
13154 if (!s.next) {
13155 s.rbstop = 2 // right repeat with end
13156 break
13157 }
13158 n++
13159 if (n == s.fmt.rbmin)
13160 s2 = s
13161 if (n == s.fmt.rbmax) {
13162 if (s2)
13163 s = s2;
13164 s.rbstop = 1 // right repeat without end
13165 break
13166 }
13167 }
13168 }
13169 }
13170
13171 /* -- initialize the generator -- */
13172 // this function is called only once per tune
13173
13174 var delpit = [0, -7, -14, 0]
13175
13176 function set_global() {
13177 var p_voice, v,
13178 nv = voice_tb.length,
13179 sy = cur_sy,
13180 st = sy.nstaff
13181
13182 insert_meter = cfmt.writefields.indexOf('M') >= 0
13183
13184 /* get the max number of staves */
13185 while (1) {
13186 sy = sy.next
13187 if (!sy)
13188 break
13189 if (sy.nstaff > st)
13190 st = sy.nstaff
13191 }
13192 nstaff = st;
13193
13194 // there must be a bar at end of tune
13195 check_end_bar()
13196
13197 /* set the pitches, the words (beams) and the repeat brackets */
13198 for (v = 0; v < nv; v++) {
13199 p_voice = voice_tb[v];
13200 set_words(p_voice)
13201 p_voice.ckey = p_voice.key // starting key
13202 // (test removed because v.second may change after %%staves)
13203 // if (!p_voice.second && !p_voice.norepbra)
13204 set_rb(p_voice)
13205 }
13206
13207 /* set the staff of the floating voices */
13208 if (nv > 1) {
13209 set_float()
13210
13211 // expand the multi-rests as needed
13212 if (glovar.mrest_p)
13213 mrest_expand()
13214 }
13215
13216 if (glovar.ottava && cfmt.sound != "play")
13217 set_ottava();
13218
13219 // set the clefs and adjust the pitches of all symbol
13220 set_clefs();
13221 self.set_pitch(null)
13222 }
13223
13224 // get the left offsets of the first and other staff systems
13225 // return left shift [no name, first music line, other lines
13226 function get_lshift() {
13227 var st, v, p_v, p1, po, fnt, w,
13228 sy = cur_sy,
13229 lsh = [0, 0, 0],
13230 nv = voice_tb.length
13231
13232 // get the max width of a voice name/subname
13233 function get_wx(p, wx) {
13234 var w, j,
13235 i = 0
13236
13237 p += '\n'
13238 while (1) {
13239 j = p.indexOf("\n", i)
13240 if (j < 0)
13241 break
13242 w = strwh(p.slice(i, j))[0] + 12
13243 if (w > wx)
13244 wx = w
13245 if (j < 0)
13246 break
13247 i = j + 1
13248 }
13249 return wx
13250 } // get_wx()
13251
13252 for (v = 0; v < nv; v++) {
13253 p_v = voice_tb[v]
13254 p1 = p_v.nm
13255 po = p_v.snm
13256 if ((p1 || po) && !fnt) {
13257 set_font("voice")
13258 fnt = gene.deffont
13259 }
13260 if (p1) {
13261 w = get_wx(p1, lsh[1])
13262 if (w > lsh[1])
13263 lsh[1] = w
13264 }
13265 if (po) {
13266 w = get_wx(po, lsh[2])
13267 if (w > lsh[2])
13268 lsh[2] = w
13269 }
13270 }
13271 // add the width of the braces/brackets
13272 w = 0
13273 while (sy) {
13274 for (st = 0; st <= sy.nstaff; st++) {
13275 if (sy.staves[st].flags
13276 & (OPEN_BRACE2 | OPEN_BRACKET2)) {
13277 w = 12
13278 break
13279 }
13280 if (sy.staves[st].flags & (OPEN_BRACE | OPEN_BRACKET))
13281 w = 6
13282 }
13283 if (w == 12)
13284 break
13285 sy = sy.next
13286 }
13287 lsh[0] = w
13288 lsh[1] += w
13289 lsh[2] += w
13290 return lsh
13291 } // get_lshift()
13292
13293 /* -- return the left indentation of the staves -- */
13294 function set_indent(lsh) {
13295 var st, v, w, p_voice, p, i, j, font,
13296 vnt = 0,
13297 fmt = tsnext ? tsnext.fmt : cfmt
13298
13299 // name or subname?
13300 if (fmt.systvoices) { // put the voice names in the staff system
13301 for (v = voice_tb.length; --v >= 0; ) {
13302 p_voice = voice_tb[v]
13303 if (!cur_sy.voices[v]
13304 || !gene.st_print[p_voice.st])
13305 continue
13306 if (p_voice.nm
13307 && (fmt.systvoices == 1
13308 || (p_voice.new_name && fmt.systvoices == 3))) {
13309 vnt = 1 // full name
13310 break
13311 }
13312 if (p_voice.snm)
13313 vnt = 2 // subname
13314 }
13315 }
13316 gene.vnt = vnt // voice name type for draw
13317 return lsh[vnt]
13318 }
13319
13320 /* -- decide on beams and on stem directions -- */
13321 /* this routine is called only once per tune */
13322 function set_beams(sym) {
13323 var s, t, g, beam, s_opp, n, m, mid_p, pu, pd,
13324 laststem = -1
13325
13326 for (s = sym; s; s = s.next) {
13327 if (s.type != C.NOTE) {
13328 if (s.type != C.GRACE)
13329 continue
13330 g = s.extra
13331 if (g.stem == 2) { /* opposite gstem direction */
13332 s_opp = s
13333 continue
13334 }
13335 if (!s.stem)
13336 s.stem = s.multi || 1
13337 for (; g; g = g.next) {
13338 g.stem = s.stem;
13339 g.multi = s.multi
13340 }
13341 continue
13342 }
13343
13344 if (!s.stem && s.multi)
13345 s.stem = s.multi
13346 if (!s.stem) { // if note alone on the staff
13347 mid_p = s.mid / 3 + 18
13348
13349 /* notes in a beam have the same stem direction */
13350 if (beam) {
13351 s.stem = laststem
13352 } else if (s.beam_st && !s.beam_end) { // beam start
13353 beam = true;
13354
13355 // the stem direction is the one of the note
13356 // farthest from the middle line
13357 pu = s.notes[s.nhd].pit;
13358 pd = s.notes[0].pit
13359 for (g = s.next; g; g = g.next) {
13360 if (g.type != C.NOTE)
13361 continue
13362 if (g.stem || g.multi) // if forced direction
13363 s.stem = g.stem || g.multi
13364 if (g.notes[g.nhd].pit > pu)
13365 pu = g.notes[g.nhd].pit
13366 if (g.notes[0].pit < pd)
13367 pd = g.notes[0].pit
13368 if (g.beam_end)
13369 break
13370 }
13371 if (!s.stem && g.beam_end) {
13372 if (pu + pd < mid_p * 2) {
13373 s.stem = 1
13374 } else if (pu + pd > mid_p * 2) {
13375 s.stem = -1
13376 } else {
13377 if (s.fmt.bstemdown)
13378 s.stem = -1
13379 }
13380 }
13381 if (!s.stem)
13382 s.stem = laststem
13383 } else { // no beam
13384 n = (s.notes[s.nhd].pit + s.notes[0].pit) / 2
13385 if (n == mid_p && s.nhd > 1) {
13386 for (m = 0; m < s.nhd; m++) {
13387 if (s.notes[m].pit >= mid_p)
13388 break
13389 }
13390 n = m * 2 < s.nhd ? mid_p - 1 : mid_p + 1
13391 }
13392 if (n < mid_p)
13393 s.stem = 1
13394 else if (n > mid_p || s.fmt.bstemdown)
13395 s.stem = -1
13396 else
13397 s.stem = laststem
13398 }
13399 } else { /* stem set by set_stem_dir */
13400 if (s.beam_st && !s.beam_end)
13401 beam = true
13402 }
13403 if (s.beam_end)
13404 beam = false;
13405 laststem = s.stem;
13406
13407 if (s_opp) { /* opposite gstem direction */
13408 for (g = s_opp.extra; g; g = g.next)
13409 g.stem = -laststem;
13410 s_opp.stem = -laststem;
13411 s_opp = null
13412 }
13413 }
13414 }
13415
13416 // check if there may be one head for unison when voice overlap
13417 function same_head(s1, s2) {
13418 var i1, i2, l1, l2, head, i11, i12, i21, i22, sh1, sh2,
13419 shu = s1.fmt.shiftunison || 0
13420
13421 if (shu >= 3)
13422 return false
13423 if ((l1 = s1.dur) >= C.BLEN)
13424 return false
13425 if ((l2 = s2.dur) >= C.BLEN)
13426 return false
13427 if (s1.stemless && s2.stemless)
13428 return false
13429 if (s1.dots != s2.dots) {
13430 if (shu & 1
13431 || s1.dots * s2.dots != 0)
13432 return false
13433 }
13434 if (s1.stem * s2.stem > 0)
13435 return false
13436
13437 /* check if a common unison */
13438 i1 = i2 = 0
13439 if (s1.notes[0].pit > s2.notes[0].pit) {
13440 //fixme:dots
13441 if (s1.stem < 0)
13442 return false
13443 while (s2.notes[i2].pit != s1.notes[0].pit) {
13444 if (++i2 > s2.nhd)
13445 return false
13446 }
13447 } else if (s1.notes[0].pit < s2.notes[0].pit) {
13448 //fixme:dots
13449 if (s2.stem < 0)
13450 return false
13451 while (s2.notes[0].pit != s1.notes[i1].pit) {
13452 if (++i1 > s1.nhd)
13453 return false
13454 }
13455 }
13456 if (s2.notes[i2].acc != s1.notes[i1].acc)
13457 return false;
13458 i11 = i1;
13459 i21 = i2;
13460 sh1 = s1.notes[i1].shhd;
13461 sh2 = s2.notes[i2].shhd
13462 do {
13463 //fixme:dots
13464 i1++;
13465 i2++
13466 if (i1 > s1.nhd) {
13467 //fixme:dots
13468 // if (s1.notes[0].pit < s2.notes[0].pit)
13469 // return false
13470 break
13471 }
13472 if (i2 > s2.nhd) {
13473 //fixme:dots
13474 // if (s1.notes[0].pit > s2.notes[0].pit)
13475 // return false
13476 break
13477 }
13478 if (s2.notes[i2].acc != s1.notes[i1].acc)
13479 return false
13480 if (sh1 < s1.notes[i1].shhd)
13481 sh1 = s1.notes[i1].shhd
13482 if (sh2 < s2.notes[i2].shhd)
13483 sh2 = s2.notes[i2].shhd
13484 } while (s2.notes[i2].pit == s1.notes[i1].pit)
13485 //fixme:dots
13486 if (i1 <= s1.nhd) {
13487 if (i2 <= s2.nhd)
13488 return false
13489 if (s2.stem > 0)
13490 return false
13491 } else if (i2 <= s2.nhd) {
13492 if (s1.stem > 0)
13493 return false
13494 }
13495 i12 = i1;
13496 i22 = i2;
13497
13498 head = 0
13499 if (l1 != l2) {
13500 if (l1 < l2) {
13501 l1 = l2;
13502 l2 = s1.dur
13503 }
13504 if (l1 < C.BLEN / 2) {
13505 if (s2.dots)
13506 head = 2
13507 else if (s1.dots)
13508 head = 1
13509 } else if (l2 < C.BLEN / 4) { /* (l1 >= C.BLEN / 2) */
13510 // if (shu == 2)
13511 // || s1.dots != s2.dots)
13512 if (shu & 2)
13513 return false
13514 head = s2.dur >= C.BLEN / 2 ? 2 : 1
13515 } else {
13516 return false
13517 }
13518 }
13519 if (!head)
13520 head = s1.p_v.scale < s2.p_v.scale ? 2 : 1
13521 if (head == 1) {
13522 for (i2 = i21; i2 < i22; i2++) {
13523 s2.notes[i2].invis = true
13524 delete s2.notes[i2].acc
13525 }
13526 for (i2 = 0; i2 <= s2.nhd; i2++)
13527 s2.notes[i2].shhd += sh1
13528 } else {
13529 for (i1 = i11; i1 < i12; i1++) {
13530 s1.notes[i1].invis = true
13531 delete s1.notes[i1].acc
13532 }
13533 for (i1 = 0; i1 <= s1.nhd; i1++)
13534 s1.notes[i1].shhd += sh2
13535 }
13536 if (s1.dots == s2.dots) // let the dots in one voice
13537 s2.dots = 0
13538 return true
13539 }
13540
13541 /* handle unison with different accidentals */
13542 function unison_acc(s1, s2, i1, i2) {
13543 var m, d, acc
13544
13545 acc = s2.notes[i2].acc
13546 if (!acc) {
13547 d = w_note[s2.head] * 2 + s2.xmx + s1.notes[i1].shac + 2
13548 acc = s1.notes[i1].acc
13549 if (typeof acc == "object") // microtone
13550 d += 2
13551 if (s2.dots)
13552 d += 6
13553 for (m = 0; m <= s1.nhd; m++) {
13554 s1.notes[m].shhd += d;
13555 s1.notes[m].shac -= d
13556 }
13557 s1.xmx += d
13558 } else {
13559 d = w_note[s1.head] * 2 + s1.xmx + s2.notes[i2].shac + 2
13560 if (typeof acc == "object") // microtone
13561 d += 2
13562 if (s1.dots)
13563 d += 6
13564 for (m = 0; m <= s2.nhd; m++) {
13565 s2.notes[m].shhd += d;
13566 s2.notes[m].shac -= d
13567 }
13568 s2.xmx += d
13569 if (s1.notes[i1].acc)
13570 s1.notes[i1].shac -= 7
13571 //fixme: why not dx_tb[s2.head] ?
13572 }
13573 }
13574
13575 var MAXPIT = 48 * 2
13576
13577 /* set the left space of a note/chord */
13578 function set_left(s) {
13579 var m, i, j, shift,
13580 w_base = w_note[s.head],
13581 w = w_base,
13582 left = []
13583
13584 for (i = 0; i < MAXPIT; i++)
13585 left.push(-100)
13586
13587 /* stem */
13588 if (s.nflags > -2) {
13589 if (s.stem > 0) {
13590 w = -w;
13591 i = s.notes[0].pit * 2;
13592 j = (Math.ceil((s.ymx - 2) / 3) + 18) * 2
13593 } else {
13594 i = (Math.ceil((s.ymn + 2) / 3) + 18) * 2;
13595 j = s.notes[s.nhd].pit * 2
13596 }
13597 if (i < 0)
13598 i = 0
13599 if (j >= MAXPIT)
13600 j = MAXPIT - 1
13601 while (i <= j)
13602 left[i++] = w
13603 }
13604
13605 /* notes */
13606 shift = s.notes[s.stem > 0 ? 0 : s.nhd].shhd; /* previous shift */
13607 for (m = 0; m <= s.nhd; m++) {
13608 w = -s.notes[m].shhd + w_base + shift;
13609 i = s.notes[m].pit * 2
13610 if (i < 0)
13611 i = 0
13612 else if (i >= MAXPIT - 1)
13613 i = MAXPIT - 2
13614 if (w > left[i])
13615 left[i] = w
13616 if (s.head != C.SQUARE)
13617 w -= 1
13618 if (w > left[i - 1])
13619 left[i - 1] = w
13620 if (w > left[i + 1])
13621 left[i + 1] = w
13622 }
13623
13624 return left
13625 }
13626
13627 /* set the right space of a note/chord */
13628 function set_right(s) {
13629 var m, i, j, k, shift,
13630 w_base = w_note[s.head],
13631 w = w_base,
13632 flags = s.nflags > 0 && s.beam_st && s.beam_end,
13633 right = []
13634
13635 for (i = 0; i < MAXPIT; i++)
13636 right.push(-100)
13637
13638 /* stem and flags */
13639 if (s.nflags > -2) {
13640 if (s.stem < 0) {
13641 w = -w;
13642 i = (Math.ceil((s.ymn + 2) / 3) + 18) * 2;
13643 j = s.notes[s.nhd].pit * 2;
13644 k = i + 4
13645 } else {
13646 i = s.notes[0].pit * 2;
13647 j = (Math.ceil((s.ymx - 2) / 3) + 18) * 2
13648 }
13649 if (i < 0)
13650 i = 0
13651 if (j > MAXPIT)
13652 j = MAXPIT
13653 while (i < j)
13654 right[i++] = w
13655 }
13656
13657 if (flags) {
13658 if (s.stem > 0) {
13659 if (s.xmx == 0)
13660 i = s.notes[s.nhd].pit * 2
13661 else
13662 i = s.notes[0].pit * 2;
13663 i += 4
13664 if (i < 0)
13665 i = 0
13666 for (; i < MAXPIT && i <= j - 4; i++)
13667 right[i] = 11
13668 } else {
13669 i = k
13670 if (i < 0)
13671 i = 0
13672 for (; i < MAXPIT && i <= s.notes[0].pit * 2 - 4; i++)
13673 right[i] = 3.5
13674 }
13675 }
13676
13677 /* notes */
13678 shift = s.notes[s.stem > 0 ? 0 : s.nhd].shhd /* previous shift */
13679 for (m = 0; m <= s.nhd; m++) {
13680 w = s.notes[m].shhd + w_base - shift;
13681 i = s.notes[m].pit * 2
13682 if (i < 0)
13683 i = 0
13684 else if (i >= MAXPIT - 1)
13685 i = MAXPIT - 2
13686 if (w > right[i])
13687 right[i] = w
13688 if (s.head != C.SQUARE)
13689 w -= 1
13690 if (w > right[i - 1])
13691 right[i - 1] = w
13692 if (w > right[i + 1])
13693 right[i + 1] = w
13694 }
13695
13696 return right
13697 }
13698
13699 /* -- shift the notes horizontally when voices overlap -- */
13700 /* this routine is called only once per tune */
13701 function set_overlap() {
13702 var s, s1, s2, s3, i, i1, i2, m, sd, t, dp,
13703 d, d2, dr, dr2, dx,
13704 left1, right1, left2, right2, right3, pl, pr,
13705 sy = cur_sy
13706
13707 // invert the voices
13708 function v_invert() {
13709 s1 = s2;
13710 s2 = s;
13711 d = d2;
13712 pl = left1;
13713 pr = right1;
13714 dr2 = dr
13715 }
13716
13717 for (s = tsfirst; s; s = s.ts_next) {
13718 if (s.type != C.NOTE
13719 || s.invis) {
13720 if (s.type == C.STAVES)
13721 sy = s.sy
13722 continue
13723 }
13724
13725 // set the dot vertical offset of secondary voices
13726 if (s.second)
13727 s.dot_low = 1 //true
13728
13729 /* treat the stem on two staves with different directions */
13730 if (s.xstem
13731 && s.ts_prev.stem < 0) {
13732 for (m = 0; m <= s.nhd; m++) {
13733 s.notes[m].shhd -= 7; // stem_xoff
13734 s.notes[m].shac += 16
13735 }
13736 }
13737
13738 /* search the next note at the same time on the same staff */
13739 s2 = s
13740 while (1) {
13741 s2 = s2.ts_next
13742 if (!s2)
13743 break
13744 if (s2.time != s.time) {
13745 s2 = null
13746 break
13747 }
13748 if (s2.type == C.NOTE
13749 && !s2.invis
13750 && s2.st == s.st)
13751 break
13752 }
13753 if (!s2)
13754 continue
13755 s1 = s
13756
13757 /* no shift if no overlap */
13758 if (s1.ymn > s2.ymx
13759 || s1.ymx < s2.ymn)
13760 continue
13761
13762 if (same_head(s1, s2))
13763 continue
13764
13765 // special case when only a second and no dots
13766 if (!s1.dots && !s2.dots)
13767 if ((s1.stem > 0 && s2.stem < 0
13768 && s1.notes[0].pit == s2.notes[s2.nhd].pit + 1)
13769 || (s1.stem < 0 && s2.stem > 0
13770 && s1.notes[s1.nhd].pit + 1 == s2.notes[0].pit)) {
13771 if (s1.stem < 0) {
13772 s1 = s2;
13773 s2 = s
13774 }
13775 d = s1.notes[0].shhd + 7
13776 for (m = 0; m <= s2.nhd; m++) // shift the lower note(s)
13777 s2.notes[m].shhd += d
13778 s2.xmx += d
13779 s1.xmx = s2.xmx // align the dots
13780 continue
13781 }
13782
13783 /* compute the minimum space for 's1 s2' and 's2 s1' */
13784 right1 = set_right(s1);
13785 left2 = set_left(s2);
13786
13787 s3 = s1.ts_prev
13788 if (s3 && s3.time == s1.time
13789 && s3.st == s1.st && s3.type == C.NOTE && !s3.invis) {
13790 right3 = set_right(s3)
13791 for (i = 0; i < MAXPIT; i++) {
13792 if (right3[i] > right1[i])
13793 right1[i] = right3[i]
13794 }
13795 } else {
13796 s3 = null
13797 }
13798 d = -10
13799 for (i = 0; i < MAXPIT; i++) {
13800 if (left2[i] + right1[i] > d)
13801 d = left2[i] + right1[i]
13802 }
13803
13804 if (d < -3 // no clash if no dots clash
13805 && ((s2.notes[0].pit & 1)
13806 || !(s1.dots || s2.dots)
13807 || (!(s1.notes[s1.nhd].pit == s2.notes[0].pit + 2
13808 && s1.dot_low)
13809 && !(s1.notes[s1.nhd].pit + 2 == s2.notes[0].pit
13810 && s2.dot_low))))
13811 continue
13812
13813 right2 = set_right(s2);
13814 left1 = set_left(s1)
13815 if (s3) {
13816 right3 = set_left(s3)
13817 for (i = 0; i < MAXPIT; i++) {
13818 if (right3[i] > left1[i])
13819 left1[i] = right3[i]
13820 }
13821 }
13822 d2 = dr = dr2 = -100
13823 for (i = 0; i < MAXPIT; i++) {
13824 if (left1[i] + right2[i] > d2)
13825 d2 = left1[i] + right2[i]
13826 if (right2[i] > dr2)
13827 dr2 = right2[i]
13828 if (right1[i] > dr)
13829 dr = right1[i]
13830 }
13831
13832 /* check for unison with different accidentals
13833 * and clash of dots */
13834 t = 0;
13835 i1 = s1.nhd;
13836 i2 = s2.nhd
13837 while (1) {
13838 dp = s1.notes[i1].pit - s2.notes[i2].pit
13839 switch (dp) {
13840 case 2:
13841 if (!(s1.notes[i1].pit & 1))
13842 s1.dot_low = false
13843 break
13844 case 1:
13845 if (s1.notes[i1].pit & 1)
13846 s2.dot_low = true
13847 else
13848 s1.dot_low = false
13849 break
13850 case 0:
13851 if (s1.notes[i1].acc != s2.notes[i2].acc) {
13852 t = -1
13853 break
13854 }
13855 if (s2.notes[i2].acc) {
13856 if (!s1.notes[i1].acc)
13857 s1.notes[i1].acc = s2.notes[i2].acc
13858 s2.notes[i2].acc = 0
13859 }
13860 if (s1.dots && s2.dots
13861 && (s1.notes[i1].pit & 1))
13862 t = 1
13863 break
13864 case -1:
13865 if (s1.notes[i1].pit & 1)
13866 s2.dot_low = false
13867 else
13868 s1.dot_low = true
13869 break
13870 case -2:
13871 if (!(s1.notes[i1].pit & 1))
13872 s2.dot_low = false
13873 break
13874 }
13875 if (t < 0)
13876 break
13877 if (dp >= 0) {
13878 if (--i1 < 0)
13879 break
13880 }
13881 if (dp <= 0) {
13882 if (--i2 < 0)
13883 break
13884 }
13885 }
13886
13887 if (t < 0) { /* unison and different accidentals */
13888 unison_acc(s1, s2, i1, i2)
13889 continue
13890 }
13891
13892 sd = 0;
13893 if (s1.dots) {
13894 if (!s2.dots
13895 || !t) // if no dot clash
13896 sd = 1 // align the dots
13897 } else if (s2.dots) {
13898 if (d2 + dr < d + dr2)
13899 sd = 1 /* align the dots */
13900 }
13901 pl = left2;
13902 pr = right2
13903 if (!s3 && d2 + dr < d + dr2)
13904 v_invert()
13905 d += 3
13906 if (d < 0)
13907 d = 0; // (not return!)
13908
13909 /* handle the previous shift */
13910 m = s1.stem >= 0 ? 0 : s1.nhd;
13911 d += s1.notes[m].shhd;
13912 m = s2.stem >= 0 ? 0 : s2.nhd;
13913 d -= s2.notes[m].shhd
13914
13915 /*
13916 * room for the dots
13917 * - if the dots of v1 don't shift, adjust the shift of v2
13918 * - otherwise, align the dots and shift them if clash
13919 */
13920 if (s1.dots) {
13921 dx = 7.7 + s1.xmx + // x 1st dot
13922 3.5 * s1.dots - 3.5 + // x last dot
13923 3; // some space
13924 if (!sd) {
13925 d2 = -100;
13926 for (i1 = 0; i1 <= s1.nhd; i1++) {
13927 i = s1.notes[i1].pit
13928 if (!(i & 1)) {
13929 if (!s1.dot_low)
13930 i++
13931 else
13932 i--
13933 }
13934 i *= 2
13935 if (i < 1)
13936 i = 1
13937 else if (i >= MAXPIT - 1)
13938 i = MAXPIT - 2
13939 if (pl[i] > d2)
13940 d2 = pl[i]
13941 if (pl[i - 1] + 1 > d2)
13942 d2 = pl[i - 1] + 1
13943 if (pl[i + 1] + 1 > d2)
13944 d2 = pl[i + 1] + 1
13945 }
13946 if (dx + d2 + 2 > d)
13947 d = dx + d2 + 2
13948 } else {
13949 if (dx < d + dr2 + s2.xmx) {
13950 d2 = 0
13951 for (i1 = 0; i1 <= s1.nhd; i1++) {
13952 i = s1.notes[i1].pit
13953 if (!(i & 1)) {
13954 if (!s1.dot_low)
13955 i++
13956 else
13957 i--
13958 }
13959 i *= 2
13960 if (i < 1)
13961 i = 1
13962 else if (i >= MAXPIT - 1)
13963 i = MAXPIT - 2
13964 if (pr[i] > d2)
13965 d2 = pr[i]
13966 if (pr[i - 1] + 1 > d2)
13967 d2 = pr[i - 1] = 1
13968 if (pr[i + 1] + 1 > d2)
13969 d2 = pr[i + 1] + 1
13970 }
13971 if (d2 > 4.5
13972 && 7.7 + s1.xmx + 2 < d + d2 + s2.xmx)
13973 s2.xmx = d2 + 3 - 7.7
13974 }
13975 }
13976 }
13977
13978 for (m = s2.nhd; m >= 0; m--) {
13979 s2.notes[m].shhd += d
13980 // if (s2.notes[m].acc
13981 // && s2.notes[m].pit < s1.notes[0].pit - 4)
13982 // s2.notes[m].shac -= d
13983 }
13984 s2.xmx += d
13985 if (sd)
13986 s1.xmx = s2.xmx // align the dots
13987 }
13988 }
13989
13990 /* -- set the stem height -- */
13991 /* this routine is called only once per tune */
13992 // (possible hook)
13993 Abc.prototype.set_stems = function() {
13994 var s, s2, g, slen, scale,ymn, ymx, nflags, ymin, ymax
13995
13996 for (s = tsfirst; s; s = s.ts_next) {
13997 if (s.type != C.NOTE) {
13998 if (s.type != C.GRACE)
13999 continue
14000 ymin = ymax = s.mid
14001 for (g = s.extra; g; g = g.next) {
14002 slen = GSTEM
14003 if (g.nflags > 1)
14004 slen += 1.2 * (g.nflags - 1);
14005 ymn = 3 * (g.notes[0].pit - 18);
14006 ymx = 3 * (g.notes[g.nhd].pit - 18)
14007 if (s.stem >= 0) {
14008 g.y = ymn;
14009 g.ys = ymx + slen;
14010 ymx = Math.round(g.ys)
14011 } else {
14012 g.y = ymx;
14013 g.ys = ymn - slen;
14014 ymn = Math.round(g.ys)
14015 }
14016 ymx += 4
14017 ymn -= 4
14018 if (ymn < ymin)
14019 ymin = ymn
14020 else if (ymx > ymax)
14021 ymax = ymx;
14022 g.ymx = ymx;
14023 g.ymn = ymn
14024 }
14025 s.ymx = ymax;
14026 s.ymn = ymin
14027 continue
14028 }
14029
14030 /* shift notes in chords (need stem direction to do this) */
14031 set_head_shift(s);
14032
14033 /* if start or end of beam, adjust the number of flags
14034 * with the other end */
14035 nflags = s.nflags
14036 if (s.beam_st && !s.beam_end) {
14037 if (s.feathered_beam)
14038 nflags = ++s.nflags
14039 for (s2 = s.next; /*s2*/; s2 = s2.next) {
14040 if (s2.type == C.NOTE) {
14041 if (s.feathered_beam)
14042 s2.nflags++
14043 if (s2.beam_end)
14044 break
14045 }
14046 }
14047 /* if (s2) */
14048 if (s2.nflags > nflags)
14049 nflags = s2.nflags
14050 } else if (!s.beam_st && s.beam_end) {
14051 //fixme: keep the start of beam ?
14052 for (s2 = s.prev; /*s2*/; s2 = s2.prev) {
14053 if (s2.beam_st)
14054 break
14055 }
14056 /* if (s2) */
14057 if (s2.nflags > nflags)
14058 nflags = s2.nflags
14059 }
14060
14061 /* set height of stem end */
14062 slen = s.fmt.stemheight
14063 switch (nflags) {
14064 // case 2: slen += 0; break
14065 case 3: slen += 4; break
14066 case 4: slen += 8; break
14067 case 5: slen += 12; break
14068 }
14069 if ((scale = s.p_v.scale) != 1)
14070 slen *= (scale + 1) * .5;
14071 ymn = 3 * (s.notes[0].pit - 18)
14072 if (s.nhd > 0) {
14073 slen -= 2;
14074 ymx = 3 * (s.notes[s.nhd].pit - 18)
14075 } else {
14076 ymx = ymn
14077 }
14078 if (s.ntrem)
14079 slen += 2 * s.ntrem /* tremolo */
14080 if (s.decstm != null) { // if deco on the stem
14081 if (nflags <= 0) {
14082 if (slen < s.decstm + 6)
14083 slen = s.decstm + 6
14084 } else {
14085 var t = nflags * 4 // beams
14086
14087 if (s.beam_st & s.beam_end)
14088 t += 2 // flags
14089 if (slen < s.decstm + 4 + t)
14090 slen = s.decstm + 4 + t
14091 }
14092 }
14093 if (s.stemless) {
14094 if (s.stem >= 0) {
14095 s.y = ymn;
14096 s.ys = ymx
14097 } else {
14098 s.ys = ymn;
14099 s.y = ymx
14100 }
14101 s.ymx = ymx + 4;
14102 s.ymn = ymn - 4
14103 } else if (s.stem >= 0) {
14104 if (s.notes[s.nhd].pit > 26
14105 && (nflags <= 0
14106 || !s.beam_st
14107 || !s.beam_end)) {
14108 slen -= 2
14109 if (s.notes[s.nhd].pit > 28)
14110 slen -= 2
14111 }
14112 s.y = ymn
14113 if (s.notes[0].tie)
14114 ymn -= 3;
14115 s.ymn = ymn - 4;
14116 s.ys = ymx + slen
14117 if (s.ys < s.mid)
14118 s.ys = s.mid;
14119 s.ymx = (s.ys + 2.5) | 0
14120 } else { /* stem down */
14121 if (s.notes[0].pit < 18
14122 && (nflags <= 0
14123 || !s.beam_st || !s.beam_end)) {
14124 slen -= 2
14125 if (s.notes[0].pit < 16)
14126 slen -= 2
14127 }
14128 s.ys = ymn - slen
14129 if (s.ys > s.mid)
14130 s.ys = s.mid;
14131 s.ymn = (s.ys - 2.5) | 0;
14132 s.y = ymx
14133 /*fixme:the tie may be lower*/
14134 if (s.notes[s.nhd].tie)
14135 ymx += 3;
14136 s.ymx = ymx + 4
14137 }
14138 }
14139 }
14140
14141 // generate a block symbol
14142 var blocks = [] // array of delayed block symbols
14143
14144 // (possible hook)
14145 Abc.prototype.block_gen = function(s) {
14146 switch (s.subtype) {
14147 case "leftmargin":
14148 case "rightmargin":
14149 case "pagescale":
14150 case "pagewidth":
14151 case "scale":
14152 case "staffwidth":
14153 self.set_format(s.subtype, s.param)
14154 break
14155 case "mc_start": // multicol start
14156 if (multicol) {
14157 error(1, s, "No end of the previous %%multicol")
14158 break
14159 }
14160 multicol = {
14161 state: parse.state,
14162 posy: posy,
14163 maxy: posy,
14164 lm: cfmt.leftmargin,
14165 rm: cfmt.rightmargin,
14166 w: cfmt.pagewidth,
14167 sc: cfmt.scale
14168 }
14169 break
14170 case "mc_new": // multicol new
14171 if (!multicol || multicol.state != parse.state) {
14172 error(1, s, "%%multicol new without start")
14173 break
14174 }
14175 if (posy > multicol.maxy)
14176 multicol.maxy = posy
14177 cfmt.leftmargin = multicol.lm
14178 cfmt.rightmargin = multicol.rm
14179 cfmt.pagewidth = multicol.w
14180 cfmt.scale = multicol.sc
14181 posy = multicol.posy
14182 img.chg = 1 //true
14183 break
14184 case "mc_end": // multicol end
14185 if (!multicol || multicol.state != parse.state) {
14186 error(1, s, "%%multicol end without start")
14187 break
14188 }
14189 if (posy < multicol.maxy)
14190 posy = multicol.maxy
14191 cfmt.leftmargin = multicol.lm
14192 cfmt.rightmargin = multicol.rm
14193 cfmt.pagewidth = multicol.w
14194 cfmt.scale = multicol.sc
14195 multicol = undefined
14196 blk_flush()
14197 img.chg = 1 //true
14198 break
14199 case "ml":
14200 blk_flush()
14201 user.img_out(s.text)
14202 break
14203 case "newpage":
14204 if (!user.page_format)
14205 break
14206 blk_flush()
14207 if (blkdiv < 0) // split the tune
14208 user.img_out('</div>')
14209 blkdiv = 2 // start the next SVG in a new page
14210 break
14211 case "sep":
14212 set_page();
14213 vskip(s.sk1);
14214 output += '<path class="stroke"\n\td="M';
14215 out_sxsy((img.width -s.l) / 2 - img.lm, ' ', 0)
14216 output += 'h' + s.l.toFixed(1) + '"/>\n';
14217 vskip(s.sk2);
14218 break
14219 case "text":
14220 set_font(s.font)
14221 use_font(s.font)
14222 write_text(s.text, s.opt)
14223 break
14224 case "title":
14225 write_title(s.text, true)
14226 break
14227 case "vskip":
14228 vskip(s.sk);
14229 break
14230 }
14231 }
14232
14233 /* -- define the start and end of a piece of tune -- */
14234 /* tsnext becomes the beginning of the next line */
14235 function set_piece() {
14236 var s, last, p_voice, st, v, nv, tmp, non_empty,
14237 non_empty_gl = [],
14238 sy = cur_sy
14239
14240 function reset_staff(st) {
14241 var p_staff = staff_tb[st],
14242 sy_staff = sy.staves[st]
14243
14244 if (!p_staff)
14245 p_staff = staff_tb[st] = {}
14246 p_staff.y = 0; // staff system not computed yet
14247 p_staff.stafflines = sy_staff.stafflines;
14248 p_staff.staffscale = sy_staff.staffscale;
14249 p_staff.ann_top = p_staff.ann_bot = 0
14250 } // reset_staff()
14251
14252 // adjust the empty flag of brace systems
14253 function set_brace() {
14254 var st, i, empty_fl,
14255 n = sy.staves.length
14256
14257 // if a system brace has empty and non empty staves, keep all staves
14258 for (st = 0; st < n; st++) {
14259 if (!(sy.staves[st].flags & (OPEN_BRACE | OPEN_BRACE2)))
14260 continue
14261 empty_fl = 0;
14262 i = st
14263 while (st < n) {
14264 empty_fl |= non_empty[st] ? 1 : 2
14265 if (sy.staves[st].flags & (CLOSE_BRACE | CLOSE_BRACE2))
14266 break
14267 st++
14268 }
14269 if (empty_fl == 3) { // if both empty and not empty staves
14270 while (i <= st) {
14271 non_empty[i] = true;
14272 non_empty_gl[i++] = true
14273 }
14274 }
14275 }
14276 } // set_brace()
14277
14278 // set the top and bottom of the staves
14279 function set_top_bot() {
14280 var st, p_staff, i, l
14281
14282 for (st = 0; st <= nstaff; st++) {
14283 p_staff = staff_tb[st]
14284
14285 // ledger lines
14286 // index = line number
14287 // values = [x symbol, x start, x stop]
14288 p_staff.hlu = [] // above the staff
14289 p_staff.hld = [] // under the staff
14290
14291 l = p_staff.stafflines.length;
14292 p_staff.topbar = 6 * (l - 1)
14293
14294 for (i = 0; i < l - 1; i++) {
14295 switch (p_staff.stafflines[i]) {
14296 case '.':
14297 case '-':
14298 continue
14299 }
14300 break
14301 }
14302 p_staff.botbar = i * 6
14303 if (i >= l - 2) { // 0, 1 or 2 lines
14304 if (p_staff.stafflines[i] != '.') {
14305 p_staff.botbar -= 6;
14306 p_staff.topbar += 6
14307 } else { // no line: big bar
14308 p_staff.botbar -= 12;
14309 p_staff.topbar += 12
14310 continue // no helper line
14311 }
14312 }
14313 if (!non_empty_gl[st])
14314 continue
14315 }
14316 } // set_top_bot()
14317
14318 // remove the staff system at start of line
14319 if (tsfirst.type == C.STAVES) {
14320 s = tsfirst
14321 tsfirst = tsfirst.ts_next
14322 tsfirst.ts_prev = null
14323 if (s.seqst)
14324 tsfirst.seqst = true
14325 s.p_v.sym = s.next
14326 if (s.next)
14327 s.next.prev = null
14328 }
14329
14330 /* reset the staves */
14331 nstaff = sy.nstaff
14332 for (st = 0; st <= nstaff; st++)
14333 reset_staff(st);
14334 non_empty = new Uint8Array(nstaff + 1)
14335
14336 /*
14337 * search the next end of line,
14338 * and mark the empty staves
14339 */
14340 for (s = tsfirst; s; s = s.ts_next) {
14341 if (s.nl)
14342 break
14343 switch (s.type) {
14344 case C.STAVES:
14345 set_brace();
14346 sy.st_print = non_empty
14347 sy = s.sy;
14348 while (nstaff < sy.nstaff)
14349 reset_staff(++nstaff)
14350 non_empty = new Uint8Array(nstaff + 1)
14351 continue
14352
14353 // the block symbols will be treated after music line generation
14354 case C.BLOCK:
14355 if (!s.play) {
14356 blocks.push(s)
14357 unlksym(s)
14358 } else if (s.ts_next && s.ts_next.shrink)
14359 s.ts_next.shrink = 0
14360 continue
14361 }
14362 st = s.st
14363 if (st > nstaff) {
14364 switch (s.type) {
14365 case C.CLEF:
14366 staff_tb[st].clef = s // clef warning/change for new staff
14367 break
14368 case C.KEY:
14369 s.p_v.ckey = s
14370 break
14371 //useless ?
14372 case C.METER:
14373 s.p_v.meter = s
14374 break
14375 }
14376 unlksym(s)
14377 continue
14378 }
14379 if (non_empty[st])
14380 continue
14381 switch (s.type) {
14382 default:
14383 continue
14384 case C.BAR:
14385 if (s.bar_mrep
14386 || sy.staves[st].staffnonote > 1)
14387 break
14388 continue
14389 case C.GRACE:
14390 break
14391 case C.NOTE:
14392 case C.REST:
14393 case C.SPACE:
14394 case C.MREST:
14395 if (sy.staves[st].staffnonote > 1)
14396 break
14397 if (s.invis)
14398 continue
14399 if (sy.staves[st].staffnonote
14400 || s.type == C.NOTE)
14401 break
14402 continue
14403 }
14404 non_empty_gl[st] = non_empty[st] = true
14405 }
14406 tsnext = s;
14407
14408 /* set the last empty staves */
14409 set_brace()
14410 sy.st_print = non_empty
14411
14412 /* define the offsets of the measure bars */
14413 set_top_bot()
14414
14415 // if not the end of the tune, set the end of the music line
14416 if (tsnext) {
14417 s = tsnext;
14418 delete s.nl;
14419 last = s.ts_prev;
14420 last.ts_next = null;
14421
14422 // and the end of the voices
14423 nv = voice_tb.length
14424 for (v = 0; v < nv; v++) {
14425 p_voice = voice_tb[v]
14426 if (p_voice.sym
14427 && p_voice.sym.time <= tsnext.time) {
14428 for (s = last; s; s = s.ts_prev) {
14429 if (s.v == v) {
14430 p_voice.s_next = s.next;
14431 s.next = null;
14432 break
14433 }
14434 }
14435 if (s)
14436 continue
14437 }
14438 p_voice.s_next = p_voice.sym;
14439 p_voice.sym = null
14440 }
14441 }
14442
14443 // initialize the music line
14444 init_music_line()
14445
14446 // keep the array of the staves to be printed
14447 gene.st_print = non_empty_gl
14448 }
14449
14450 /* -- position the symbols along the staff -- */
14451 // (possible hook)
14452 Abc.prototype.set_sym_glue = function(width) {
14453 var g, x, some_grace, stretch,
14454 cnt = 4,
14455 xmin = 0, // sigma shrink = minimum spacing
14456 xx = 0, // sigma natural spacing
14457 xs = 0, // sigma unexpandable elements with no space
14458 xse = 0, // sigma unexpandable elements with space
14459 ll = !tsnext || // last line? yes
14460 (tsnext.type == C.BLOCK // no, but followed by %%command
14461 && !tsnext.play)
14462 || blocks.length, // (abcm2ps compatibility)
14463 s = tsfirst,
14464 spf = 1, // spacing factor
14465 xx0 = 0
14466
14467 /* calculate the whole space of the symbols */
14468 for ( ; s; s = s.ts_next) {
14469 if (s.type == C.GRACE && !some_grace)
14470 some_grace = s
14471 if (s.seqst) {
14472 xmin += s.shrink
14473 if (xmin > width) {
14474 if (cfmt.singleline)
14475 width = xmin
14476 else
14477 error(1, s, "Line too much shrunk $1 $2 $3",
14478 xmin.toFixed(1),
14479 xx.toFixed(1),
14480 width.toFixed(1))
14481 // break
14482 }
14483 if (s.space) {
14484 if (s.space < s.shrink) {
14485 xse += s.shrink;
14486 xx += s.shrink
14487 } else {
14488 // xx += s.space * spf + s.shrink * (1 - spf)
14489 xx += s.space
14490 xx0 += s.shrink
14491 }
14492 } else {
14493 xs += s.shrink
14494 }
14495 }
14496 }
14497
14498 // can occur when bar alone in a staff system
14499 if (!xx) {
14500 realwidth = 0
14501 return
14502 }
14503
14504 // stretch or not?
14505 s = tsfirst
14506
14507 if (ll) {
14508 if ((xx - xx0 + xs) / width > (1 - s.fmt.stretchlast))
14509 stretch = 1 //true
14510 } else if (s.fmt.stretchstaff) {
14511 stretch = 1 //true
14512 }
14513
14514 // strong shrink
14515 if (xmin >= width) {
14516 x = 0
14517 for ( ; s; s = s.ts_next) {
14518 if (s.seqst)
14519 x += s.shrink;
14520 s.x = x
14521 }
14522 // realwidth = width
14523 spf_last = .65
14524 } else {
14525 if (stretch) {
14526 if (xx == xse) // if no space
14527 xx += 10
14528 spf = (width - xs - xse) / (xx - xse)
14529 } else {
14530 spf = spf_last
14531 if (ll && spf < s.fmt.stretchlast)
14532 spf = s.fmt.stretchlast
14533 else if (!ll)
14534 spf = 1 - cfmt.maxshrink * .75
14535 if (spf > (width - xs) / xx)
14536 spf = (width - xs) / xx
14537 }
14538 while (--cnt >= 0) {
14539 xx = 0;
14540 xse = 0;
14541 x = 0
14542 for (s = tsfirst; s; s = s.ts_next) {
14543 if (s.seqst) {
14544 if (s.space) {
14545 if (s.space * spf <= s.shrink) {
14546 xse += s.shrink;
14547 xx += s.shrink;
14548 x += s.shrink
14549 } else {
14550 xx += s.space;
14551 x += s.space * spf
14552 }
14553 } else {
14554 x += s.shrink
14555 }
14556 }
14557 s.x = x
14558 }
14559 if (!stretch && x < width)
14560 break
14561 if (Math.abs(x - width) < 0.1)
14562 break
14563 if (xx == xse) // if no space
14564 xx += 10
14565 spf = (width - xs - xse) / (xx - xse)
14566 }
14567 spf_last = spf
14568 }
14569 realwidth = x
14570
14571 /* set the x offsets of the grace notes */
14572 for (s = some_grace; s; s = s.ts_next) {
14573 if (s.type != C.GRACE)
14574 continue
14575 if (s.gr_shift)
14576 x = s.prev.x + s.prev.wr
14577 else
14578 x = s.x - s.wl
14579 for (g = s.extra; g; g = g.next)
14580 g.x += x
14581 }
14582 }
14583
14584 // set the starting symbols of the voices for the new music line
14585 function set_sym_line() {
14586 var p_v, s,
14587 v = voice_tb.length
14588
14589 // set the first symbol of each voice
14590 while (--v >= 0) {
14591 p_v = voice_tb[v]
14592 if (p_v.sym && p_v.s_prev) {
14593 p_v.sym.prev = p_v.s_prev
14594 p_v.s_prev.next = p_v.sym
14595 }
14596 s = p_v.s_next // (set in set_piece)
14597 p_v.s_next = null
14598 p_v.sym = s
14599 if (s) {
14600 if (s.prev)
14601 s.prev.next = s
14602 p_v.s_prev = s.prev // (save for play)
14603 s.prev = null
14604 } else {
14605 p_v.s_prev = null
14606 }
14607 }
14608 }
14609
14610 // set the left offset the images
14611 function set_posx() {
14612 posx = img.lm / cfmt.scale
14613 }
14614
14615 // initialize the start of generation / new music line
14616 // and output the inter-staff blocks if any
14617 function gen_init() {
14618 var s = tsfirst,
14619 tim = s.time
14620
14621 for ( ; s; s = s.ts_next) {
14622 if (s.time != tim) {
14623 set_page()
14624 return
14625 }
14626 switch (s.type) {
14627 case C.NOTE:
14628 case C.REST:
14629 case C.MREST:
14630 case C.SPACE:
14631 set_page()
14632 return
14633 default:
14634 continue
14635 case C.STAVES:
14636 cur_sy = s.sy
14637 // break
14638 continue
14639 case C.BLOCK:
14640 if (s.play)
14641 continue // keep for play
14642 self.block_gen(s)
14643 break
14644 }
14645 unlksym(s)
14646 if (s.p_v.s_next == s)
14647 s.p_v.s_next = s.next
14648 }
14649 tsfirst = null /* no more notes */
14650 }
14651
14652 /* -- generate the music -- */
14653 // (possible hook)
14654 Abc.prototype.output_music = function() {
14655 var v, lwidth, indent, lsh, line_height, ts1st, tslast, p_v, meter1,
14656 nv = voice_tb.length
14657
14658 set_global()
14659 if (nv > 1) // if many voices
14660 self.set_stem_dir() // set the stems direction in 'multi'
14661
14662 for (v = 0; v < nv; v++)
14663 set_beams(voice_tb[v].sym); /* decide on beams */
14664
14665 self.set_stems() // set the stem lengths
14666
14667 set_acc_shft() // set the horizontal offset of accidentals
14668 if (nv > 1) { // if many voices
14669 set_rest_offset(); /* set the vertical offset of rests */
14670 set_overlap(); /* shift the notes on voice overlap */
14671 }
14672 set_allsymwidth(1) // set the width of all symbols
14673
14674 // output the blocks and define the page layout
14675 gen_init()
14676 if (!tsfirst)
14677 return
14678
14679 lsh = get_lshift()
14680
14681 /* if single line, adjust the page width */
14682 if (cfmt.singleline) {
14683 v = get_ck_width();
14684 lwidth = lsh[0] + v[0] + v[1] + get_width(tsfirst, null)[0]
14685 v = cfmt.singleline == 2 // if as wide as the page width
14686 ? get_lwidth() : lwidth
14687 if (v > lwidth)
14688 lwidth = v
14689 else
14690 img.width = lwidth * cfmt.scale + img.lm + img.rm + 2
14691 } else {
14692
14693 /* else, split the tune into music lines */
14694 lwidth = get_lwidth();
14695 cut_tune(lwidth, lsh)
14696 }
14697
14698 // save symbol pointers for play
14699 ts1st = tsfirst
14700 v = nv
14701 while (--v >= 0)
14702 voice_tb[v].osym = voice_tb[v].sym
14703 meter1 = ts1st.p_v.meter
14704
14705 spf_last = .65 // last spacing factor
14706 while (1) { /* loop per music line */
14707 set_piece();
14708 indent = set_indent(lsh)
14709 if (!line_height
14710 && cfmt.indent
14711 && indent < cfmt.indent)
14712 indent = cfmt.indent
14713 self.set_sym_glue(lwidth - indent)
14714 if (realwidth) {
14715 if (img.wx < realwidth)
14716 img.wx = realwidth
14717 if (indent) {
14718 img.wx += indent
14719 posx += indent
14720 }
14721 draw_sym_near(); // delayed output
14722 line_height = set_staff();
14723 if (line_height) { // if some music
14724 draw_systems(indent);
14725 draw_all_sym();
14726 delayed_update();
14727 vskip(line_height)
14728 }
14729 if (indent)
14730 posx -= indent;
14731 }
14732
14733 blk_flush()
14734 while (blocks.length)
14735 self.block_gen(blocks.shift())
14736 if (tslast)
14737 tslast.ts_next.ts_prev = tslast
14738 if (!tsnext)
14739 break
14740 tsnext.ts_prev.ts_next = // (restore for play)
14741 tsfirst = tsnext
14742
14743 // next line
14744 gen_init()
14745 if (!tsfirst)
14746 break
14747 tslast = tsfirst.ts_prev
14748 tsfirst.ts_prev = null;
14749 set_sym_line();
14750 lwidth = get_lwidth() // the image size may have changed
14751 }
14752
14753 // restore for play
14754 //--fixme: no, good links, but playback crashes!!!
14755 // tsfirst = ts1st.ts_next // skip staves
14756 tsfirst = ts1st
14757 v = nv
14758 while (--v >= 0) {
14759 p_v = voice_tb[v]
14760 if (p_v.sym && p_v.s_prev)
14761 p_v.sym.prev = p_v.s_prev
14762 p_v.sym = p_v.osym
14763 }
14764 ts1st.p_v.meter = meter1
14765 }
14766 // abc2svg - parse.js - ABC parse
14767 //
14768 // Copyright (C) 2014-2025 Jean-Francois Moine
14769 //
14770 // This file is part of abc2svg-core.
14771 //
14772 // abc2svg-core is free software: you can redistribute it and/or modify
14773 // it under the terms of the GNU Lesser General Public License as published by
14774 // the Free Software Foundation, either version 3 of the License, or
14775 // (at your option) any later version.
14776 //
14777 // abc2svg-core is distributed in the hope that it will be useful,
14778 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14779 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14780 // GNU Lesser General Public License for more details.
14781 //
14782 // You should have received a copy of the GNU Lesser General Public License
14783 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
14784
14785 var a_gch, // array of parsed guitar chords
14786 a_dcn = [], // array of parsed decoration names
14787 multicol, // multi column object
14788 maps = {} // maps object - see set_map()
14789 var qplet_tb = new Int8Array([ 0, 1, 3, 2, 3, 0, 2, 0, 3, 0 ]),
14790 ntb = "CDEFGABcdefgab"
14791
14792 // set the source references of a symbol
14793 function set_ref(s) {
14794 s.fname = parse.fname;
14795 s.istart = parse.istart;
14796 s.iend = parse.iend
14797 }
14798
14799 // -- %% pseudo-comment
14800
14801 // clef definition (%%clef, K: and V:)
14802 function new_clef(clef_def) {
14803 var s = {
14804 type: C.CLEF,
14805 clef_line: 2,
14806 clef_type: "t",
14807 v: curvoice.v,
14808 p_v: curvoice,
14809 time: curvoice.time,
14810 dur: 0,
14811 clef_small : 1 //true
14812 },
14813 i = 1
14814
14815 set_ref(s)
14816
14817 switch (clef_def[0]) {
14818 case '"':
14819 i = clef_def.indexOf('"', 1);
14820 s.clef_name = clef_def.slice(1, i);
14821 i++
14822 break
14823 case 'a':
14824 if (clef_def[1] == 'u') { // auto
14825 s.clef_type = "a";
14826 s.clef_auto = true;
14827 i = 4
14828 break
14829 }
14830 i = 4 // alto
14831 case 'C':
14832 s.clef_type = "c";
14833 s.clef_line = 3
14834 break
14835 case 'b': // bass
14836 i = 4
14837 case 'F':
14838 s.clef_type = "b";
14839 s.clef_line = 4
14840 break
14841 case 'n': // none
14842 i = 4
14843 s.invis = true
14844 s.clef_none = 1 //true
14845 break
14846 case 't':
14847 if (clef_def[1] == 'e') { // tenor
14848 s.clef_type = "c";
14849 s.clef_line = 4
14850 break
14851 }
14852 i = 6
14853 case 'G':
14854 // s.clef_type = "t" // treble
14855 break
14856 case 'p':
14857 i = 4
14858 case 'P': // perc
14859 s.clef_type = "p";
14860 s.clef_line = 3;
14861 break
14862 default:
14863 syntax(1, "Unknown clef '$1'", clef_def)
14864 return //undefined
14865 }
14866 if (clef_def[i] >= '1' && clef_def[i] <= '9') {
14867 s.clef_line = +clef_def[i]
14868 i++
14869 }
14870
14871 // handle the octave (+/-8 - ^/_8)
14872 delete curvoice.snd_oct
14873 if (clef_def[i + 1] != '8'
14874 && clef_def[i + 1] != '1')
14875 return s
14876 switch (clef_def[i]) { // octave
14877 case '^':
14878 s.clef_oct_transp = true
14879 case '+':
14880 s.clef_octave = clef_def[i + 1] == '8' ? 7 : 14
14881 if (!s.clef_oct_transp) // MIDI higher octave
14882 curvoice.snd_oct = clef_def[i + 1] == 8 ? 12 : 24
14883 break
14884 case '_':
14885 s.clef_oct_transp = true
14886 case '-':
14887 s.clef_octave = clef_def[i + 1] == '8' ? -7 : -14
14888 if (!s.clef_oct_transp) // MIDI lower octave
14889 curvoice.snd_oct = clef_def[i + 1] == 8 ? -12 : -24
14890 break
14891 }
14892 return s
14893 }
14894
14895 // convert an interval to a base-40 interval
14896 function get_interval(param, score) {
14897 var i, val, tmp, note, pit
14898
14899 tmp = new scanBuf;
14900 tmp.buffer = param
14901 pit = []
14902 for (i = 0; i < 2; i++) {
14903 note = tmp.buffer[tmp.index] ? parse_acc_pit(tmp) : null
14904 if (!note) {
14905 if (i != 1 || !score) {
14906 syntax(1, errs.bad_transp)
14907 return
14908 }
14909 pit[i] = 242 // 'c' (C5)
14910 } else {
14911 if (typeof note.acc == "object") {
14912 syntax(1, errs.bad_transp)
14913 return
14914 }
14915 pit[i] = abc2svg.pab40(note.pit, note.acc)
14916 }
14917 }
14918 return pit[1] - pit[0]
14919 }
14920
14921 // transpose a note for the score
14922 // Return the transposed real accidental
14923 function nt_trans(nt,
14924 a) { // real accidental
14925 var ak, an, d, b40, n
14926
14927 if (typeof a == "object") { // if microtonal accidental
14928 n = a[0] // numerator
14929 d = a[1] // denominator
14930 a = n > 0 ? 1 : -1 // base accidental for transpose
14931 }
14932
14933 b40 = abc2svg.pab40(nt.pit, a)
14934 + curvoice.tr_sco // base-40 transposition
14935
14936 nt.pit = abc2svg.b40p(b40) // new pitch
14937 an = abc2svg.b40a(b40) // new accidental
14938
14939 if (!d) { // if not a microtonal accidental
14940 if (an == -3) // if triple sharp/flat
14941 return an
14942 if (a && !an)
14943 an = 3 // needed for %%map
14944 a = an
14945 if (!nt.acc // if no old accidental
14946 && !curvoice.ckey.k_none) // and normal key
14947 a = 0 // no accidental
14948 nt.acc = a
14949 return an
14950 }
14951
14952 // set the microtonal accidental after transposition
14953 switch (an) {
14954 case -2:
14955 if (n > 0)
14956 n -= d * 2
14957 else
14958 n -= d
14959 break
14960 case -1:
14961 if (n > 0)
14962 n -= d
14963 break
14964 case 0:
14965 case 3:
14966 if (n > 0)
14967 n -= d
14968 else
14969 n += d
14970 break
14971 case 1:
14972 if (n < 0)
14973 n += d
14974 break
14975 case 2:
14976 if (n < 0)
14977 n += d * 2
14978 else
14979 n += d
14980 break
14981 }
14982 nt.acc = [n, d]
14983 return an
14984 } // nt_trans()
14985
14986 // set the linebreak character
14987 function set_linebreak(param) {
14988 var i, item
14989
14990 for (i = 0; i < 128; i++) {
14991 if (char_tb[i] == "\n")
14992 char_tb[i] = nil // remove old definition
14993 }
14994 param = param.split(/\s+/)
14995 for (i = 0; i < param.length; i++) {
14996 item = param[i]
14997 switch (item) {
14998 case '!':
14999 case '$':
15000 case '*':
15001 case ';':
15002 case '?':
15003 case '@':
15004 break
15005 case "<none>":
15006 continue
15007 case "<EOL>":
15008 item = '\n'
15009 break
15010 default:
15011 syntax(1, "Bad value '$1' in %%linebreak - ignored",
15012 item)
15013 continue
15014 }
15015 char_tb[item.charCodeAt(0)] = '\n'
15016 }
15017 }
15018
15019 // set a new user character (U: or %%user)
15020 function set_user(parm) {
15021 var k, c, v,
15022 a = parm.match(/(.)[=\s]*(\[I:.+\]|".+"|!.+!)$/)
15023
15024 if (!a) {
15025 syntax(1, 'Lack of starting [, ! or " in U: / %%user')
15026 return
15027 }
15028 c = a[1];
15029 v = a[2]
15030 if (c[0] == '\\') {
15031 if (c[1] == 't')
15032 c = '\t'
15033 else if (!c[1])
15034 c = ' '
15035 }
15036
15037 k = c.charCodeAt(0)
15038 if (k >= 128) {
15039 syntax(1, errs.not_ascii)
15040 return
15041 }
15042 switch (char_tb[k][0]) {
15043 case '0': // nil
15044 case 'd':
15045 case 'i':
15046 case ' ':
15047 break
15048 case '"':
15049 case '!':
15050 case '[':
15051 if (char_tb[k].length > 1)
15052 break
15053 // fall thru
15054 default:
15055 syntax(1, "Bad user character '$1'", c)
15056 return
15057 }
15058 switch (v) {
15059 case "!beambreak!":
15060 v = " "
15061 break
15062 case "!ignore!":
15063 v = "i"
15064 break
15065 case "!nil!":
15066 case "!none!":
15067 v = "d"
15068 break
15069 }
15070 char_tb[k] = v
15071 }
15072
15073 // get a stafflines value
15074 function get_st_lines(param) {
15075 if (!param)
15076 return
15077 if (/^[\]\[|.':-]+$/.test(param)) // '
15078 return param.replace(/\]/g, '[')
15079
15080 var n = +param
15081 switch (n) {
15082 case 0: return "..."
15083 case 1: return "..|"
15084 case 2: return ".||"
15085 case 3: return ".|||"
15086 }
15087 if (isNaN(n) || n < 0 || n > 16)
15088 return //undefined
15089 return "||||||||||||||||".slice(0, n)
15090 }
15091
15092 // create a block symbol in the tune body
15093 function new_block(subtype) {
15094 var s = {
15095 type: C.BLOCK,
15096 subtype: subtype,
15097 dur: 0
15098 }
15099
15100 sym_link(s)
15101 return s
15102 }
15103
15104 // set the voice parameters
15105 // (possible hook)
15106 Abc.prototype.set_vp = function(a) {
15107 var s, item, pos, val, clefpit,
15108 tr_p = 0
15109
15110 while (1) {
15111 item = a.shift()
15112 if (!item)
15113 break
15114 if (item.slice(-1) == '='
15115 && !a.length) {
15116 syntax(1, errs.bad_val, item)
15117 break
15118 }
15119 switch (item) {
15120 case "clef=":
15121 s = a.shift() // keep last clef
15122 break
15123 case "clefpitch=":
15124 item = a.shift() // (<note><octave>)
15125 if (item) {
15126 val = ntb.indexOf(item[0])
15127 if (val >= 0) {
15128 switch (item[1]) {
15129 case "'":
15130 val += 7
15131 break
15132 case ',':
15133 val -= 7
15134 if (item[2] == ',')
15135 val -= 7
15136 break
15137 }
15138 clefpit = 4 - val // 4 = 'G'
15139 break
15140 }
15141 }
15142 syntax(1, errs.bad_val, item)
15143 break
15144 case "octave=":
15145 val = +a.shift()
15146 if (isNaN(val))
15147 syntax(1, errs.bad_val, item)
15148 else
15149 curvoice.octave = val
15150 break
15151 case "cue=":
15152 // (ignore cue=off)
15153 // curvoice.scale = a.shift() == 'on' ? .7 : 1
15154 if (a.shift() == 'on')
15155 curvoice.scale = .7
15156 break
15157 case "instrument=":
15158
15159 // instrument=M/N => score=MN and sound=cN
15160 // (instrument=M == instrument=M/M)
15161 item = a.shift()
15162 val = item.indexOf('/')
15163 if (val < 0) {
15164 val = get_interval('c' + item)
15165 if (val == undefined)
15166 break
15167 curvoice.sound = val
15168 tr_p |= 2
15169 val = 0
15170 } else {
15171 val = get_interval('c' + item.slice(val + 1))
15172 if (val == undefined)
15173 break
15174 curvoice.sound = val
15175 tr_p |= 2
15176 val = get_interval(item.replace('/', ''))
15177 if (val == undefined)
15178 break
15179 }
15180 curvoice.score = cfmt.sound ? curvoice.sound : val
15181 tr_p |= 1
15182 break
15183 case "map=": // %%voicemap
15184 curvoice.map = a.shift()
15185 break
15186 case "name=":
15187 case "nm=":
15188 curvoice.nm = a.shift()
15189 if (curvoice.nm[0] == '"')
15190 curvoice.nm = cnv_escape(curvoice.nm.slice(1, -1))
15191 curvoice.new_name = true
15192 break
15193 case "stem=": // compatibility
15194 case "pos=": // from %%pos only
15195 if (item == "pos=")
15196 item = a.shift()
15197 .slice(1, -1) // always inside dble quotes
15198 .split(' ')
15199 else
15200 item = ["stm", a.shift()];
15201 val = posval[item[1]]
15202 if (val == undefined) {
15203 syntax(1, errs.bad_val, "%%pos")
15204 break
15205 }
15206 switch (item[2]) {
15207 case "align": val |= C.SL_ALIGN; break
15208 case "center": val |= C.SL_CENTER; break
15209 case "close": val |= C.SL_CLOSE; break
15210 }
15211 if (!pos)
15212 pos = {}
15213 pos[item[0]] = val
15214 break
15215 case "scale=": // %%voicescale
15216 val = +a.shift()
15217 if (isNaN(val) || val < .5 || val > 2)
15218 syntax(1, errs.bad_val, "%%voicescale")
15219 else
15220 curvoice.scale = val
15221 break
15222 case "score=":
15223 if (cfmt.nedo) {
15224 syntax(1, errs.notransp)
15225 break
15226 }
15227 // score=MN
15228 // (score=M == score=Mc)
15229 item = a.shift()
15230 if (cfmt.sound)
15231 break
15232 val = get_interval(item, true)
15233 if (val != undefined) {
15234 curvoice.score = val
15235 tr_p |= 1
15236 }
15237 break
15238 case "shift=":
15239 if (cfmt.nedo) {
15240 syntax(1, errs.notransp)
15241 break
15242 }
15243 val = get_interval(a.shift())
15244 if (val != undefined) {
15245 curvoice.shift = val
15246 tr_p = 3
15247 }
15248 break
15249 case "sound=":
15250 if (cfmt.nedo) {
15251 syntax(1, errs.notransp)
15252 break
15253 }
15254 // concert-score display: apply sound=
15255 // sounding-score display: apply sound= only if M != c/C
15256 // sound: apply sound=
15257 val = get_interval(a.shift())
15258 if (val == undefined)
15259 break
15260 curvoice.sound = val
15261 if (cfmt.sound)
15262 curvoice.score = val
15263 tr_p |= 2
15264 break
15265 case "subname=":
15266 case "sname=":
15267 case "snm=":
15268 curvoice.snm = a.shift()
15269 if (curvoice.snm[0] == '"')
15270 curvoice.snm = curvoice.snm.slice(1, -1);
15271 break
15272 case "stafflines=":
15273 val = get_st_lines(a.shift())
15274 if (val == undefined) {
15275 syntax(1, "Bad %%stafflines value")
15276 break
15277 }
15278 if (curvoice.st != undefined)
15279 par_sy.staves[curvoice.st].stafflines = val
15280 curvoice.stafflines = val
15281 break
15282 case "staffnonote=":
15283 val = +a.shift()
15284 if (isNaN(val))
15285 syntax(1, "Bad %%staffnonote value")
15286 else
15287 curvoice.staffnonote = val
15288 break
15289 case "staffscale=":
15290 val = +a.shift()
15291 if (isNaN(val) || val < .3 || val > 2)
15292 syntax(1, "Bad %%staffscale value")
15293 else
15294 curvoice.staffscale = val
15295 break
15296 case "tacet=":
15297 val = a.shift()
15298 curvoice.tacet = val || undefined
15299 break
15300 case "transpose=": // (abcMIDI compatibility)
15301 val = get_transp(a.shift())
15302 if (val == undefined) {
15303 syntax(1, errs.bad_transp)
15304 } else {
15305 curvoice.sound = val
15306 if (cfmt.sound)
15307 curvoice.score = val
15308 tr_p = 2
15309 }
15310 break
15311 default:
15312 switch (item.slice(0, 4)) {
15313 case "treb":
15314 case "bass":
15315 case "alto":
15316 case "teno":
15317 case "perc":
15318 s = item
15319 break
15320 default:
15321 if ("GFC".indexOf(item[0]) >= 0)
15322 s = item
15323 else if (item.slice(-1) == '=')
15324 a.shift()
15325 break
15326 }
15327 break
15328 }
15329 }
15330 if (pos) {
15331 curvoice.pos = clone(curvoice.pos)
15332 for (item in pos)
15333 if (pos.hasOwnProperty(item))
15334 curvoice.pos[item] = pos[item]
15335 }
15336
15337 if (s) {
15338 s = new_clef(s)
15339 if (s) {
15340 if (clefpit)
15341 s.clefpit = clefpit
15342 get_clef(s)
15343 }
15344 }
15345
15346 // if transposition
15347 if (tr_p & 2) { // curvoice.tr_sco is set in key_trans()
15348 tr_p = (curvoice.sound | 0) + (curvoice.shift | 0)
15349 if (tr_p)
15350 curvoice.tr_snd = abc2svg.b40m(tr_p + 122) - 36
15351 // semi-tone interval
15352 else if (curvoice.tr_snd)
15353 curvoice.tr_snd = 0
15354 curvoice.tr_snd40 = tr_p // (for play chords)
15355 }
15356 } // set_vp()
15357
15358 // set the K: / V: parameters
15359 function set_kv_parm(a) { // array of items
15360 if (!curvoice.init) { // add the global parameters if not done yet
15361 curvoice.init = true
15362 if (info.V) {
15363 if (info.V[curvoice.id])
15364 a = info.V[curvoice.id].concat(a)
15365 if (info.V['*'])
15366 a = info.V['*'].concat(a)
15367 }
15368 }
15369 if (a.length)
15370 self.set_vp(a)
15371 } // set_kv_parm()
15372
15373 // memorize the K:/V: parameters
15374 function memo_kv_parm(vid, // voice ID (V:) / '*' (K:/V:*)
15375 a) { // array of items
15376 if (!a.length)
15377 return
15378 if (!info.V)
15379 info.V = {}
15380 if (info.V[vid])
15381 Array.prototype.push.apply(info.V[vid], a)
15382 else
15383 info.V[vid] = a
15384 }
15385
15386 // K: key signature
15387 // return the key and the voice/clef parameters
15388 function new_key(param) {
15389 var i, key_end, c, tmp, note,
15390 sf = "FCGDAEB".indexOf(param[0]) - 1,
15391 mode = 0,
15392 s = {
15393 type: C.KEY,
15394 dur: 0
15395 }
15396
15397 set_ref(s);
15398
15399 // tonic
15400 i = 1
15401 if (sf < -1) {
15402 switch (param[0]) {
15403 case 'H': // bagpipe
15404 key_end = true
15405 if (param[1].toLowerCase() != 'p') {
15406 syntax(1, "Unknown bagpipe-like key")
15407 break
15408 }
15409 s.k_bagpipe = param[1];
15410 sf = param[1] == 'P' ? 0 : 2;
15411 i++
15412
15413 // initialize the temperament if not done yet
15414 if (!cfmt.temper)
15415 // detune in cents for just intonation in A
15416 // (from https://patrickmclaurin.com/wordpress/?page_id=2420)
15417 // C ^C D _E E F ^F G _A A _B B
15418 // 15.3 -14.0 -2.0 -10.0 1.9 13.3 -16.0 -31.8 -12.0 0.0 11.4 3.8
15419 // but 'A' bagpipe = 480Hz => raise Math.log2(480/440)*1200 = 151
15420 cfmt.temper = new Float32Array([
15421 // 1.66, 1.37, 1.49, 1.41, 1.53, 1.63, 1.35, 1.19, 1.39, 1.51, 1.62, 1.55
15422 // C ^C D _E E F ^F G _A A _B B
15423 11.62, 12.55,
15424 1.66, 2.37, 3.49, 0,
15425 1.66, 2.37, 3.49, 4.41, 5.53, 0,
15426 3.49, 4.41, 5.53, 6.63, 7.35,
15427 4.41, 5.53, 6.63, 7.35, 8.19, 0,
15428 6.63, 7.35, 8.19, 9.39, 10.51, 0,
15429 8.19, 9.39, 10.51, 11.62, 12.55, 0,
15430 10.51, 11.62, 12.55,
15431 1.66, 1.66
15432 ])
15433 break
15434 case 'P':
15435 syntax(1, "K:P is deprecated");
15436 sf = 0;
15437 s.k_drum = true;
15438 key_end = true
15439 break
15440 case 'n': // none
15441 if (param.indexOf("none") == 0) {
15442 sf = 0;
15443 s.k_none = true;
15444 i = 4
15445 break
15446 }
15447 // fall thru
15448 default:
15449 s.k_map = []
15450 s.k_mode = 0
15451 return [s, info_split(param)]
15452 }
15453 }
15454
15455 if (!key_end) {
15456 switch (param[i]) {
15457 case '#': sf += 7; i++; break
15458 case 'b': sf -= 7; i++; break
15459 }
15460 param = param.slice(i).trim()
15461 switch (param.slice(0, 3).toLowerCase()) {
15462 default:
15463 if (param[0] != 'm'
15464 || (param[1] != ' ' && param[1] != '\t'
15465 && param[1] != '\n')) {
15466 key_end = true
15467 break
15468 }
15469 // fall thru ('m')
15470 case "aeo":
15471 case "m":
15472 case "min": sf -= 3;
15473 mode = 5
15474 break
15475 case "dor": sf -= 2;
15476 mode = 1
15477 break
15478 case "ion":
15479 case "maj": break
15480 case "loc": sf -= 5;
15481 mode = 6
15482 break
15483 case "lyd": sf += 1;
15484 mode = 3
15485 break
15486 case "mix": sf -= 1;
15487 mode = 4
15488 break
15489 case "phr": sf -= 4;
15490 mode = 2
15491 break
15492 }
15493 if (!key_end)
15494 param = param.replace(/\w+\s*/, '')
15495
15496 // [exp] accidentals
15497 if (param.indexOf("exp ") == 0) {
15498 param = param.replace(/\w+\s*/, '')
15499 if (!param)
15500 syntax(1, "No accidental after 'exp'");
15501 s.exp = 1 //true
15502 }
15503 c = param[0]
15504 if (c == '^' || c == '_' || c == '=') {
15505 s.k_a_acc = [];
15506 tmp = new scanBuf;
15507 tmp.buffer = param
15508 do {
15509 note = parse_acc_pit(tmp)
15510 if (!note)
15511 break
15512 s.k_a_acc.push(note);
15513 c = param[tmp.index]
15514 while (c == ' ')
15515 c = param[++tmp.index]
15516 } while (c == '^' || c == '_' || c == '=');
15517 param = param.slice(tmp.index)
15518 } else if (s.exp && param.indexOf("none") == 0) {
15519 sf = 0
15520 param = param.replace(/\w+\s*/, '')
15521 }
15522 }
15523
15524 if (sf < -7 || sf > 7) {
15525 syntax(1, "Key with double sharps/flats")
15526 if (sf > 7)
15527 sf -= 12
15528 else
15529 sf += 12
15530 }
15531 s.k_sf = sf;
15532
15533 // set the map of the notes with accidentals
15534 s.k_map = s.k_bagpipe && !sf
15535 ? abc2svg.keys[9] // implicit F# and C#
15536 : abc2svg.keys[sf + 7]
15537 if (s.k_a_acc) {
15538 s.k_map = Array.prototype.slice.call(s.k_map) // simple Array
15539 i = s.k_a_acc.length // (for micro-accidentals)
15540 while (--i >= 0) {
15541 note = s.k_a_acc[i]
15542 s.k_map[(note.pit + 19) % 7] = note.acc
15543 }
15544 }
15545 s.k_mode = mode
15546
15547 // key note as base-40
15548 s.k_b40 = [1,24,7,30,13,36,19, 2 ,25,8,31,14,37,20,3][sf + 7]
15549
15550 return [s, info_split(param)]
15551 }
15552
15553 // M: meter
15554 function new_meter(p) {
15555 var p_v,
15556 s = {
15557 type: C.METER,
15558 dur: 0,
15559 a_meter: []
15560 },
15561 meter = {},
15562 val, v,
15563 m1 = 0, m2,
15564 i = 0, j,
15565 wmeasure,
15566 in_parenth;
15567
15568 set_ref(s)
15569
15570 if (p.indexOf("none") == 0) {
15571 i = 4; /* no meter */
15572 wmeasure = 1; // simplify measure numbering and C.MREST conversion
15573 } else {
15574 wmeasure = 0
15575 while (i < p.length) {
15576 if (p[i] == '=')
15577 break
15578 switch (p[i]) {
15579 case 'C':
15580 meter.top = p[i++]
15581 if (!m1) {
15582 m1 = 4;
15583 m2 = 4
15584 }
15585 break
15586 case 'c':
15587 case 'o':
15588 meter.top = p[i++]
15589 if (!m1) {
15590 if (p[i - 1] == 'c') {
15591 m1 = 2;
15592 m2 = 4 // c = 2/4
15593 } else {
15594 m1 = 3;
15595 m2 = 4 // o = 3/4
15596 }
15597 switch (p[i]) {
15598 case '|':
15599 m2 /= 2 // c| = 2/2, o| = 3/2
15600 break
15601 case '.':
15602 m1 *= 3;
15603 m2 *= 2 // c. = 6/8, o. = 9/8
15604 break
15605 }
15606 }
15607 break
15608 case '.':
15609 case '|':
15610 m1 = 0;
15611 meter.top = p[i++]
15612 break
15613 case '(':
15614 if (p[i + 1] == '(') { /* "M:5/4 ((2+3)/4)" */
15615 in_parenth = true;
15616 meter.top = p[i++];
15617 s.a_meter.push(meter);
15618 meter = {}
15619 }
15620 j = i + 1
15621 while (j < p.length) {
15622 if (p[j] == ')' || p[j] == '/')
15623 break
15624 j++
15625 }
15626 if (p[j] == ')' && p[j + 1] == '/') { /* "M:5/4 (2+3)/4" */
15627 i++ /* remove the parenthesis */
15628 continue
15629 } /* "M:5 (2+3)" */
15630 /* fall thru */
15631 case ')':
15632 in_parenth = p[i] == '(';
15633 meter.top = p[i++];
15634 s.a_meter.push(meter);
15635 meter = {}
15636 continue
15637 default:
15638 if (p[i] <= '0' || p[i] > '9') {
15639 syntax(1, "Bad char '$1' in M:", p[i])
15640 return
15641 }
15642 m2 = 2; /* default when no bottom value */
15643 meter.top = p[i++]
15644 for (;;) {
15645 while (p[i] >= '0' && p[i] <= '9')
15646 meter.top += p[i++]
15647 if (p[i] == ')') {
15648 if (p[i + 1] != '/')
15649 break
15650 i++
15651 }
15652 if (p[i] == '/') {
15653 i++;
15654 if (p[i] <= '0' || p[i] > '9') {
15655 syntax(1, "Bad char '$1' in M:", p[i])
15656 return
15657 }
15658 meter.bot = p[i++]
15659 while (p[i] >= '0' && p[i] <= '9')
15660 meter.bot += p[i++]
15661 break
15662 }
15663 if (p[i] != ' ' && p[i] != '+')
15664 break
15665 if (i >= p.length
15666 || p[i + 1] == '(') /* "M:5 (2/4+3/4)" */
15667 break
15668 meter.top += p[i++]
15669 }
15670 m1 = eval(meter.top.replace(/ /g, '+'))
15671 break
15672 }
15673 if (!in_parenth) {
15674 if (meter.bot)
15675 m2 = +meter.bot
15676 wmeasure += m1 * C.BLEN / m2
15677 }
15678 s.a_meter.push(meter);
15679 meter = {}
15680 while (p[i] == ' ')
15681 i++
15682 if (p[i] == '+') {
15683 meter.top = p[i++];
15684 s.a_meter.push(meter);
15685 meter = {}
15686 }
15687 }
15688 }
15689 if (p[i] == '=') {
15690 val = p.substring(++i).match(/^(\d+)\/(\d+)$/)
15691 if (!val) {
15692 syntax(1, "Bad duration '$1' in M:", p.substring(i))
15693 return
15694 }
15695 wmeasure = C.BLEN * val[1] / val[2]
15696 }
15697 if (!wmeasure) {
15698 syntax(1, errs.bad_val, 'M:')
15699 return
15700 }
15701 s.wmeasure = wmeasure
15702
15703 if (cfmt.writefields.indexOf('M') < 0)
15704 s.a_meter = []
15705
15706 if (parse.state != 3) {
15707 info.M = p;
15708 glovar.meter = s
15709 if (parse.state) {
15710
15711 /* in the tune header, change the unit note length */
15712 if (!glovar.ulen) {
15713 if (wmeasure <= 1
15714 || wmeasure >= C.BLEN * 3 / 4)
15715 glovar.ulen = C.BLEN / 8
15716 else
15717 glovar.ulen = C.BLEN / 16
15718 }
15719 for (v = 0; v < voice_tb.length; v++) {
15720 voice_tb[v].meter = s;
15721 voice_tb[v].wmeasure = wmeasure
15722 }
15723 }
15724 } else {
15725 curvoice.wmeasure = wmeasure
15726 if (is_voice_sig())
15727 curvoice.meter = s
15728 else
15729 sym_link(s)
15730
15731 // set the meter of the overlay voices
15732 for (p_v = curvoice.voice_down; p_v; p_v = p_v.voice_down)
15733 p_v.wmeasure = wmeasure
15734 }
15735 }
15736
15737 // link P: or Q:
15738 function link_pq(s, text) {
15739 var p_v, s2
15740
15741 if (curvoice.v == par_sy.top_voice) {
15742 sym_link(s)
15743 } else if (voice_tb[par_sy.top_voice].time == s.time) {
15744 p_v = curvoice
15745 curvoice = voice_tb[par_sy.top_voice]
15746 sym_link(s)
15747 curvoice = p_v
15748 } else if (voice_tb[par_sy.top_voice].time > s.time) {
15749 p_v = voice_tb[par_sy.top_voice]
15750 for (s2 = p_v.sym; ; s2 = s2.next) {
15751 if (s2.time >= s.time) {
15752 set_ref(s)
15753 s.fmt = cfmt
15754 s.next = s2
15755 s.prev = s2.prev
15756 if (s2.prev)
15757 s.prev.next = s
15758 else
15759 p_v.sym = s
15760 s2.prev = s
15761 s.v = s2.v
15762 s.p_v = p_v
15763 s.st = p_v.st
15764 break
15765 }
15766 }
15767 } else {
15768 set_ref(s)
15769 s.fmt = cfmt
15770 if (!parse.pq_d)
15771 parse.pq_d = []
15772 parse.pq_d.push(s) // delayed insertion
15773 }
15774 if (!parse.pq)
15775 parse.pq = {}
15776 parse.pq[text] = s.time
15777 } // link_pq()
15778
15779 /* Q: tempo */
15780 function new_tempo(text) {
15781 var i, c, d, nd,
15782 txt = text, // (for info.Q)
15783 s = {
15784 type: C.TEMPO,
15785 dur: 0
15786 }
15787
15788 // get a note duration
15789 function get_nd(p) {
15790 var n, d,
15791 nd = p.match(/(\d+)\/(\d+)/)
15792
15793 if (nd) {
15794 d = +nd[2]
15795 if (d && !isNaN(d) && !(d & (d - 1))) {
15796 n = +nd[1]
15797 if (!isNaN(n))
15798 return C.BLEN * n / d
15799 }
15800 }
15801 syntax(1, "Invalid note duration $1", c)
15802 } // get_nd()
15803
15804 set_ref(s)
15805
15806 if (cfmt.writefields.indexOf('Q') < 0)
15807 s.invis = true // don't display
15808
15809 /* string before */
15810 if (text[0] == '"') {
15811 c = text.match(/"([^"]*)"/) // "
15812 if (!c) {
15813 syntax(1, "Unterminated string in Q:")
15814 return
15815 }
15816 s.tempo_str1 = c[1]
15817 text = text.slice(c[0].length).replace(/^\s+/,'')
15818 }
15819
15820 // string after
15821 if (text.slice(-1) == '"') {
15822 i = text.indexOf('"')
15823 s.tempo_str2 = text.slice(i + 1, -1)
15824 text = text.slice(0, i).replace(/\s+$/,'')
15825 }
15826
15827 /* beat */
15828 i = text.indexOf('=')
15829 if (i > 0) {
15830 d = text.slice(0, i).split(/\s+/)
15831 text = text.slice(i + 1).replace(/^\s+/,'')
15832 while (1) {
15833 c = d.shift()
15834 if (!c)
15835 break
15836 nd = get_nd(c)
15837 if (!nd)
15838 return
15839 if (!s.tempo_notes)
15840 s.tempo_notes = []
15841 s.tempo_notes.push(nd)
15842 }
15843
15844 // tempo value
15845 if (text.slice(0, 4) == "ca. ") {
15846 s.tempo_ca = 'ca. '
15847 text = text.slice(4)
15848 }
15849 i = text.indexOf('/')
15850 if (i > 0) {
15851 nd = get_nd(text)
15852 if (!nd)
15853 return
15854 s.new_beat = nd
15855 } else {
15856 s.tempo = +text
15857 if (!s.tempo || isNaN(s.tempo)) {
15858 syntax(1, "Bad tempo value")
15859 return
15860 }
15861 }
15862 }
15863
15864 if (parse.state < 2 // if in tune header
15865 || (!curvoice.time && !glovar.tempo)) {
15866 info.Q = txt
15867 glovar.tempo = s
15868 return
15869 }
15870
15871 if (!glovar.tempo)
15872 syntax(0, "No previous tempo")
15873 s.time = curvoice.time
15874 text = 'Q' + (s.tempo_str1 ? 'S' : '') + s.time
15875 // accept [Q:"text"][Q:1/4=60]
15876 if (parse.pq
15877 && parse.pq[text] == s.time)
15878 return // already seen
15879 link_pq(s, text)
15880 }
15881
15882 // treat the information fields which may embedded
15883 function do_info(info_type, text) {
15884 var s, d1, d2, a, vid, tim, v, p_v
15885
15886 // skip this line if the current voice is ignored
15887 // but keep the time related definitions
15888 if (curvoice && curvoice.ignore) {
15889 switch (info_type) {
15890 default:
15891 return
15892 case 'P':
15893 case 'Q':
15894 case 'V':
15895 break
15896 }
15897 }
15898
15899 switch (info_type) {
15900
15901 // info fields in any state
15902 case 'I':
15903 self.do_pscom(text)
15904 break
15905 case 'L':
15906 a = text.match(/^1\/(\d+)(=(\d+)\/(\d+))?$/)
15907 if (a) {
15908 d1 = +a[1]
15909 if (!d1 || (d1 & (d1 - 1)) != 0)
15910 break
15911 d1 = C.BLEN / d1
15912 if (a[2]) { // if '='
15913 d2 = +a[4]
15914 d2 = d2 ? +a[3] / d2 * C.BLEN : 0
15915 } else {
15916 d2 = d1
15917 }
15918 } else if (text == "auto") {
15919 d1 = d2 = -1
15920 }
15921 if (!d2) {
15922 syntax(1, "Bad L: value")
15923 break
15924 }
15925 if (parse.state <= 1) {
15926 glovar.ulen = d1
15927 } else {
15928 curvoice.ulen = d1;
15929 curvoice.dur_fact = d2 / d1
15930 }
15931 break
15932 case 'M':
15933 new_meter(text)
15934 break
15935 case 'U':
15936 set_user(text)
15937 break
15938
15939 // fields in tune header or tune body
15940 case 'P':
15941 if (!parse.state)
15942 break
15943 if (parse.state == 1) {
15944 info.P = text
15945 break
15946 }
15947 s = {
15948 type: C.PART,
15949 text: text,
15950 time: curvoice.time
15951 }
15952 if (info.P) {
15953 tim = parse.pq && parse.pq[text] // time of previous P: with same text
15954 if (tim == s.time)
15955 break // already seen
15956 if (tim != null) {
15957 syntax(1, "Misplaced P:") // different dates
15958 break
15959 }
15960 }
15961
15962 if (cfmt.writefields.indexOf('P') < 0)
15963 s.invis = 1 //true
15964 link_pq(s, text)
15965 break
15966 case 'Q':
15967 if (!parse.state)
15968 break
15969 new_tempo(text)
15970 break
15971 case 'V':
15972 get_voice(text)
15973 if (parse.state == 3)
15974 curvoice.ignore = !par_sy.voices[curvoice.v]
15975 break
15976
15977 // key signature at end of tune header or in tune body
15978 case 'K':
15979 if (!parse.state) // ignore if in file header
15980 break
15981 get_key(text)
15982 break
15983
15984 // info in any state
15985 case 'N':
15986 case 'R':
15987 if (!info[info_type])
15988 info[info_type] = text
15989 else
15990 info[info_type] += '\n' + text
15991 break
15992 case 'r':
15993 if (!user.keep_remark
15994 || parse.state != 3)
15995 break
15996 s = {
15997 type: C.REMARK,
15998 text: text,
15999 dur: 0
16000 }
16001 sym_link(s)
16002 break
16003 default:
16004 syntax(0, "'$1:' line ignored", info_type)
16005 break
16006 }
16007 }
16008
16009 // music line parsing functions
16010
16011 /* -- adjust the duration and time of symbols in a measure when L:auto -- */
16012 function adjust_dur(s) {
16013 var s2, time, auto_time, i, fac;
16014
16015 /* search the start of the measure */
16016 s2 = curvoice.last_sym
16017 if (!s2)
16018 return;
16019
16020 /* the bar time is correct if there are multi-rests */
16021 if (s2.type == C.MREST
16022 || s2.type == C.BAR) /* in second voice */
16023 return
16024 while (s2.type != C.BAR && s2.prev)
16025 s2 = s2.prev;
16026 time = s2.time;
16027 auto_time = curvoice.time - time
16028 fac = curvoice.wmeasure / auto_time
16029
16030 if (fac == 1)
16031 return /* already good duration */
16032
16033 for ( ; s2; s2 = s2.next) {
16034 s2.time = time
16035 if (!s2.dur || s2.grace)
16036 continue
16037 s2.dur *= fac;
16038 s2.dur_orig *= fac;
16039 time += s2.dur
16040 if (s2.type != C.NOTE && s2.type != C.REST)
16041 continue
16042 for (i = 0; i <= s2.nhd; i++)
16043 s2.notes[i].dur *= fac
16044 }
16045 curvoice.time = s.time = time
16046 }
16047
16048 /* -- parse a bar -- */
16049 function new_bar() {
16050 var s2, c, bar_type,
16051 line = parse.line,
16052 s = {
16053 type: C.BAR,
16054 fname: parse.fname,
16055 istart: parse.bol + line.index,
16056 dur: 0,
16057 multi: 0 // needed for decorations
16058 }
16059
16060 if (vover && vover.bar) // end of voice overlay
16061 get_vover('|')
16062 if (glovar.new_nbar) { // %%setbarnb
16063 s.bar_num = glovar.new_nbar;
16064 glovar.new_nbar = 0
16065 }
16066 bar_type = line.char()
16067 while (1) {
16068 c = line.next_char()
16069 switch (c) {
16070 case '|':
16071 case '[':
16072 case ']':
16073 case ':':
16074 bar_type += c
16075 continue
16076 }
16077 break
16078 }
16079 if (bar_type[0] == ':') {
16080 if (bar_type == ':') { // ":" alone
16081 bar_type = '|';
16082 s.bar_dotted = true
16083 } else {
16084 s.rbstop = 2 // right repeat with end
16085 }
16086 }
16087
16088 // set the annotations and the decorations
16089 if (a_gch)
16090 csan_add(s)
16091 if (a_dcn.length)
16092 deco_cnv(s)
16093
16094 /* if the last element is '[', it may start
16095 * a chord or an embedded header */
16096 if (bar_type.slice(-1) == '['
16097 && !(/[0-9" ]/.test(c))) { // "
16098 bar_type = bar_type.slice(0, -1);
16099 line.index--;
16100 c = '['
16101 }
16102
16103 // check if a repeat variant
16104 if (c > '0' && c <= '9') {
16105 s.text = c
16106 while (1) {
16107 c = line.next_char()
16108 if ("0123456789,.-".indexOf(c) < 0)
16109 break
16110 s.text += c
16111 }
16112 } else if (c == '"' && bar_type.slice(-1) == '[') {
16113 s.text = ""
16114 while (1) {
16115 c = line.next_char()
16116 if (!c) {
16117 syntax(1, "No end of repeat string")
16118 return
16119 }
16120 if (c == '"') {
16121 line.index++
16122 break
16123 }
16124 s.text += c
16125 }
16126 }
16127
16128 // ']' as the first character indicates a repeat bar stop
16129 if (bar_type[0] == ']') {
16130 s.rbstop = 2 // with end
16131 if (bar_type.length != 1)
16132 bar_type = bar_type.slice(1)
16133 else
16134 s.invis = true
16135 }
16136
16137 s.iend = parse.bol + line.index
16138
16139 if (s.text
16140 && bar_type.slice(-1) == '['
16141 && bar_type != '[')
16142 bar_type = bar_type.slice(0, -1)
16143
16144 // there cannot be variants on a left repeat bar
16145 if (bar_type.slice(-1) == ':') { // left repeat
16146 s.rbstop = 1 // end the bracket
16147 if (s.text) {
16148 syntax(1, "Variant ending on a left repeat bar")
16149 delete s.text
16150 }
16151 curvoice.tie_s_rep = null // no tie anymore on new variant
16152 }
16153
16154 // handle the accidentals (ties and repeat)
16155 if (s.text) {
16156 s.rbstart = s.rbstop = 2
16157 if (s.text[0] == '1') {
16158 curvoice.tie_s_rep = curvoice.tie_s
16159 if (curvoice.acc_tie)
16160 curvoice.acc_tie_rep = curvoice.acc_tie.slice()
16161 else if (curvoice.acc_tie_rep)
16162 curvoice.acc_tie_rep = null
16163 } else {
16164 curvoice.tie_s = curvoice.tie_s_rep
16165 if (curvoice.acc_tie_rep)
16166 curvoice.acc_tie = curvoice.acc_tie_rep.slice()
16167 }
16168 if (curvoice.norepbra
16169 && !curvoice.second)
16170 s.norepbra = 1 //true
16171 }
16172
16173 if (curvoice.ulen < 0) // L:auto
16174 adjust_dur(s);
16175
16176 // merge ":| |:" into "::" and other cases
16177 if ((bar_type == "[" || bar_type == "|:")
16178 && !curvoice.eoln
16179 && !s.a_gch && !s.invis) { // no annotation nor invisible
16180 s2 = curvoice.last_sym
16181
16182 // if the previous symbol is also a bar
16183 if (s2 && s2.type == C.BAR) {
16184 // && !s2.a_gch && !s2.a_dd
16185 // && !s.a_gch && !s.a_dd) {
16186
16187 // remove the invisible variant bars
16188 // when no shift is needed
16189 if ((bar_type == "["
16190 && !s2.text)
16191 || s.norepbra) {
16192 if (s.text) {
16193 s2.text = s.text
16194 if (curvoice.st && !s.norepbra
16195 && !(par_sy.staves[curvoice.st - 1]
16196 .flags & STOP_BAR))
16197 s2.xsh = 4 // volta shift
16198 }
16199 // if (s.a_gch)
16200 // s2.a_gch = s.a_gch
16201 if (s.norepbra)
16202 s2.norepbra = 1 //true
16203 if (s.rbstart)
16204 s2.rbstart = s.rbstart
16205 if (s.rbstop)
16206 s2.rbstop = s.rbstop
16207 //--fixme: pb when on next line and empty staff above
16208 return
16209 }
16210
16211 // merge back-to-back repeat bars
16212 if (bar_type == "|:") {
16213 switch (s2.bar_type) {
16214 case ":|": // :| + |: => ::
16215 s2.bar_type = "::";
16216 s2.rbstop = 2
16217 return
16218 }
16219 }
16220 }
16221 }
16222
16223 /* set some flags */
16224 switch (bar_type) {
16225 case "[":
16226 case "[]":
16227 case "[|]":
16228 s.invis = true;
16229 bar_type = s.rbstart ? "[" : "[]"
16230 break
16231 case ":|:":
16232 case ":||:":
16233 bar_type = "::"
16234 break
16235 case "||":
16236 if (cfmt["abc-version"] >= "2.2")
16237 break
16238 // fall thru - play repeat on double bar when old ABC version
16239 case "[|":
16240 case "|]":
16241 s.rbstop = 2
16242 break
16243 }
16244 s.bar_type = bar_type
16245 if (!curvoice.lyric_restart)
16246 curvoice.lyric_restart = s
16247 if (!curvoice.sym_restart)
16248 curvoice.sym_restart = s
16249
16250 sym_link(s);
16251
16252 s.st = curvoice.st /* original staff */
16253
16254 // possibly shift the volta bracket if not on the first staff
16255 if (s.text && s.st > 0 && !s.norepbra
16256 && !(par_sy.staves[s.st - 1].flags & STOP_BAR)
16257 && bar_type != '[')
16258 s.xsh = 4 // volta shift
16259
16260 if (!s.bar_dotted && !s.invis)
16261 curvoice.acc = [] // no accidental anymore
16262 }
16263
16264 // parse %%staves / %%score
16265 // return an array of [vid, flags] / null
16266 function parse_staves(p) {
16267 var v, vid,
16268 vids = {},
16269 a_vf = [],
16270 err = false,
16271 flags = 0,
16272 brace = 0,
16273 bracket = 0,
16274 parenth = 0,
16275 flags_st = 0,
16276 e,
16277 a = p.match(/[^[\]|{}()*+\s]+|[^\s]/g)
16278
16279 if (!a) {
16280 syntax(1, errs.bad_val, "%%score")
16281 return // null
16282 }
16283 while (1) {
16284 e = a.shift()
16285 if (!e)
16286 break
16287 switch (e) {
16288 case '[':
16289 if (parenth || brace + bracket >= 2) {
16290 syntax(1, errs.misplaced, '[');
16291 err = true
16292 break
16293 }
16294 flags |= brace + bracket == 0 ? OPEN_BRACKET : OPEN_BRACKET2;
16295 bracket++;
16296 flags_st <<= 8;
16297 flags_st |= OPEN_BRACKET
16298 break
16299 case '{':
16300 if (parenth || brace || bracket >= 2) {
16301 syntax(1, errs.misplaced, '{');
16302 err = true
16303 break
16304 }
16305 flags |= !bracket ? OPEN_BRACE : OPEN_BRACE2;
16306 brace++;
16307 flags_st <<= 8;
16308 flags_st |= OPEN_BRACE
16309 break
16310 case '(':
16311 if (parenth) {
16312 syntax(1, errs.misplaced, '(');
16313 err = true
16314 break
16315 }
16316 flags |= OPEN_PARENTH;
16317 parenth++;
16318 flags_st <<= 8;
16319 flags_st |= OPEN_PARENTH
16320 break
16321 case '*':
16322 if (brace && !parenth && !(flags & (OPEN_BRACE | OPEN_BRACE2)))
16323 flags |= FL_VOICE
16324 break
16325 case '+':
16326 flags |= MASTER_VOICE
16327 break
16328 case ']':
16329 case '}':
16330 case ')':
16331 syntax(1, "Bad voice ID in %%score");
16332 err = true
16333 break
16334 default: // get / create the voice in the voice table
16335 vid = e
16336 while (1) {
16337 e = a.shift()
16338 if (!e)
16339 break
16340 switch (e) {
16341 case ']':
16342 if (!(flags_st & OPEN_BRACKET)) {
16343 syntax(1, errs.misplaced, ']');
16344 err = true
16345 break
16346 }
16347 bracket--;
16348 flags |= brace + bracket == 0 ?
16349 CLOSE_BRACKET :
16350 CLOSE_BRACKET2;
16351 flags_st >>= 8
16352 continue
16353 case '}':
16354 if (!(flags_st & OPEN_BRACE)) {
16355 syntax(1, errs.misplaced, '}');
16356 err = true
16357 break
16358 }
16359 brace--;
16360 flags |= !bracket ?
16361 CLOSE_BRACE :
16362 CLOSE_BRACE2;
16363 flags &= ~FL_VOICE;
16364 flags_st >>= 8
16365 continue
16366 case ')':
16367 if (!(flags_st & OPEN_PARENTH)) {
16368 syntax(1, errs.misplaced, ')');
16369 err = true
16370 break
16371 }
16372 parenth--;
16373 flags |= CLOSE_PARENTH;
16374 flags_st >>= 8
16375 continue
16376 case '|':
16377 flags |= STOP_BAR
16378 continue
16379 }
16380 break
16381 }
16382 if (vids[vid]) {
16383 syntax(1, "Double voice in %%score")
16384 err = true
16385 } else {
16386 vids[vid] = true
16387 a_vf.push([vid, flags])
16388 }
16389 flags = 0
16390 if (!e)
16391 break
16392 a.unshift(e)
16393 break
16394 }
16395 }
16396 if (flags_st != 0) {
16397 syntax(1, "'}', ')' or ']' missing in %%score");
16398 err = true
16399 }
16400 if (err || !a_vf.length)
16401 return //null
16402 return a_vf
16403 }
16404
16405 // split an info string
16406 function info_split(text) {
16407 if (!text)
16408 return []
16409 var a = text.match(/[^\s"=]+=?|"[^"]*"/g) // "
16410 if (!a) {
16411 //fixme: bad error text
16412 syntax(1, "Unterminated string")
16413 return []
16414 }
16415 return a
16416 }
16417
16418 // parse a duration and return [numerator, denominator]
16419 // 'line' is not always 'parse.line'
16420 var reg_dur = /(\d*)(\/*)(\d*)/g /* (stop comment) */
16421
16422 function parse_dur(line) {
16423 var res, num, den;
16424
16425 reg_dur.lastIndex = line.index;
16426 res = reg_dur.exec(line.buffer)
16427 if (!res[0])
16428 return [1, 1];
16429 num = res[1] || 1;
16430 den = res[3] || 1
16431 if (!res[3])
16432 den *= 1 << res[2].length;
16433 line.index = reg_dur.lastIndex
16434 return [+num, +den]
16435 }
16436
16437 // parse the note accidental and pitch
16438 function parse_acc_pit(line) {
16439 var note, acc, pit, d, nd,
16440 c = line.char()
16441
16442 // optional accidental
16443 switch (c) {
16444 case '^':
16445 c = line.next_char()
16446 if (c == '^') {
16447 acc = 2;
16448 c = line.next_char()
16449 } else {
16450 acc = 1
16451 }
16452 break
16453 case '=':
16454 acc = 3;
16455 c = line.next_char()
16456 break
16457 case '_':
16458 c = line.next_char()
16459 if (c == '_') {
16460 acc = -2;
16461 c = line.next_char()
16462 } else {
16463 acc = -1
16464 }
16465 break
16466 }
16467
16468 /* look for microtone value */
16469 if (acc == 1 || acc == -1) {
16470 if ((c >= '1' && c <= '9')
16471 || c == '/') { // shortcut
16472 nd = parse_dur(line);
16473 if (acc < 0)
16474 nd[0] = -nd[0]
16475 if (cfmt.nedo && nd[1] == 1) {
16476 nd[0] *= 12
16477 nd[1] *= cfmt.nedo
16478 }
16479 acc = nd
16480 c = line.char()
16481 }
16482 }
16483
16484 /* get the pitch */
16485 pit = ntb.indexOf(c) + 16;
16486 c = line.next_char()
16487 if (pit < 16) {
16488 syntax(1, "'$1' is not a note", line.buffer[line.index - 1])
16489 return //undefined
16490 }
16491
16492 // octave
16493 while (c == "'") {
16494 pit += 7;
16495 c = line.next_char()
16496 }
16497 while (c == ',') {
16498 pit -= 7;
16499 c = line.next_char()
16500 }
16501 note = {
16502 pit: pit,
16503 shhd: 0,
16504 shac: 0
16505 }
16506 if (acc)
16507 note.acc = acc
16508 return note
16509 }
16510
16511 // return the mapping of a note
16512 //
16513 // The global 'maps' object is indexed by the map name.
16514 // Its content is an object ('map') indexed from the map type:
16515 // - normal = ABC note
16516 // - octave = 'o' + ABC note in C..B interval
16517 // - key = 'k' + scale index
16518 // - tonic = 't' + mode index
16519 // - any = '*'
16520 // The 'map' is stored in the note. It is an array of
16521 // [0] array of heads (glyph names)
16522 // [1] print (note)
16523 // [2] color
16524 // [3] play (note)
16525 function set_map(p_v, note, acc,
16526 trp_p) { // flag "do transpose?"
16527 var nn = not2abc(note.pit, acc),
16528 map = maps[p_v.map]
16529
16530 if (!map)
16531 return
16532
16533 // test if 'nn' is in the map
16534 function map_p() {
16535 if (map[nn])
16536 return 1 //true
16537 var sf, d
16538
16539 nn = 'o' + nn.replace(/[',]+/, '') // '
16540 if (map[nn])
16541 return 1 //true
16542 //fixme: useless
16543 d = abc2svg.keys[p_v.ckey.k_sf + 7][(note.pit + 75) % 7]
16544 d = (!d && acc == 3) ? 0 : acc
16545 nn = 'k' + ['__','_','','^','^^','='][d + 2] // key chromatic
16546 + ntb[(note.pit + 75 - p_v.ckey.k_sf * 11) % 7]
16547 if (map[nn])
16548 return 1 //true
16549 nn = nn.replace(/[_=^]/g,'') // key diatonic
16550 if (map[nn])
16551 return 1 //true
16552 sf = p_v.ckey.k_sf + [0, 2, 4, -1, 1, 3, -2][p_v.ckey.k_mode]
16553 if (sf < -7)
16554 sf += 7
16555 else if (sf > 7)
16556 sf -= 7
16557 d = abc2svg.keys[sf + 7]
16558 [(note.pit + 75) % 7]
16559 if (d && acc == 3)
16560 d = -d
16561 else if (!d && !acc)
16562 d = 3
16563 else
16564 d = acc - d
16565 nn = 't' + ['__','_','=','^','^^',''] // tonic chromatic
16566 [d + 2]
16567 + ntb[(note.pit + 75 - p_v.ckey.k_mode
16568 - p_v.ckey.k_sf * 11) % 7]
16569 if (map[nn])
16570 return 1 //true
16571 nn = nn.replace(/[_=^]/g,'') // tonic diatonic
16572 if (map[nn])
16573 return 1 //true
16574 nn = '*' // any note
16575 return map[nn]
16576 } // map_p()
16577
16578 if (!map_p()) // note in the map?
16579 return // no
16580 map = map[nn]
16581
16582 if (trp_p) {
16583 if (map[1] && map[1].notrp)
16584 note.notrp = 1 //true // no transpose
16585 return
16586 }
16587
16588 if (map[1] // if note transpose
16589 && !note.map) { // for the first time
16590 note.pit = map[1].pit
16591 note.acc = map[1].acc
16592 if (map[1].notrp) {
16593 note.notrp = 1 //true // no transpose
16594 note.noplay = 1 //true // no play
16595 }
16596 }
16597 note.map = map
16598
16599 if (map[2]) // if color
16600 note.color = map[2]
16601 nn = map[3]
16602 if (nn) // if play map
16603 note.midi = pit2mid(nn.pit + 19, nn.acc)
16604 }
16605
16606 /* -- parse note or rest with pitch and length -- */
16607 // 'line' is not always 'parse.line'
16608 function parse_basic_note(line, ulen) {
16609 var nd,
16610 note = parse_acc_pit(line)
16611
16612 if (!note)
16613 return //null
16614
16615 // duration
16616 if (line.char() == '0') { // compatibility
16617 parse.stemless = true;
16618 line.index++
16619 }
16620 nd = parse_dur(line);
16621 note.dur = ulen * nd[0] / nd[1]
16622 return note
16623 }
16624
16625 function parse_vpos() {
16626 var line = parse.line,
16627 ty = 0
16628
16629 if (a_dcn.length && a_dcn[a_dcn.length - 1] == "dot") {
16630 ty = C.SL_DOTTED
16631 a_dcn.pop()
16632 }
16633 switch (line.next_char()) {
16634 case "'":
16635 line.index++
16636 return ty + C.SL_ABOVE
16637 case ",":
16638 line.index++
16639 return ty + C.SL_BELOW
16640 case '?': // slur between staves (like ~)
16641 line.index++
16642 return ty + C.SL_CENTER
16643 }
16644 return ty + C.SL_AUTO
16645 }
16646
16647 // on end of slur, create the slur
16648 function slur_add(s, nt) { // nt = note if slur ending on note
16649 var i, s2, sl
16650
16651 // go back and find the last start of slur
16652 for (i = curvoice.sls.length; --i >= 0; ) {
16653 sl = curvoice.sls[i]
16654
16655 // the slur must not start and stop on a same symbol
16656 if (sl.ss == s)
16657 continue
16658 curvoice.sls.splice(i, 1)
16659 sl.se = s // ending symbol
16660 if (nt)
16661 sl.nte = nt
16662 s2 = sl.ss // start of slur
16663 if (!s2.sls)
16664 s2.sls = []
16665 s2.sls.push(sl)
16666
16667 // set a flag if the slur starts on a grace note
16668 if (sl.grace)
16669 sl.grace.sl1 = true
16670 return
16671 }
16672
16673 // the lack of a starting slur may be due to a repeat
16674 for (s2 = s.prev; s2; s2 = s2.prev) {
16675 if (s2.type == C.BAR
16676 && s2.bar_type[0] == ':'
16677 && s2.text) {
16678 if (!s2.sls)
16679 s2.sls = [];
16680 s2.sls.push({
16681 //fixme: should go back to the bar "|1" and find the slur type...
16682 ty: C.SL_AUTO,
16683 ss: s2,
16684 se: s
16685 })
16686 if (nt)
16687 s2.sls[s2.sls.length - 1].nte = nt
16688 return
16689 }
16690 }
16691 // syntax(1, "End of slur without start")
16692 if (!s.sls)
16693 s.sls = [];
16694 s.sls.push({
16695 ty: C.SL_AUTO,
16696 se: s,
16697 loc: 'i' // no slur start
16698 })
16699 if (nt)
16700 s.sls[s.sls.length - 1].nte = nt
16701 }
16702
16703 // convert a diatonic pitch and accidental to a MIDI pitch with cents
16704 function pit2mid(pit, acc) {
16705 var p = [0, 2, 4, 5, 7, 9, 11][pit % 7], // chromatic pitch
16706 o = ((pit / 7) | 0) * 12, // octave
16707 p0, p1, s, b40
16708
16709 if (curvoice.snd_oct)
16710 o += curvoice.snd_oct
16711 if (acc == 3) // if natural accidental
16712 acc = 0
16713 if (acc) {
16714 if (typeof acc == "object") {
16715 s = acc[0] / acc[1] // microtonal accidental
16716 if (acc[1] == 100) // in cents
16717 return p + o + s
16718 } else {
16719 s = acc // simple accidental
16720 }
16721 } else {
16722 if (cfmt.temper)
16723 return cfmt.temper[abc2svg.p_b40[pit % 7]] + o
16724 return p + o
16725 }
16726 if (!cfmt.nedo) { // non equal temperament
16727 if (!cfmt.temper) {
16728 p += o + s // standard temperament
16729 return p
16730 }
16731 } else { // equal temperament
16732 p0 = cfmt.temper[abc2svg.p_b40[pit % 7]] // main note
16733 if (typeof acc != "object") { // if not a fraction
16734 b40 = abc2svg.p_b40[pit % 7] + acc
16735 p1 = cfmt.temper[b40]
16736 if (s > 0) { // sharp
16737 if (p1 < p0)
16738 p1 += 12
16739 } else {
16740 if (p1 > p0)
16741 p1 -= 12
16742 }
16743 return p1 + o
16744 }
16745
16746 if (acc[1] == cfmt.nedo) { // fraction with the edo divider
16747 b40 = abc2svg.p_b40[pit % 7]
16748 return cfmt.temper[b40] + o + s
16749 }
16750 }
16751
16752 p0 = cfmt.temper[abc2svg.p_b40[pit % 7]] // main note
16753 if (s > 0) { // sharp
16754 p1 = cfmt.temper[(abc2svg.p_b40[pit % 7] + 1) % 40]
16755 if (p1 < p0)
16756 p1 += 12
16757 } else { // flat
16758 p1 = cfmt.temper[(abc2svg.p_b40[pit % 7] + 39) % 40]
16759 if (p1 > p0)
16760 p1 -= 12
16761 s = -s
16762 }
16763 return p0 + o + (p1 - p0) * s
16764 } // pit2mid()
16765
16766 // handle the ties
16767 // @s = tie ending smbol
16768 // @tei_s = tie starting symbol
16769 function do_ties(s, tie_s) {
16770 var i, m, not1, not2, mid, g,
16771 nt = 0,
16772 se = (tie_s.time + tie_s.dur) == curvoice.time // 'start-end' flag
16773
16774 for (m = 0; m <= s.nhd; m++) {
16775 not2 = s.notes[m]
16776 mid = not2.midi
16777 if (tie_s.type != C.GRACE) {
16778 for (i = 0; i <= tie_s.nhd; i++) {
16779 not1 = tie_s.notes[i]
16780 if (!not1.tie_ty)
16781 continue
16782 if (not1.midi == mid
16783 && (!se
16784 || !not1.tie_e)) { // (if unison)
16785 not2.tie_s = not1
16786 not2.s = s
16787 if (se) {
16788 not1.tie_e = not2
16789 not1.s = tie_s
16790 }
16791 nt++
16792 break
16793 }
16794 }
16795 } else {
16796 for (g = tie_s.extra; g; g = g.next) {
16797 not1 = g.notes[0] // (fixme: only one note)
16798 if (!not1.tie_ty)
16799 continue
16800 if (not1.midi == mid) {
16801 g.ti1 = true
16802 not2.tie_s = not1
16803 not2.s = s
16804 not1.tie_e = not2
16805 not1.s = g
16806 nt++
16807 break
16808 }
16809 }
16810 }
16811 }
16812
16813 if (!nt)
16814 error(1, tie_s, "Bad tie")
16815 else
16816 s.ti2 = true
16817 } // do_ties()
16818
16819 // (possible hook)
16820 Abc.prototype.new_note = function(grace, sls) {
16821 var note, s, in_chord, c, tie_s, acc_tie,
16822 i, n, s2, nd, res, num, apit, div, ty,
16823 chdur = 1,
16824 dpit = 0,
16825 sl1 = [],
16826 line = parse.line,
16827 a_dcn_sav = a_dcn // save parsed decoration names
16828
16829 a_dcn = []
16830 parse.stemless = false;
16831 s = {
16832 type: C.NOTE,
16833 fname: parse.fname,
16834 stem: 0,
16835 multi: 0,
16836 nhd: 0,
16837 xmx: 0
16838 }
16839 s.istart = parse.bol + line.index
16840
16841 if (curvoice.color)
16842 s.color = curvoice.color
16843
16844 if (grace) {
16845 s.grace = true
16846 } else {
16847 if (curvoice.tie_s) { // if tie from previous note / grace note
16848 tie_s = curvoice.tie_s
16849 curvoice.tie_s = null
16850 }
16851 if (a_gch)
16852 csan_add(s)
16853 if (parse.repeat_n) {
16854 s.repeat_n = parse.repeat_n;
16855 s.repeat_k = parse.repeat_k;
16856 parse.repeat_n = 0
16857 }
16858 }
16859 c = line.char()
16860 switch (c) {
16861 case 'X':
16862 s.invis = true
16863 case 'Z':
16864 s.type = C.MREST;
16865 c = line.next_char()
16866 s.nmes = (c > '0' && c <= '9') ? line.get_int() : 1;
16867 if (curvoice.wmeasure == 1) {
16868 error(1, s, "multi-measure rest, but no measure!")
16869 return
16870 }
16871 s.dur = curvoice.wmeasure * s.nmes
16872
16873 // convert 'Z'/'Z1' to a whole measure rest
16874 if (s.nmes == 1) {
16875 s.type = C.REST;
16876 s.dur_orig = s.dur;
16877 s.fmr = 1 // full measure rest
16878 s.notes = [{
16879 pit: 18,
16880 dur: s.dur
16881 }]
16882 } else {
16883 glovar.mrest_p = true
16884 if (par_sy.voices.length == 1) {
16885 s.tacet = curvoice.tacet
16886 delete s.invis // show the 'H' when 'Xn'
16887 }
16888 }
16889 break
16890 case 'y':
16891 s.type = C.SPACE;
16892 s.invis = true;
16893 s.dur = 0;
16894 c = line.next_char()
16895 if (c >= '0' && c <= '9')
16896 s.width = line.get_int()
16897 else
16898 s.width = 10
16899 if (tie_s) {
16900 curvoice.tie_s = tie_s
16901 tie_s = null
16902 }
16903 break
16904 case 'x':
16905 s.invis = true
16906 case 'z':
16907 s.type = C.REST;
16908 line.index++;
16909 nd = parse_dur(line);
16910 s.dur_orig = ((curvoice.ulen < 0) ?
16911 C.BLEN :
16912 curvoice.ulen) * nd[0] / nd[1];
16913 if (s.dur_orig < 12) {
16914 error(0, s, "Bad note duration $1", s.dur_orig)
16915 s.dur_orig = 12
16916 }
16917 s.dur = s.dur_orig * curvoice.dur_fact;
16918 if (s.dur == curvoice.wmeasure)
16919 s.fmr = 1 // full measure rest
16920 s.notes = [{
16921 pit: 18,
16922 dur: s.dur_orig
16923 }]
16924 break
16925 case '[': // chord
16926 in_chord = true;
16927 c = line.next_char()
16928 i = line.buffer.indexOf(']', line.index)
16929 if (i < 0) {
16930 syntax(1, "No end of chord")
16931 return
16932 }
16933 n = line.index // save the parser index
16934 line.index = i + 1 // set the parser to the end of chord
16935 nd = parse_dur(line)
16936 chdur = nd[0] / nd[1] // length factor of the chord
16937 in_chord = reg_dur.lastIndex // hack: index after the chord length
16938 line.index = n // restore the parser index
16939 // fall thru
16940 default: // accidental, chord, note
16941 if (curvoice.acc_tie) {
16942 acc_tie = curvoice.acc_tie
16943 curvoice.acc_tie = null
16944 }
16945 s.notes = []
16946
16947 // loop on the chord
16948 while (1) {
16949
16950 // when in chord, get the slurs and decorations
16951 if (in_chord) {
16952 while (1) {
16953 if (!c)
16954 break
16955 i = c.charCodeAt(0);
16956 if (i >= 128) {
16957 syntax(1, errs.not_ascii)
16958 return //null
16959 }
16960 ty = char_tb[i]
16961 switch (ty[0]) {
16962 case '(':
16963 sl1.push(parse_vpos());
16964 c = line.char()
16965 continue
16966 case '!':
16967 if (ty.length > 1)
16968 a_dcn.push(ty.slice(1, -1))
16969 else
16970 get_deco() // line -> a_dcn
16971 c = line.next_char()
16972 continue
16973 }
16974 break
16975 }
16976 }
16977 note = parse_basic_note(line,
16978 s.grace ? C.BLEN / 4 :
16979 curvoice.ulen < 0 ?
16980 C.BLEN :
16981 curvoice.ulen)
16982 if (!note)
16983 return //null
16984
16985 note.dur *= chdur // chord factor
16986 if (note.dur < 12) {
16987 error(0, s, "Bad note duration $1", note.dur)
16988 note.dur = 12
16989 }
16990
16991 if (curvoice.octave)
16992 note.pit += curvoice.octave * 7
16993
16994 // get the real accidental
16995 apit = note.pit + 19 // pitch from C-1
16996 i = note.acc
16997 if (!i) {
16998 if (cfmt["propagate-accidentals"][0] == 'p')
16999 i = curvoice.acc[apit % 7]
17000 else
17001 i = curvoice.acc[apit]
17002 if (!i)
17003 i = curvoice.ckey.k_map[apit % 7] || 0
17004 }
17005
17006 if (i
17007 && !curvoice.ckey.k_drum) {
17008 if (cfmt["propagate-accidentals"][0] == 'p')
17009 curvoice.acc[apit % 7] = i
17010 else if (cfmt["propagate-accidentals"][0] != 'n')
17011 curvoice.acc[apit] = i
17012 }
17013
17014 if (acc_tie && acc_tie[apit])
17015 i = acc_tie[apit] // tied note
17016
17017 // set the MIDI pitch
17018 if (!note.midi) // if not map play
17019 note.midi = pit2mid(apit, i)
17020
17021 // transpose
17022 if (curvoice.tr_sco) {
17023 set_map(curvoice, note, i, 1) // possible transpose?
17024 if (!note.notrp) { // yes
17025 i = nt_trans(note, i)
17026 if (i == -3) { // if triple sharp/flat
17027 error(1, s, "triple sharp/flat")
17028 i = note.acc > 0 ? 1 : -1
17029 note.pit += i
17030 note.acc = i
17031 }
17032 dpit = note.pit + 19 - apit
17033 }
17034 }
17035 if (curvoice.tr_snd)
17036 note.midi += curvoice.tr_snd
17037 if (curvoice.map)
17038 set_map(curvoice, note, i)
17039
17040 //fixme: does not work if transposition
17041 if (i) {
17042 switch (cfmt["writeout-accidentals"][1]) {
17043 case 'd': // added
17044 s2 = curvoice.ckey
17045 if (!s2.k_a_acc)
17046 break
17047 for (n = 0; n < s2.k_a_acc.length; n++) {
17048 if ((s2.k_a_acc[n].pit - note.pit)
17049 % 7 == 0) {
17050 note.acc = i
17051 break
17052 }
17053 }
17054 break
17055 case 'l': // all
17056 note.acc = i
17057 break
17058 }
17059 }
17060
17061 // starting slurs
17062 if (sl1.length) {
17063 while (1) {
17064 i = sl1.shift()
17065 if (!i)
17066 break
17067 curvoice.sls.push({
17068 ty: i,
17069 ss: s,
17070 nts: note // starting note
17071 })
17072 }
17073 }
17074 s.notes.push(note)
17075 if (!in_chord)
17076 break
17077
17078 // in chord: get the ending slurs and the ties
17079 c = line.char()
17080 while (1) {
17081 switch (c) {
17082 case ')':
17083 slur_add(s, note)
17084 c = line.next_char()
17085 continue
17086 case '-':
17087 note.tie_ty = parse_vpos()
17088 note.s = s
17089 curvoice.tie_s = s
17090 s.ti1 = true
17091 if (curvoice.acc[apit]
17092 || (acc_tie
17093 && acc_tie[apit])) {
17094 if (!curvoice.acc_tie)
17095 curvoice.acc_tie = []
17096 i = curvoice.acc[apit]
17097 if (acc_tie && acc_tie[apit])
17098 i = acc_tie[apit]
17099 curvoice.acc_tie[apit] = i
17100 }
17101 c = line.char()
17102 continue
17103 case '.':
17104 c = line.next_char()
17105 switch (c) {
17106 case '-':
17107 case '(':
17108 a_dcn.push("dot")
17109 continue
17110 }
17111 syntax(1, "Misplaced dot")
17112 break
17113 }
17114 break
17115 }
17116 if (a_dcn.length) {
17117 s.time = curvoice.time // (needed for !tie)!
17118 dh_cnv(s, note)
17119 }
17120
17121 if (c == ']') {
17122 line.index = in_chord
17123 s.nhd = s.notes.length - 1
17124 break
17125 }
17126 }
17127
17128 // handle the starting slurs
17129 if (sls.length) {
17130 while (1) {
17131 i = sls.shift()
17132 if (!i)
17133 break
17134 curvoice.sls.push({
17135 ty: i,
17136 ss: s
17137 // no starting note
17138 })
17139 if (grace)
17140 curvoice.sls[curvoice.sls.length - 1].grace =
17141 grace
17142 }
17143 }
17144
17145 // the duration of the chord is the duration of the 1st note
17146 s.dur_orig = s.notes[0].dur;
17147 s.dur = s.notes[0].dur * curvoice.dur_fact
17148 break
17149 }
17150 if (s.grace && s.type != C.NOTE) {
17151 syntax(1, errs.bad_grace)
17152 return //null
17153 }
17154
17155 if (s.notes) { // if note or rest
17156 if (!s.fmr) { // (but not full measure rest)
17157 n = s.dur_orig / 12 // check its duration
17158 i = 0
17159 while (!(n & 1)) {
17160 n >>= 1
17161 i++
17162 }
17163 if ((n + 1) & n)
17164 error(0, s, "Non standard note duration $1",
17165 n + '/' + (1 << (6 - i)))
17166 }
17167 if (!grace) {
17168 switch (curvoice.pos.stm & 0x07) {
17169 case C.SL_ABOVE: s.stem = 1; break
17170 case C.SL_BELOW: s.stem = -1; break
17171 case C.SL_HIDDEN: s.stemless = true; break
17172 }
17173
17174 // adjust the symbol duration
17175 num = curvoice.brk_rhythm
17176 if (num) {
17177 curvoice.brk_rhythm = 0;
17178 s2 = curvoice.last_note
17179 if (num > 0) {
17180 n = num * 2 - 1;
17181 s.dur = s.dur * n / num;
17182 s.dur_orig = s.dur_orig * n / num
17183 for (i = 0; i <= s.nhd; i++)
17184 s.notes[i].dur =
17185 s.notes[i].dur * n / num;
17186 s2.dur /= num;
17187 s2.dur_orig /= num
17188 for (i = 0; i <= s2.nhd; i++)
17189 s2.notes[i].dur /= num
17190 } else {
17191 num = -num;
17192 n = num * 2 - 1;
17193 s.dur /= num;
17194 s.dur_orig /= num
17195 for (i = 0; i <= s.nhd; i++)
17196 s.notes[i].dur /= num;
17197 s2.dur = s2.dur * n / num;
17198 s2.dur_orig = s2.dur_orig * n / num
17199 for (i = 0; i <= s2.nhd; i++)
17200 s2.notes[i].dur =
17201 s2.notes[i].dur * n / num
17202 }
17203 curvoice.time = s2.time + s2.dur;
17204
17205 // adjust the time of the grace notes, bars...
17206 for (s2 = s2.next; s2; s2 = s2.next)
17207 s2.time = curvoice.time
17208 }
17209 } else { /* grace note - adjust its duration */
17210 div = curvoice.ckey.k_bagpipe ? 8 : 4
17211 for (i = 0; i <= s.nhd; i++)
17212 s.notes[i].dur /= div;
17213 s.dur /= div;
17214 s.dur_orig /= div
17215 if (grace.stem)
17216 s.stem = grace.stem
17217 }
17218
17219 curvoice.last_note = s
17220
17221 // get the possible ties and end of slurs
17222 c = line.char()
17223 while (1) {
17224 switch (c) {
17225 case '.':
17226 if (line.buffer[line.index + 1] != '-')
17227 break
17228 a_dcn.push("dot")
17229 line.index++
17230 // fall thru
17231 case '-':
17232 ty = parse_vpos()
17233 for (i = 0; i <= s.nhd; i++) {
17234 s.notes[i].tie_ty = ty
17235 s.notes[i].s = s
17236 }
17237 curvoice.tie_s = grace || s
17238 curvoice.tie_s.ti1 = true
17239 for (i = 0; i <= s.nhd; i++) {
17240 note = s.notes[i]
17241 apit = note.pit + 19 // pitch from C-1
17242 - dpit // (if transposition)
17243 if (curvoice.acc[apit]
17244 || (acc_tie
17245 && acc_tie[apit])) {
17246 if (!curvoice.acc_tie)
17247 curvoice.acc_tie = []
17248 n = curvoice.acc[apit]
17249 if (acc_tie && acc_tie[apit])
17250 n = acc_tie[apit]
17251 curvoice.acc_tie[apit] = n
17252 }
17253 }
17254 c = line.char()
17255 continue
17256 }
17257 break
17258 }
17259
17260 // handle the ties ending on this chord/note
17261 if (tie_s) // if tie from previous note / grace note
17262 do_ties(s, tie_s)
17263 }
17264
17265 sym_link(s)
17266
17267 if (!grace) {
17268 if (!curvoice.lyric_restart)
17269 curvoice.lyric_restart = s
17270 if (!curvoice.sym_restart)
17271 curvoice.sym_restart = s
17272 }
17273
17274 if (a_dcn_sav.length) {
17275 a_dcn = a_dcn_sav
17276 deco_cnv(s, s.prev)
17277 }
17278 if (grace && s.ottava)
17279 grace.ottava = s.ottava
17280 if (parse.stemless)
17281 s.stemless = true
17282 s.iend = parse.bol + line.index
17283 return s
17284 }
17285
17286 // adjust the duration of the elements in a tuplet
17287 function tp_adj(s, fact) {
17288 var d,
17289 tim = s.time,
17290 to = curvoice.time - tim, // previous delta time
17291 tt = to * fact // new delta time
17292
17293 curvoice.time = tim + tt
17294 while (1) {
17295 //fixme: tuplets in grace notes?
17296 s.in_tuplet = true
17297 if (!s.grace) {
17298 s.time = tim
17299 if (s.dur) {
17300 d = Math.round(s.dur * tt / to) // new duration
17301 to -= s.dur // old remaining time
17302 s.dur = d
17303 tt -= s.dur // new remaining time
17304 tim += s.dur
17305 }
17306 }
17307 if (!s.next) {
17308 if (s.tpe)
17309 s.tpe++
17310 else
17311 s.tpe = 1
17312 break
17313 }
17314 s = s.next
17315 }
17316 } // tp_adj()
17317
17318 // get a decoration
17319 function get_deco() {
17320 var c,
17321 line = parse.line,
17322 i = line.index, // in case no deco end
17323 dcn = ""
17324
17325 while (1) {
17326 c = line.next_char()
17327 if (!c) {
17328 line.index = i
17329 syntax(1, "No end of decoration")
17330 return
17331 }
17332 if (c == '!')
17333 break
17334 dcn += c
17335 }
17336 a_dcn.push(dcn)
17337 } // get_deco()
17338
17339 // characters in the music line (ASCII only)
17340 var nil = "0",
17341 char_tb = [
17342 nil, nil, nil, nil, /* 00 - .. */
17343 nil, nil, nil, nil,
17344 nil, " ", "\n", nil, /* . \t \n . */
17345 nil, nil, nil, nil,
17346 nil, nil, nil, nil,
17347 nil, nil, nil, nil,
17348 nil, nil, nil, nil,
17349 nil, nil, nil, nil, /* .. - 1f */
17350 " ", "!", '"', "i", /* (sp) ! " # */
17351 "\n", nil, "&", nil, /* $ % & ' */
17352 "(", ")", "i", nil, /* ( ) * + */
17353 nil, "-", "!dot!", nil, /* , - . / */
17354 nil, nil, nil, nil, /* 0 1 2 3 */
17355 nil, nil, nil, nil, /* 4 5 6 7 */
17356 nil, nil, "|", "i", /* 8 9 : ; */
17357 "<", "n", "<", "i", /* < = > ? */
17358 "i", "n", "n", "n", /* @ A B C */
17359 "n", "n", "n", "n", /* D E F G */
17360 "!fermata!", "d", "d", "d", /* H I J K */
17361 "!emphasis!", "!lowermordent!",
17362 "d", "!coda!", /* L M N O */
17363 "!uppermordent!", "d",
17364 "d", "!segno!", /* P Q R S */
17365 "!trill!", "d", "d", "d", /* T U V W */
17366 "n", "d", "n", "[", /* X Y Z [ */
17367 "\\","|", "n", "n", /* \ ] ^ _ */
17368 "i", "n", "n", "n", /* ` a b c */
17369 "n", "n", "n", "n", /* d e f g */
17370 "d", "d", "d", "d", /* h i j k */
17371 "d", "d", "d", "d", /* l m n o */
17372 "d", "d", "d", "d", /* p q r s */
17373 "d", "!upbow!",
17374 "!downbow!", "d", /* t u v w */
17375 "n", "n", "n", "{", /* x y z { */
17376 "|", "}", "!gmark!", nil, /* | } ~ (del) */
17377 ] // char_tb[]
17378
17379 function parse_music_line() {
17380 var grace, last_note_sav, a_dcn_sav, no_eol, s, tps,
17381 tp = [],
17382 tpn = -1,
17383 sls = [],
17384 line = parse.line
17385
17386 // check if a transposing macro matches a source sequence
17387 // if yes return the base note
17388 function check_mac(m) {
17389 var i, j, b
17390
17391 for (i = 1, j = line.index + 1; i < m.length; i++, j++) {
17392 if (m[i] == line.buffer[j])
17393 continue
17394 if (m[i] != 'n') // search the base note
17395 return //undefined
17396 b = ntb.indexOf(line.buffer[j])
17397 if (b < 0)
17398 return //undefined
17399 while (line.buffer[j + 1] == "'") {
17400 b += 7;
17401 j++
17402 }
17403 while (line.buffer[j + 1] == ',') {
17404 b -= 7;
17405 j++
17406 }
17407 }
17408 line.index = j
17409 return b
17410 } // check_mac()
17411
17412 // convert a note as a number into a note as a ABC string
17413 function n2n(n) {
17414 var c = ''
17415
17416 while (n < 0) {
17417 n += 7;
17418 c += ','
17419 }
17420 while (n >= 14) {
17421 n -= 7;
17422 c += "'"
17423 }
17424 return ntb[n] + c
17425 } // n2n()
17426
17427 // expand a transposing macro
17428 function expand(m, b) {
17429 if (b == undefined) // if static macro
17430 return m
17431 var c, i,
17432 r = "", // result
17433 n = m.length
17434
17435 for (i = 0; i < n; i++) {
17436 c = m[i]
17437 if (c >= 'h' && c <= 'z') {
17438 r += n2n(b + c.charCodeAt(0) - 'n'.charCodeAt(0))
17439 } else {
17440 r += c
17441 }
17442 }
17443 return r
17444 } // expand()
17445
17446 // parse a macro
17447 function parse_mac(k, m, b) {
17448 var te, ti, curv, s,
17449 line_sav = line,
17450 istart_sav = parse.istart;
17451
17452 parse.line = line = new scanBuf;
17453 parse.istart += line_sav.index;
17454
17455 // if the macro is not displayed
17456 if (cfmt.writefields.indexOf('m') < 0) {
17457
17458 // build the display sequence from the original sequence
17459 line.buffer = k.replace('n', n2n(b))
17460 s = curvoice.last_sym
17461 ti = curvoice.time // start time
17462 parse_seq(true)
17463 if (!s)
17464 s = curvoice.sym
17465 for (s = s.next ; s; s = s.next)
17466 s.noplay = true
17467 te = curvoice.time // end time
17468 curv = curvoice
17469
17470 // and put the macro sequence in a play specific voice
17471 curvoice = clone_voice(curv.id + '-p')
17472 if (!par_sy.voices[curvoice.v]) {
17473 curvoice.second = true
17474 par_sy.voices[curvoice.v] = {
17475 st: curv.st,
17476 second: true,
17477 range: curvoice.v
17478 }
17479 }
17480 curvoice.time = ti
17481 s = curvoice.last_sym
17482 parse.line = line = new scanBuf
17483 parse.istart += line_sav.index
17484 line.buffer = expand(m, b)
17485 parse_seq(true)
17486 if (curvoice.time != te)
17487 syntax(1, "Bad length of the macro sequence")
17488 if (!s)
17489 s = curvoice.sym
17490 for ( ; s; s = s.next)
17491 s.invis = s.play = true
17492 curvoice = curv
17493 } else {
17494 line.buffer = expand(m, b)
17495 parse_seq(true)
17496 }
17497
17498 parse.line = line = line_sav
17499 parse.istart = istart_sav
17500 } // parse_mac()
17501
17502 // parse a music sequence
17503 function parse_seq(in_mac) {
17504 var c, idx, type, k, s, dcn, i, n, text, note
17505
17506 while (1) {
17507 c = line.char()
17508 if (!c)
17509 break
17510
17511 // check if start of a macro
17512 if (!in_mac && maci[c]) {
17513 n = undefined
17514 for (k in mac) {
17515 if (!mac.hasOwnProperty(k)
17516 || k[0] != c)
17517 continue
17518 if (k.indexOf('n') < 0) {
17519 if (line.buffer.indexOf(k, line.index)
17520 != line.index)
17521 continue
17522 line.index += k.length
17523 } else {
17524 n = check_mac(k)
17525 if (n == undefined)
17526 continue
17527 }
17528 parse_mac(k, mac[k], n)
17529 n = 1
17530 break
17531 }
17532 if (n)
17533 continue
17534 }
17535
17536 idx = c.charCodeAt(0)
17537 if (idx >= 128) {
17538 syntax(1, errs.not_ascii)
17539 line.index++
17540 break
17541 }
17542
17543 type = char_tb[idx]
17544 switch (type[0]) {
17545 case ' ': // beam break
17546 s = curvoice.last_note
17547 if (s) {
17548 s.beam_end = true
17549 if (grace)
17550 grace.gr_shift = true
17551 }
17552 break
17553 case '\n': // line break
17554 if (cfmt.barsperstaff)
17555 break
17556 curvoice.eoln = true
17557 break
17558 case '&': // voice overlay
17559 if (grace) {
17560 syntax(1, errs.bad_grace)
17561 break
17562 }
17563 c = line.next_char()
17564 if (c == ')') {
17565 get_vover(c) // full overlay stop
17566 break
17567 }
17568 get_vover('&')
17569 continue
17570 case '(': // slur start - tuplet - vover
17571 c = line.next_char()
17572 if (c > '0' && c <= '9') { // tuplet
17573 if (grace) {
17574 syntax(1, errs.bad_grace)
17575 break
17576 }
17577 var pplet = line.get_int(),
17578 qplet = qplet_tb[pplet],
17579 rplet = pplet
17580
17581 c = line.char()
17582 if (c == ':') {
17583 c = line.next_char()
17584 if (c > '0' && c <= '9') {
17585 qplet = line.get_int();
17586 c = line.char()
17587 }
17588 if (c == ':') {
17589 c = line.next_char()
17590 if (c > '0' && c <= '9') {
17591 rplet = line.get_int();
17592 c = line.char()
17593 } else {
17594 syntax(1, "Invalid 'r' in tuplet")
17595 continue
17596 }
17597 }
17598 }
17599 if (qplet == 0 || qplet == undefined)
17600 qplet = (curvoice.wmeasure % 9) == 0 ?
17601 3 : 2;
17602 if (tpn < 0)
17603 tpn = tp.length // new tuplet
17604 tp.push({
17605 p: pplet,
17606 q: qplet,
17607 r: rplet,
17608 ro: rplet,
17609 f: curvoice.tup || cfmt.tuplets
17610 })
17611 continue
17612 }
17613 if (c == '&') { // voice overlay start
17614 if (grace) {
17615 syntax(1, errs.bad_grace)
17616 break
17617 }
17618 get_vover('(')
17619 break
17620 }
17621 line.index--;
17622 sls.push(parse_vpos())
17623 continue
17624 case ')': // slur end
17625 s = curvoice.last_sym
17626 if (s) {
17627 switch (s.type) {
17628 case C.SPACE:
17629 if (!s.notes) {
17630 s.notes = []
17631 s.notes[0] = {}
17632 }
17633 case C.NOTE:
17634 case C.REST:
17635 break
17636 case C.GRACE:
17637
17638 // stop the slur on the last grace note
17639 for (s = s.extra; s.next; s = s.next)
17640 ;
17641 break
17642 default:
17643 s = null
17644 break
17645 }
17646 }
17647 if (!s) {
17648 syntax(1, errs.bad_char, c)
17649 break
17650 }
17651 slur_add(s)
17652 break
17653 case '!': // start of decoration
17654 if (type.length > 1) // decoration letter
17655 a_dcn.push(type.slice(1, -1))
17656 else
17657 get_deco() // (line -> a_dcn)
17658 break
17659 case '"':
17660 if (grace) {
17661 syntax(1, errs.bad_grace)
17662 break
17663 }
17664 parse_gchord(type)
17665 break
17666 case '[':
17667 if (type.length > 1) { // U: [I:xxx]
17668 self.do_pscom(type.slice(3, -1))
17669 break
17670 }
17671 var c_next = line.buffer[line.index + 1]
17672
17673 if ('|[]: "'.indexOf(c_next) >= 0
17674 || (c_next >= '1' && c_next <= '9')) {
17675 if (grace) {
17676 syntax(1, errs.bar_grace)
17677 break
17678 }
17679 new_bar()
17680 continue
17681 }
17682 if (line.buffer[line.index + 2] == ':') {
17683 if (grace) {
17684 syntax(1, errs.bad_grace)
17685 break
17686 }
17687 i = line.buffer.indexOf(']', line.index + 1)
17688 if (i < 0) {
17689 syntax(1, "Lack of ']'")
17690 break
17691 }
17692 text = line.buffer.slice(line.index + 3, i).trim()
17693
17694 parse.istart = parse.bol + line.index;
17695 parse.iend = parse.bol + ++i;
17696 line.index = 0;
17697 do_info(c_next, text);
17698 line.index = i
17699 continue
17700 }
17701 // fall thru ('[' is start of chord)
17702 case 'n': // note/rest
17703 s = self.new_note(grace, sls)
17704 if (!s)
17705 continue
17706
17707 // handle the tuplets
17708 if (grace || !s.notes)
17709 continue
17710
17711 if (tpn >= 0) { // new tuplet
17712 s.tp = tp.slice(tpn)
17713 tpn = -1
17714 if (tps)
17715 s.tp[0].s = tps // if nested
17716 tps = s
17717 } else if (!tps) {
17718 continue // no tuplet active
17719 }
17720
17721 k = tp[tp.length - 1]
17722 if (--k.r > 0)
17723 continue // not end of tuplet yet
17724
17725 while (1) {
17726 tp_adj(tps, k.q / k.p)
17727 i = k.ro // number of notes of this tuplet
17728 if (k.s)
17729 tps = k.s // start of upper tuplet
17730
17731 tp.pop() // previous level
17732 if (!tp.length) {
17733 tps = null // done
17734 break
17735 }
17736 k = tp[tp.length - 1]
17737 k.r -= i
17738 if (k.r > 0)
17739 break
17740 }
17741 continue
17742 case '<': /* '<' and '>' */
17743 if (!curvoice.last_note) {
17744 syntax(1, "No note before '<'")
17745 break
17746 }
17747 if (grace) {
17748 syntax(1, "Cannot have a broken rhythm in grace notes")
17749 break
17750 }
17751 n = c == '<' ? 1 : -1
17752 while (c == '<' || c == '>') {
17753 n *= 2;
17754 c = line.next_char()
17755 }
17756 curvoice.brk_rhythm = n
17757 continue
17758 case 'i': // ignore
17759 break
17760 case '{':
17761 if (grace) {
17762 syntax(1, "'{' in grace note")
17763 break
17764 }
17765 last_note_sav = curvoice.last_note;
17766 curvoice.last_note = null;
17767 a_dcn_sav = a_dcn;
17768 a_dcn = []
17769 grace = {
17770 type: C.GRACE,
17771 fname: parse.fname,
17772 istart: parse.bol + line.index,
17773 dur: 0,
17774 multi: 0
17775 }
17776 if (curvoice.color)
17777 grace.color = curvoice.color
17778 switch (curvoice.pos.gst & 0x07) {
17779 case C.SL_ABOVE: grace.stem = 1; break
17780 case C.SL_BELOW: grace.stem = -1; break
17781 case C.SL_HIDDEN: grace.stem = 2; break /* opposite */
17782 }
17783 sym_link(grace);
17784 c = line.next_char()
17785 if (c == '/') {
17786 grace.sappo = true // acciaccatura
17787 break
17788 }
17789 continue
17790 case '|':
17791 if (grace) {
17792 syntax(1, errs.bar_grace)
17793 break
17794 }
17795 new_bar()
17796 continue
17797 case '}':
17798 if (curvoice.ignore) {
17799 grace = null
17800 break
17801 }
17802 s = curvoice.last_note
17803 if (!grace || !s) {
17804 syntax(1, errs.bad_char, c)
17805 break
17806 }
17807 if (a_dcn.length)
17808 syntax(1, "Decoration ignored");
17809 grace.extra = grace.next;
17810 grace.extra.prev = null;
17811 grace.next = null;
17812 curvoice.last_sym = grace;
17813 grace = null
17814 if (!s.prev // if one grace note
17815 && !curvoice.ckey.k_bagpipe) {
17816 for (i = 0; i <= s.nhd; i++)
17817 s.notes[i].dur *= 2;
17818 s.dur *= 2;
17819 s.dur_orig *= 2
17820 }
17821 curvoice.last_note = last_note_sav;
17822 a_dcn = a_dcn_sav
17823 break
17824 case "\\":
17825 if (!line.buffer[line.index + 1]) {
17826 no_eol = true
17827 break
17828 }
17829 // fall thru
17830 default:
17831 syntax(1, errs.bad_char, c)
17832 break
17833 }
17834 line.index++
17835 }
17836 } // parse_seq()
17837
17838 if (parse.state != 3) // if not in tune body
17839 return
17840
17841 if (parse.tp) {
17842 tp = parse.tp
17843 tpn = parse.tpn
17844 tps = parse.tps
17845 parse.tp = null
17846 }
17847
17848 parse_seq()
17849
17850 if (tp.length) {
17851 parse.tp = tp
17852 parse.tps = tps
17853 parse.tpn = tpn
17854 }
17855 if (sls.length)
17856 syntax(1, "Start of slur without note")
17857 if (grace) {
17858 syntax(1, "No end of grace note sequence");
17859 curvoice.last_sym = grace.prev;
17860 curvoice.last_note = last_note_sav
17861 if (grace.prev)
17862 grace.prev.next = null
17863 }
17864 if (!no_eol && !cfmt.barsperstaff && !vover
17865 && char_tb['\n'.charCodeAt(0)] == '\n')
17866 curvoice.eoln = true
17867 if (curvoice.eoln && cfmt.breakoneoln && curvoice.last_note)
17868 curvoice.last_note.beam_end = true
17869 }
17870 // abc2svg - subs.js - text output
17871 //
17872 // Copyright (C) 2014-2025 Jean-Francois Moine
17873 //
17874 // This file is part of abc2svg-core.
17875 //
17876 // abc2svg-core is free software: you can redistribute it and/or modify
17877 // it under the terms of the GNU Lesser General Public License as published by
17878 // the Free Software Foundation, either version 3 of the License, or
17879 // (at your option) any later version.
17880 //
17881 // abc2svg-core is distributed in the hope that it will be useful,
17882 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17883 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17884 // GNU Lesser General Public License for more details.
17885 //
17886 // You should have received a copy of the GNU Lesser General Public License
17887 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
17888
17889 // add font styles
17890 var sheet
17891 var add_fstyle = abc2svg.el
17892 ? function(s) {
17893 var e
17894
17895 font_style += "\n" + s
17896 if (!abc2svg.styles) {
17897 e = document.createElement('style')
17898 document.head.appendChild(e)
17899 abc2svg.styles = e
17900 }
17901 sheet = abc2svg.styles.sheet
17902 s = s.match(/[^{]+{[^}]+}/g) // insert each style
17903 while (1) {
17904 e = s.shift()
17905 if (!e)
17906 break
17907 sheet.insertRule(e, sheet.cssRules.length)
17908 }
17909 } // add_fstyle()
17910 : function(s) { font_style += "\n" + s }
17911
17912 // width of characters according to the font type
17913 // these tables were created from the font 'Liberation'
17914
17915 // serif
17916 var
17917 sw_tb = new Float32Array([
17918 .000,.000,.000,.000,.000,.000,.000,.000, // 00
17919 .000,.000,.000,.000,.000,.000,.000,.000,
17920 .000,.000,.000,.000,.000,.000,.000,.000, // 10
17921 .000,.000,.000,.000,.000,.000,.000,.000,
17922 .250,.333,.408,.500,.500,.833,.778,.333, // 20
17923 .333,.333,.500,.564,.250,.564,.250,.278,
17924 .500,.500,.500,.500,.500,.500,.500,.500, // 30
17925 .500,.500,.278,.278,.564,.564,.564,.444,
17926 .921,.722,.667,.667,.722,.611,.556,.722, // 40
17927 .722,.333,.389,.722,.611,.889,.722,.722,
17928 .556,.722,.667,.556,.611,.722,.722,.944, // 50
17929 .722,.722,.611,.333,.278,.333,.469,.500,
17930 .333,.444,.500,.444,.500,.444,.333,.500, // 60
17931 .500,.278,.278,.500,.278,.778,.500,.500,
17932 .500,.500,.333,.389,.278,.500,.500,.722, // 70
17933 .500,.500,.444,.480,.200,.480,.541,.500]),
17934 // sans-serif
17935 ssw_tb = new Float32Array([
17936 .000,.000,.000,.000,.000,.000,.000,.000, // 00
17937 .000,.000,.000,.000,.000,.000,.000,.000,
17938 .000,.000,.000,.000,.000,.000,.000,.000, // 10
17939 .000,.000,.000,.000,.000,.000,.000,.000,
17940 .278,.278,.355,.556,.556,.889,.667,.191, // 20
17941 .333,.333,.389,.584,.278,.333,.278,.278,
17942 .556,.556,.556,.556,.556,.556,.556,.556, // 30
17943 .556,.556,.278,.278,.584,.584,.584,.556,
17944 1.015,.667,.667,.722,.722,.667,.611,.778, // 40
17945 .722,.278,.500,.667,.556,.833,.722,.778,
17946 .667,.778,.722,.667,.611,.722,.667,.944, // 50
17947 .667,.667,.611,.278,.278,.278,.469,.556,
17948 .333,.556,.556,.500,.556,.556,.278,.556, // 60
17949 .556,.222,.222,.500,.222,.833,.556,.556,
17950 .556,.556,.333,.500,.278,.556,.500,.722, // 70
17951 .500,.500,.500,.334,.260,.334,.584,.512]),
17952 // monospace
17953 mw_tb = new Float32Array([
17954 .0,.0,.0,.0,.0,.0,.0,.0, // 00
17955 .0,.0,.0,.0,.0,.0,.0,.0,
17956 .0,.0,.0,.0,.0,.0,.0,.0, // 10
17957 .0,.0,.0,.0,.0,.0,.0,.0,
17958 .52,.52,.52,.52,.52,.52,.52,.52, // 20
17959 .52,.52,.52,.52,.52,.52,.52,.52,
17960 .52,.52,.52,.52,.52,.52,.52,.52, // 30
17961 .52,.52,.52,.52,.52,.52,.52,.52,
17962 .52,.52,.52,.52,.52,.52,.52,.52, // 40
17963 .52,.52,.52,.52,.52,.52,.52,.52,
17964 .52,.52,.52,.52,.52,.52,.52,.52, // 50
17965 .52,.52,.52,.52,.52,.52,.52,.52,
17966 .52,.52,.52,.52,.52,.52,.52,.52, // 60
17967 .52,.52,.52,.52,.52,.52,.52,.52,
17968 .52,.52,.52,.52,.52,.52,.52,.52, // 70
17969 .52,.52,.52,.52,.52,.52,.52,.52])
17970
17971 /* -- return the character width -- */
17972 function cwid(c, font) {
17973 var i = c.charCodeAt(0) // utf-16
17974
17975 if (i >= 0x80) { // if not ASCII
17976 if (i >= 0x300 && i < 0x370)
17977 return 0; // combining diacritical mark
17978 i = 0x61 // 'a'
17979 }
17980 return (font || gene.curfont).cw_tb[i]
17981 }
17982 // return the character width with the current font
17983 function cwidf(c) {
17984 return cwid(c) * gene.curfont.swfac
17985 }
17986
17987 // make XML clean
17988 function clean_txt(p) {
17989 return p.replace(/<|>|&[^&\s]*?;|&/g, function(c) {
17990 switch (c) {
17991 case '<': return "&lt;"
17992 case '>': return "&gt;"
17993 case '&': return "&amp;"
17994 }
17995 return c // &xxx;
17996 })
17997 } // clean_txt()
17998
17999 // estimate the width and height of a string ..
18000 var strwh
18001
18002 (function() {
18003 if (typeof document != "undefined"
18004 && abc2svg.el) {
18005
18006 // .. by the browser
18007
18008 // change the function
18009 strwh = function(str) {
18010 if (str.wh)
18011 return str.wh
18012
18013 var c,
18014 el = abc2svg.el, // hidden <span> created by edit/abcweb/...
18015 font = gene.curfont,
18016 h = font.size,
18017 w = 0,
18018 n = str.length,
18019 i0 = 0,
18020 i = 0
18021
18022 if (!el.parentElement) // insert back the <span> in the document
18023 document.body.appendChild(el)
18024 el.className = font_class(font)
18025
18026 if (typeof str == "object") { // if string already converted
18027 el.innerHTML = str
18028 str.wh = [ el.clientWidth, el.clientHeight ]
18029 return str.wh
18030 }
18031 str = clean_txt(str)
18032
18033 while (1) {
18034 i = str.indexOf('$', i)
18035 if (i >= 0) {
18036 c = str[i + 1]
18037 if (c == '0') {
18038 font = gene.deffont
18039 } else if (c >= '1' && c <= '9') {
18040 font = get_font("u" + c)
18041 } else {
18042 i++
18043 continue
18044 }
18045 el.className = font_class(font)
18046 }
18047
18048 el.innerHTML = str.slice(i0, i >= 0 ? i : undefined)
18049 w += el.clientWidth
18050 //fixme: bad width if space(s) at end of string
18051 if (el.clientHeight > h)
18052 h = el.clientHeight
18053
18054 if (i < 0)
18055 break
18056 i += 2;
18057 i0 = i
18058 }
18059 return [w, h]
18060 }
18061 } else {
18062
18063 // .. by internal tables
18064 strwh = function(str) {
18065 var font = gene.curfont,
18066 swfac = font.swfac,
18067 h = font.size,
18068 w = 0,
18069 i, j, c,
18070 n = str.length
18071
18072 for (i = 0; i < n; i++) {
18073 c = str[i]
18074 switch (c) {
18075 case '$':
18076 c = str[i + 1]
18077 if (c == '0') {
18078 font = gene.deffont
18079 } else if (c >= '1' && c <= '9') {
18080 font = get_font("u" + c)
18081 } else {
18082 c = '$'
18083 break
18084 }
18085 i++;
18086 swfac = font.swfac
18087 if (font.size > h)
18088 h = font.size
18089 continue
18090 case '&':
18091 if (str[i + 1] == ' ')
18092 break // normal '&'
18093 j = str.indexOf(';', i)
18094 if (j > 0 && j - i < 10) {
18095 i = j;
18096 c = 'a' // XML character reference
18097 }
18098 break
18099 }
18100 w += cwid(c, font) * swfac
18101 }
18102 return [w, h]
18103 }
18104 }
18105 })()
18106
18107 // convert a string to a SVG text, handling the font changes
18108 // The string size is memorized into the String.
18109 function str2svg(str) {
18110 // check if the string is already converted
18111 if (typeof str == "object")
18112 return str
18113
18114 var n_font, wh,
18115 o_font = gene.deffont,
18116 c_font = gene.curfont,
18117 o = ""
18118
18119 // start a '<tspan>' element
18120 function tspan(nf, of) {
18121 var cl
18122
18123 if (nf.class
18124 && nf.name == of.name
18125 && nf.size == of.size
18126 && nf.weight == of.weight
18127 && nf.style == of.style)
18128 cl = nf.class // change only the class
18129 else
18130 cl = font_class(nf)
18131
18132 return '<tspan\n\tclass="' + cl + '">'
18133 } // tspan()
18134
18135 if (c_font != o_font)
18136 o = tspan(c_font, o_font)
18137 o += str.replace(/<|>|&[^&\s]*?;|&|\$./g, function(c){
18138 switch (c) {
18139 case '<': return "&lt;"
18140 case '>': return "&gt;"
18141 case '&': return "&amp;"
18142 default:
18143 if (c[0] != '$')
18144 break
18145 if (c[1] == '0')
18146 n_font = gene.deffont
18147 else if (c[1] >= '1' && c[1] <= '9')
18148 n_font = get_font("u" + c[1])
18149 else
18150 break
18151 c = ''
18152 if (n_font == c_font)
18153 return c
18154 if (c_font != o_font)
18155 c = "</tspan>"
18156 c_font = n_font
18157 if (c_font == o_font)
18158 return c
18159 return c + tspan(c_font, o_font)
18160 }
18161 return c // &xxx;
18162 })
18163 if (c_font != o_font)
18164 o += "</tspan>"
18165
18166 // convert to String and memorize the string width and height
18167 o = new String(o)
18168 if (abc2svg.el)
18169 strwh(o) // browser
18170 else
18171 o.wh = strwh(str) // CLI
18172
18173 gene.curfont = c_font // keep the current font for the next paragraph
18174
18175 return o
18176 } // str2svg()
18177
18178 // set the default and current font
18179 function set_font(xxx) {
18180 if (typeof xxx == "string")
18181 xxx = get_font(xxx)
18182 gene.curfont = gene.deffont = xxx
18183 }
18184
18185 // output a string handling the font changes
18186 function out_str(str) {
18187 output += str2svg(str)
18188 }
18189
18190 // output a string, handling the font changes
18191 // the action is:
18192 // 'c' align center
18193 // 'r' align right
18194 // 'j' justify - w is the line width
18195 // otherwise align left
18196 function xy_str(x, y,
18197 str, // string or object String with attribute 'wh'
18198 action, // default: align left
18199 w, // needed for justify
18200 wh) { // optional [width, height]
18201 if (!wh)
18202 wh = str.wh || strwh(str)
18203 if (cfmt.singleline || cfmt.trimsvg) {
18204 var wx = wh[0]
18205 switch (action) {
18206 case 'c':
18207 wx = wh[0] / 2
18208 break
18209 case 'j':
18210 wx = w
18211 break
18212 case 'r':
18213 wx = 0
18214 break
18215 }
18216 if (img.wx < x + wx)
18217 img.wx = x + wx
18218 }
18219
18220 output += '<text class="' + font_class(gene.deffont)
18221 if (action != 'j' && str.length > 5
18222 && gene.deffont.wadj)
18223 output += '" lengthAdjust="' + gene.deffont.wadj +
18224 '" textLength="' + wh[0].toFixed(1);
18225 output += '" x="';
18226 out_sxsy(x, '" y="', y)
18227 switch (action) {
18228 case 'c':
18229 output += '" text-anchor="middle">'
18230 break
18231 case 'j':
18232 output += '" textLength="' + w.toFixed(1) + '">'
18233 break
18234 case 'r':
18235 output += '" text-anchor="end">'
18236 break
18237 default:
18238 output += '">'
18239 break
18240 }
18241 out_str(str);
18242 output += "</text>\n"
18243 }
18244
18245 // move last capitalized word to front when after a comma
18246 function trim_title(title, is_subtitle) {
18247 var i
18248
18249 if (cfmt.titletrim) {
18250 i = title.lastIndexOf(", ")
18251 if (i < 0 || title[i + 2] < 'A' || title[i + 2] > 'Z') {
18252 i = 0
18253 } else if (cfmt.titletrim == 1) { // (true) compatibility
18254 if (i < title.length - 7
18255 || title.indexOf(' ', i + 3) >= 0)
18256 i = 0
18257 } else {
18258 if (i < title.length - cfmt.titletrim - 2)
18259 i = 0
18260 }
18261 if (i)
18262 title = title.slice(i + 2).trim() + ' ' + title.slice(0, i)
18263 }
18264 if (!is_subtitle
18265 && cfmt.writefields.indexOf('X') >= 0)
18266 title = info.X + '. ' + title
18267 if (cfmt.titlecaps)
18268 return title.toUpperCase()
18269 return title
18270 }
18271
18272 // return the width of the music line
18273 function get_lwidth() {
18274 if (img.chg)
18275 set_page()
18276 return (img.width - img.lm - img.rm
18277 - 2) // for bar thickness at eol
18278 / cfmt.scale
18279 }
18280
18281 // header generation functions
18282 function write_title(title, is_subtitle) {
18283 var h, wh
18284
18285 if (!title)
18286 return
18287 set_page();
18288 if (is_subtitle) {
18289 set_font("subtitle");
18290 h = cfmt.subtitlespace
18291 } else {
18292 set_font("title");
18293 h = cfmt.titlespace
18294 }
18295 wh = strwh(title)
18296 wh[1] += gene.curfont.pad * 2
18297 vskip(wh[1] + h + gene.curfont.pad)
18298 h = gene.curfont.pad + wh[1] * .22 // + descent
18299 if (cfmt.titleleft)
18300 xy_str(0, h, title, null, null, wh)
18301 else
18302 xy_str(get_lwidth() / 2, h, title, "c", null, wh)
18303 }
18304
18305 /* -- output a header format '111 (222)' -- */
18306 function put_inf2r(x, y, str1, str2, action) {
18307 if (!str1) {
18308 if (!str2)
18309 return
18310 str1 = str2;
18311 str2 = null
18312 }
18313 if (!str2)
18314 xy_str(x, y, str1, action)
18315 else
18316 xy_str(x, y, str1 + ' (' + str2 + ')', action)
18317 }
18318
18319 /* -- write a text block (%%begintext / %%text / %%center) -- */
18320 function write_text(text, action) {
18321 if (action == 's')
18322 return // skip
18323 set_page();
18324
18325 var wh, font, o,
18326 strlw = get_lwidth(),
18327 sz = gene.curfont.size,
18328 lineskip = sz * cfmt.lineskipfac,
18329 parskip = sz * cfmt.parskipfac,
18330 i, j, x, words, w, k, ww, str;
18331
18332 switch (action) {
18333 default:
18334 // case 'c':
18335 // case 'r':
18336 font = gene.curfont
18337 switch (action) {
18338 case 'c': x = strlw / 2; break
18339 case 'r': x = strlw - font.pad; break
18340 default: x = font.pad; break
18341 }
18342 j = 0
18343 while (1) {
18344 i = text.indexOf('\n', j)
18345 if (i == j) { // new paragraph
18346 vskip(parskip);
18347 blk_flush()
18348 use_font(gene.curfont)
18349 while (text[i + 1] == '\n') {
18350 vskip(lineskip);
18351 i++
18352 }
18353 if (i == text.length)
18354 break
18355 } else {
18356 if (i < 0)
18357 str = text.slice(j)
18358 else
18359 str = text.slice(j, i)
18360 ww = strwh(str)
18361 vskip(ww[1] * cfmt.lineskipfac
18362 + font.pad * 2)
18363 xy_str(x, font.pad + ww[1] * .2, str, action)
18364 if (i < 0)
18365 break
18366 }
18367 j = i + 1
18368 }
18369 vskip(parskip);
18370 blk_flush()
18371 break
18372 case 'f':
18373 case 'j':
18374 j = 0
18375 while (1) {
18376 i = text.indexOf('\n\n', j)
18377 if (i < 0)
18378 words = text.slice(j)
18379 else
18380 words = text.slice(j, i);
18381 words = words.split(/\s+/);
18382 w = k = wh = 0
18383 for (j = 0; j < words.length; j++) {
18384 ww = strwh(words[j] + ' ') // &nbsp;
18385 w += ww[0]
18386 if (w >= strlw) {
18387 vskip(wh * cfmt.lineskipfac)
18388 xy_str(0, ww[1] * .2,
18389 words.slice(k, j).join(' '),
18390 action, strlw,
18391 [w - ww[0], ww[1]])
18392 k = j;
18393 w = ww[0]
18394 wh = 0
18395 }
18396 if (ww[1] > wh)
18397 wh = ww[1]
18398 }
18399 if (w != 0) { // last line
18400 vskip(wh * cfmt.lineskipfac)
18401 xy_str(0, ww[1] * .2, words.slice(k).join(' '))
18402 }
18403 vskip(parskip);
18404 blk_flush()
18405 if (i < 0)
18406 break
18407 while (text[i + 2] == '\n') {
18408 vskip(lineskip);
18409 i++
18410 }
18411 if (i == text.length)
18412 break
18413 use_font(gene.curfont);
18414 j = i + 2
18415 }
18416 break
18417 }
18418 }
18419
18420 /* -- output the words after tune -- */
18421 function put_words(words) {
18422 var p, i, j, nw, w, lw, x1, x2, i1, i2, do_flush,
18423 maxn = 0, // max number of characters per line
18424 n = 1 // number of verses
18425
18426 // output a line of words after tune
18427 function put_wline(p, x) {
18428 var i = 0,
18429 k = 0
18430
18431 if (p[0] == '$' // if font change
18432 && p[1] >= '0' && p[1] <= '9') {
18433 gene.curfont = p[1] == '0' ? gene.deffont
18434 : get_font("u" + p[1])
18435 p = p.slice(2)
18436 }
18437
18438 if ((p[i] >= '0' && p[i] <= '9') || p[i + 1] == '.') {
18439 while (i < p.length) {
18440 i++
18441 if (p[i] == ' '
18442 || p[i - 1] == ':'
18443 || p[i - 1] == '.')
18444 break
18445 }
18446 k = i
18447 while (p[i] == ' ')
18448 i++
18449 }
18450
18451 var y = gene.curfont.size * .22 // descent
18452 if (k != 0)
18453 xy_str(x, y, p.slice(0, k), 'r')
18454 if (i < p.length)
18455 xy_str(x + 5, y, p.slice(i), 'l')
18456 } // put_wline()
18457
18458 // estimate the width of the lines
18459 words = words.split('\n')
18460 nw = words.length
18461 for (i = 0; i < nw; i++) {
18462 p = words[i]
18463 if (!p) {
18464 while (i + 1 < nw && !words[i + 1])
18465 i++
18466 n++
18467 } else if (p.length > maxn) {
18468 maxn = p.length
18469 i1 = i // keep this line
18470 }
18471 }
18472 if (i1 == undefined)
18473 return // no text in the W: lines!
18474
18475 set_font("words")
18476 vskip(cfmt.wordsspace)
18477 svg_flush()
18478
18479 w = get_lwidth() / 2 // half line width
18480 lw = strwh(words[i1])[0]
18481 i1 = i2 = 0
18482 if (lw < w) { // if 2 columns
18483 j = n >> 1
18484 for (i = 0; i < nw; i++) {
18485 p = words[i]
18486 if (!p) {
18487 if (--j <= 0)
18488 i1 = i
18489 while (i + 1 < nw && !words[i + 1])
18490 i++
18491 if (j <= 0) {
18492 i2 = i + 1
18493 break
18494 }
18495 }
18496 }
18497 n >>= 1
18498 }
18499 if (i2) {
18500 x1 = (w - lw) / 2 + 10
18501 x2 = x1 + w
18502 } else { // one column
18503 x2 = w - lw / 2 + 10
18504 }
18505
18506 do_flush = true
18507 for (i = 0; i < i1 || i2 < nw; i++, i2++) {
18508 vskip(cfmt.lineskipfac * gene.curfont.size)
18509 if (i < i1) {
18510 p = words[i]
18511 if (p)
18512 put_wline(p, x1)
18513 else
18514 use_font(gene.curfont)
18515 }
18516 if (i2 < nw) {
18517 p = words[i2]
18518 if (p) {
18519 put_wline(p, x2)
18520 } else {
18521
18522
18523 if (--n == 0) {
18524 if (i < i1) {
18525 n++
18526 } else if (i2 < nw - 1) {
18527
18528 // center the last verse
18529 x2 = w - lw / 2 + 10
18530 svg_flush()
18531 }
18532 }
18533 }
18534 }
18535
18536 if (!words[i + 1] && !words[i2 + 1]) {
18537 if (do_flush) {
18538 svg_flush()
18539 do_flush = false
18540 }
18541 } else {
18542 do_flush = true
18543 }
18544 }
18545 }
18546
18547 /* -- output history -- */
18548 function put_history() {
18549 var i, j, c, str, font, h, w, wh, head,
18550 names = cfmt.infoname.split("\n"),
18551 n = names.length
18552
18553 for (i = 0; i < n; i++) {
18554 c = names[i][0]
18555 if (cfmt.writefields.indexOf(c) < 0)
18556 continue
18557 str = info[c]
18558 if (!str)
18559 continue
18560 if (!font) {
18561 font = true;
18562 set_font("history");
18563 vskip(cfmt.textspace);
18564 h = gene.curfont.size * cfmt.lineskipfac
18565 }
18566 head = names[i].slice(2)
18567 if (head[0] == '"')
18568 head = head.slice(1, -1);
18569 vskip(h);
18570 wh = strwh(head);
18571 xy_str(0, wh[1] * .22, head, null, null, wh);
18572 w = wh[0];
18573 str = str.split('\n');
18574 xy_str(w, wh[1] * .22, str[0])
18575 for (j = 1; j < str.length; j++) {
18576 if (!str[j]) { // new paragraph
18577 vskip(gene.curfont.size * cfmt.parskipfac)
18578 continue
18579 }
18580 vskip(h);
18581 xy_str(w, wh[1] * .22, str[j])
18582 }
18583 vskip(h * cfmt.parskipfac)
18584 use_font(gene.curfont)
18585 }
18586 }
18587
18588 // build a new sequence of the parts with clearer names
18589 function part_seq() {
18590 var i,
18591 o = ""
18592
18593 for (i = 0; i < info.P.length; i++) {
18594 if (i)
18595 o += ' '
18596 o += partname(info.P[i])[1]
18597 }
18598 return o
18599 } // part_seq()
18600
18601 // get the meaningful names of a part (P:)
18602 function partname(c) {
18603 var i, r, tmp
18604
18605 if (cfmt.partname) {
18606 tmp = cfmt.partname.split('\n')
18607
18608 for (i = 0; i < tmp.length; i++) {
18609 if (tmp[i][0] == c) {
18610 r = tmp[i].match(/.\s+(\S+)\s*(.+)?/)
18611 break
18612 }
18613 }
18614 }
18615 if (!r)
18616 return [0, c, c]
18617 if (!r[2])
18618 r[2] = r[1]
18619 if (r[2][0] == '"')
18620 r[2] = r[2].slice(1, -1)
18621 return r
18622 } // partname()
18623
18624 /* -- output the tune heading -- */
18625 // (possible hook)
18626 Abc.prototype.tunhd = function() {
18627 var i, j, area, composer, origin, rhythm, down1, down2, p,
18628 lwidth = get_lwidth()
18629
18630 vskip(cfmt.topspace)
18631
18632 /* titles */
18633 if (info.T
18634 && cfmt.writefields.indexOf('T') >= 0) {
18635 i = 0
18636 while (1) {
18637 j = info.T.indexOf("\n", i)
18638 if (j < 0) {
18639 write_title(info.T.substring(i), i != 0)
18640 break
18641 }
18642 write_title(info.T.slice(i, j), i != 0);
18643 i = j + 1
18644 }
18645 }
18646
18647 /* rhythm, composer, origin */
18648 down1 = down2 = 0
18649 if (parse.ckey.k_bagpipe
18650 && !cfmt.infoline
18651 && cfmt.writefields.indexOf('R') >= 0)
18652 rhythm = info.R
18653 if (rhythm) {
18654 set_font("composer");
18655 down1 = cfmt.composerspace + gene.curfont.size + 2
18656 xy_str(0, -down1 + gene.curfont.size *.22, rhythm)
18657 }
18658 area = info.A
18659 if (cfmt.writefields.indexOf('C') >= 0)
18660 composer = info.C
18661 if (cfmt.writefields.indexOf('O') >= 0)
18662 origin = info.O
18663 if (composer || origin || cfmt.infoline) {
18664 var xcomp, align;
18665
18666 set_font("composer");
18667 if (cfmt.aligncomposer < 0) {
18668 xcomp = 0;
18669 align = ' '
18670 } else if (cfmt.aligncomposer == 0) {
18671 xcomp = lwidth * .5;
18672 align = 'c'
18673 } else {
18674 xcomp = lwidth;
18675 align = 'r'
18676 }
18677 if (composer || origin) {
18678 down2 = cfmt.composerspace + 2
18679 i = 0
18680 while (1) {
18681 down2 += gene.curfont.size
18682 if (composer)
18683 j = composer.indexOf("\n", i)
18684 else
18685 j = -1
18686 if (j < 0) {
18687 put_inf2r(xcomp, -down2 + gene.curfont.size *.22,
18688 composer ? composer.substring(i) : null,
18689 origin,
18690 align)
18691 break
18692 }
18693 xy_str(xcomp, -down2 + gene.curfont.size *.22,
18694 composer.slice(i, j), align);
18695 i = j + 1
18696 }
18697 }
18698
18699 rhythm = rhythm ? null : info.R
18700 if ((rhythm || area) && cfmt.infoline) {
18701
18702 /* if only one of rhythm or area then do not use ()'s
18703 * otherwise output 'rhythm (area)' */
18704 set_font("info");
18705 down2 += cfmt.infospace + gene.curfont.size
18706 put_inf2r(lwidth, -down2 + gene.curfont.size *.22,
18707 rhythm, area, 'r')
18708 }
18709 }
18710
18711 /* parts */
18712 if (info.P
18713 && cfmt.writefields.indexOf('P') >= 0) {
18714 set_font("parts");
18715 i = cfmt.partsspace + gene.curfont.size + gene.curfont.pad
18716 if (down1 + i > down2)
18717 down2 = down1 + i
18718 else
18719 down2 += i
18720 p = info.P
18721 if (cfmt.partname)
18722 p = part_seq()
18723 xy_str(0, -down2 + gene.curfont.size *.22, p)
18724 down2 += gene.curfont.pad
18725 } else if (down1 > down2) {
18726 down2 = down1
18727 }
18728 vskip(down2 + cfmt.musicspace)
18729 } // tunhd()
18730
18731 // output the tune header
18732 function write_heading() {
18733 vskip(cfmt.topspace)
18734 self.tunhd()
18735 } // write_heading()
18736 // abc2svg - svg.js - svg functions
18737 //
18738 // Copyright (C) 2014-2025 Jean-Francois Moine
18739 //
18740 // This file is part of abc2svg-core.
18741 //
18742 // abc2svg-core is free software: you can redistribute it and/or modify
18743 // it under the terms of the GNU Lesser General Public License as published by
18744 // the Free Software Foundation, either version 3 of the License, or
18745 // (at your option) any later version.
18746 //
18747 // abc2svg-core is distributed in the hope that it will be useful,
18748 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18749 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18750 // GNU Lesser General Public License for more details.
18751 //
18752 // You should have received a copy of the GNU Lesser General Public License
18753 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
18754
18755 var output = "", // output buffer
18756 style = '\
18757 \n.stroke{stroke:currentColor;fill:none}\
18758 \n.bW{stroke:currentColor;fill:none;stroke-width:1}\
18759 \n.bthW{stroke:currentColor;fill:none;stroke-width:3}\
18760 \n.slW{stroke:currentColor;fill:none;stroke-width:.7}\
18761 \n.slthW{stroke:currentColor;fill:none;stroke-width:1.5}\
18762 \n.sltnW{stroke:currentColor;fill:none;stroke-width:.25}\
18763 \n.sldW{stroke:currentColor;fill:none;stroke-width:.7;stroke-dasharray:5,10}\
18764 \n.sW{stroke:currentColor;fill:none;stroke-width:.7}\
18765 \n.box{outline:1px solid black;outline-offset:1px}',
18766 font_style = '',
18767 posx = cfmt.leftmargin / cfmt.scale, // default x offset of the images
18768 posy = 0, // y offset in the block
18769 img = { // image
18770 width: cfmt.pagewidth, // width
18771 lm: cfmt.leftmargin, // left and right margins
18772 rm: cfmt.rightmargin,
18773 wx: 0, // used width between the left and right margins
18774 chg: 1 //true
18775 },
18776 defined_glyph = {},
18777 defs = '',
18778 fulldefs = '', // unreferenced defs as <filter>
18779 stv_g = { /* staff/voice graphic parameters */
18780 scale: 1,
18781 stsc: 1, // staff scale
18782 vsc: 1, // voice scale
18783 dy: 0,
18784 st: -1,
18785 v: -1,
18786 g: 0
18787 // color: undefined
18788 },
18789 blkdiv = 0 // block of contiguous SVGs
18790 // -1: block started
18791 // 0: no block
18792 // 1: start a block
18793 // 2: start a new page
18794
18795 // glyphs in music font
18796 var tgls = {
18797 "mtr ": {x:0, y:0, c:"\u0020"}, // space
18798 brace: {x:0, y:0, c:"\ue000"},
18799 lphr: {x:0, y:23, c:"\ue030"},
18800 mphr: {x:0, y:23, c:"\ue038"},
18801 sphr: {x:0, y:25, c:"\ue039"},
18802 short: {x:0, y:32, c:"\ue038"},
18803 tick: {x:0, y:25, c:"\ue039"},
18804 rdots: {x:0, y:0, c:"\ue043"}, // repeat dots
18805 rdot: {x:0, y:0, c:"\ue044"}, // single repeat dot
18806 dsgn: {x:-12, y:0, c:"\ue045"}, // D.S.
18807 dcap: {x:-12, y:0, c:"\ue046"}, // D.C.
18808 sgno: {x:-5, y:0, c:"\ue047"}, // segno
18809 coda: {x:-10, y:0, c:"\ue048"},
18810 tclef: {x:-8, y:0, c:"\ue050"},
18811 cclef: {x:-8, y:0, c:"\ue05c"},
18812 bclef: {x:-8, y:0, c:"\ue062"},
18813 pclef: {x:-6, y:0, c:"\ue069"},
18814 spclef: {x:-6, y:0, c:"\ue069"},
18815 stclef: {x:-8, y:0, c:"\ue07a"},
18816 scclef: {x:-8, y:0, c:"\ue07b"},
18817 sbclef: {x:-7, y:0, c:"\ue07c"},
18818 oct: {x:0, y:2, c:"\ue07d"}, // 8 for clefs
18819 oct2: {x:0, y:2, c:"\ue07e"}, // 15 for clefs
18820 mtr0: {x:0, y:0, c:"\ue080"}, // meters
18821 mtr1: {x:0, y:0, c:"\ue081"},
18822 mtr2: {x:0, y:0, c:"\ue082"},
18823 mtr3: {x:0, y:0, c:"\ue083"},
18824 mtr4: {x:0, y:0, c:"\ue084"},
18825 mtr5: {x:0, y:0, c:"\ue085"},
18826 mtr6: {x:0, y:0, c:"\ue086"},
18827 mtr7: {x:0, y:0, c:"\ue087"},
18828 mtr8: {x:0, y:0, c:"\ue088"},
18829 mtr9: {x:0, y:0, c:"\ue089"},
18830 mtrC: {x:0, y:0, c:"\ue08a"}, // common time (4/4)
18831 "mtrC|": {x:0, y:0, c:"\ue08b"}, // cut time (2/2)
18832 "mtr+": {x:0, y:0, c:"\ue08c"},
18833 "mtr(": {x:0, y:0, c:"\ue094"},
18834 "mtr)": {x:0, y:0, c:"\ue095"},
18835 HDD: {x:-7, y:0, c:"\ue0a0"},
18836 breve: {x:-7, y:0, c:"\ue0a1"},
18837 HD: {x:-5.2, y:0, c:"\ue0a2"},
18838 Hd: {x:-3.8, y:0, c:"\ue0a3"},
18839 hd: {x:-3.7, y:0, c:"\ue0a4"},
18840 ghd: {x:2, y:0, c:"\ue0a4", sc:.66}, // grace note head
18841 pshhd: {x:-3.7, y:0, c:"\ue0a9"},
18842 pfthd: {x:-3.7, y:0, c:"\ue0b3"},
18843 x: {x:-3.7, y:0, c:"\ue0a9"}, // 'x' note head
18844 "circle-x": {x:-3.7, y:0, c:"\ue0b3"}, // 'circle-x' note head
18845 srep: {x:-5, y:0, c:"\ue101"},
18846 "dot+": {x:-5, y:0, sc:.7, c:"\ue101"},
18847 diamond: {x:-4, y:0, c:"\ue1b9"},
18848 triangle: {x:-4, y:0, c:"\ue1bb"},
18849 dot: {x:-1, y:0, c:"\ue1e7"},
18850 flu1: {x:-.3, y:0, c:"\ue240"}, // flags
18851 fld1: {x:-.3, y:0, c:"\ue241"},
18852 flu2: {x:-.3, y:0, c:"\ue242"},
18853 fld2: {x:-.3, y:0, c:"\ue243"},
18854 flu3: {x:-.3, y:3.5, c:"\ue244"},
18855 fld3: {x:-.3, y:-4, c:"\ue245"},
18856 flu4: {x:-.3, y:8, c:"\ue246"},
18857 fld4: {x:-.3, y:-9, c:"\ue247"},
18858 flu5: {x:-.3, y:12.5, c:"\ue248"},
18859 fld5: {x:-.3, y:-14, c:"\ue249"},
18860 "acc-1": {x:-1, y:0, c:"\ue260"}, // flat
18861 "cacc-1": {x:-18, y:0, c:"\ue26a\ue260\ue26b"}, // courtesy flat (note deco)
18862 "sacc-1": {x:-1, y:0, sc:.7, c:"\ue260"}, // small flat (editorial)
18863 acc3: {x:-1, y:0, c:"\ue261"}, // natural
18864 "cacc3": {x:-18, y:0, c:"\ue26a\ue261\ue26b"}, // courtesy natural (note deco)
18865 sacc3: {x:-1, y:0, sc:.7, c:"\ue261"}, // small natural (editorial)
18866 acc1: {x:-2, y:0, c:"\ue262"}, // sharp
18867 "cacc1": {x:-18, y:0, c:"\ue26a\ue262\ue26b"}, // courtesy sharp (note deco)
18868 sacc1: {x:-2, y:0, sc: .7, c:"\ue262"}, // small sharp (editorial)
18869 acc2: {x:-3, y:0, c:"\ue263"}, // double sharp
18870 "acc-2": {x:-3, y:0, c:"\ue264"}, // double flat
18871 "acc-1_2": {x:-2, y:0, c:"\ue280"}, // quarter-tone flat
18872 "acc-3_2": {x:-3, y:0, c:"\ue281"}, // three-quarter-tones flat
18873 acc1_2: {x:-1, y:0, c:"\ue282"}, // quarter-tone sharp
18874 acc3_2: {x:-3, y:0, c:"\ue283"}, // three-quarter-tones sharp
18875 accent: {x:-3, y:2, c:"\ue4a0"},
18876 stc: {x:0, y:-2, c:"\ue4a2"}, // staccato
18877 emb: {x:0, y:-2, c:"\ue4a4"},
18878 wedge: {x:0, y:0, c:"\ue4a8"},
18879 marcato: {x:-3, y:-2, c:"\ue4ac"},
18880 hld: {x:-7, y:-2, c:"\ue4c0"}, // fermata
18881 brth: {x:0, y:0, c:"\ue4ce"},
18882 caes: {x:0, y:8, c:"\ue4d1"},
18883 r00: {x:-1.5, y:0, c:"\ue4e1"},
18884 r0: {x:-1.5, y:0, c:"\ue4e2"},
18885 r1: {x:-3.5, y:-6, c:"\ue4e3"},
18886 r2: {x:-3.2, y:0, c:"\ue4e4"},
18887 r4: {x:-3, y:0, c:"\ue4e5"},
18888 r8: {x:-3, y:0, c:"\ue4e6"},
18889 r16: {x:-4, y:0, c:"\ue4e7"},
18890 r32: {x:-4, y:0, c:"\ue4e8"},
18891 r64: {x:-4, y:0, c:"\ue4e9"},
18892 r128: {x:-4, y:0, c:"\ue4ea"},
18893 // mrest: {x:-10, y:0, c:"\ue4ee"},
18894 mrep: {x:-6, y:0, c:"\ue500"},
18895 mrep2: {x:-9, y:0, c:"\ue501"},
18896 p: {x:-3, y:0, c:"\ue520"},
18897 f: {x:-3, y:0, c:"\ue522"},
18898 pppp: {x:-15, y:0, c:"\ue529"},
18899 ppp: {x:-14, y:0, c:"\ue52a"},
18900 pp: {x:-8, y:0, c:"\ue52b"},
18901 mp: {x:-8, y:0, c:"\ue52c"},
18902 mf: {x:-8, y:0, c:"\ue52d"},
18903 ff: {x:-7, y:0, c:"\ue52f"},
18904 fff: {x:-10, y:0, c:"\ue530"},
18905 ffff: {x:-14, y:0, c:"\ue531"},
18906 sfz: {x:-10, y:0, c:"\ue539"},
18907 trl: {x:-3, y:-3, c:"\ue566"}, // trill
18908 turn: {x:-5, y:0, c:"\ue567"},
18909 turnx: {x:-5, y:0, c:"\ue569"},
18910 umrd: {x:-6, y:2, c:"\ue56c"},
18911 lmrd: {x:-6, y:2, c:"\ue56d"},
18912 dplus: {x:-3, y:0, c:"\ue582"}, // plus
18913 sld: {x:-3, y:2, c:"\ue5d0"}, // slide
18914 grm: {x:-3, y:-2, c:"\ue5e2"}, // grace mark
18915 dnb: {x:-3, y:0, c:"\ue610"}, // down bow
18916 upb: {x:-2, y:0, c:"\ue612"}, // up bow
18917 opend: {x:-2, y:-2, c:"\ue614"}, // harmonic
18918 roll: {x:0, y:0, c:"\ue618"},
18919 thumb: {x:-2, y:-2, c:"\ue624"},
18920 snap: {x:-2, y:-2, c:"\ue630"},
18921 ped: {x:-10, y:0, c:"\ue650"},
18922 pedoff: {x:-5, y:0, c:"\ue655"},
18923 "mtro.": {x:0, y:0, c:"\ue910"}, // tempus perfectum prolatione perfecta
18924 mtro: {x:0, y:0, c:"\ue911"}, // tempus perfectum
18925 "mtro|": {x:0, y:0, c:"\ue912"}, // tempus perfectum (twice as fast)
18926 "mtrc.": {x:0, y:0, c:"\ue914"}, // tempus imperfectum prolatione perfecta
18927 mtrc: {x:0, y:0, c:"\ue915"}, // tempus imperfectum
18928 "mtrc|": {x:0, y:0, c:"\ue918"}, // tempus imperfectum (twice as fast)
18929 longa: {x:-4.7, y:0, c:"\ue95d"},
18930 custos: {x:-4, y:3, c:"\uea02"},
18931 ltr: {x:2, y:6, c:"\ueaa4"} // long trill element
18932 }
18933
18934 // glyphs to put in <defs>
18935 var glyphs = {
18936 }
18937
18938 // convert a meter string to a SmuFL encoded string
18939 function m_gl(s) {
18940 return s.replace(/./g,
18941 function(e) {
18942 var m = tgls["mtr" + e]
18943 //fixme: !! no m.x nor m.y yet !!
18944 // if (!m.x && !m.y)
18945 return m ? m.c : 0
18946 // return '<tspan dx="'+ m.x.toFixed(1) +
18947 // '" dy="' + m.y.toFixed(1) +
18948 // '">' +
18949 // m.c + '</tspan>'
18950 })
18951 }
18952
18953 // mark a glyph as used and add it in <defs>
18954 function def_use(gl) {
18955 var i, j, g
18956
18957 if (defined_glyph[gl])
18958 return
18959 defined_glyph[gl] = true;
18960 g = glyphs[gl]
18961 if (!g) {
18962 //throw new Error("unknown glyph: " + gl)
18963 error(1, null, "Unknown glyph: '$1'", gl)
18964 return // fixme: the xlink is set
18965 }
18966 j = 0
18967 while (1) {
18968 i = g.indexOf('xlink:href="#', j)
18969 if (i < 0)
18970 break
18971 i += 13;
18972 j = g.indexOf('"', i);
18973 def_use(g.slice(i, j))
18974 }
18975 defs += '\n' + g
18976 }
18977
18978 // add user defs from %%beginsvg
18979 function defs_add(text) {
18980 var i, j, gl, tag, is,
18981 ie = 0
18982
18983 // remove XML comments
18984 text = text.replace(/<!--.*?-->/g, '')
18985
18986 while (1) {
18987 is = text.indexOf('<', ie);
18988 if (is < 0)
18989 break
18990 i = text.indexOf('id="', is)
18991 if (i < 0)
18992 break
18993 i += 4;
18994 j = text.indexOf('"', i);
18995 if (j < 0)
18996 break
18997 gl = text.slice(i, j);
18998 ie = text.indexOf('>', j);
18999 if (ie < 0)
19000 break
19001 if (text[ie - 1] == '/') {
19002 ie++
19003 } else {
19004 i = text.indexOf(' ', is);
19005 if (i < 0)
19006 break
19007 tag = text.slice(is + 1, i);
19008 ie = text.indexOf('</' + tag + '>', ie)
19009 if (ie < 0)
19010 break
19011 ie += 3 + tag.length
19012 }
19013 if (text.substr(is, 7) == '<filter')
19014 fulldefs += text.slice(is, ie) + '\n'
19015 else
19016 glyphs[gl] = text.slice(is, ie)
19017 }
19018 }
19019
19020 // output the stop/start of a graphic sequence
19021 function set_g() {
19022
19023 // close the previous sequence
19024 if (stv_g.started) {
19025 stv_g.started = false;
19026 glout()
19027 output += "</g>\n"
19028 }
19029
19030 // check if new sequence needed
19031 if (stv_g.scale == 1 && !stv_g.color)
19032 return
19033
19034 // open the new sequence
19035 glout()
19036 output += '<g '
19037 if (stv_g.scale != 1) {
19038 if (stv_g.st < 0)
19039 output += voice_tb[stv_g.v].scale_str
19040 else if (stv_g.v < 0)
19041 output += staff_tb[stv_g.st].scale_str
19042 else
19043 output += 'transform="translate(0,' +
19044 (posy - stv_g.dy).toFixed(1) +
19045 ') scale(' + stv_g.scale + ')"'
19046 }
19047 if (stv_g.color) {
19048 if (stv_g.scale != 1)
19049 output += ' ';
19050 output += 'color="' + stv_g.color + '"'
19051 }
19052 output += ">\n";
19053 stv_g.started = true
19054 }
19055
19056 /* set the color */
19057 function set_color(color) {
19058 if (color == stv_g.color)
19059 return undefined // same color
19060 var old_color = stv_g.color;
19061 stv_g.color = color;
19062 set_g()
19063 return old_color
19064 }
19065
19066 /* -- set the staff scale (only) -- */
19067 function set_sscale(st) {
19068 var new_scale, dy
19069
19070 if (st != stv_g.st && stv_g.scale != 1)
19071 stv_g.scale = 1
19072 new_scale = st >= 0 ? staff_tb[st].staffscale : 1
19073 if (st >= 0 && new_scale != 1)
19074 dy = staff_tb[st].y
19075 else
19076 dy = posy
19077 if (new_scale == stv_g.scale && dy == stv_g.dy
19078 && stv_g.st == st && stv_g.vsc == 1)
19079 return
19080 stv_g.stsc =
19081 stv_g.scale = new_scale
19082 stv_g.vsc = 1
19083 stv_g.dy = dy;
19084 stv_g.st = st;
19085 stv_g.v = -1;
19086 set_g()
19087 }
19088
19089 /* -- set the voice or staff scale -- */
19090 function set_scale(s) {
19091 var new_dy = posy,
19092 st = staff_tb[s.st].staffscale == 1 ? -1 : s.st,
19093 new_scale = s.p_v.scale
19094
19095 if (st >= 0) {
19096 new_scale *= staff_tb[st].staffscale
19097 new_dy = staff_tb[st].y
19098 }
19099 if (new_scale == stv_g.scale && stv_g.dy == new_dy)
19100 return
19101 stv_g.scale = new_scale;
19102 stv_g.vsc = s.p_v.scale
19103 stv_g.dy = new_dy;
19104 stv_g.st = st
19105 stv_g.v = s.v;
19106 set_g()
19107 }
19108
19109 // -- set the staff output buffer and scale when delayed output
19110 function set_dscale(st, no_scale) {
19111 if (output) {
19112 if (stv_g.started) { // close the previous sequence
19113 stv_g.started = false
19114 glout()
19115 output += "</g>\n"
19116 }
19117 if (stv_g.st < 0) {
19118 staff_tb[0].output += output
19119 } else if (stv_g.scale == 1) {
19120 staff_tb[stv_g.st].output += output
19121 } else {
19122 staff_tb[stv_g.st].sc_out += output
19123 }
19124 output = ""
19125 }
19126 if (st < 0)
19127 stv_g.scale = 1
19128 else
19129 stv_g.scale = no_scale ? 1 : staff_tb[st].staffscale;
19130 stv_g.st = st;
19131 stv_g.dy = 0
19132 }
19133
19134 // update the y offsets of delayed output
19135 function delayed_update() {
19136 var st, new_out, text
19137
19138 for (st = 0; st <= nstaff; st++) {
19139 if (staff_tb[st].sc_out) {
19140 output += '<g ' + staff_tb[st].scale_str + '>\n' +
19141 staff_tb[st].sc_out + '</g>\n';
19142 staff_tb[st].sc_out = ""
19143 }
19144 if (!staff_tb[st].output)
19145 continue
19146 output += '<g transform="translate(0,' +
19147 (-staff_tb[st].y).toFixed(1) +
19148 ')">\n' +
19149 staff_tb[st].output +
19150 '</g>\n';
19151 staff_tb[st].output = ""
19152 }
19153 }
19154
19155 // output the annotations
19156 function anno_out(s, t, f) {
19157 if (s.istart == undefined)
19158 return
19159 var type = s.type,
19160 h = s.ymx - s.ymn + 4,
19161 wl = s.wl || 2,
19162 wr = s.wr || 2
19163
19164 if (s.grace)
19165 type = C.GRACE
19166
19167 f(t || abc2svg.sym_name[type], s.istart, s.iend,
19168 s.x - wl - 2, staff_tb[s.st].y + s.ymn + h - 2,
19169 wl + wr + 4, h, s);
19170 }
19171
19172 function a_start(s, t) {
19173 anno_out(s, t, user.anno_start)
19174 }
19175 function a_stop(s, t) {
19176 anno_out(s, t, user.anno_stop)
19177 }
19178 function empty_function() {
19179 }
19180 // the values are updated on generate()
19181 var anno_start = empty_function,
19182 anno_stop = empty_function
19183
19184 // output the stop user annotations
19185 function anno_put() {
19186 var s
19187 while (1) {
19188 s = anno_a.shift()
19189 if (!s)
19190 break
19191 switch (s.type) {
19192 case C.CLEF:
19193 case C.METER:
19194 case C.KEY:
19195 case C.REST:
19196 if (s.type != C.REST || s.rep_nb) {
19197 set_sscale(s.st)
19198 break
19199 }
19200 // fall thru
19201 case C.GRACE:
19202 case C.NOTE:
19203 case C.MREST:
19204 set_scale(s)
19205 break
19206 // default:
19207 // continue
19208 }
19209 anno_stop(s)
19210 }
19211 } // anno_put()
19212
19213 // output a string with x, y, a and b
19214 // In the string,
19215 // X and Y are replaced by scaled x and y
19216 // A and B are replaced by a and b as string
19217 // F and G are replaced by a and b as float
19218 function out_XYAB(str, x, y, a, b) {
19219 x = sx(x);
19220 y = sy(y);
19221 output += str.replace(/X|Y|A|B|F|G/g, function(c) {
19222 switch (c) {
19223 case 'X': return x.toFixed(1)
19224 case 'Y': return y.toFixed(1)
19225 case 'A': return a
19226 case 'B': return b
19227 case 'F': return a.toFixed(1)
19228 // case 'G':
19229 default: return b.toFixed(1)
19230 }
19231 })
19232 }
19233
19234 // open / close containers
19235 function g_open(x, y, rot, sx, sy) {
19236 glout()
19237 out_XYAB('<g transform="translate(X,Y', x, y);
19238 if (rot)
19239 output += ') rotate(' + rot.toFixed(2)
19240 if (sx) {
19241 output += ') scale(' + sx
19242 if (sy)
19243 output += ', ' + sy
19244 }
19245 output += ')">\n';
19246 stv_g.g++
19247 }
19248 function g_close() {
19249 glout()
19250 stv_g.g--;
19251 output += '</g>\n'
19252 }
19253
19254 // external SVG string
19255 Abc.prototype.out_svg = function(str) { output += str }
19256
19257 // exported functions for the annotation
19258 function sx(x) {
19259 if (stv_g.g)
19260 return x
19261 return (x + posx) / stv_g.scale
19262 }
19263 Abc.prototype.sx = sx
19264 function sy(y) {
19265 if (stv_g.g)
19266 return -y
19267 if (stv_g.scale == 1)
19268 return posy - y
19269 if (stv_g.v >= 0)
19270 return (stv_g.dy - y) / stv_g.vsc
19271 return stv_g.dy - y // staff scale only
19272 }
19273 Abc.prototype.sy = sy;
19274 Abc.prototype.sh = function(h) {
19275 if (stv_g.st < 0)
19276 return h / stv_g.scale
19277 return h
19278 }
19279 // for absolute X,Y coordinates
19280 Abc.prototype.ax = function(x) { return x + posx }
19281 Abc.prototype.ay = function(y) {
19282 if (stv_g.st < 0)
19283 return posy - y
19284 return posy + (stv_g.dy - y) * stv_g.scale - stv_g.dy
19285 }
19286 Abc.prototype.ah = function(h) {
19287 if (stv_g.st < 0)
19288 return h
19289 return h * stv_g.scale
19290 }
19291 // output scaled (x + <sep> + y)
19292 function out_sxsy(x, sep, y) {
19293 x = sx(x);
19294 y = sy(y);
19295 output += x.toFixed(1) + sep + y.toFixed(1)
19296 }
19297 Abc.prototype.out_sxsy = out_sxsy
19298
19299 // define the start of a path
19300 function xypath(x, y, fill) {
19301 if (fill)
19302 out_XYAB('<path d="mX Y', x, y)
19303 else
19304 out_XYAB('<path class="stroke" d="mX Y', x, y)
19305 }
19306 Abc.prototype.xypath = xypath
19307
19308 // draw all the helper/ledger lines
19309 function draw_all_hl() {
19310 var st, p_st
19311
19312 function hlud(hla, d) {
19313 var hl, hll, i, xp, dx2, x2,
19314 n = hla.length
19315
19316 if (!n)
19317 return
19318 for (i = 0; i < n; i++) { // for all lines
19319 hll = hla[i]
19320 if (!hll || !hll.length)
19321 continue
19322 xp = sx(hll[0][0]) // previous x
19323 output +=
19324 '<path class="stroke" stroke-width="1" d="M' +
19325 xp.toFixed(1) + ' ' +
19326 sy(p_st.y + d * i).toFixed(1)
19327 dx2 = 0
19328 while (1) {
19329 hl = hll.shift()
19330 if (!hl)
19331 break
19332 x2 = sx(hl[0])
19333 output += 'm' +
19334 (x2 - xp + hl[1] - dx2).toFixed(2) +
19335 ' 0h' + (-hl[1] + hl[2]).toFixed(2)
19336 xp = x2
19337 dx2 = hl[2]
19338 }
19339 output += '"/>\n'
19340 }
19341 } // hlud()
19342
19343 for (st = 0; st <= nstaff; st++) {
19344 p_st = staff_tb[st]
19345 if (!p_st.hlu)
19346 continue // (staff not yet displayed)
19347 set_sscale(st)
19348 hlud(p_st.hlu, 6)
19349 hlud(p_st.hld, -6)
19350 }
19351 } // draw_all_hl()
19352
19353 // output the list of glyphs and the stems
19354 // [0] = x glyph
19355 // [1] = y glyph
19356 // [2] = glyph code
19357 // [3] = x, y, h of stem (3 values per stem)
19358 var gla = [[], [], "", [], [], []]
19359 function glout() {
19360 var e,
19361 v = []
19362
19363 // glyphs (notes, accidentals...)
19364 if (gla[0].length) {
19365 while (1) {
19366 e = gla[0].shift()
19367 if (e == undefined)
19368 break
19369 v.push(e.toFixed(1))
19370 }
19371 output += '<text x="' + v.join(',')
19372
19373 v = []
19374 while (1) {
19375 e = gla[1].shift()
19376 if (e == undefined)
19377 break
19378 v.push(e.toFixed(1))
19379 }
19380 output += '"\ny="' + v.join(',')
19381
19382 output += '"\n>' + gla[2] + '</text>\n'
19383 gla[2] = ""
19384 }
19385
19386 // stems
19387 if (!gla[3].length)
19388 return
19389 output += '<path class="sW" d="'
19390 while (1) {
19391 e = gla[3].shift()
19392 if (e == undefined)
19393 break
19394 output += 'M' + e.toFixed(1) +
19395 ' ' + gla[3].shift().toFixed(1) +
19396 'v' + gla[3].shift().toFixed(1)
19397 }
19398 output += '"/>\n'
19399 } // glout()
19400
19401 // output a glyph
19402 function xygl(x, y, gl) {
19403 // (avoid ps<->js loop)
19404 // if (psxygl(x, y, gl))
19405 // return
19406 if (glyphs[gl]) {
19407 def_use(gl)
19408 out_XYAB('<use x="X" y="Y" xlink:href="#A"/>\n', x, y, gl)
19409 } else {
19410 var tgl = tgls[gl]
19411 if (tgl) {
19412 x += tgl.x * stv_g.scale;
19413 y -= tgl.y
19414 if (tgl.sc) {
19415 out_XYAB('<text transform="translate(X,Y) scale(A)">B</text>\n',
19416 x, y, tgl.sc, tgl.c);
19417 } else {
19418 // out_XYAB('<text x="X" y="Y">A</text>\n', x, y, tgl.c)
19419 gla[0].push(sx(x))
19420 gla[1].push(sy(y))
19421 gla[2] += tgl.c
19422 }
19423 } else if (gl != 'nil') {
19424 error(1, null, 'no definition of $1', gl)
19425 }
19426 }
19427 }
19428 // - specific functions -
19429 // gua gda (acciaccatura)
19430 function out_acciac(x, y, dx, dy, up) {
19431 if (up) {
19432 x -= 1;
19433 y += 4
19434 } else {
19435 x -= 5;
19436 y -= 4
19437 }
19438 out_XYAB('<path class="stroke" d="mX YlF G"/>\n',
19439 x, y, dx, -dy)
19440 }
19441 // staff system brace
19442 function out_brace(x, y, h) {
19443 //fixme: '-6' depends on the scale
19444 x += posx - 6;
19445 y = posy - y;
19446 h /= 24;
19447 output += '<text transform="translate(' +
19448 x.toFixed(1) + ',' + y.toFixed(1) +
19449 ') scale(2.5,' + h.toFixed(2) +
19450 ')">' + tgls.brace.c + '</text>\n'
19451 }
19452
19453 // staff system bracket
19454 function out_bracket(x, y, h) {
19455 x += posx - 5;
19456 y = posy - y - 3;
19457 h += 2;
19458 output += '<path d="m' + x.toFixed(1) + ' ' + y.toFixed(1) + '\n\
19459 c10.5 1 12 -4.5 12 -3.5c0 1 -3.5 5.5 -8.5 5.5\n\
19460 v' + h.toFixed(1) + '\n\
19461 c5 0 8.5 4.5 8.5 5.5c0 1 -1.5 -4.5 -12 -3.5"/>\n'
19462 }
19463 // hyphen
19464 function out_hyph(x, y, w) {
19465 var n, a_y,
19466 d = 25 + ((w / 20) | 0) * 3
19467
19468 if (w > 15.)
19469 n = ((w - 15) / d) | 0
19470 else
19471 n = 0;
19472 x += (w - d * n - 5) / 2;
19473 out_XYAB('<path class="stroke" stroke-width="1.2"\n\
19474 stroke-dasharray="5,A"\n\
19475 d="mX YhB"/>\n',
19476 x, y + 4, // set the line a bit upper
19477 Math.round((d - 5) / stv_g.scale), d * n + 5)
19478 }
19479 // stem [and flags]
19480 function out_stem(x, y, h, grace,
19481 nflags, straight) { // optional
19482 //fixme: dx KO with half note or longa
19483 var dx = grace ? GSTEM_XOFF : 3.5,
19484 slen = -h
19485
19486 if (h < 0)
19487 dx = -dx; // down
19488 x += dx * stv_g.scale
19489 if (stv_g.v >= 0)
19490 slen /= voice_tb[stv_g.v].scale;
19491 gla[3].push(sx(x))
19492 gla[3].push(sy(y))
19493 gla[3].push(slen)
19494 if (!nflags)
19495 return
19496
19497 y += h
19498 if (h > 0) { // up
19499 if (!straight) {
19500 if (!grace) {
19501 xygl(x, y, "flu" + nflags)
19502 return
19503 } else { // grace
19504 output += '<path d="'
19505 if (nflags == 1) {
19506 out_XYAB('MX Yc0.6 3.4 5.6 3.8 3 10\n\
19507 1.2 -4.4 -1.4 -7 -3 -7\n', x, y)
19508 } else {
19509 while (--nflags >= 0) {
19510 out_XYAB('MX Yc1 3.2 5.6 2.8 3.2 8\n\
19511 1.4 -4.8 -2.4 -5.4 -3.2 -5.2\n', x, y);
19512 y -= 3.5
19513 }
19514 }
19515 }
19516 } else { // straight
19517 output += '<path d="'
19518 if (!grace) {
19519 while (--nflags >= 0) {
19520 out_XYAB('MX Yl7 3.2 0 3.2 -7 -3.2z\n',
19521 x, y);
19522 y -= 5.4
19523 }
19524 } else { // grace
19525 while (--nflags >= 0) {
19526 out_XYAB('MX Yl3 1.5 0 2 -3 -1.5z\n',
19527 x, y);
19528 y -= 3
19529 }
19530 }
19531 }
19532 } else { // down
19533 if (!straight) {
19534 if (!grace) {
19535 xygl(x, y, "fld" + nflags)
19536 return
19537 } else { // grace
19538 output += '<path d="'
19539 if (nflags == 1) {
19540 out_XYAB('MX Yc0.6 -3.4 5.6 -3.8 3 -10\n\
19541 1.2 4.4 -1.4 7 -3 7\n', x, y)
19542 } else {
19543 while (--nflags >= 0) {
19544 out_XYAB('MX Yc1 -3.2 5.6 -2.8 3.2 -8\n\
19545 1.4 4.8 -2.4 5.4 -3.2 5.2\n', x, y);
19546 y += 3.5
19547 }
19548 }
19549 }
19550 } else { // straight
19551 output += '<path d="'
19552 if (!grace) {
19553 while (--nflags >= 0) {
19554 out_XYAB('MX Yl7 -3.2 0 -3.2 -7 3.2z\n',
19555 x, y);
19556 y += 5.4
19557 }
19558 // } else { // grace
19559 //--fixme: error?
19560 }
19561 }
19562 }
19563 output += '"/>\n'
19564 }
19565 // tremolo
19566 function out_trem(x, y, ntrem) {
19567 out_XYAB('<path d="mX Y\n\t', x - 4.5, y)
19568 while (1) {
19569 output += 'l9 -3v3l-9 3z'
19570 if (--ntrem <= 0)
19571 break
19572 output += 'm0 5.4'
19573 }
19574 output += '"/>\n'
19575 }
19576 // tuplet bracket - the staves are not defined
19577 function out_tubr(x, y, dx, dy, up) {
19578 var h = up ? -3 : 3;
19579
19580 y += h;
19581 dx /= stv_g.scale;
19582 output += '<path class="stroke" d="m';
19583 out_sxsy(x, ' ', y);
19584 output += 'v' + h.toFixed(1) +
19585 'l' + dx.toFixed(1) + ' ' + (-dy).toFixed(1) +
19586 'v' + (-h).toFixed(1) + '"/>\n'
19587 }
19588 // tuplet bracket with number - the staves are not defined
19589 function out_tubrn(x, y, dx, dy, up, str) {
19590 var dxx,
19591 sw = str.length * 10,
19592 h = up ? -3 : 3;
19593
19594 set_font("tuplet")
19595 xy_str(x + dx / 2, y + dy / 2 - gene.curfont.size * .1,
19596 str, 'c')
19597 dx /= stv_g.scale
19598 if (!up)
19599 y += 6;
19600 output += '<path class="stroke" d="m';
19601 out_sxsy(x, ' ', y);
19602 dxx = dx - sw + 1
19603 if (dy > 0)
19604 sw += dy / 8
19605 else
19606 sw -= dy / 8
19607 output += 'v' + h.toFixed(1) +
19608 'm' + dx.toFixed(1) + ' ' + (-dy).toFixed(1) +
19609 'v' + (-h).toFixed(1) + '"/>\n' +
19610 '<path class="stroke" stroke-dasharray="' +
19611 (dxx / 2).toFixed(1) + ' ' + sw.toFixed(1) +
19612 '" d="m';
19613 out_sxsy(x, ' ', y - h);
19614 output += 'l' + dx.toFixed(1) + ' ' + (-dy).toFixed(1) + '"/>\n'
19615
19616 }
19617 // underscore line
19618 function out_wln(x, y, w) {
19619 out_XYAB('<path class="stroke" stroke-width="0.8" d="mX YhF"/>\n',
19620 x, y + 1, w)
19621 }
19622
19623 // decorations with string
19624 var deco_str_style = {
19625 crdc: { // cresc., decresc., dim., ...
19626 dx: 0,
19627 dy: 5,
19628 style: 'font:italic 14px text,serif',
19629 anchor: ' text-anchor="middle"'
19630 },
19631 dacs: { // long repeats (da capo, fine...)
19632 dx: 0,
19633 dy: 3,
19634 style: 'font:bold 15px text,serif',
19635 anchor: ' text-anchor="middle"'
19636 },
19637 pf: {
19638 dx: 0,
19639 dy: 5,
19640 style: 'font:italic bold 16px text,serif',
19641 anchor: ' text-anchor="middle"'
19642 }
19643 }
19644 deco_str_style.at = deco_str_style.crdc
19645
19646 function out_deco_str(x, y, de) {
19647 var name = de.dd.glyph // class
19648
19649 if (name == 'fng') {
19650 out_XYAB('\
19651 <text x="X" y="Y" style="font-size:14px">A</text>\n',
19652 x - 2, y + 1, m_gl(de.dd.str))
19653 return
19654 }
19655
19656 if (name == '@') { // compatibility
19657 name = 'at'
19658 } else if (!/^[A-Za-z][A-Za-z\-_]*$/.test(name)) {
19659 error(1, de.s, "No function for decoration '$1'", de.dd.name)
19660 return
19661 }
19662
19663 var f,
19664 a_deco = deco_str_style[name]
19665
19666 if (!a_deco)
19667 a_deco = deco_str_style.crdc // default style
19668 else if (a_deco.style)
19669 style += "\n." + name + "{" + a_deco.style + "}",
19670 delete a_deco.style
19671
19672 x += a_deco.dx;
19673 y += a_deco.dy;
19674 out_XYAB('<text x="X" y="Y" class="A"B>', x, y,
19675 name, a_deco.anchor || "");
19676 set_font("annotation");
19677 out_str(de.dd.str)
19678 output += '</text>\n'
19679 }
19680
19681 function out_arp(x, y, val) {
19682 g_open(x, y, 270);
19683 x = 0;
19684 val = Math.ceil(val / 6)
19685 while (--val >= 0) {
19686 xygl(x, 6, "ltr");
19687 x += 6
19688 }
19689 g_close()
19690 }
19691 function out_cresc(x, y, val, defl) {
19692 x += val * stv_g.scale
19693 val = -val;
19694 out_XYAB('<path class="stroke"\n\
19695 d="mX YlF ', x, y, val)
19696 if (defl.nost)
19697 output += '-2.2m0 -3.6l' + (-val).toFixed(1) + ' -2.2"/>\n'
19698 else
19699 output += '-4l' + (-val).toFixed(1) + ' -4"/>\n'
19700
19701 }
19702 function out_dim(x, y, val, defl) {
19703 out_XYAB('<path class="stroke"\n\
19704 d="mX YlF ', x, y, val)
19705 if (defl.noen)
19706 output += '-2.2m0 -3.6l' + (-val).toFixed(1) + ' -2.2"/>\n'
19707 else
19708 output += '-4l' + (-val).toFixed(1) + ' -4"/>\n'
19709 }
19710 function out_ltr(x, y, val) {
19711 y += 4;
19712 val = Math.ceil(val / 6)
19713 while (--val >= 0) {
19714 xygl(x, y, "ltr");
19715 x += 6
19716 }
19717 }
19718 Abc.prototype.out_lped = function(x, y, val, defl) {
19719 if (!defl.nost)
19720 xygl(x, y, "ped");
19721 if (!defl.noen)
19722 xygl(x + val + 6, y, "pedoff")
19723 }
19724 function out_8va(x, y, val, defl) {
19725 if (val < 18) {
19726 val = 18
19727 x -= 4
19728 }
19729 if (!defl.nost) {
19730 out_XYAB('<text x="X" y="Y" \
19731 style="font:italic bold 12px text,serif">8\
19732 <tspan dy="-4" style="font-size:10px">va</tspan></text>\n',
19733 x - 8, y);
19734 x += 12;
19735 val -= 12
19736 }
19737 y += 6;
19738 out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhF"/>\n',
19739 x, y, val)
19740 if (!defl.noen)
19741 out_XYAB('<path class="stroke" d="mX Yv6"/>\n', x + val, y)
19742 }
19743 function out_8vb(x, y, val, defl) {
19744 if (val < 18) {
19745 val = 18
19746 x -= 4
19747 }
19748 if (!defl.nost) {
19749 out_XYAB('<text x="X" y="Y" \
19750 style="font:italic bold 12px text,serif">8\
19751 <tspan dy=".5" style="font-size:10px">vb</tspan></text>\n',
19752 x - 8, y);
19753 x += 10
19754 val -= 10
19755 }
19756 // y -= 2;
19757 out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhF"/>\n',
19758 x, y, val)
19759 if (!defl.noen)
19760 out_XYAB('<path class="stroke" d="mX Yv-6"/>\n', x + val, y)
19761 }
19762 function out_15ma(x, y, val, defl) {
19763 if (val < 25) {
19764 val = 25
19765 x -= 6
19766 }
19767 if (!defl.nost) {
19768 out_XYAB('<text x="X" y="Y" \
19769 style="font:italic bold 12px text,serif">15\
19770 <tspan dy="-4" style="font-size:10px">ma</tspan></text>\n',
19771 x - 10, y);
19772 x += 20;
19773 val -= 20
19774 }
19775 y += 6;
19776 out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhF"/>\n',
19777 x, y, val)
19778 if (!defl.noen)
19779 out_XYAB('<path class="stroke" d="mX Yv6"/>\n', x + val, y)
19780 }
19781 function out_15mb(x, y, val, defl) {
19782 if (val < 24) {
19783 val = 24
19784 x -= 5
19785 }
19786 if (!defl.nost) {
19787 out_XYAB('<text x="X" y="Y" \
19788 style="font:italic bold 12px text,serif">15\
19789 <tspan dy=".5" style="font-size:10px">mb</tspan></text>\n',
19790 x - 10, y);
19791 x += 18
19792 val -= 18
19793 }
19794 // y -= 2;
19795 out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhF"/>\n',
19796 x, y, val)
19797 if (!defl.noen)
19798 out_XYAB('<path class="stroke" d="mX Yv-6"/>\n', x + val, y)
19799 }
19800 var deco_val_tb = {
19801 arp: out_arp,
19802 cresc: out_cresc,
19803 dim: out_dim,
19804 ltr: out_ltr,
19805 lped: function(x, y, val, defl) {
19806 self.out_lped(x, y, val, defl)
19807 },
19808 "8va": out_8va,
19809 "8vb": out_8vb,
19810 "15ma": out_15ma,
19811 "15mb": out_15mb
19812 }
19813
19814 function out_deco_val(x, y, name, val, defl) {
19815 if (deco_val_tb[name])
19816 deco_val_tb[name](x, y, val, defl)
19817 else
19818 error(1, null, "No function for decoration '$1'", name)
19819 }
19820
19821 function out_glisq(x2, y2, de) {
19822 var ar, a, len,
19823 de1 = de.start,
19824 x1 = de1.x,
19825 y1 = de1.y + staff_tb[de1.st].y,
19826 dx = x2 - x1,
19827 dy = self.sh(y1 - y2)
19828
19829 if (!stv_g.g)
19830 dx /= stv_g.scale
19831
19832 ar = Math.atan2(dy, dx)
19833 a = ar / Math.PI * 180
19834 len = (dx - (de1.s.dots ? 13 + de1.s.xmx : 8)
19835 - 8 - (de.s.notes[0].shac || 0))
19836 / Math.cos(ar)
19837
19838 g_open(x1, y1, a);
19839 x1 = de1.s.dots ? 13 + de1.s.xmx : 8;
19840 len = len / 6 | 0
19841 if (len < 1)
19842 len = 1
19843 while (--len >= 0) {
19844 xygl(x1, 0, "ltr");
19845 x1 += 6
19846 }
19847 g_close()
19848 }
19849
19850 function out_gliss(x2, y2, de) {
19851 var ar, a, len,
19852 de1 = de.start,
19853 x1 = de1.x,
19854 y1 = de1.y + staff_tb[de1.st].y,
19855 dx = x2 - x1,
19856 dy = self.sh(y1 - y2)
19857
19858 if (!stv_g.g)
19859 dx /= stv_g.scale
19860
19861 ar = Math.atan2(dy, dx)
19862 a = ar / Math.PI * 180
19863 len = (dx - (de1.s.dots ? 13 + de1.s.xmx : 8)
19864 - 8 - (de.s.notes[0].shac || 0))
19865 / Math.cos(ar)
19866
19867 g_open(x1, y1, a);
19868 xypath(de1.s.dots ? 13 + de1.s.xmx : 8, 0)
19869 output += 'h' + len.toFixed(1) + '" stroke-width="1"/>\n';
19870 g_close()
19871 }
19872
19873 var deco_l_tb = {
19874 glisq: out_glisq,
19875 gliss: out_gliss
19876 }
19877
19878 function out_deco_long(x, y, de) {
19879 var s, p_v, m, nt, i,
19880 name = de.dd.glyph,
19881 de1 = de.start
19882
19883 if (!deco_l_tb[name]) {
19884 error(1, null, "No function for decoration '$1'", name)
19885 return
19886 }
19887
19888 // if no start or no end, get the y offset of the other end
19889 p_v = de.s.p_v // voice
19890 if (de.defl.noen) { // if no end
19891 s = p_v.s_next // start of the next music line
19892 while (s && !s.dur)
19893 s = s.next
19894 if (s) {
19895 for (m = 0; m <= s.nhd; m++) {
19896 nt = s.notes[m]
19897 if (!nt.a_dd)
19898 continue
19899 for (i = 0; i < nt.a_dd.length; i++) {
19900 if (nt.a_dd[i].name == de.dd.name) {
19901 y = 3 * (nt.pit - 18)
19902 + staff_tb[de.s.st].y
19903 break
19904 }
19905 }
19906 }
19907 }
19908 x += 8 // (there is no note width)
19909 } else if (de.defl.nost) { // no start
19910 s = p_v.s_prev // end of the previous music line
19911 while (s && !s.dur)
19912 s = s.prev
19913 if (s) {
19914 for (m = 0; m <= s.nhd; m++) {
19915 nt = s.notes[m]
19916 if (!nt.a_dd)
19917 continue
19918 for (i = 0; i < nt.a_dd.length; i++) {
19919 if (nt.a_dd[i].name == de1.dd.name) {
19920 de1.y = 3 * (nt.pit - 18)
19921 break
19922 }
19923 }
19924 }
19925 }
19926 de1.x -= 8 // (there is no note width)
19927 }
19928 deco_l_tb[name](x, y, de)
19929 }
19930
19931 // add a tempo note in 'str' and return its number of characters
19932 function tempo_note(str, s, dur, dy) {
19933 var p,
19934 elts = identify_note(s, dur)
19935
19936 switch (elts[0]) { // head
19937 case C.OVAL:
19938 p = "\ueca2"
19939 break
19940 case C.EMPTY:
19941 p = "\ueca3"
19942 break
19943 default:
19944 switch (elts[2]) { // flags
19945 case 2:
19946 p = "\ueca9"
19947 break
19948 case 1:
19949 p = "\ueca7"
19950 break
19951 default:
19952 p = "\ueca5"
19953 break
19954 }
19955 break
19956 }
19957 str.push('<tspan\nclass="' +
19958 font_class(cfmt.musicfont) +
19959 '" style="font-size:' +
19960 (gene.curfont.size * 1.3).toFixed(1) + 'px"' +
19961 dy + '>' +
19962 p + '</tspan>'
19963 + (elts[1] ? '\u2009.' : '')) // dot
19964 return elts[1] ? 2 : 1
19965 } // tempo_note()
19966
19967 // build the tempo string
19968 function tempo_build(s) {
19969 var i, j, bx, p, wh, dy, h,
19970 w = 0,
19971 str = []
19972
19973 if (s.tempo_str) // already done
19974 return
19975
19976 // the music font must be defined
19977 if (!cfmt.musicfont.used)
19978 get_font("music")
19979
19980 set_font("tempo")
19981 h = gene.curfont.size
19982 if (s.tempo_str1) {
19983 str.push(s.tempo_str1)
19984 w += strwh(s.tempo_str1)[0]
19985 }
19986 if (s.tempo_notes) {
19987 dy = ' dy="-1"' // notes a bit higher
19988 h *= 1.3
19989 for (i = 0; i < s.tempo_notes.length; i++) {
19990 j = tempo_note(str, s, s.tempo_notes[i], dy)
19991 w += j * gene.curfont.swfac
19992 dy = ''
19993 }
19994 str.push('<tspan dy="1">=</tspan>')
19995 w += cwidf('=')
19996 if (s.tempo_ca) {
19997 str.push(s.tempo_ca)
19998 w += strwh(s.tempo_ca)[0]
19999 j = s.tempo_ca.length + 1
20000 }
20001 if (s.tempo) { // with a number of beats per minute
20002 str.push(s.tempo)
20003 w += strwh(s.tempo.toString())[0]
20004 } else { // with a beat as a note
20005 j = tempo_note(str, s, s.new_beat, ' dy="-1"')
20006 w += j * gene.curfont.swfac
20007 dy = 'y'
20008 }
20009 }
20010 if (s.tempo_str2) {
20011 if (dy)
20012 str.push('<tspan\n\tdy="1">' +
20013 s.tempo_str2 + '</tspan>')
20014 else
20015 str.push(s.tempo_str2)
20016 w += strwh(s.tempo_str2)[0]
20017 }
20018
20019 // build the string
20020 s.tempo_str = str.join(' ')
20021 w += cwidf(' ') * (str.length - 1)
20022 s.tempo_wh = [w, h]
20023 } // tempo_build()
20024
20025 // output a tempo
20026 function writempo(s, x, y) {
20027 var bh
20028
20029 set_font("tempo")
20030 if (gene.curfont.box) {
20031 gene.curfont.box = false
20032 bh = s.tempo_wh[1] + 2
20033 }
20034
20035 //fixme: xy_str() cannot be used because <tspan> in s.tempo_str
20036 //fixme: then there cannot be font changes by "$n" in the Q: texts
20037 output += '<text class="' + font_class(gene.curfont) +
20038 '" x="'
20039 out_sxsy(x, '" y="', y + gene.curfont.size * .22)
20040 output += '">' + s.tempo_str + '</text>\n'
20041
20042 if (bh) {
20043 gene.curfont.box = true
20044 output += '<rect class="stroke" x="'
20045 out_sxsy(x - 2, '" y="', y + bh - 1)
20046 output += '" width="' + (s.tempo_wh[0] + 4).toFixed(1) +
20047 '" height="' + bh.toFixed(1) +
20048 '"/>\n'
20049 }
20050
20051 // don't display anymore
20052 s.invis = true
20053 } // writempo()
20054
20055 // update the vertical offset
20056 function vskip(h) {
20057 posy += h
20058 }
20059
20060 // clear the styles
20061 function clr_sty() {
20062 font_style = ''
20063 if (cfmt.fullsvg) {
20064 defined_glyph = {}
20065 for (var i = 0; i < abc2svg.font_tb.length; i++)
20066 abc2svg.font_tb[i].used = 0 //false
20067 ff.used = 0 //false // clear the font-face
20068 } else {
20069 style =
20070 fulldefs = ''
20071 }
20072 } // clr_sty()
20073
20074 // create the SVG image of the block
20075 function svg_flush() {
20076 if (multicol || !user.img_out || posy == 0)
20077 return
20078
20079 var i, font,
20080 fmt = tsnext ? tsnext.fmt : cfmt,
20081 w = Math.ceil((fmt.trimsvg || fmt.singleline == 1)
20082 ? (cfmt.leftmargin + img.wx * cfmt.scale + cfmt.rightmargin + 2)
20083 : img.width),
20084 head = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"\n\
20085 xmlns:xlink="http://www.w3.org/1999/xlink"\n\
20086 fill="currentColor" stroke-width=".7"',
20087 g = ''
20088
20089 glout()
20090
20091 if (cfmt.fgcolor)
20092 head += ' color="' + cfmt.fgcolor + '"'
20093 font = get_font("music")
20094 head += ' class="' + font_class(font) +
20095 ' tune' + tunes.length + '"\n' // tune index for play
20096
20097 posy *= cfmt.scale
20098 if (user.imagesize != undefined)
20099 head += user.imagesize
20100 else
20101 head += ' width="' + w
20102 + 'px" height="' + posy.toFixed(2) + 'px"'
20103 head += ' viewBox="0 0 ' + w + ' '
20104 + posy.toFixed(2) + '">\n'
20105 head += fulldefs
20106 if (cfmt.bgcolor)
20107 head += '<rect width="100%" height="100%" fill="'
20108 + cfmt.bgcolor + '"/>\n'
20109
20110 if (style || font_style)
20111 head += '<style>' + font_style + style + '\n</style>\n'
20112
20113 if (defs)
20114 head += '<defs>' + defs + '\n</defs>\n'
20115
20116 // if %%pagescale != 1, do a global scale
20117 // (with a container: transform scale in <svg> does not work
20118 // the same in all browsers)
20119 // the class is used to know that the container is global
20120 if (cfmt.scale != 1) {
20121 head += '<g class="g" transform="scale(' +
20122 cfmt.scale + ')">\n';
20123 g = '</g>\n'
20124 }
20125
20126 if (psvg) // if PostScript support
20127 psvg.ps_flush(true); // + setg(0)
20128
20129 // start a block if needed
20130 if (parse.state == 1 && user.page_format && !blkdiv)
20131 blkdiv = 1 // new tune
20132 if (blkdiv > 0) {
20133 user.img_out(blkdiv == 1 ?
20134 '<div class="nobrk">' :
20135 '<div class="nobrk newpage">')
20136 blkdiv = -1 // block started
20137 } else if (blkdiv < 0 && cfmt.splittune) {
20138 i = 1 // header and first music line
20139 blkdiv = 0
20140 }
20141 user.img_out(head + output + g + "</svg>");
20142 if (i)
20143 user.img_out("</div>")
20144 output = ""
20145
20146 clr_sty()
20147 defs = '';
20148 posy = 0
20149 img.wx = 0 // space used between the margins
20150 }
20151
20152 // mark the end of a <div> block
20153 function blk_flush() {
20154 svg_flush()
20155 if (blkdiv < 0 && !parse.state) {
20156 user.img_out('</div>')
20157 blkdiv = 0
20158 }
20159 }
20160 Abc.prototype.blk_flush = blk_flush
20161 // abc2svg - tune.js - tune generation
20162 //
20163 // Copyright (C) 2014-2025 Jean-Francois Moine
20164 //
20165 // This file is part of abc2svg-core.
20166 //
20167 // abc2svg-core is free software: you can redistribute it and/or modify
20168 // it under the terms of the GNU Lesser General Public License as published by
20169 // the Free Software Foundation, either version 3 of the License, or
20170 // (at your option) any later version.
20171 //
20172 // abc2svg-core is distributed in the hope that it will be useful,
20173 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20174 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20175 // GNU Lesser General Public License for more details.
20176 //
20177 // You should have received a copy of the GNU Lesser General Public License
20178 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
20179
20180 var par_sy, // current staff system for parse
20181 cur_sy, // current staff system for generation
20182 voice_tb,
20183 curvoice,
20184 staves_found,
20185 vover, // voice overlay
20186 tsfirst
20187
20188 /* apply the %%voice options of the current voice */
20189 function voice_filter() {
20190 var opt
20191
20192 function vfilt(opts, opt) {
20193 var i,
20194 sel = new RegExp(opt)
20195
20196 if (sel.test(curvoice.id)
20197 || sel.test(curvoice.nm)) {
20198 for (i = 0; i < opts.length; i++)
20199 self.do_pscom(opts[i])
20200 }
20201 }
20202
20203 // global
20204 if (parse.voice_opts)
20205 for (opt in parse.voice_opts) {
20206 if (parse.voice_opts.hasOwnProperty(opt))
20207 vfilt(parse.voice_opts[opt], opt)
20208 }
20209
20210 // tune
20211 if (parse.tune_v_opts)
20212 for (opt in parse.tune_v_opts) {
20213 if (parse.tune_v_opts.hasOwnProperty(opt))
20214 vfilt(parse.tune_v_opts[opt], opt)
20215 }
20216 }
20217
20218 /* -- link a ABC symbol into the current voice -- */
20219 // if a voice is ignored (not in %%staves) don't link the symbol
20220 // but update the time for P: and Q:
20221 function sym_link(s) {
20222 var tim = curvoice.time
20223
20224 if (!s.fname)
20225 set_ref(s)
20226 if (!curvoice.ignore) {
20227 s.prev = curvoice.last_sym
20228 if (curvoice.last_sym)
20229 curvoice.last_sym.next = s
20230 else
20231 curvoice.sym = s
20232 } else if (s.bar_type) {
20233 curvoice.last_bar = s
20234 }
20235 curvoice.last_sym = s
20236 s.v = curvoice.v;
20237 s.p_v = curvoice;
20238 s.st = curvoice.cst;
20239 s.time = tim
20240 if (s.dur && !s.grace)
20241 curvoice.time += s.dur;
20242 parse.ufmt = true
20243 s.fmt = cfmt // global parameters
20244 s.pos = curvoice.pos
20245 if (curvoice.second)
20246 s.second = true
20247 if (curvoice.floating)
20248 s.floating = true
20249 if (curvoice.eoln) {
20250 s.soln = true
20251 curvoice.eoln = false
20252 }
20253 }
20254
20255 /* -- add a new symbol in a voice -- */
20256 function sym_add(p_voice, type) {
20257 var s = {
20258 type:type,
20259 dur:0
20260 },
20261 s2,
20262 p_voice2 = curvoice;
20263
20264 curvoice = p_voice;
20265 sym_link(s);
20266 curvoice = p_voice2;
20267 s2 = s.prev
20268 if (!s2)
20269 s2 = s.next
20270 if (s2) {
20271 s.fname = s2.fname;
20272 s.istart = s2.istart;
20273 s.iend = s2.iend
20274 }
20275 return s
20276 }
20277
20278 /* -- sort all symbols by time and vertical sequence -- */
20279 // weight of the symbols !! depends on the symbol type !!
20280 var w_tb = new Uint8Array([
20281 6, // bar
20282 2, // clef
20283 8, // custos
20284 6, // sm (sequence marker, after bar)
20285 7, // grace
20286 3, // key
20287 4, // meter
20288 9, // mrest
20289 9, // note
20290 0, // part
20291 9, // rest
20292 5, // space (before bar)
20293 0, // staves
20294 1, // stbrk
20295 0, // tempo
20296 0, // (free)
20297 0, // block
20298 0 // remark
20299 ])
20300
20301 function sort_all() {
20302 var s, s2, time, w, wmin, ir, fmt, v, p_voice, prev,
20303 fl, new_sy,
20304 nv = voice_tb.length,
20305 vtb = [],
20306 vn = [], // voice indexed by range
20307 sy = cur_sy // first staff system
20308
20309 // check if different bars at the same time
20310 function b_chk() {
20311 var bt, s, s2, v, t,
20312 ir = 0
20313
20314 while (1) {
20315 v = vn[ir++]
20316 if (v == undefined)
20317 break
20318 s = vtb[v]
20319 if (!s || !s.bar_type || s.invis
20320 || s.time != time)
20321 continue
20322 if (!bt) {
20323 bt = s.bar_type
20324 if (s.text && bt == '|')
20325 t = s.text
20326 continue
20327 }
20328 if (s.bar_type != bt)
20329 break
20330 if (s.text && !t && bt == '|') {
20331 t = s.text
20332 break
20333 }
20334 }
20335
20336 if (v == undefined)
20337 return // no problem
20338
20339 // change "::" to ":| |:"
20340 // and "|1" to "| [1"
20341 if (bt == "::" || bt == ":|"
20342 || t) {
20343 ir = 0
20344 bt = t ? '|' : "::"
20345 while (1) {
20346 v = vn[ir++]
20347 if (v == undefined)
20348 break
20349 s = vtb[v]
20350 if (!s || s.invis
20351 || s.bar_type != bt
20352 || (bt == '|' && !s.text))
20353 continue
20354 s2 = clone(s)
20355 if (bt == "::") {
20356 s.bar_type = ":|"
20357 s2.bar_type = "|:"
20358 } else {
20359 // s.bar_type = '|'
20360 delete s.text
20361 delete s.rbstart
20362 s2.bar_type = '['
20363 s2.invis = 1 //true
20364 s2.xsh = 0
20365 }
20366 s2.next = s.next
20367 if (s2.next)
20368 s2.next.prev = s2
20369 s2.prev = s
20370 s.next = s2
20371 }
20372 } else {
20373 error(1, s, "Different bars $1 and $2",
20374 (bt + (t || '')), (s.bar_type + (s.text || '')))
20375 }
20376 } // b_chk()
20377
20378 // set the first symbol of each voice
20379 for (v = 0; v < nv; v++) {
20380 s = voice_tb[v].sym
20381 vtb[v] = s
20382 if (sy.voices[v]) {
20383 vn[sy.voices[v].range] = v
20384 if (!prev && s) {
20385 fmt = s.fmt
20386 p_voice = voice_tb[v]
20387 prev = { // symbol defining the first staff system
20388 type: C.STAVES,
20389 fname: parse.fname,
20390 dur: 0,
20391 v: v,
20392 p_v: p_voice,
20393 time: 0,
20394 st: 0,
20395 sy: sy,
20396 next: s,
20397 fmt: fmt,
20398 seqst: true
20399 }
20400 }
20401 }
20402 }
20403
20404 if (!prev)
20405 return // no symbol yet
20406
20407 // insert the first staff system in the first voice
20408 p_voice.sym = tsfirst = s = prev
20409 if (s.next)
20410 s.next.prev = s
20411 else
20412 p_voice.last_sym = s
20413
20414 // if Q: from tune header, put it at start of the music
20415 // (after the staff system)
20416 s = glovar.tempo
20417 if (s) {
20418 s.v = v = p_voice.v
20419 s.p_v = p_voice
20420 s.st = 0
20421 s.time = 0
20422 s.prev = prev
20423 s.next = prev.next
20424 if (s.next)
20425 s.next.prev = s
20426 else
20427 p_voice.last_sym = s
20428 s.prev.next = s
20429 s.fmt = fmt
20430 glovar.tempo = null
20431 vtb[v] = s
20432 }
20433
20434 // if only one voice, quickly create the time links
20435 if (nv == 1) {
20436 s = tsfirst
20437 s.ts_next = s.next
20438 while (1) {
20439 s = s.next
20440 if (!s)
20441 return
20442 if (s.time != s.prev.time
20443 || w_tb[s.prev.type])
20444 s.seqst = 1 //true
20445 if (s.type == C.PART) { // move the part
20446 s.prev.next =
20447 s.prev.ts_next = s.next
20448 if (s.next) {
20449 s.next.part = s // to the next symbol
20450 s.next.prev = s.prev
20451 if (s.soln)
20452 s.next.soln = 1 //true
20453 if (s.seqst)
20454 s.next.seqst = 1 //true
20455 }
20456 continue
20457 }
20458 s.ts_prev = s.prev
20459 s.ts_next = s.next
20460 }
20461 // not reached
20462 }
20463
20464 // loop on the symbols of all voices
20465 while (1) {
20466 if (new_sy) {
20467 sy = new_sy;
20468 new_sy = null;
20469 vn.length = 0
20470 for (v = 0; v < nv; v++) {
20471 if (!sy.voices[v])
20472 continue
20473 vn[sy.voices[v].range] = v
20474 }
20475 }
20476
20477 /* search the min time and symbol weight */
20478 wmin = time = 10000000 // big int
20479 ir = 0
20480 while (1) {
20481 v = vn[ir++]
20482 if (v == undefined)
20483 break
20484 s = vtb[v]
20485 if (!s || s.time > time)
20486 continue
20487 w = w_tb[s.type]
20488 if (s.type == C.GRACE
20489 && s.next
20490 && s.next.type == C.BAR)
20491 w = 5 // < bar
20492 if (s.time < time) {
20493 time = s.time;
20494 wmin = w
20495 } else if (w < wmin) {
20496 wmin = w
20497 }
20498 }
20499
20500 if (wmin > 127)
20501 break // done
20502
20503 // check the type of the measure bars
20504 if (wmin == 6) // !! weight of bars
20505 b_chk()
20506
20507 /* link the vertical sequence */
20508 ir = 0
20509 while (1) {
20510 v = vn[ir++]
20511 if (v == undefined)
20512 break
20513 s = vtb[v]
20514 if (!s
20515 || s.time != time)
20516 continue
20517 w = w_tb[s.type]
20518 if (s.type == C.GRACE
20519 && s.next
20520 && s.next.type == C.BAR)
20521 w = 5 // < bar
20522 if (w != wmin)
20523 continue
20524 if (!w
20525 && s.type == C.PART) { // move the part
20526 if (s.prev)
20527 s.prev.next = s.next
20528 else
20529 s.p_v.sym = s.next
20530 vtb[v] = s.next
20531 if (s.next) {
20532 s.next.part = s // to the next symbol
20533 s.next.prev = s.prev
20534 if (s.soln)
20535 s.next.soln = 1 //true
20536 // } else {
20537 // ignored
20538 }
20539 continue
20540 }
20541 if (s.type == C.STAVES)
20542 new_sy = s.sy
20543 if (fl) {
20544 fl = 0;
20545 s.seqst = true
20546 }
20547 s.ts_prev = prev
20548 prev.ts_next = s
20549 prev = s
20550
20551 vtb[v] = s.next
20552 }
20553 if (wmin) // if some width
20554 fl = 1 //true // start a new sequence
20555 }
20556 }
20557
20558 // adjust some voice elements
20559 // (possible hook)
20560 Abc.prototype.voice_adj = function (sys_chg) {
20561 var p_voice, s, s2, v, sl
20562
20563 // insert the delayed P: and Q: in the top_voice
20564 function ins_pq() {
20565 var s, s2,
20566 p_v = voice_tb[par_sy.top_voice]
20567
20568 while (1) {
20569 s = parse.pq_d.shift()
20570 if (!s)
20571 break
20572 for (s2 = p_v.sym; ; s2 = s2.next) {
20573 if (s2.time >= s.time
20574 && s2.dur) {
20575 s.next = s2
20576 s.prev = s2.prev
20577 s.prev.next =
20578 s2.prev = s
20579 s.v = s2.v
20580 s.p_v = p_v
20581 s.st = s2.st
20582 break
20583 }
20584 }
20585 }
20586 } // ins_pq()
20587
20588 // set the duration of the notes under a feathered beam
20589 function set_feathered_beam(s1) {
20590 var s, s2, t, d, b, i, a,
20591 d = s1.dur,
20592 n = 1
20593
20594 /* search the end of the beam */
20595 for (s = s1; s; s = s.next) {
20596 if (s.beam_end || !s.next)
20597 break
20598 n++
20599 }
20600 if (n <= 1) {
20601 delete s1.feathered_beam
20602 return
20603 }
20604 s2 = s;
20605 b = d / 2; /* smallest note duration */
20606 a = d / (n - 1); /* delta duration */
20607 t = s1.time
20608 if (s1.feathered_beam > 0) { /* !beam-accel! */
20609 for (s = s1, i = n - 1;
20610 s != s2;
20611 s = s.next, i--) {
20612 d = ((a * i) | 0) + b;
20613 s.dur = d;
20614 s.time = t;
20615 t += d
20616 }
20617 } else { /* !beam-rall! */
20618 for (s = s1, i = 0;
20619 s != s2;
20620 s = s.next, i++) {
20621 d = ((a * i) | 0) + b;
20622 s.dur = d;
20623 s.time = t;
20624 t += d
20625 }
20626 }
20627 s.dur = s.time + s.dur - t;
20628 s.time = t
20629 } // end set_feathered_beam()
20630
20631 // terminate voice cloning
20632 if (curvoice && curvoice.clone) {
20633 parse.istart = parse.eol
20634 do_cloning()
20635 }
20636
20637 // if only one voice and a time skip,
20638 // fill the voice with the sequence "Z |" (multi-rest and bar)
20639 if (par_sy.one_v) // if one voice
20640 fill_mr_ba(voice_tb[par_sy.top_voice])
20641
20642 if (parse.pq_d)
20643 ins_pq() // insert delayed P: and Q:
20644
20645 for (v = 0; v < voice_tb.length; v++) {
20646 p_voice = voice_tb[v]
20647 if (!sys_chg) { // if not %%score
20648 delete p_voice.eoln
20649 while (1) { // set the end of slurs
20650 sl = p_voice.sls.shift()
20651 if (!sl)
20652 break
20653 s = sl.ss
20654 // error(1, s, "Lack of ending slur(s)")
20655 if (!s.sls)
20656 s.sls = []
20657 sl.loc = 'o' // no slur end
20658 s.sls.push(sl)
20659 }
20660 } // not %%score
20661 for (s = p_voice.sym; s; s = s.next) {
20662 if (s.time >= staves_found)
20663 break
20664 }
20665 for ( ; s; s = s.next) {
20666
20667 // if the symbol has a sequence weight smaller than the bar one
20668 // and if there a time skip,
20669 // add an invisible bar before it
20670 if (w_tb[s.type] < 5
20671 && s.type != C.STAVES
20672 && s.type != C.CLEF
20673 && s.time // not at start of tune
20674 && (!s.prev || s.time > s.prev.time + s.prev.dur)) {
20675 s2 = {
20676 type: C.BAR,
20677 bar_type: "[]",
20678 v: s.v,
20679 p_v: s.p_v,
20680 st: s.st,
20681 time: s.time,
20682 dur:0,
20683 next: s,
20684 prev: s.prev,
20685 fmt: s.fmt,
20686 invis: 1
20687 }
20688 if (s.prev)
20689 s.prev.next = s2
20690 else
20691 voice_tb[s.v].sym = s2
20692 s.prev = s2
20693 }
20694
20695 switch (s.type) {
20696 case C.GRACE:
20697 if (!cfmt.graceword)
20698 continue
20699 for (s2 = s.next; s2; s2 = s2.next) {
20700 switch (s2.type) {
20701 case C.SPACE:
20702 continue
20703 case C.NOTE:
20704 if (!s2.a_ly)
20705 break
20706 s.a_ly = s2.a_ly;
20707 s2.a_ly = null
20708 break
20709 }
20710 break
20711 }
20712 continue
20713 case C.NOTE:
20714 if (s.feathered_beam)
20715 set_feathered_beam(s)
20716 break
20717 }
20718 }
20719 }
20720 }
20721
20722 /* -- create a new staff system -- */
20723 function new_syst(init) {
20724 var st, v, sy_staff, p_voice,
20725 sy_new = {
20726 voices: [],
20727 staves: [],
20728 top_voice: 0
20729 }
20730
20731 if (init) { /* first staff system */
20732 cur_sy = par_sy = sy_new
20733 return
20734 }
20735
20736 // update the previous system
20737 for (v = 0; v < voice_tb.length; v++) {
20738 if (par_sy.voices[v]) {
20739 st = par_sy.voices[v].st
20740 sy_staff = par_sy.staves[st]
20741 p_voice = voice_tb[v]
20742
20743 sy_staff.staffnonote = p_voice.staffnonote
20744 if (p_voice.staffscale)
20745 sy_staff.staffscale = p_voice.staffscale;
20746 }
20747 }
20748 for (st = 0; st < par_sy.staves.length; st++) {
20749 sy_new.staves[st] = clone(par_sy.staves[st]);
20750 sy_new.staves[st].flags = 0
20751 }
20752 par_sy.next = sy_new;
20753 par_sy = sy_new
20754 }
20755
20756 /* -- set the bar numbers -- */
20757 // (possible hook)
20758 Abc.prototype.set_bar_num = function() {
20759 var s, s2, rep_tim, k, n, nu, txt,
20760 tim = 0, // time of the previous bar
20761 bar_num = gene.nbar,
20762 bar_tim = 0, // time of previous repeat variant
20763 ptim = 0, // time of previous bar
20764 wmeasure = voice_tb[cur_sy.top_voice].meter.wmeasure
20765
20766 // check the measure duration
20767 function check_meas() {
20768 var s3
20769
20770 if (tim > ptim + wmeasure
20771 && s.prev.type != C.MREST)
20772 return 1 //true
20773
20774 // the measure is too short,
20775 // check if there is a bar a bit further
20776 for (s3 = s.next; s3 && s3.time == s.time; s3 = s3.next)
20777 ;
20778 for ( ; s3 && !s3.bar_type; s3 = s3.next)
20779 ;
20780 return s3 && (s3.time - bar_tim) % wmeasure
20781 }
20782
20783 // don't count a bar at start of tune
20784 for (s = tsfirst; ; s = s.ts_next) {
20785 if (!s)
20786 return
20787 switch (s.type) {
20788 case C.METER:
20789 wmeasure = s.wmeasure
20790 // fall thru
20791 case C.CLEF:
20792 case C.KEY:
20793 case C.STBRK:
20794 continue
20795 case C.BAR:
20796 if (s.bar_num)
20797 bar_num = s.bar_num // %%setbarnb)
20798 break
20799 }
20800 break
20801 }
20802
20803 // at start of tune, check for an anacrusis
20804 for (s2 = s.ts_next; s2; s2 = s2.ts_next) {
20805 if (s2.type == C.BAR && s2.time
20806 && !s2.invis && !s2.bar_dotted) {
20807 if (s2.time < wmeasure) { // if anacrusis
20808 s = s2
20809 bar_tim = s.time
20810 }
20811 break
20812 }
20813 }
20814
20815 // set the measure number on the top bars
20816 for ( ; s; s = s.ts_next) {
20817 switch (s.type) {
20818 case C.METER:
20819 if (wmeasure != 1) // if not M:none
20820 bar_num += (s.time - bar_tim) / wmeasure
20821 bar_tim = s.time
20822 wmeasure = s.wmeasure
20823 while (s.ts_next && s.ts_next.wmeasure)
20824 s = s.ts_next
20825 break
20826 case C.BAR:
20827 if (s.time <= tim)
20828 break // already seen
20829 tim = s.time
20830
20831 nu = 1 //true // no num update
20832 txt = ""
20833 for (s2 = s; s2; s2 = s2.next) {
20834 if (s2.time > tim)
20835 break
20836 if (!s2.bar_type)
20837 continue
20838 if (s2.bar_type != '[')
20839 nu = 0 //false // do update
20840 if (s2.text)
20841 txt = s2.text
20842 }
20843 if (s.bar_num) {
20844 bar_num = s.bar_num // (%%setbarnb)
20845 ptim = bar_tim = tim
20846 break
20847 }
20848 if (wmeasure == 1) { // if M:none
20849 if (s.bar_dotted)
20850 break
20851 if (txt) {
20852 if (!cfmt.contbarnb) {
20853 if (txt[0] == '1')
20854 rep_tim = bar_num
20855 else
20856 bar_num = rep_tim
20857 }
20858 }
20859 if (!nu)
20860 s.bar_num = ++bar_num
20861 break
20862 }
20863
20864 n = bar_num + (tim - bar_tim) / wmeasure
20865 k = n - (n | 0)
20866 if (cfmt.checkbars
20867 && k
20868 && check_meas())
20869 error(0, s, "Bad measure duration")
20870 if (tim > ptim + wmeasure) { // if more than one measure
20871 n |= 0
20872 k = 0
20873 bar_tim = tim // re-synchronize
20874 bar_num = n
20875 }
20876
20877 if (txt) {
20878 if (txt[0] == '1') {
20879 if (!cfmt.contbarnb)
20880 rep_tim = tim - bar_tim
20881 if (!nu)
20882 s.bar_num = n
20883 } else {
20884 if (!cfmt.contbarnb)
20885 bar_tim = tim - rep_tim
20886 n = bar_num + (tim - bar_tim) / wmeasure
20887 if (n == (n | 0))
20888 s.bar_num = n
20889 }
20890 } else if (n == (n | 0)) {
20891 s.bar_num = n
20892 }
20893 if (!k)
20894 ptim = tim
20895 break
20896 }
20897 }
20898 }
20899
20900 // convert a note to ABC
20901 function not2abc(pit, acc) {
20902 var i,
20903 nn = ''
20904
20905 if (acc) {
20906 if (typeof acc != "object") {
20907 nn = ['__', '_', '', '^', '^^', '='][acc + 2]
20908 } else {
20909 i = acc[0]
20910 if (i > 0) {
20911 nn += '^'
20912 } else {
20913 nn += '_'
20914 i = -i
20915 }
20916 nn += i + '/' + acc[1]
20917 }
20918 }
20919 nn += ntb[(pit + 75) % 7]
20920 for (i = pit; i >= 23; i -= 7)
20921 nn += "'"
20922 for (i = pit; i < 16; i += 7)
20923 nn += ","
20924 return nn
20925 } // not2abc()
20926
20927 // note mapping
20928 // %%map map_name note [print [note_head]] [param]*
20929 function get_map(text) {
20930 if (!text)
20931 return
20932
20933 var i, note, notes, map, tmp, ns,
20934 ty = '',
20935 a = text.split(/\s+/)
20936
20937 if (a.length < 3) {
20938 syntax(1, errs.not_enough_p)
20939 return
20940 }
20941 ns = a[1]
20942 if (ns != '*') {
20943 if (ns.indexOf("octave,") == 0 // remove the octave part
20944 || ns.indexOf("key,") == 0
20945 || !ns.indexOf("tonic,")) {
20946 ty = ns[0]
20947 ns = ns.split(',')[1].toUpperCase()
20948 }
20949 tmp = new scanBuf
20950 tmp.buffer = ns
20951 note = parse_acc_pit(tmp)
20952 if (!note) {
20953 syntax(1, "Bad note in %%map")
20954 return
20955 }
20956 ns = ty + not2abc(note.pit, note.acc)
20957 }
20958
20959 notes = maps[a[0]]
20960 if (!notes)
20961 maps[a[0]] = notes = {}
20962 map = notes[ns]
20963 if (!map)
20964 notes[ns] = map = []
20965
20966 // try the optional 'print' and 'heads' parameters
20967 a.shift()
20968 a.shift()
20969 if (!a.length)
20970 return
20971 a = info_split(a.join(' '))
20972 i = 0
20973 if (a[0].indexOf('=') < 0) {
20974 if (a[0][0] != '*') {
20975 tmp = new scanBuf; // print
20976 tmp.buffer = a[0];
20977 map[1] = parse_acc_pit(tmp)
20978 }
20979 if (!a[1])
20980 return
20981 i++
20982 if (a[1].indexOf('=') < 0) {
20983 map[0] = a[1].split(',') // heads
20984 i++
20985 }
20986 }
20987
20988 for (; i < a.length; i++) {
20989 switch (a[i]) {
20990 case "heads=":
20991 if (!a[++i]) {
20992 syntax(1, errs.not_enough_p)
20993 break
20994 }
20995 map[0] = a[i].split(',')
20996 break
20997 case "print=":
20998 case "play=":
20999 case "print_notrp=":
21000 if (!a[++i]) {
21001 syntax(1, errs.not_enough_p)
21002 break
21003 }
21004 tmp = new scanBuf;
21005 tmp.buffer = a[i];
21006 note = parse_acc_pit(tmp)
21007 if (a[i - 1][5] == '_') // if print no transpose
21008 note.notrp = 1 //true
21009 if (a[i - 1][1] == 'r')
21010 map[1] = note
21011 else
21012 map[3] = note
21013 break
21014 case "color=":
21015 if (!a[++i]) {
21016 syntax(1, errs.not_enough_p)
21017 break
21018 }
21019 map[2] = a[i]
21020 break
21021 }
21022 }
21023 }
21024
21025 // get a abcm2ps/abcMIDI compatible transposition value as a base-40 interval
21026 // The value may be
21027 // - [+|-]<number of semitones>[s|f]
21028 // - <note1>[<note2>] % <note2> default is 'c'
21029 function get_transp(param) {
21030 if (param[0] == '0')
21031 return 0
21032 if ("123456789-+".indexOf(param[0]) >= 0) { // by semi-tone
21033 var val = parseInt(param)
21034 if (isNaN(val) || val < -36 || val > 36) {
21035 //fixme: no source reference...
21036 syntax(1, errs.bad_transp)
21037 return
21038 }
21039 val += 36
21040 val = ((val / 12 | 0) - 3) * 40 + abc2svg.isb40[val % 12]
21041 if (param.slice(-1) == 'b')
21042 val += 4
21043 return val
21044 }
21045 // return undefined
21046 } // get_transp()
21047
21048 /* -- process a pseudo-comment (%% or I:) -- */
21049 // (possible hook)
21050 Abc.prototype.do_pscom = function(text) {
21051 var h1, val, s, cmd, param, n, k, b
21052
21053 cmd = text.match(/[^\s]+/)
21054 if (!cmd)
21055 return
21056 cmd = cmd[0];
21057
21058 // ignore the command if the voice is ignored,
21059 // but not if %%score/%%staves!
21060 if (curvoice && curvoice.ignore) {
21061 switch (cmd) {
21062 case "staves":
21063 case "score":
21064 break
21065 default:
21066 return
21067 }
21068 }
21069
21070 param = text.replace(cmd, '').trim()
21071
21072 if (param.slice(-5) == ' lock') {
21073 fmt_lock[cmd] = true;
21074 param = param.slice(0, -5).trim()
21075 } else if (fmt_lock[cmd]) {
21076 return
21077 }
21078
21079 switch (cmd) {
21080 case "clef":
21081 if (parse.state >= 2) {
21082 s = new_clef(param)
21083 if (s)
21084 get_clef(s)
21085 }
21086 return
21087 case "deco":
21088 deco_add(param)
21089 return
21090 case "linebreak":
21091 set_linebreak(param)
21092 return
21093 case "map":
21094 get_map(param)
21095 return
21096 case "maxsysstaffsep":
21097 case "sysstaffsep":
21098 if (parse.state == 3) {
21099 val = get_unit(param)
21100 if (isNaN(val)) {
21101 syntax(1, errs.bad_val, "%%" + cmd)
21102 return
21103 }
21104 par_sy.voices[curvoice.v][cmd[0] == 'm' ? "maxsep" : "sep"] =
21105 val
21106 return
21107 }
21108 break
21109 case "multicol":
21110 switch (param) {
21111 case "start":
21112 case "new":
21113 case "end":
21114 break
21115 default:
21116 syntax(1, "Unknown keyword '$1' in %%multicol", param)
21117 return
21118 }
21119 s = {
21120 type: C.BLOCK,
21121 subtype: "mc_" + param,
21122 dur: 0
21123 }
21124 if (parse.state >= 2) {
21125 if (curvoice.clone)
21126 do_cloning()
21127 curvoice = voice_tb[0]
21128 curvoice.eoln = 1 //true
21129 sym_link(s)
21130 return
21131 }
21132 set_ref(s)
21133 self.block_gen(s)
21134 return
21135 case "ottava":
21136 if (parse.state != 3)
21137 return
21138 n = parseInt(param)
21139 if (isNaN(n) || n < -2 || n > 2
21140 || (!n && !curvoice.ottava)) {
21141 syntax(1, errs.bad_val, "%%ottava")
21142 return
21143 }
21144 k = n
21145 if (n) {
21146 curvoice.ottava = n
21147 } else {
21148 n = curvoice.ottava
21149 curvoice.ottava = 0
21150 }
21151 a_dcn.push(["15mb", "8vb", "", "8va", "15ma"][n + 2]
21152 + (k ? '(' : ')'))
21153 return
21154 case "repbra":
21155 if (curvoice)
21156 curvoice.norepbra = !get_bool(param)
21157 return
21158 case "repeat":
21159 if (parse.state != 3)
21160 return
21161 if (!curvoice.last_sym) {
21162 syntax(1, "%%repeat cannot start a tune")
21163 return
21164 }
21165 if (!param.length) {
21166 n = 1;
21167 k = 1
21168 } else {
21169 b = param.split(/\s+/);
21170 n = parseInt(b[0]);
21171 k = parseInt(b[1])
21172 if (isNaN(n) || n < 1
21173 || (curvoice.last_sym.type == C.BAR
21174 && n > 2)) {
21175 syntax(1, "Incorrect 1st value in %%repeat")
21176 return
21177 }
21178 if (isNaN(k)) {
21179 k = 1
21180 } else {
21181 if (k < 1) {
21182 syntax(1, "Incorrect 2nd value in %%repeat")
21183 return
21184 }
21185 }
21186 }
21187 parse.repeat_n = curvoice.last_sym.type == C.BAR ? n : -n;
21188 parse.repeat_k = k
21189 return
21190 case "sep":
21191 var h2, len, values, lwidth;
21192
21193 set_page();
21194 lwidth = img.width - img.lm - img.rm;
21195 h1 = h2 = len = 0
21196 if (param) {
21197 values = param.split(/\s+/);
21198 h1 = get_unit(values[0])
21199 if (values[1]) {
21200 h2 = get_unit(values[1])
21201 if (values[2])
21202 len = get_unit(values[2])
21203 }
21204 if (isNaN(h1) || isNaN(h2) || isNaN(len)) {
21205 syntax(1, errs.bad_val, "%%sep")
21206 return
21207 }
21208 }
21209 if (h1 < 1)
21210 h1 = 14
21211 if (h2 < 1)
21212 h2 = h1
21213 if (len < 1)
21214 len = 90
21215 if (parse.state >= 2) {
21216 if (curvoice.clone)
21217 do_cloning()
21218 s = new_block(cmd);
21219 s.x = (lwidth - len) / 2 / cfmt.scale;
21220 s.l = len / cfmt.scale;
21221 s.sk1 = h1;
21222 s.sk2 = h2
21223 return
21224 }
21225 vskip(h1);
21226 output += '<path class="stroke"\n\td="M';
21227 out_sxsy((lwidth - len) / 2 / cfmt.scale, ' ', 0);
21228 output += 'h' + (len / cfmt.scale).toFixed(1) + '"/>\n';
21229 vskip(h2);
21230 blk_flush()
21231 return
21232 case "setbarnb":
21233 val = parseInt(param)
21234 if (isNaN(val) || val < 1) {
21235 syntax(1, "Bad %%setbarnb value")
21236 break
21237 }
21238 glovar.new_nbar = val
21239 return
21240 case "staff":
21241 if (parse.state != 3)
21242 return
21243 if (curvoice.clone)
21244 do_cloning()
21245 val = parseInt(param)
21246 if (isNaN(val)) {
21247 syntax(1, "Bad %%staff value '$1'", param)
21248 return
21249 }
21250 var st
21251 if (param[0] == '+' || param[0] == '-')
21252 st = curvoice.cst + val
21253 else
21254 st = val - 1
21255 if (st < 0 || st > nstaff) {
21256 syntax(1, "Bad %%staff number $1 (cur $2, max $3)",
21257 st, curvoice.cst, nstaff)
21258 return
21259 }
21260 delete curvoice.floating;
21261 curvoice.cst = st
21262 return
21263 case "staffbreak":
21264 if (parse.state != 3)
21265 return
21266 if (curvoice.clone)
21267 do_cloning()
21268 s = {
21269 type: C.STBRK,
21270 dur:0
21271 }
21272 if (param.slice(-1) == 'f') {
21273 s.stbrk_forced = true
21274 param = param.replace(/\sf$/, '')
21275 }
21276 if (param) {
21277 val = get_unit(param)
21278 if (isNaN(val)) {
21279 syntax(1, errs.bad_val, "%%staffbreak")
21280 return
21281 }
21282 s.xmx = val
21283 } else {
21284 s.xmx = 14
21285 }
21286 sym_link(s)
21287 return
21288 case "tacet":
21289 if (param[0] == '"')
21290 param = param.slice(1, -1)
21291 // fall thru
21292 case "stafflines":
21293 case "staffscale":
21294 case "staffnonote":
21295 set_v_param(cmd, param)
21296 return
21297 case "staves":
21298 case "score":
21299 if (!parse.state)
21300 return
21301 if (parse.scores && parse.scores.length > 0) {
21302 text = parse.scores.shift();
21303 cmd = text.match(/([^\s]+)\s*(.*)/);
21304 param = cmd[2]
21305 cmd = cmd[1]
21306 }
21307 get_staves(cmd, param)
21308 return
21309 case "center":
21310 case "text":
21311 k = cmd[0] == 'c' ? 'c' : cfmt.textoption
21312 set_font("text")
21313 if (parse.state >= 2) {
21314 if (curvoice.clone)
21315 do_cloning()
21316 s = new_block("text")
21317 s.text = param
21318 s.opt = k
21319 s.font = cfmt.textfont
21320 return
21321 }
21322 write_text(param, k)
21323 return
21324 case "transpose": // (abcm2ps compatibility)
21325 if (cfmt.sound)
21326 return
21327 val = get_transp(param)
21328 if (val == undefined) { // accept note interval
21329 val = get_interval(param)
21330 if (val == undefined)
21331 return
21332 }
21333 switch (parse.state) {
21334 case 0:
21335 cfmt.transp = 0
21336 // fall thru
21337 case 1:
21338 cfmt.transp = (cfmt.transp || 0) + val
21339 return
21340 }
21341 curvoice.shift = val
21342 key_trans()
21343 return
21344 case "tune":
21345 //fixme: to do
21346 return
21347 case "user":
21348 set_user(param)
21349 return
21350 case "voicecolor":
21351 if (curvoice)
21352 curvoice.color = param
21353 return
21354 case "vskip":
21355 val = get_unit(param)
21356 if (isNaN(val)) {
21357 syntax(1, errs.bad_val, "%%vskip")
21358 return
21359 }
21360 if (val < 0) {
21361 syntax(1, "%%vskip cannot be negative")
21362 return
21363 }
21364 if (parse.state >= 2) {
21365 if (curvoice.clone)
21366 do_cloning()
21367 s = new_block(cmd);
21368 s.sk = val
21369 return
21370 }
21371 vskip(val);
21372 return
21373 case "newpage":
21374 case "leftmargin":
21375 case "rightmargin":
21376 case "pagescale":
21377 case "pagewidth":
21378 case "printmargin":
21379 case "scale":
21380 case "staffwidth":
21381 if (parse.state >= 2) {
21382 if (curvoice.clone)
21383 do_cloning()
21384 s = new_block(cmd);
21385 s.param = param
21386 return
21387 }
21388 if (cmd == "newpage") {
21389 blk_flush()
21390 if (user.page_format)
21391 blkdiv = 2 // start the next SVG in a new page
21392 return
21393 }
21394 break
21395 }
21396 self.set_format(cmd, param)
21397 }
21398
21399 // treat the %%beginxxx / %%endxxx sequences
21400 // (possible hook)
21401 Abc.prototype.do_begin_end = function(type,
21402 opt,
21403 text) {
21404 var i, j, action, s
21405
21406 if (curvoice && curvoice.clone)
21407 do_cloning()
21408 switch (type) {
21409 case "js":
21410 js_inject(text)
21411 break
21412 case "ml":
21413 if (cfmt.pageheight) {
21414 syntax(1, "Cannot have %%beginml with %%pageheight")
21415 break
21416 }
21417 if (parse.state >= 2) {
21418 s = new_block(type);
21419 s.text = text
21420 } else {
21421 blk_flush()
21422 if (user.img_out)
21423 user.img_out(text)
21424 }
21425 break
21426 case "svg":
21427 j = 0
21428 while (1) {
21429 i = text.indexOf('<style', j)
21430 if (i < 0)
21431 break
21432 i = text.indexOf('>', i)
21433 j = text.indexOf('</style>', i)
21434 if (j < 0) {
21435 syntax(1, "No </style> in %%beginsvg sequence")
21436 break
21437 }
21438 s = text.slice(i + 1, j).replace(/\s+$/gm, '')
21439 if (cfmt.fullsvg) {
21440 i = s.match(/@font-face[^}]*}/)
21441 if (i && i[0].indexOf("text") > 0) {
21442 ff.text = "\n"
21443 + i[0] // assume only one @font-face
21444 s = s.replace(i[0], '')
21445 }
21446 }
21447 if (s && s != "\n")
21448 style += s
21449 }
21450 j = 0
21451 while (1) {
21452 i = text.indexOf('<defs>\n', j)
21453 if (i < 0)
21454 break
21455 j = text.indexOf('</defs>', i)
21456 if (j < 0) {
21457 syntax(1, "No </defs> in %%beginsvg sequence")
21458 break
21459 }
21460 defs_add(text.slice(i + 6, j))
21461 }
21462 break
21463 case "text":
21464 action = get_textopt(opt);
21465 if (!action)
21466 action = cfmt.textoption
21467 set_font("text")
21468 if (text.indexOf('\\') >= 0)
21469 text = cnv_escape(text)
21470 if (parse.state > 1) {
21471 s = new_block(type);
21472 s.text = text
21473 s.opt = action
21474 s.font = cfmt.textfont
21475 break
21476 }
21477 write_text(text, action)
21478 break
21479 }
21480 }
21481
21482 /* -- generate a piece of tune -- */
21483 function generate() {
21484 var s, v, p_voice;
21485
21486 if (a_dcn.length) {
21487 syntax(1, "Decoration(s) without symbol: $1", a_dcn)
21488 a_dcn = []
21489 }
21490
21491 if (parse.tp) {
21492 syntax(1, "No end of tuplet")
21493 s = parse.tps
21494 if (s)
21495 delete s.tp
21496 delete parse.tp
21497 }
21498
21499 if (vover) {
21500 syntax(1, "No end of voice overlay");
21501 get_vover(vover.bar ? '|' : ')')
21502 }
21503
21504 self.voice_adj()
21505 sort_all() /* define the time / vertical sequences */
21506
21507 if (tsfirst) {
21508 for (v = 0; v < voice_tb.length; v++) {
21509 if (!voice_tb[v].key)
21510 voice_tb[v].key = parse.ckey // set the starting key
21511 }
21512 if (user.anno_start)
21513 anno_start = a_start
21514 if (user.anno_stop)
21515 anno_stop = a_stop
21516 self.set_bar_num()
21517
21518 if (info.P)
21519 tsfirst.parts = info.P // for play
21520
21521 // give the parser result to the application
21522 if (user.get_abcmodel)
21523 user.get_abcmodel(tsfirst, voice_tb, abc2svg.sym_name, info)
21524
21525 if (user.img_out) // if SVG generation
21526 self.output_music()
21527 } // (tsfirst)
21528
21529 // finish the generation
21530 set_page() // the page layout may have changed
21531 if (info.W)
21532 put_words(info.W)
21533 put_history()
21534 parse.state = 0 // file header
21535 blk_flush() // (force end of block)
21536
21537 if (tsfirst) { // if non void, keep tune data for upper layers
21538 tunes.push([tsfirst, voice_tb, info, cfmt])
21539 tsfirst = null
21540 }
21541 }
21542
21543 // transpose the current key of the voice (called on K: or V:)
21544 function key_trans() {
21545 var i, n, a_acc, b40, d,
21546 s = curvoice.ckey, // current key
21547 ti = s.time || 0
21548
21549 if (s.k_bagpipe || s.k_drum)
21550 return // no transposition
21551
21552 // set the score transposition
21553 n = (curvoice.score | 0) // new transposition
21554 + (curvoice.shift | 0)
21555 + (cfmt.transp | 0)
21556 if ((curvoice.tr_sco | 0) == n) { // if same transposition
21557 s.k_sf = curvoice.ckey.k_sf
21558 return
21559 }
21560
21561 // get the current key or create a new one
21562 if (is_voice_sig()) { // if no symbol yet
21563 curvoice.key = s // new root key of the voice
21564 } else if (curvoice.time != ti) { // if no K: at this time
21565 s = clone(s.orig || s) // new key
21566 if (!curvoice.new)
21567 s.k_old_sf = curvoice.ckey.k_sf
21568 sym_link(s)
21569 }
21570 curvoice.ckey = s // current key
21571
21572 if (cfmt.transp && curvoice.shift) // if %%transpose and shift=
21573 syntax(0, "Mix of old and new transposition syntaxes");
21574
21575 // define the new key
21576 curvoice.tr_sco = n // b40 interval
21577
21578 n = abc2svg.b40l5[(n + 202) % 40] // transpose in the line of fifth
21579 + s.orig.k_sf // + old = new sf
21580 if (n < -7) {
21581 n += 12
21582 curvoice.tr_sco -= 4
21583 } else if (n > 7) {
21584 n -= 12
21585 curvoice.tr_sco += 4
21586 }
21587 if (!s.k_none)
21588 s.k_sf = n
21589 for (b40 = 0; b40 < 40; b40++) {
21590 if (abc2svg.b40l5[b40] == n)
21591 break
21592 }
21593 s.k_b40 = b40
21594
21595 // transpose the accidental list
21596 if (!s.k_a_acc)
21597 return
21598 d = b40 - s.orig.k_b40
21599 a_acc = []
21600 for (i = 0; i < s.k_a_acc.length; i++) {
21601 b40 = abc2svg.pab40(s.k_a_acc[i].pit, s.k_a_acc[i].acc) + d
21602 a_acc[i] = {
21603 pit: abc2svg.b40p(b40),
21604 acc: abc2svg.b40a(b40) || 3
21605 }
21606 }
21607 s.k_a_acc = a_acc
21608 }
21609
21610 // fill a voice with a multi-rest and a bar
21611 function fill_mr_ba(p_v) {
21612 var v, p_v2,
21613 mxt = 0
21614
21615 for (v = 0; v < voice_tb.length; v++) {
21616 if (voice_tb[v].time > mxt) {
21617 p_v2 = voice_tb[v]
21618 mxt = p_v2.time
21619 }
21620 }
21621 if (p_v.time >= mxt)
21622 return
21623
21624 var p_v_sav = curvoice,
21625 dur = mxt - p_v.time,
21626 s = {
21627 type: C.MREST,
21628 stem: 0,
21629 multi: 0,
21630 nhd: 0,
21631 xmx: 0,
21632 frm: 1, //true // full measure rest
21633 dur: dur,
21634 dur_orig: dur,
21635 nmes: dur / p_v.wmeasure,
21636 notes: [{
21637 pit: 18,
21638 dur: dur
21639 }],
21640 tacet: p_v.tacet
21641 },
21642 s2 = {
21643 type: C.BAR,
21644 bar_type: '|',
21645 dur: 0,
21646 multi: 0
21647 }
21648
21649 if (p_v2.last_sym.bar_type)
21650 s2.bar_type = p_v2.last_sym.bar_type
21651 // s2.soln = p_v2.last_sym.soln
21652
21653 glovar.mrest_p = 1 //true
21654
21655 curvoice = p_v
21656 sym_link(s)
21657 sym_link(s2)
21658
21659 curvoice = p_v_sav
21660 } // fill_mr_ba()
21661
21662 /* -- get staves definition (%%staves / %%score) -- */
21663 function get_staves(cmd, parm) {
21664 var s, p_voice, p_voice2, i, flags, v, vid, a_vf, eoln,
21665 st, range,
21666 nv = voice_tb.length,
21667 maxtime = 0
21668
21669 // if sequence with many voices, load the other voices
21670 if (curvoice && curvoice.clone) {
21671 // i = parse.eol
21672 // parse.eol = parse.bol // remove the %%staves line
21673 do_cloning()
21674 // parse.eol = i
21675 }
21676
21677 if (parm) {
21678 a_vf = parse_staves(parm) // => array of [vid, flags]
21679 if (!a_vf)
21680 return
21681 } else if (staves_found < 0) {
21682 syntax(1, errs.bad_val, '%%' + cmd)
21683 return
21684 }
21685
21686 /* create a new staff system */
21687 for (v = 0; v < nv; v++) {
21688 p_voice = voice_tb[v]
21689 if (p_voice.eoln) {
21690 eoln = 1
21691 delete p_voice.eoln
21692 }
21693 if (p_voice.time > maxtime)
21694 maxtime = p_voice.time
21695 }
21696 if (!maxtime) { // if first %%staves
21697 par_sy.staves = []
21698 par_sy.voices = []
21699 } else {
21700 // if (nv) // if many voices
21701 self.voice_adj(1)
21702
21703 // synchronize the voices
21704 for (v = 0; v < nv; v++) {
21705 p_voice = voice_tb[v]
21706 //fixme: does not work if measure bar and %%staves delta time < measure duration
21707 if (maxtime - p_voice.time >= p_voice.meter.wmeasure)
21708 p_voice.acc = [] // no accidental anymore
21709 p_voice.time = maxtime
21710 p_voice.lyric_restart = p_voice.last_sym
21711 p_voice.sym_restart = p_voice.last_sym
21712 }
21713
21714 /*
21715 * create a new staff system and
21716 * link the 'staves' symbol in a voice which is seen from
21717 * the previous system - see sort_all
21718 */
21719 if (!par_sy.voices[curvoice.v])
21720 for (v = 0; v < par_sy.voices.length; v++) {
21721 if (par_sy.voices[v]) {
21722 curvoice = voice_tb[v]
21723 break
21724 }
21725 }
21726
21727 curvoice.eoln = eoln
21728 s = {
21729 type: C.STAVES,
21730 dur: 0
21731 }
21732
21733 sym_link(s); // link the staves in this voice
21734 par_sy.nstaff = nstaff;
21735
21736 // if no parameter, duplicate the current staff system
21737 if (!parm) {
21738 s.sy = clone(par_sy, 2) // clone the staves and voices
21739 par_sy.next = s.sy
21740 par_sy = s.sy
21741 staves_found = maxtime
21742 curvoice = voice_tb[par_sy.top_voice]
21743 return
21744 }
21745
21746 new_syst();
21747 s.sy = par_sy
21748 }
21749
21750 staves_found = maxtime
21751
21752 /* initialize the (old) voices */
21753 for (v = 0; v < nv; v++) {
21754 p_voice = voice_tb[v]
21755 delete p_voice.second
21756 delete p_voice.floating
21757 if (p_voice.ignore) {
21758 p_voice.ignore = 0 //false
21759 s = p_voice.sym
21760 if (s) {
21761 while (s.next)
21762 s = s.next
21763 }
21764 p_voice.last_sym = s // set back the last symbol
21765 }
21766 }
21767 range = 0
21768 for (i = 0; i < a_vf.length; i++) {
21769 vid = a_vf[i][0];
21770 p_voice = new_voice(vid);
21771 v = p_voice.v
21772
21773 a_vf[i][0] = p_voice;
21774
21775 // set the range and add the overlay voices
21776 while (1) {
21777 par_sy.voices[v] = {
21778 range: range++
21779 }
21780 p_voice = p_voice.voice_down
21781 if (!p_voice)
21782 break
21783 v = p_voice.v
21784 }
21785 }
21786 par_sy.top_voice = a_vf[0][0].v
21787 if (a_vf.length == 1)
21788 par_sy.one_v = 1 //true // one voice
21789
21790 /* change the behavior from %%staves to %%score */
21791 if (cmd[1] == 't') { /* if %%staves */
21792 for (i = 0; i < a_vf.length; i++) {
21793 flags = a_vf[i][1]
21794 if (!(flags & (OPEN_BRACE | OPEN_BRACE2)))
21795 continue
21796 if ((flags & (OPEN_BRACE | CLOSE_BRACE))
21797 == (OPEN_BRACE | CLOSE_BRACE)
21798 || (flags & (OPEN_BRACE2 | CLOSE_BRACE2))
21799 == (OPEN_BRACE2 | CLOSE_BRACE2))
21800 continue
21801 if (a_vf[i + 1][1] != 0)
21802 continue
21803 if ((flags & OPEN_PARENTH)
21804 || (a_vf[i + 2][1] & OPEN_PARENTH))
21805 continue
21806
21807 /* {a b c} -> {a *b c} */
21808 if (a_vf[i + 2][1] & (CLOSE_BRACE | CLOSE_BRACE2)) {
21809 a_vf[i + 1][1] |= FL_VOICE
21810
21811 /* {a b c d} -> {(a b) (c d)} */
21812 } else if (a_vf[i + 2][1] == 0
21813 && (a_vf[i + 3][1]
21814 & (CLOSE_BRACE | CLOSE_BRACE2))) {
21815 a_vf[i][1] |= OPEN_PARENTH;
21816 a_vf[i + 1][1] |= CLOSE_PARENTH;
21817 a_vf[i + 2][1] |= OPEN_PARENTH;
21818 a_vf[i + 3][1] |= CLOSE_PARENTH
21819 }
21820 }
21821 }
21822
21823 /* set the staff system */
21824 st = -1
21825 for (i = 0; i < a_vf.length; i++) {
21826 flags = a_vf[i][1]
21827 if ((flags & (OPEN_PARENTH | CLOSE_PARENTH))
21828 == (OPEN_PARENTH | CLOSE_PARENTH)) {
21829 flags &= ~(OPEN_PARENTH | CLOSE_PARENTH);
21830 a_vf[i][1] = flags
21831 }
21832 p_voice = a_vf[i][0]
21833 if (flags & FL_VOICE) {
21834 p_voice.floating = true;
21835 p_voice.second = true
21836 } else {
21837 st++;
21838 if (!par_sy.staves[st]) {
21839 par_sy.staves[st] = {
21840 staffscale: 1
21841 }
21842 }
21843 par_sy.staves[st].stafflines = p_voice.stafflines || "|||||",
21844 par_sy.staves[st].flags = 0
21845 }
21846 v = p_voice.v;
21847 p_voice.st = p_voice.cst =
21848 par_sy.voices[v].st = st;
21849 par_sy.staves[st].flags |= flags
21850 if (flags & OPEN_PARENTH) {
21851 p_voice2 = p_voice
21852 while (i < a_vf.length - 1) {
21853 p_voice = a_vf[++i][0];
21854 v = p_voice.v
21855 if (a_vf[i][1] & MASTER_VOICE) {
21856 p_voice2.second = true
21857 p_voice2 = p_voice
21858 } else {
21859 p_voice.second = true;
21860 }
21861 p_voice.st = p_voice.cst
21862 = par_sy.voices[v].st
21863 = st
21864 if (a_vf[i][1] & CLOSE_PARENTH)
21865 break
21866 }
21867 par_sy.staves[st].flags |= a_vf[i][1]
21868 }
21869 }
21870 if (st < 0)
21871 st = 0
21872 par_sy.nstaff = nstaff = st
21873
21874 /* change the behaviour of '|' in %%score */
21875 if (cmd[1] == 'c') { /* if %%score */
21876 for (st = 0; st < nstaff; st++)
21877 par_sy.staves[st].flags ^= STOP_BAR
21878 }
21879
21880 nv = voice_tb.length
21881 st = 0
21882 for (v = 0; v < nv; v++) {
21883 p_voice = voice_tb[v]
21884 if (par_sy.voices[v])
21885 st = p_voice.st
21886 else
21887 p_voice.st = st // (this avoids later crashes)
21888
21889 // if first %%staves
21890 // update the staff of the symbols with no time
21891 if (!maxtime) {
21892 for (s = p_voice.sym; s; s = s.next)
21893 s.st = st
21894 }
21895
21896 if (!par_sy.voices[v])
21897 continue
21898
21899 // set the staff of the overlay voices
21900 p_voice2 = p_voice.voice_down
21901 while (p_voice2) {
21902 p_voice2.second = 1 //true
21903 i = p_voice2.v
21904 p_voice2.st = p_voice2.cst =
21905 par_sy.voices[i].st = st
21906 p_voice2 = p_voice2.voice_down
21907 }
21908
21909 par_sy.voices[v].second = p_voice.second;
21910 st = p_voice.st
21911 if (st > 0 && p_voice.norepbra == undefined
21912 && !(par_sy.staves[st - 1].flags & STOP_BAR))
21913 p_voice.norepbra = true
21914 }
21915
21916 curvoice = parse.state >= 2 ? voice_tb[par_sy.top_voice] : null
21917 }
21918
21919 // get a voice or create a clone of the current voice
21920 function clone_voice(id) {
21921 var v, p_voice
21922
21923 for (v = 0; v < voice_tb.length; v++) {
21924 p_voice = voice_tb[v]
21925 if (p_voice.id == id)
21926 return p_voice // found
21927 }
21928 p_voice = clone(curvoice);
21929 p_voice.v = voice_tb.length;
21930 p_voice.id = id;
21931 p_voice.sym = p_voice.last_sym = null;
21932
21933 p_voice.key = clone(curvoice.key)
21934 p_voice.sls = []
21935
21936 delete p_voice.nm
21937 delete p_voice.snm
21938 delete p_voice.new_name
21939 delete p_voice.lyric_restart
21940 delete p_voice.lyric_cont
21941 delete p_voice.sym_restart
21942 delete p_voice.sym_cont
21943 delete p_voice.have_ly
21944 delete p_voice.tie_s
21945
21946 voice_tb.push(p_voice)
21947 return p_voice
21948 } // clone_voice()
21949
21950 /* -- get a voice overlay -- */
21951 function get_vover(type) {
21952 var p_voice2, p_voice3, range, s, time, v, v2, v3, s2
21953
21954 /* treat the end of overlay */
21955 if (type == '|'
21956 || type == ')') {
21957 if (!curvoice.last_note) {
21958 syntax(1, errs.nonote_vo)
21959 if (vover) {
21960 curvoice = vover.p_voice
21961 vover = null
21962 }
21963 return
21964 }
21965 curvoice.last_note.beam_end = true
21966 if (!vover) {
21967 syntax(1, "Erroneous end of voice overlay")
21968 return
21969 }
21970 if (curvoice.time != vover.p_voice.time) {
21971 if (!curvoice.ignore)
21972 syntax(1, "Wrong duration in voice overlay");
21973 if (curvoice.time > vover.p_voice.time)
21974 vover.p_voice.time = curvoice.time
21975 }
21976 curvoice.acc = [] // no accidental anymore
21977
21978 // if the last symbols are spaces, move them to the main voice
21979 p_voice2 = vover.p_voice // main voice
21980 s = curvoice.last_sym
21981 if (s.type == C.SPACE && p_voice2.last_sym.type != C.SPACE) {
21982 s.p_v = p_voice2
21983 s.v = s.p_v.v
21984 while (s.prev.type == C.SPACE) {
21985 s = s.prev
21986 s.p_v = p_voice2
21987 s.v = s.p_v.v
21988 }
21989 s2 = s.prev
21990 s2.next = null
21991 s.prev = p_voice2.last_sym
21992 s.prev.next = s
21993 p_voice2.last_sym = curvoice.last_sym
21994 curvoice.last_sym = s2
21995 }
21996
21997 curvoice = p_voice2
21998 vover = null
21999 return
22000 }
22001
22002 /* treat the full overlay start */
22003 if (type == '(') {
22004 if (vover) {
22005 syntax(1, "Voice overlay already started")
22006 return
22007 }
22008 vover = {
22009 p_voice: curvoice,
22010 time: curvoice.time
22011 }
22012 return
22013 }
22014
22015 /* (here is treated a new overlay - '&') */
22016 /* create the extra voice if not done yet */
22017 if (!curvoice.last_note) {
22018 syntax(1, errs.nonote_vo)
22019 return
22020 }
22021 curvoice.last_note.beam_end = true;
22022 p_voice2 = curvoice.voice_down
22023 if (!p_voice2) {
22024 p_voice2 = clone_voice(curvoice.id + 'o');
22025 curvoice.voice_down = p_voice2;
22026 p_voice2.time = 0;
22027 p_voice2.second = true;
22028 p_voice2.last_note = null
22029 v2 = p_voice2.v;
22030 if (par_sy.voices[curvoice.v]) { // if voice in the staff system
22031 par_sy.voices[v2] = {
22032 st: curvoice.st,
22033 second: true
22034 }
22035 range = par_sy.voices[curvoice.v].range
22036 for (v = 0; v < par_sy.voices.length; v++) {
22037 if (par_sy.voices[v]
22038 && par_sy.voices[v].range > range)
22039 par_sy.voices[v].range++
22040 }
22041 par_sy.voices[v2].range = range + 1
22042 }
22043 }
22044 p_voice2.ulen = curvoice.ulen
22045 p_voice2.dur_fact = curvoice.dur_fact
22046 p_voice2.acc = [] // no accidental
22047
22048 if (!vover) { /* first '&' in a measure */
22049 time = p_voice2.time
22050 if (curvoice.ignore)
22051 s = curvoice.last_bar
22052 else
22053 for (s = curvoice.last_sym; s; s = s.prev) {
22054 if (s.type == C.BAR
22055 || s.time <= time) /* (if start of tune) */
22056 break
22057 }
22058 vover = {
22059 bar: (s && s.bar_type) ? s.bar_type : '|',
22060 p_voice: curvoice,
22061 time: s ? s.time : curvoice.time
22062 }
22063 } else {
22064 if (curvoice != vover.p_voice
22065 && curvoice.time != vover.p_voice.time) {
22066 syntax(1, "Wrong duration in voice overlay")
22067 if (curvoice.time > vover.p_voice.time)
22068 vover.p_voice.time = curvoice.time
22069 }
22070 }
22071 p_voice2.time = vover.time;
22072 curvoice = p_voice2
22073 }
22074
22075 // check if a clef, key or time signature may go at start of the current voice
22076 function is_voice_sig() {
22077 var s
22078
22079 if (curvoice.time)
22080 return false
22081 if (!curvoice.last_sym)
22082 return true
22083 for (s = curvoice.last_sym; s; s = s.prev)
22084 if (w_tb[s.type])
22085 return false
22086 return true
22087 }
22088
22089 // treat a clef found in the tune body
22090 function get_clef(s) {
22091 var s2, s3
22092
22093 // special case for percussion
22094 if (s.clef_type == 'p') { // if percussion clef
22095 s2 = curvoice.ckey
22096 s2.k_drum = 1 //true
22097 s2.k_sf = 0
22098 s2.k_b40 = 2
22099 s2.k_map = abc2svg.keys[7]
22100 if (!curvoice.key)
22101 curvoice.key = s2 // new root key
22102 }
22103
22104 if (!curvoice.time // (force a clef when new voice)
22105 && is_voice_sig()) {
22106 curvoice.clef = s
22107 s.fmt = cfmt
22108 return
22109 }
22110
22111 // if not clef=none,
22112 // move the clef before a key and/or a (not right repeat) bar
22113 if (s.clef_none)
22114 s2 = null
22115 else
22116 for (s2 = curvoice.last_sym;
22117 s2 && s2.time == curvoice.time;
22118 s2 = s2.prev) {
22119 if (w_tb[s2.type])
22120 break
22121 }
22122 if (s2
22123 && s2.time == curvoice.time // if no time skip
22124 && s2.k_sf != undefined) {
22125 s3 = s2 // move before a key signature
22126 s2 = s2.prev
22127 }
22128 if (s2
22129 && s2.time == curvoice.time
22130 && s2.bar_type && s2.bar_type[0] != ':')
22131 s3 = s2 // move before a measure bar
22132 if (s3) {
22133 s2 = curvoice.last_sym
22134 curvoice.last_sym = s3.prev
22135 sym_link(s)
22136 s.next = s3
22137 s3.prev = s
22138 curvoice.last_sym = s2
22139 if (s.soln) {
22140 delete s.soln
22141 curvoice.eoln = true
22142 }
22143 } else {
22144 sym_link(s)
22145 }
22146 }
22147
22148 // treat K: (kp = key signature + parameters)
22149 function get_key(parm) {
22150 var v, p_voice,
22151 // [s_key, a] = new_key(parm) // KO with nodejs
22152 a = new_key(parm),
22153 s_key = a[0],
22154 s = s_key,
22155 empty = s.k_sf == undefined && !s.k_a_acc
22156
22157 a = a[1]
22158
22159 if (empty)
22160 s.invis = 1 //true // don't display empty K:
22161 else
22162 s.orig = s // new transposition base
22163
22164 if (parse.state == 1) { // in tune header (first K:)
22165 parse.ckey = s // root key
22166 if (empty) {
22167 s_key.k_sf = 0;
22168 s_key.k_none = true
22169 s_key.k_map = abc2svg.keys[7]
22170 }
22171 for (v = 0; v < voice_tb.length; v++) {
22172 p_voice = voice_tb[v];
22173 p_voice.ckey = clone(s_key)
22174 }
22175 if (a.length) {
22176 memo_kv_parm('*', a)
22177 a = []
22178 }
22179 if (!glovar.ulen)
22180 glovar.ulen = C.BLEN / 8;
22181 goto_tune()
22182 } else if (!empty) {
22183 if (curvoice.tr_sco)
22184 curvoice.tr_sco = undefined
22185 s.k_old_sf = curvoice.ckey.k_sf // memorize the previous key
22186 curvoice.ckey = s
22187 sym_link(s)
22188 }
22189
22190 // set the voice parameters
22191 if (!curvoice) { // if first K:
22192 if (!voice_tb.length) {
22193 curvoice = new_voice("1")
22194 var def = 1 // true
22195 } else {
22196 curvoice = voice_tb[staves_found < 0 ? 0 : par_sy.top_voice]
22197 }
22198 }
22199
22200 p_voice = curvoice.clone
22201 if (p_voice)
22202 curvoice.clone = null // don't stop the multi-voice sequence
22203 get_voice(curvoice.id + ' ' + a.join(' '))
22204 if (p_voice)
22205 curvoice.clone = p_voice
22206
22207 if (def)
22208 curvoice.default = 1 //true
22209 }
22210
22211 // get / create a new voice
22212 function new_voice(id) {
22213 var v, p_v_sav,
22214 p_voice = voice_tb[0],
22215 n = voice_tb.length
22216
22217 // if first explicit voice and no music, replace the default V:1
22218 if (n == 1
22219 && p_voice.default) {
22220 delete p_voice.default
22221 if (!p_voice.time) { // if no symbol yet
22222 p_voice.id = id
22223 p_voice.init = 0 // set back the global voice parameters
22224 return p_voice // default voice
22225 }
22226 }
22227 for (v = 0; v < n; v++) {
22228 p_voice = voice_tb[v]
22229 if (p_voice.id == id)
22230 return p_voice // old voice
22231 }
22232
22233 p_voice = {
22234 v: v,
22235 id: id,
22236 time: staves_found >= 0 ? staves_found : 0,
22237 new: true,
22238 pos: {
22239 // dyn: 0,
22240 // gch: 0,
22241 // gst: 0,
22242 // orn: 0,
22243 // stm: 0,
22244 // tup: 0,
22245 // voc: 0,
22246 // vol: 0
22247 },
22248 scale: 1,
22249 // st: 0,
22250 // cst: 0,
22251 ulen: glovar.ulen,
22252 dur_fact: 1,
22253 // key: clone(parse.ckey), // key at start of tune (parse / gene)
22254 // ckey: clone(parse.ckey), // current key (parse / gene)
22255 meter: clone(glovar.meter),
22256 wmeasure: glovar.meter.wmeasure,
22257 staffnonote: 1,
22258 clef: {
22259 type: C.CLEF,
22260 clef_auto: true,
22261 clef_type: "a", // auto
22262 time: 0
22263 },
22264 acc: [], // accidentals of the measure (parse)
22265 sls: [], // slurs - used in parsing and in generation
22266 hy_st: 0
22267 }
22268
22269 voice_tb.push(p_voice);
22270
22271 if (parse.state == 3) {
22272 // p_voice.key = parse.ckey // (done later in music.js)
22273 p_voice.ckey = clone(parse.ckey)
22274 if (p_voice.ckey.k_bagpipe
22275 && !p_voice.pos.stm) {
22276 p_voice.pos = clone(p_voice.pos)
22277 p_voice.pos.stm &= ~0x07
22278 p_voice.pos.stm |= C.SL_BELOW
22279 }
22280 }
22281
22282 // par_sy.voices[v] = {
22283 // range: -1
22284 // }
22285
22286 return p_voice
22287 }
22288
22289 // this function is called at program start and on end of tune
22290 function init_tune() {
22291 nstaff = -1;
22292 voice_tb = [];
22293 curvoice = null;
22294 new_syst(true);
22295 staves_found = -1;
22296 gene = {}
22297 a_de = [] // remove old decorations
22298 cross = {} // new cross voice decorations
22299 }
22300
22301 // treat V: with many voices
22302 function do_cloning() {
22303 var i,
22304 clone = curvoice.clone,
22305 vs = clone.vs,
22306 a = clone.a,
22307 bol = clone.bol,
22308 eol = parse.bol,
22309 parse_sav = parse,
22310 file = parse.file
22311
22312 delete curvoice.clone
22313
22314 if (file[eol - 1] == '[') // if stop on [V:xx]
22315 eol--
22316
22317 // insert the music sequence in each voice
22318 include++;
22319 for (i = 0; i < vs.length; i++) {
22320 parse = Object.create(parse_sav) // create a new parse context
22321 parse.line = Object.create(parse_sav.line)
22322 get_voice(vs[i] + ' ' + a.join(' '))
22323 tosvg(parse.fname, file, bol, eol)
22324 }
22325 include--
22326 parse = parse_sav // restore the parse context
22327 }
22328
22329 // treat a 'V:' info
22330 function get_voice(parm) {
22331 var v, vs,
22332 a = info_split(parm),
22333 vid = a.shift()
22334
22335 if (!vid)
22336 return // empty V:
22337
22338 // if end of sequence with many voices, load the other voices
22339 if (curvoice && curvoice.clone)
22340 do_cloning()
22341
22342 if (vid.indexOf(',') > 0) // if many voices
22343 vs = vid.split(',')
22344 else
22345 vs = [vid]
22346
22347 if (parse.state < 2) { // memorize the voice parameters
22348 while (1) {
22349 vid = vs.shift()
22350 if (!vid)
22351 break
22352 if (a.length)
22353 memo_kv_parm(vid, a)
22354 if (vid != '*' && parse.state == 1)
22355 curvoice = new_voice(vid)
22356 }
22357 return
22358 }
22359
22360 if (vid == '*') {
22361 syntax(1, "Cannot have V:* in tune body")
22362 return
22363 }
22364
22365 curvoice = new_voice(vs[0])
22366
22367 // if many voices, memorize the start of sequence
22368 if (vs.length > 1) {
22369 vs.shift()
22370 curvoice.clone = {
22371 vs: vs,
22372 a: a.slice(0), // copy the parameters
22373 bol: parse.iend
22374 }
22375 if (parse.file[curvoice.clone.bol - 1] != ']')
22376 curvoice.clone.bol++ // start of new line
22377 }
22378
22379 set_kv_parm(a)
22380
22381 key_trans()
22382
22383 v = curvoice.v
22384 if (curvoice.new) { // if new voice
22385 delete curvoice.new
22386 if (staves_found < 0) { // if no %%score/%%staves
22387 curvoice.st = curvoice.cst = ++nstaff;
22388 par_sy.nstaff = nstaff;
22389 par_sy.voices[v] = {
22390 st: nstaff,
22391 range: v
22392 }
22393 par_sy.staves[nstaff] = {
22394 stafflines: curvoice.stafflines || "|||||",
22395 staffscale: 1
22396 }
22397 } else if (!par_sy.voices[v]) {
22398 curvoice.ignore = 1 // voice not declared in %%staves
22399 return
22400 }
22401 }
22402
22403 if (!curvoice.filtered
22404 && par_sy.voices[v]
22405 && (parse.voice_opts
22406 || parse.tune_v_opts)) {
22407 curvoice.filtered = true;
22408 voice_filter()
22409 }
22410 }
22411
22412 // change state from 'tune header' to 'in tune body'
22413 // curvoice is defined when called from get_voice()
22414 function goto_tune() {
22415 var v, p_voice
22416
22417 set_page();
22418 write_heading();
22419 blk_flush() // tune heading in a specific SVG
22420
22421 if (glovar.new_nbar) {
22422 gene.nbar = glovar.new_nbar // measure numbering
22423 glovar.new_nbar = 0
22424 } else {
22425 gene.nbar = 1
22426 }
22427
22428 parse.state = 3 // in tune body
22429
22430 // update some voice parameters
22431 for (v = 0; v < voice_tb.length; v++) {
22432 p_voice = voice_tb[v];
22433 p_voice.ulen = glovar.ulen
22434 if (parse.ckey.k_bagpipe
22435 && !p_voice.pos.stm) {
22436 p_voice.pos = clone(p_voice.pos)
22437 p_voice.pos.stm &= ~0x07
22438 p_voice.pos.stm |= C.SL_BELOW
22439 }
22440 }
22441
22442 // initialize the voices when no %%staves/score
22443 if (staves_found < 0) {
22444 v = voice_tb.length
22445 par_sy.nstaff =
22446 nstaff = v - 1
22447 while (--v >= 0) {
22448 p_voice = voice_tb[v];
22449 delete p_voice.new; // old voice
22450 p_voice.st = p_voice.cst = v;
22451 par_sy.voices[v] = {
22452 st: v,
22453 range: v
22454 }
22455 par_sy.staves[v] = {
22456 stafflines: p_voice.stafflines || "|||||",
22457 staffscale: 1
22458 }
22459 }
22460 }
22461 }
22462 // abc2svg - lyrics.js - lyrics
22463 //
22464 // Copyright (C) 2014-2025 Jean-Francois Moine
22465 //
22466 // This file is part of abc2svg-core.
22467 //
22468 // abc2svg-core is free software: you can redistribute it and/or modify
22469 // it under the terms of the GNU Lesser General Public License as published by
22470 // the Free Software Foundation, either version 3 of the License, or
22471 // (at your option) any later version.
22472 //
22473 // abc2svg-core is distributed in the hope that it will be useful,
22474 // but WITHOUT ANY WARRANTY; without even the implied warranty of
22475 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22476 // GNU Lesser General Public License for more details.
22477 //
22478 // You should have received a copy of the GNU Lesser General Public License
22479 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
22480
22481 // parse a symbol line (s:)
22482 function get_sym(p, cont) {
22483 var s, c, i, j, d
22484
22485 if (curvoice.ignore)
22486 return
22487
22488 // get the starting symbol of the lyrics
22489 if (cont) { // +:
22490 s = curvoice.sym_cont
22491 if (!s) {
22492 syntax(1, "+: symbol line without music")
22493 return
22494 }
22495 } else {
22496 if (curvoice.sym_restart) { // new music
22497 curvoice.sym_start = curvoice.sym_restart;
22498 curvoice.sym_restart = null
22499 }
22500 s = curvoice.sym_start
22501 if (!s)
22502 s = curvoice.sym
22503 if (!s) {
22504 syntax(1, "s: without music")
22505 return
22506 }
22507 }
22508
22509 /* scan the symbol line */
22510 i = 0
22511 while (1) {
22512 while (p[i] == ' ' || p[i] == '\t')
22513 i++;
22514 c = p[i]
22515 if (!c)
22516 break
22517 switch (c) {
22518 case '|':
22519 while (s && s.type != C.BAR)
22520 s = s.next
22521 if (!s) {
22522 syntax(1, "Not enough measure bars for symbol line")
22523 return
22524 }
22525 s = s.next;
22526 i++
22527 continue
22528 case '!':
22529 case '"':
22530 j = ++i
22531 i = p.indexOf(c, j)
22532 if (i < 0) {
22533 syntax(1, c == '!' ?
22534 "No end of decoration" :
22535 "No end of chord symbol/annotation");
22536 i = p.length
22537 continue
22538 }
22539 d = p.slice(j - 1, i + 1)
22540 break
22541 case '*':
22542 break
22543 default:
22544 d = c.charCodeAt(0)
22545 if (d < 128) {
22546 d = char_tb[d]
22547 if (d.length > 1
22548 && (d[0] == '!' || d[0] == '"')) {
22549 c = d[0]
22550 break
22551 }
22552 }
22553 syntax(1, errs.bad_char, c)
22554 break
22555 }
22556
22557 /* store the element in the next note */
22558 while (s && s.type != C.NOTE)
22559 s = s.next
22560 if (!s) {
22561 syntax(1, "Too many elements in symbol line")
22562 return
22563 }
22564 switch (c) {
22565 default:
22566 // case '*':
22567 break
22568 case '!':
22569 a_dcn.push(d.slice(1, -1))
22570 deco_cnv(s, s.prev)
22571 break
22572 case '"':
22573 parse.line.index = j + 2 // (+ 's:')
22574 parse_gchord(d)
22575 if (a_gch) // if no error
22576 csan_add(s)
22577 break
22578 }
22579 s = s.next;
22580 i++
22581 }
22582 curvoice.sym_cont = s
22583 }
22584
22585 /* -- parse a lyric (vocal) line (w:) -- */
22586 function get_lyrics(p, cont) {
22587 var s, word, i, j, ly, dfnt, ln, c, cf
22588
22589 if (curvoice.ignore)
22590 return
22591 if ((curvoice.pos.voc & 0x07) != C.SL_HIDDEN)
22592 curvoice.have_ly = true
22593
22594 // get the starting symbol of the lyrics
22595 if (cont) { // +:
22596 s = curvoice.lyric_cont
22597 if (!s) {
22598 syntax(1, "+: lyric without music")
22599 return
22600 }
22601 if (p[0] == '~') { // +:~next~words
22602 while (!s.a_ly)
22603 s = s.prev
22604 ly = s.a_ly[curvoice.lyric_line]
22605 p = ly.t.replace(/ /g,'~') + p
22606 }
22607 dfnt = get_font("vocal")
22608 if (gene.deffont != dfnt) { // if vocalfont change
22609 if (gene.curfont == gene.deffont)
22610 gene.curfont = dfnt
22611 gene.deffont = dfnt
22612 }
22613 } else {
22614 set_font("vocal")
22615 if (curvoice.lyric_restart) { // new music
22616 curvoice.lyric_start = s = curvoice.lyric_restart;
22617 curvoice.lyric_restart = null;
22618 curvoice.lyric_line = 0
22619 } else {
22620 curvoice.lyric_line++;
22621 s = curvoice.lyric_start
22622 }
22623 if (!s)
22624 s = curvoice.sym
22625 if (!s) {
22626 syntax(1, "w: without music")
22627 return
22628 }
22629 }
22630
22631 /* scan the lyric line */
22632 i = 0
22633 cf = gene.curfont
22634 while (1) {
22635 while (p[i] == ' ' || p[i] == '\t')
22636 i++
22637 if (!p[i])
22638 break
22639 ln = 0
22640 j = parse.istart + i + 2 // start index
22641 switch (p[i]) {
22642 case '|':
22643 while (s && s.type != C.BAR)
22644 s = s.next
22645 if (!s) {
22646 syntax(1, "Not enough measure bars for lyric line")
22647 return
22648 }
22649 s = s.next;
22650 i++
22651 continue
22652 case '-':
22653 case '_':
22654 word = p[i]
22655 ln = p[i] == '-' ? 2 : 3 // line continuation
22656 break
22657 case '*':
22658 word = ""
22659 break
22660 default:
22661 word = "";
22662 while (1) {
22663 if (!p[i])
22664 break
22665 switch (p[i]) {
22666 case '_':
22667 case '*':
22668 case '|':
22669 i--
22670 case ' ':
22671 case '\t':
22672 break
22673 case '~':
22674 word += ' '
22675 i++
22676 continue
22677 case '-':
22678 ln = 1 // start of line
22679 break
22680 case '\\':
22681 if (!p[++i])
22682 continue
22683 word += p[i++]
22684 continue
22685 case '$':
22686 word += p[i++]
22687 c = p[i]
22688 if (c == '0')
22689 gene.curfont = gene.deffont
22690 else if (c >= '1' && c <= '9')
22691 gene.curfont = get_font("u" + c)
22692 // fall thru
22693 default:
22694 word += p[i++]
22695 continue
22696 }
22697 break
22698 }
22699 break
22700 }
22701
22702 /* store the word in the next note */
22703 while (s && s.type != C.NOTE)
22704 s = s.next
22705 if (!s) {
22706 syntax(1, "Too many words in lyric line")
22707 return
22708 }
22709 if (word
22710 && (s.pos.voc & 0x07) != C.SL_HIDDEN) {
22711 ly = {
22712 t: word,
22713 font: cf,
22714 istart: j,
22715 iend: j + word.length
22716 }
22717 if (ln)
22718 ly.ln = ln
22719 if (!s.a_ly)
22720 s.a_ly = []
22721 s.a_ly[curvoice.lyric_line] = ly
22722 cf = gene.curfont
22723 }
22724 s = s.next;
22725 i++
22726 }
22727 curvoice.lyric_cont = s
22728 }
22729
22730 // install the words under a note
22731 // (this function is called during the generation)
22732 function ly_set(s) {
22733 var i, j, ly, d, s1, s2, p, w, spw, xx, sz, shift, dw, r,
22734 s3 = s, // start of the current time sequence
22735 wx = 0,
22736 wl = 0,
22737 n = 0,
22738 dx = 0,
22739 a_ly = s.a_ly,
22740 align = 0
22741
22742 // get the available horizontal space before the next lyric words
22743 for (s2 = s.ts_next; s2; s2 = s2.ts_next) {
22744 if (s2.seqst) {
22745 dx += s2.shrink
22746 n++ // number of symbols without word
22747 }
22748 if (s2.bar_type) { // stop on a bar
22749 dx += 3 // and take some of its spacing
22750 break
22751 }
22752 if (!s2.a_ly)
22753 continue
22754 i = s2.a_ly.length
22755 while (--i >= 0) {
22756 ly = s2.a_ly[i]
22757 if (!ly)
22758 continue
22759 if (!ly.ln || ly.ln < 2)
22760 break
22761 }
22762 if (i >= 0)
22763 break
22764 }
22765
22766 // define the offset of the words
22767 for (i = 0; i < a_ly.length; i++) {
22768 ly = a_ly[i]
22769 if (!ly)
22770 continue
22771 gene.curfont = ly.font
22772 ly.t = str2svg(ly.t)
22773 p = ly.t.replace(/<[^>]*>/g, '') // remove the XML tags
22774 if (ly.ln >= 2) {
22775 ly.shift = 0
22776 continue
22777 }
22778 spw = cwid(' ') * ly.font.swfac
22779 w = ly.t.wh[0]
22780 r = abc2svg.lypre.exec(p)
22781 if (s.type == C.GRACE) { // %%graceword
22782 shift = s.wl
22783 } else if (r) {
22784 r = r[0]
22785 if (p[0] == '(') {
22786 sz = spw
22787 } else {
22788 set_font(ly.font)
22789 if (p[r.length] == ' '
22790 || r.slice(-1) == ':')
22791 sz = strwh(p.slice(0, r.length))[0]
22792 else
22793 sz = w * .2
22794 }
22795 shift = (w - sz) * .4
22796 if (shift > 14)
22797 shift = 14
22798 shift += sz
22799 if (p[0] >= '0' && p[0] <= '9') {
22800 if (shift > align)
22801 align = shift
22802 }
22803 } else {
22804 shift = w * .4
22805 if (shift > 14)
22806 shift = 14
22807 }
22808 ly.shift = shift
22809 if (shift > wl)
22810 wl = shift // max left space
22811 w += spw * 1.5 // space after the syllable
22812 w -= shift // right width
22813 if (w > wx)
22814 wx = w // max width
22815 }
22816
22817 // set the left space
22818 while (!s3.seqst)
22819 s3 = s3.ts_prev
22820 if (s3.ts_prev && s3.ts_prev.bar_type)
22821 wl -= 4 // don't move too much the measure bar
22822 if (s3.wl < wl) {
22823 s3.shrink += wl - s3.wl
22824 s3.wl = wl
22825 }
22826
22827 // if not room enough, shift the following notes to the right
22828 dx -= 6
22829 if (dx < wx && s2) {
22830 dx = (wx - dx) / n
22831 s1 = s.ts_next
22832 while (1) {
22833 if (s1.seqst) {
22834 s1.shrink += dx
22835 s3.wr += dx // (needed for end of line)
22836 s3 = s1
22837 }
22838 if (s1 == s2)
22839 break
22840 s1 = s1.ts_next
22841 }
22842 }
22843
22844 if (align > 0) {
22845 for (i = 0; i < a_ly.length; i++) {
22846 ly = a_ly[i]
22847 if (ly && ly.t[0] >= '0' && ly.t[0] <= '9')
22848 ly.shift = align
22849 }
22850 }
22851 } // ly_set()
22852
22853 /* -- draw the lyrics under (or above) notes -- */
22854 /* (the staves are not yet defined) */
22855 function draw_lyric_line(p_voice, j, y) {
22856 var p, lastx, w, s, s2, ly, lyl, ln,
22857 hyflag, lflag, x0, shift
22858
22859 if (p_voice.hy_st & (1 << j)) {
22860 hyflag = true;
22861 p_voice.hy_st &= ~(1 << j)
22862 }
22863 for (s = p_voice.sym; /*s*/; s = s.next)
22864 if (s.type != C.CLEF
22865 && s.type != C.KEY && s.type != C.METER)
22866 break
22867 lastx = s.prev ? s.prev.x : tsfirst.x;
22868 x0 = 0
22869 for ( ; s; s = s.next) {
22870 if (s.a_ly)
22871 ly = s.a_ly[j]
22872 else
22873 ly = null
22874 if (!ly) {
22875 switch (s.type) {
22876 case C.REST:
22877 case C.MREST:
22878 if (lflag) {
22879 out_wln(lastx + 3, y, x0 - lastx);
22880 lflag = false;
22881 lastx = s.x + s.wr
22882 }
22883 }
22884 continue
22885 }
22886 if (ly.font != gene.curfont) /* font change */
22887 gene.curfont = ly.font
22888 p = ly.t;
22889 ln = ly.ln || 0
22890 w = p.wh[0]
22891 shift = ly.shift
22892 if (hyflag) {
22893 if (ln == 3) { // '_'
22894 ln = 2
22895 } else if (ln < 2) { // not '-'
22896 out_hyph(lastx, y, s.x - shift - lastx);
22897 hyflag = false;
22898 lastx = s.x + s.wr
22899 }
22900 }
22901 if (lflag
22902 && ln != 3) { // not '_'
22903 out_wln(lastx + 3, y, x0 - lastx + 3);
22904 lflag = false;
22905 lastx = s.x + s.wr
22906 }
22907 if (ln >= 2) { // '-' or '_'
22908 if (x0 == 0 && lastx > s.x - 18)
22909 lastx = s.x - 18
22910 if (ln == 2) // '-'
22911 hyflag = true
22912 else
22913 lflag = true;
22914 x0 = s.x - shift
22915 continue
22916 }
22917 x0 = s.x - shift;
22918 if (ln) // '-' at end
22919 hyflag = true
22920 if (user.anno_start || user.anno_stop) {
22921 s2 = {
22922 p_v: s.p_v,
22923 st: s.st,
22924 istart: ly.istart,
22925 iend: ly.iend,
22926 ts_prev: s,
22927 ts_next: s.ts_next,
22928 x: x0,
22929 y: y,
22930 ymn: y,
22931 ymx: y + gene.curfont.size,
22932 wl: 0,
22933 wr: w
22934 }
22935 anno_start(s2, 'lyrics')
22936 }
22937 xy_str(x0, y, p)
22938 anno_stop(s2, 'lyrics')
22939 lastx = x0 + w
22940 }
22941 if (hyflag) {
22942 hyflag = false;
22943 x0 = realwidth - 10
22944 if (x0 < lastx + 10)
22945 x0 = lastx + 10;
22946 out_hyph(lastx, y, x0 - lastx)
22947 if (p_voice.s_next && p_voice.s_next.fmt.hyphencont)
22948 p_voice.hy_st |= (1 << j)
22949 }
22950
22951 /* see if any underscore in the next line */
22952 for (p_voice.s_next; s; s = s.next) {
22953 if (s.type == C.NOTE) {
22954 if (!s.a_ly)
22955 break
22956 ly = s.a_ly[j]
22957 if (ly && ly.ln == 3) { // '_'
22958 lflag = true;
22959 x0 = realwidth - 15
22960 if (x0 < lastx + 12)
22961 x0 = lastx + 12
22962 }
22963 break
22964 }
22965 }
22966 if (lflag) {
22967 out_wln(lastx + 3, y, x0 - lastx + 3);
22968 lflag = false
22969 }
22970 }
22971
22972 function draw_lyrics(p_voice, nly, a_h, y,
22973 incr) { /* 1: below, -1: above */
22974 var j, top,
22975 sc = staff_tb[p_voice.st].staffscale;
22976
22977 set_font("vocal")
22978 if (incr > 0) { /* under the staff */
22979 if (y > -tsfirst.fmt.vocalspace)
22980 y = -tsfirst.fmt.vocalspace;
22981 y *= sc
22982 for (j = 0; j < nly; j++) {
22983 y -= a_h[j] * 1.1;
22984 draw_lyric_line(p_voice, j,
22985 y + a_h[j] * .22) // (descent)
22986 }
22987 return y / sc
22988 }
22989
22990 /* above the staff */
22991 top = staff_tb[p_voice.st].topbar + tsfirst.fmt.vocalspace
22992 if (y < top)
22993 y = top;
22994 y *= sc
22995 for (j = nly; --j >= 0;) {
22996 draw_lyric_line(p_voice, j, y + a_h[j] * .22)
22997 y += a_h[j] * 1.1
22998 }
22999 return y / sc
23000 }
23001
23002 // -- draw all the lyrics --
23003 /* (the staves are not yet defined) */
23004 function draw_all_lyrics() {
23005 var p_voice, s, v, nly, i, x, y, w, a_ly, ly,
23006 lyst_tb = new Array(nstaff + 1),
23007 nv = voice_tb.length,
23008 h_tb = new Array(nv),
23009 nly_tb = new Array(nv),
23010 above_tb = new Array(nv),
23011 rv_tb = new Array(nv),
23012 top = 0,
23013 bot = 0,
23014 st = -1
23015
23016 /* compute the number of lyrics per voice - staff
23017 * and their y offset on the staff */
23018 for (v = 0; v < nv; v++) {
23019 p_voice = voice_tb[v]
23020 if (!p_voice.sym)
23021 continue
23022 if (p_voice.st != st) {
23023 top = 0;
23024 bot = 0;
23025 st = p_voice.st
23026 }
23027 nly = 0
23028 if (p_voice.have_ly) {
23029 if (!h_tb[v])
23030 h_tb[v] = []
23031 for (s = p_voice.sym; s; s = s.next) {
23032 a_ly = s.a_ly
23033 if (!a_ly)
23034 continue
23035 /*fixme:should get the real width*/
23036 x = s.x;
23037 w = 10
23038 for (i = 0; i < a_ly.length; i++) {
23039 ly = a_ly[i]
23040 if (ly) {
23041 x -= ly.shift;
23042 w = ly.t.wh[0]
23043 break
23044 }
23045 }
23046 y = y_get(p_voice.st, 1, x, w)
23047 if (top < y)
23048 top = y;
23049 y = y_get(p_voice.st, 0, x, w)
23050 if (bot > y)
23051 bot = y
23052 while (nly < a_ly.length)
23053 h_tb[v][nly++] = 0
23054 for (i = 0; i < a_ly.length; i++) {
23055 ly = a_ly[i]
23056 if (!ly)
23057 continue
23058 if (!h_tb[v][i]
23059 || ly.t.wh[1] > h_tb[v][i])
23060 h_tb[v][i] = ly.t.wh[1]
23061 }
23062 }
23063 } else {
23064 y = y_get(p_voice.st, 1, 0, realwidth)
23065 if (top < y)
23066 top = y;
23067 y = y_get(p_voice.st, 0, 0, realwidth)
23068 if (bot > y)
23069 bot = y
23070 }
23071 if (!lyst_tb[st])
23072 lyst_tb[st] = {}
23073 lyst_tb[st].top = top;
23074 lyst_tb[st].bot = bot;
23075 nly_tb[v] = nly
23076 if (nly == 0)
23077 continue
23078 if (p_voice.pos.voc)
23079 above_tb[v] = (p_voice.pos.voc & 0x07) == C.SL_ABOVE
23080 else if (voice_tb[v + 1]
23081 /*fixme:%%staves:KO - find an other way..*/
23082 && voice_tb[v + 1].st == st
23083 && voice_tb[v + 1].have_ly)
23084 above_tb[v] = true
23085 else
23086 above_tb[v] = false
23087 if (above_tb[v])
23088 lyst_tb[st].a = true
23089 else
23090 lyst_tb[st].b = true
23091 }
23092
23093 /* draw the lyrics under the staves */
23094 i = 0
23095 for (v = 0; v < nv; v++) {
23096 p_voice = voice_tb[v]
23097 if (!p_voice.sym)
23098 continue
23099 if (!p_voice.have_ly)
23100 continue
23101 if (above_tb[v]) {
23102 rv_tb[i++] = v
23103 continue
23104 }
23105 st = p_voice.st;
23106 // don't scale the lyrics
23107 set_dscale(st, true)
23108 if (nly_tb[v] > 0)
23109 lyst_tb[st].bot = draw_lyrics(p_voice, nly_tb[v],
23110 h_tb[v],
23111 lyst_tb[st].bot, 1)
23112 }
23113
23114 /* draw the lyrics above the staff */
23115 while (--i >= 0) {
23116 v = rv_tb[i];
23117 p_voice = voice_tb[v];
23118 st = p_voice.st;
23119 set_dscale(st, true);
23120 lyst_tb[st].top = draw_lyrics(p_voice, nly_tb[v],
23121 h_tb[v],
23122 lyst_tb[st].top, -1)
23123 }
23124
23125 /* set the max y offsets of all symbols */
23126 for (v = 0; v < nv; v++) {
23127 p_voice = voice_tb[v]
23128 if (!p_voice.sym)
23129 continue
23130 st = p_voice.st;
23131 if (lyst_tb[st].a) {
23132 top = lyst_tb[st].top + 2
23133 for (s = p_voice.sym; s; s = s.next) {
23134 /*fixme: may have lyrics crossing a next symbol*/
23135 if (s.a_ly) {
23136 /*fixme:should set the real width*/
23137 y_set(st, 1, s.x - 2, 10, top)
23138 }
23139 }
23140 }
23141 if (lyst_tb[st].b) {
23142 bot = lyst_tb[st].bot - 2
23143 if (nly_tb[p_voice.v] > 0) {
23144 for (s = p_voice.sym; s; s = s.next) {
23145 if (s.a_ly) {
23146 /*fixme:should set the real width*/
23147 y_set(st, 0, s.x - 2, 10, bot)
23148 }
23149 }
23150 } else {
23151 y_set(st, 0, 0, realwidth, bot)
23152 }
23153 }
23154 }
23155 }
23156 // abc2svg - gchord.js - chord symbols
23157 //
23158 // Copyright (C) 2014-2025 Jean-Francois Moine
23159 //
23160 // This file is part of abc2svg-core.
23161 //
23162 // abc2svg-core is free software: you can redistribute it and/or modify
23163 // it under the terms of the GNU Lesser General Public License as published by
23164 // the Free Software Foundation, either version 3 of the License, or
23165 // (at your option) any later version.
23166 //
23167 // abc2svg-core is distributed in the hope that it will be useful,
23168 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23169 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23170 // GNU Lesser General Public License for more details.
23171 //
23172 // You should have received a copy of the GNU Lesser General Public License
23173 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
23174
23175 // -- parse a chord symbol / annotation --
23176 // the result is added in the global variable a_gch
23177 // 'type' may be a single '"' or a string '"xxx"' created by U:
23178 function parse_gchord(type) {
23179 var c, text, gch, x_abs, y_abs,
23180 i, j, istart, iend,
23181 ann_font = get_font("annotation"),
23182 h_ann = ann_font.size,
23183 line = parse.line
23184
23185 function get_float() {
23186 var txt = ''
23187
23188 while (1) {
23189 c = text[i++]
23190 if ("1234567890.-".indexOf(c) < 0)
23191 return parseFloat(txt)
23192 txt += c
23193 }
23194 } // get_float()
23195
23196 istart = parse.bol + line.index
23197 if (type.length > 1) { // U:
23198 text = type.slice(1, -1);
23199 iend = istart + 1
23200 } else {
23201 i = ++line.index // search the ending double quote
23202 while (1) {
23203 j = line.buffer.indexOf('"', i)
23204 if (j < 0) {
23205 syntax(1, "No end of chord symbol/annotation")
23206 return
23207 }
23208 if (line.buffer[j - 1] != '\\'
23209 || line.buffer[j - 2] == '\\') // (string ending with \\")
23210 break
23211 i = j + 1
23212 }
23213 text = cnv_escape(line.buffer.slice(line.index, j))
23214 line.index = j
23215 iend = parse.bol + line.index + 1
23216 }
23217
23218 if (ann_font.pad)
23219 h_ann += ann_font.pad
23220 i = 0;
23221 type = 'g'
23222 while (1) {
23223 c = text[i]
23224 if (!c)
23225 break
23226 gch = {
23227 text: "",
23228 istart: istart,
23229 iend: iend,
23230 font: ann_font
23231 }
23232 switch (c) {
23233 case '@':
23234 type = c;
23235 i++;
23236 x_abs = get_float()
23237 if (c != ',') {
23238 syntax(1, "',' lacking in annotation '@x,y'");
23239 y_abs = 0
23240 } else {
23241 y_abs = get_float()
23242 if (c != ' ')
23243 i--
23244 }
23245 gch.x = x_abs;
23246 gch.y = y_abs
23247 break
23248 case '^':
23249 gch.pos = C.SL_ABOVE
23250 // fall thru
23251 case '_':
23252 if (c == '_')
23253 gch.pos = C.SL_BELOW
23254 // fall thru
23255 case '<':
23256 case '>':
23257 i++;
23258 type = c
23259 break
23260 default:
23261 switch (type) {
23262 case 'g':
23263 gch.font = get_font("gchord")
23264 gch.pos = curvoice.pos.gch || C.SL_ABOVE
23265 break
23266 case '^':
23267 gch.pos = C.SL_ABOVE
23268 break
23269 case '_':
23270 gch.pos = C.SL_BELOW
23271 break
23272 case '@':
23273 gch.x = x_abs;
23274 y_abs -= h_ann;
23275 gch.y = y_abs
23276 break
23277 }
23278 break
23279 }
23280 gch.type = type
23281 while (1) {
23282 c = text[i]
23283 if (!c)
23284 break
23285 switch (c) {
23286 default:
23287 gch.text += c;
23288 i++
23289 continue
23290 case '&': /* skip "&xxx;" */
23291 while (1) {
23292 gch.text += c;
23293 c = text[++i]
23294 switch (c) {
23295 default:
23296 continue
23297 case ';':
23298 case undefined:
23299 case '\\':
23300 break
23301 }
23302 break
23303 }
23304 if (c == ';') {
23305 i++;
23306 gch.text += c
23307 continue
23308 }
23309 break
23310 case '\n': // abcm2ps compatibility
23311 case ';':
23312 break
23313 }
23314 i++
23315 break
23316 }
23317 gch.otext = gch.text // save for play accompaniment
23318 if (!a_gch)
23319 a_gch = []
23320 a_gch.push(gch)
23321 }
23322 }
23323
23324 // transpose a chord symbol
23325 function gch_tr1(p, tr) {
23326 var i, o, n, ip,
23327 csa = p.split('/')
23328
23329 tr = abc2svg.b40l5[(tr + 202) % 40] // transpose in the line of fifth
23330 for (i = 0; i < csa.length; i++) { // main and optional bass
23331 p = csa[i];
23332 o = p.search(/[A-G]/)
23333 if (o < 0)
23334 continue // strange chord symbol!
23335 ip = o + 1
23336
23337 // bbb fb cb gb db ab eb bb f c g d a e b f# c# g# d# a# e# b# f##
23338 // -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13
23339 n = "FCGDAEB".indexOf(p[o]) - 1
23340 if (p[ip] == '#' || p[ip] == '\u266f') {
23341 n += 7
23342 ip++
23343 } else if (p[ip] == 'b' || p[ip] == '\u266d') {
23344 n -= 7
23345 ip++
23346 }
23347 n += tr // transpose
23348
23349 // // remove chords with double sharps/flats
23350 // if ((!i && n > 7) // main chord
23351 // || (i && n > 12)) // bass
23352 // n -= 12
23353 // else if (i < -7)
23354 // n += 12
23355
23356 csa[i] = p.slice(0, o)
23357 + "FCGDAEB"[(n + 22) % 7]
23358 + (n >= 13 ? '##'
23359 : n >= 6 ? '#'
23360 : n <= -9 ? 'bb'
23361 : n <= -2 ? 'b'
23362 : '')
23363 + p.slice(ip)
23364 }
23365 return csa.join('/')
23366 } // gch_tr1
23367
23368 // parser: add the parsed list of chord symbols and annotations
23369 // to the symbol (note, rest or bar)
23370 // and transpose the chord symbols
23371 function csan_add(s) {
23372 var i, gch
23373
23374 // there cannot be chord symbols on measure bars
23375 if (s.type == C.BAR) {
23376 for (i = 0; i < a_gch.length; i++) {
23377 if (a_gch[i].type == 'g') {
23378 error(1, s,
23379 "There cannot be chord symbols on measure bars")
23380 a_gch.splice(i)
23381 }
23382 }
23383 }
23384
23385 if (curvoice.tr_sco
23386 || curvoice.tr_snd) {
23387 for (i = 0; i < a_gch.length; i++) {
23388 gch = a_gch[i]
23389 if (gch.type == 'g') {
23390 if (curvoice.tr_snd40)
23391 gch.otext = gch_tr1(gch.text, curvoice.tr_snd40)
23392 if (curvoice.tr_sco)
23393 gch.text = gch_tr1(gch.text, curvoice.tr_sco)
23394 }
23395 }
23396 }
23397
23398 if (s.a_gch)
23399 s.a_gch = s.a_gch.concat(a_gch)
23400 else
23401 s.a_gch = a_gch
23402 a_gch = null
23403 } // csan_add
23404
23405 // generator: build the chord symbols / annotations
23406 // (possible hook)
23407 Abc.prototype.gch_build = function(s) {
23408
23409 /* split the chord symbols / annotations
23410 * and initialize their vertical offsets */
23411 var gch, wh, xspc, ix,
23412 y_left = 0,
23413 y_right = 0,
23414 GCHPRE = .4; // portion of chord before note
23415
23416 // change the accidentals in the chord symbols,
23417 // convert the escape sequences in annotations, and
23418 // set the offsets
23419 for (ix = 0; ix < s.a_gch.length; ix++) {
23420 gch = s.a_gch[ix]
23421 if (gch.type == 'g') {
23422 gch.text = gch.text.replace(/##|#|=|bb|b/g,
23423 function(x) {
23424 switch (x) {
23425 case '##': return "&#x1d12a;"
23426 case '#': return "\u266f"
23427 case '=': return "\u266e"
23428 case 'b': return "\u266d"
23429 }
23430 return "&#x1d12b;"
23431 });
23432 } else {
23433 if (gch.type == '@'
23434 && !user.anno_start && !user.anno_stop) {
23435 set_font(gch.font)
23436 gch.text = str2svg(gch.text)
23437 continue /* no width */
23438 }
23439 }
23440
23441 /* set the offsets and widths */
23442 set_font(gch.font);
23443 gch.text = str2svg(gch.text)
23444 wh = gch.text.wh
23445 switch (gch.type) {
23446 case '@':
23447 break
23448 default:
23449 // case 'g': // chord symbol
23450 // case '^': /* above */
23451 // case '_': /* below */
23452 xspc = wh[0] * GCHPRE
23453 if (xspc > 8)
23454 xspc = 8;
23455 gch.x = -xspc;
23456 break
23457 case '<': /* left */
23458 gch.x = -(wh[0] + 6);
23459 y_left -= wh[1];
23460 gch.y = y_left + wh[1] / 2
23461 break
23462 case '>': /* right */
23463 gch.x = 6;
23464 y_right -= wh[1];
23465 gch.y = y_right + wh[1] / 2
23466 break
23467 }
23468 }
23469
23470 /* move upwards the top and middle texts */
23471 y_left /= 2;
23472 y_right /= 2
23473 for (ix = 0; ix < s.a_gch.length; ix++) {
23474 gch = s.a_gch[ix]
23475 switch (gch.type) {
23476 case '<': /* left */
23477 gch.y -= y_left
23478 break
23479 case '>': /* right */
23480 gch.y -= y_right
23481 break
23482 }
23483 }
23484 }
23485
23486 // -- draw the chord symbols and annotations
23487 // (the staves are not yet defined)
23488 // (unscaled delayed output)
23489 // (possible hook)
23490 Abc.prototype.draw_gchord = function(i, s, x, y) {
23491 if (s.invis && s.play) // play sequence: no chord nor annotation
23492 return
23493 var y2,
23494 an = s.a_gch[i],
23495 h = an.text.wh[1],
23496 pad = an.font.pad,
23497 w = an.text.wh[0] + pad * 2,
23498 dy = h * .22 // descent
23499
23500 if (an.font.figb) {
23501 h *= 2.4
23502 dy += an.font.size * 1.3
23503 }
23504
23505 switch (an.type) {
23506 case '_': // below
23507 y -= h + pad
23508 break
23509 case '^': // above
23510 y += pad
23511 break
23512 case '<': // left
23513 case '>': // right
23514 if (an.type == '<') {
23515 /*fixme: what symbol space?*/
23516 if (s.notes[0].acc)
23517 x -= s.notes[0].shac
23518 x -= pad
23519 } else {
23520 if (s.xmx)
23521 x += s.xmx
23522 if (s.dots)
23523 x += 1.5 + 3.5 * s.dots
23524 x += pad
23525 }
23526 y += (s.type == C.NOTE ?
23527 (((s.notes[s.nhd].pit + s.notes[0].pit) >> 1) -
23528 18) * 3 :
23529 12) // fixed offset on rests and bars
23530 - h / 2
23531 break
23532 default: // chord symbol
23533 if (y >= 0)
23534 y += pad
23535 else
23536 y -= h + pad
23537 break
23538 case '@': // absolute
23539 y += (s.type == C.NOTE ?
23540 (((s.notes[s.nhd].pit + s.notes[0].pit) >> 1) -
23541 18) * 3 :
23542 12) // fixed offset on rests and bars
23543 - h / 2
23544 if (y > 0) {
23545 y2 = y + h + pad + 2
23546 if (y2 > staff_tb[s.st].ann_top)
23547 staff_tb[s.st].ann_top = y2
23548 } else {
23549 y2 = y - 2
23550 if (y2 < staff_tb[s.st].ann_bot)
23551 staff_tb[s.st].ann_bot = y2
23552 }
23553 break
23554 }
23555
23556 if (an.type != '@') {
23557 if (y >= 0)
23558 y_set(s.st, 1, x, w, y + h + pad + 2)
23559 else
23560 y_set(s.st, 0, x, w, y - pad)
23561 }
23562
23563 use_font(an.font)
23564 set_font(an.font)
23565 set_dscale(s.st)
23566 if (user.anno_start)
23567 user.anno_start("annot", an.istart, an.iend,
23568 x - 2, y + h + 2, w + 4, h + 4, s)
23569 xy_str(x, y + dy, an.text)
23570 if (user.anno_stop)
23571 user.anno_stop("annot", an.istart, an.iend,
23572 x - 2, y + h + 2, w + 4, h + 4, s)
23573 } // draw_gchord()
23574
23575 // draw all chord symbols
23576 function draw_all_chsy() {
23577 var s, san1, an, i, x, y, w,
23578 n_an = 0, // max number of annotations
23579 minmax = new Array(nstaff + 1)
23580
23581 // set a vertical offset to all the chord symbols/annotations
23582 function set_an_yu(j) {
23583 var an, i, s, x, y, w
23584
23585 for (s = san1 ; s; s = s.ts_next) {
23586 an = s.a_gch
23587 if (!an)
23588 continue
23589 i = an.length - j - 1
23590 an = an[i]
23591 if (!an)
23592 continue
23593 if (an.pos == C.SL_ABOVE) {
23594 x = s.x + an.x
23595 w = an.text.wh[0]
23596 if (w && x + w > realwidth)
23597 x = realwidth - w // let the text in the page
23598 y = y_get(s.st, 1, x, w) // y / staff
23599 if (an.type == 'g' && y < minmax[s.st].yup)
23600 y = minmax[s.st].yup
23601 } else if (an.pos == C.SL_BELOW
23602 || an.pos == C.SL_HIDDEN) {
23603 continue
23604 } else {
23605 x = s.x + an.x
23606 y = an.y
23607 }
23608 self.draw_gchord(i, s, x, y)
23609 }
23610 } // set_an_yu()
23611
23612 function set_an_yl(i) {
23613 var an, x, y, w
23614
23615 for (var s = san1 ; s; s = s.ts_next) {
23616 an = s.a_gch
23617 if (!an)
23618 continue
23619 an = an[i]
23620 if (!an
23621 || an.pos != C.SL_BELOW)
23622 continue
23623 x = s.x + an.x
23624 w = an.text.wh[0]
23625 if (w && x + w > realwidth) // let the text inside the page
23626 x = realwidth - w
23627 y = y_get(s.st, 0, x, w) - 2 // y / staff
23628 if (an.type == 'g' && y > minmax[s.st].ydn)
23629 y = minmax[s.st].ydn
23630 self.draw_gchord(i, s, x, y)
23631 }
23632 } // set_an_yl()
23633
23634 // get the number of chord symbols / annotations
23635 // and the vertical offset for the chord symbols
23636 for (i = 0; i <= nstaff; i++)
23637 minmax[i] = {
23638 ydn: staff_tb[i].botbar - 3,
23639 yup: staff_tb[i].topbar + 4
23640 }
23641 for (s = tsfirst; s; s = s.ts_next) {
23642 an = s.a_gch
23643 if (!an)
23644 continue
23645 if (!san1)
23646 san1 = s // first chord symbol / annotation
23647 i = an.length
23648 if (i > n_an)
23649 n_an = i
23650 while (--i >= 0) {
23651 if (an[i].type == 'g') {
23652 an = an[i]
23653 x = s.x + an.x
23654 w = an.text.wh[0]
23655 if (w && x + w > realwidth)
23656 x = realwidth - w
23657 if (an.pos == C.SL_ABOVE) {
23658 y = y_get(s.st, true, x, w)
23659 if (y > minmax[s.st].yup)
23660 minmax[s.st].yup = y
23661 } else if (an.pos == C.SL_BELOW) {
23662 y = y_get(s.st, false, x, w) - 2
23663 if (y < minmax[s.st].ydn)
23664 minmax[s.st].ydn = y
23665 }
23666 break
23667 }
23668 }
23669 }
23670 if (!san1)
23671 return // no chord symbol nor annotation
23672
23673 // draw the elements
23674 set_dscale(-1) // restore the scale parameters
23675 for (i = 0; i < n_an; i++) {
23676 set_an_yu(i) // upper offsets
23677 set_an_yl(i) // lower offsets
23678 }
23679 } // draw_all_chsy()
23680 // abc2svg - tail.js
23681 //
23682 // Copyright (C) 2014-2025 Jean-Francois Moine
23683 //
23684 // This file is part of abc2svg-core.
23685 //
23686 // abc2svg-core is free software: you can redistribute it and/or modify
23687 // it under the terms of the GNU Lesser General Public License as published by
23688 // the Free Software Foundation, either version 3 of the License, or
23689 // (at your option) any later version.
23690 //
23691 // abc2svg-core is distributed in the hope that it will be useful,
23692 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23693 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23694 // GNU Lesser General Public License for more details.
23695 //
23696 // You should have received a copy of the GNU Lesser General Public License
23697 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
23698
23699 // initialize
23700 init_tune()
23701
23702 // Abc functions used by the modules
23703 Abc.prototype.a_de = function() { return a_de }
23704 Abc.prototype.add_style = function(s) { style += s };
23705 Abc.prototype.anno_a = anno_a
23706 Abc.prototype.cfmt = function() { return cfmt };
23707 Abc.prototype.clone = clone;
23708 Abc.prototype.clr_sty = clr_sty
23709 Abc.prototype.deco_put = function(nm, s) {
23710 a_dcn.push(nm)
23711 deco_cnv(s)
23712 }
23713 Abc.prototype.defs_add = defs_add
23714 Abc.prototype.dh_put = function(nm, s, nt) {
23715 a_dcn.push(nm)
23716 dh_cnv(s, nt)
23717 }
23718 Abc.prototype.draw_meter = draw_meter
23719 Abc.prototype.draw_note = draw_note;
23720 Abc.prototype.errs = errs;
23721 Abc.prototype.font_class = font_class;
23722 Abc.prototype.gch_tr1 = gch_tr1;
23723 Abc.prototype.get_bool = get_bool;
23724 Abc.prototype.get_cur_sy = function() { return cur_sy };
23725 Abc.prototype.get_curvoice = function() { return curvoice };
23726 Abc.prototype.get_delta_tb = function() { return delta_tb };
23727 Abc.prototype.get_decos = function() { return decos };
23728 Abc.prototype.get_font = get_font;
23729 Abc.prototype.get_font_style = function() { return font_style };
23730 Abc.prototype.get_glyphs = function() { return glyphs };
23731 Abc.prototype.get_img = function() { return img };
23732 Abc.prototype.get_lwidth = get_lwidth
23733 Abc.prototype.get_maps = function() { return maps };
23734 Abc.prototype.get_multi = function() { return multicol };
23735 Abc.prototype.get_newpage = function() {
23736 if (block.newpage) {
23737 block.newpage = false;
23738 return true
23739 }
23740 };
23741 Abc.prototype.get_parse = function() { return parse }
23742 Abc.prototype.get_posy = function() { return posy }
23743 Abc.prototype.get_staff_tb = function() { return staff_tb };
23744 Abc.prototype.get_top_v = function() { return par_sy.top_voice };
23745 Abc.prototype.get_tsfirst = function() { return tsfirst };
23746 Abc.prototype.get_unit = get_unit;
23747 Abc.prototype.get_user = function() { return user }
23748 Abc.prototype.get_voice_tb = function() { return voice_tb };
23749 Abc.prototype.glout = glout
23750 Abc.prototype.glovar = function() { return glovar }
23751 Abc.prototype.info = function() { return info };
23752 Abc.prototype.new_block = new_block;
23753 Abc.prototype.out_arp = out_arp;
23754 Abc.prototype.out_deco_str = out_deco_str;
23755 Abc.prototype.out_deco_val = out_deco_val;
23756 Abc.prototype.out_ltr = out_ltr;
23757 Abc.prototype.param_set_font = param_set_font;
23758 Abc.prototype.part_seq = part_seq
23759 Abc.prototype.psdeco = empty_function;
23760 Abc.prototype.psxygl = empty_function;
23761 Abc.prototype.set_cur_sy = function(sy) { cur_sy = sy };
23762 Abc.prototype.set_curvoice = function(p_v) { curvoice = p_v }
23763 Abc.prototype.set_dscale = set_dscale;
23764 Abc.prototype.set_font = set_font;
23765 Abc.prototype.set_a_gch = function(s, a) { a_gch = a; csan_add(s) }
23766 Abc.prototype.set_hl = set_hl
23767 Abc.prototype.set_map = set_map
23768 Abc.prototype.set_page = set_page
23769 Abc.prototype.set_pagef = function() { blkdiv = 1 }
23770 Abc.prototype.set_realwidth = function(v) { realwidth = v }
23771 Abc.prototype.set_scale = set_scale;
23772 Abc.prototype.set_sscale = set_sscale
23773 Abc.prototype.set_tsfirst = function(s) { tsfirst = s };
23774 Abc.prototype.set_v_param = set_v_param;
23775 Abc.prototype.str2svg = str2svg
23776 Abc.prototype.strwh = strwh;
23777 Abc.prototype.stv_g = function() { return stv_g };
23778 Abc.prototype.svg_flush = svg_flush;
23779 Abc.prototype.syntax = syntax;
23780 Abc.prototype.tunes = tunes
23781 Abc.prototype.unlksym = unlksym;
23782 Abc.prototype.use_font = use_font;
23783 Abc.prototype.vskip = vskip
23784 Abc.prototype.xy_str = xy_str;
23785 Abc.prototype.xygl = xygl;
23786 Abc.prototype.y_get = y_get
23787 Abc.prototype.y_set = y_set
23788 } // end of Abc()
23789
23790 // compatibility
23791 var Abc = abc2svg.Abc
23792
23793 // nodejs
23794 if (typeof module == 'object' && typeof exports == 'object') {
23795 exports.abc2svg = abc2svg;
23796 exports.Abc = Abc
23797 }
23798 // abc2svg - modules.js - module handling
23799 //
23800 // Copyright (C) 2018-2025 Jean-Francois Moine
23801 //
23802 // This file is part of abc2svg-core.
23803 //
23804 // abc2svg-core is free software: you can redistribute it and/or modify
23805 // it under the terms of the GNU Lesser General Public License as published by
23806 // the Free Software Foundation, either version 3 of the License, or
23807 // (at your option) any later version.
23808 //
23809 // abc2svg-core is distributed in the hope that it will be useful,
23810 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23811 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23812 // GNU Lesser General Public License for more details.
23813 //
23814 // You should have received a copy of the GNU Lesser General Public License
23815 // along with abc2svg-core. If not, see <http://www.gnu.org/licenses/>.
23816
23817 // empty function
23818 if (!abc2svg.loadjs) {
23819 abc2svg.loadjs = function(fn, onsuccess, onerror) {
23820 if (onerror)
23821 onerror(fn)
23822 }
23823 }
23824
23825 abc2svg.modules = {
23826 ambitus: {},
23827 begingrid: { fn: 'grid3' },
23828 beginps: { fn: 'psvg' },
23829 break: {},
23830 capo: {},
23831 chordnames: {},
23832 clip: {},
23833 clairnote: { fn: 'clair' },
23834 voicecombine: { fn: 'combine' },
23835 diagram: { fn: 'diag' },
23836 equalbars: {},
23837 fit2box: {},
23838 gamelan: {},
23839 grid: {},
23840 grid2: {},
23841 jazzchord: {},
23842 jianpu: {},
23843 mdnn: {},
23844 MIDI: {},
23845 nns: {},
23846 pageheight: { fn: 'page' },
23847 pedline: {},
23848 percmap: { fn: 'perc' },
23849 playswing: { fn: 'swing' },
23850 roman: {},
23851 soloffs: {},
23852 sth: {},
23853 strtab: {},
23854 temperament: { fn: 'temper' },
23855 temponame: { fn: 'tempo' },
23856 tropt: {},
23857 titleformat: { fn: 'tunhd' },
23858
23859 nreq: 0,
23860
23861 // scan the file and find the required modules
23862 // @file: ABC file
23863 // @relay: (optional) callback function for continuing the treatment
23864 // @errmsg: (optional) function to display an error message if any
23865 // This function gets one argument: the message
23866 // return true when all modules are loaded
23867 load: function(file, relay, errmsg) {
23868
23869 function get_errmsg() {
23870 if (typeof user == 'object' && user.errmsg)
23871 return user.errmsg
23872 if (typeof abc2svg.printErr == 'function')
23873 return abc2svg.printErr
23874 if (typeof alert == 'function')
23875 return function(m) { alert(m) }
23876 if (typeof console == 'object')
23877 return console.log
23878 return function(){}
23879 } // get_errmsg()
23880
23881 // call back functions for loadjs()
23882 function load_end() {
23883 if (--abc2svg.modules.nreq == 0)
23884 abc2svg.modules.cbf()
23885 }
23886
23887 // test if some keyword in the file
23888 var m, i, fn,
23889 nreq_i = this.nreq,
23890 ls = file.match(/(%%|I:).+?\b/g)
23891
23892 if (!ls)
23893 return true
23894 this.cbf = relay || // (only one callback function)
23895 function(){}
23896 this.errmsg = errmsg || get_errmsg()
23897
23898 for (i = 0; i < ls.length; i++) {
23899 fn = ls[i].replace(/\n?(%%|I:)/, '')
23900 m = abc2svg.modules[fn]
23901 if (!m || m.loaded)
23902 continue
23903
23904 m.loaded = true
23905
23906 // load the module
23907 if (m.fn)
23908 fn = m.fn
23909 this.nreq++
23910 abc2svg.loadjs(fn + "-1.js",
23911 load_end,
23912 function () {
23913 abc2svg.modules.errmsg(
23914 'Error loading the module ' + fn)
23915 load_end()
23916 })
23917 }
23918 return this.nreq == nreq_i
23919 }
23920 } // modules
23921 abc2svg.version="v1.22.34";abc2svg.vdate="2025-11-22"