Debian Codegraph @

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

raw symbols SCIP
1 // abc2svg - chordnames.js - change the names of the chord symbols
2 //
3 // Copyright (C) 2020-2023 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 by %%chordnames.
21 //
22 // Parameters
23 // 1st syntax:
24 // %%chordnames <comma separated list of chord names>
25 // Each name replaces one chord. The order is:
26 // CDEFGAB<N.C.>
27 // 2nd syntax:
28 // %%chordnames <comma separated list of key ':' value>
29 // The key may be a chord letter ('A') with/or an accidental
30 // Example:
31 // %%chordnames Bb:B,B:H,b:s % German chords
32
33 if (typeof abc2svg == "undefined")
34 var abc2svg = {}
35
36 abc2svg.chordnames = {
37
38 gch_build: function(of, s) {
39 var gch, ix, t,
40 cfmt = this.cfmt()
41
42 if (s.a_gch && cfmt.chordnames) {
43 for (ix = 0; ix < s.a_gch.length; ix++) {
44 gch = s.a_gch[ix]
45 t = gch.text
46 if (gch.type != 'g' || !t)
47 continue
48 if (t[0] == 'n' || t[0] == 'N')
49 t = 'N'
50 gch.text = t.replace(cfmt.chordnames.re,
51 function(c){return cfmt.chordnames.o[c]})
52 }
53 }
54 of(s)
55 }, // gch_build()
56
57 gimpl: 'CDEFGABN',
58 set_fmt: function(of, cmd, parm) {
59 var i, v,
60 re = [],
61 o = {},
62 cfmt = this.cfmt()
63
64 if (cmd == "chordnames") {
65 parm = parm.split(',')
66 if (parm[0].indexOf(':') > 0) { // by object
67 for (i = 0; i < parm.length; i++) {
68 v = parm[i].split(':')
69 if (!v[1]) // (no ':')
70 continue
71 o[v[0]] = v[1]
72 re.push(v[0])
73 }
74 } else { // implicit
75 for (i = 0; i < parm.length; i++) {
76 v = abc2svg.chordnames.gimpl[i]
77 o[v] = parm[i]
78 re.push(v)
79 }
80 }
81 cfmt.chordnames = {re: new RegExp(re.join('|'), 'g'), o: o}
82 return
83 }
84 of(cmd, parm)
85 }, // set_fmt()
86
87 set_hooks: function(abc) {
88 abc.gch_build = abc2svg.chordnames.gch_build.bind(abc, abc.gch_build)
89 abc.set_format = abc2svg.chordnames.set_fmt.bind(abc, abc.set_format)
90 } // set_hooks()
91 } // chordnames
92
93 if (!abc2svg.mhooks)
94 abc2svg.mhooks = {}
95 abc2svg.mhooks.chordnames = abc2svg.chordnames.set_hooks