Debian Codegraph @

lib/ChordPro/res/abc/abc2svg/tohtml.js

raw symbols SCIP
1 // abc2svg - tohtml.js - HTML+SVG generation
2 //
3 // Copyright (C) 2014-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 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 General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
19
20 var init_done, pw, ml, mr, pkf, lkf, fn,
21 h_sty = ""
22
23 // replace <>& by XML character references
24 function clean_txt(txt) {
25 return txt.replace(/<|>|&.*?;|&/g, function(c) {
26 switch (c) {
27 case '<': return "&lt;"
28 case '>': return "&gt;"
29 case '&': return "&amp;"
30 }
31 return c
32 })
33 }
34
35 abc2svg.abort = function(e) {
36 if (!init_done) // if empty document
37 user.img_out('')
38 abc.get_parse().state = 0 // force block flush
39 abc.blk_flush()
40 if (typeof abc2svg.printErr == 'function')
41 abc2svg.printErr(e.message + "\n*** Abort ***\n" + e.stack)
42 else
43 abc2svg.print("<pre>" + e.message + "\n*** Abort ***\n" + e.stack + "</pre>")
44 abc2svg.abc_end()
45 abc2svg.quit()
46 }
47
48 function get_date() {
49 return (new Date()).toUTCString()
50 } // get_date()
51
52 function header_footer(str) {
53 var c, d, i, t,
54 j = 0,
55 r = ["", "", ""]
56
57 if (str[0] == '"')
58 str = str.slice(1, -1)
59 while (1) {
60 i = str.indexOf('$', j)
61 if (i < 0)
62 break
63 c = str[++i]
64 switch (c) {
65 case 'd':
66 if (!abc2svg.get_mtime)
67 break // cannot know the modification date of the file
68 d = abc2svg.get_mtime(abc.get_parse().fname)
69 break
70 case 'D':
71 d = get_date()
72 break
73 case 'F':
74 d = abc.get_parse().fname
75 break
76 case 'I':
77 str = str.replace('$', '')
78 d = str[i]
79 case 'T':
80 t = abc.info()[c]
81 d = t ? t.split('\n', 1)[0] : ''
82 break
83 case 'P':
84 d = '\x0c' // form feed
85 break
86 case 'V':
87 d = "abc2svg-" + abc2svg.version
88 break
89 default:
90 d = ''
91 i--
92 break
93 }
94 str = str.replace('$' + c, d)
95 j = i
96 }
97 str = str.split('\n')
98 for (j = 0; j < str.length; j++) {
99 if (j != 0)
100 for (i = 0; i < 3; i++)
101 r[i] += '<br>'
102 t = str[j].split('\t')
103 if (t.length == 1) {
104 r[1] += t[0]
105 } else {
106 for (i = 0; i < 3; i++) {
107 if (t[i])
108 r[i] += t[i]
109 }
110 }
111 }
112 return r
113 } // header_footer()
114
115 // set a paragraph style
116 function set_pstyle() {
117 var nml, nmr, nlkf, npkf, npw,
118 cfmt = abc.cfmt(),
119 psty = ''
120
121 nml = cfmt.leftmargin
122 if (nml != ml) {
123 if (ml == undefined)
124 ml = nml
125 psty += 'margin-left:' + nml.toFixed(1) + 'px;'
126 }
127 nmr = cfmt.rightmargin
128 if (nmr != mr) {
129 if (mr == undefined)
130 mr = nmr
131 psty += 'margin-right:' + nmr.toFixed(1) + 'px;'
132 }
133 nlkf = cfmt.lineskipfac
134 if (nlkf != lkf) {
135 if (lkf == undefined)
136 lkf = nlkf
137 psty += 'line-height:' + ((nlkf * 100) | 0).toString() + '%;'
138 }
139 npkf = cfmt.parskipfac
140 if (npkf != pkf) {
141 if (pkf == undefined)
142 pkf = npkf
143 psty += 'margin-bottom:' + npkf.toFixed(2) + 'em;'
144 }
145 npw = cfmt.pagewidth
146 if (npw != pw || nml != ml || nmr != mr) {
147 if (pw == undefined)
148 pw = npw
149 psty += 'width:' + (npw - nml - nmr).toFixed(1) + 'px;'
150 }
151 return psty
152 }
153
154 // entry point from cmdline
155 abc2svg.abc_init = function(args) {
156 // var cfmt = abc.cfmt()
157
158 // output a header or footer
159 function gen_hf(type, str) {
160 var i, page,
161 lcr = ["l", "c", "r"],
162 //fixme: handle font changes?
163 a = header_footer(clean_txt(str))
164
165 abc2svg.print('<table class="' + type + '" width="100%"><tr>')
166 for (i = 0; i < 3; i++) {
167 str = a[i]
168 if (!str)
169 str = '&nbsp;'
170 //fixme
171 if (str.indexOf('\x0c') >= 0) {
172 str = str.replace('\x0c', '')
173 page = " page"
174 } else {
175 page = ''
176 }
177 abc2svg.print('<td class="' + lcr[i] + page +
178 '" width="33%">' +
179 str + '</td>')
180 }
181 abc2svg.print('</table>')
182 }
183
184 user.page_format = true
185
186 // output the html header
187 user.img_out = function(str) {
188 if (!str)
189 return
190 if (init_done) {
191 abc2svg.print(str)
192 return
193 }
194 if (/^<style>[^<]+<\/style>$/.test(str)) {
195 h_sty = str.replace(/^<style>\n|<\/style>$/g,'')
196 return
197 }
198
199 var cfmt = abc.cfmt(),
200 header = cfmt.header,
201 footer = cfmt.footer,
202 topmargin = cfmt.topmargin || "1cm",
203 botmargin = cfmt.botmargin || "1cm",
204 media_s = '@media print {\n\
205 body {margin:0; padding:0; border:0}\n\
206 .newpage {page-break-before: always}\n\
207 div.nobrk {page-break-inside: avoid}\n\
208 }',
209 media_f = '@media screen {\n\
210 .header, .footer, .h-sp, .f-sp {display: none}\n\
211 }\n\
212 @media print {\n\
213 body {margin:0; padding:0; border:0;\n\
214 counter-reset: page;\n\
215 counter-increment: page; }\n\
216 .newpage {page-break-before: always}\n\
217 div.nobrk {page-break-inside: avoid}\n\
218 .header {\n\
219 position: fixed;\n\
220 top: ' + cfmt.headerfont.size + 'px;\n\
221 height: ' + (cfmt.headerfont.size * 2) + 'px;\n\
222 ' + abc.style_font(cfmt.headerfont) + ';\n\
223 left: ' + cfmt.leftmargin.toFixed(1) + 'px;\n\
224 width: ' + (cfmt.pagewidth - cfmt.leftmargin
225 - cfmt.rightmargin).toFixed(1) + 'px\n\
226 }\n\
227 .footer {\n\
228 position: fixed;\n\
229 bottom: 0;\n\
230 height: ' + (cfmt.footerfont.size * 2) + 'px;\n\
231 ' + abc.style_font(cfmt.footerfont) + ';\n\
232 left: ' + cfmt.leftmargin.toFixed(1) + 'px;\n\
233 width: ' + (cfmt.pagewidth - cfmt.leftmargin
234 - cfmt.rightmargin).toFixed(1) + 'px\n\
235 }\n\
236 .h-sp, .f-sp {height: '
237 + (cfmt.headerfont.size * 2) + 'px}\n\
238 div.page:after {\n\
239 counter-increment: page;\n\
240 content: counter(page);\n\
241 }\n\
242 .l {text-align: left}\n\
243 .c {text-align: center}\n\
244 .r {text-align: right}\n\
245 }';
246
247 // no margin / header / footer when SVG page formatting
248 if (abc.page)
249 topmargin = botmargin = header = footer = 0
250
251 abc2svg.print('<!DOCTYPE html>\n\
252 <html>\n\
253 <meta charset="utf-8"/>\n\
254 <meta name="generator" content="abc2svg-' + abc2svg.version + '"/>\n\
255 <!-- CreationDate: ' + get_date() + '-->\n\
256 <style>\n\
257 body {width:' + cfmt.pagewidth.toFixed(0) +'px}\n\
258 svg {display:block}\n\
259 p {' + set_pstyle() + 'margin-top:0}\n\
260 p span {line-height:' + ((cfmt.lineskipfac * 100) | 0).toString() + '%}\n' +
261 ((header || footer) ? media_f : media_s))
262 // important for chrome and --headless (abctopdf)
263 if (abc.page)
264 abc2svg.print('@page{size:' +
265 (cfmt.pagewidth / 96).toFixed(2) + 'in ' +
266 (cfmt.pageheight / 96).toFixed(2) + 'in;margin:0}')
267
268 abc2svg.print(h_sty + '</style>\n\
269 <title>' + fn.replace(/.*\//,'')
270 + '</title>\n\
271 <body>')
272 if (header || footer) {
273 if (header)
274 gen_hf("header", header)
275 if (footer)
276 gen_hf("footer", footer)
277
278 abc2svg.print('\
279 <table style="margin:0" width="100%">\n\
280 <thead><tr><td>\n\
281 <div class="h-sp">&nbsp;</div>\n\
282 </td></tr></thead>\n\
283 <tbody><tr><td>')
284 init_done = 2 // with header/footer
285 } else {
286 init_done = 1
287 }
288
289 // output the first generated string
290 abc2svg.print(str)
291 }
292
293 // get the main ABC source file name
294 for (var i = 0; i < args.length; i++) {
295 var a = args[i]
296
297 if (a[0] == '-') {
298 i++
299 continue
300 }
301 fn = a
302 break
303 }
304 }
305
306 abc2svg.abc_end = function() {
307 var font_style = abc.get_font_style()
308
309 if (!init_done) // if empty document
310 user.img_out('')
311 if (user.errtxt)
312 abc2svg.print("<pre>" + clean_txt(user.errtxt) + "</pre>")
313 if (font_style) // if some %%text at the end
314 abc2svg.print('<style>\n' + font_style + '\n</style>')
315 if (init_done == 2) // if with header/footer
316 abc2svg.print('\
317 </td></tr></tbody>\n\
318 <tfoot><tr><td>\n\
319 <div class="f-sp">&nbsp;</div>\n\
320 </td></tr></tfoot>\n\
321 </table>')
322 //abc2svg.print('</html>')
323 }