Debian Codegraph @

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

raw symbols SCIP
1 // break.js - module to handle the %%break command
2 //
3 // Copyright (C) 2018-2024 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 "%%break" appears in a ABC source.
21 //
22 // Parameters
23 // %%break measure_nb [":" num "/" den] [" " measure ...]*
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.break = {
29
30 // get the %%break parameters
31 get_break: function(parm) {
32 var C = abc2svg.C,
33 b, c, d, n, sq,
34 a = parm.split(/[ ,]/),
35 cfmt = this.cfmt()
36
37 if (!cfmt.break)
38 cfmt.break = []
39 for (n = 1; n < a.length; n++) {
40 b = a[n];
41 c = b.match(/(\d+)([a-z]?)(:\d+\/\d+)?/)
42 if (!c) {
43 this.syntax(1, this.errs.bad_val, "%%break")
44 continue
45 }
46 if (c[2])
47 sq = c[2].charCodeAt(0) - 0x61
48 if (!c[3]) {
49 cfmt.break.push({ // on measure bar
50 m: c[1],
51 t: 0,
52 sq: sq})
53 continue
54 }
55 d = c[3].match(/:(\d+)\/(\d+)/)
56 if (!d || d[2] < 1) {
57 this.syntax(1, "Bad denominator in %%break")
58 continue
59 }
60 cfmt.break.push({
61 m: c[1],
62 t: d[1] * C.BLEN / d[2],
63 sq: sq})
64 }
65 }, // get_break()
66
67 // insert the EOLs of %%break
68 do_break: function() {
69 var i, m, t, brk, s, seq,
70 voice_tb = this.get_voice_tb(),
71 v = this.get_cur_sy().top_voice,
72 s1 = voice_tb[v].sym,
73 cfmt = this.cfmt()
74
75 for (i = 0; i < cfmt.break.length; i++) {
76 brk = cfmt.break[i];
77 m = brk.m
78 for (s = s1; s; s = s.next) {
79 if (s.bar_num == m)
80 break
81 }
82 if (!s)
83 continue
84
85 if (brk.sq) {
86 seq = brk.sq
87 for (s = s.ts_next; s; s = s.ts_next) {
88 if (s.bar_num == m) {
89 if (--seq == 0)
90 break
91 }
92 }
93 if (!s)
94 continue
95 }
96
97 t = brk.t
98 if (t) {
99 t = s.time + t
100 for ( ; s; s = s.next) {
101 if (s.time >= t)
102 break
103 }
104 } else {
105 s = s.next
106 }
107 if (s)
108 s.soln = true
109 }
110 }, // do_break()
111
112 do_pscom: function (of, text) {
113 if (text.slice(0, 6) == "break ")
114 abc2svg.break.get_break.call(this, text)
115 else
116 of(text)
117 },
118
119 set_bar_num: function(of) {
120 of()
121 if (this.cfmt().break)
122 abc2svg.break.do_break.call(this)
123 },
124
125 set_hooks: function(abc) {
126 abc.do_pscom = abc2svg.break.do_pscom.bind(abc, abc.do_pscom);
127 abc.set_bar_num = abc2svg.break.set_bar_num.bind(abc, abc.set_bar_num)
128 }
129 } // break
130
131 if (!abc2svg.mhooks)
132 abc2svg.mhooks = {}
133 abc2svg.mhooks.break = abc2svg.break.set_hooks