Debian Codegraph @

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

raw symbols SCIP
1 // equalbars.js - module to set equal spaced measure bars
2 //
3 // Copyright (C) 2018-2025 Jean-François 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 "%%equalbars" appears in a ABC source.
21 //
22 // Parameters
23 // %%equalbars bool
24
25 "use strict"
26 if (typeof abc2svg == "undefined")
27 var abc2svg = {}
28
29 abc2svg.equalbars = {
30
31 // new tune - clear the width of the start of the staff
32 output_music: function(of) {
33 this.equalbars_d = 0;
34 of()
35 },
36
37 // get the equalbars parameter
38 set_fmt: function(of, cmd, parm) {
39 if (cmd != "equalbars") {
40 of(cmd, parm)
41 return
42 }
43 var fmt = this.cfmt()
44 fmt.equalbars = this.get_bool(parm)
45 fmt.stretchlast = 1
46 },
47
48 // adjust the symbol offsets of a music line
49 // only the bars of the first voice are treated
50 set_sym_glue: function(of, width) {
51 var C = abc2svg.C,
52 s, s2, d, w, i, n, x, g, t, t0, f,
53 bars = [],
54 tsfirst = this.get_tsfirst();
55
56 of(width) // compute the x offset of the symbols
57 if (!this.cfmt().equalbars)
58 return
59
60 // search the first note/rest/space
61 for (s2 = tsfirst; s2; s2 = s2.next) {
62 switch (s2.type) {
63 default:
64 continue
65 case C.GRACE:
66 case C.MREST:
67 case C.NOTE:
68 case C.REST:
69 case C.SPACE:
70 break
71 }
72 break
73 }
74 if (!s2)
75 return
76
77 // build an array of the bars
78 t0 = t = s2.time
79 for (s = s2; s.next; s = s.next) {
80 if (s.type == C.BAR && s.seqst && s.time != t) {
81 bars.push([s, s.time - t]);
82 t = s.time
83 }
84 }
85
86 // push the last bar or replace it in the array
87 if (s.time != t)
88 bars.push([s, s.time - t])
89 else
90 bars[bars.length - 1][0] = s // replace the last bar
91
92 t = s.time
93 if (s.dur)
94 t += s.dur;
95
96 n = bars.length
97 if (n <= 1)
98 return // one or no bar
99
100 // if small width, get the widest measure
101 if (s.x < width) {
102 w = 0
103 x = 0
104 for (i = 0; i < n; i++) {
105 s = bars[i][0]
106 if (s.x - x > w)
107 w = s.x - x
108 x = s.x
109 }
110 if (w * n < width)
111 width = w * n
112 this.set_realwidth(width)
113 }
114
115 // set the measure parameters
116 x = s2.type == C.GRACE ? s2.extra.x : (s2.x - s2.wl)
117 if (this.equalbars_d < x)
118 this.equalbars_d = x // new offset of the first note/rest
119
120 d = this.equalbars_d
121 w = (width - d) / (t - t0) // width per time unit
122
123 // loop on the bars
124 for (i = 0; i < n; i++) {
125 do { // don't shift the 1st note from the bar
126 if (s2.type == C.GRACE) {
127 for (g = s2.extra; g; g = g.next)
128 g.x = d + g.x - x
129 } else {
130 s2.x = d + s2.x - x
131 }
132 s2 = s2.ts_next
133 } while (!s2.seqst)
134
135 s = bars[i][0]; // next bar
136 f = w * bars[i][1] / (s.x - x)
137
138 // and update the x offsets
139 for ( ; s2 != s; s2 = s2.ts_next) {
140 if (s2.type == C.GRACE) {
141 for (g = s2.extra; g; g = g.next)
142 g.x = d + (g.x - x) * f
143 // } else if (s2.x) {
144 } else {
145 s2.x = d + (s2.x - x) * f
146 }
147 }
148 d += w * bars[i][1];
149 x = s2.x
150 while (1) {
151 s2.x = d;
152 s2 = s2.ts_next
153 if (!s2 || s2.seqst)
154 break
155 }
156 if (!s2)
157 break
158 }
159 }, // set_sym_glue()
160
161 set_hooks: function(abc) {
162 abc.output_music = abc2svg.equalbars.output_music.bind(abc, abc.output_music);
163 abc.set_format = abc2svg.equalbars.set_fmt.bind(abc, abc.set_format);
164 abc.set_sym_glue = abc2svg.equalbars.set_sym_glue.bind(abc, abc.set_sym_glue)
165 }
166 } // equalbars
167
168 if (!abc2svg.mhooks)
169 abc2svg.mhooks = {}
170 abc2svg.mhooks.equalbars = abc2svg.equalbars.set_hooks