forked from moinejf/abc2svg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoabw.js
351 lines (325 loc) · 9.54 KB
/
toabw.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// abc2svg - toabw.js - SVG generation for Abiword
//
// Copyright (C) 2018 Jean-Francois Moine
//
// This file is part of abc2svg.
//
// abc2svg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// abc2svg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
// This script is a backend which permits to generate (ABW+SVG)
// instead of the (XHTML+SVG) default of batch scripts.
//
// Usage:
// - creation of a .abw file (example with abcjs24 - result 'o.abw')
//
// abcjs24 toabw.js some_ABC_file.abc > o.abw
//
// (the script must appear immediately after the command)
//
// - conversion of ABW to PDF (result 'o.pdf')
//
// abiword -t pdf --to-name=o.pdf o.abw
//
//Constraints:
// The abc2svg music font (abc2svg.woff or abc2svg.ttf) must be installed
// in the local system for correct rendering and conversion to PDF
// (the generated pdf file contains the used glyphs so that it may be
// displayed anywhere).
var topmargin, botmargin, page_size, page_type,
header, footer,
section = '',
data = '',
seq = 1
// set a value in page unit
function set_unit(p) {
if (typeof p == "string")
return p
if (page_type[0] == 'L')
return (p / 96).toFixed(2) + 'in'
return (p / 37.8).toFixed(2) + 'cm'
}
function header_footer(str) {
var c, i, t,
j = 0,
r = ["", "", ""]
if (str[0] == '"')
str = str.slice(1, -1)
if (str.indexOf('\t') < 0) // if no TAB
str = '\t' + str // center
for (i = 0; i < str.length; i++) {
c = str[i]
switch (c) {
case '\t':
if (j < 2)
j++ // next column
continue
case '\\': // hope '\n'
for (j = 0; j < 3; j++)
r[j] += '\n';
j = 0;
i++
continue
default:
r[j] += c
continue
case '$':
break
}
c = str[++i]
switch (c) {
case 'd': // cannot know the modification date of the file
break
case 'D':
r[j] += (new Date()).toUTCString()
break
case 'F':
r[j] += abc.get_fname()
break
case 'I':
c = str[++i]
case 'T':
t = abc.get_info(c)
if (t)
r[j] += t.split('\n', 1)[0]
break
case 'P':
r[j] += '<field type="page_number" xid="' +
(++seq).toString() + '"></field>'
break
case 'V':
r[j] += "abc2svg-" + abc2svg.version
break
}
}
return r
}
// output a header or footer
function gen_hf(type, str) {
var a, i, j, res,
lcr = ["left", "center", "right"]
function hfp(hf_pos) {
if (hf_pos == 0)
return '<p style="Normal" xid="' +
(++seq).toString() + '">'
return '<p style="Normal" xid="' + (++seq).toString() +
'" props="text-align:' + lcr[hf_pos] + '">'
} // hfp()
//fixme: could be reduced:
// if only one 'l', 'c' or 'r' => <p> with align
// if no 'c' -> table with 2 columns
res = '<section id="'+ (type[0] == 'h' ? '0' : '1') +
'" listid="0" parentid="0" type="' + type +
'" xid="' + (++seq).toString() + '">\n\
<table xid="' + (++seq).toString() + '"\
props="list-tag:1; table-column-props:6.00cm/6.00cm/6.00cm/;\
table-column-leftpos:' + (page_type[0] == 'L' ? '0.6in' : '1.5cm') + '">\n';
a = header_footer(str)
for (i = 0; i < 3; i++) {
res += '<cell xid="' + (++seq).toString() + '"\
props="left-attach:' + i + '; right-attach:' + (i + 1).toString() +
'; bot-attach:1; top-attach:0;\
top-color:ffffff; right-color:ffffff; bg-style:1;\
left-color:ffffff; bot-color:ffffff">\n';
str = a[i]
if (!str) {
res += '</cell>\n'
continue
}
j = str.indexOf('\n')
if (j >= 0)
res += hfp(i) + str.slice(0, j) + '</p>\n' +
hfp(i) + str.slice(j + 1)
else
res += hfp(i) + str;
res += '</p>\n</cell>\n'
}
return res + '</table>\n</section>'
} // gen_hf()
// output the abw file
function abw_out() {
var now = new Date(),
cdate = now.toUTCString()
print('<?xml version="1.0" encoding="UTF-8"?>\n\
<!DOCTYPE abiword PUBLIC "-//ABISOURCE//DTD AWML 1.0 Strict//EN"\
"http://www.abisource.com/awml.dtd">\n\
<abiword template="false" xmlns:ct="http://www.abisource.com/changetracking.dtd"\
xmlns:fo="http://www.w3.org/1999/XSL/Format"\
xmlns:math="http://www.w3.org/1998/Math/MathML"\
xid-max="'+ seq.toString() + '" xmlns:dc="http://purl.org/dc/elements/1.1/"\
styles="unlocked" fileformat="1.0" xmlns:svg="http://www.w3.org/2000/svg"\
xmlns:awml="http://www.abisource.com/awml.dtd"\
xmlns="http://www.abisource.com/awml.dtd"\
xmlns:xlink="http://www.w3.org/1999/xlink"\
version="0.99.2" xml:space="preserve" props="dom-dir:ltr;\
document-footnote-restart-section:0; document-endnote-type:numeric;\
document-endnote-place-enddoc:1; document-endnote-initial:1; lang:en-US;\
document-endnote-restart-section:0; document-footnote-restart-page:0;\
document-footnote-type:numeric; document-footnote-initial:1;\
document-endnote-place-endsection:0">\n\
\n\
<metadata>\n\
<m key="abiword.date_last_changed">' + cdate + '\n\
</m>\n\
<m key="abiword.generator">abc2svg</m>\n\
<m key="dc.creator">user</m>\n\
<m key="dc.date">' + cdate + '\n\
</m>\n\
<m key="dc.format">application/x-abiword</m>\n\
</metadata>\n\
<rdf>\n\
</rdf>\n\
<history version="1" edit-time="111" last-saved="1511365517"\
uid="e1099094-cf9b-11e7-913c-a8bbb9484d15">\n\
<version id="1" started="1511365517" uid="23b1c56a-cf9c-11e7-913c-a8bbb9484d15"\
auto="0" top-xid="3"/>\n\
</history>\n\
<styles>\n\
<s type="P" name="Normal" followedby="Current Settings"\
props="font-family:Times New Roman;\
margin-top:0pt; color:000000; margin-left:0pt; text-position:normal;\
widows:2; font-style:normal; text-indent:0in; font-variant:normal;\
font-weight:normal; margin-right:0pt; font-size:12pt; text-decoration:none;\
margin-bottom:0pt; line-height:1.0; bgcolor:transparent; text-align:left;\
font-stretch:normal"/>\n\
</styles>\n\
<pagesize pagetype="' + page_type + '"\
orientation="portrait" ' + page_size + '\
page-scale="1.00"/>\n\
<section xid="1" props="page-margin-footer:1.00cm; page-margin-header: 1.00cm;\
page-margin-right: 0.00cm;\
page-margin-left: 0.00cm;\
page-margin-top: ' + topmargin + ';\
page-margin-bottom: ' + botmargin + '">\n'+ section + '</section>');
if (header)
print(header)
if (footer)
print(footer)
print('<data>' + data + '</data>\n</abiword>')
}
// replace <>& by XML character references
function clean_txt(txt) {
return txt.replace(/<|>|&.*?;|&/g, function(c) {
switch (c) {
case '<': return "<"
case '>': return ">"
}
if (c == '&')
return "&"
return c
})
}
function abort(e) {
abc.blk_flush();
if (typeof printErr == 'function')
printErr(e.message + "\n*** Abort ***\n" + e.stack)
else
section += "<p>" + e.message + "\n*** Abort ***\n" + e.stack + "</p>\n";
abc_end();
quit()
}
function svg_out(str) {
var r, w, h
switch (str.slice(0, 4)) {
case '<svg':
r = str.slice(0, 200).match(/.*width="(.*?)px" height="(.*?)px"/);
w = r[1] / 96;
h = r[2] / 96;
data += '<d name="g'+ (++seq).toString() +
'" mime-type="image/svg+xml" base64="no">\n\
<![CDATA[' + str + ']]>\n\
</d>\n';
section += '<p style="Normal" xid="'+
(seq + 1).toString() + '"><image dataid="g' +
seq.toString() + '" xid="' +seq.toString() +
'" props="height:' + h.toFixed(2) + 'in; width:' +
w.toFixed(2) + 'in"/></p>\n';
seq++
// get the first header/footer
if (header == undefined) {
r = abc.get_fmt("header");
header = r ? gen_hf("header", r) : ''
}
if (footer == undefined) {
r = abc.get_fmt("footer");
footer = r ? gen_hf("footer", r) : ''
}
break
case '<div':
if (str.indexOf('newpage') > 0)
section += '<p style="Normal" xid="' +
(++seq).toString() + '"><pbr/></p>\n'
break
case '</di': // end of image
break
//fixme: markup
default:
section += str.replace(/<p| style|<span|<\/span>|<br\/>|size:.+?px/g,
function(c) {
switch (c) {
case '<p':
return '<p style="Normal" xid="' +
(++seq).toString() + '"'
case ' style':
return ' props'
case '<span':
return '<c'
case '</span>':
return '</c>'
case '<br/>': // hope followed by '\n'!
return ''
default: // size:..px
var r = c.match(/size:(.+)px/)
return 'size:' + (r[1] * 72 / 96).toFixed(2) + 'pt'
}
})
section += '\n'
break
}
}
// entry point from cmdline
function abc_init() {
// get the page parameters
user.img_out = function(str) {
var pw = abc.get_fmt("pagewidth");
page_type = pw > 800 ? 'Letter' : 'A4';
page_size = pw > 800 ?
'width="8.50" height="11.00" units="in"' :
'width="210.00" height="297.00" units="mm"';
topmargin = set_unit(abc.get_fmt("topmargin") || 37.8);
botmargin = set_unit(abc.get_fmt("botmargin") || 37.8);
// output the first generated string
svg_out(str);
// change the output function
user.img_out = svg_out
}
user.page_format = true
// define some parameters and functions in the Abc object
abc.tosvg('toabw', "%%beginjs\n\
Abc.prototype.get_fmt = function(k) { return cfmt[k] }\n\
Abc.prototype.get_info = function(k) { return info[k] }\n\
Abc.prototype.get_fname = function() { return parse.ctx.fname }\n\
%%endjs\n\
%%fullsvg 1\n\
%%musicfont abc2svg")
}
function abc_end() {
if (user.errtxt)
section += "<p>" + clean_txt(user.errtxt) + "</p>\n";
abw_out()
}
// nodejs
if (typeof module == 'object' && typeof exports == 'object') {
exports.abort = abort;
exports.abc_init = abc_init;
exports.abc_end = abc_end
}