Debian Codegraph @

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

raw symbols SCIP
1 // tunhd.js - module for a formatted tune header
2 //
3 // Copyright (C) 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 "%%titleformat" appears in a ABC source.
21 //
22
23 "use strict"
24 if (typeof abc2svg == "undefined")
25 var abc2svg = {}
26
27 abc2svg.tunhd = {
28 info_fnt: {
29 A: "info",
30 C: "composer",
31 O: "composer",
32 P: "parts",
33 Q: "tempo",
34 R: "info",
35 T: "title",
36 X: "title"
37 },
38
39 // output the tune header from a titleformat
40 tunhd: function(of) {
41 var abc = this,
42 cfmt = abc.cfmt(),
43 glovar = abc.glovar(),
44 info = abc.info(),
45 info_sz = {
46 A: cfmt.infospace,
47 C: cfmt.composerspace,
48 O: cfmt.composerspace,
49 R: cfmt.infospace
50 },
51 info_nb = {},
52 c, align, q, j,
53 hfnt = abc.get_font("history"),
54 line = [], // array of [align, str, w, h]
55 p = cfmt.titleformat,
56 i = 0,
57 ya = {
58 l: cfmt.titlespace,
59 c: cfmt.titlespace,
60 r: cfmt.titlespace
61 },
62 xa = {
63 l: 0,
64 c: abc.get_lwidth() * .5,
65 r: abc.get_lwidth()
66 }
67
68 if (!p)
69 return of()
70
71 // output one title line
72 function out() {
73 var item, align, p, h, x, yd,
74 yb = {
75 l: 0,
76 c: 0,
77 r: 0
78 },
79 y = 0,
80 i = 0
81
82 while (1) { // get the y offset of the top text
83 item = line[i++] // [ align, string, height ]
84 if (!item)
85 break
86 align = item[0]
87 h = item[2] * 1.1
88 if (y < h)
89 y = h
90 yb[align] = item[2]
91 }
92 ya.l += y - yb.l
93 ya.c += y - yb.c
94 ya.r += y - yb.r
95 while (1) {
96 item = line.shift()
97 if (!item)
98 break
99 align = item[0]
100 p = item[1]
101 h = item[2] * 1.1
102 x = xa[align]
103 y = ya[align] + h
104 yd = y - item[2] * .22 // descent
105 abc.out_svg('<text class="'
106 + abc.font_class(hfnt)
107 + '" x="')
108 abc.out_sxsy(x, '" y="', -yd)
109 if (align == 'c')
110 abc.out_svg('" text-anchor="middle')
111 else if (align == 'r')
112 abc.out_svg('" text-anchor="end')
113 abc.out_svg('">' + p + '</text>\n')
114 ya[align] = y
115 }
116 if (ya.c > ya.l)
117 ya.l = ya.c
118 if (ya.r > ya.l)
119 ya.l = ya.r
120 ya.c = ya.r = ya.l
121 } // out()
122
123 // convert a string containing info fields ($x)
124 // and return [ align, string, height ]
125 function cnv(p) {
126 var c, j, t, fntnam, fnt, wh, nfnt,
127 h = 0,
128 i = 0,
129 l = 0,
130 o = ""
131
132 while (1) {
133 c = p[i++]
134 if (!c)
135 break
136 if (c != '$') {
137 if (!o)
138 h = hfnt.size * 1.1
139 o += c
140 continue
141 }
142 if (p[i] < 'A' || p[i] > 'Z') {
143 if (isNaN(+p[i]))
144 o += c
145 else
146 nfnt = +p[i++]
147 continue
148 }
149 c = p[i++] // info letter
150 if (!info[c])
151 continue // return?
152 j = info_nb[c] || 0
153 info_nb[c] = j + 1
154 t = info[c].split('\n')[j] // info value
155 if (!t)
156 continue // return?
157 fntnam = abc2svg.tunhd.info_fnt[c] || "history"
158 fnt = abc.get_font(fntnam)
159 switch (c) {
160 case 'P':
161 t = cfmt.partname ? abc.part_seq(c) : info.P
162 break
163 case 'Q':
164 abc.set_width(glovar.tempo)
165 t = glovar.tempo.tempo_str
166 glovar.tempo.invis = 1 //true
167 break
168 case 'T':
169 if (j)
170 fnt = abc.get_font("subtitle")
171 break
172 default:
173 t = info[c].split('\n')[j]
174 break
175 }
176 if (fnt != hfnt || nfnt) {
177 if (nfnt)
178 fnt = abc.get_font("u" + nfnt)
179 abc.set_font(fnt)
180 }
181 if (c == 'Q') { // string already formatted
182 wh = glovar.tempo.tempo_wh
183 } else {
184 t = abc.str2svg(t)
185 wh = t.wh
186 }
187 if (fnt != hfnt)
188 t = '<tspan class="' + abc.font_class(fnt)
189 + '">' + t + '</tspan>'
190 if (wh[1] > h)
191 h = wh[1]
192 o += t
193 }
194 if (!o)
195 return
196 return [ align, o, h]
197 } // cnv()
198
199 abc.set_font(hfnt)
200 while (1) {
201 while (p[i] == ' ')
202 i++
203 c = p[i++]
204 if (!c)
205 break
206 if (c < 'A' || c > 'Z') {
207 switch (c) {
208 case ',': // end line
209 out()
210 // fall thru
211 default:
212 continue
213 case '<':
214 align = 'l'
215 c = p[i++]
216 break
217 case '>':
218 align = 'r'
219 c = p[i++]
220 break
221 case '"':
222 align = 'c'
223 break
224 }
225 } else {
226 switch (p[i]) { // old syntax
227 case '-':
228 align = 'l'
229 i++
230 break
231 case '1':
232 align = 'r'
233 i++
234 break
235 case '0':
236 i++
237 // fall thru
238 default:
239 align = 'c'
240 break
241 }
242 }
243 if (c != '"') {
244 q = "$" + c
245 if (p[i] == '+')
246 q += " $" + p[++i] // assume there is a letter
247 } else {
248 j = p.indexOf('"' , i + 1)
249 if (j < 0) {
250 //fixme: error
251 i = p.length
252 continue
253 }
254 q = p.slice(i, j)
255 i = j + 1
256 }
257 q = cnv(q)
258 if (q) // if some text
259 line.push(q)
260 }
261 out() // last line
262
263 abc.vskip(ya.l + cfmt.musicspace)
264 }, // tunhd()
265
266 set_fmt: function(of, cmd, parm) {
267 if (cmd == "titleformat")
268 this.cfmt()[cmd] = parm
269 else
270 of(cmd, parm)
271 }, // set_fmt()
272
273 set_hooks: function(abc) {
274 abc.set_format = abc2svg.tunhd.set_fmt.bind(abc, abc.set_format)
275 abc.tunhd = abc2svg.tunhd.tunhd.bind(abc, abc.tunhd)
276 }
277 } // tunhd
278
279 if (!abc2svg.mhooks)
280 abc2svg.mhooks = {}
281 abc2svg.mhooks.tunhd = abc2svg.tunhd.set_hooks