Debian Codegraph @

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

raw symbols SCIP
1 // grid3.js - module to insert a manual chords
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 "%%begingrid" appears in a ABC source.
21 //
22 // Parameters
23 // %%begingrid [ chord-define [ noprint ] ] [ lm=<left margin> ] [ width=<width> ]
24 // list of chords, '-' or '.', measure bars ('|') and ':' for repeat
25 // %%endgrid
26 //
27 // %%gridfont font_name size (default: 'serif 16')
28 //
29 // When this command appears inside a tune and when 'chord-define' is present,
30 // the chords are used to define the chord symbols that are displayed
31 // above the staff system.
32 // When 'noprint' is also present, the grid itself is not displayed.
33 //
34 // The left margin may be '-1' to center the grid (default).
35
36 if (typeof abc2svg == "undefined")
37 var abc2svg = {}
38
39 abc2svg.grid3 = {
40
41 // generate the grid
42 block_gen: function(of, s) {
43 if (s.subtype != "grid3") {
44 of(s)
45 return
46 }
47
48 this.set_page()
49 this.blk_flush()
50
51 var abc = this,
52 cfmt = abc.cfmt(),
53 img = abc.get_img(),
54 posy = abc.get_posy(),
55 txt = s.text,
56 font, font_cl, cls, w,
57 ln, i,
58 lines = [],
59 cl = [],
60 bars = [],
61 cells = [],
62 nr = 0, // number of rows
63 nc = 0, // number of columns
64 wc = 0 // width of a cell
65
66 // generate the grid
67 function build_grid() {
68 var i, k, l, line, bl, bar, w, hr, x0, x, y, yl, cl, cell,
69 lc = '',
70 path = '<path class="stroke" stroke-width="1" d="M',
71 sf = '" style="font-size:' + (font.size * .72).toFixed(1) + 'px'
72 // small font
73
74 function build_ch(cl, x, y, n) {
75 return '<text class="' + cl + '" x="' +
76 x.toFixed(1) + '" y="' + y.toFixed(1) + '">' +
77 cell[n] + '</text>\n'
78 } // build_ch()
79
80 function build_cell(cell, x, y, yl, hr) {
81 var line
82
83 if (cell.length > 1) {
84 line = path +
85 (x - wc / 2).toFixed(1) + ' ' +
86 yl.toFixed(1) + 'l' +
87 wc.toFixed(1) + ' -' + hr.toFixed(1) +
88 '"/>\n'
89 if (cell[1]) {
90 line += path +
91 (x - wc / 2).toFixed(1) + ' ' +
92 (yl - hr).toFixed(1) + 'l' +
93 (wc / 2).toFixed(1) + ' ' + (hr / 2).toFixed(1) +
94 '"/>\n' +
95 build_ch(cls + sf, x - wc / 3, y, 0) +
96 build_ch(cls + sf, x, y - hr * .32, 1)
97 } else {
98 line += build_ch(cls + sf,
99 x - wc * .2, y - hr / 4, 0)
100 }
101 if (cell.length >= 3) {
102 if (cell[3]) {
103 line += path +
104 x.toFixed(1) + ' ' +
105 (yl - hr / 2).toFixed(1) + 'l' +
106 (wc / 2).toFixed(1) + ' ' + (hr / 2).toFixed(1) +
107 '"/>\n' +
108 build_ch(cls + sf, x, y + hr * .3, 2) +
109 build_ch(cls + sf, x + wc / 3, y, 3)
110 } else {
111 line += build_ch(cls + sf,
112 x + wc * .2, y + hr / 4, 2)
113 }
114 }
115 } else {
116 line = build_ch(cls, x, y, 0)
117 }
118 return line
119 } // build_cell()
120
121 // -- build_grid() --
122
123 // build the content of the cells
124 hr = font.size * 2.1
125 if (wc < hr * 1.4)
126 wc = hr * 1.4 // cell width
127 if (s.width) {
128 w = s.width
129 wc = w / nc
130 } else {
131 w = wc * nc // grid width
132 }
133 x0 = s.lm < 0
134 ? (img.width / cfmt.scale - w) / 2 // center
135 : s.lm + 1
136 if ((s.fmt || cfmt).trimsvg) // adjust the SVG width
137 img.wx = x0 + w - img.lm - img.rm
138
139 // generate the cells
140 yl = posy + 3
141 y = posy + 3 - font.size * .7
142 while (1) {
143 cl = cells.shift()
144 if (!cl)
145 break
146 y += hr
147 yl += hr
148 x = x0 + wc / 2
149 while (1) {
150 cell = cl.shift()
151 if (!cell)
152 break
153 lc += build_cell(cell, x, y, yl, hr)
154 x += wc
155 }
156 }
157
158 // draw the lines
159 line = '<path class="stroke" d="\n'
160 y = posy + 3
161 for (i = 0; i <= nr; i++) {
162 line += 'M' + x0.toFixed(1) + ' ' + y.toFixed(1) +
163 'h' + w.toFixed(1)+ '\n'
164 y += hr
165 }
166 x = x0
167 y = posy + 3
168 for (i = 0; i <= nc; i++) {
169 line += 'M' + x.toFixed(1) + ' ' + y.toFixed(1) +
170 'v' + (hr * nr).toFixed(1) + '\n'
171 x += wc
172 }
173 line += '"/>\n'
174
175 // insert the cells
176 line += lc
177
178 // show the repeat signs
179 y = posy + 3 - font.size * .7
180 while (1) {
181 bl = bars.shift()
182 if (!bl)
183 break
184 x = x0
185 y += hr
186 while (1) {
187 bar = bl.shift()
188 if (!bar)
189 break
190 if (bar[0] == ':')
191 line += '<text class="' + cls + '" x="' +
192 (x - 5).toFixed(1) +
193 '" y="' + y.toFixed(1) +
194 '" style="font-weight:bold;font-size:' +
195 (font.size * 1.6).toFixed(1) +
196 'px">:</text>\n'
197 if (bar.slice(-1) == ':')
198 line += '<text class="' + cls + '" x="' +
199 (x + 5).toFixed(1) +
200 '" y="' + y.toFixed(1) +
201 '" style="font-weight:bold;font-size:' +
202 (font.size * 1.6).toFixed(1) +
203 'px">:</text>\n'
204 x += wc
205 }
206 }
207 abc.out_svg(line)
208 abc.vskip(hr * nr + 6)
209 } // build_grid()
210
211 // ----- block_gen ----
212
213 // set the text style
214 if (!cfmt.gridfont)
215 abc.param_set_font("gridfont", "serif 16")
216 font = abc.get_font('grid')
217 font_cl = abc.font_class(font)
218 cls = font_cl + " mid"
219 abc.add_style("\n.mid {text-anchor:middle}")
220 abc.set_font('grid') // (for strwh())
221
222 // scan the grid content
223 txt = txt.split('\n')
224 while (1) {
225 ln = txt.shift() // line
226 if (!ln)
227 break
228
229 // extract the bars and the chords
230 ln = ln.match(/[|:]+|[^|:\s]+/g)
231 bars[nr] = []
232 cells[nr] = []
233 i = -1
234 while (1) {
235 cl = ln.shift()
236 if (!cl)
237 break
238 if (cl.match(/[:|]+/)) {
239 bars[nr][++i] = cl
240 cells[nr][i] = []
241 } else {
242 if (!cells[nr][i]) { // if starting '|' is missing
243 bars[nr][++i] = '|'
244 cells[nr][i] = []
245 }
246 if (cl == '.' || cl == '-')
247 cl = ''
248 cells[nr][i].push(cl)
249 }
250 }
251 if (cells[nr][i].length)
252 bars[nr][++i] = '|' // missing ending bar
253 else
254 cells[nr][i] = null // keep just the measure bar
255
256 if (i > nc)
257 nc = i
258
259 i = 0
260 while (1) {
261 cl = cells[nr][i++]
262 if (!cl)
263 break
264 if (cl.length == 2) {
265 cl[2] = cl[1] // "| A B |" => "|A - B|"
266 cl[1] = ''
267 }
268 w = abc.strwh(cl.join(''))[0]
269 if (w > wc)
270 wc = w
271 }
272 nr++
273 }
274 wc += abc.strwh(' ')[0]
275
276 // build the grid and insert it in the music
277 build_grid()
278 abc.blk_flush()
279 }, // block_gen()
280
281 // handle %%begingrid
282 do_begin_end: function(of, type, opt, txt) {
283 var i, s, v,
284 lm = -1, // left margin - default: center the grid
285 width // grid width - default: computed
286
287 if (type != "grid") {
288 of(type, opt, txt)
289 return
290 }
291
292 // replace the accidentals
293 txt = txt.replace(/#|=|b/g,
294 function(x) {
295 switch (x) {
296 case '#': return "\u266f"
297 case '=': return "\u266e"
298 // case 'b': return "\u266d"
299 }
300 return "\u266d"
301 })
302
303 opt = opt.trim().split(/\s+/)
304 while (1) {
305 i = opt.shift()
306 if (!i)
307 break
308 switch (i[0]) {
309 case 'c': // chord-define
310 this.cfmt().csdef = txt
311 break
312 case 'n': // noprint
313 type = ""
314 break
315 case 'l': // lm=..
316 case 'w': // width=..
317 v = i.match(/-?[\d.]+.?.?/)
318 if (v) {
319 v = this.get_unit(v[0])
320 if (!isNaN(v)) {
321 if (i[0] == 'l')
322 lm = v
323 else
324 width = v
325 }
326 }
327 break
328 }
329 }
330
331 if (type) {
332 type += "3"
333 if (this.get_parse().state >= 2) {
334 s = this.new_block(type)
335 s.text = txt
336 s.lm = lm
337 s.width = width
338 } else {
339 abc2svg.grid3.block_gen.call(this, null, {
340 subtype: type,
341 text: txt,
342 lm: lm,
343 width: width
344 })
345 }
346 }
347 }, // do_begin_end()
348
349 output_music: function(of) {
350 var ln, i, dt, ss, ntim, p_vc, s3, cl,
351 C = abc2svg.C,
352 abc = this,
353 s = abc.get_tsfirst(),
354 vt = abc.get_voice_tb(),
355 t = abc.cfmt().csdef,
356 cs = []
357
358 // add a chord symbol
359 function add_cs(ss, ch) {
360 var s = { // invisible rest in the voice "grid3"
361 type: C.REST,
362 fname: ss.fname,
363 istart: ss.istart,
364 iend: ss.iend,
365 v: p_vc.v,
366 p_v: p_vc,
367 time: ntim,
368 st: 0,
369 fmt: ss.fmt,
370 dur: 0,
371 dur_orig: 0,
372 invis: true,
373 seqst: true,
374 nhd: 0,
375 notes: [{
376 pit: 18,
377 dur: 0
378 }]
379 }
380
381 if (ch != '.' && ch != '-') {
382 abc.set_a_gch(s, [{ // define the chord symbol
383 type: 'g',
384 text: ch,
385 otext: ch,
386 istart: ss.istart,
387 iend: ss.iend,
388 font: abc.get_font("gchord"),
389 pos: p_vc.pos.gch || C.SL_ABOVE
390 }])
391 }
392
393 // insert the rest
394 if (!p_vc.last_sym) {
395 p_vc.sym = s
396 } else {
397 s.prev = p_vc.last_sym
398 s.prev.next = s
399 }
400 p_vc.last_sym = s
401 s.ts_next = ss
402 s.ts_prev = ss.ts_prev
403 s.ts_prev.ts_next = s
404 ss.ts_prev = s
405 if (s.time == ss.time)
406 delete ss.seqst
407 return s
408 } // add_cs()
409
410 if (t) { // if chord-define
411 p_vc = {
412 id: "grid3",
413 v: vt.length,
414 time: 0,
415 pos: {
416 gst: 0
417 },
418 scale: 1,
419 st: 0,
420 second: true,
421 sls: []
422 }
423 vt.push(p_vc) // add the voice supporting the chord symbols
424
425 t = t.split('\n')
426 while (1) { // scan the grid content
427 ln = t.shift() // line
428 if (!ln)
429 break
430 ln = ln.trimLeft()
431 if (ln[0] == '|')
432 ln = ln.slice(ln[1] == ':' ? 2 : 1)
433 if (ln[ln.length - 1] != '|')
434 ln = ln + '|'
435
436 // extract the bars and the chords
437 ln = ln.match(/[|:]+|[^|:\s]+/g)
438 while (1) {
439 cl = ln.shift()
440 if (!cl)
441 break
442 if (cl[0] == '|' || cl[0] == ':') {
443 while (s && !s.dur)
444 s = s.ts_next
445 if (!s)
446 break
447 ss = s // first note/rest
448 while (s && !s.bar_type)
449 s = s.ts_next // end of measure
450 if (!cs.length)
451 cs = ['.']
452 ntim = ss.time
453 dt = (s.time - ntim) / cs.length
454 s3 = null
455 for (i = 0; i < cs.length; i++) {
456 if ((cs[i] != '.'
457 && cs[i] != '-')
458 || !s3) {
459 while (ss.time < ntim)
460 ss = ss.ts_next
461 s3 = add_cs(ss, cs[i])
462 }
463 s3.dur += dt
464 s3.dur_orig =
465 s3.notes[0].dur = s3.dur
466 ntim += dt
467 }
468 while (s && s.type != C.BAR)
469 s = s.ts_next
470 ss = {
471 type: C.BAR,
472 bar_type: "|",
473 fname: s.fname,
474 istart: s.istart,
475 iend: s.iend,
476 v: p_vc.v,
477 p_v: p_vc,
478 st: 0,
479 time: s.time,
480 dur: 0,
481 nhd: 0,
482 notes: [{
483 pit: 18
484 }],
485 ts_next: s,
486 prev: p_vc.last_sym,
487 ts_prev: s.ts_prev
488 }
489 if (!s)
490 break
491 ss.fmt = s.fmt
492 if (s.seqst) {
493 ss.seqst = true
494 delete s.seqst
495 }
496 ss.prev.next =
497 ss.ts_prev.ts_next =
498 s.ts_prev = ss
499 p_vc.last_sym = ss
500 cs = []
501 } else {
502 cs.push(cl)
503 }
504 }
505 }
506 }
507 of()
508 }, // output_music()
509
510 set_hooks: function(abc) {
511 abc.block_gen = abc2svg.grid3.block_gen.bind(abc, abc.block_gen)
512 abc.do_begin_end = abc2svg.grid3.do_begin_end.bind(abc, abc.do_begin_end)
513 abc.output_music = abc2svg.grid3.output_music.bind(abc, abc.output_music)
514 }
515 } // grid3
516
517 if (!abc2svg.mhooks)
518 abc2svg.mhooks = {}
519 abc2svg.mhooks.grid3 = abc2svg.grid3.set_hooks