Debian Codegraph @

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

raw symbols SCIP
1 // wps.js
2 // (c) 2009 Tomas Hlavaty
3 // reference: http://logand.com/sw/wps/log.html
4
5 function isQuoted(V) {
6 return V.q;
7 }
8
9 function quote(V) {
10 V.q = true;
11 return V;
12 }
13
14 function unquote(V) {
15 delete V.q;
16 return V;
17 }
18
19 function Symbol(N) {
20 this.nm = N;
21 return this;
22 }
23
24 function isSymbol(V) {
25 return V && V.constructor === Symbol;
26 }
27
28 function symbolName(V) {
29 return V.nm;
30 }
31
32 function isArray(V) {
33 return V && V.constructor === Array;
34 }
35
36 function inDs(Ds, K) {
37 for(var I = Ds.length - 1; 0 <= I; --I) {
38 if("undefined" != typeof Ds[I][K])
39 return Ds[I];
40 }
41 return false;
42 }
43
44 function member(C, L) {
45 return 0 <= L.indexOf(C);
46 }
47
48 function PsParser() {
49 var Self = this;
50 function init(L) {
51 Self.L = L;
52 Self.N = L.length;
53 Self.I = 0;
54 Self.D = 0;
55 }
56 function peek() {return Self.I < Self.N && Self.L[Self.I];}
57 function xchar() {return Self.I < Self.N && Self.L[Self.I++];}
58 function skip() { // TODO white space ffeed + null???
59 while(Self.I < Self.N && member(Self.L[Self.I], " \t\n"))
60 Self.I++;
61 }
62 function comment() {
63 while("%" == peek()) {
64 while(peek() && "\n" != peek())
65 xchar();
66 skip();
67 }
68 }
69 function text() {
70 // TODO hex text in <>
71 // TODO ASCII base-85 <~ and ~>
72 xchar();
73 var L = [];
74 var N = 1;
75 while(0 < N && peek()) {
76 var C = xchar();
77 switch(C) {
78 case "(":
79 N++;
80 break;
81 case ")":
82 N--;
83 if(N <= 0) C = false;
84 break;
85 case "\\":
86 C = xchar();
87 switch(C) {
88 case "(": break;
89 case ")": break;
90 case "\\": break;
91 case "n": C = "\n"; break;
92 case "r": C = "\r"; break;
93 case "t": C = "\t"; break;
94 // TODO \n (ignore \n) \b \f \ddd octal
95 default:
96 C = false;
97 }
98 break;
99 }
100 if(C !== false) L.push(C);
101 }
102 return L.join("");
103 }
104 function symbol() {
105 // TODO 1e10 1E-5 real numbers
106 // TODO radix numbers 8#1777 16#FFFE 2#1000
107 var C = xchar();
108 if(member(C, "()<>/% \t\n")) throw new Error("Symbol expected, got " + C);
109 var N = member(C, "+-0123456789.");
110 var F = "." == C;
111 var L = [C];
112 while(peek() && !member(peek(), "()<>[]{}/% \t\n")) {
113 C = xchar();
114 L.push(C);
115 if(N && !member(C, "0123456789")) {
116 if(!F && "." == C) F = true;
117 else N = false;
118 }
119 }
120 L = L.join("");
121 if(1 == L.length && member(L, "+-.")) N = false;
122 return N ? (F ? parseFloat(L) : parseInt(L, 10)) : new Symbol(L);
123 }
124 function token() {
125 skip();
126 switch(peek()) { // TODO read dict in <> <~~> <<>> immediate literal //
127 case false: return undefined;
128 case "%": return comment();
129 case "[": return new Symbol(xchar());
130 case "]": return new Symbol(xchar());
131 case "{": Self.D++; return new Symbol(xchar());
132 case "}": Self.D--; return new Symbol(xchar());
133 case "/": xchar(); var X = symbol(); return quote(X);
134 case "(": return text();
135 case "<":
136 xchar();
137 if("<" != peek()) throw new Error("Encoded strings not implemented yet");
138 xchar();
139 return new Symbol("<<");
140 case ">":
141 xchar();
142 if(">" != peek()) throw new Error("Unexpected >");
143 xchar();
144 return new Symbol(">>");
145 default: return symbol();
146 }
147 }
148 PsParser.prototype.init = init;
149 PsParser.prototype.peek = peek;
150 PsParser.prototype.token = token;
151 return this;
152 }
153
154 function Ps0(Os, Ds, Es) {
155 function run(X, Z) {
156 if(isSymbol(X) && !isQuoted(X)) { // executable name
157 var K = symbolName(X);
158 var D = inDs(Ds, K);
159 if(!D)
160 throw new Error("bind error '" + K + "'");
161 Es.push([false, D[K]]);
162 } else if(Z && isArray(X) && isQuoted(X)) { // proc from Es
163 if(0 < X.length) {
164 var F = X[0];
165 var R = quote(X.slice(1));
166 if(0 < R.length) Es.push([false, R]);
167 run(F, false);
168 }
169 } else if("function" == typeof X) X(); // operator
170 else Os.push(X);
171 }
172 function exec() {
173 var X = Os.pop();
174 run(X, false);
175 }
176 function step() {
177 var C = Es.pop();
178 var L = C.shift(); // TODO use for 'exit'
179 var X = C.pop();
180 for(var I = 0; I < C.length; I++)
181 Os.push(C[I]);
182 run(X, true);
183 }
184 var PsP = new PsParser;
185 function parse(L) {
186 PsP.init(L);
187 while(PsP.peek()) {
188 var T = PsP.token();
189 if(T || T === 0) {
190 Os.push(T);
191 if(PsP.D <= 0 || isSymbol(T) &&
192 (member(symbolName(T), "[]{}") ||
193 "<<" == symbolName(T) || ">>" == symbolName(T))) {
194 exec();
195 while(0 < Es.length)
196 step();
197 }
198 }
199 }
200 return Os;
201 }
202 Ps0.prototype.run = run;
203 Ps0.prototype.exec = exec;
204 Ps0.prototype.step = step;
205 Ps0.prototype.parse = parse;
206 return this;
207 }
208
209 function Wps(psvg_i) {
210 var psvg = psvg_i;
211 var Os = [];
212 var Sd = {};
213 var Ds = [Sd];
214 var Es = [];
215 var Ps = new Ps0(Os, Ds, Es);
216
217 // trivial
218 Sd["true"] = function() {Os.push(true);};
219 Sd["false"] = function() {Os.push(false);};
220 Sd["null"] = function() {Os.push(null);};
221 // math
222 Sd["sub"] = function() {var X = Os.pop(); Os.push(Os.pop() - X);};
223 Sd["mul"] = function() {Os.push(Os.pop() * Os.pop());};
224 Sd["div"] = function() {var X = Os.pop(); Os.push(Os.pop() / X);};
225 Sd["mod"] = function() {var X = Os.pop(); Os.push(Os.pop() % X);};
226 // stack
227 var M = {};
228 Sd["mark"] = function() {Os.push(M);};
229 Sd["counttomark"] = function() {
230 var N = 0;
231 for(var I = Os.length - 1; 0 <= I; I--)
232 if(M === Os[I]) return Os.push(N);
233 else N++;
234 throw new Error("Mark not found");
235 };
236 Sd["<<"] = Sd["mark"]; // TODO doc
237 Sd[">>"] = function() { // TODO doc
238 var D = {};
239 while(0 < Os.length) {
240 var V = Os.pop();
241 if(M === V) return Os.push(D);
242 D[Os.pop()] = V;
243 }
244 throw new Error("Mark not found");
245 };
246 Sd["exch"] = function() {
247 var Y = Os.pop();
248 var X = Os.pop();
249 Os.push(Y);
250 Os.push(X);
251 };
252 Sd["clear"] = function() {Os.length = 0;};
253 Sd["pop"] = function() {Os.pop();};
254 Sd["index"] = function() {
255 Os.push(Os[Os.length - 2 - Os.pop()]);
256 };
257 Sd["roll"] = function() { // TODO in ps
258 var J = Os.pop();
259 var N = Os.pop();
260 var X = [];
261 var Y = [];
262 //jfm patch
263 if (J < 0)
264 J = N + J
265 for(var I = 0; I < N; I++)
266 if(I < J) X.unshift(Os.pop());
267 else Y.unshift(Os.pop());
268 for(I = 0; I < J; I++) Os.push(X.shift());
269 for(I = 0; I < N - J; I++) Os.push(Y.shift());
270 };
271 Sd["copy"] = function() {
272 var N = Os.pop();
273 if("object" == typeof N) {
274 var X = Os.pop();
275 for(var I in X)
276 N[I] = X[I];
277 Os.push(N);
278 } else {
279 var X = Os.length - N;
280 for(var I = 0; I < N; I++)
281 Os.push(Os[X + I]);
282 }
283 };
284 // array
285 Sd["length"] = function() {Os.push(Os.pop().length);};
286 Sd["astore"] = function() {
287 var A = Os.pop();
288 var N = A.length;
289 for(var I = N - 1; 0 <= I; I--)
290 A[I] = Os.pop();
291 Os.push(A);
292 };
293 Sd["array"] = function() {Os.push(new Array(Os.pop()));};
294 // conditionals
295 Sd["eq"] = function() {var Y = Os.pop(); var X = Os.pop(); Os.push(X == Y);};
296 Sd["lt"] = function() {var Y = Os.pop(); var X = Os.pop(); Os.push(X < Y);};
297 // control
298 Sd["ifelse"] = function() {
299 var N = Os.pop();
300 var P = Os.pop();
301 var C = Os.pop();
302 Es.push([false, C === true ? P : N]);
303 };
304 //jfm++
305 Sd["and"] = function() {
306 var A = Os.pop();
307 var B = Os.pop();
308 if (true === A || false === A) {
309 Os.push(A == true && B === true)
310 return
311 }
312 Os.push(A & B)
313 }
314 //jfm--
315 Sd["repeat"] = function Xrepeat() { // TODO in ps
316 var B = Os.pop();
317 var N = Os.pop();
318 if(1 < N) Es.push([true, N - 1, B, Xrepeat]);
319 if(0 < N) Es.push([false, B]);
320 };
321 Sd["for"] = function Xfor() { // TODO in ps
322 var B = Os.pop();
323 var L = Os.pop();
324 var K = Os.pop();
325 var J = Os.pop();
326 if(K < 0) {
327 if(L <= J + K) Es.push([true, J + K, K, L, B, Xfor]);
328 if(L <= J) Es.push([false, J, B]);
329 } else {
330 if(J + K <= L) Es.push([true, J + K, K, L, B, Xfor]);
331 if(J <= L) Es.push([false, J, B]);
332 }
333 };
334 Sd["exec"] = function() {Es.push([false, Os.pop()]);};
335 Sd["cvx"] = function() {
336 var X = Os.pop();
337 if(isSymbol(X) && isQuoted(X)) Os.push(unquote(X)); // executable name
338 else if(isArray(X) && !isQuoted(X)) Os.push(quote(X)); // proc
339 // TODO string -> parse
340 else Os.push(X);
341 };
342 // dictionary
343 Sd["dict"] = function() {Os.pop(); Os.push({});};
344 Sd["get"] = function() {
345 var K = Os.pop();
346 var D = Os.pop();
347 // TODO other datatypes
348 if(isSymbol(K)) Os.push(D[symbolName(K)]);
349 else Os.push(D[K]);
350 };
351 Sd["getinterval"] = function() {
352 var N = Os.pop(),
353 K = Os.pop() + N,
354 D = Os.pop(),
355 A = []
356 while (--N >= 0)
357 A.push(D[K++])
358 Os.push(A);
359 };
360 Sd["put"] = function() {
361 var V = Os.pop();
362 var K = Os.pop();
363 var D = Os.pop();
364 // TODO other datatypes
365 if(isSymbol(K)) D[symbolName(K)] = V;
366 else D[K] = V;
367 };
368 Sd["begin"] = function() {Ds.push(Os.pop());};
369 Sd["end"] = function() {Ds.pop();};
370 Sd["currentdict"] = function() {Os.push(Ds[Ds.length - 1]);};
371 Sd["where"] = function() {
372 var K = symbolName(Os.pop());
373 var D = inDs(Ds, K);
374 if(D) {
375 Os.push(D);
376 Os.push(true);
377 } else Os.push(false);
378 };
379 // miscellaneous
380 Sd["save"] = function() {
381 var X = Ds.slice();
382 for(var I = 0; I < X.length; I++) {
383 var A = X[I];
384 var B = {};
385 for(var J in A)
386 B[J] = A[J];
387 X[I] = B;
388 }
389 Os.push(X);
390 };
391 Sd["restore"] = function() {
392 var X = Os.pop();
393 while(0 < Ds.length)
394 Ds.pop();
395 while(0 < X.length)
396 Ds.unshift(X.pop());
397 };
398 Sd["type"] = function() {
399 var A = Os.pop();
400 var X;
401 if(null === A) X = "nulltype";
402 else if(true === A || false === A) X = "booleantype";
403 else if(M === A) X = "marktype";
404 else if("string" == typeof A) X = "stringtype";
405 else if(isSymbol(A)) X = isQuoted(A) ? "nametype" : "operatortype";
406 else if("function" == typeof A) X = "operatortype";
407 else if(isArray(A)) X = "arraytype";
408 else if("object" == typeof A) X = "dicttype";
409 else if(1 * A == A) X = A % 1 == 0 ? "integertype" : "realtype";
410 else throw new Error("Undefined type '" + A + "'");
411 Os.push(X);
412 // filetype
413 // packedarraytype (LanguageLevel 2)
414 // fonttype
415 // gstatetype (LanguageLevel 2)
416 // savetype
417 };
418 var Sb = true;
419 Sd[".strictBind"] = function() {Sb = true === Os.pop();};
420 Sd["bind"] = function() {Os.push(bind(Os.pop()));};
421 function bind(X) {
422 if(isSymbol(X) && !isQuoted(X)) {
423 //jfm++
424 // var K = symbolName(X);
425 // var D = inDs(Ds, K);
426 // if(Sb) {
427 // if(!D)
428 // throw new Error("bind error '" + K + "'");
429 // return bind(D[K]);
430 // } else return !D ? X : bind(D[K]);
431 return X
432 //jfm--
433 } else if(isArray(X) && isQuoted(X)) {
434 var N = X.length;
435 var A = [];
436 for(var I = 0; I < N; I++) {
437 var Xi = X[I];
438 var Xb = bind(Xi);
439 if(isArray(Xi))
440 A = A.concat(isQuoted(Xi) ? quote([Xb]) : [Xb]);
441 else
442 A = A.concat(Xb);
443 }
444 return quote(A);
445 }
446 return X;
447 }
448 // debugging
449 Sd["="] = function() {var X = Os.pop(); alert(X && X.nm || X);}; // TODO
450 Sd["=="] = function() {alert(Os.pop());}; // TODO
451 Sd["stack"] = function() {alert(Os);}; // TODO
452 Sd["pstack"] = function() {alert(Os);}; // TODO
453 // js ffi
454 Sd[".call"] = function() {
455 var N = Os.pop();
456 var K = Os.pop();
457 var D = Os.pop();
458 var X = [];
459 for(var I = 0; I < N; I++) X.unshift(Os.pop());
460 if (!D[K]) throw new Error(".call: " + K + " undef")
461 Os.push(D[K].apply(D, X));
462 };
463 //jfm++
464 Sd[".call0"] = function() {
465 var N = Os.pop(),
466 K = Os.pop(),
467 D = Os.pop(),
468 X = []
469 for(var I = 0; I < N; I++) X.unshift(Os.pop());
470 if (!D[K]) throw new Error(".call0: " + K + " undef")
471 D[K].apply(D, X);
472 };
473 Sd[".svg"] = function() {Os.push(psvg)};
474 //jfm--
475 Sd[".math"] = function() {Os.push(Math);};
476 Sd[".date"] = function() {Os.push(new Date());}; // TODO split new and Date
477 Sd[".window"] = function() {Os.push(window);};
478 Sd[".callback"] = function() { // TODO event arg?
479 var X = Os.pop();
480 Os.push(function() {
481 Ps.run(X, true);
482 while(0 < Es.length)
483 Ps.step();
484 });
485 };
486 // html5
487 Sd[".minv"] = function() { // TODO in ps
488 var M = Os.pop();
489 var a = M[0]; var b = M[1];
490 var d = M[2]; var e = M[3];
491 var g = M[4]; var h = M[5];
492 Os.push([e, b, d, a, d*h-e*g, b*g-a*h]);
493 };
494 Sd[".mmul"] = function() { // TODO in ps
495 var B = Os.pop();
496 var A = Os.pop();
497 var a = A[0]; var b = A[1];
498 var d = A[2]; var e = A[3];
499 var g = A[4]; var h = A[5];
500 var r = B[0]; var s = B[1];
501 var u = B[2]; var v = B[3];
502 var x = B[4]; var y = B[5];
503 Os.push([a*r+b*u, a*s+b*v, d*r+e*u, d*s+e*v, g*r+h*u+x, g*s+h*v+y]);
504 };
505 Sd[".xy"] = function() { // TODO in ps
506 var M = Os.pop();
507 var Y = Os.pop();
508 var X = Os.pop();
509 Os.push(M[0] * X + M[2] * Y + M[4]);
510 Os.push(M[1] * X + M[3] * Y + M[5]);
511 };
512 // TODO js ffi to manipulate strings so the following can be in ps
513 Sd[".rgb"] = function() { // TODO in ps
514 var B = Os.pop();
515 var G = Os.pop();
516 var R = Os.pop();
517 Os.push("rgb(" + R + "," + G + "," + B + ")");
518 };
519 Sd[".rgba"] = function() { // TODO in ps
520 var A = Os.pop();
521 var B = Os.pop();
522 var G = Os.pop();
523 var R = Os.pop();
524 Os.push("rgba(" + R + "," + G + "," + B + "," + A + ")");
525 };
526
527 function parse() {
528 var T = arguments;
529 if(T.length)
530 for(var I = 0; I < T.length; I++)
531 Ps.parse(T[I]);
532 else Ps.parse(T);
533 return Os;
534 }
535 Wps.prototype.parse = parse;
536 return this;
537 }
538 // psvg.js - small PS to SVG convertor for abc2svg
539
540 // Copyright (C) 2014-2023 Jean-Francois Moine
541 //
542 // This file is part of abc2svg.
543 //
544 // abc2svg is free software: you can redistribute it and/or modify
545 // it under the terms of the GNU Lesser General Public License as published by
546 // the Free Software Foundation, either version 3 of the License, or
547 // (at your option) any later version.
548 //
549 // abc2svg is distributed in the hope that it will be useful,
550 // but WITHOUT ANY WARRANTY; without even the implied warranty of
551 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
552 // GNU Lesser General Public License for more details.
553 //
554 // You should have received a copy of the GNU Lesser General Public License
555 // along with abc2svg. If not, see <http://www.gnu.org/licenses/>.
556
557 if (typeof abc2svg == "undefined")
558 var abc2svg = {}
559
560 function Psvg(abcobj_r) {
561 var svgbuf = '',
562
563 // SVG functions - adapted from abcm2ps svg.c
564 abcobj = abcobj_r,
565 wps = new Wps(this),
566 g = 0, // graphic state
567 gchg, // graphic change
568 gcur = { // current graphic context
569 cx:0,
570 cy:0,
571 xoffs:0,
572 yoffs:0,
573 xscale: 1,
574 yscale: 1,
575 rotate: 0,
576 sin: 0,
577 cos: 1,
578 linewidth: 0.7,
579 dash: ''
580 },
581 gc_stack = [], // graphic stack
582 x_rot = 0,
583 y_rot = 0,
584 font_n = "",
585 font_n_old = "",
586 font_s = 0,
587 path;
588
589 // function called from Abc
590 function getorig() {
591 setg(0);
592 return [gcur.xoffs - gcur.xorig, gcur.yoffs - gcur.yorig]
593 }
594
595 function defg1() {
596 gchg = false;
597 setg(0);
598 svgbuf += "<g"
599 if (gcur.xscale != 1 || gcur.yscale != 1 || gcur.rotate) {
600 svgbuf += ' transform="'
601 if (gcur.xscale != 1 || gcur.yscale != 1) {
602 if (gcur.xscale == gcur.yscale)
603 svgbuf += "scale(" + gcur.xscale.toFixed(3) + ")"
604 else
605 svgbuf += "scale(" + gcur.xscale.toFixed(3) +
606 "," + gcur.yscale.toFixed(3) + ")"
607 }
608 if (gcur.rotate) {
609 if (gcur.xoffs || gcur.yoffs) {
610 var x, xtmp = gcur.xoffs,
611 y = gcur.yoffs,
612 _sin = gcur.sin,
613 _cos = gcur.cos;
614 x = xtmp * _cos - y * _sin;
615 y = xtmp * _sin + y * _cos;
616 svgbuf += " translate(" + x.toFixed(1) + "," +
617 y.toFixed(1) + ")";
618 x_rot = gcur.xoffs;
619 y_rot = gcur.yoffs;
620 gcur.xoffs = 0;
621 gcur.yoffs = 0
622 }
623 svgbuf += " rotate(" + gcur.rotate.toFixed(2) + ")"
624 }
625 svgbuf += '"'
626 }
627 output_font(false)
628 if (gcur.rgb)
629 svgbuf += ' style="color:' + gcur.rgb + '"';
630 svgbuf += ">\n";
631 g = 1
632 }
633
634 function objdup(obj) {
635 var k, tmp = new obj.constructor()
636 for (k in obj)
637 if (obj.hasOwnProperty(k))
638 tmp[k] = obj[k]
639 return tmp
640 }
641
642 function output_font(back) {
643 var name = gcur.font_n
644 if (!name)
645 return
646 var prop = "",
647 i = name.indexOf("Italic"),
648 j = 100,
649 o = name.indexOf("Oblique"),
650 b = name.indexOf("Bold"),
651 flags = 0
652 if (b > 0) {
653 prop = ' font-weight="bold"';
654 j = b;
655 flags = 2
656 }
657 if (i > 0 || o > 0) {
658 if (i > 0) {
659 prop += ' font-style="italic"';
660 if (i < j)
661 j = i;
662 flags |= 4
663 }
664 if (o > 0) {
665 prop += ' font-style="oblique"';
666 if (o < j)
667 j = o;
668 flags = 8
669 }
670 }
671 if (j != 100) {
672 if (name[j - 1] == '-')
673 j--;
674 name = name.slice(0, j)
675 }
676
677 if (back) {
678 if (!(flags & 2)
679 && font_n_old.indexOf("Bold") >= 0)
680 prop += ' font-weight="normal"';
681 if (!(flags & 12)
682 && (font_n_old.indexOf("Italic") >= 0
683 || font_n_old.indexOf("Oblique") >= 0))
684 prop += ' font-style="normal"'
685 }
686 svgbuf += ' font-family="' + name + '"' +
687 prop + ' font-size="' + gcur.font_s + '"'
688 }
689
690 function path_def() {
691 if (path)
692 return
693 setg(1);
694 gcur.px = gcur.cx;
695 gcur.py = gcur.cy;
696 path = '<path d="m' + (gcur.xoffs + gcur.cx).toFixed(1) +
697 ' ' + (gcur.yoffs - gcur.cy).toFixed(1) + '\n'
698 }
699
700 function path_end() {
701 svgbuf += path;
702 path = ''
703 }
704
705 function setg(newg) {
706 if (g == 2) {
707 svgbuf += "</text>\n";
708 g = 1
709 }
710 if (newg == 0) {
711 if (g) {
712 g = 0;
713 svgbuf += "</g>\n"
714 if (gcur.rotate) {
715 gcur.xoffs = x_rot;
716 gcur.yoffs = y_rot;
717 x_rot = 0;
718 y_rot = 0
719 }
720 }
721 } else if (gchg) {
722 defg1()
723 }
724 }
725
726 function strw(s) {
727 return s.length * gcur.font_s * 0.5 // fixme: approximate value
728 }
729 Psvg.prototype.strw = strw;
730
731 // graphic functions called from wps.js
732 function arc(x, y, r, a1, a2, arcn) {
733 var x1, y1, x2, y2
734 if (a1 >= 360)
735 a1 -= 360
736 if (a2 >= 360)
737 a2 -= 360;
738 x1 = x + r * Math.cos(a1 * Math.PI / 180);
739 y1 = y + r * Math.sin(a1 * Math.PI / 180)
740
741 if (gcur.cx != undefined) {
742 if (path) {
743 if (x1 != gcur.cx || y1 != gcur.cy)
744 path += 'l'
745 else
746 path += 'm';
747 path += (x1 - gcur.cx).toFixed(1) + " " +
748 (-(y1 - gcur.cy)).toFixed(1)
749 } else {
750 gcur.cx = x1;
751 gcur.cy = y1;
752 path_def()
753 }
754 } else {
755 if (path) // should not occur
756 path = ''
757 gcur.cx = x1;
758 gcur.cy = y1;
759 path_def()
760 }
761
762 if (a1 == a2) { /* circle */
763 a2 = 180 - a1;
764 x2 = x + r * Math.cos(a2 * Math.PI / 180);
765 y2 = y + r * Math.sin(a2 * Math.PI / 180);
766 path += 'a' + r.toFixed(2) + ' ' + r.toFixed(2) + ' 0 0 ' +
767 (arcn ? '1 ' : '0 ') +
768 (x2 - x1).toFixed(2) + ' ' +
769 (y1 - y2).toFixed(2) + ' ' +
770 r.toFixed(2) + ' ' + r.toFixed(2) + ' 0 0 ' +
771 (arcn ? '1 ' : '0 ') +
772 (x1 - x2).toFixed(2) + ' ' +
773 (y2 - y1).toFixed(2) + '\n';
774 gcur.cx = x1;
775 gcur.cy = y1
776 } else {
777 x2 = x + r * Math.cos(a2 * Math.PI / 180);
778 y2 = y + r * Math.sin(a2 * Math.PI / 180);
779 path += 'a' + r.toFixed(2) + ' ' + r.toFixed(2) + ' 0 0 ' +
780 (arcn ? '1 ' : '0 ') +
781 (x2 - x1).toFixed(2) + ' ' +
782 (y1 - y2).toFixed(2) + '\n';
783 gcur.cx = x2;
784 gcur.cy = y2
785 }
786 }
787 Psvg.prototype.arc = arc
788
789 Psvg.prototype.arcn = function(x, y, r, a1, a2) {
790 arc(x, y, r, a1, a2, true)
791 }
792
793 Psvg.prototype.closepath = function() {
794 if (path && gcur.cx)
795 rlineto(gcur.px - gcur.cx, gcur.py - gcur.cy)
796 }
797
798 Psvg.prototype.cx = function() {
799 return gcur.cx
800 }
801
802 Psvg.prototype.cy = function() {
803 return gcur.cy
804 }
805
806 Psvg.prototype.curveto = function(x1, y1, x2, y2, x, y) {
807 path_def();
808 path += "\tC" +
809 (gcur.xoffs + x1).toFixed(1) + " " + (gcur.yoffs - y1).toFixed(1) + " " +
810 (gcur.xoffs + x2).toFixed(1) + " " + (gcur.yoffs - y2).toFixed(1) + " " +
811 (gcur.xoffs + x).toFixed(1) + " " + (gcur.yoffs - y).toFixed(1) + "\n";
812 gcur.cx = x;
813 gcur.cy = y
814 }
815
816 Psvg.prototype.eofill = function() {
817 path_end();
818 svgbuf += '" fill-rule="evenodd" fill="currentColor"/>\n'
819 }
820
821 Psvg.prototype.fill = function() {
822 path_end();
823 svgbuf += '" fill="currentColor"/>\n'
824 }
825
826 Psvg.prototype.gsave = function() {
827 gc_stack.push(objdup(gcur))
828 }
829
830 Psvg.prototype.grestore = function() {
831 gcur = gc_stack.pop();
832 gchg = true
833 }
834
835 Psvg.prototype.lineto = function(x, y) {
836 path_def()
837 if (x == gcur.cx)
838 path += "\tv" + (gcur.cy - y).toFixed(1) + "\n"
839 else if (y == gcur.cy)
840 path += "\th" + (x - gcur.cx).toFixed(1) + "\n"
841 else
842 path += "\tl" + (x - gcur.cx).toFixed(1) + " " +
843 (gcur.cy - y).toFixed(1) + "\n";
844 gcur.cx = x;
845 gcur.cy = y
846 }
847
848 Psvg.prototype.moveto = function(x, y) {
849 gcur.cx = x;
850 gcur.cy = y
851 if (path) {
852 path += "\tM" + (gcur.xoffs + gcur.cx).toFixed(1) + " " +
853 (gcur.yoffs - gcur.cy).toFixed(1) + "\n"
854 } else if (g == 2) {
855 svgbuf += "</text>\n";
856 g = 1
857 }
858 }
859
860 Psvg.prototype.newpath = function() {
861 gcur.cx = gcur.cy = undefined
862 }
863
864 Psvg.prototype.rcurveto = function(x1, y1, x2, y2, x, y) {
865 path_def();
866 path += "\tc" +
867 x1.toFixed(1) + " " + (-y1).toFixed(1) + " " +
868 x2.toFixed(1) + " " + (-y2).toFixed(1) + " " +
869 x.toFixed(1) + " " + (-y).toFixed(1) + "\n";
870 gcur.cx += x;
871 gcur.cy += y
872 }
873
874 function rlineto(x, y) {
875 path_def()
876 if (x == 0)
877 path += "\tv" + (-y).toFixed(1) + "\n"
878 else if (y == 0)
879 path += "\th" + x.toFixed(1) + "\n"
880 else
881 path += "\tl" + x.toFixed(1) + " " +
882 (-y).toFixed(1) + "\n";
883 gcur.cx += x;
884 gcur.cy += y
885 }
886 Psvg.prototype.rlineto = rlineto;
887
888 Psvg.prototype.rmoveto = function(x, y) {
889 if (path) {
890 path += "\tm" + x.toFixed(1) + " " +
891 (-y).toFixed(1) + "\n"
892 } else if (g == 2) {
893 svgbuf += "</text>\n";
894 g = 1
895 }
896 gcur.cx += x;
897 gcur.cy += y
898 }
899
900 Psvg.prototype.rotate = function(a) {
901 setg(0)
902
903 // convert orig and currentpoint coord to absolute coord
904 var x, xtmp = gcur.xoffs,
905 y = gcur.yoffs,
906 _sin = gcur.sin,
907 _cos = gcur.cos;
908 x = xtmp * _cos - y * _sin;
909 y = xtmp * _sin + y * _cos;
910 gcur.xoffs = x / gcur.xscale;
911 gcur.yoffs = y / gcur.yscale;
912
913 xtmp = gcur.cx;
914 y = gcur.cy;
915 x = xtmp * _cos - y * _sin;
916 y = -xtmp * _sin + y * _cos;
917 gcur.cx = x / gcur.xscale;
918 gcur.cy = y / gcur.yscale;
919
920 // rotate
921 a = 360 - a;
922 gcur.rotate += a
923 if (gcur.rotate > 180)
924 gcur.rotate -= 360
925 else if (gcur.rotate <= -180)
926 gcur.rotate += 360
927 a = gcur.rotate * Math.PI / 180;
928 gcur.sin = _sin = Math.sin(a);
929 gcur.cos = _cos = Math.cos(a);
930 x = gcur.cx;
931 gcur.cx = (x * _cos + gcur.cy * _sin) * gcur.xscale;
932 gcur.cy = (-x * _sin + gcur.cy * _cos) * gcur.yscale;
933 x = gcur.xoffs;
934 gcur.xoffs = (x * _cos + gcur.yoffs * _sin) *
935 gcur.xscale;
936 gcur.yoffs = (-x * _sin + gcur.yoffs * _cos) *
937 gcur.yscale;
938 gchg = true
939 }
940
941 Psvg.prototype.scale = function(sx, sy) {
942 gcur.xoffs /= sx;
943 gcur.yoffs /= sy;
944 gcur.cx /= sx;
945 gcur.cy /= sy;
946 gcur.xscale *= sx;
947 gcur.yscale *= sy;
948 gchg = true
949 }
950
951 Psvg.prototype.selectfont = function(s, h) {
952 s = s.nm; // Symbol
953 if (font_s != h || s != font_n) {
954 gcur.font_n_old = gcur.font_n;
955 gcur.font_n = s;
956 gcur.font_s = h;
957 gchg = true
958 }
959 }
960
961 Psvg.prototype.setdash = function(a, o) {
962 var n = a.length, i
963 if (n == 0) {
964 gcur.dash= ''
965 return
966 }
967 gcur.dash = ' stroke-dashoffset="' + o + '" stroke-dasharray="';
968 i = 0
969 while (1) {
970 gcur.dash += a[i]
971 if (--n == 0)
972 break
973 gcur.dash += ' '
974 }
975 gcur.dash += '"'
976 }
977
978 Psvg.prototype.setlinewidth = function(w) {
979 gcur.linewidth = w
980 }
981
982 Psvg.prototype.setrgbcolor = function(r, g, b) {
983 var rgb = 0x1000000 +
984 (Math.floor(r * 255) << 16) +
985 (Math.floor(g * 255) << 8) +
986 Math.floor(b * 255);
987 rgb = rgb.toString(16);
988 rgb = rgb.replace('1', '#')
989 if (rgb != gcur.rgb) {
990 gcur.rgb = rgb;
991 gchg = true
992 }
993 }
994
995 Psvg.prototype.show = function(s) {
996 var span, x, y
997 if (gchg) {
998 if (g == 2)
999 span = true
1000 else
1001 defg1()
1002 }
1003 x = gcur.cx;
1004 y = gcur.cy
1005 if (span) {
1006 svgbuf += "<tspan\n\t";
1007 output_font(true);
1008 svgbuf += ">"
1009 } else if (g != 2) {
1010 if (!g)
1011 defg1()
1012 svgbuf += '<text x="' + (x + gcur.xoffs).toFixed(1) + '" y="' +
1013 (gcur.yoffs - y).toFixed(1) + '">';
1014 g = 2
1015 }
1016 svgbuf += s.replace(/<|>|&/g, function(c){
1017 switch (c) {
1018 case '<': return "&lt;"
1019 case '>': return "&gt;"
1020 case '&': return "&amp;"
1021 }
1022 })
1023 if (span)
1024 svgbuf += "</tspan>";
1025 gcur.cx = x + strw(s)
1026 }
1027
1028 Psvg.prototype.stroke = function() {
1029 path_end()
1030 if (gcur.linewidth != 0.7)
1031 svgbuf += '" stroke-width="' + gcur.linewidth.toFixed(1);
1032 svgbuf += '" stroke="currentColor" fill="none"' + gcur.dash + '/>\n'
1033 }
1034
1035 Psvg.prototype.translate = function(x, y) {
1036 gcur.xoffs += x;
1037 gcur.yoffs -= y;
1038 gcur.cx -= x;
1039 gcur.cy -= y
1040 }
1041
1042 // abcm2ps functions
1043 Psvg.prototype.arp = function(val, x, y) {
1044 var xy = getorig();
1045 ps_flush();
1046 abcobj.out_arp((x + xy[0]) * abcobj.stv_g().scale, y - xy[1], val)
1047 }
1048 Psvg.prototype.ltr = function(val, x, y) {
1049 var xy = getorig();
1050 ps_flush();
1051 abcobj.out_ltr((x + xy[0]) * abcobj.stv_g().scale, y - xy[1], val)
1052 }
1053 Psvg.prototype.xygl = function(x, y, gl) {
1054 var xy = getorig();
1055 ps_flush();
1056 abcobj.xygl((x + xy[0]) * abcobj.stv_g().scale, y - xy[1], gl)
1057 }
1058 Psvg.prototype.xygls = function(str, x, y, gl) {
1059 var xy = getorig();
1060 ps_flush();
1061 abcobj.out_deco_str((x + xy[0]) * abcobj.stv_g().scale, y - xy[1], gl, str)
1062 }
1063 Psvg.prototype.xyglv = function(val, x, y, gl) {
1064 var xy = getorig();
1065 ps_flush();
1066 abcobj.out_deco_val((x + xy[0]) * abcobj.stv_g().scale, y - xy[1], gl, val)
1067 }
1068 Psvg.prototype.y0 = function(y) {
1069 var staff_tb = abcobj.get_staff_tb()
1070 return y + staff_tb[0].y
1071 }
1072 Psvg.prototype.y1 = function(y) {
1073 var staff_tb = abcobj.get_staff_tb()
1074 return y + staff_tb[1].y
1075 }
1076
1077 // flush the PS buffer
1078 function ps_flush(g0) {
1079 if (g0)
1080 setg(0);
1081 if (!svgbuf)
1082 return
1083 abcobj.out_svg(svgbuf);
1084 svgbuf = ''
1085 }
1086 Psvg.prototype.ps_flush = ps_flush
1087
1088 // evaluate a PS user sequence (%beginps .. %%endps)
1089 Psvg.prototype.ps_eval = function(txt) {
1090 wps.parse(txt);
1091 ps_flush(true)
1092 }
1093
1094 // ------ output builtin decorations
1095 // common part
1096 function pscall(f, x, y, script) {
1097 gcur.xorig = gcur.xoffs = abcobj.sx(0);
1098 gcur.yorig = gcur.yoffs = abcobj.sy(0);
1099 gcur.cx = 0;
1100 gcur.cy = 0;
1101 wps.parse(script +
1102 (x / abcobj.stv_g().scale).toFixed(1) + ' ' + y.toFixed(1) + ' ' + f);
1103 ps_flush(true) // + setg(0)
1104 return true
1105 }
1106
1107 // try to generate a decoration by PS
1108 Psvg.prototype.psdeco = function(x, y, de) {
1109 var dd, de2, script, defl,
1110 f = de.dd.glyph,
1111 Os = wps.parse('/' + f + ' where'),
1112 A = Os.pop(),
1113 staff_tb = abcobj.get_staff_tb()
1114
1115 if (!A)
1116 return false;
1117 defl = 0
1118 if (de.defl.nost)
1119 defl = 1
1120 if (de.defl.noen)
1121 defl |= 2
1122 if (de.s.stem >= 0)
1123 defl |= 4;
1124
1125 Os.pop();
1126 script = '/defl ' + defl + ' def '
1127 if (de.lden) {
1128 script += x.toFixed(1) + ' ' + y.toFixed(1) + ' ';
1129 de2 = de.start;
1130 x = de2.x;
1131 y = de2.y + staff_tb[de2.st].y
1132 if (x > de.x - 20)
1133 x = de.x - 20
1134 }
1135 dd = de.dd
1136 if (de.has_val) {
1137 script += de.val + ' '
1138 } else if (dd.str) {
1139 script += '(' + dd.str + ') ';
1140 y += dd.h * 0.2
1141 }
1142 return pscall(f, x, y, script)
1143 }
1144
1145 // try to generate a glyph by PS
1146 Psvg.prototype.psxygl = function(x, y, gl) {
1147 var Os = wps.parse('/' + gl + ' where'),
1148 A = Os.pop()
1149 if (!A)
1150 return false
1151 Os.pop()
1152 return pscall(gl, x, y, 'dlw ')
1153 }
1154
1155 Psvg.prototype.svgcall = function(f, x, y, v1, v2) {
1156 var xy = getorig();
1157 ps_flush();
1158 f((x + xy[0]) * abcobj.stv_g().scale, y - xy[1], v1, v2)
1159 }
1160
1161 // initialize the PostScript functions
1162 wps.parse("\
1163 currentdict/systemdict currentdict put\n\
1164 systemdict/{/mark cvx put\n\
1165 systemdict/[/mark cvx put\n\
1166 systemdict/]\n\
1167 /counttomark cvx\n\
1168 /array cvx\n\
1169 /astore cvx\n\
1170 /exch cvx\n\
1171 /pop cvx\n\
1172 5 array astore cvx put\n\
1173 systemdict/}/] cvx/cvx cvx 2 array astore cvx put\n\
1174 systemdict/def{currentdict 2 index 2 index put pop pop}put\n\
1175 \n\
1176 /maxlength 1000 def % TODO\n\
1177 /.bdef{bind def}bind def\n\
1178 /.xdef{exch def}.bdef\n\
1179 /dup{0 index}.bdef\n\
1180 /load{dup where pop exch get}.bdef\n\
1181 /.ldef{load def}.bdef\n\
1182 /if{{}ifelse}.bdef\n\
1183 /cleartomark{array pop}.bdef\n\
1184 /known{exch begin where{currentdict eq}{false}if end}.bdef\n\
1185 /store{1 index where{3 1 roll put}{def}ifelse}.bdef\n\
1186 /not{{false}{true}ifelse}.bdef\n\
1187 %/.logand{{{true}{false}ifelse}{pop false}ifelse}.bdef\n\
1188 %/and/.logand .ldef % TODO numeric and\n\
1189 /.logor{{pop true}{{true}{false}ifelse}ifelse}.bdef\n\
1190 /or/.logor .ldef % TODO numeric or\n\
1191 /ne{eq not}.bdef\n\
1192 /ge{lt not}.bdef\n\
1193 /le{1 index 1 index eq 3 1 roll lt or}.bdef\n\
1194 /gt{le not}.bdef\n\
1195 /.repeat{1 1 4 2 roll for}.bdef\n\
1196 \n\
1197 %% math\n\
1198 \n\
1199 /floor{.math(floor)1 .call}.bdef\n\
1200 \n\
1201 /neg{0 exch sub}.bdef\n\
1202 /add{neg sub}.bdef\n\
1203 /idiv{div floor}.bdef\n\
1204 \n\
1205 /.pi{.math(PI)get}.bdef\n\
1206 \n\
1207 /abs{.math(abs)1 .call}.bdef\n\
1208 %/.acos{.math(acos)1 .call}.bdef\n\
1209 %/.asin{.math(asin)1 .call}.bdef\n\
1210 /atan{.math(atan2)2 .call 180 mul .pi div}.bdef\n\
1211 %/.atan2{.math(atan2)2 .call}.bdef\n\
1212 %/ceiling{.math(ceil)1 .call}.bdef\n\
1213 /cos{.pi mul 180 div .math(cos)1 .call}.bdef\n\
1214 %/.exp{.math(exp)1 .call}.bdef\n\
1215 %/log{.math(log)1 .call}.bdef\n\
1216 %/.max{.math(max)2 .call}.bdef\n\
1217 %/.min{.math(min)2 .call}.bdef\n\
1218 %/.pow{.math(pow)2 .call}.bdef\n\
1219 %/.random{.math(random)0 .call}.bdef\n\
1220 %/rand{.random}.bdef % TODO follow spec\n\
1221 %/round{.math(round)1 .call}.bdef\n\
1222 %/sin{.math(sin)1 .call}.bdef\n\
1223 %/sqrt{.math(sqrt)1 .call}.bdef\n\
1224 %/.tan{.math(tan)1 .call}.bdef\n\
1225 %/truncate{.math(truncate)1 .call}.bdef % TODO Math.truncate does not exist!\n\
1226 \n\
1227 % graphic\n\
1228 /arc{.svg(arc)5 .call0}.bdef\n\
1229 /arcn{.svg(arcn)5 .call0}.bdef\n\
1230 /closepath{.svg(closepath)0 .call}.bdef\n\
1231 /currentpoint{.svg(cx)0 .call .svg(cy)0 .call}.bdef\n\
1232 /curveto{.svg(curveto)6 .call0}.bdef\n\
1233 /eofill{.svg(eofill)0 .call0}.bdef\n\
1234 /fill{.svg(fill)0 .call0}.bdef\n\
1235 /grestore{.svg(grestore)0 .call0}.bdef\n\
1236 /gsave{.svg(gsave)0 .call0}.bdef\n\
1237 /lineto{.svg(lineto)2 .call0}.bdef\n\
1238 /moveto{.svg(moveto)2 .call0}.bdef\n\
1239 /newpath{.svg(newpath)0 .call0}.bdef\n\
1240 /rcurveto{.svg(rcurveto)6 .call0}.bdef\n\
1241 /rlineto{.svg(rlineto)2 .call0}.bdef\n\
1242 /rmoveto{.svg(rmoveto)2 .call0}.bdef\n\
1243 /rotate{.svg(rotate)1 .call0}.bdef\n\
1244 /scale{.svg(scale)2 .call0}.bdef\n\
1245 /selectfont{.svg(selectfont)2 .call0}.bdef\n\
1246 /setdash{.svg(setdash)2 .call0}.bdef\n\
1247 /setlinewidth{.svg(setlinewidth)1 .call0}.bdef\n\
1248 /setrgbcolor{.svg(setrgbcolor)3 .call0}.bdef\n\
1249 /show{.svg(show)1 .call0}.bdef\n\
1250 /stroke{.svg(stroke)0 .call0}.bdef\n\
1251 /stringwidth{.svg(strw)1 .call 1}.bdef %fixme: height KO\n\
1252 /translate{.svg(translate)2 .call0}.bdef\n\
1253 \n\
1254 /setgray{255 mul dup dup setrgbcolor}.bdef\n\
1255 % abcm2ps syms.c\n\
1256 /!{bind def}bind def\n\
1257 /T/translate load def\n\
1258 /M/moveto load def\n\
1259 /RM/rmoveto load def\n\
1260 /L/lineto load def\n\
1261 /RL/rlineto load def\n\
1262 /C/curveto load def\n\
1263 /RC/rcurveto load def\n\
1264 /SLW/setlinewidth load def\n\
1265 /defl 0 def\n\
1266 /dlw{0.7 SLW}!\n\
1267 /xymove{/x 2 index def/y 1 index def M}!\n\
1268 /showc{dup stringwidth pop .5 mul neg 0 RM show}!\n\
1269 %\n\
1270 % abcm2ps internal glyphs\n\
1271 /arp{.svg(arp)3 .call0}.bdef\n\
1272 /ltr{.svg(ltr)3 .call0}.bdef\n\
1273 /ft0{(acc-1).svg(xygl)3 .call0}.bdef\n\
1274 /nt0{(acc3).svg(xygl)3 .call0}.bdef\n\
1275 /sh0{(acc1).svg(xygl)3 .call0}.bdef\n\
1276 /dsh0{(acc2).svg(xygl)3 .call0}.bdef\n\
1277 /trl{(trl).svg(xygl)3 .call0}.bdef\n\
1278 /lmrd{(lmrd).svg(xygl)3 .call0}.bdef\n\
1279 /turn{(turn).svg(xygl)3 .call0}.bdef\n\
1280 /umrd{(umrd).svg(xygl)3 .call0}.bdef\n\
1281 /y0{.svg(y0)1 .call}.bdef\n\
1282 /y1{.svg(y1)1 .call}.bdef\n")
1283
1284 } // Psvg()
1285
1286 abc2svg.psvg = {
1287 do_begin_end: function(of, type, opt, text) {
1288 if (type != "ps") {
1289 of(type, opt, text)
1290 return
1291 }
1292 if (opt == 'nosvg')
1293 return
1294 if (!this.psvg)
1295 this.psvg = new Psvg(this);
1296 this.psvg.ps_eval.call(this.psvg, text)
1297 },
1298 psdeco: function(of, x, y, de) {
1299 if (!this.psvg) // no %%beginps yet
1300 return false
1301 return this.psvg.psdeco.call(this.psvg, x, y, de)
1302 },
1303 psxygl: function(of, x, y, gl) {
1304 if (!this.psvg)
1305 return false
1306 return this.psvg.psxygl.call(this.psvg, x, y, gl)
1307 },
1308
1309 set_hooks: function(abc) {
1310 abc.do_begin_end = abc2svg.psvg.do_begin_end.bind(abc, abc.do_begin_end);
1311 abc.psdeco = abc2svg.psvg.psdeco.bind(abc, abc.psdeco);
1312 abc.psxygl = abc2svg.psvg.psxygl.bind(abc, abc.psxygl)
1313 }
1314 } // psvg
1315
1316 if (!abc2svg.mhooks)
1317 abc2svg.mhooks = {}
1318 abc2svg.mhooks.psvg = abc2svg.psvg.set_hooks