| 1 |
|
| 2 |
|
| 3 |
e |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
var init_done, pw, ml, mr, pkf, lkf, fn, |
| 21 |
h_sty = "" |
| 22 |
|
| 23 |
|
| 24 |
function clean_txt(txt) { |
| 25 |
return txt.replace(/<|>|&.*?;|&/g, function(c) { |
| 26 |
switch (c) { |
| 27 |
case '<': return "<" |
| 28 |
case '>': return ">" |
| 29 |
case '&': return "&" |
| 30 |
} |
| 31 |
return c |
| 32 |
}) |
| 33 |
} |
| 34 |
|
| 35 |
abc2svg.abort = function(e) { |
| 36 |
if (!init_done) |
| 37 |
user.img_out('') |
| 38 |
abc.get_parse().state = 0 |
| 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 |
} |
| 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 |
| 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' |
| 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 |
} |
| 114 |
|
| 115 |
|
| 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 |
|
| 155 |
abc2svg.abc_init = function(args) { |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
function gen_hf(type, str) { |
| 160 |
var i, page, |
| 161 |
lcr = ["l", "c", "r"], |
| 162 |
|
| 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 = ' ' |
| 170 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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"> </div>\n\ |
| 282 |
</td></tr></thead>\n\ |
| 283 |
<tbody><tr><td>') |
| 284 |
init_done = 2 |
| 285 |
} else { |
| 286 |
init_done = 1 |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
abc2svg.print(str) |
| 291 |
} |
| 292 |
|
| 293 |
|
| 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) |
| 310 |
user.img_out('') |
| 311 |
if (user.errtxt) |
| 312 |
abc2svg.print("<pre>" + clean_txt(user.errtxt) + "</pre>") |
| 313 |
if (font_style) |
| 314 |
abc2svg.print('<style>\n' + font_style + '\n</style>') |
| 315 |
if (init_done == 2) |
| 316 |
abc2svg.print('\ |
| 317 |
</td></tr></tbody>\n\ |
| 318 |
<tfoot><tr><td>\n\ |
| 319 |
<div class="f-sp"> </div>\n\ |
| 320 |
</td></tr></tfoot>\n\ |
| 321 |
</table>') |
| 322 |
|
| 323 |
} |