Debian Codegraph @

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

raw symbols SCIP
1 // jianpu.js - module to output jiănpŭ (简谱) music sheets
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 when "%%jianpu" appears in a ABC source.
21 //
22 // Parameters (none)
23 // %%jianpu 1
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.jianpu = {
29
30 k_tb: [ "Cb", "Gb", "Db", "Ab", "Eb", "Bb", "F",
31 "C",
32 "G", "D", "A", "E", "B", "F#", "C#" ],
33 cde2fcg: new Int8Array([0, 2, 4, -1, 1, 3, 5]),
34 cgd2cde: new Int8Array([0, -4, -1, -5, -2, -6, -3,
35 0, -4, -1, -5, -2, -6, -3, 0]),
36 acc2: new Int8Array([-2, -1, 3, 1, 2]),
37 acc_tb: ["\ue264", "\ue260", , "\ue262", "\ue263", "\ue261"],
38
39 // don't calculate the beams
40 calc_beam: function(of, bm, s1) {
41 if (!s1.p_v.jianpu)
42 return of(bm, s1)
43 // return 0
44 }, // calc_beam()
45
46 // adjust some symbols before the generation
47 output_music: function(of) {
48 var p_v, v,
49 C = abc2svg.C,
50 abc = this,
51 cur_sy = abc.get_cur_sy(),
52 voice_tb = abc.get_voice_tb()
53
54 // handle the overlay voices
55 function ov_def(v) {
56 var s1, tim,
57 s = p_v.sym
58
59 while (s) {
60 s1 = s.ts_prev
61 if (!s.invis
62 && s.dur
63 && s1.v != v
64 && s1.st == s.st // overlay start
65 && s1.time == s.time) {
66 while (1) { // go back to the previous bar
67 if (!s1.prev
68 || s1.prev.bar_type)
69 break
70 s1 = s1.prev
71 }
72 //add deco '{' on s1
73 while (!s1.bar_type) {
74 s1.dy = 14
75 s1.notes[0].pit = 30
76 if (s1.type == C.REST)
77 s1.combine = -1
78 s1 = s1.next
79 }
80 // add deco '}' on s1
81
82 while (1) {
83 s.dy = -14
84 s.notes[0].pit = 20
85 if (!s.next
86 || s.next.bar_type
87 || s.next.time >= s1.time)
88 break
89 s = s.next
90 }
91 }
92 s = s.next
93 }
94 } // ov_def()
95
96 // output the key and time signatures
97 function set_head() {
98 var v, p_v, mt, s2, sk, s,
99 tsfirst = abc.get_tsfirst()
100
101 // search a jianpu voice
102 for (v = 0; v < voice_tb.length; v++) {
103 p_v = voice_tb[v]
104 if (p_v.jianpu)
105 break
106 }
107 if (v >= voice_tb.length)
108 return
109
110 mt = p_v.meter.a_meter[0]
111 sk = p_v.key
112 s2 = p_v.sym
113 s = {
114 type: C.BLOCK,
115 subtype: "text",
116 time: s2.time,
117 dur: 0,
118 v: 0,
119 p_v: p_v,
120 st: 0,
121 fmt: s2.fmt,
122 seqst: true,
123 text: (sk.k_mode + 1) + "=" +
124 (abc2svg.jianpu.k_tb[sk.k_sf + 7 +
125 abc2svg.jianpu.cde2fcg[sk.k_mode]]),
126 font: abc.get_font("text")
127 }
128
129 if (mt)
130 s.text += ' ' + (mt.bot ? (mt.top + '/' + mt.bot) : mt.top)
131
132 // insert the block after the first %%staves
133 s2 = tsfirst
134 s.next = s2.next
135 if (s.next)
136 s.next.prev = s
137 s.prev = s2
138 s2.next = s
139 s.ts_next = s2.ts_next
140 s.ts_next.ts_prev = s
141 s.ts_prev = s2
142 s2.ts_next = s
143 } // set head()
144
145 // expand a long note/rest
146 function slice(s) {
147 var n, s2, s3,
148 jn = s.type == C.REST ? 0 : 8 // '0' or '-'
149
150 if (s.dur >= C.BLEN)
151 n = 3
152 else if (s.dur == C.BLEN / 2)
153 n = 1
154 else
155 n = 2
156
157 // duplicate the note/rest for display
158 s2 = abc.clone(s)
159 s2.invis =
160 s2.play = 1 //true
161 s2.next = s
162 if (s2.prev)
163 s2.prev.next = s2
164 s.prev = s2
165 s2.ts_next = s
166 if (s2.ts_prev)
167 s2.ts_prev.ts_next = s2
168 s.ts_prev = s2
169 delete s.seqst
170 s.noplay = 1 // true
171
172 // create the continuation symbols
173 // s.notes[0].dur =
174 s.dur = s.dur_orig = C.BLEN / 4
175 delete s.fmr
176 while (--n >= 0) {
177 s2 = {
178 type: C.REST,
179 v: s.v,
180 p_v: s.p_v,
181 st: s.st,
182 dur: C.BLEN / 4,
183 dur_orig: C.BLEN / 4,
184 fmt: s.fmt,
185 stem: 0,
186 multi: 0,
187 nhd: 0,
188 notes: [{
189 dur: s.dur,
190 pit: s.notes[0].pit,
191 jn: jn
192 }],
193 xmx: 0,
194 noplay: true,
195 time: s.time + C.BLEN / 4,
196 prev: s,
197 next: s.next
198 }
199 s.next = s2
200 if (s2.next)
201 s2.next.prev = s2
202
203 if (!s.ts_next) {
204 s.ts_next = s2
205 if (s.soln)
206 s.soln = false
207 s2.ts_prev = s
208 s2.seqst = true
209 } else {
210 for (s3 = s.ts_next; s3; s3 = s3.ts_next) {
211 if (s3.time < s2.time)
212 continue
213 if (s3.time > s2.time) {
214 s2.seqst = true
215 s3 = s3.ts_prev
216 }
217 s2.ts_next = s3.ts_next
218 s2.ts_prev = s3
219 if (s2.ts_next)
220 s2.ts_next.ts_prev = s2
221 s3.ts_next = s2
222 break
223 }
224 }
225 s = s2
226 }
227 } // slice()
228
229 function set_note(s, sf) {
230 var i, m, note, p, pit, a, nn,
231 delta = abc2svg.jianpu.cgd2cde[sf + 7] - 2
232
233 s.stem = -1
234 s.stemless = true
235
236 if (s.sls) {
237 for (i = 0; i < s.sls.length; i++)
238 s.sls[i].ty = C.SL_ABOVE
239 }
240
241 for (m = 0; m <= s.nhd; m++) {
242 note = s.notes[m]
243
244 // note head
245 p = note.pit
246 pit = p + delta
247 note.jn = ((pit + 77) % 7) + 1 // note number
248
249 // set a fixed offset to the note for the decorations
250 note.pit = 25 // "e"
251
252 note.jo = (pit / 7) | 0 // octave number
253
254 // accidentals
255 a = note.acc
256 if (a) {
257 nn = abc2svg.jianpu.cde2fcg[(p + 5 + 16 * 7) % 7] - sf
258 if (a != 3)
259 nn += a * 7
260 nn = ((((nn + 1 + 21) / 7) | 0) + 2 - 3 + 32 * 5) % 5
261 note.acc = abc2svg.jianpu.acc2[nn]
262 }
263
264 // set the slurs and ties up
265 if (note.sls) {
266 for (i = 0; i < note.sls.length; i++)
267 note.sls[i].ty = C.SL_ABOVE
268 }
269 if (note.tie_ty)
270 note.tie_ty = C.SL_ABOVE
271 }
272
273 // change the long notes
274 if (s.dur >= C.BLEN / 2
275 && !s.invis)
276 slice(s)
277
278 // replace the staccato dot
279 if (s.a_dd) {
280 for (i = 0; i < s.a_dd.length; i++) {
281 if (s.a_dd[i].glyph == "stc") {
282 abc.deco_put("gstc", s)
283 s.a_dd[i] = s.a_dd.pop()
284 }
285 }
286 }
287 } // set_note()
288
289 function set_sym(p_v) {
290 var s, g,
291 sf = p_v.key.k_sf
292
293 delete p_v.key.k_a_acc // no accidental
294
295 // no (visible) clef
296 s = p_v.clef
297 s.invis = true
298 s.clef_type = 't'
299 s.clef_line = 2
300
301 // scan the voice
302 for (s = p_v.sym; s; s = s.next) {
303 s.st = p_v.st
304 switch (s.type) {
305 case C.CLEF:
306 s.invis = true
307 s.clef_type = 't'
308 s.clef_line = 2
309 // continue
310 default:
311 continue
312 case C.KEY:
313 sf = s.k_sf
314 s.a_gch = [{
315 type: '@',
316 font: abc.get_font("annotation"),
317 wh: [10, 10],
318 x: -5,
319 y: 26,
320 text: (s.k_mode + 1) + "=" +
321 (abc2svg.jianpu.k_tb[sf + 7 +
322 abc2svg.jianpu.cde2fcg[s.k_mode]])
323 }]
324 continue
325 case C.REST:
326 if (s.notes[0].jn)
327 continue
328 s.notes[0].jn = 0
329 if (s.dur >= C.BLEN / 2
330 && !s.invis)
331 slice(s)
332 continue
333 case C.NOTE: // change the notes
334 set_note(s, sf)
335 break
336 case C.GRACE:
337 for (g = s.extra; g; g = g.next)
338 set_note(g, sf)
339 break
340 }
341 }
342 } // set_sym()
343
344 // -- output_music --
345
346 set_head()
347
348 for (v = 0; v < voice_tb.length; v++) {
349 p_v = voice_tb[v]
350 if (p_v.jianpu) {
351 set_sym(p_v)
352 if (p_v.second)
353 ov_def(v)
354 }
355 }
356
357 of()
358 }, // output_music()
359
360 draw_symbols: function(of, p_voice) {
361 var s, s2, g, nl, y,
362 C = abc2svg.C,
363 abc = this,
364 dot = "\ue1e7",
365 anno_a = abc.anno_a,
366 staff_tb = abc.get_staff_tb(),
367 out_svg = abc.out_svg,
368 out_sxsy = abc.out_sxsy,
369 xypath = abc.xypath
370
371 if (!p_voice.jianpu) {
372 of(p_voice)
373 return
374 }
375
376 // draw the duration lines under the notes
377 function draw_dur(s1, x, y, s2, n, nl) {
378 var s, s3
379
380 xypath(x - 3, y + 5)
381 out_svg('h' + (s2.x - s1.x + 8).toFixed(1) + '"/>\n')
382 y -= 2.5
383 while (++n <= nl) {
384 s = s1
385 while (1) {
386 if (s.nflags && s.nflags >= n) {
387 s3 = s
388 while (s != s2) {
389 if (s.next.beam_br1
390 || (s.next.beam_br2 && n > 2)
391 || (s.next.nflags
392 && s.next.nflags < n))
393 break
394 s = s.next
395 }
396 draw_dur(s3, s3.x, y, s, n, nl)
397 }
398 if (s == s2)
399 break
400 s = s.next
401 }
402 }
403 } // draw_dur()
404
405 // draw the duration lines of grace notes
406 // and the slurs
407 // (assume all notes have the same duration)
408 function draw_dgr(s) {
409 var g, x, l,
410 y = staff_tb[s.st].y,
411 s2 = s.extra,
412 nl = s2.nflags
413
414 for (g = s2; g.next; g = g.next)
415 ;
416 out_svg('<g transform="translate(')
417 out_sxsy(s2.x, ',', y + 19) // (font height + 4)
418 out_svg(') scale(.5)" stroke-width="1.5">\n')
419 abc.stv_g().g++ // in container
420 l = ((g.x - s2.x) * 2 + 8).toFixed(1)
421 y = (nl > 0 ? 5 * nl : 0) + 2
422 if (nl > 0) {
423 xypath(-3, 0)
424 while (--nl >= 0) {
425 out_svg('m0 4h' + l)
426 l = -l
427 }
428 out_svg('"/>\n')
429 }
430 if (s.fmt.graceslurs // if a slur,
431 && !s.sl1) { // but not an explicit one
432 l = Math.abs(l)
433 out_svg('<path class="stroke" \
434 d="m' + (l / 2 - 3).toFixed(1)+' '+y.toFixed(1))
435 if (s.gr_shift) {
436 out_svg('m0 0c0 10 0 10 -10 10')
437 } else {
438 out_svg('m0 0c0 10 0 10 10 10')
439 }
440 out_svg('"/>\n')
441 }
442 out_svg('</g>\n')
443 abc.stv_g().g--
444 } // draw_dgr()
445
446 function out_mus(x, y, p) {
447 out_svg('<text x="')
448 out_sxsy(x, '" y="', y)
449 out_svg('">' + p + '</text>\n')
450 } // out_mus()
451
452 function out_txt(x, y, p) {
453 out_svg('<text class="fj" x="')
454 out_sxsy(x, '" y="', y)
455 out_svg('">' + p + '</text>\n')
456 } // out_txt()
457
458 function draw_hd(s, x, y) {
459 var m, note, ym
460
461 for (m = 0; m <= s.nhd; m++) {
462 note = s.notes[m]
463 out_txt(x - 3.5, y + 8, "01234567-"[note.jn])
464 if (note.acc)
465 out_mus(x - 12, y + 12,
466 abc2svg.jianpu.acc_tb[note.acc + 2])
467 if (note.jo > 2) {
468 out_mus(x - 1, y + 22, dot)
469 if (note.jo > 3) {
470 y += 3
471 out_mus(x - 1, y + 22, dot)
472 }
473 } else if (note.jo < 2) {
474 ym = y + 4
475 if (m == 0 && s.nflags > 0)
476 ym -= 2.5 * s.nflags
477 out_mus(x - 1, ym, dot)
478 if (note.jo < 1) {
479 ym -= 3
480 out_mus(x - 1, ym, dot)
481 }
482 }
483 y += 20
484 }
485 } // draw_hd()
486
487 function draw_note(s) {
488 var sc = 1,
489 x = s.x,
490 y = staff_tb[s.st].y
491
492 if (s.dy)
493 y += s.dy // voice overlay
494
495 if (s.grace) {
496 out_svg('<g transform="translate(')
497 out_sxsy(x, ',', y + 15) // (font height)
498 out_svg(') scale(.5)">\n')
499 abc.stv_g().g++ // in container
500 x = 0
501 y = 0
502 sc = .5
503 }
504
505 draw_hd(s, x, y)
506
507 if (s.nflags >= 0 && s.dots)
508 out_mus(x + 8 * sc, y + 13 * sc, dot)
509 if (s.grace) {
510 out_svg('</g>\n')
511 abc.stv_g().g--
512 }
513 anno_a.push(s)
514 } // draw_note()
515
516 // -- draw_symbols --
517 for (s = p_voice.sym; s; s = s.next) {
518 if (s.invis)
519 continue
520 switch (s.type) {
521 case C.METER:
522 abc.draw_meter(s)
523 break
524 case C.NOTE:
525 case C.REST:
526 draw_note(s)
527 break
528 case C.GRACE:
529 for (g = s.extra; g; g = g.next)
530 draw_note(g)
531 break
532 }
533 }
534
535 // draw the (pseudo) beams
536 for (s = p_voice.sym; s; s = s.next) {
537 if (s.invis)
538 continue
539 switch (s.type) {
540 case C.NOTE:
541 case C.REST:
542 nl = s.nflags
543 if (nl <= 0)
544 continue
545 y = staff_tb[s.st].y
546 s2 = s
547 while (1) {
548 if (s.nflags > nl)
549 nl = s.nflags
550 if (s.beam_end)
551 break
552 if (s.type == C.GRACE)
553 draw_dgr(s)
554 if (!s.next)
555 break
556 s = s.next
557 }
558 if (s.dy)
559 y += s.dy
560 draw_dur(s2, s2.x, y, s, 1, nl)
561 break
562 case C.GRACE:
563 draw_dgr(s)
564 break
565 }
566 }
567 }, // draw_symbols()
568
569 // set some parameters
570 set_fmt: function(of, cmd, param) {
571 if (cmd == "jianpu") {
572 this.set_v_param("jianpu", param)
573 return
574 }
575 of(cmd, param)
576 }, // set_fmt()
577
578 // adjust some values
579 set_pitch: function(of, last_s) {
580 of(last_s)
581 if (!last_s)
582 return // first time
583
584 var C = abc2svg.C
585
586 for (var s = this.get_tsfirst(); s; s = s.ts_next) {
587 if (!s.p_v.jianpu)
588 continue
589 switch (s.type) {
590
591 // draw the key signature only in the first voice
592 // and not at start of the staff
593 case C.KEY:
594 if (s.prev.type == C.CLEF
595 || s.v != 0)
596 s.a_gch = null
597 break
598
599 // adjust the vertical spacing above the note heads
600 case C.NOTE:
601 s.ymx = 20 * s.nhd + 22
602 if (s.notes[s.nhd].jo > 2) {
603 s.ymx += 3
604 if (s.notes[s.nhd].jo > 3)
605 s.ymx += 2
606 }
607 s.ymn = 0 // bottom of line
608 break
609 }
610 }
611 }, // set_pitch()
612
613 set_vp: function(of, a) {
614 var i,
615 p_v = this.get_curvoice()
616
617 for (i = 0; i < a.length; i++) {
618 if (a[i] == "jianpu=") {
619 p_v.jianpu = this.get_bool(a[++i])
620 if (p_v.jianpu)
621 this.set_vp([
622 "staffsep=", "20",
623 "sysstaffsep=", "14",
624 "stafflines=", "...",
625 "tuplets=", "0 1 0 1"
626 // [auto, slur, number, above]
627 ])
628 break
629 }
630 }
631 of(a)
632 }, // set_vp()
633
634 // set the width of some symbols
635 set_width: function(of, s) {
636 of(s)
637 if (!s.p_v // (if voice_tb[v].clef/key/meter)
638 || !s.p_v.jianpu)
639 return
640
641 var w, m, note,
642 C = abc2svg.C
643
644 switch (s.type) {
645 case C.CLEF:
646 case C.KEY:
647 // case C.METER:
648 s.wl = s.wr = .1 // (must not be null)
649 break
650 case C.NOTE:
651 for (m = 0; m <= s.nhd; m++) {
652 note = s.notes[m]
653 if (note.acc && s.wl < 14) // room for the accidental
654 s.wl = 14
655 }
656 break
657 }
658 }, // set_width()
659
660 set_hooks: function(abc) {
661 abc.calculate_beam = abc2svg.jianpu.calc_beam.bind(abc, abc.calculate_beam)
662 abc.draw_symbols = abc2svg.jianpu.draw_symbols.bind(abc, abc.draw_symbols)
663 abc.output_music = abc2svg.jianpu.output_music.bind(abc, abc.output_music)
664 abc.set_format = abc2svg.jianpu.set_fmt.bind(abc, abc.set_format)
665 abc.set_pitch = abc2svg.jianpu.set_pitch.bind(abc, abc.set_pitch)
666 abc.set_vp = abc2svg.jianpu.set_vp.bind(abc, abc.set_vp)
667 abc.set_width = abc2svg.jianpu.set_width.bind(abc, abc.set_width)
668
669 // big staccato dot
670 abc.get_glyphs().gstc = '<circle id="gstc" cx="0" cy="-3" r="2"/>'
671 abc.get_decos().gstc = "0 gstc 5 1 1"
672
673 abc.add_style("\n.fj{font:15px sans-serif}")
674 } // set_hooks()
675 } // jianpu
676
677 if (!abc2svg.mhooks)
678 abc2svg.mhooks = {}
679 abc2svg.mhooks.jianpu = abc2svg.jianpu.set_hooks