Debian Codegraph @

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

raw symbols SCIP
1 // clair.js - module to output Clairnote sheets (https:clairnote.org)
2 //
3 // Copyright (C) 2019-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 when "%%clairnote" appears in a ABC source.
21 //
22 // Parameters (none)
23 // %%clairnote 1
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.clair = {
29
30 // table for helper lines (from C, to c'')
31 // index = (note pitch + 16)
32 // value = array of line numbers / null
33 hl_tb: [
34 new Int8Array([-10,-8,-6,-4,-2]), // _B,, -18
35 new Int8Array([-10,-9,-8,-6,-4,-2]), // B,, -17
36 new Int8Array([-8,-6,-4,-2]), // C, -16
37 new Int8Array([-8,-6,-4,-2]), // ^C, -15
38 new Int8Array([-8,-7,-6,-4,-2]), // D, -14
39 new Int8Array([-6,-4,-2]), // ^D, -13
40 new Int8Array([-6,-4,-2]), // E, -12
41 new Int8Array([-6,-4,-2]), // F, -11
42 new Int8Array([-6,-5,-4,-2]), // ^F, -10
43 new Int8Array([-4,-2]), // G, -9
44 new Int8Array([-4,-2]), // ^G, -8
45 new Int8Array([-4,-2]), // A, -7
46 new Int8Array([-4,-3,-2]), // _B, -6
47 new Int8Array([-2]), // B, -5
48 new Int8Array([-2]), // C -4
49 new Int8Array([-2]), // ^C -3
50 new Int8Array([-2,-1]), // D -2
51 null, // ^D -1
52 null, // E 0
53 null, // F 1
54 new Int8Array([1]), // ^F 2
55 null, // G 3
56 null, // ^G 4
57 null, // A 5
58 new Int8Array([3,4]), // _B 6
59 new Int8Array([4]), // B 7
60 new Int8Array([4]), // c 8
61 new Int8Array([4]), // ^c 9
62 new Int8Array([4,5]), // d 10
63 null, // _e 11
64 null, // e 12
65 null, // f 13
66 new Int8Array([7]), // ^f 14
67 null, // g 15
68 null, // ^g 16
69 null, // a 17
70 new Int8Array([9,10]), // _b 18
71 new Int8Array([10]), // b 19
72 new Int8Array([10]), // c' 20
73 new Int8Array([10]), // ^c' 21
74 new Int8Array([10,11]), // d' 22
75 new Int8Array([10,11]), // _e' 23
76 new Int8Array([10,12]), // e' 24
77 new Int8Array([10,12]), // f' 25
78 new Int8Array([10,12,13]), // ^f' 26
79 new Int8Array([10,12,13]), // g' 27
80 new Int8Array([10,12,14]), // ^g' 28
81 new Int8Array([10,12,14]), // a' 29
82 new Int8Array([10,12,14,15]), // _b' 30
83 new Int8Array([10,12,14,15]), // b' 31
84 new Int8Array([10,12,14,16]) // c'' 32
85 ],
86
87 // draw the helper lines
88 draw_hl: function(of, s) {
89 if (!s.p_v.clair) {
90 of(s)
91 return
92 }
93
94 var i, m, hl,
95 dx = s.grace ? 4 : [4.7, 5, 6, 7.2, 7.5][s.head] * 1.4,
96 p_st = this.get_staff_tb()[s.st]
97
98 for (m = 0; m <= s.nhd; m++) {
99 hl = abc2svg.clair.hl_tb[s.notes[m].pit]
100 if (!hl)
101 continue
102 for (i = 0; i < hl.length; i++)
103 this.set_hl(p_st, hl[i], s.x, -dx, dx)
104 }
105 }, // draw_hl()
106
107 // draw the key signature
108 draw_keysig: function(of, x, s) {
109 if (!s.p_v.clair) {
110 of(x, s)
111 return
112 }
113
114 var i,
115 staffb = this.get_staff_tb()[s.st].y,
116 a_tb = new Int8Array(24),
117 sc = [0, 2, 4, 5, 7, 9, 11, 12, 14, 16, 17, 19, 21, 23],
118 // major scale (2 octaves)
119 bn_tb = [7, 2, -3, 4, -1, 6, 1, 8, 3, -2, 5, 0, 7, 2, -3],
120 // #sf -> (major) base note
121 bn = bn_tb[s.k_sf + 7] // base note
122
123 if (!s.k_a_acc) {
124 if (s.k_sf > 0) {
125 for (i = 1; i <= s.k_sf; i++) {
126 a_tb[bn_tb[i] + 3] = 1; // index = y + 3
127 a_tb[bn_tb[i] + 12 + 3] = 1
128 }
129 if (bn + sc[s.k_mode] > 8)
130 bn -= 12
131 for (i = 7; --i >= 0; ) {
132 y = bn + sc[s.k_mode + i]
133 this.xygl(x, staffb + y * 3, (y & 1) ? "wk" : "bk");
134 if (a_tb[y + 3])
135 this.xygl(x,
136 staffb + y * 3,
137 "sht");
138 switch (s.k_mode + i) {
139 case 3:
140 case 7:
141 case 10:
142 x += 4.5
143 break
144 }
145 }
146 } else {
147 for (i = s.k_sf; i < 0; i++) {
148 a_tb[bn_tb[i + 6] + 3] = 1; // index = y + 3
149 a_tb[bn_tb[i + 6] + 12 + 3 ] = 1
150 }
151 if (bn + sc[s.k_mode] > 8)
152 bn -= 12
153 for (i = 0; i < 7; i++) { // down top
154 y = bn + sc[s.k_mode + i]
155 this.xygl(x, staffb + y * 3, (y & 1) ? "wk" : "bk");
156 if (a_tb[y + 3])
157 this.xygl(x,
158 staffb + y * 3,
159 "flt");
160 switch (s.k_mode + i) {
161 case 2:
162 case 6:
163 case 9:
164 x += 4.5
165 break
166 }
167 }
168 }
169 } else if (s.k_a_acc.length) {
170 for (i = 0; i < s.k_a_acc.length; i++) {
171 var acc = s.k_a_acc[i];
172 j = sc[(acc.pit + 3) % 7];
173 a_tb[j] = acc.acc;
174 a_tb[j + 12] = acc.acc
175 }
176 // todo
177 }
178 }, // draw_keysig()
179
180 // update a clef
181 new_clef: function(s) {
182 switch (s.clef_type) {
183 case 't':
184 s.clef_line = 3;
185 s.clefpit = -2;
186 s.clef_name = "cl4"
187 break
188 case 'c':
189 s.clef_line = 3;
190 s.clefpit = 6;
191 s.clef_name = "cl3"
192 break
193 case 'b':
194 s.clef_line = 3;
195 s.clefpit = 14;
196 s.clef_name = "cl2"
197 break
198 }
199 }, // new_clef()
200
201 // function called before SVG generation
202 output_music: function(of) {
203 var s, m, mp, p_v, v,
204 cfmt = this.cfmt(),
205 tsfirst = this.get_tsfirst(),
206 voice_tb = this.get_voice_tb()
207
208 // define the new clefs and key signatures
209 // (clefs converted from clairnote.ly)
210 this.do_begin_end("svg", null, '<defs>\n\
211 <g id="cl2">\n\
212 <path transform="translate(-9,-27) scale(8)" d="m0.2656 -0.78107\n\
213 C0.3775 -0.79547 0.4351 -0.84567 0.7003 -0.85587\n\
214 C0.9459 -0.86587 1.0531 -0.85987 1.1805 -0.83797\n\
215 C1.6967 -0.74937 2.1173 -0.13032 2.1173 0.64059\n\
216 C2.1173 2.10531 0.9987 3.04975 0.019 3.8078\n\
217 C0 3.8345 0 3.846 0 3.8652\n\
218 C0 3.9101 0.022 3.94 0.056 3.94\n\
219 C0.071 3.94 0.079 3.93904 0.107 3.9231\n\
220 C1.3341 3.23572 2.6095 2.2656 2.6095 0.57604\n\
221 C2.6095 -0.4711 2.0006 -1.05061 1.1664 -1.05061\n\
222 C0.9058 -1.05561 0.7658 -1.05861 0.5568 -1.02591\n\
223 C0.4588 -1.01061 0.248 -0.97281 0.219 -0.92831\n\
224 C0.165 -0.89151 0.162 -0.77308 0.266 -0.78129"/>\n\
225 <text x="-5" y="-24" font-size="28px">&#xe083;</text>\n\
226 </g>\n\
227 <g id="cl3">\n\
228 <path transform="translate(-9,-12) scale(8)" d="m1.0406 -2.93878\n\
229 C0.9606 -2.93578 0.8881 -2.93178 0.8237 -2.92878\n\
230 L0.8237 -2.92846\n\
231 C0.6586 -2.92046 0.4659 -2.89806 0.3697 -2.87906\n\
232 C0.1409 -2.83386 0.0236 -2.78916 0 -2.75937\n\
233 C-0.018 -2.73927 -0.015 -2.71087 0 -2.69037\n\
234 C0.023 -2.64587 0.145 -2.67017 0.4188 -2.72887\n\
235 C0.5108 -2.74867 0.6924 -2.76597 0.8607 -2.77257\n\
236 C1.0868 -2.78157 1.2883 -2.70417 1.3194 -2.69167\n\
237 C1.7053 -2.53668 2.0444 -2.24033 2.0444 -1.46855\n\
238 C2.0444 -0.8488 1.8942 -0.04261 1.4629 -0.04261\n\
239 C1.4489 -0.04061 1.4419 -0.03861 1.4289 -0.02891\n\
240 C1.4149 -0.01311 1.4179 -0.00091 1.4169 0.01179\n\
241 C1.4169 0.01193 1.4169 0.01195 1.4169 0.01211\n\
242 C1.4169 0.01225 1.4169 0.01227 1.4169 0.01243\n\
243 C1.4169 0.02513 1.4169 0.03723 1.4289 0.05313\n\
244 C1.4389 0.06213 1.4479 0.06493 1.4629 0.06683\n\
245 C1.8942 0.06683 2.0444 0.87302 2.0444 1.49278\n\
246 C2.0444 2.26455 1.7053 2.56059 1.3194 2.71559\n\
247 C1.2884 2.72799 1.0868 2.80579 0.8607 2.79679\n\
248 C0.6924 2.78979 0.5113 2.77259 0.4188 2.75279\n\
249 C0.145 2.69409 0.0231 2.66979 0 2.71429\n\
250 C-0.011 2.73479 -0.014 2.76349 0 2.78359\n\
251 C0.024 2.81339 0.1409 2.85799 0.3697 2.90328\n\
252 C0.4657 2.92228 0.6586 2.94468 0.8237 2.95268\n\
253 L0.8237 2.953\n\
254 C0.9525 2.958 1.1126 2.9714 1.305 2.96\n\
255 C1.9479 2.916 2.5587 2.47655 2.5587 1.48844\n\
256 C2.5587 0.89409 2.1807 0.20184 1.7065 0.01218\n\
257 C2.1807 -0.17748 2.5587 -0.86972 2.5587 -1.46406\n\
258 C2.5587 -2.45218 1.9479 -2.89194 1.305 -2.93594\n\
259 C1.209 -2.94194 1.1207 -2.94094 1.0406 -2.93794"/>\n\
260 <text x="-5,-5" y="0,-24" font-size="28px">&#xe083;&#xe084;</text>\n\
261 </g>\n\
262 <g id="cl4">\n\
263 <path transform="translate(-9,3) scale(8)" d="m1.5506 -4.76844\n\
264 C1.5376 -4.76844 1.5066 -4.75114 1.5136 -4.73384\n\
265 L1.7544 -4.17292\n\
266 C1.8234 -3.97367 1.8444 -3.88334 1.8444 -3.66416\n\
267 C1.8444 -3.16204 1.5635 -2.76967 1.2174 -2.38312\n\
268 L1.0789 -2.2278\n\
269 C0.5727 -1.68982 0 -1.16441 0 -0.45906\n\
270 C0 -0.36713 -0.6414 1.05 1.4549 1.05\n\
271 C1.5319 1.05 1.6984 1.0492 1.8799 1.0372\n\
272 C2.0139 1.0282 2.1594 0.9969 2.2732 0.9744\n\
273 C2.3771 0.9538 2.5752 0.8757 2.5752 0.8757\n\
274 C2.7512 0.8152 2.6612 0.62915 2.5442 0.6835\n\
275 C2.5442 0.6835 2.3481 0.7626 2.2449 0.7822\n\
276 C2.1355 0.803 1.9939 0.8319 1.8645 0.8382\n\
277 C1.6935 0.8462 1.5257 0.8402 1.4569 0.8352\n\
278 C1.1541 0.8139 0.8667 0.67432 0.6558 0.48763\n\
279 C0.5148 0.36284 0.3782 0.17408 0.3582 -0.12709\n\
280 C0.3582 -0.76471 0.792 -1.23147 1.255 -1.71365\n\
281 L1.3978 -1.86523\n\
282 C1.8046 -2.29959 2.185 -2.75829 2.185 -3.32815\n\
283 C2.185 -3.77846 1.9185 -4.42204 1.6113 -4.75678\n\
284 C1.5983 -4.76858 1.5713 -4.77188 1.5513 -4.76828"/>\n\
285 <text x="-3" y="0" font-size="28px">&#xe084;</text>\n\
286 </g>\n\
287 <ellipse id="bk" class="fill" rx="2.9" ry="2.4"/>\n\
288 <ellipse id="wk" class="stroke" stroke-width="1" rx="2.5" ry="2"/>\n\
289 <path id="flt" class="stroke" stroke-width="1.5" d="m-2.5 -1l-7 -7"/>\n\
290 <path id="sht" class="stroke" stroke-width="1.5" d="m-2.5 1l-7 7"/>\n\
291 </defs>')
292
293 // change the pitches of all notes
294 // (pitch + accidental => offset)
295 for (v = 0; v < voice_tb.length; v++) {
296 p_v = voice_tb[v]
297 if (!p_v.clair)
298 continue
299 p_v.scale = 1.3;
300 for (s = p_v.sym; s; s = s.next) {
301 if (!s.dur)
302 continue
303 for (m = 0; m <= s.nhd; m++) {
304 mp = s.notes[m].midi
305 if (mp) {
306 mp -= 46;
307 s.notes[m].pit = mp
308 delete s.notes[m].acc
309 }
310 }
311 }
312 }
313 of()
314 },
315
316 // set a format parameter
317 set_fmt: function(of, cmd, param) {
318 if (cmd == "clairnote") {
319 if (!this.get_bool(param))
320 return
321 this.set_v_param("clair", true);
322 this.set_v_param("stafflines", "|-|---|-|")
323 this.set_v_param("staffscale", .8)
324 return
325 }
326 of(cmd, param)
327 },
328
329 // set the pitches according to the clefs
330 set_pitch: function(of, last_s) {
331 if (last_s) {
332 of(last_s)
333 return // not the 1st time
334 }
335 var p_v, s, v,
336 voice_tb = this.get_voice_tb(),
337 staff_tb = this.get_staff_tb()
338
339 for (v = 0; v < voice_tb.length; v++) {
340 p_v = voice_tb[v]
341 if (!p_v.clair)
342 continue
343 abc2svg.clair.new_clef(staff_tb[p_v.st].clef)
344 for (s = p_v.sym; s; s = s.next) {
345 if (s.clef_type)
346 abc2svg.clair.new_clef(s)
347 }
348 }
349 of(last_s)
350 },
351
352 // set the clairnote flag in the current voice
353 set_vp: function(of, a) {
354 var i,
355 curvoice = this.get_curvoice()
356
357 for (i = 0; i < a.length; i++) {
358 if (a[i] == "clair=") {
359 curvoice.clair = a[i + 1]
360 break
361 }
362 }
363 of(a)
364 },
365
366 // set the width of the clairnote key signatures
367 set_width: function(of, s) {
368 if (s.k_sf && s.p_v && s.p_v.clair) {
369 s.wl = 8;
370 s.wr = 10
371 } else {
372 of(s)
373 }
374 },
375
376 set_hooks: function(abc) {
377 abc.draw_hl = abc2svg.clair.draw_hl.bind(abc, abc.draw_hl);
378 abc.draw_keysig = abc2svg.clair.draw_keysig.bind(abc, abc.draw_keysig);
379 abc.output_music = abc2svg.clair.output_music.bind(abc, abc.output_music);
380 abc.set_format = abc2svg.clair.set_fmt.bind(abc, abc.set_format);
381 abc.set_pitch = abc2svg.clair.set_pitch.bind(abc, abc.set_pitch);
382 abc.set_vp = abc2svg.clair.set_vp.bind(abc, abc.set_vp);
383 abc.set_width = abc2svg.clair.set_width.bind(abc, abc.set_width)
384 }
385 } // clair
386
387 if (!abc2svg.mhooks)
388 abc2svg.mhooks = {}
389 abc2svg.mhooks.clair = abc2svg.clair.set_hooks