Debian Codegraph @

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

raw symbols SCIP
1 // combine.js - module to add a combine chord line
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 "%%voicecombine" appears in a ABC source.
21 //
22 // Parameters
23 // %%voicecombine n 'n' is the combine level
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.combine = {
29
30 // function called at start of the generation when multi-voices
31 comb_v: function() {
32 var C = abc2svg.C,
33 abc = this
34
35 // get the symbol of the note to combine
36 function get_cmb(s) {
37 var p,
38 s2 = s.ts_next,
39 i = s.p_v.id.indexOf('.') // '.' is the group separator
40
41 if (i >= 0) {
42 p = s.p_v.id.slice(0, i) // group
43 while (s2 && s2.time == s.time) {
44 if (s2.p_v.id.indexOf(p) == 0)
45 break
46 s2 = s2.ts_next
47 }
48 }
49 return s2
50 } // get_cmb()
51
52 // check if voice combine may occur
53 function may_combine(s) {
54 var nhd2,
55 s2 = get_cmb(s)
56
57 if (!s2 || (s2.type != C.NOTE && s2.type != C.REST))
58 return false
59 if (s2.st != s.st
60 || s2.time != s.time
61 || s2.dur != s.dur)
62 return false
63 if (s.combine <= 0
64 && s2.type != s.type)
65 return false
66 // if (s2.a_dd) { //fixme: should check the double decorations
67 // return false
68 // }
69 if (s.a_gch && s2.a_gch)
70 return false
71 if (s.type == C.REST) {
72 if (s.type == s2.type) {
73 if (s.invis && !s2.invis)
74 return //false
75 } else if (s.combine <= 2) {
76 return //false
77 }
78 return true
79 }
80 if (s2.beam_st != s.beam_st
81 || s2.beam_end != s.beam_end)
82 return false;
83 nhd2 = s2.nhd
84 if (s.combine <= 1
85 && s.notes[0].pit <= s2.notes[nhd2].pit + 1)
86 return false
87 return true
88 } // may_combine()
89
90 // combine two notes
91 function combine_notes(s, s2) {
92 var nhd, type, m, not
93
94 // put the notes of the 2nd voice into the 1st one
95 for (m = 0; m <= s2.nhd; m++) {
96 not = abc.clone(s2.notes[m])
97 not.noplay = true // and don't play it
98 s.notes.push(not)
99 }
100 s.nhd = nhd = s.notes.length - 1;
101 s.notes.sort(abc2svg.pitcmp) // sort the notes by pitch
102
103 if (s.combine >= 3) { // remove unison heads
104 //fixme: KO for playback
105 for (m = nhd; m > 0; m--) {
106 if (s.notes[m].pit == s.notes[m - 1].pit
107 && s.notes[m].acc == s.notes[m - 1].acc)
108 s.notes.splice(m, 1)
109 }
110 s.nhd = nhd = s.notes.length - 1
111 }
112
113 s.ymx = 3 * (s.notes[nhd].pit - 18) + 4;
114 s.ymn = 3 * (s.notes[0].pit - 18) - 4;
115
116 // force the tie directions
117 type = s.notes[0].tie_ty
118 if ((type & 0x07) == C.SL_AUTO)
119 s.notes[0].tie_ty = C.SL_BELOW | (type & C.SL_DOTTED);
120 type = s.notes[nhd].tie_ty
121 if ((type & 0x07) == C.SL_AUTO)
122 s.notes[nhd].tie_ty = C.SL_ABOVE | (type & C.SL_DOTTED)
123 } // combine_notes()
124
125 // combine 2 voices
126 // return the remaining one
127 function do_combine(s) {
128 var s2, s3, type, i, n, sl
129
130 s2 = get_cmb(s)
131
132 // there may be more voices
133 if (!s.in_tuplet
134 && s2.combine != undefined && s2.combine >= 0
135 && may_combine(s2))
136 s2 = do_combine(s2)
137
138 if (s.type != s2.type) { // if note and rest
139 if (s2.type != C.REST) {
140 s2 = s;
141 s = s2.ts_next
142 }
143 } else if (s.type == C.REST) {
144 if (s.invis
145 && !s2.invis)
146 delete s.invis
147 s.multi = 0
148 } else {
149 combine_notes(s, s2)
150 if (s2.ti1)
151 s.ti1 = true
152 if (s2.ti2)
153 s.ti2 = true
154 }
155
156 // if some slurs start on the second symbol
157 // move them to the combined symbol
158 // also, set a flag in the symbols of the ending slurs
159 if (s2.sls) {
160 if (s.sls)
161 Array.prototype.push.apply(s.sls, s2.sls)
162 else
163 s.sls = s2.sls
164 for (i = 0; i < s2.sls.length; i++) {
165 sl = s2.sls[i]
166 if (sl.se)
167 sl.se.slsr = s // reverse pointer
168 sl.ty = C.SL_BELOW
169 }
170 delete s2.sls
171 }
172
173 // if a combined slur is ending on the second symbol,
174 // update its starting symbol
175 s3 = s2.slsr // pointer to the starting symbol
176 if (s3) {
177 for (i = 0; i < s3.sls.length; i++) {
178 sl = s3.sls[i]
179 if (sl.se == s2)
180 sl.se = s
181 }
182 }
183
184 if (s2.a_gch)
185 s.a_gch = s2.a_gch
186 if (s2.a_dd) {
187 if (!s.a_dd)
188 s.a_dd = s2.a_dd
189 else
190 Array.prototype.push.apply(s.a_dd, s2.a_dd)
191 }
192
193 s2.play = s2.invis = true // don't display, but play
194 return s
195 } // do_combine()
196
197 // code of comb_v()
198 var s, s2, g, i, r
199
200 for (s = abc.get_tsfirst(); s; s = s.ts_next) {
201 switch (s.type) {
202 case C.REST:
203 if (s.combine == undefined || s.combine < 0)
204 continue
205 if (may_combine(s))
206 s = do_combine(s)
207 // continue // fall thru
208 default:
209 continue
210 case C.NOTE:
211 if (s.combine == undefined || s.combine <= 0)
212 continue
213 break
214 }
215
216 if (!s.beam_st)
217 continue
218
219 s2 = s
220 while (1) {
221 if (!may_combine(s2)) {
222 s2 = null
223 break
224 }
225 //fixme: may have rests in beam
226 if (s2.beam_end)
227 break
228 do {
229 s2 = s2.next
230 } while (s2.type != C.NOTE && s2.type != C.REST)
231 }
232 if (!s2)
233 continue
234 s2 = s
235 while (1) {
236 s2 = do_combine(s2)
237 //fixme: may have rests in beam
238 if (s2.beam_end)
239 break
240 do {
241 s2 = s2.next
242 } while (s2.type != C.NOTE && s2.type != C.REST)
243 }
244 }
245 }, // comb_v()
246
247 do_pscom: function(of, text) {
248 if (text.slice(0, 13) == "voicecombine ")
249 this.set_v_param("combine", text.split(/[ \t]/)[1])
250 else
251 of(text)
252 },
253
254 new_note: function(of, gr, tp) {
255 var curvoice = this.get_curvoice()
256 var s = of(gr, tp)
257 if (s && s.notes && curvoice.combine != undefined)
258 s.combine = curvoice.combine
259 return s
260 },
261
262 set_stem_dir: function(of) {
263 of();
264 abc2svg.combine.comb_v.call(this)
265 },
266
267 // set the combine parameter in the current voice
268 set_vp: function(of, a) {
269 var i,
270 curvoice = this.get_curvoice()
271
272 for (i = 0; i < a.length; i++) {
273 if (a[i] == "combine=") { // %%voicecombine
274 curvoice.combine = a[i + 1]
275 break
276 }
277 }
278 of(a)
279 },
280
281 set_hooks: function(abc) {
282 abc.do_pscom = abc2svg.combine.do_pscom.bind(abc, abc.do_pscom);
283 abc.new_note = abc2svg.combine.new_note.bind(abc, abc.new_note);
284 abc.set_stem_dir = abc2svg.combine.set_stem_dir.bind(abc, abc.set_stem_dir);
285 abc.set_vp = abc2svg.combine.set_vp.bind(abc, abc.set_vp)
286 }
287 } // combine
288
289 if (!abc2svg.mhooks)
290 abc2svg.mhooks = {}
291 abc2svg.mhooks.combine = abc2svg.combine.set_hooks