Debian Codegraph @

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

raw symbols SCIP
1 // fit2box.js - module for filling a tune in a box
2 //
3 // Copyright (C) 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 "%%fit2box" appears in a ABC source.
21 //
22 // Parameters
23 // %%fit2box width height
24 // width is the width of the box.
25 // The value '*' (star) is the value of %%pagewidth
26 // height is the height of the box.
27 // The value '*' (star) is the value od %%pageheight
28
29 if (typeof abc2svg == "undefined")
30 var abc2svg = {}
31
32 abc2svg.fit2box = {
33
34 // generation function with %%fit2box
35 do_fit: function(mus) {
36 var r, sv, v, w, h, hh, sc, marg, tit, cl,
37 parse = mus.get_parse(),
38 f = parse.file,
39 fn = parse.fname,
40 cfmt = mus.cfmt(),
41 wb = cfmt.fit2box[0],
42 hb = cfmt.fit2box[1],
43 user = mus.get_user(),
44 io = user.img_out, // save the original img_out
45 ob = "" // output buffer
46
47 user.img_out = function(p) { // function to get the output file
48 ob += p
49 }
50
51 // get a parameter
52 function getparm(parm) {
53 var j, v,
54 i = f.indexOf("%%" + parm)
55
56 if (i >= 0 && (!i || f[i - 1] == '\n')) {
57 j = f.indexOf('\n', i)
58 v = f.slice(i, j).split(/\s+/)
59 v = mus.get_unit(v[1])
60 } else {
61 v = cfmt[parm]
62 }
63 return v
64 } // getparm()
65
66 // set an old or new parameter
67 function setparm(parm, v) {
68 var i, j,
69 p = "%%" + parm
70
71 i = f.indexOf(p)
72 if (i >= 0 && (!i || f[i - 1] == '\n')) {
73 j = f.indexOf('\n', i)
74 f = f.replace(f.slice(i, j), p + ' ' + v)
75 } else {
76 f = p + ' ' + v + '\n' + f
77 }
78 } // setparm()
79
80 // get/set the box dimensions
81 if (wb == "*")
82 wb = getparm("pagewidth")
83 if (hb == "*")
84 hb = getparm("pageheight")
85 if (!hb)
86 hb = 1123 // (1123 = 29.7cm)
87
88 // set some parameters
89 setparm("stretchlast", "0")
90 setparm("stretchstaff", "0")
91
92 // get the original margins and remove them for the first generation
93 // (assume left margin == right margin)
94 marg = getparm("leftmargin")
95 setparm("leftmargin", "0")
96 setparm("rightmargin", "0")
97
98 // set the box width twice as before to avoid line breaks
99 setparm("pagewidth", (wb * 2).toFixed(2))
100
101 // force the scale
102 if (f.indexOf("\n%%pagescale ") >= 0)
103 f = f.replace(/(\n%%pagescale).*/, "$1 1")
104 else
105 f = f.replace(/(\nK:.*)/, "$1\n%%pagescale 1")
106 cfmt.trimsvg = 1
107 cfmt.fullsvg = "a"
108
109 // do a first generation
110 if (abc2svg.fit2box.otosvg)
111 abc2svg.fit2box.otosvg(fn, f)
112 else
113 mus.tosvg(fn, f)
114
115 // analyse the result of the generation
116 cfmt = mus.cfmt()
117 w = h = hh = 0
118 r = ob.match(/<svg[^>]*/g)
119 if (!r) {
120 user.img_out = io // restore the normal output
121 return // no SVG
122 }
123 while (1) {
124 sv = r.shift() // next music line
125 if (!sv)
126 break
127 v = sv.match(/viewBox="0 0 ([\d.]+) ([\d.]+)"/)
128 cl = sv.match(/class="([^"]+)"/) // "
129 //console.log("- sv ====\n"+sv+"\n ====\n cl:"+cl)
130 if (!tit // the first SVG is the tune header
131 || cl[1] == "header"
132 || cl[1] == "footer") {
133 hh += +v[2]
134 if (cl[1] != "header"
135 && cl[1] != "footer")
136 tit = 1
137 continue
138 }
139 if (+v[1] > w)
140 w = +v[1] // max width (thanks to trimsvg)
141 h += +v[2] // whole height
142 }
143 //console.log("-- box:"+wb+"x"+hb+" w:"+w.toFixed(2)+" marg:"+marg.toFixed(2)
144 //+" h:"+h.toFixed(2)+" hh:"+hh.toFixed(2))
145
146 sc = (hb - hh) / h // height scale
147
148 //fixme: magic value!
149 w += 24
150 v = (wb - marg * 2) / w // width scale
151 //console.log(" scw:"+v.toFixed(3)+" sch:"+sc.toFixed(3))
152
153 if (v <= sc) {
154 sc = v // width constraint
155 } else { // height constraint
156 v = Math.round((wb - w * sc) / 2) // margins
157 if (v < marg)
158 marg = v
159 }
160
161 setparm("pagewidth", wb)
162 setparm("leftmargin", marg.toFixed(0)) // restore the margins
163 setparm("rightmargin", marg.toFixed(0))
164 setparm("pagescale", sc)
165 setparm("stretchstaff", 1)
166 setparm("stretchlast", 1)
167 cfmt.fullsvg = ""
168 cfmt.trimsvg = 0
169
170 // do the last generation
171 //console.log("---\n"+f.slice(0, 500)+"\n---")
172 //console.log("-> "+wb+" "+hb+" sc:"+sc.toFixed(3)+" marg:"+marg)
173 mus.tunes.shift() // remove the tune class
174 user.img_out = io // restore the normal output
175 if (abc2svg.fit2box.otosvg) { // restore the tosvg function
176 mus.tosvg = abc2svg.fit2box.otosvg
177 abc2svg.fit2box.otosvg = null
178 }
179 mus.tosvg(fn, f)
180 abc2svg.fit2box.on = 0
181 }, // do_fit()
182
183 tosvg: function(of, fn, file, bol, eof) {
184 var parse = this.get_parse()
185
186 parse.fname = fn
187 parse.file = bol ? file.slice(bol) : file
188 parse.eol = 0
189
190 abc2svg.fit2box.on = 1
191 abc2svg.fit2box.do_fit(this)
192 }, // tosvg()
193
194 // get a formatting parameter
195 set_fmt: function(of, cmd, parm) {
196 if (cmd != "fit2box")
197 return of(cmd, parm)
198 if (abc2svg.fit2box.on)
199 return
200 abc2svg.fit2box.on = 1
201 if (!parm) { // stop fit2box
202 if (abc2svg.fit2box.otosvg) { // restore the tosvg function
203 this.tosvg = abc2svg.fit2box.otosvg
204 abc2svg.fit2box.otosvg = null
205 }
206 return
207 }
208
209 var cfmt = this.cfmt(),
210 parse = this.get_parse(),
211 f = parse.file
212
213 cfmt.fit2box = parm.split(/\s+/)
214
215 // if no tune yet, change the generation function
216 if (f.indexOf("X:") < 0) {
217 if (!abc2svg.fit2box.otosvg) {
218 abc2svg.fit2box.otosvg = this.tosvg
219 this.tosvg = abc2svg.fit2box.tosvg.bind(this, this.tosvg)
220 }
221 return
222 }
223
224 // do the fit2box generation now
225 parse.file = parse.file.slice(parse.eol)
226 parse.eol = 0
227 abc2svg.fit2box.do_fit(this)
228 parse.file = f
229 parse.eol = parse.file.length - 2 // stop the current parsing in tosvg()
230 }, // set_fmt()
231
232 set_hooks: function(abc) {
233 abc.set_format = abc2svg.fit2box.set_fmt.bind(abc, abc.set_format)
234 } // set_hooks()
235 } // fit2box
236
237 if (!abc2svg.mhooks)
238 abc2svg.mhooks = {}
239 abc2svg.mhooks.fit2box = abc2svg.fit2box.set_hooks