Debian Codegraph @

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

raw symbols SCIP
1 // sth.js - module to set the stem heights
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 "%%sth" appears in a ABC source.
21 //
22 // Parameters
23 // %%sth h1 h2 h3 ...
24 // The values h1, h2, .. are applied to the following notes which
25 // have a stem and which are not inside a beam.
26 // The value may be '*' for keeping the original stem length.
27
28 if (typeof abc2svg == "undefined")
29 var abc2svg = {}
30
31 abc2svg.sth = {
32
33 // function called after beam calculation
34 recal_beam: function(bm, s) {
35 var staff_tb = this.get_staff_tb(),
36 y = staff_tb[s.st].y,
37 s2 = bm.s2,
38 y2 = staff_tb[s2.st].y
39
40 if (s.sth != undefined)
41 s.ys = s.sth
42 if (s2.sth != undefined)
43 s2.ys = s2.sth;
44 bm.a = (s.ys + y - s2.ys - y2) / (s.xs - s2.xs);
45 bm.b = s.ys - s.xs * bm.a + y
46 while (1) {
47 s.ys = bm.a * s.xs + bm.b - y
48 if (s.stem > 0)
49 s.ymx = s.ys + 2.5
50 else
51 s.ymn = s.ys - 2.5;
52 s = s.next
53 if (s == s2)
54 break
55 }
56 },
57
58 // function called after the stem heights have been computed
59 set_sth: function() {
60 var s, h, v, sth_a, p_voice,
61 voice_tb = this.get_voice_tb()
62
63 for (v = 0; v < voice_tb.length; v++) {
64 p_voice = voice_tb[v]
65 if (p_voice.sth != null) // if no stem length in this voice
66 continue
67 sth_a = []
68 for (s = p_voice.sym; s; s = s.next) {
69 if (s.sth) {
70 sth_a = s.sth;
71 s.sth = null
72 }
73 if (sth_a.length == 0
74 || s.nflags <= -2 || s.stemless
75 || !(s.beam_st || s.beam_end))
76 continue
77 h = sth_a.shift()
78 if (h == '*')
79 continue // no change
80 if (h == '|') { // skip to the next measure bar
81 for (s = s.next; s; s = s.next) {
82 if (s.bar_type)
83 break
84 }
85 continue
86 }
87 h = Number(h)
88 if (isNaN(h) || !h)
89 continue // fixme: error
90 if (s.stem >= 0) {
91 s.ys = s.y + h;
92 s.ymx = (s.ys + 2.5) | 0
93 } else {
94 s.ys = s.y - h;
95 s.ymn = (s.ys - 2.5) | 0
96 }
97 s.sth = s.ys
98 }
99 }
100 }, // set_sth()
101
102 calculate_beam: function(of, bm, s1) {
103 var done = of(bm, s1)
104 if (done && bm.s2 && (s1.sth || bm.s2.sth))
105 abc2svg.sth.recal_beam.call(this, bm, s1)
106 return done
107 },
108
109 new_note: function(of, grace, tp_fact) {
110 var C = abc2svg.C,
111 s = of(grace, tp_fact),
112 curvoice = this.get_curvoice()
113
114 if (curvoice.sth && s && s.type == C.NOTE) {
115 s.sth = curvoice.sth;
116 curvoice.sth = null // some stem widths in this voice
117 }
118 return s
119 },
120
121 set_fmt: function(of, cmd, param) {
122 if (cmd == "sth") {
123 var curvoice = this.get_curvoice()
124 if (curvoice)
125 curvoice.sth = param.split(/[ \t;-]+/)
126 return
127 }
128 of(cmd, param)
129 },
130
131 set_stems: function(of) {
132 of();
133 abc2svg.sth.set_sth.call(this)
134 },
135
136 set_hooks: function(abc) {
137 abc.calculate_beam = abc2svg.sth.calculate_beam.bind(abc, abc.calculate_beam);
138 abc.new_note = abc2svg.sth.new_note.bind(abc, abc.new_note);
139 abc.set_format = abc2svg.sth.set_fmt.bind(abc, abc.set_format);
140 abc.set_stems = abc2svg.sth.set_stems.bind(abc, abc.set_stems)
141 }
142 } // sth
143
144 if (!abc2svg.mhooks)
145 abc2svg.mhooks = {}
146 abc2svg.mhooks.sth = abc2svg.sth.set_hooks