Debian Codegraph @

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

raw symbols SCIP
1 // capo.js - module to add a capo chord line
2 //
3 // Copyright (C) 2018-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 "%%capo" appears in a ABC source.
21 //
22 // Parameters
23 // %%capo n 'n' is the capo fret number
24
25 if (typeof abc2svg == "undefined")
26 var abc2svg = {}
27
28 abc2svg.capo = {
29 // b40 intervals of capo
30 icb40: [0, 5, 6,11,16,17,22,23,28,33,34,39],
31 // e f ^f g _a a _b b c _d d _e
32
33 gch_build: function(of, s) {
34 var t, i, gch, gch2, i2,
35 abc = this,
36 p_v = abc.get_curvoice(),
37 a_gch = s.a_gch
38
39 if (p_v.capo && a_gch) {
40 t = p_v.capo
41 i = 0
42
43 while (1) {
44 gch = a_gch[i++]
45 if (!gch) {
46 of(s)
47 return
48 }
49 if (gch.type == 'g')
50 break
51 }
52 gch2 = Object.create(gch)
53 gch2.capo = false // (would be erased when setting gch)
54 gch2.text = abc.gch_tr1(gch2.text, -abc2svg.capo.icb40[t % 12])
55 if (!p_v.capo_first) { // if new voice
56 p_v.capo_first = true
57 gch2.text += " (capo: " + t.toString() + ")"
58 }
59
60 gch2.font = abc.get_font(abc.cfmt().capofont ?
61 "capo" : "annotation")
62 a_gch.splice(i, 0, gch2)
63
64 // set a mark in the first chord symbol for %%diagram
65 gch.capo = true
66 }
67 of(s)
68 },
69
70 set_fmt: function(of, cmd, param) {
71 if (cmd == "capo") {
72 this.set_v_param("capo_", param)
73 return
74 }
75 of(cmd, param)
76 },
77
78 // get the parameters of the current voice
79 set_vp: function(of, a) {
80 var i, v,
81 p_v = this.get_curvoice()
82
83 for (i = 0; i < a.length; i++) {
84 if (a[i] == "capo_=") {
85 v = Number(a[++i])
86 if (isNaN(v) || v <= 0)
87 this.syntax(1, "Bad fret number in %%capo")
88 else
89 p_v.capo = v
90 break
91 }
92 }
93 of(a)
94 }, // set_vp()
95
96 set_hooks: function(abc) {
97 abc.gch_build = abc2svg.capo.gch_build.bind(abc, abc.gch_build);
98 abc.set_format = abc2svg.capo.set_fmt.bind(abc, abc.set_format)
99 abc.set_vp = abc2svg.capo.set_vp.bind(abc, abc.set_vp)
100 }
101 } // capo
102
103 if (!abc2svg.mhooks)
104 abc2svg.mhooks = {}
105 abc2svg.mhooks.capo = abc2svg.capo.set_hooks