Debian Codegraph @

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

raw symbols SCIP
1 // abc2svg - jazzchord.js - Adds jazz chord styling to 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 // Code adapted from Chris Fargen.
21 // https://gist.github.com/chrisfargen/4324c6cf6fed2c8f9a6eae1680e53169
22 //
23 // This module is loaded by %%jazzchord.
24 //
25 // Parameters
26 // %%jazzchord [ string '=' replacement-string ]*
27
28 if (typeof abc2svg == "undefined")
29 var abc2svg = {}
30
31 abc2svg.jazzchord = {
32
33 // default replacements
34 defrep: {
35 "-": "–",
36 "°": "o",
37 "º": "o",
38 "ᵒ": "o",
39 "0": "ø",
40 // "6/9": "⁶⁄₉",
41 // "maj": "Δ",
42 // "M": "Δ",
43 // "min": "–",
44 // "m": "–",
45 "^": "∆"
46 },
47
48 gch_build: function(of, s) {
49 var gch, ix, r, t,
50 fmt = s.fmt
51
52 if (!fmt.jazzchord) {
53 of(s)
54 return
55 }
56
57 // jazzify a chord
58 function jzch(t) {
59 var r = '',
60 a = t.match(/(\[?[A-G])([#♯b♭]?)([^/]*)\/?(.*)\)?/)
61 // a[1]=note, a[2]=acc, a[3]=quality, a[4]=bass
62
63 if (!a)
64 return t
65 if (a[2])
66 r = "$7" + a[2]
67 if (a[3][0] == 'm' // if minor
68 && a[3].slice(0, 3) != "maj") {
69 if (!r)
70 r += "$7"
71 if (a[3].slice(0, 3) == "min") {
72 r += a[3].slice(0, 3)
73 a[3] = a[3].slice(3)
74 } else { // assume 'm'
75 r += 'm'
76 a[3] = a[3].slice(1)
77 }
78 }
79 if (a[3])
80 r += (r ? "$0" : '') + "$8" + a[3]
81 if (a[4])
82 r += (r ? "$0" : '') + "$9/" + a[4]
83 if (!r)
84 return t
85 return a[1] + r + "$0"
86 } // jzch()
87
88 for (ix = 0; ix < s.a_gch.length; ix++) {
89 gch = s.a_gch[ix]
90 t = gch.text
91 if (gch.type != 'g'
92 || t.indexOf('$') >= 0) // if some formatting already
93 continue
94 switch (t) {
95 case "/": gch.text = "\ue101"; continue
96 case "%": gch.text = "\ue500"; continue
97 case "%%": gch.text = "\ue501"; continue
98 }
99
100 if (fmt.jzreg) { // if replacement list
101 t = t.replace(fmt.jzRE,
102 function(x) {
103 return fmt.jzrep[x]
104 })
105 }
106
107 if (fmt.jazzchord == 1) {
108 if (t[0] == '(')
109 t = t.slice(1, -1)
110 t = t.split('(') // possible "ch1(ch2)"
111 r = jzch(t[0])
112 if (t.length > 1)
113 r += '(' + jzch(t[1])
114 } else {
115 r = t
116 }
117 if (gch.text[0] == '(')
118 gch.text = '(' + r + ')'
119 else
120 gch.text = r
121 }
122 of(s) // build the chord symbols
123 }, // gch_build()
124
125 set_fmt: function(of, cmd, parm) {
126 var i, k, s,
127 cfmt = this.cfmt()
128
129 if (cmd == "jazzchord") {
130 cfmt.jazzchord = this.get_bool(parm)
131 if (!cfmt.jazzchord)
132 return
133 if (parm[0] == '2')
134 cfmt.jazzchord = 2 // no style
135
136 if(!cfmt.jzreg) { // if new definition
137 // cfmt.jzreg = "-|°|º|ᵒ|0|6/9|maj|M|min|m|\\^"
138 cfmt.jzreg = "-|°|º|ᵒ|0|\\^"
139 cfmt.jzrep = Object.create(abc2svg.jazzchord.defrep)
140 cfmt.jzRE = new RegExp(cfmt.jzreg, 'g')
141 }
142 if (parm && parm.indexOf('=') > 0) {
143 parm = parm.split(/[\s]+/)
144 for (cmd = 0; cmd < parm.length; cmd++) {
145 k = parm[cmd].split('=')
146 if (k.length != 2)
147 //fixme: error
148 continue
149 s = k[1] // replacement
150 k = k[0] // key
151 i = cfmt.jzreg.indexOf(k)
152 if (i >= 0) { // if old key
153 if (s) { // new value
154 cfmt.jzrep[k] = s
155 } else {
156 cfmt.jzreg = cfmt.jzreg.replace(k, '')
157 cfmt.jzreg = cfmt.jzreg.replace('||', '|')
158 delete cfmt.jzrep[k]
159 }
160 } else {
161 cfmt.jzreg += '|' + k
162 cfmt.jzrep[k] = s
163 }
164 cfmt.jzRE = new RegExp(cfmt.jzreg, 'g')
165 }
166 }
167 return
168 }
169 of(cmd, parm)
170 }, // set_fmt()
171
172 set_hooks: function(abc) {
173 abc.gch_build = abc2svg.jazzchord.gch_build.bind(abc, abc.gch_build)
174 abc.set_format = abc2svg.jazzchord.set_fmt.bind(abc, abc.set_format)
175
176 abc.add_style("\
177 \n.jc7{font-size:90%}\
178 \n.jc8{baseline-shift:25%;font-size:75%;letter-spacing:-0.05em}\
179 \n.jc9{font-size:75%;letter-spacing:-0.05em}\
180 ")
181 abc.param_set_font("setfont-7", "* * class=jc7")
182 abc.param_set_font("setfont-8", "* * class=jc8")
183 abc.param_set_font("setfont-9", "* * class=jc9")
184 } // set_hooks()
185 } // jazzchord
186
187 if (!abc2svg.mhooks)
188 abc2svg.mhooks = {}
189 abc2svg.mhooks.jazzchord = abc2svg.jazzchord.set_hooks