-
Notifications
You must be signed in to change notification settings - Fork 31
/
svg.c
4653 lines (4510 loc) · 98.7 KB
/
svg.c
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SVG definitions.
*
* This file is part of abcm2ps.
*
* Copyright (C) 2011-2019 Jean-François Moine
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef NaN
#define NaN ((float) (2 << 22))
#endif
#include "abcm2ps.h"
enum elt_t { /* element types */
VAL,
STR,
SEQ, /* {..} */
BRK, /* [..] */
};
struct elt_s {
struct elt_s *next;
char type;
union {
float v;
char *s;
struct elt_s *e;
} u;
};
struct ps_sym_s {
char *n; /* name */
struct elt_s *e; /* value */
int exec; /* current number of execution */
};
/* -- PostScript tiny interpreter -- */
#define NELTS 2048 /* number of elements per block */
#define NSYMS 512 /* max number of symbols */
static struct elt_s *elts;
static struct elt_s *stack, *free_elt;
static struct ps_sym_s ps_sym[NSYMS];
static int n_sym;
static int ps_error;
static int in_cnt; /* in [..] or {..} */
static char *path;
static char path_buf[256];
/* graphical context */
static struct gc {
float cx, cy; // current point (volatile)
float xscale, yscale; // scale
float xoffs, yoffs; // translate
float rotate, sin, cos; // rotate
char *font_n; // current font
float font_s;
char *font_n_old;
float linewidth;
int rgb;
char dash[64];
} gcur, gold, gsave[8];
static int nsave;
static float x_rot, y_rot; /* save x and y offset when rotate != 0 */
static int g; /* current container */
static int boxend;
static char *defs; // SVG ID's from %%beginsvg
static int defssz;
/* abcm2ps definitions */
static struct {
char *def;
char use;
char defined;
} def_tb[] = {
#define D_brace 0
{ "<path id=\"brace\" class=\"fill\" d=\"m-2.5 101\n"
" c-4.5 -4.6 -7.5 -12.2 -4.4 -26.8\n"
" 3.5 -14.3 3.2 -21.7 -2.1 -24.2\n"
" 7.4 2.4 7.3 14.2 3.5 29.5\n"
" -2.7 9.5 -1.5 16.2 3 21.5\n"
" M-2.5 1c-4.5 4.6 -7.5 12.2 -4.4 26.8\n"
" c3.5 14.3 3.2 21.7 -2.1 24.2\n"
" 7.4 -2.4 7.3 -14.2 3.5 -29.5\n"
" -2.7 -9.5 -1.5 -16.2 3 -21.5\"/>\n"},
#define D_utclef 1
{ "<path id=\"utclef\" class=\"fill\" d=\"m-50 -90\n"
" c-72 -41 -72 -158 52 -188\n"
" 150 -10 220 188 90 256\n"
" -114 52 -275 0 -293 -136\n"
" -15 -181 93 -229 220 -334\n"
" 88 -87 79 -133 62 -210\n"
" -51 33 -94 105 -89 186\n"
" 17 267 36 374 49 574\n"
" 6 96 -19 134 -77 135\n"
" -80 1 -126 -93 -61 -133\n"
" 85 -41 133 101 31 105\n"
" 23 17 92 37 90 -92\n"
" -10 -223 -39 -342 -50 -617\n"
" 0 -90 0 -162 96 -232\n"
" 56 72 63 230 22 289\n"
" -74 106 -257 168 -255 316\n"
" 9 153 148 185 252 133\n"
" 86 -65 29 -192 -80 -176\n"
" -71 12 -105 67 -59 124\"/>\n"},
#define D_tclef 2
{ "<use id=\"tclef\" transform=\"translate(0,6) scale(0.045)\"\n"
" xlink:href=\"#utclef\"/>\n", D_utclef},
#define D_stclef 3
{ "<use id=\"stclef\" transform=\"translate(0,5.4) scale(0.037)\"\n"
" xlink:href=\"#utclef\"/>\n", D_utclef},
#define D_ubclef 4
{ "<path id=\"ubclef\" class=\"fill\" d=\"m-200 -87\n"
" c124 -35 222 -78 254 -236\n"
" 43 -228 -167 -246 -192 -103\n"
" 59 -80 157 22 92 78\n"
" -62 47 -115 -22 -106 -88\n"
" 21 -141 270 -136 274 52\n"
" -1 175 -106 264 -322 297\n"
" m357 -250\n"
" c0 -36 51 -34 51 0\n"
" 0 37 -51 36 -51 0\n"
" m-2 -129\n"
" c0 -36 51 -34 51 0\n"
" 0 38 -51 37 -51 0\"/>\n"},
#define D_bclef 5
{ "<use id=\"bclef\" transform=\"translate(0,18) scale(0.045)\"\n"
" xlink:href=\"#ubclef\"/>\n", D_ubclef},
#define D_sbclef 6
{ "<use id=\"sbclef\" transform=\"translate(0,14.5) scale(0.037)\"\n"
" xlink:href=\"#ubclef\"/>\n", D_ubclef},
#define D_ucclef 7
{ "<path id=\"ucclef\" class=\"fill\" d=\"\n"
" m-51 -264\n"
" v262\n"
" h-13\n"
" v-529\n"
" h13\n"
" v256\n"
" c25 -20 41 -36 63 -109\n"
" 14 31 13 51 56 70\n"
" 90 34 96 -266 -41 -185\n"
" 52 19 27 80 -11 77\n"
" -90 -38 33 -176 139 -69\n"
" 72 79 1 241 -134 186\n"
" l-16 39 16 38\n"
" c135 -55 206 107 134 186\n"
" -106 108 -229 -31 -139 -69\n"
" 38 -3 63 58 11 77\n"
" 137 81 131 -219 41 -185\n"
" -43 19 -45 30 -56 64\n"
" -22 -73 -38 -89 -63 -109\n"
" m-99 -267\n"
" h57\n"
" v529\n"
" h-57\n"
" v-529\"/>\n"},
#define D_cclef 8
{ "<use id=\"cclef\" transform=\"translate(0,12) scale(0.045)\"\n"
" xlink:href=\"#ucclef\"/>\n", D_ucclef},
#define D_scclef 9
{ "<use id=\"scclef\" transform=\"translate(0,9.5) scale(0.037)\"\n"
" xlink:href=\"#ucclef\"/>\n", D_ucclef},
#define D_pclef 10
{ "<path id=\"pclef\" d=\"m-2.7 9h5.4v-18h-5.4v18\" class=\"stroke\" stroke-width=\"1.4\"/>\n"},
#define D_hd 11
{ "<ellipse id=\"hd\" rx=\"4.1\" ry=\"2.9\"\n"
" transform=\"rotate(-20)\" class=\"fill\"/>\n"},
#define D_Hd 12
{ "<path id=\"Hd\" class=\"fill\" d=\"m3 -1.6\n"
" c-1 -1.8 -7 1.4 -6 3.2\n"
" 1 1.8 7 -1.4 6 -3.2\n"
" m0.5 -0.3\n"
" c2 3.8 -5 7.6 -7 3.8\n"
" -2 -3.8 5 -7.6 7 -3.8\"/>\n"},
#define D_HD 13
{ "<path id=\"HD\" class=\"fill\" d=\"m-2.7 -1.4\n"
" c1.5 -2.8 6.9 0 5.3 2.7\n"
" -1.5 2.8 -6.9 0 -5.3 -2.7\n"
" m8.3 1.4\n"
" c0 -1.5 -2.2 -3 -5.6 -3\n"
" -3.4 0 -5.6 1.5 -5.6 3\n"
" 0 1.5 2.2 3 5.6 3\n"
" 3.4 0 5.6 -1.5 5.6 -3\"/>\n"},
#define D_HDD 14
{ "<g id=\"HDD\">\n"
" <use xlink:href=\"#HD\"/>\n"
" <path d=\"m-6 -4v8m12 0v-8\" class=\"stroke\"/>\n"
"</g>\n", D_HD},
#define D_breve 15
{ "<g id=\"breve\" class=\"stroke\">\n"
" <path d=\"m-6 -2.7h12m0 5.4h-12\" stroke-width=\"2.5\"/>\n"
" <path d=\"m-6 -5v10m12 0v-10\"/>\n"
"</g>\n"},
#define D_longa 16
{ "<g id=\"longa\" class=\"stroke\">\n"
" <path d=\"m-6 2.7h12m0 -5.4h-12\" stroke-width=\"2.5\"/>\n"
" <path d=\"m-6 5v-10m12 0v16\"/>\n"
"</g>\n"},
#define D_ghd 17
{ "<path id=\"ghd\" class=\"fill\" d=\"m2.2 -1.5\n"
" c-1.32 -2.31 -5.94 0.33 -4.62 2.64\n"
" 1.32 2.31 5.94 -0.33 4.62 -2.64\"/>\n"},
#define D_r00 18
{ "<rect id=\"r00\" class=\"fill\"\n"
" x=\"-1.6\" y=\"-6\" width=\"3\" height=\"12\"/>\n"},
#define D_r0 19
{ "<rect id=\"r0\" class=\"fill\"\n"
" x=\"-1.6\" y=\"-6\" width=\"3\" height=\"6\"/>\n"},
#define D_r1 20
{ "<rect id=\"r1\" class=\"fill\"\n"
" x=\"-3.5\" y=\"-6\" width=\"7\" height=\"3\"/>\n"},
#define D_r2 21
{ "<rect id=\"r2\" class=\"fill\"\n"
" x=\"-3.5\" y=\"-3\" width=\"7\" height=\"3\"/>\n"},
#define D_r4 22
{ "<path id=\"r4\" class=\"fill\" d=\"m-1 -8.5\n"
" l3.6 5.1 -2.1 5.2 2.2 4.3\n"
" c-2.6 -2.3 -5.1 0 -2.4 2.6\n"
" -4.8 -3 -1.5 -6.9 1.4 -4.1\n"
" l-3.1 -4.5 1.9 -5.1 -1.5 -3.5\"/>\n"},
#define D_r8e 23
{ "<path id=\"r8e\" class=\"fill\" d=\"m 0 0\n"
" c-1.5 1.5 -2.4 2 -3.6 2\n"
" 2.4 -2.8 -2.8 -4 -2.8 -1.2\n"
" c0 2.7 4.3 2.4 5.9 0.6\"/>\n"},
#define D_r8 24
{ "<g id=\"r8\">\n"
" <path d=\"m3.3 -4l-3.4 9.6\" class=\"stroke\"/>\n"
" <use x=\"3.4\" y=\"-4\" xlink:href=\"#r8e\"/>\n"
"</g>\n", D_r8e},
#define D_r16 25
{ "<g id=\"r16\">\n"
" <path d=\"m3.3 -4l-4 15.6\" class=\"stroke\"/>\n"
" <use x=\"3.4\" y=\"-4\" xlink:href=\"#r8e\"/>\n"
" <use x=\"1.9\" y=\"2\" xlink:href=\"#r8e\"/>\n"
"</g>\n", D_r8e},
#define D_r32 26
{ "<g id=\"r32\">\n"
" <path d=\"m4.8 -10l-5.5 21.6\" class=\"stroke\"/>\n"
" <use x=\"4.9\" y=\"-10\" xlink:href=\"#r8e\"/>\n"
" <use x=\"3.4\" y=\"-4\" xlink:href=\"#r8e\"/>\n"
" <use x=\"1.9\" y=\"2\" xlink:href=\"#r8e\"/>\n"
"</g>\n", D_r8e},
#define D_r64 27
{ "<g id=\"r64\">\n"
" <path d=\"m4.8 -10 l-7 27.6\" class=\"stroke\"/>\n"
" <use x=\"4.9\" y=\"-10\" xlink:href=\"#r8e\"/>\n"
" <use x=\"3.4\" y=\"-4\" xlink:href=\"#r8e\"/>\n"
" <use x=\"1.9\" y=\"2\" xlink:href=\"#r8e\"/>\n"
" <use x=\"0.4\" y=\"8\" xlink:href=\"#r8e\"/>\n"
"</g>\n", D_r8e},
#define D_r128 28
{ "<g id=\"r128\">\n"
" <path d=\"m5.8 -16 l-8.5 33.6\" class=\"stroke\"/>\n"
" <use x=\"5.9\" y=\"-16\" xlink:href=\"#r8e\"/>\n"
" <use x=\"4.4\" y=\"-10\" xlink:href=\"#r8e\"/>\n"
" <use x=\"2.9\" y=\"-4\" xlink:href=\"#r8e\"/>\n"
" <use x=\"1.4\" y=\"2\" xlink:href=\"#r8e\"/>\n"
" <use x=\"0.1\" y=\"8\" xlink:href=\"#r8e\"/>\n"
"</g>\n", D_r8e},
#define D_mrest 29
{ "<g id=\"mrest\" class=\"stroke\">\n"
" <path d=\"m-20 6v-12m40 0v12\"/>\n"
" <path d=\"m-20 0h40\" stroke-width=\"5\"/>\n"
"</g>\n"},
#define D_usharp 30
{ "<path id=\"usharp\" class=\"fill\" d=\"\n"
" m136 -702\n"
" v890\n"
" h32\n"
" v-890\n"
" m128 840\n"
" h32\n"
" v-888\n"
" h-32\n"
" m-232 286\n"
" v116\n"
" l338 -96\n"
" v-116\n"
" m-338 442\n"
" v116\n"
" l338 -98\n"
" v-114\"/>\n"},
#define D_uflat 31
{ "<path id=\"uflat\" class=\"fill\" d=\"\n"
" m100 -746\n"
" h32\n"
" v734\n"
" l-32 4\n"
" m32 -332\n"
" c46 -72 152 -90 208 -20\n"
" 100 110 -120 326 -208 348\n"
" m0 -28\n"
" c54 0 200 -206 130 -290\n"
" -50 -60 -130 -4 -130 34\"/>\n"},
#define D_unat 32
{ "<path id=\"unat\" class=\"fill\" d=\"\n"
" m96 -750\n"
" h-32\n"
" v716\n"
" l32 -8\n"
" 182 -54\n"
" v282\n"
" h32\n"
" v-706\n"
" l-34 10\n"
" -180 50\n"
" v-290\n"
" m0 592\n"
" v-190\n"
" l182 -52\n"
" v188\"/>\n"},
#define D_udblesharp 33
{ "<path id=\"udblesharp\" class=\"fill\" d=\"\n"
" m240 -282\n"
" c40 -38 74 -68 158 -68\n"
" v-96\n"
" h-96\n"
" c0 84 -30 118 -68 156\n"
" -40 -38 -70 -72 -70 -156\n"
" h-96\n"
" v96\n"
" c86 0 120 30 158 68\n"
" -38 38 -72 68 -158 68\n"
" v96\n"
" h96\n"
" c0 -84 30 -118 70 -156\n"
" 38 38 68 72 68 156\n"
" h96\n"
" v-96\n"
" c-84 0 -118 -30 -158 -68\"/>\n"},
#define D_udbleflat 34
{ "<path id=\"udbleflat\" class=\"fill\" d=\"\n"
" m20 -746\n"
" h24\n"
" v734\n"
" l-24 4\n"
" m24 -332\n"
" c34 -72 114 -90 156 -20\n"
" 75 110 -98 326 -156 348\n"
" m0 -28\n"
" c40 0 150 -206 97 -290\n"
" -37 -60 -97 -4 -97 34\n"
" m226 -450\n"
" h24\n"
" v734\n"
" l-24 4\n"
" m24 -332\n"
" c34 -72 114 -90 156 -20\n"
" 75 110 -98 326 -156 348\n"
" m0 -28\n"
" c40 0 150 -206 97 -290\n"
" -37 -60 -97 -4 -97 34\"/>\n"},
#define D_sh0 35
{ "<use id=\"sh0\" transform=\"translate(-4,5) scale(0.018)\"\n"
" xlink:href=\"#usharp\"/>\n", D_usharp},
#define D_ft0 36
{ "<use id=\"ft0\" transform=\"translate(-3.5,3.5) scale(0.018)\"\n"
" xlink:href=\"#uflat\"/>\n", D_uflat},
#define D_nt0 37
{ "<use id=\"nt0\" transform=\"translate(-3,5) scale(0.018)\"\n"
" xlink:href=\"#unat\"/>\n", D_unat},
#define D_dsh0 38
{ "<use id=\"dsh0\" transform=\"translate(-4,5) scale(0.018)\"\n"
" xlink:href=\"#udblesharp\"/>\n", D_udblesharp},
#define D_dft0 39
{ "<use id=\"dft0\" transform=\"translate(-4,3.5) scale(0.018)\"\n"
" xlink:href=\"#udbleflat\"/>\n", D_udbleflat},
#define D_sh1 40
{ "<g id=\"sh1\">\n"
" <path d=\"M0 7.8v-15.4\" class=\"stroke\"/>\n"
" <path class=\"fill\" d=\"M-1.8 2.7l3.6 -1.1v2.2l-3.6 1.1v-2.2z\n"
" M-1.8 -3.7l3.6 -1.1v2.2l-3.6 1.1v-2.2\"/>\n"
"</g>\n"},
#define D_sh513 41
{ "<g id=\"sh513\">\n"
" <path d=\"M-2.5 8.7v-15.4M0 7.8v-15.4M2.5 6.9v-15.4\" class=\"stroke\"/>\n"
" <path class=\"fill\" d=\"M-3.7 3.1l7.4 -2.2v2.2l-7.4 2.2v-2.2z\n"
" M-3.7 -3.2l7.4 -2.2v2.2l-7.4 2.2v-2.2\"/>\n"
"</g>\n"},
#define D_ft1 42
{ "<g id=\"ft1\" transform=\"scale(-1,1)\">\n"
" <use xlink:href=\"#ft0\"/>\n"
"</g>\n", D_ft0},
#define D_ft513 43
{ "<g id=\"ft513\">\n"
" <path class=\"fill\" d=\"M0.6 -2.7\n"
" c-5.7 -3.1 -5.7 3.6 0 6.7c-3.9 -4 -4 -7.6 0 -5.8\n"
" M1 -2.7c5.7 -3.1 5.7 3.6 0 6.7c3.9 -4 4 -7.6 0 -5.8\"/>\n"
" <path d=\"M1.6 3.5v-13M0 3.5v-13\" class=\"stroke\" stroke-width=\".6\"/>\n"
"</g>\n"},
#define D_pshhd 44
{ "<g id=\"pshhd\">\n"
" <use xlink:href=\"#dsh0\"/>\n"
"</g>\n", D_dsh0},
#define D_pfthd 45
{ "<g id=\"pfthd\">\n"
" <use xlink:href=\"#dsh0\"/>\n"
" <circle r=\"4\" class=\"stroke\"/>\n"
"</g>\n", D_dsh0},
#define D_csig 46
{ "<path id=\"csig\" class=\"fill\" d=\"\n"
" m6 -5.3\n"
" c0.9 0 2.3 0.7 2.4 2.2\n"
" -1.2 -2 -3.6 0.1 -1.6 1.7\n"
" 2 1 3.8 -3.5 -0.8 -4.7\n"
" -2 -0.4 -6.4 1.3 -5.8 7\n"
" 0.4 6.4 7.9 6.8 9.1 0.7\n"
" -2.3 5.6 -6.7 5.1 -6.8 0\n"
" -0.5 -4.4 0.7 -7.5 3.5 -6.9\"/>\n"},
#define D_ctsig 47
{ "<g id=\"ctsig\">\n"
" <use xlink:href=\"#csig\"/>\n"
" <path d=\"m5 8v-16\" class=\"stroke\"/>\n"
"</g>\n", D_csig},
#define D_pmsig 48
{ "<path id=\"pmsig\" class=\"stroke\" stroke-width=\".8\"\n"
" d=\"M0 -7a5 5 0 0 1 0 -10a5 5 0 0 1 0 10\"/>\n"},
#define D_pMsig 49
{ "<g id=\"pMsig\">\n"
" <use xlink:href=\"#pmsig\"/>\n"
" <path class=\"fill\" d=\"M0 -10a2 2 0 0 1 0 -4a2 2 0 0 1 0 4\"/>\n"
"</g>\n", D_pmsig},
#define D_imsig 50
{ "<path id=\"imsig\" class=\"stroke\" stroke-width=\".8\"\n"
" d=\"M3 -8a5 5 0 1 1 0 -8\"/>\n"},
#define D_iMsig 51
{ "<g id=\"iMsig\">\n"
" <use xlink:href=\"#imsig\"/>\n"
" <path class=\"fill\" d=\"M0 -10a2 2 0 0 1 0 -4a2 2 0 0 1 0 4\"/>\n"
"</g>\n", D_imsig},
#define D_hl 52
{ "<path id=\"hl\" class=\"stroke\" d=\"m-6 0h12\"/>\n"},
#define D_hl1 53
{ "<path id=\"hl1\" class=\"stroke\" d=\"m-7 0h14\"/>\n"},
#define D_hl2 54
{ "<path id=\"hl2\" class=\"stroke\" d=\"m-9 0h18\"/>\n"},
#define D_ghl 55
{ "<path id=\"ghl\" class=\"stroke\" d=\"m-3.5 0h7\"/>\n"},
#define D_rdots 56
{ "<g id=\"rdots\" class=\"fill\">\n"
" <circle cx=\"0\" cy=\"-9\" r=\"1.2\"/>\n"
" <circle cx=\"0\" cy=\"-15\" r=\"1.2\"/>\n"
"</g>\n"},
#define D_srep 57
{ "<path id=\"srep\" class=\"fill\" d=\"M-1 6l11 -12h3l-11 12h-3\"/>\n"},
#define D_mrep 58
{ "<path id=\"mrep\" class=\"fill\"\n"
" d=\"M-5 -4.5a1.5 1.5 0 0 1 0 3a1.5 1.5 0 0 1 0 -3\n"
" M4.5 2a1.5 1.5 0 0 1 0 3a1.5 1.5 0 0 1 0 -3\n"
" M-7 6l11 -12h3l-11 12h-3\"/>\n"},
#define D_mrep2 59
{ "<g id=\"mrep2\" class=\"fill\">\n"
" <path d=\"M-5.5 -7.5a1.5 1.5 0 0 1 0 3a1.5 1.5 0 0 1 0 -3\n"
" M5 4.5a1.5 1.5 0 0 1 0 3a1.5 1.5 0 0 1 0 -3\"/>\n"
" <path d=\"M-7 8l14 -10m-14 4l14 -10\" class=\"stroke\" stroke-width=\"1.8\"/>\n"
"</g>\n"},
#define D_accent 60
{ "<g id=\"accent\" class=\"stroke\" stroke-width=\"1.2\">\n"
" <path d=\"m-4 0l8 -2l-8 -2\"/>\n"
"</g>\n"},
#define D_umrd 61
{ "<path id=\"umrd\" class=\"fill\" d=\"m0 -4\n"
" l2.2 -2.2 2.1 2.9 0.7 -0.7 0.2 0.2\n"
" -2.2 2.2 -2.1 -2.9 -0.7 0.7\n"
" -2.2 2.2 -2.1 -2.9 -0.7 0.7 -0.2 -0.2\n"
" 2.2 -2.2 2.1 2.9 0.7 -0.7\"/>\n"},
#define D_lmrd 62
{ "<g id=\"lmrd\">\n"
" <use xlink:href=\"#umrd\"/>\n"
" <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"-8\" class=\"stroke\" stroke-width=\".6\"/>\n"
"</g>\n", D_umrd},
#define D_grm 63
{ "<path id=\"grm\" class=\"fill\" d=\"\n"
" m-5 -2.5\n"
" c5 -8.5 5.5 4.5 10 -2\n"
" -5 8.5 -5.5 -4.5 -10 2\"/>\n"},
#define D_stc 64
{ "<circle id=\"stc\" class=\"fill\" cx=\"0\" cy=\"-3\" r=\"1.2\"/>\n"},
#define D_sld 65
{ "<path id=\"sld\" class=\"fill\" d=\"\n"
" m-7.2 4.8\n"
" c1.8 0.7 4.5 -0.2 7.2 -4.8\n"
" -2.1 5 -5.4 6.8 -7.6 6\"/>\n"},
#define D_emb 66
{ "<path id=\"emb\" d=\"m-2.5 -3h5\" class=\"stroke\" stroke-width=\"1.2\" stroke-linecap=\"round\"/>\n"},
#define D_hld 67
{ "<g id=\"hld\" class=\"fill\">\n"
" <circle cx=\"0\" cy=\"-3\" r=\"1.3\"/>\n"
" <path d=\"m-7.5 -1.5\n"
" c0 -11.5 15 -11.5 15 0\n"
" h-0.25\n"
" c-1.25 -9 -13.25 -9 -14.5 0\"/>\n"
"</g>\n"},
#define D_cpu 68
{ "<path id=\"cpu\" class=\"fill\" d=\"\n"
" m-6 0\n"
" c0.4 -7.3 11.3 -7.3 11.7 0\n"
" c-1.3 -6 -10.4 -6 -11.7 0\"/>\n"},
#define D_upb 69
{ "<path id=\"upb\" class=\"stroke\" d=\"\n"
" m-2.6 -9.4\n"
" l2.6 8.8\n"
" 2.6 -8.8\"/>\n"},
#define D_dnb 70
{ "<g id=\"dnb\">\n"
" <path d=\"M-3.2 -2v-7.2m6.4 0v7.2\" class=\"stroke\"/>\n"
" <path d=\"M-3.2 -6.8v-2.4l6.4 0v2.4\" class=\"fill\"/>\n"
"</g>\n"},
#define D_sgno 71
{ "<g id=\"sgno\">\n"
" <path class=\"fill\" d=\"m0 -3\n"
" c1.5 1.7 6.4 -0.3 3 -3.7\n"
" -10.4 -7.8 -8 -10.6 -6.5 -11.9\n"
" 4 -1.9 5.9 1.7 4.2 2.6\n"
" -1.3 0.7 -2.9 -1.3 -0.7 -2\n"
" -1.5 -1.7 -6.4 0.3 -3 3.7\n"
" 10.4 7.8 8 10.6 6.5 11.9\n"
" -4 1.9 -5.9 -1.7 -4.2 -2.6\n"
" 1.3 -0.7 2.9 1.3 0.7 2\"/>\n"
" <line x1=\"-6\" y1=\"-4.2\" x2=\"6.6\" y2=\"-16.8\" class=\"stroke\"/>\n"
" <circle cx=\"-6\" cy=\"-10\" r=\"1.2\"/>\n"
" <circle cx=\"6\" cy=\"-11\" r=\"1.2\"/>\n"
"</g>\n"},
#define D_coda 72
{ "<g id=\"coda\" class=\"stroke\">\n"
" <path d=\"m0 -2v-20m-10 10h20\"/>\n"
" <circle cx=\"0\" cy=\"-12\" r=\"6\" stroke-width=\"1.7\"/>\n"
"</g>\n"},
#define D_dplus 73
{ "<path id=\"dplus\" class=\"stroke\" stroke-width=\"1.7\"\n"
" d=\"m0 -0.5v-6m-3 3h6\"/>\n"},
#define D_lphr 74
{ "<path id=\"lphr\" class=\"stroke\" stroke-width=\"1.2\"\n"
" d=\"m0 0v18\"/>\n"},
#define D_mphr 75
{ "<path id=\"mphr\" class=\"stroke\" stroke-width=\"1.2\"\n"
" d=\"m0 0v12\"/>\n"},
#define D_sphr 76
{ "<path id=\"sphr\" class=\"stroke\" stroke-width=\"1.2\"\n"
" d=\"m0 0v6\"/>\n"},
#define D_opend 77
{ "<circle id=\"opend\" class=\"stroke\"\n"
" cx=\"0\" cy=\"-3\" r=\"2.5\"/>\n"},
#define D_snap 78
{ "<path id=\"snap\" class=\"stroke\"\n"
" d=\"M-3 -6\n"
" c0 -5 6 -5 6 0\n"
" c0 5 -6 5 -6 0\n"
" M0 -5v6\"/>\n"},
#define D_thumb 79
{ "<path id=\"thumb\" class=\"stroke\"\n"
" d=\"M-2.5 -7\n"
" c0 -6 5 -6 5 0\n"
" c0 6 -5 6 -5 0\n"
" M-2.5 -9v4\"/>\n"},
#define D_turn 80
{ "<path id=\"turn\" class=\"fill\" d=\"\n"
" m5.2 -8\n"
" c1.4 0.5 0.9 4.8 -2.2 2.8\n"
" l-4.8 -3.5\n"
" c-3 -2 -5.8 1.8 -3.6 4.4\n"
" 1 1.1 2 0.8 2.1 -0.1\n"
" 0.1 -0.9 -0.7 -1.2 -1.9 -0.6\n"
" -1.4 -0.5 -0.9 -4.8 2.2 -2.8\n"
" l4.8 3.5\n"
" c3 2 5.8 -1.8 3.6 -4.4\n"
" -1 -1.1 -2 -0.8 -2.1 0.1\n"
" -0.1 0.9 0.7 1.2 1.9 0.6\"/>\n"},
#define D_turnx 81
{ "<g id=\"turnx\">\n"
" <use xlink:href=\"#turn\"/>\n"
" <path d=\"M0 -1.5v-9\" class=\"stroke\"/>\n"
"</g>\n", D_turn},
#define D_wedge 82
{ "<path id=\"wedge\" class=\"fill\" d=\"M0 -1l-1.5 -5h3l-1.5 5\"/>\n"},
#define D_ltr 83
{ "<path id=\"ltr\" class=\"fill\"\n"
" d=\"m0 -0.4c2 -1.5 3.4 -1.9 3.9 0.4\n"
" c0.2 0.8 0.7 0.7 2.1 -0.4\n"
" v0.8c-2 1.5 -3.4 1.9 -3.9 -0.4\n"
" c-0.2 -0.8 -0.7 -0.7 -2.1 0.4z\"/>\n"},
#define D_custos 84
{ "<g id=\"custos\">\n"
" <path d=\"M-4 0l2 2.5l2 -2.5l2 2.5l2 -2.5\n"
" l-2 -2.5l-2 2.5l-2 -2.5l-2 2.5\" class=\"fill\"/>\n"
" <path d=\"M3.5 0l5 -7\" class=\"stroke\"/>\n"
"</g>\n"},
#define D_showerror 85
{ "<circle id=\"showerror\" r=\"30\" stroke=\"#ffc0c0\" stroke-width=\"2.5\" fill=\"none\"/>\n"},
#define D_sfz 86
{ "<text id=\"sfz\" style=\"font:italic 14px serif\"\n"
" x=\"-5\" y=\"-7\">s<tspan\n"
" font-size=\"16\" font-weight=\"bold\">f</tspan>z</text>\n"},
#define D_trl 87
{ "<text id=\"trl\" style=\"font:italic bold 16px serif\"\n"
" x=\"-2\" y=\"-4\">tr</text>\n"},
#define D_marcato 88
{ "<path id=\"marcato\" d=\"m-3 0l3 -7l3 7l-1.5 0l-1.8 -4.2l-1.7 4.2\"/>\n"},
#define D_ped 89
{ "<text id=\"ped\" font-family=\"serif\" font-size=\"16\" font-style=\"italic\"\n"
" x=\"-10\" y=\"-4\">Ped</text>\n"},
#define D_pedoff 90
{ "<text id=\"pedoff\" font-family=\"serif\" font-size=\"16\" font-style=\"italic\"\n"
" x=\"-5\" y=\"-4\">*</text>\n"},
};
static struct {
int index;
char *def;
} font_gl[] = {
{D_brace,
"<text id=\"brace\" class=\"music\" x=\"-3\" y=\"0\"\n"
" transform=\"scale(3,-4.2)\"></text>\n"},
{D_sgno,
"<text id=\"sgno\" class=\"music\" x=\"-6\" y=\"-4\"></text>\n"},
{D_coda,
"<text id=\"coda\" class=\"music\" x=\"-12\" y=\"-6\"></text>\n"},
{D_tclef,
"<text id=\"tclef\" class=\"music\" x=\"-8\" y=\"0\"></text>\n"},
{D_cclef,
"<text id=\"cclef\" class=\"music\" x=\"-7\" y=\"0\"></text>\n"},
{D_bclef,
"<text id=\"bclef\" class=\"music\" x=\"-7\" y=\"0\"></text>\n"},
{D_pclef,
"<text id=\"pclef\" class=\"music\" x=\"-6\" y=\"0\"></text>\n"},
{D_stclef,
"<text id=\"stclef\" class=\"music\" x=\"-8\" y=\"0\"></text>\n"},
{D_scclef,
"<text id=\"scclef\" class=\"music\" x=\"-8\" y=\"0\"></text>\n"},
{D_sbclef,
"<text id=\"sbclef\" class=\"music\" x=\"-7\" y=\"0\"></text>\n"},
{D_csig,
"<text id=\"csig\" class=\"music\" x=\"0\" y=\"0\"></text>\n"},
{D_ctsig,
"<text id=\"ctsig\" class=\"music\" x=\"0\" y=\"0\"></text>\n"},
{D_HDD,
"<text id=\"HDD\" class=\"music\" x=\"-7\" y=\"0\"></text>\n"},
{D_breve,
"<text id=\"breve\" class=\"music\" x=\"-6\" y=\"0\"></text>\n"},
{D_HD,
"<text id=\"HD\" class=\"music\" x=\"-5.2\" y=\"0\"></text>\n"},
{D_Hd,
"<text id=\"Hd\" class=\"music\" x=\"-3.8\" y=\"0\"></text>\n"},
{D_hd,
"<text id=\"hd\" class=\"music\" x=\"-3.7\" y=\"0\"></text>\n"},
{D_ft0,
"<text id=\"ft0\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_nt0,
"<text id=\"nt0\" class=\"music\" x=\"-2\" y=\"0\"></text>\n"},
{D_sh0,
"<text id=\"sh0\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_dsh0,
"<text id=\"dsh0\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_pshhd,
"<text id=\"pshhd\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_dft0,
"<text id=\"dft0\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_accent,
"<text id=\"accent\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_marcato,
"<text id=\"marcato\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_hld,
"<text id=\"hld\" class=\"music\" x=\"-7\" y=\"0\"></text>\n"},
{D_r00,
"<text id=\"r00\" class=\"music\" x=\"-1.5\" y=\"0\"></text>\n"},
{D_r0,
"<text id=\"r0\" class=\"music\" x=\"-1.5\" y=\"0\"></text>\n"},
{D_r1,
"<text id=\"r1\" class=\"music\" x=\"-3.5\" y=\"-6\"></text>\n"},
{D_r2,
"<text id=\"r2\" class=\"music\" x=\"-3.2\" y=\"0\"></text>\n"},
{D_r4,
"<text id=\"r4\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_r8,
"<text id=\"r8\" class=\"music\" x=\"-3\" y=\"0\"></text>\n"},
{D_r16,
"<text id=\"r16\" class=\"music\" x=\"-4\" y=\"0\"></text>\n"},
{D_r32,
"<text id=\"r32\" class=\"music\" x=\"-4\" y=\"0\"></text>\n"},
{D_r64,
"<text id=\"r64\" class=\"music\" x=\"-4\" y=\"0\"></text>\n"},
{D_r128,
"<text id=\"r128\" class=\"music\" x=\"-4\" y=\"0\"></text>\n"},
{D_mrest,
"<text id=\"mrest\" class=\"music\" x=\"-10\" y=\"0\"></text>\n"},
{D_mrep,
"<text id=\"mrep\" class=\"music\" x=\"-6\" y=\"0\"></text>\n"},
{D_mrep2,
"<text id=\"mrep2\" class=\"music\" x=\"-9\" y=\"0\"></text>\n"},
{D_turn,
"<text id=\"turn\" class=\"music\" x=\"-4\" y=\"0\"></text>\n"},
{D_umrd,
"<text id=\"umrd\" class=\"music\" x=\"-7\" y=\"-2\"></text>\n"},
{D_lmrd,
"<text id=\"lmrd\" class=\"music\" x=\"-7\" y=\"-2\"></text>\n"},
{D_ped,
"<text id=\"ped\" class=\"music\" x=\"-10\" y=\"0\"></text>\n"},
{D_pedoff,
"<text id=\"pedoff\" class=\"music\" x=\"-6\" y=\"0\"></text>\n"},
{D_longa,
"<text id=\"longa\" class=\"music\" x=\"-6\" y=\"0\"></text>\n"},
};
// switch to a music font
void svg_font_switch(void)
{
int i, j;
for (i = 0; i < sizeof font_gl / sizeof font_gl[0]; i++) {
j = font_gl[i].index;
def_tb[j].def = font_gl[i].def;
def_tb[j].use = 0;
}
}
/* PS functions */
static void ps_exec(char *op);
static void elts_link(struct elt_s *e)
{
int i;
/* set the linkages - the first element is the link to the next block */
for (i = 1; i < NELTS - 1; i++) {
e[i].next = &e[i + 1];
if (e[i].type == STR)
free(e[i].u.s);
e[i].type = VAL;
}
e[NELTS - 1].next = NULL;
}
/* (re)initialize all PS elements */
static void elts_reset(void)
{
struct elt_s *e;
if (!elts)
elts = calloc(sizeof *elts, NELTS);
elts_link(elts);
free_elt = elts + 1;
/* link all blocks */
for (e = elts; e->u.e; e = e->u.e) {
elts_link(e->u.e);
e[NELTS - 1].next = e->u.e;
}
}
static struct elt_s *elt_new(void)
{
struct elt_s *e;
e = free_elt;
if (!e) {
e = calloc(sizeof *e, NELTS);
if (!e) {
fprintf(stderr, "svg: elt_new out of memory\n");
ps_error = 1;
return e;
}
elts_link(e);
e->u.e = elts;
elts = e;
e++;
}
free_elt = e->next;
e->next = NULL;
e->type = VAL;
return e;
}
static void elt_free(struct elt_s *e)
{
struct elt_s *e2;
e->next = free_elt;
free_elt = e;
switch (e->type) {
case STR:
free(e->u.s);
e->type = VAL;
e->u.v = 0;
break;
case SEQ:
case BRK:
e2 = e->u.e;
e->type = VAL;
e->u.v = 0;
while (e2) {
e = e2->next;
elt_free(e2);
e2 = e;
}
break;
}
}
static struct elt_s *elt_dup(struct elt_s *e)
{
struct elt_s *e2, *e3, *e4;
e2 = elt_new();
if (!e2)
return e2;
e2->type = e->type;
switch (e->type) {
case VAL:
e2->u.v = e->u.v;
break;
case STR:
e2->u.s = strdup(e->u.s);
break;
case SEQ:
case BRK:
e = e->u.e;
if (!e) {
e2->u.e = NULL;
break;
}
e3 = e2->u.e = elt_dup(e);
if (!e3)
break;
for (;;) {
e = e->next;
if (!e)
break;
e4 = elt_dup(e);
if (!e4)
break;
e3->next = e4;
e3 = e4;
}
e3->next = NULL;
break;
}
return e2;
}
static void elt_dump(struct elt_s *e)
{
int type;
type = e->type;
switch (type) {
case VAL:
fprintf(stderr, " %.2f", e->u.v);
break;
case STR:
fprintf(stderr, " %s", e->u.s);
if (e->u.s[0] == '(')
fprintf(stderr, ")");
break;
case SEQ:
case BRK:
fprintf(stderr, type == SEQ ? " {" : " [");
e = e->u.e;
while (e) {
elt_dump(e);
e = e->next;
}
fprintf(stderr, type == SEQ ? " }" : " ]");
}
}
static void elt_lst_dump(struct elt_s *e)
{
do {
elt_dump(e);
e = e->next;
} while (e);
}
static struct ps_sym_s *ps_sym_lookup(char *name)
{
struct ps_sym_s *ps;
if (n_sym == 0)
return NULL;
ps = &ps_sym[n_sym];
for (;;) {
ps--;
if (strcmp(ps->n, name) == 0)
break;
if (ps == ps_sym)
return NULL;
}
return ps;
}
static struct ps_sym_s *ps_sym_def(char *name, struct elt_s *e)
{
struct ps_sym_s *ps;
ps = ps_sym_lookup(name);
if (ps) {
elt_free(ps->e);
} else {
if (n_sym >= NSYMS) {
fprintf(stderr, "svg: Too many PS symbols\n");
ps_error = 1;
return NULL;
}
ps = &ps_sym[n_sym++];
ps->n = strdup(name);
}
ps->e = e;
ps->exec = 0;
return ps;
}
static void push(struct elt_s *e)
{
e->next = stack;
stack = e;
}
static void stack_dump(void)
{
fprintf(stderr, "stack:");
if (stack)
elt_lst_dump(stack);
else
fprintf(stderr, "(empty)");
fprintf(stderr, "\n");
}
static struct elt_s *pop(int type)
{
struct elt_s *e;
e = stack;
if (!e) {
fprintf(stderr, "svg pop: Stack empty\n");
ps_error = 1;
return NULL;
}
if (e->type != type) {
fprintf(stderr, "svg pop: Bad element type %d != %d\n",
e->type, type);
stack_dump();
ps_error = 1;
return NULL;
}
stack = e->next;
return e;
}
static float pop_free_val(void)
{
struct elt_s *e;
e = pop(VAL);
if (!e)
return 0;
e->next = free_elt;
free_elt = e;
return e->u.v;
}
static char *pop_free_str(void)
{
struct elt_s *e;
char *s;
e = pop(STR);
if (!e)
return NULL;
s = e->u.s;
e->type = VAL;
e->next = free_elt;
free_elt = e;
return s;
}
/* PS condition code */
#define C_EQ 0
#define C_NE 1
#define C_GT 2
#define C_GE 3
#define C_LT 4
#define C_LE 5
static void cond(int type)
{
float v;
char *s, *s2;