Debian Codegraph @

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

raw symbols SCIP
1 // diag.js - module to insert guitar chord diagrams
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 "%%diagram" or "%%setdiag" appear in a ABC source.
21 //
22 // The command %%diagram draws a chord diagram above the chord symbols.
23 //
24 // Parameters
25 // %%diagram [ <#diagram> | 0 ]
26 // with
27 // <#diagram> identifier of the diagram type (number)
28 // ('1' is for the 6 strings guitar)
29 //
30 // The command %%setdiag defines the chord diagram of a chord symbol
31 // for the current diagram type.
32 //
33 // Parameters
34 // %%setdiag <chord> <dots> <label[,pos]> <fingers> [barre=<num>-<num>]
35 // with
36 // <chord> = chord symbol
37 // <dots> = list of diagram offset of dots on the strings - '0 or 'x': no dot
38 // <label> = text to write on the left of the <pos>th fret
39 // (<pos> default = 1, fret #0 is no text)
40 // <fingers> = finger numbers ou 'x' (mute) or '0'/'y' (no finger)
41 // barre=<num>-<num> draw a bar between the two string numbers in the first fret
42 // (numbering order 654321 for E,A,D,GBe)
43
44 if (typeof abc2svg == "undefined")
45 var abc2svg = {}
46
47 abc2svg.diag = {
48
49 cd4: {},
50 // common diagrams by Guido Gonzato from http://www.guitar-chord.org/ (2021-08-27)
51 cd6: {
52 C: "x32010 ,0 x32010",
53 Cm: "x03320 fr3 x03420 barre=5-1",
54 C7: "x32310 ,0 x32410",
55 Cm7: "003020 fr3 x03020 barre=5-1",
56 CM7: "032000 ,0 x21000",
57 Csus4: "x03340 fr3 x02340 barre=5-1",
58 D: "000232 ,0 x00132",
59 Dm: "000231 ,0 x00231",
60 D7: "000212 ,0 x00312",
61 Dm7: "000211 ,0 xx0211",
62 DM7: "000222 ,0 xx0111",
63 Dsus4: "000233 ,0 xx0134",
64 E: "022100 ,0 023100",
65 Em: "022000 ,0 023000",
66 E7: "020100 ,0 020100",
67 Em7: "022030 ,0 023040",
68 EM7: "021100 ,0 031200",
69 Esus4: "022200 ,0 023400",
70 F: "033200 fr1 034200 barre=6-1",
71 Fm: "033000 fr1 034000 barre=6-1",
72 F7: "030200 fr1 030200 barre=6-1",
73 Fm7: "030000 fr1 030000 barre=6-1",
74 FM7: "xx3210 ,0 xx3210",
75 Fsus4: "033300 fr1 023400 barre=6-1",
76 G: "320003 ,0 230004",
77 Gm: "033000 fr3 034000 barre=6-1",
78 G7: "320001 ,0 320001",
79 Gm7: "030000 fr3 030000 barre=6-1",
80 GM7: "320002 ,0 310002",
81 Gsus4: "033300 fr3 023400 barre=6-1",
82 A: "002220 ,0 002340",
83 Am: "002210 ,0 002310",
84 A7: "002020 ,0 002030",
85 Am7: "002010 ,0 002010",
86 AM7: "002120 ,0 x02130",
87 Asus4: "x02230 ,0 x02340",
88 B: "x03330 fr2 002340 barre=5-1",
89 Bm: "x03320 fr2 003410 barre=5-1",
90 B7: "021202 ,0 x21304",
91 Bm7: "003020 fr2 x03020 barre=5-1",
92 BM7: "003230 fr2 x03240 barre=5-1",
93 Bsus4: "x03340 fr2 x02340 barre=5-1",
94 }, // cd6{}
95
96 // function called before tune generation
97 draw_gchord: function(of, i, s, x, y) {
98 var gch, nm,
99 cfmt = this.cfmt(),
100 n = cfmt.diag, // number of strings
101 glyphs = this.get_glyphs()
102
103 // create the base decorations if not done yet
104 if (!glyphs.ddot) {
105 this.add_style("\
106 \n.fng {font:6px sans-serif}\
107 \n.frn {font:italic 7px sans-serif}")
108 glyphs.ddot = '<circle id="ddot" class="fill" r="1.5"/>'
109 }
110
111 if (!glyphs["fb" + n]) {
112 if (n == 4) {
113 glyphs.fb4 = '<g id="fb4">\n\
114 <path class="stroke" stroke-width="0.4" d="\
115 M-6 -24h12m0 6h-12\
116 m0 6h12m0 6h-12\
117 m0 6h12"/>\n\
118 <path class="stroke" stroke-width="0.5" d="\
119 M-6 -24v24m4 0v-24\
120 m4 0v24m4 0v-24"/>\n\
121 </g>'
122 glyphs.nut4 =
123 '<path id="nut4" class="stroke" stroke-width="1.6" d="\
124 M-6.2 -24.5h12.4"/>'
125 } else {
126 glyphs.fb6 = '<g id="fb6">\n\
127 <path class="stroke" stroke-width="0.4" d="\
128 M-10 -24h20m0 6h-20\
129 m0 6h20m0 6h-20\
130 m0 6h20"/>\n\
131 <path class="stroke" stroke-width="0.5" d="\
132 M-10 -24v24m4 0v-24\
133 m4 0v24m4 0v-24\
134 m4 0v24m4 0v-24"/>\n\
135 </g>'
136 glyphs.nut6 =
137 '<path id="nut6" class="stroke" stroke-width="1.6" d="\
138 M-10.2 -24.5h20.4"/>'
139 }
140 }
141
142 // convert the chord symbol to a "better known" one
143 function ch_cnv(t) {
144 var a = t.match(/[A-G][#♯b♭]?([^/]*)\/?/) // a[1] = chord type
145 if (a && a[1]) {
146 a[2] = abc2svg.ch_alias[a[1]]
147 if (a[2] != undefined)
148 t = t.replace(a[1], a[2])
149 }
150 return t.replace('/', '.')
151 .replace(/\u266f/g,'#')
152 .replace(/\u266d/g,'b')
153 } // ch_cnv()
154
155 // create a glyph of the diagram
156 function diag_add(nm) { // chord name
157 var dc, i, l, x, y,
158 d = abc2svg.diag["cd" + n][nm.slice(1)]
159 // definition of the diagram
160
161 if (!d)
162 return // no diagram of this chord
163 d = d.split(' ')
164 x = 2 - 2 * n
165 dc = '<g id="' + nm + '">\n\
166 <use xlink:href="#fb' + n + '"/>\n'
167 l = d[1].split(',') // label,position
168 if (!l[0] || l[0].slice(-1) == l[1])
169 dc += '<use xlink:href="#nut' + n + '"/>\n'
170 if (l[0])
171 dc += '<text x="' + (x - 10).toString()
172 + '" y="' + ((l[1] || 1) * 6 - 25)
173 + '" class="frn">' + l[0] + '</text>\n'
174 // fingers
175 dc += '<text x="' + (n == 6 ? '-12,-8,-4,0,4,8'
176 : '-8,-4,0,4')
177 + '" y="-26" class="fng">'
178 + d[2].replace(/[y0]/g, ' ')
179 + '</text>\n'
180 // dots
181 for (i = 0; i < n; i++) {
182 l = d[0][i]
183 if (l && l != 'x' && l != '0')
184 dc += '<use x="' + (i * 4 + x)
185 + '" y="' + (l * 6 - 27)
186 + '" xlink:href="#ddot"/>\n'
187 }
188 // barre
189 if (d[3]) {
190 l = d[3].match(/barre=(\d)-(\d)/)
191 if (l)
192 dc += '<path id="barre" class="stroke"\
193 stroke-width="1.4" d="M' + ((n - l[1]) * 4 + x - 2)
194 + ' -21h' + ((l[1] - l[2]) * 4 + 4) + '"/>'
195 }
196 dc += '</g>'
197 glyphs[nm] = dc
198 } // diag_add()
199
200 // --- draw_gchord ---
201 of(i, s, x, y) // draw the chord symbols and the annotations
202
203 if ((s.invis && s.play) // play sequence: no chord nor annotation
204 || !n) // no %%diagram
205 return
206 gch = s.a_gch[i]
207 if (!gch || gch.type != 'g' || gch.capo)
208 return
209 nm = n + ch_cnv(gch.text) // glyph name
210
211 // build a glyph if not done yet
212 if (!glyphs[nm])
213 diag_add(nm)
214
215 // output the diagram above the chord symbol
216 if (glyphs[nm]) {
217 x = s.x
218 y = this.y_get(s.st, 1, x - 10, 20)
219 this.xygl(x, y + 2, nm)
220 this.y_set(s.st, 1, x - 10, 20, y + 34)
221 }
222 }, // draw_gchord()
223
224 set_fmt: function(of, cmd, param) {
225 var a, d, n,
226 cfmt = this.cfmt()
227
228 switch (cmd) {
229 case "diagram":
230 n = param
231 if (n == '0')
232 n = ''
233 else if (n != '4')
234 n = '6'
235 cfmt.diag = n
236 return
237 case "setdiag":
238 n = cfmt.diag
239 if (!n)
240 n = "6" // default = 6 strings guitar
241 a = param.match(/(\S*)\s+(.*)/)
242 if (a && a.length == 3) {
243 d = a[2].split(' ')
244 if (d && d.length >= 3) {
245 abc2svg.diag["cd" + n][a[1].replace('/', '.')]
246 = a[2]
247 return
248 }
249 }
250 this.syntax(1, this.errs.bad_val, "%%setdiag")
251 return
252 }
253 of(cmd, param)
254 },
255
256 set_hooks: function(abc) {
257 abc.draw_gchord = abc2svg.diag.draw_gchord.bind(abc,abc.draw_gchord)
258 abc.set_format = abc2svg.diag.set_fmt.bind(abc, abc.set_format)
259 }
260 } // diag
261
262 if (!abc2svg.mhooks)
263 abc2svg.mhooks = {}
264 abc2svg.mhooks.diag = abc2svg.diag.set_hooks