-
Notifications
You must be signed in to change notification settings - Fork 15
/
drawtune.c
3613 lines (3472 loc) · 93.4 KB
/
drawtune.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
/*
* yaps - program to convert abc files to PostScript.
* Copyright (C) 1999 James Allwright
* e-mail: [email protected]
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*/
/* drawtune.c */
/* This file contains routines for generating final PostScript Output */
/* There are 2 stages to this process. First the symbol positions are */
/* calculated, then the PostScript symbols are generated. */
#ifdef _MSC_VER
#define ANSILIBS 1
#endif
#include <stdio.h>
#ifdef ANSILIBS
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#endif
#include "abc.h"
#include "structs.h"
#include "sizes.h"
#include "drawtune.h"
/* external functions and variables */
extern struct tune thetune;
extern int debugging;
extern int pagenumbering;
extern int barnums, nnbars;
extern char outputname[256];
extern char outputroot[256];
extern int make_open();
extern void printlib();
extern int count_dots(int *base, int *base_exp, int n, int m);
extern void monospace(struct tune* t);
extern void spacevoices(struct tune* t);
/* provided by debug.c */
extern void showtune(struct tune *t);
struct key* newkey(char* name, int sharps, char accidental[], int mult[]);
struct aclef* newclef(enum cleftype t, int octave);
char* addstring(char *s);
extern int lineno;
extern int separate_voices;
extern int print_xref;
extern int landscape;
static void pagebottom();
static void setfont(int size, int num);
FILE* f = NULL;
struct feature* beamset[64];
struct feature* gracebeamset[32];
int beamctr, gracebeamctr;;
int rootstem;
int fontsize, fontnum;
int donemeter;
int ingrace, inchord;
int chordcount;
struct feature* chordhead;
double scale, descend, totlen, oldplace;
int pagecount = 1;
int pagelen, pagewidth, xmargin, ymargin;
double scaledlen, scaledwidth;
int staffsep;
int eps_out;
int titleleft = 0;
int titlecaps = 0;
int gchords_above = 1;
int redcolor; /* [SS] 2013-11-04*/
enum placetype {left, right, centre};
struct font textfont;
struct font titlefont;
struct font subtitlefont;
struct font wordsfont;
struct font composerfont;
struct font vocalfont;
struct font gchordfont;
struct font partsfont;
/* arrays giving character widths for characters 32-255 */
/* font for lyrics : 13 point Times-Bold */
double timesbold_width[224] = {
3.249, 4.328, 7.214, 6.499, 6.499, 12.999, 10.828, 4.328,
4.328, 4.328, 6.499, 7.409, 3.249, 7.409, 3.249, 3.613,
6.499, 6.499, 6.499, 6.499, 6.499, 6.499, 6.499, 6.499,
6.499, 6.499, 4.328, 4.328, 7.409, 7.409, 7.409, 6.499,
12.089, 9.385, 8.67, 9.385, 9.385, 8.67, 7.942, 10.113,
10.113, 5.056, 6.499, 10.113, 8.67, 12.271, 9.385, 10.113,
7.942, 10.113, 9.385, 7.227, 8.67, 9.385, 9.385, 12.999,
9.385, 9.385, 8.67, 4.328, 3.613, 4.328, 7.552, 6.499,
4.328, 6.499, 7.227, 5.771, 7.227, 5.771, 4.328, 6.499,
7.227, 3.613, 4.328, 7.227, 3.613, 10.828, 7.227, 6.499,
7.227, 7.227, 5.771, 5.056, 4.328, 7.227, 6.499, 9.385,
6.499, 6.499, 5.771, 5.121, 2.859, 5.121, 6.759, 3.249,
3.249, 3.249, 3.249, 3.249, 3.249, 3.249, 3.249, 3.249,
3.249, 3.249, 3.249, 3.249, 3.249, 3.249, 3.249, 3.249,
3.613, 4.328, 4.328, 4.328, 4.328, 4.328, 4.328, 4.328,
4.328, 3.249, 4.328, 4.328, 3.249, 4.328, 4.328, 4.328,
3.249, 4.328, 6.499, 6.499, 6.499, 6.499, 2.859, 6.499,
4.328, 9.71, 3.899, 6.499, 7.409, 4.328, 9.71, 4.328,
5.199, 7.409, 3.899, 3.899, 4.328, 7.227, 7.019, 3.249,
4.328, 3.899, 4.289, 6.499, 9.749, 9.749, 9.749, 6.499,
9.385, 9.385, 9.385, 9.385, 9.385, 9.385, 12.999, 9.385,
8.67, 8.67, 8.67, 8.67, 5.056, 5.056, 5.056, 5.056,
9.385, 9.385, 10.113, 10.113, 10.113, 10.113, 10.113, 7.409,
10.113, 9.385, 9.385, 9.385, 9.385, 9.385, 7.942, 7.227,
6.499, 6.499, 6.499, 6.499, 6.499, 6.499, 9.385, 5.771,
5.771, 5.771, 5.771, 5.771, 3.613, 3.613, 3.613, 3.613,
6.499, 7.227, 6.499, 6.499, 6.499, 6.499, 6.499, 7.409,
6.499, 7.227, 7.227, 7.227, 7.227, 6.499, 7.227, 6.499,
};
/* font for chord names : 12 point Helvetica */
double helvetica_width[224] = {
3.335, 3.335, 4.259, 6.671, 6.671, 10.667, 8.003, 2.651,
3.995, 3.995, 4.667, 7.007, 3.335, 3.995, 3.335, 3.335,
6.671, 6.671, 6.671, 6.671, 6.671, 6.671, 6.671, 6.671,
6.671, 6.671, 3.335, 3.335, 7.007, 7.007, 7.007, 6.671,
12.179, 8.003, 8.003, 8.663, 8.663, 8.003, 7.331, 9.335,
8.663, 3.335, 5.999, 8.003, 6.671, 9.995, 8.663, 9.335,
8.003, 9.335, 8.663, 8.003, 7.331, 8.663, 8.003, 11.327,
8.003, 8.003, 7.331, 3.335, 3.335, 3.335, 5.627, 6.671,
2.663, 6.671, 6.671, 5.999, 6.671, 6.671, 3.335, 6.671,
6.671, 2.663, 2.663, 5.999, 2.663, 9.995, 6.671, 6.671,
6.671, 6.671, 3.995, 5.999, 3.335, 6.671, 5.999, 8.663,
5.999, 5.999, 5.999, 4.007, 3.119, 4.007, 7.007, 3.335,
3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
3.335, 3.995, 6.671, 6.671, 2.003, 6.671, 6.671, 6.671,
6.671, 2.291, 3.995, 6.671, 3.995, 3.995, 5.999, 5.999,
3.335, 6.671, 6.671, 6.671, 3.335, 3.335, 6.443, 4.199,
2.663, 3.995, 3.995, 6.671, 11.999, 11.999, 3.335, 7.331,
3.335, 3.995, 3.995, 3.995, 3.995, 3.995, 3.995, 3.995,
3.995, 3.335, 3.995, 3.995, 3.335, 3.995, 3.995, 3.995,
11.999, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
3.335, 11.999, 3.335, 4.439, 3.335, 3.335, 3.335, 3.335,
6.671, 9.335, 11.999, 4.379, 3.335, 3.335, 3.335, 3.335,
3.335, 10.667, 3.335, 3.335, 3.335, 3.335, 3.335, 3.335,
2.663, 7.331, 11.327, 7.331, 3.335, 3.335, 3.335, 3.335,
};
static int ISOencoding(char ch1, char ch2)
/* converts the 2 characters following '\' into an ISO Latin 1 Font */
/* character code in the range 128-255 */
{
int code;
code = 0;
switch (ch1) {
case '`':
switch(ch2) {
case 'A': code = 0300; break;
case 'E': code = 0310; break;
case 'I': code = 0314; break;
case 'O': code = 0322; break;
case 'U': code = 0331; break;
case 'a': code = 0340; break;
case 'e': code = 0350; break;
case 'i': code = 0354; break;
case 'o': code = 0362; break;
case 'u': code = 0371; break;
default:
break;
};
break;
case '\'':
switch(ch2) {
case 'A': code = 0301; break;
case 'E': code = 0311; break;
case 'I': code = 0315; break;
case 'U': code = 0332; break;
case 'Y': code = 0335; break;
case 'a': code = 0341; break;
case 'e': code = 0351; break;
case 'i': code = 0355; break;
case 'o': code = 0363; break;
case 'u': code = 0372; break;
case 'y': code = 0375; break;
default:
break;
};
break;
case '^':
switch(ch2) {
case 'A': code = 0302; break;
case 'E': code = 0312; break;
case 'I': code = 0316; break;
case 'O': code = 0324; break;
case 'U': code = 0333; break;
case 'a': code = 0342; break;
case 'e': code = 0352; break;
case 'i': code = 0356; break;
case 'o': code = 0364; break;
case 'u': code = 0373; break;
default:
break;
};
break;
case '"':
switch(ch2) {
case 'A': code = 0304; break;
case 'E': code = 0313; break;
case 'I': code = 0317; break;
case 'O': code = 0326; break;
case 'U': code = 0334; break;
case 'a': code = 0344; break;
case 'e': code = 0353; break;
case 'i': code = 0357; break;
case 'o': code = 0366; break;
case 'u': code = 0374; break;
case 'y': code = 0377; break;
default:
break;
};
break;
case '~':
switch(ch2) {
case 'A': code = 0303; break;
case 'N': code = 0321; break;
case 'O': code = 0325; break;
case 'a': code = 0343; break;
case 'n': code = 0361; break;
case 'o': code = 0365; break;
default:
break;
};
break;
case 'c':
switch(ch2) {
case 'C': code = 0307; break;
case 'c': code = 0347; break;
default:
break;
};
break;
case 'o':
switch(ch2) {
case 'A': code = 0305; break;
case 'a': code = 0345; break;
default:
break;
};
break;
case 'A':
if (ch2=='A') {
code = 0305;
};
if (ch2=='E') {
code = 0305;
};
break;
case 'a':
if (ch2=='a') {
code = 0345;
};
if (ch2=='e') {
code = 0346;
};
break;
case 's':
if (ch2=='s') {
code = 0337;
};
break;
default:
break;
};
return(code);
}
static int getISO(char s[], int j, int* code)
/* convert input into 8-bit character code */
/* s[] is input string, j is place in string */
/* returns new place in string and writes 8-bit value to code */
{
int i;
int count;
i =j;
if (s[i]=='\\') {
/* look for 2 character code */
*code = ISOencoding(s[i+1], s[i+2]);
if (*code != 0) {
i = i+3;
} else {
/* look for octal code */
i = i+1;
*code = 0;
count = 0;
while ((s[i]>='0')&&(s[i]<='7')&&(count<3)) {
*code = ((*code)*8+(int)s[i]-'0')%256;
count = count+1;
i = i+1;
};
if (*code == 0) {
*code = '\\';
};
};
} else {
*code = (int)s[i] & 0xFF;
i = i + 1;
};
return(i);
}
static void ISOdecode(char s[], char out[])
/* convert coded characters to straight 8-bit ascii */
{
int i, j, len;
int code;
len = strlen(s);
i = 0;
j = 0;
while (i<len) {
i = getISO(s, i, &code);
out[j] = (char)code;
j = j + 1;
};
out[j] = '\0';
}
static void ISOfprintf(char s[])
/* interpret special characters and print to file */
{
int i, len, ch;
int code;
len = strlen(s);
i = 0;
while (i<len) {
switch(s[i]) {
case '(':
fprintf(f, "\\(");
i = i+1;
break;
case ')':
fprintf(f, "\\)");
i = i+1;
break;
case '\\':
i = getISO(s, i, &code);
fprintf(f, "\\%03o", code);
break;
default:
ch = 0xFF & (int)s[i];
if ((ch > 31) && (ch < 128)) {
fprintf(f, "%c", s[i]);
} else {
fprintf(f, "\\%03o", 0xFF & (int)s[i]);
};
i = i+1;
break;
};
};
}
static double stringwidth(char* str, double ptsize, int fontno)
/* calculate width of string */
{
int i, len, ch;
double width;
int code;
double baseptsize;
double* charwidth;
if (fontno == 3) {
charwidth = &helvetica_width[0];
baseptsize = 12.0;
} else {
charwidth = ×bold_width[0];
baseptsize = 13.0;
};
width = 0.0;
len = strlen(str);
i = 0;
while (i < len) {
i = getISO(str, i, &code);
ch = code - 32;
if ((ch >= 0) && (ch < 224)) {
width = width + charwidth[ch];
} else {
width = width + charwidth[0]; /* approximate with space */
};
};
return(width*ptsize/baseptsize);
}
void setscaling(char* s)
/* scaling value from string following -s in arglist */
{
double f;
f = -1.0;
sscanf(s, "%lf", &f);
if (f < 0.001) {
f = TUNE_SCALING;
};
scale = f;
}
void setmargins(s)
char* s;
/* set margin values from string following -M in arglist */
{
int i, j;
xmargin = XMARGIN;
ymargin = YMARGIN;
if (s != NULL) {
i = -1;
j = -1;
sscanf(s, "%dx%d", &i, &j);
if ((i != -1) && (j != -1)) {
xmargin = i;
ymargin = j;
};
};
}
void setpagesize(s)
char* s;
/* set page size from string following -P in arglist */
{
int i, j;
pagelen = A4_PAGELEN - 2 * ymargin;
pagewidth = A4_PAGEWIDTH - 2 * xmargin;
if (s != NULL) {
i = 0;
j = 0;
sscanf(s, "%dx%d", &i, &j);
if (i == 0) {
pagelen = A4_PAGELEN - 2 * ymargin;
pagewidth = A4_PAGEWIDTH - 2 * xmargin;
};
if (i == 1) {
pagelen = US_LETTER_PAGELEN - 2 * ymargin;
pagewidth = US_LETTER_PAGEWIDTH - 2 * xmargin;
};
if ((i > 100) && (i > 2*xmargin) && (j > 100) && (j > 2*ymargin)) {
pagewidth = i - 2 * xmargin;
pagelen = j - 2 * ymargin;
};
};
if (landscape) {
i = xmargin;
xmargin = ymargin;
ymargin = i;
i = pagelen;
pagelen = pagewidth;
pagewidth = i;
};
scaledlen = ((double)(pagelen))/scale;
scaledwidth = ((double)(pagewidth))/scale;
}
static void set_space(afont, s)
/* set vertical space to appear above a line in the given font */
/* s is a string specifying space in points (default), centimetres */
/* or inches */
struct font* afont;
char* s;
{
int count;
double x;
char units[40];
count = sscanf(s, "%lf%s", &x, units);
if (count > 0) {
if ((count >= 2) && (strncmp(units, "cm", 2) == 0)) {
x = x*28.3;
};
if ((count >= 2) && (strncmp(units, "in", 2) == 0)) {
x = x*72.0 ;
};
afont->space = (int)x;
};
}
static void init_font(afont, ptsize, spce, defnum, specialnum)
/* initialize font structure with given values */
struct font* afont;
int ptsize, spce, defnum, specialnum;
{
afont->pointsize = ptsize;
afont->space = spce;
afont->default_num = defnum;
afont->special_num = specialnum;
afont->name = NULL;
afont->defined = 0;
}
static void startpage()
/* Encapsulated PostScript for a page header */
{
fprintf(f, "%%%%Page: %d %d\n", pagecount, pagecount);
fprintf(f, "%%%%BeginPageSetup\n");
fprintf(f, "gsave\n");
if (landscape) {
fprintf(f, "90 rotate %d %d T\n", xmargin, -ymargin);
} else {
fprintf(f, "%d %d T\n", xmargin, ymargin+pagelen);
};
fprintf(f, "0.8 setlinewidth 0 setlinecap\n");
fprintf(f, "%.3f %.3f scale\n", scale, scale);
fprintf(f, "%%%%EndPageSetup\n\n");
fontnum = 0;
fontsize = 0;
totlen = 0.0;
oldplace = 0.0;
descend = 0.0;
}
static void closepage()
/* Encapsulated PostScript for page end */
{
if (pagenumbering) {
setfont(12, 3);
pagebottom();
fprintf(f, "(%d) %.1f 0 M cshow\n", pagecount, scaledwidth/2.0);
};
fprintf(f, "%%%%PageTrailer\n");
fprintf(f, "grestore\n");
fprintf(f, "showpage\n\n");
pagecount = pagecount + 1;
}
void newpage()
/* create PostScript for a new page of paper */
/* everything will now be printed on this fresh sheet */
/* ignore the command if the sheet is completely blank */
{
if (totlen > 0.0) {
closepage();
startpage();
};
}
static void pagebottom()
/* move to the bottom of the page */
{
fprintf(f, "0 %.1f T\n", -(scaledlen - totlen));
totlen = scaledlen;
descend = 0.0;
}
static void newblock(double height, double descender)
/* The page is modelled as a series of vertical bands */
/* This routine finds room for a fresh band below the current one */
/* It also does a translation to set a new y=0 line */
/* Subsequent calls may draw up to height units above or */
/* descender units below the line y=0 */
{
if (totlen+(descend+height+descender) > scaledlen - (double)(pagenumbering*12)) {
newpage();
};
fprintf(f, "0 %.1f T\n", - descend - height);
totlen = totlen + (descend + height);
descend = descender;
}
static void staveline()
/* draw 5 lines of a stave */
{
fprintf(f, "%.1f staff\n", scaledwidth);
}
static void printclef(struct aclef* t, double x, double yup, double ydown)
/* draw a clef of the specified type */
{
switch (t->type) {
case treble:
fprintf(f, "%.1f tclef\n", x);
break;
case bass:
fprintf(f, "%.1f bclef\n", x);
break;
case alto:
fprintf(f, "%.1f cclef\n", x);
break;
case baritone:
fprintf(f, "0 %d T %.1f cclef 0 %d T\n", 4*TONE_HT, x, -4*TONE_HT);
break;
case tenor:
fprintf(f, "0 %d T %.1f cclef 0 %d T\n", 2*TONE_HT, x, -2*TONE_HT);
break;
case mezzo:
fprintf(f, "0 %d T %.1f cclef 0 %d T\n", -2*TONE_HT, x, 2*TONE_HT);
break;
case soprano:
fprintf(f, "0 %d T %.1f cclef 0 %d T\n", -4*TONE_HT, x, 4*TONE_HT);
break;
default:
break;
};
if (t->octave > 0) {
fprintf(f, "%.1f %.1f (%d) bnum\n", x, yup - CLEFNUM_HT + 3, t->octave);
};
if (t->octave < 0) {
fprintf(f, "%.1f %.1f (%d) bnum\n", x, -ydown, - t->octave);
};
}
void set_keysig(struct key* k, struct key* newval)
/* copy across key signature */
{
int i;
for (i=0; i<7; i++) {
k->map[i] = newval->map[i];
k->mult[i] = newval->mult[i];
};
k->sharps = newval->sharps;
}
static double size_keysig(char oldmap[], char newmap[])
/* compute width taken up by drawing the key signature */
{
int i, n;
n = 0;
for (i=0; i<7; i++) {
if (((newmap[i] == '=')&&(oldmap[i] != '=')) ||
(newmap[i] == '^') || (newmap[i] == '_')) {
n = n + 1;
};
};
return((double)n * 5.0);
}
static double size_timesig(struct fract* meter)
/* compute width of the time signature */
{
double len1, len2;
char temp[20];
sprintf(temp, "%d", meter->num);
len1 = stringwidth(temp, 16.0, 2);
sprintf(temp, "%d", meter->denom);
len2 = stringwidth(temp, 16.0, 2);
if (len1 > len2) {
return(len1);
} else {
return(len2);
};
}
static void draw_keysig(char oldmap[], char newmap[], int newmult[],
double x, struct aclef* clef)
/* draw key specified key signature at position x on line */
/* arrays oldmap[], newmap[], newmult[] are indexed with a=0 to g=7 */
/* sharp_pos[] and flat_pos[] give order of notes */
/* e.g. sharp_pos[0] = 8 means 1st sharp drawn is conventionally f */
/* which is in position 8 on the stave (bottom line is E = 0) */
{
double xpos;
static int sharp_pos[7] = { 8, 5, 9, 6, 3, 7, 4 };
static int flat_pos[7] = { 4, 7, 3, 6, 2, 5, 1};
int i;
int offset, pos;
int note;
switch (clef->type) {
case treble:
offset = 0;
break;
case soprano:
offset = 2;
break;
case mezzo:
offset = 4;
break;
case alto:
offset = 6;
break;
case tenor:
offset = 8;
break;
case baritone:
offset = 10;
break;
case bass:
offset = 12;
break;
case noclef:
break;
};
xpos = x;
/* draw naturals to cancel out old accidentals */
for (i=0; i<7; i++) {
note = (sharp_pos[i] + 4) % 7;
if ((newmap[note] == '=')&&(oldmap[note] != '=')) {
pos = (sharp_pos[i] + offset - 3) % 7 + 3;
fprintf(f, " %.1f %d nt0", xpos, pos*TONE_HT);
xpos = xpos + 5;
};
};
/* draw sharps */
for (i=0; i<7; i++) {
note = (sharp_pos[i] + 4) % 7;
if (newmap[note] == '^') {
pos = (sharp_pos[i] + offset - 3) % 7 + 3;
if (newmult[note] == 2) {
fprintf(f, " %.1f %d dsh0", xpos, pos*TONE_HT);
} else {
fprintf(f, " %.1f %d sh0", xpos, pos*TONE_HT);
};
xpos = xpos + 5;
};
};
/* draw flats */
for (i=0; i<7; i++) {
note = (flat_pos[i] + 4) % 7;
if (newmap[note] == '_') {
pos = (flat_pos[i] + offset - 1)%7 + 1;
if (newmult[note] == 2) {
fprintf(f, " %.1f %d dft0", xpos, pos*TONE_HT);
} else {
fprintf(f, " %.1f %d ft0", xpos, pos*TONE_HT);
};
xpos = xpos + 5;
};
};
fprintf(f, "\n");
}
static void draw_meter(struct fract* meter, double x)
/* draw meter (time signature) at specified x value */
{
fprintf(f, "%.1f (%d) (%d) tsig\n", x, meter->num, meter->denom);
}
static double maxstrwidth(struct llist* strings, double ptsize, int fontno)
/* return the width of longest string in the list */
{
double width, max;
char* str;
max = 0.0;
str = firstitem(strings);
while (str != NULL) {
width = stringwidth(str, ptsize, fontno);
if (width > max) {
max = width;
};
str = nextitem(strings);
};
return(max);
}
static void sizerest(struct rest* r, struct feature* ft)
/* compute width of rest element */
{
double width;
if (r->multibar > 0) {
ft->xleft = 0.0;
ft->xright = 40.0;
} else {
switch (r->base_exp) {
case 1:
case 0:
ft->xleft = 2.5;
ft->xright = 2.5;
break;
case -1:
ft->xleft = 2.5;
ft->xright = 2.5;
break;
case -2:
ft->xleft = 3.0;
ft->xright = 4.0;
break;
case -3:
ft->xleft = 4.0;
ft->xright = 3.0;
break;
case -4:
ft->xleft = 5.0;
ft->xright = 3.0;
break;
case -5:
ft->xleft = 6.0;
ft->xright = 5.0;
break;
case -6:
default:
ft->xleft = 7.0;
ft->xright = 5.0;
break;
};
if (r->dots > 0) {
ft->xright = ft->xright + (r->dots* (float) DOT_SPACE);
};
};
if (r->gchords != NULL) {
width = maxstrwidth(r->gchords, (double)(gchordfont.pointsize),
gchordfont.default_num);
if (width > ft->xleft + ft->xright) {
ft->xright = (float) width - ft->xleft;
};
};
}
static int acc_upsize(char ch)
/* returns height above note of accent */
{
int size;
switch (ch) {
case '=':
size = NAT_UP;
break;
case '^':
size = SH_UP;
break;
case '_':
size = FLT_UP;
break;
default:
size = 0;
break;
};
return(size);
}
static int acc_downsize(char ch)
/* returns depth below note of accent */
{
int size;
switch (ch) {
case '=':
size = NAT_DOWN;
break;
case '^':
size = SH_DOWN;
break;
case '_':
size = FLT_DOWN;
break;
default:
size = 0;
break;
};
return(size);
}
static void setstemlen(struct note* n, int ingrace)
/* work out how long stem needs to be for an isolated note */
/* if the note is in a beamed set, the length may be altered later */
{
if (n->base_exp >= 0) {
n->stemlength = 0.0;
} else {
if (ingrace) {
if (n->beaming != single) {
n->stemlength = GRACE_STEMLEN; /* default stem length */
/* value will be recalculated later by setbeams() */
} else {
switch(n->base_exp) {
case -6:
n->stemlength = (float) GRACE_STEMLEN + 2* (float) TAIL_SEP* (float) 0.7;
break;
case -5:
n->stemlength = (float) GRACE_STEMLEN + (float) TAIL_SEP* (float) 0.7;
break;
default:
n->stemlength = (float) GRACE_STEMLEN; /* default stem length */
break;
};
};
} else {
if (n->beaming != single) {
n->stemlength = STEMLEN; /* default stem length */
/* value will be recalculated later by setbeams() */
} else {
switch(n->base_exp) {
case -6:
n->stemlength = (float) STEMLEN + 2* (float) TAIL_SEP;
break;
case -5:
n->stemlength = (float) STEMLEN + (float) TAIL_SEP;
break;
default:
n->stemlength = (float) STEMLEN; /* default stem length */
break;
};
};
};
};
}
static void sizenote(struct note* n, struct feature* f, int ingrace)
/* compute width and height of note element */
{
double width;
char* decorators;
int i;
f->ydown = (float) TONE_HT*(n->y - 2);
f->yup = (float) TONE_HT*(n->y + 2);
if (ingrace) {
f->xleft = (float) GRACE_HALF_HEAD;
f->xright = (float) GRACE_HALF_HEAD;
} else {
if (n->base_exp >= 1) {
f->xleft = HALF_BREVE;
f->xright = HALF_BREVE;
} else {
f->xleft = HALF_HEAD;
f->xright = HALF_HEAD;
};
if (n->fliphead) {
if (n->stemup) {
f->xright = (float) f->xright + (float) HALF_HEAD * 2;
} else {
f->xleft = f->xleft + (float) HALF_HEAD * 2;
};
};
};
if (n->dots > 0) {
f->xright = f->xright + (n->dots* (float) DOT_SPACE);
};
if ((n->stemup) && (n->base_exp <= -3) && (n->beaming==single)) {
if (f->xright < (HALF_HEAD + TAILWIDTH)) {
f->xright = HALF_HEAD + TAILWIDTH;
};
};
if (n->accidental != ' ') {
f->xleft = (float) HALF_HEAD + (float) ACC_OFFSET + (n->acc_offset-1) * (float) ACC_OFFSET2;
if (n->stemup) {
f->ydown = (float) TONE_HT*(n->y) - (float)(acc_downsize(n->accidental));
} else {
f->yup = (float) ((float) TONE_HT*(n->y) + (double)(acc_upsize(n->accidental)));
};
};
if (n->stemlength > 0.0) {
if (n->stemup) {
f->yup = TONE_HT*(n->y) + n->stemlength;
} else {
f->ydown = TONE_HT*(n->y) - n->stemlength;
};
};
if (n->accents != NULL) {
decorators = n->accents;
for (i=0; i<(int) strlen(decorators); i++) {
switch(decorators[i]) {
case 'H':
case '~':
case 'u':
case 'v':
case 'T':
f->yup = f->yup + BIG_DEC_HT;
break;
case 'R':
if (n->stemup) {
f->ydown = f->ydown + BIG_DEC_HT;
} else {
f->yup = f->yup + BIG_DEC_HT;
};
break;
case 'M':
case '.':
if (n->stemup) {
f->ydown = f->ydown + SMALL_DEC_HT;
} else {
f->yup = f->yup + SMALL_DEC_HT;
};
break;
default: