Debian Codegraph @

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

raw symbols SCIP
1 // abc2svg - nns.js - module to output chords in the NNS
2 // (Nashville Notation System)
3 //
4 // Copyright (C) 2021-2023 Jean-Francois Moine
5 //
6 // This file is part of abc2svg.
7 //
8 // abc2svg is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // abc2svg is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
20 //
21 // This module is loaded when "%%nns" appears in a ABC source.
22 //
23 // Parameters
24 // %%nns [n] [include=<list>] [nomusic] [repbrk] [roman= 1 | 2]
25 // <n> = number of chords per line (1: auto)
26 // > 0: above the tune, < 0: under the tune
27 // <list> = comma separated list of (continuous) measure numbers
28 // 'nomusic' displays only the chord
29 // 'repbrk' starts a new grid line on start/stop repeat
30 // 'roman' display the chord in the Roman Numeral Notation
31 // =1: uppercase letters with 'm' for minor chords (default)
32 // =2: lowercase letters for minor chords
33 // %%nnsfont font_name size (default: 'monospace 16')
34
35 if (typeof abc2svg == "undefined")
36 var abc2svg = {}
37
38 abc2svg.nns = {
39 note_nm: "CDEFGAB",
40
41 // Nashville
42 nns_nm: ["1", "♯1", "2", "♭3", "3", "4", "♯4",
43 "5", "♯5", "6", "♭7", "7"],
44 // Roman
45 rnn_nm: ["I", "♯I", "II", "♭III", "III", "IV", "♯IV",
46 "V", "♯V", "VI", "♭VII", "VII"],
47 rnn_nm_m: ["III", "♯III", "IV", "♭V", "V", "VI", "♯VI",
48 "VII", "♯VII", "I", "♭II", "II", "III"],
49 // Roman 2
50 rnn2_nm: ["I", "♯I", "ii", "♭III", "iii", "IV", "♯IV",
51 "V", "♯V", "vi", "♭VII", "vii"],
52 rnn2_nm_m: ["III", "♯III", "iv", "♭V", "v", "VI", "♯VI",
53 "VII", "♯VII", "i", "♭II", "ii", "III"],
54 // inversions: 1st: (upper)6, 2nd: (upper)6 (lower) 4
55
56 // generate the grid
57 block_gen: function(of, s) {
58 if (s.subtype != "nns") {
59 of(s)
60 return
61 }
62
63 var abc = this,
64 img,
65 cfmt = abc.cfmt(),
66 nns = cfmt.nns
67
68 // generate the chord list
69 function build_nns(s, font) {
70 var i, k, l, nr, bar, w, hr, x, y, row,
71 chords = s.chords,
72 bars = s.bars,
73 parts = s.parts || [],
74 cells = [],
75 nc = nns.n
76
77 // set the chord(s) in each measure
78 function set_chords() {
79 var i, ch,
80 pch = '-'
81
82 for (i = 0; i < chords.length; i++) {
83 ch = chords[i]
84 if (!ch[0])
85 ch[0] = pch
86 switch (ch.length) {
87 case 0:
88 continue
89 case 1:
90 pch = ch[0]
91 continue
92 case 2:
93 pch = ch[1]
94 continue
95 case 3:
96 pch = ch[2]
97 continue
98 }
99 pch = ch[3]
100 }
101 } // set_chords()
102
103 // get the original length of chord symbol
104 function get_l(p) {
105 var i,
106 l = 0
107
108 for (i = 0; i < p.length; i++) {
109 if (p[i] == '<') {
110 while (p[i] != '>')
111 i++
112 continue
113 }
114 l++
115 }
116 return l
117 } // get_l()
118
119 function build_cell(c) {
120 var i, j, k,
121 u = '',
122 t = ''
123
124 row += ' '
125 if (c.length == 1) {
126 row += c[0] // one chord in the measure
127 return
128 }
129 i = 0
130 while (i < 4) {
131 t += c[i]
132 k = get_l(c[i])
133 j = k
134 while (--j >= 0)
135 u += ' '
136 if (i < 3 && !c[i + 1]) {
137 while (i < 3 && !c[i + 1])
138 i++
139 i++
140 if (i < 4 && c[i])
141 t += ' '
142 } else {
143 j = k
144 t += '<tspan dx="-'
145 + (j * .6).toFixed(1)
146 + 'em" dy=".2em">'
147 while (--j >= 0)
148 t += '_'
149 t += '</tspan>'
150 i++
151 if (i < 4 && c[i])
152 t += '<tspan dy="-.2em"> </tspan>'
153 else
154 t += '<tspan dx="-.6em" dy="-.2em"> </tspan>'
155 }
156 }
157 row += t + '<tspan dx="-'
158 + (.6 * u.length).toFixed(1)
159 + 'em" text-decoration="underline">'
160 + u
161 + '</tspan>'
162 } // build_cell()
163
164 // ------- build_nns() -------
165
166 // set some chords in each measure
167 set_chords()
168
169 // build the content of the measures
170 if (!nns.ls) {
171 cells = chords
172 } else { // with list of mesure numbers
173 bar = bars
174 bars = [ ]
175 for (i = 0; i < nns.ls.length; i++) {
176 l = nns.ls[i]
177 if (l.indexOf('-') < 0)
178 l = [l, l]
179 else
180 l = l.split('-')
181 for (k = l[0] - 1; k < l[1]; k++) {
182 if (!chords[k]) // error
183 break
184 cells.push(chords[k])
185 bars.push(bar[k])
186 }
187 }
188 bars.push(bar[k]) // ending bar
189 }
190
191 // get the number of columns
192 if (nc < 0)
193 nc = -nc
194 if (nc < 3) { // auto
195 nc = cells.length % 6 == 0 ? 6 : 8
196 if (nc == 8 && cells.length < 12)
197 nc = 4
198 }
199 if (nc > cells.length)
200 nc = cells.length
201
202 // generate the rows
203 abc.set_font('nns')
204 x = img.lm + 30
205 y = -1 + font.size * .6
206 nr = 0
207 hr = font.size * 2
208 for (i = 0; i < cells.length; i++) {
209 if (i == 0
210 || (nns.repbrk
211 && (bars[i].slice(-1) == ':' || bars[i][0] == ':'))
212 || parts[i]
213 || k >= nc) {
214 if (row) {
215 abc.out_svg('<text class="'
216 + abc.font_class(font)
217 + '" x="')
218 abc.out_sxsy(x, '" y="', y)
219 abc.out_svg('">' + row + '</text>\n')
220 }
221 row = ''
222 y -= hr // new row
223 k = 0
224 nr++
225 if (parts[i]) {
226 w = font.size * parts[i].length * .6 + 10
227 if (w < 50)
228 w = 50
229 abc.out_svg('<text class="'
230 + abc.font_class(font)
231 + ' box'
232 + '" x="')
233 abc.out_sxsy(x - w, '" y="', y)
234 abc.out_svg('">' + parts[i] + '</text>\n')
235 }
236 }
237 k++
238 if (bars[i].slice(-1) == ':')
239 row += ' |:'
240 build_cell(cells[i])
241 if (bars[i + 1][0] == ':')
242 row += ' :|'
243 }
244 if (row) {
245 abc.out_svg('<text class="'
246 + abc.font_class(font)
247 + '" x="')
248 abc.out_sxsy(x, '" y="', y)
249 abc.out_svg('">' + row + '</text>\n')
250 }
251
252 abc.vskip(hr * nr + 6)
253 } // build_nns()
254
255 // ----- block_gen() -----
256 var p_voice, n, font, f2
257
258 abc.set_page()
259 img = abc.get_img()
260
261 // set the text style
262 if (!cfmt.nnsfont)
263 abc.param_set_font("nnsfont", "monospace 16")
264 font = abc.get_font('nns')
265
266 // create the chord list
267 abc.blk_flush()
268 build_nns(s, font)
269 abc.blk_flush()
270 }, // block_gen()
271
272 // hook before the generation
273 set_stems: function(of) {
274 var C, tsfirst, voice_tb, fmt, p_v, s, s2,
275 abc = this,
276 nns = abc.cfmt().nns
277
278 function get_beat(s) {
279 var beat = C.BLEN / 4
280
281 if (!s.a_meter[0] || s.a_meter[0].top[0] == 'C'
282 || !s.a_meter[0].bot)
283 return beat
284 beat = C.BLEN / s.a_meter[0].bot[0] |0
285 if (s.a_meter[0].bot[0] == 8
286 && s.a_meter[0].top[0] % 3 == 0)
287 beat = C.BLEN / 8 * 3
288 return beat
289 } // get_beat()
290
291 // transpose the chord back to "C"
292 function set_nm(p, tr, mode) {
293 var i, o, o2, a, n,
294 csa = []
295
296 i = p.indexOf('/') // get the bass if any
297 if (i > 0) {
298 while (1) {
299 if (p[i -1] != '<')
300 break
301 i = p.indexOf('/', i + 1)
302 if (i < 0)
303 break
304 }
305 }
306 if (i < 0) {
307 csa.push(p)
308 } else {
309 csa.push(p.slice(0, i))
310 csa.push(p.slice(i + 1))
311 }
312
313 for (i = 0; i < csa.length; i++) { // main and optional bass
314 p = csa[i]
315
316 o = p.search(/[A-G]/) // get the base chord
317 if (o < 0)
318 continue // not a chord!
319
320 a = 0
321 o2 = o + 1
322 if (p[o2] == '#' || p[o2] == '♯') {
323 a++
324 o2++
325 } else if (p[o2] == 'b' || p[o2] == '♭') {
326 a--
327 o2++
328 }
329 n = ([0, 2, 4, 5, 7, 9, 11]
330 [abc2svg.nns.note_nm.indexOf(p[o])]
331 + a
332 + tr) % 12
333 if (!nns.roman) {
334 n = abc2svg.nns.nns_nm[n] // major and minor
335 } else if (nns.roman == 1) {
336 if (!mode)
337 n = abc2svg.nns.rnn_nm[n]
338 else
339 n = abc2svg.nns.rnn_nm_m[n]
340 } else {
341 if (!mode)
342 n = abc2svg.nns.rnn2_nm[n]
343 else
344 n = abc2svg.nns.rnn2_nm_m[n]
345 if (p[o2] == 'm')
346 o2++
347 }
348 csa[i] = p.slice(0, o)
349 + n
350 + p.slice(o2)
351 }
352 return csa.join('/')
353 } // set_nm()
354
355 // build the arrays of chords and bars
356 function build_chords(sb) { // block 'nns'
357 var s, i, w, bt, rep,
358 bars = [],
359 chords = [],
360 parts = [],
361 chord = [],
362 beat = get_beat(voice_tb[0].meter),
363 wm = voice_tb[0].meter.wmeasure,
364 cur_beat = 0,
365 beat_i = 0,
366 tr = (tsfirst.p_v.key.k_sf + 12) * 5, // transposition to "C"
367 mode = tsfirst.p_v.key.k_mode
368
369 // scan all the symbols
370 bars.push('|')
371 for (s = tsfirst; s; s = s.ts_next) {
372 while (s.time > cur_beat) {
373 if (beat_i < 3) // only 2, 3 or 4 beats / measure...
374 beat_i++
375 cur_beat += beat
376 }
377 if (s.part)
378 parts[chords.length] = s.part.text
379 switch (s.type) {
380 case C.KEY:
381 tr = (s.k_sf + 12) * 5
382 mode = s.k_mode
383 break
384 case C.NOTE:
385 case C.REST:
386 if (!s.a_gch)
387 break
388
389 // search a chord symbol
390 for (i = 0; i < s.a_gch.length; i++) {
391 if (s.a_gch[i].type == 'g') {
392 if (!chord[beat_i]) {
393 chord[beat_i] =
394 set_nm(s.a_gch[i].text,
395 tr, mode)
396 }
397 break
398 }
399 }
400 break
401 case C.BAR:
402 bt = s.bar_type
403 if (s.time < wm) { // if anacrusis
404 if (chord.length) {
405 chords.push(chord)
406 bars.push(bt)
407 } else {
408 bars[0] = bt
409 }
410 } else {
411 if (!s.bar_num) // if not normal measure bar
412 break
413 chords.push(chord)
414 bars.push(bt)
415 }
416 chord = []
417 cur_beat = s.time // synchronize in case of error
418 beat_i = 0
419 if (bt.indexOf(':') >= 0)
420 rep = true // some repeat
421 while (s.ts_next && s.ts_next.type == C.BAR)
422 s = s.ts_next
423 break
424 case C.METER:
425 beat = get_beat(s)
426 wm = s.wmeasure
427 break
428 }
429 }
430
431 if (chord.length) {
432 bars.push('')
433 chords.push(chord)
434 }
435 if (!chords.length)
436 return // no chord in this tune
437
438 sb.chords = chords
439 sb.bars = bars
440 if (parts.length)
441 sb.parts = parts
442 } // build_chords
443
444 // -------- set_stems --------
445
446 // create a specific block
447 if (nns) {
448 C = abc2svg.C
449 tsfirst = this.get_tsfirst()
450 fmt = tsfirst.fmt
451 voice_tb = this.get_voice_tb()
452 p_v = voice_tb[this.get_top_v()]
453 s = {
454 type: C.BLOCK,
455 subtype: 'nns',
456 dur: 0,
457 time: 0,
458 p_v: p_v,
459 v: p_v.v,
460 st: p_v.st
461 }
462
463 build_chords(s) // build the array of the chords
464
465 // and insert it in the tune
466 if (!s.chords) { // if no chord
467 ;
468 } else if (nns.nomusic) { // if just the chords
469 this.set_tsfirst(s)
470 } else if (nns.n < 0) { // below
471 for (s2 = tsfirst; s2.ts_next; s2 = s2.ts_next)
472 ;
473 s.time = s2.time
474 s.prev = p_v.last_sym.prev // before the last symbol
475 s.prev.next = s
476 s.next = p_v.last_sym
477 p_v.last_sym.prev = s
478
479 s.ts_prev = s2.ts_prev
480 s.ts_prev.ts_next = s
481 s.ts_next = s2
482 s2.ts_prev = s
483 if (s2.seqst) {
484 s.seqst = true
485 s2.seqst = false
486 }
487 } else { // above
488 s.next = p_v.sym
489 s.ts_next = tsfirst
490 tsfirst.ts_prev = s
491 this.set_tsfirst(s)
492 p_v.sym.prev = s
493 p_v.sym = s
494 }
495 s.fmt = s.prev ? s.prev.fmt : fmt
496 }
497 of()
498 }, // set_stems()
499
500 set_fmt: function(of, cmd, parm) {
501 if (cmd == "nns") {
502 if (!parm)
503 parm = "1"
504 parm = parm.split(/\s+/)
505 var nns = {
506 n: Number(parm.shift())
507 }
508 if (isNaN(nns.n)) {
509 if (parm.length) {
510 this.syntax(1, this.errs.bad_val, "%%nns")
511 return
512 }
513 nns.n = 1
514 }
515 while (parm.length) {
516 var item = parm.shift()
517 if (item == "nomusic")
518 nns.nomusic = true
519 else if (item == "roman")
520 nns.roman = 1
521 else if (item == "repbrk")
522 nns.repbrk = true
523 else if (item.slice(0, 8) == "include=")
524 nns.ls = item.slice(8).split(',')
525 else if (item.slice(0, -1) == "roman=")
526 nns.roman = item.slice(-1) == "1" ? 1 : 2
527 }
528 this.cfmt().nns = nns
529 return
530 }
531 of(cmd, parm)
532 },
533
534 set_hooks: function(abc) {
535 abc.block_gen = abc2svg.nns.block_gen.bind(abc, abc.block_gen)
536 abc.set_stems = abc2svg.nns.set_stems.bind(abc, abc.set_stems)
537 abc.set_format = abc2svg.nns.set_fmt.bind(abc, abc.set_format)
538 }
539 } // nns
540
541 if (!abc2svg.mhooks)
542 abc2svg.mhooks = {}
543 abc2svg.mhooks.nns = abc2svg.nns.set_hooks