| 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 |
if (typeof abc2svg == "undefined") |
| 34 |
var abc2svg = {} |
| 35 |
|
| 36 |
abc2svg.grid = { |
| 37 |
pl: '<path class="stroke" stroke-width="1" d="M', |
| 38 |
|
| 39 |
|
| 40 |
block_gen: function(of, s) { |
| 41 |
if (s.subtype != "grid") { |
| 42 |
of(s) |
| 43 |
return |
| 44 |
} |
| 45 |
|
| 46 |
var abc = this, |
| 47 |
img, cls, |
| 48 |
cfmt = abc.cfmt(), |
| 49 |
grid = cfmt.grid |
| 50 |
|
| 51 |
|
| 52 |
function build_grid(s, font) { |
| 53 |
var i, k, l, nr, bar, w, hr, x0, x, y, yl, ps, d, |
| 54 |
lc = '', |
| 55 |
chords = s.chords, |
| 56 |
bars = s.bars, |
| 57 |
parts = s.parts || [], |
| 58 |
wmx = s.wmx, |
| 59 |
cells = [], |
| 60 |
nc = grid.n |
| 61 |
|
| 62 |
|
| 63 |
function set_chords() { |
| 64 |
var i, ch, |
| 65 |
pch = '-' |
| 66 |
|
| 67 |
for (i = 0; i < chords.length; i++) { |
| 68 |
ch = chords[i] |
| 69 |
if (!ch[0]) |
| 70 |
ch[0] = pch |
| 71 |
if (ch.length == 0) |
| 72 |
continue |
| 73 |
if (ch.length == 1) { |
| 74 |
pch = ch[0] |
| 75 |
continue |
| 76 |
} |
| 77 |
if (ch.length == 2) { |
| 78 |
ch[2] = ch[1]; |
| 79 |
ch[1] = null; |
| 80 |
pch = ch[2] |
| 81 |
continue |
| 82 |
} |
| 83 |
if (ch.length == 3) { |
| 84 |
pch = ch[2] |
| 85 |
continue |
| 86 |
} |
| 87 |
if (!ch[2]) |
| 88 |
ch[2] = ch[1] || ch[0]; |
| 89 |
pch = ch[3] |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
function build_cell(cell, x, y, yl, hr) { |
| 94 |
if (cell.length > 1) { |
| 95 |
abc.out_svg(abc2svg.grid.pl) |
| 96 |
abc.out_sxsy(x - wmx / 2, ' ', yl) |
| 97 |
abc.out_svg('l' + |
| 98 |
wmx.toFixed(1) + ' -' + hr.toFixed(1) + |
| 99 |
'"/>\n') |
| 100 |
if (cell[1]) { |
| 101 |
abc.out_svg(abc2svg.grid.pl) |
| 102 |
abc.out_sxsy(x - wmx / 2, ' ', yl + hr) |
| 103 |
abc.out_svg('l' + |
| 104 |
(wmx / 2).toFixed(1) + ' ' + (hr / 2).toFixed(1) + |
| 105 |
'"/>\n') |
| 106 |
abc.set_font('gs') |
| 107 |
abc.xy_str(x - wmx / 3, y, cell[0]) |
| 108 |
abc.xy_str(x, y + hr / 3, cell[1]) |
| 109 |
} else { |
| 110 |
abc.set_font('gs') |
| 111 |
abc.xy_str(x - wmx * .2, y + hr / 4, cell[0]) |
| 112 |
} |
| 113 |
if (cell.length >= 3) { |
| 114 |
if (cell[3]) { |
| 115 |
abc.out_svg(abc2svg.grid.pl) |
| 116 |
abc.out_sxsy(x, ' ', yl + hr / 2) |
| 117 |
abc.out_svg('l' + |
| 118 |
(wmx / 2).toFixed(1) + ' ' + (hr / 2).toFixed(1) + |
| 119 |
'"/>\n') |
| 120 |
abc.set_font('gs') |
| 121 |
abc.xy_str(x, y - hr / 3, cell[2]) |
| 122 |
abc.xy_str(x + wmx / 3, y, cell[3]) |
| 123 |
} else { |
| 124 |
abc.set_font('gs') |
| 125 |
abc.xy_str(x + wmx * .2, y - hr / 4, cell[2]) |
| 126 |
} |
| 127 |
} |
| 128 |
} else { |
| 129 |
abc.set_font('grid') |
| 130 |
abc.xy_str(x, y, cell[0]) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
function draw_hl() { |
| 136 |
var i, i1, j, x, |
| 137 |
y = -1 |
| 138 |
|
| 139 |
for (i = 0; i <= nr + 1; i++) { |
| 140 |
j = 0 |
| 141 |
i1 = i > 0 ? i - 1 : 0 |
| 142 |
while (1) { |
| 143 |
while (j <= nc && !d[i1][j]) |
| 144 |
j++ |
| 145 |
if (j > nc) |
| 146 |
break |
| 147 |
x = wmx * j |
| 148 |
while (j <= nc && d[i1][j]) |
| 149 |
j++ |
| 150 |
if (i && i1 < nr) { |
| 151 |
while (j <= nc && d[i1 + 1][j]) |
| 152 |
j++ |
| 153 |
} |
| 154 |
abc.out_svg('M') |
| 155 |
abc.out_sxsy(x0 + x, ' ', y) |
| 156 |
abc.out_svg('h' + (wmx * j - x).toFixed(1)+ '\n') |
| 157 |
} |
| 158 |
y -= hr |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
function draw_vl() { |
| 164 |
var i, i1, j, y, |
| 165 |
x = x0 |
| 166 |
|
| 167 |
for (i = 0; i <= nc; i++) { |
| 168 |
j = 0 |
| 169 |
i1 = i > 0 ? i - 1 : 0 |
| 170 |
while (1) { |
| 171 |
while (j <= nr && !d[j][i1]) |
| 172 |
j++ |
| 173 |
if (j > nr) |
| 174 |
break |
| 175 |
y = hr * j |
| 176 |
while (j <= nr && d[j][i1]) |
| 177 |
j++ |
| 178 |
abc.out_svg('M') |
| 179 |
abc.out_sxsy(x, ' ', -y - .5) |
| 180 |
abc.out_svg('v' + (hr * j - y + 1).toFixed(1) + '\n') |
| 181 |
} |
| 182 |
x += wmx |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
set_chords() |
| 190 |
|
| 191 |
|
| 192 |
if (!grid.ls) { |
| 193 |
cells = chords |
| 194 |
} else { |
| 195 |
bar = bars; |
| 196 |
bars = [ ] |
| 197 |
ps = parts |
| 198 |
parts = [] |
| 199 |
for (i = 0; i < grid.ls.length; i++) { |
| 200 |
l = grid.ls[i] |
| 201 |
if (l.indexOf('-') < 0) |
| 202 |
l = [l, l] |
| 203 |
else |
| 204 |
l = l.split('-') |
| 205 |
for (k = l[0] - 1; k < l[1]; k++) { |
| 206 |
if (!chords[k]) |
| 207 |
break |
| 208 |
cells.push(chords[k]); |
| 209 |
bars.push(bar[k]) |
| 210 |
parts.push(ps[k]) |
| 211 |
} |
| 212 |
} |
| 213 |
bars.push(bar[k]) |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
if (nc < 0) |
| 218 |
nc = -nc |
| 219 |
if (nc < 3) |
| 220 |
nc = cells.length % 6 == 0 ? 6 : 8 |
| 221 |
if (nc > cells.length) |
| 222 |
nc = cells.length; |
| 223 |
|
| 224 |
hr = font.size * 2 |
| 225 |
if (wmx < hr * 1.5) |
| 226 |
wmx = hr * 1.5 |
| 227 |
|
| 228 |
x0 = img.width - img.lm - img.rm |
| 229 |
w = wmx * nc |
| 230 |
if (w > x0) { |
| 231 |
nc /= 2; |
| 232 |
w /= 2 |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
yl = -1 |
| 237 |
y = -1 + font.size * .6 |
| 238 |
nr = -1 |
| 239 |
x0 = (x0 / cfmt.scale - w) / 2 |
| 240 |
d = [] |
| 241 |
for (i = 0; i < cells.length; i++) { |
| 242 |
if (i == 0 |
| 243 |
|| (grid.repbrk |
| 244 |
&& (bars[i].slice(-1) == ':' || bars[i][0] == ':')) |
| 245 |
|| parts[i] |
| 246 |
|| k >= nc) { |
| 247 |
y -= hr |
| 248 |
yl -= hr |
| 249 |
x = x0 + wmx / 2 |
| 250 |
k = 0 |
| 251 |
nr++ |
| 252 |
d[nr] = [] |
| 253 |
} |
| 254 |
d[nr][k] = 1 |
| 255 |
k++ |
| 256 |
build_cell(cells[i], x, y, yl, hr) |
| 257 |
x += wmx |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
abc.out_svg('<path class="stroke" stroke-width="1" d="\n') |
| 262 |
draw_hl() |
| 263 |
draw_vl() |
| 264 |
abc.out_svg('"/>\n') |
| 265 |
|
| 266 |
|
| 267 |
y = -1 + font.size * .7 |
| 268 |
x = x0 |
| 269 |
for (i = 0; i < bars.length; i++) { |
| 270 |
bar = bars[i] |
| 271 |
if (bar[0] == ':') { |
| 272 |
abc.out_svg('<text class="' + cls + '" x="') |
| 273 |
abc.out_sxsy(x - 5, '" y="', y) |
| 274 |
abc.out_svg('" style="font-weight:bold;font-size:' + |
| 275 |
(font.size * 1.5).toFixed(1) + 'px">:</text>\n') |
| 276 |
} |
| 277 |
if (i == 0 |
| 278 |
|| (grid.repbrk |
| 279 |
&& (bar.slice(-1) == ':' || bar[0] == ':')) |
| 280 |
|| parts[i] |
| 281 |
|| k >= nc) { |
| 282 |
y -= hr; |
| 283 |
x = x0 |
| 284 |
k = 0 |
| 285 |
if (parts[i]) { |
| 286 |
w = abc.strwh(parts[i])[0] |
| 287 |
abc.out_svg('<text class="' + cls + '" x="') |
| 288 |
abc.out_sxsy(x - 2 - w, '" y="', y) |
| 289 |
abc.out_svg('" style="font-weight:bold">' + |
| 290 |
parts[i] + '</text>\n') |
| 291 |
} |
| 292 |
} |
| 293 |
k++ |
| 294 |
if (bar.slice(-1) == ':') { |
| 295 |
abc.out_svg('<text class="' + cls + '" x="') |
| 296 |
abc.out_sxsy(x + 5, '" y="', y) |
| 297 |
abc.out_svg('" style="font-weight:bold;font-size:' + |
| 298 |
(font.size * 1.5).toFixed(1) + 'px">:</text>\n') |
| 299 |
} |
| 300 |
x += wmx |
| 301 |
} |
| 302 |
abc.vskip(hr * (nr + 1) + 6) |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
var p_voice, n, font, f2 |
| 307 |
|
| 308 |
abc.set_page() |
| 309 |
img = abc.get_img() |
| 310 |
|
| 311 |
|
| 312 |
font = abc.get_font('grid') |
| 313 |
if (font.class) |
| 314 |
font.class += ' mid' |
| 315 |
else |
| 316 |
font.class = 'mid' |
| 317 |
cls = abc.font_class(font) |
| 318 |
|
| 319 |
|
| 320 |
abc.param_set_font("gsfont", |
| 321 |
font.name + ' ' + (font.size * .7).toFixed(1)) |
| 322 |
f2 = cfmt.gsfont |
| 323 |
if (font.weight) |
| 324 |
f2.weight = font.weight |
| 325 |
if (font.style) |
| 326 |
f2.style = font.style |
| 327 |
f2.class = font.class |
| 328 |
abc.add_style("\n.mid {text-anchor:middle}") |
| 329 |
|
| 330 |
|
| 331 |
abc.blk_flush() |
| 332 |
build_grid(s, font) |
| 333 |
abc.blk_flush() |
| 334 |
}, |
| 335 |
|
| 336 |
set_stems: function(of) { |
| 337 |
var C = abc2svg.C, |
| 338 |
abc = this, |
| 339 |
tsfirst = abc.get_tsfirst(), |
| 340 |
voice_tb = abc.get_voice_tb(), |
| 341 |
cfmt = abc.cfmt(), |
| 342 |
grid = cfmt.grid |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
function cs_filter(a_cs) { |
| 348 |
var i, cs, t |
| 349 |
|
| 350 |
for (i = 0; i < a_cs.length; i++) { |
| 351 |
cs = a_cs[i] |
| 352 |
if (cs.type != 'g') |
| 353 |
continue |
| 354 |
t = cs.text |
| 355 |
if (cfmt.altchord) { |
| 356 |
for (i++; i < a_cs.length; i++) { |
| 357 |
cs = a_cs[i] |
| 358 |
if (cs.type != 'g') |
| 359 |
continue |
| 360 |
t = cs.text |
| 361 |
break |
| 362 |
} |
| 363 |
} |
| 364 |
return t.replace(/\[|\]/g, '') |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
function get_beat(s) { |
| 369 |
var beat = C.BLEN / 4 |
| 370 |
|
| 371 |
if (!s.a_meter[0] || s.a_meter[0].top[0] == 'C' |
| 372 |
|| !s.a_meter[0].bot) |
| 373 |
return beat |
| 374 |
beat = C.BLEN / s.a_meter[0].bot[0] |0 |
| 375 |
if (s.a_meter[0].bot[0] == 8 |
| 376 |
&& s.a_meter[0].top[0] % 3 == 0) |
| 377 |
beat = C.BLEN / 8 * 3 |
| 378 |
return beat |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
function build_chords(sb) { |
| 383 |
var s, i, w, bt, rep, |
| 384 |
bars = [], |
| 385 |
chords = [], |
| 386 |
parts = [], |
| 387 |
chord = [], |
| 388 |
beat = get_beat(voice_tb[0].meter), |
| 389 |
wm = voice_tb[0].meter.wmeasure, |
| 390 |
cur_beat = 0, |
| 391 |
beat_i = 0, |
| 392 |
wmx = 0, |
| 393 |
some_chord = 0 |
| 394 |
|
| 395 |
|
| 396 |
bars.push('|') |
| 397 |
for (s = tsfirst; s; s = s.ts_next) { |
| 398 |
while (s.time > cur_beat) { |
| 399 |
if (beat_i < 3) |
| 400 |
beat_i++ |
| 401 |
cur_beat += beat |
| 402 |
} |
| 403 |
if (s.part) |
| 404 |
parts[chords.length] = s.part.text |
| 405 |
switch (s.type) { |
| 406 |
case C.NOTE: |
| 407 |
case C.REST: |
| 408 |
case C.SPACE: |
| 409 |
if (!s.a_gch || chord[beat_i]) |
| 410 |
break |
| 411 |
bt = cs_filter(s.a_gch) |
| 412 |
if (!bt) |
| 413 |
break |
| 414 |
w = abc.strwh(bt.replace( |
| 415 |
/<[^>]+>/gm,'')) |
| 416 |
if (w[0] > wmx) |
| 417 |
wmx = w[0] |
| 418 |
bt = new String(bt) |
| 419 |
bt.wh = w |
| 420 |
chord[beat_i] = bt |
| 421 |
break |
| 422 |
case C.BAR: |
| 423 |
i = s.bar_num |
| 424 |
bt = s.bar_type |
| 425 |
while (s.ts_next && s.ts_next.time == s.time) { |
| 426 |
if (s.ts_next.dur |
| 427 |
|| s.ts_next.type == C.SPACE) |
| 428 |
break |
| 429 |
s = s.ts_next |
| 430 |
if (s.type == C.METER) { |
| 431 |
beat = get_beat(s) |
| 432 |
wm = s.wmeasure |
| 433 |
continue |
| 434 |
} |
| 435 |
if (s.type != C.BAR) |
| 436 |
continue |
| 437 |
if (s.bar_type[0] == ':' |
| 438 |
&& bt[0] != ':') |
| 439 |
bt = ':' + bt |
| 440 |
if (s.bar_type.slice(-1) == ':' |
| 441 |
&& bt.slice(-1) != ':') |
| 442 |
bt += ':' |
| 443 |
if (s.bar_num) |
| 444 |
i = s.bar_num |
| 445 |
if (s.part) |
| 446 |
parts[chords.length + 1] = s.part.text |
| 447 |
} |
| 448 |
if (grid.norep) |
| 449 |
bt = '|' |
| 450 |
if (s.time < wm) { |
| 451 |
if (chord.length) { |
| 452 |
chords.push(chord) |
| 453 |
bars.push(bt) |
| 454 |
} else { |
| 455 |
bars[0] = bt |
| 456 |
} |
| 457 |
} else { |
| 458 |
if (!i) |
| 459 |
break |
| 460 |
chords.push(chord) |
| 461 |
bars.push(bt) |
| 462 |
} |
| 463 |
if (chord.length) |
| 464 |
some_chord++ |
| 465 |
chord = [] |
| 466 |
cur_beat = s.time |
| 467 |
beat_i = 0 |
| 468 |
if (bt.indexOf(':') >= 0) |
| 469 |
rep = true |
| 470 |
break |
| 471 |
case C.METER: |
| 472 |
beat = get_beat(s) |
| 473 |
wm = s.wmeasure |
| 474 |
break |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
if (chord.length) { |
| 479 |
bars.push('') |
| 480 |
chords.push(chord) |
| 481 |
} |
| 482 |
if (!some_chord) |
| 483 |
return |
| 484 |
|
| 485 |
wmx += abc.strwh(rep ? ' ' : ' ')[0] |
| 486 |
|
| 487 |
sb.chords = chords |
| 488 |
sb.bars = bars |
| 489 |
if (grid.parts && parts.length) |
| 490 |
sb.parts = parts |
| 491 |
sb.wmx = wmx |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
if (grid) { |
| 498 |
var C = abc2svg.C, |
| 499 |
tsfirst = this.get_tsfirst(), |
| 500 |
fmt = tsfirst.fmt, |
| 501 |
voice_tb = this.get_voice_tb(), |
| 502 |
p_v = voice_tb[this.get_top_v()], |
| 503 |
s = { |
| 504 |
type: C.BLOCK, |
| 505 |
subtype: 'grid', |
| 506 |
dur: 0, |
| 507 |
time: 0, |
| 508 |
p_v: p_v, |
| 509 |
v: p_v.v, |
| 510 |
st: p_v.st |
| 511 |
} |
| 512 |
|
| 513 |
if (!cfmt.gridfont) |
| 514 |
abc.param_set_font("gridfont", "serif 16") |
| 515 |
abc.set_font('grid') |
| 516 |
build_chords(s) |
| 517 |
|
| 518 |
|
| 519 |
if (!s.chords) { |
| 520 |
; |
| 521 |
} else if (grid.nomusic) { |
| 522 |
this.set_tsfirst(s) |
| 523 |
} else if (grid.n < 0) { |
| 524 |
for (var s2 = tsfirst; s2.ts_next; s2 = s2.ts_next) |
| 525 |
; |
| 526 |
s.time = s2.time |
| 527 |
s.prev = p_v.last_sym.prev |
| 528 |
s.prev.next = s |
| 529 |
s.next = p_v.last_sym |
| 530 |
p_v.last_sym.prev = s |
| 531 |
|
| 532 |
s.ts_prev = s2.ts_prev |
| 533 |
s.ts_prev.ts_next = s |
| 534 |
s.ts_next = s2 |
| 535 |
s2.ts_prev = s |
| 536 |
if (s2.seqst) { |
| 537 |
s.seqst = true |
| 538 |
s2.seqst = false |
| 539 |
} |
| 540 |
} else { |
| 541 |
s.next = p_v.sym |
| 542 |
s.ts_next = tsfirst |
| 543 |
tsfirst.ts_prev = s |
| 544 |
this.set_tsfirst(s) |
| 545 |
p_v.sym.prev = s |
| 546 |
p_v.sym = s |
| 547 |
} |
| 548 |
s.fmt = s.prev ? s.prev.fmt : fmt |
| 549 |
} |
| 550 |
of() |
| 551 |
}, |
| 552 |
|
| 553 |
set_fmt: function(of, cmd, parm) { |
| 554 |
if (cmd == "grid") { |
| 555 |
if (!parm) |
| 556 |
parm = "1"; |
| 557 |
parm = parm.split(/\s+/) |
| 558 |
var grid = {n: Number(parm.shift())} |
| 559 |
if (isNaN(grid.n)) { |
| 560 |
if (parm.length) { |
| 561 |
this.syntax(1, this.errs.bad_val, "%%grid") |
| 562 |
return |
| 563 |
} |
| 564 |
grid.n = 1 |
| 565 |
} |
| 566 |
while (parm.length) { |
| 567 |
var item = parm.shift() |
| 568 |
if (item == "norepeat") |
| 569 |
grid.norep = true |
| 570 |
else if (item == "nomusic") |
| 571 |
grid.nomusic = true |
| 572 |
else if (item == "parts") |
| 573 |
grid.parts = true |
| 574 |
else if (item == "repbrk") |
| 575 |
grid.repbrk = true |
| 576 |
else if (item.slice(0, 8) == "include=") |
| 577 |
grid.ls = item.slice(8).split(',') |
| 578 |
} |
| 579 |
this.cfmt().grid = grid |
| 580 |
return |
| 581 |
} |
| 582 |
of(cmd, parm) |
| 583 |
}, |
| 584 |
|
| 585 |
set_hooks: function(abc) { |
| 586 |
abc.block_gen = abc2svg.grid.block_gen.bind(abc, abc.block_gen) |
| 587 |
abc.set_stems = abc2svg.grid.set_stems.bind(abc, abc.set_stems) |
| 588 |
abc.set_format = abc2svg.grid.set_fmt.bind(abc, abc.set_format) |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
if (!abc2svg.mhooks) |
| 593 |
abc2svg.mhooks = {} |
| 594 |
abc2svg.mhooks.grid = abc2svg.grid.set_hooks |