Debian Codegraph @

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

raw symbols SCIP
1 // grid2.js - module to replace a voice in the music by a chord grid
2 //
3 // Copyright (C) 2018-2025 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 "%%grid2" appears in a ABC source.
21 //
22 // Parameters
23 // %%grid2 y
24 // This command may appear globally or in a voice.
25
26 if (typeof abc2svg == "undefined")
27 var abc2svg = {}
28
29 abc2svg.grid2 = {
30
31 // function called before tune generation
32 do_grid: function() {
33 var s, v, p_v, ix, cs, c_a_cs, bt, gch,
34 voice_tb = this.get_voice_tb()
35
36 if (this.cfmt().grid2)
37 for (v = 0; v < voice_tb.length; v++)
38 if (voice_tb[v].grid2 == undefined)
39 voice_tb[v].grid2 = 1
40
41 for (v = 0; v < voice_tb.length; v++) {
42 p_v = voice_tb[v]
43 if (!p_v.grid2)
44 continue
45 this.set_curvoice(p_v)
46 this.set_v_param("stafflines", "...") // no staff
47 p_v.clef.invis = true; // no clef
48 p_v.clef.clef_none = 1 //true // don't make it visible
49 p_v.key.k_sf = 0 // no key signature
50 delete p_v.key.k_a_acc
51 p_v.staffnonote = 2 // draw the staff
52 for (s = p_v.sym; s; s = s.next) {
53 delete s.a_dd // no decoration
54 if (!s.dur) {
55 if (s.bar_type)
56 bt = s.time
57 continue
58 }
59
60 // set all notes
61 s.invis = true; // as invisible
62 delete s.sl1; // with no slur
63 delete s.ti1 // and no tie
64 delete s.ti2
65 for (ix = 0; ix <= s.nhd; ix++)
66 delete s.notes[ix].tie_ty
67 if (s.tf) // don't show the tuplets
68 s.tf[0] = 1
69 if (!s.a_gch) {
70 if (s.time == bt)
71 s.a_gch = [ this.clone(c_a_cs) ]
72 continue
73 }
74 for (ix = 0; ix < s.a_gch.length; ix++) {
75 gch = s.a_gch[ix]
76 if (gch.type == 'g') {
77 c_a_cs = gch
78 break
79 }
80 }
81 }
82 }
83 }, // do_grid()
84
85 // adjust the width of the symbol with a chord
86 set_w: function(of, s) {
87 of(s)
88 if (!s.p_v || !s.p_v.grid2
89 || !s.a_gch
90 || ((!s.prev || s.prev.invis)
91 && (!s.next || s.next.invis)))
92 return
93 var i, gch
94
95 for (i = 0; i < s.a_gch.length; i++) {
96 gch = s.a_gch[i]
97 if (gch.type == 'g') {
98 if (s.wl < -gch.x)
99 s.wl = -gch.x
100 if (s.wr < gch.text.wh[0])
101 s.wr = gch.text.wh[0]
102 break
103 }
104 }
105 }, // set_w()
106
107 // draw the chord symbol in the middle of the staff
108 draw_gchord: function(of, i, s, x, y) {
109 var an
110 if (s.p_v.grid2) {
111 this.set_dscale(s.st)
112 an = s.a_gch[i]
113 if (an.type == 'g') {
114 this.use_font(an.font)
115 this.set_font(an.font)
116 this.xy_str(s.x + an.x, 12 - an.font.size * .5,
117 an.text)
118 }
119 } else {
120 of(i, s, x, y)
121 }
122 },
123
124 output_music: function(of) {
125 abc2svg.grid2.do_grid.call(this);
126 of()
127 },
128
129 set_fmt: function(of, cmd, param) {
130 if (cmd == "grid2") {
131 var curvoice = this.get_curvoice(),
132 v = this.get_bool(param)
133
134 if (curvoice)
135 curvoice.grid2 = v
136 else
137 this.cfmt().grid2 = v
138 return
139 }
140 of(cmd, param)
141 },
142
143 set_hooks: function(abc) {
144 abc.draw_gchord = abc2svg.grid2.draw_gchord.bind(abc, abc.draw_gchord);
145 abc.output_music = abc2svg.grid2.output_music.bind(abc, abc.output_music);
146 abc.set_format = abc2svg.grid2.set_fmt.bind(abc, abc.set_format)
147 abc.set_width = abc2svg.grid2.set_w.bind(abc, abc.set_width)
148 }
149 } // grid2
150
151 if (!abc2svg.mhooks)
152 abc2svg.mhooks = {}
153 abc2svg.mhooks.grid2 = abc2svg.grid2.set_hooks