Debian Codegraph @

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

raw symbols SCIP
1 // clip.js - module to handle the %%clip 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 "%%clip" appears in a ABC source.
21 //
22 // Parameters
23 // %%clip start_measure_nb [":" num "/" den] "-" end_measure_nb [":" num "/" den]
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.clip = {
29
30 get_clip: function(parm) {
31 var C = abc2svg.C
32
33 // get the start/stop points
34 function get_symsel(a) {
35 var j, d, sq,
36 b = a.match(/(\d+)([a-z]?)(:\d+\/\d+)?/)
37
38 if (!b)
39 return
40 if (b[2])
41 sq = b[2].charCodeAt(0) - 0x61
42 if (!b[3])
43 return {m: b[1], t: 0, sq: sq} // on measure bar
44 a = b[3].match(/:(\d+)\/(\d+)/)
45 if (!a || a[2] < 1)
46 return
47 return {m: b[1], t: a[1] * C.BLEN / a[2], sq: sq}
48 } // get_symsel()
49
50 var b, c,
51 a = parm.split(/[ -]/)
52
53 if (a.length != 3) {
54 this.syntax(1, this.errs.bad_val, "%%clip")
55 return
56 }
57 if (!a[1])
58 b = {m: 0, t: 0}
59 else
60 b = get_symsel(a[1]);
61 c = get_symsel(a[2])
62 if (!b || !c) {
63 this.syntax(1, this.errs.bad_val, "%%clip")
64 return
65 }
66 this.cfmt().clip = [b, c]
67 }, // get_clip()
68
69 // cut the tune
70 do_clip: function() {
71 var C = abc2svg.C,
72 abc = this,
73 voice_tb = this.get_voice_tb(),
74 cfmt = this.cfmt()
75
76 // check the pointers (ties and slurs)
77 function chkptr(last) {
78 var i, sl, s2, m, nt,
79 tim = last.time,
80 s = abc.get_tsfirst()
81
82 do {
83 if (s.sls) {
84 for (i = 0; i < s.sls.length; i++) {
85 sl = s.sls[i]
86 s2 = sl.se // slur end
87 if (s2.time >= tim)
88 sl.loc = 'o' // no end
89 }
90 }
91 if (s.ti1 // if tie
92 && s.time + s.dur >= tim) { // ending out of the clip seq.
93 for (m = 0; m <= s.nhd; m++) {
94 nt = s.notes[m]
95 if (nt.tie_e && nt.tie_e.s.time >= tim)
96 delete nt.tie_e // no end
97 }
98 }
99 s = s.ts_next
100 } while (s)
101 } // chkptr()
102
103 // go to a global (measure + time)
104 function go_global_time(s, sel) {
105 var s2, bar_time, seq
106
107 if (sel.m <= 1) { // special case: there is no measure 0/1
108 if (sel.m == 1) {
109 for (s2 = s; s2; s2 = s2.ts_next) {
110 if (s2.type == C.BAR
111 && s2.time != 0)
112 break
113 }
114 if (s2.time < voice_tb[abc.get_cur_sy().top_voice].
115 meter.wmeasure)
116 s = s2
117 }
118 } else {
119 for ( ; s; s = s.ts_next) {
120 if (s.type == C.BAR
121 && s.bar_num >= sel.m)
122 break
123 }
124 if (!s)
125 return // null
126
127 if (sel.sq) {
128 seq = sel.sq
129 for (s = s.ts_next; s; s = s.ts_next) {
130 if (s.type == C.BAR
131 && s.bar_num == sel.m) {
132 if (--seq == 0)
133 break
134 }
135 }
136 if (!s)
137 return // null
138 }
139 }
140
141 if (sel.t == 0)
142 return s;
143 bar_time = s.time + sel.t
144 while (s.time < bar_time) {
145 s = s.ts_next
146 if (!s)
147 return s
148 }
149 do {
150 s = s.ts_prev // go back to the previous sequence
151 } while (!s.seqst)
152 return s
153 }
154
155 var s, s2, sy, p_voice, v
156
157 // remove the beginning of the tune
158 s = this.get_tsfirst()
159 if (cfmt.clip[0].m > 0
160 || cfmt.clip[0].t > 0) {
161 s = go_global_time(s, cfmt.clip[0])
162 if (!s) {
163 this.set_tsfirst(null)
164 return
165 }
166
167 // update the start of voices
168 sy = this.get_cur_sy()
169 for (s2 = this.get_tsfirst(); s2 != s; s2 = s2.ts_next) {
170 switch (s2.type) {
171 case C.CLEF:
172 s2.p_v.clef = s2
173 break
174 case C.KEY:
175 s2.p_v.key = this.clone(s2.as.u.key)
176 s2.p_v.ckey = this.clone(s2.as.u.ckey)
177 break
178 case C.METER:
179 s2.p_v.meter = this.clone(s2.as.u.meter)
180 break
181 case C.STAVES:
182 sy = s2.sy;
183 this.set_cur_sy(sy)
184 break
185 }
186 }
187 for (v = 0; v < voice_tb.length; v++) {
188 p_voice = voice_tb[v]
189 for (s2 = s; s2; s2 = s2.ts_next) {
190 if (s2.v == v) {
191 delete s2.prev
192 break
193 }
194 }
195 p_voice.sym = s2
196 }
197 s2 = this.get_tsfirst()
198 if (s != s2) {
199 if (s2.type == C.STAVES) {
200 s2.ts_next = s
201 s.ts_prev = s2
202 s2.next = s2.p_v.sym
203 s2.p_v.sym = s2
204 s2.next.prev = s2
205 } else {
206 this.set_tsfirst(s)
207 delete s.ts_prev
208 }
209 }
210 }
211
212 /* remove the end of the tune */
213 s = go_global_time(s, cfmt.clip[1])
214 if (!s)
215 return
216
217 /* keep the current sequence */
218 do {
219 s = s.ts_next
220 if (!s)
221 return
222 } while (!s.seqst)
223
224 /* cut the voices */
225 for (v = 0; v < voice_tb.length; v++) {
226 p_voice = voice_tb[v]
227 for (s2 = s.ts_prev; s2; s2 = s2.ts_prev) {
228 if (s2.v == v) {
229 delete s2.next
230 break
231 }
232 }
233 if (!s2)
234 p_voice.sym = null
235 }
236 delete s.ts_prev.ts_next
237
238 chkptr(s.ts_prev) // check the pointers (ties and slurs)
239 }, // do_clip()
240
241 do_pscom: function (of, text) {
242 if (text.slice(0, 5) == "clip ")
243 abc2svg.clip.get_clip.call(this, text)
244 else
245 of(text)
246 },
247
248 set_bar_num: function(of) {
249 of()
250 if (this.cfmt().clip)
251 abc2svg.clip.do_clip.call(this)
252 },
253
254 set_hooks: function(abc) {
255 abc.do_pscom = abc2svg.clip.do_pscom.bind(abc, abc.do_pscom);
256 abc.set_bar_num = abc2svg.clip.set_bar_num.bind(abc, abc.set_bar_num)
257 }
258 } // clip
259
260 if (!abc2svg.mhooks)
261 abc2svg.mhooks = {}
262 abc2svg.mhooks.clip = abc2svg.clip.set_hooks