Debian Codegraph @

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

raw symbols SCIP
1 // abc2svg - strtab.js - tablature for string instruments
2 //
3 // Copyright (C) 2020-2025 Jean-Francois Moine
4 //
5 // This file is part of abc2svg.
6 //
7 // abc2svg is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // abc2svg is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
19 //
20 // This module is loaded by %%strtab.
21 //
22 // The command %%strtab changes the display of the voice to a tablature.
23 // Syntax:
24 // %%strtab <string list> [diafret] [nodot]
25 // <string list> is the list of the strings as ABC notes
26 // diafret indicates the instrument has diatonic frets
27 // nodot removes the dots of dotted notes
28 // The fret may be forced by a decoration format
29 // "!" digit "s!"
30 // where 'digit' is the string range in the string list (last string is '1')
31 //
32 // The command %%minfret indicates the smallest usable fret numbers.
33 // Syntax:
34 // %%minfret [ <string number> : <fret number> ]*
35 // Each command replaces the previous value.
36 //
37 // %%tabfont font_name size (default: sans-serifBold 7)
38 // %%cstabfont font_name size (default %%gchordfont with size / 1.6)
39 //
40 // This module accepts Willem Vree's tablature syntax:
41 // https://wim.vree.org/svgParse/abc2xml.html#tab
42
43 if (typeof abc2svg == "undefined")
44 var abc2svg = {}
45
46 abc2svg.strtab = {
47
48 // draw the tablature
49 draw_symbols: function(of, p_v) {
50 var s, m, not, stb, x, y, g,
51 C = abc2svg.C,
52 abc = this
53
54 // draw the note heads
55 function draw_heads(stb, s) {
56 var m, not, x, y
57
58 for (m = 0; m <= s.nhd; m++) {
59 not = s.notes[m]
60 if (!not.nb)
61 continue
62 x = s.x - 3
63 if (not.nb.length > 1)
64 x -= 3
65 y = 3 * (not.pit - 18)
66 if (s.grace) {
67 abc.out_svg('<text class="bg' + abc.bgn
68 + '" transform="translate(')
69 abc.out_sxsy(x, ',', stb + y - 1.9)
70 abc.out_svg(') scale(.75)')
71 } else {
72 abc.out_svg('<text class="bg' + abc.bgn + '" x="')
73 abc.out_sxsy(x, '" y="', stb + y - 2.5)
74 }
75 abc.out_svg('">' + not.nb + '</text>\n')
76 }
77 } // draw_heads()
78
79 // draw the stems, flags and beams
80 function draw_stems(stb, s) {
81 if (!s.tabst)
82 return // don't draw any stem
83 var s1, s2, nfl, l, i, x,
84 y = stb + 3 * (s.notes[0].pit - 19) // -18) - 3
85 * s.p_v.staffscale,
86 h = (11 + 3 * (s.notes[0].pit - 18)) * s.p_v.staffscale
87
88 abc.out_svg('<path class="sW" d="M')
89 abc.out_sxsy(s.x, ' ', y)
90 abc.out_svg('v' + h.toFixed(1) +'"/>\n')
91
92 // draw the dots
93 if (s.dots) {
94 x = s.x + 4
95 i = (s.dur / 12) >> ((5 - s.nflags) - s.dots)
96 while (s.dots-- > 0) {
97 abc.xygl(x, stb - 8, (i & (1 << s.dots)) ? "dot" : "dot+")
98 x += 3.5
99 }
100 }
101
102 // draw the flag(s)
103 if (s.nflags <= 0
104 || !s.beam_end) // inside beam
105 return
106 y -= h
107 if (s.beam_st) { // no beam
108 abc.out_svg('<text class="f'
109 + abc.get_font('music').fid
110 + '" transform="translate(')
111 abc.out_sxsy(s.x, ',', y)
112 abc.out_svg(') scale('
113 + (s.grace ? '.5,.5' : '.7,.7') + ')">'
114 + String.fromCharCode(0xe23f + 2 * s.nflags)
115 + '</text>\n')
116 return
117 }
118
119 // draw the beam(s)
120 s2 = s
121 nfl = s.nflags
122 while (1) {
123 if (s.nflags > nfl)
124 nfl = s.nflags
125 if (s.beam_st)
126 break
127 s = s.prev
128 }
129 s1 = s
130 l = (s2.x - s1.x).toFixed(1)
131 abc.out_svg('<path d="M')
132 abc.out_sxsy(s1.x, ' ', y)
133 abc.out_svg('h' + l
134 + 'v-3h-' + l
135 + 'v3"/>\n')
136 if (nfl == 1)
137 return
138 //fixme: do more levels
139 y += 5
140 while (1) {
141 while (s.nflags < 2) {
142 s = s.next
143 if (s == s2)
144 break
145 }
146 if (s == s2)
147 break
148 s1 = s
149 while (s.next.nflags >= 2) {
150 s = s.next
151 if (s == s2)
152 break
153 }
154 l = (s.x - s1.x).toFixed(1)
155 abc.out_svg('<path d="M')
156 abc.out_sxsy(s1.x, ' ', y)
157 abc.out_svg('h' + l
158 + 'v-3h-' + l
159 + 'v3"/>\n')
160 if (s == s2)
161 break
162 s = s.next
163 if (s == s2)
164 break
165 }
166 } // draw_stems()
167
168 if (!p_v.tab) {
169 of(p_v)
170 return
171 }
172
173 // define the 'bgx' filter if not done yet
174 m = abc.cfmt().bgcolor || "white"
175 if (abc.bgt != m) {
176 if (!abc.bgn)
177 abc.bgn = 1
178 else
179 abc.bgn++
180 abc.bgt = m
181 abc.defs_add('\
182 <filter x="-0.1" y="0.1" width="1.2" height=".8" id="bg' + abc.bgn + '">\n\
183 <feFlood flood-color="' + m + '"/>\n\
184 <feComposite in="SourceGraphic" operator="over"/>\n\
185 </filter>')
186 abc.add_style('\n.bg' + abc.bgn + '{filter:url(#bg' + abc.bgn + ')}')
187 }
188
189 // adjust the symbol before generation
190 for (s = p_v.sym; s; s = s.next) {
191 switch (s.type) {
192 case C.KEY:
193 case C.METER:
194 case C.REST:
195 s.invis = true
196 break
197 }
198 }
199
200 stb = abc.get_staff_tb()[p_v.st].y
201
202 // draw the stems and beams
203 abc.set_sscale(-1) // no scale
204 for (s = p_v.sym; s; s = s.next) {
205 switch (s.type) {
206 case C.GRACE:
207 for (g = s.extra; g; g = g.next)
208 draw_stems(stb, g)
209 break
210 case C.NOTE:
211 draw_stems(stb, s)
212 break
213 }
214 }
215
216 // draw the note heads
217 abc.set_scale(p_v.sym) // (for draw_all_ties)
218 abc.out_svg('<g class="'
219 + abc.font_class(abc.get_font('tab'))
220 + '">\n')
221 for (s = p_v.sym; s; s = s.next) {
222 switch (s.type) {
223 case C.GRACE:
224 for (g = s.extra; g; g = g.next)
225 draw_heads(stb, g)
226 break
227 case C.NOTE:
228 draw_heads(stb, s)
229 break
230 }
231 }
232 abc.out_svg('</g>\n')
233
234 of(p_v)
235 }, // draw_symbols()
236
237 // change the font size of the chord symbols
238 csan_bld: function(of, s) {
239 if (s.p_v.tab) {
240 var i, gch,
241 fmt = this.cfmt()
242
243 for (i = 0; i < s.a_gch.length; i++) {
244 gch = s.a_gch[i]
245 if (gch.type != 'g')
246 continue
247
248 // create the smaller font if not done yet
249 if (!fmt.cstabfont) {
250 var f = gch.font
251
252 this.param_set_font("cstabfont",
253 f.name + ' ' + (f.size / 1.6).toFixed(1))
254 }
255 gch.font = this.get_font("cstab")
256 }
257 }
258 of(s)
259 }, // set_csan()
260
261 // set a format parameter
262 set_fmt: function(of, cmd, parm) {
263 switch (cmd) {
264 case "cstabfont":
265 this.param_set_font("cstabfont", parm)
266 return
267 case "strtab":
268 if (!parm)
269 return
270 var p_v = this.get_curvoice()
271 if (!p_v) {
272 this.get_parse().tab = parm
273 return
274 }
275 this.set_v_param("clef", "tab")
276 if (parm.indexOf("diafret") >= 0) {
277 this.set_v_param("diafret", true)
278 parm = parm.replace(/\s*diafret\s*/, "")
279 }
280 this.set_v_param("strings", parm)
281 return
282 case "minfret":
283 this.set_v_param("minfret", parm)
284 return
285 }
286 of(cmd, parm)
287 }, // set_fmt()
288
289 // change the notes when the global generation settings are done
290 set_stems: function(of) {
291 var p_v, i, m, nt, n, bi, bn, strss, g,
292 C = abc2svg.C,
293 abc = this,
294 s = abc.get_tsfirst(), // first symbol
295 strs = [], // notes per staff - index = staff
296 lstr = [] // lowest string per staff - index staff
297
298 // set a string (pitch) and a fret number
299 function set_pit(p_v, s, nt, i) {
300 var m,
301 st = s.st
302
303 if (i >= 0) {
304 nt.nb = ((p_v.diafret ? nt.pit : nt.midi) - p_v.tab[i])
305 .toString()
306 if (p_v.diafret && nt.acc)
307 nt.nb += '+'
308 nt.pit = i * 2 + 18
309 } else {
310 nt.nb = ""
311 nt.pit = 18
312 }
313 nt.acc = 0
314 nt.invis = true
315 if (!s.grace)
316 strss[i] = s.time + s.dur
317 if (p_v.pos.stm != C.SL_HIDDEN) {
318 if (!lstr[st])
319 lstr[st] = [ 10, null, C.BLEN, null ]
320 if (lstr[st][0] > i) {
321 lstr[st][0] = i // lowest string
322 lstr[st][1] = s
323 }
324 if (s.dur < lstr[st][2])
325 lstr[st][2] = s.dur
326 if (s.dots) {
327 lstr[st][3] = s.dots
328 delete s.dots
329 }
330 }
331 s.stemless = 1 //true
332 if (s.dots) { // have nicer dots
333 if (p_v.nodot)
334 delete s.dots
335 s.xmx = 0
336 for (m = 0; m <= s.nhd; m++)
337 s.notes[m].shhd = 0
338 s.dot_low = 0
339 }
340 } // set_pit()
341
342 function set_notes(p_v, s) {
343 var i, bi, bn, nt, m, n, ns
344
345 s.stem = -1 // down stems
346
347 // handle the fret numbers as chord decoration
348 if (!s.nhd && s.a_dd) {
349 i = s.a_dd.length
350 while (--i >= 0) {
351 bi = strnum(s.a_dd[i].name)
352 if (bi >= 0) {
353 nt = s.notes[0]
354 set_pit(p_v, s, nt, bi)
355 break
356 }
357 }
358 }
359 delete s.a_dd
360
361 if (s.sls) { // set the slurs above the staff
362 for (i = 0; i < s.sls.length; i++) {
363 s.sls[i].ty &= ~0x07
364 s.sls[i].ty |= C.SL_ABOVE
365 }
366 }
367
368 ls: for (m = 0; m <= s.nhd; m++) {
369 nt = s.notes[m]
370 if (nt.sls) { // if start of slur
371 for (i = 0; i < nt.sls.length; i++) {
372 nt.sls[i].ty &= ~0x07
373 nt.sls[i].ty |= C.SL_ABOVE
374 }
375 }
376 if (nt.nb) {
377 delete nt.a_dd
378 continue
379 }
380 if (nt.a_dd) {
381 i = nt.a_dd.length
382 while (--i >= 0) {
383 bi = strnum(nt.a_dd[i].name)
384 if (bi >= 0) {
385 set_pit(p_v, s, nt, bi)
386 delete nt.a_dd
387 continue ls
388 }
389 }
390 delete nt.a_dd
391 }
392
393 // search the best string
394 bn = 100
395 bi = -1
396 ns = i = p_v.tab.length
397 while (--i >= 0) {
398 if (strss[i] && strss[i] > s.time)
399 continue
400 n = (p_v.diafret ?
401 nt.pit : nt.midi) -
402 p_v.tab[i]
403 if (n >= 0 && n < bn
404 && (!p_v.minfret
405 || !p_v.minfret[ns - i]
406 || n >= p_v.minfret[ns - i])) {
407 bi = i
408 bn = n
409 }
410 }
411 set_pit(p_v, s, nt, bi)
412 }
413
414 s.y = 3 * (nt.pit - 18)
415 s.ymx = s.y + 2
416 s.ymn = 3 * (s.notes[0].pit - 18)
417 } // set_notes()
418
419 // get the string number from the decoration
420 // format is either !<n>s! or !<n>!
421 function strnum(n) {
422 n = n.match(/^([1-9])s?$/)
423 return n ? p_v.tab.length - n[1] : -1
424 } // strnum()
425
426 // set_stems entry
427 of()
428
429 // change the notes of the strings when a capo
430 p_v = abc.get_voice_tb()
431 for (n = 0; n < p_v.length; n++) {
432 if (!p_v[n].tab)
433 continue
434 m = p_v[n].capo
435 if (m) {
436 for (i = 0; i < p_v[n].tab.length; i++)
437 p_v[n].tab[i] += m
438 }
439 }
440
441 // loop on the notes of the voices with a tablature
442 for ( ; s; s = s.ts_next) {
443
444 // let a stem on the lowest string
445 if (s.seqst || (s.ts_prev && s.ts_prev.type == C.GRACE)) {
446 for (i = 0; i < lstr.length; i++) {
447 if (lstr[i] && lstr[i][2] < C.BLEN) {
448 lstr[i][1].tabst = 1
449 lstr[i][1].dots = lstr[i][3]
450 }
451 lstr[i] = null
452 }
453 }
454
455 p_v = s.p_v
456 if (!p_v.tab)
457 continue
458 strss = strs[s.st]
459 if (!strss)
460 strss = strs[s.st] = []
461 switch (s.type) {
462 case C.KEY:
463 case C.REST:
464 case C.TIME:
465 s.invis = true
466 default:
467 delete s.a_dd
468 break
469 case C.GRACE:
470 if (p_v.pos.gst == C.SL_HIDDEN)
471 s.sappo = 0
472 for (g = s.extra; g; g = g.next) {
473 set_notes(p_v, g)
474 for (i = 0; i < lstr.length; i++) {
475 if (lstr[i] && lstr[i][2] < C.BLEN) {
476 lstr[i][1].tabst = 1
477 lstr[i][1].dots = lstr[i][3]
478 }
479 lstr[i] = null
480 }
481 }
482 break
483 case C.NOTE:
484 set_notes(p_v, s)
485 break
486 }
487 }
488 for (i = 0; i < lstr.length; i++) {
489 if (lstr[i] && lstr[i][2] < C.BLEN) {
490 lstr[i][1].tabst = 1 // top of stem
491 lstr[i][1].dots = lstr[i][3]
492 }
493 }
494 }, // set_stems()
495
496 // set the bottom of the stems at end of line generation
497 set_glue: function(of, w) {
498 var v, p_v,
499 vtb = this.get_voice_tb()
500
501 of(w)
502 for (v = 0; v < vtb.length; v++) {
503 p_v = vtb[v]
504 if (!p_v.tab || !p_v.sym
505 || p_v.pos.stm == abc2svg.C.SL_HIDDEN)
506 continue
507 p_v.sym.ymn = -16
508 }
509 }, // set_glue()
510
511 // get the parameters of the current voice
512 set_vp: function(of, a) {
513 var i, e, g, tab, strs, ok,
514 parse = this.get_parse(),
515 p_v = this.get_curvoice()
516
517 // convert a list of ABC notes into a list of MIDI pitches
518 function abc2tab(p) {
519 var i, c, a,
520 t = []
521
522 if (p_v.diafret) {
523 for (i = 0; i < p.length; i++) {
524 c = p[i]
525 c = "CDEFGABcdefgab".indexOf(c)
526 if (c < 0)
527 return // null
528 c += 16
529 while (1) {
530 if (p[i + 1] == "'") {
531 c += 7
532 i++
533 } else if (p[i + 1] == ",") {
534 c -= 7
535 i++
536 } else {
537 break
538 }
539 }
540 t.push(c)
541 }
542 } else {
543 for (i = 0; i < p.length; i++) {
544 c = p[i]
545 switch (c) {
546 case '^':
547 case '_':
548 a = c == '^' ? 1 : -1
549 c = p[++i]
550 break
551 default:
552 a = 0
553 break
554 }
555 c = "CCDDEFFGGAABccddeffggaab".indexOf(c)
556 if (c < 0)
557 return // null
558 c += 60 + a
559 while (1) {
560 if (p[i + 1] == "'") {
561 c += 12
562 i++
563 } else if (p[i + 1] == ",") {
564 c -= 12
565 i++
566 } else {
567 break
568 }
569 }
570 t.push(c)
571 }
572 }
573 return t
574 } // abc2tab
575
576 // convert an array of <note name><octave> into a list of MIDI pitches
577 function str2tab(a) {
578 var str, p, o,
579 t = []
580
581 if (p_v.diafret) {
582 while (1) {
583 str = a.shift()
584 if (!str)
585 break
586 p = "CDEFGAB".indexOf(str[0])
587 o = Number(str[1])
588 if (p < 0 || isNaN(o))
589 return // null
590 t.push(o * 7 + p - 12) // C4 = 16 (12 = 4 * 7 - 16)
591 }
592 } else {
593 while (1) {
594 str = a.shift()
595 if (!str)
596 break
597 p = "CCDDEFFGGAAB".indexOf(str[0])
598 if (p < 0)
599 return // undefined
600 o = str[1]
601 switch (o) {
602 case '#':
603 case 'b':
604 p += o == '#' ? 1 : -1
605 o = Number(str[2])
606 break
607 default:
608 o = Number(str[1])
609 break
610 }
611 if (isNaN(o))
612 return // undefined
613 t.push((o + 1) * 12 + p) // C4 = 60
614 }
615 }
616 return t
617 } // str2tab()
618
619 // convert the list of <string number> ':' <fret number>
620 function minfret(a) {
621 var sf,
622 sfa = a.split(' ')
623
624 p_v.minfret = {}
625 while (1) {
626 sf = sfa.shift()
627 if (!sf)
628 break
629 sf = sf.split(':')
630 if (sf.length != 2)
631 break //fixme: error
632 p_v.minfret[sf[0]] = sf[1]
633 }
634 } //minfret()
635
636 for (i = 0; i < a.length; i++) {
637 switch (a[i]) {
638 case "clef=":
639 e = a[i + 1]
640 if (e != "tab")
641 break
642 a.splice(i, 1)
643 // fall thru
644 case "tab":
645 a.splice(i, 1)
646 i--
647 ok = true
648 break
649 case "strings=":
650 strs = a[++i]
651 ok = true
652 break
653 case "nostems":
654 p_v.pos.stm = abc2svg.C.SL_HIDDEN
655 p_v.pos.gst = abc2svg.C.SL_HIDDEN
656 break
657 case "capo=":
658 p_v.capo = Number(a[++i])
659 break
660 case "diafret=":
661 i++
662 case "diafret":
663 p_v.diafret = true
664 break
665 case "minfret=":
666 minfret(a[++i])
667 break
668 case "nodot":
669 p_v.nodot = 1 //true
670 break
671 }
672 }
673
674 // define the elements of the tablature
675 if (ok) {
676 if (!strs && parse.tab) { // if a global definition
677 strs = parse.tab
678 if (strs.indexOf("diafret") >= 0) {
679 p_v.diafret = true
680 strs = strs.replace(/\s*diafret\s*/, "")
681 }
682 if (strs.indexOf("nodot") >= 0) {
683 p_v.nodot = 1 //true
684 strs = strs.replace(/\s*nodot\s*/, "")
685 }
686 }
687 if (strs) {
688 e = strs.slice(-1)
689 if (e >= '1' && e <= '9')
690 tab = str2tab(strs.split(',')) // W.V.'s syntax
691 else
692 tab = abc2tab(strs) // ABC syntax
693 if (!tab) {
694 this.syntax(1, "Bad strings in tablature")
695 ok = false
696 }
697 } else if (!p_v.tab) {
698 tab = p_v.diafret ?
699 [17, 14, 10] : // dulcimer
700 [40, 45, 50, 55, 59, 64] // guitar strings
701 } else {
702 tab = p_v.tab
703 }
704 }
705 if (ok) {
706 if (p_v.capo) {
707 p_v.tab = []
708 for (i = 0; i < tab.length; i++)
709 p_v.tab.push(tab[i] + p_v.capo)
710 } else {
711 p_v.tab = tab
712 }
713
714 a.push("clef=") // set the clef
715 g = this.get_glyphs()
716 if (tab.length == 3) {
717 a.push('"tab3"')
718 if (!g.tab3)
719 // SMuFL: -none-
720 g.tab3 = '<text id="tab3"\
721 x="-2,-2,-2" y="-4,3,10"\
722 style="font:bold 8px sans-serif">TAB</text>'
723 } else if (tab.length == 4) {
724 a.push('"tab4"')
725 if (!g.tab4)
726 // SMuFL: \ue06e
727 g.tab4 = '<text id="tab4"\
728 x="-3,-3,-3" y="-8,1,10"\
729 style="font:bold 12px sans-serif">TAB</text>'
730 } else if (tab.length == 5) {
731 a.push('"tab5"')
732 if (!g.tab5)
733 // SMuFL: -none-
734 g.tab5 = '<text id="tab5"\
735 x="-4,-4,-4" y="-11,-2,7"\
736 style="font:bold 12px sans-serif">TAB</text>'
737 } else {
738 a.push('"tab6"')
739 if (!g.tab6)
740 // SMuFL: \ue06d
741 g.tab6 = '<text id="tab6"\
742 x="-4,-4,-4" y="-14.5,-4,5.5"\
743 style="font:bold 13px sans-serif">TAB</text>'
744 }
745 a.push("stafflines=")
746 a.push("|||||||||".slice(0, tab.length))
747 p_v.staffscale = 1.6
748 // p_v.scale = .6
749 // p_v.straightflags = true
750 }
751 of(a)
752 }, // set_vp()
753
754 set_hooks: function(abc) {
755 abc.draw_symbols = abc2svg.strtab.draw_symbols.bind(abc, abc.draw_symbols)
756 abc.gch_build = abc2svg.strtab.csan_bld.bind(abc, abc.gch_build)
757 abc.set_format = abc2svg.strtab.set_fmt.bind(abc, abc.set_format);
758 abc.set_stems = abc2svg.strtab.set_stems.bind(abc, abc.set_stems)
759 abc.set_sym_glue = abc2svg.strtab.set_glue.bind(abc, abc.set_sym_glue)
760 abc.set_vp = abc2svg.strtab.set_vp.bind(abc, abc.set_vp)
761
762 // define specific decorations used to force the string number
763 var decos = abc.get_decos()
764 decos["1s"] = "3 nil 0 0 0"
765 decos["2s"] = "3 nil 0 0 0"
766 decos["3s"] = "3 nil 0 0 0"
767 decos["4s"] = "3 nil 0 0 0"
768 decos["5s"] = "3 nil 0 0 0"
769 decos["6s"] = "3 nil 0 0 0"
770
771 abc.param_set_font("tabfont", "sans-serifBold 7")
772 } // set_hooks()
773 } // strtab
774
775 if (!abc2svg.mhooks)
776 abc2svg.mhooks = {}
777 abc2svg.mhooks.strtab = abc2svg.strtab.set_hooks