-
Notifications
You must be signed in to change notification settings - Fork 31
/
subs.c
1915 lines (1794 loc) · 39.6 KB
/
subs.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
/*
* Low-level utilities.
*
* This file is part of abcm2ps.
*
* Copyright (C) 1998-2019 Jean-François Moine (http://moinejf.free.fr)
* Adapted from abc2ps, Copyright (C) 1996-1998 Michael Methfessel
*
* 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 <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#ifdef HAVE_PANGO
#include <pango/pangocairo.h>
#include <pango/pangofc-font.h>
#endif
#include "abcm2ps.h"
char tex_buf[TEX_BUF_SZ]; /* result of tex_str() */
int outft = -1; /* last font in the output file */
static int stropx; /* index current string output operation */
static float strlw; /* line width */
static int curft; /* current (wanted) font */
static int defft; /* default font */
static char strtx; /* PostScript text outputing (bits) */
#define TX_STR 1 /* string started */
#define TX_ARR 2 /* glyph/string array started */
/* width of characters according to the encoding */
/* these are the widths for Times-Roman, extracted from the 'a2ps' package */
/*fixme-hack: set 500 to control characters for utf-8*/
static short cw_tb[] = {
500,500,500,500,500,500,500,500, // 00
500,500,500,500,500,500,500,500,
500,500,500,500,500,500,500,500, // 10
500,500,500,500,500,500,500,500,
250,333,408,500,500,833,778,333, // 20
333,333,500,564,250,564,250,278,
500,500,500,500,500,500,500,500, // 30
500,500,278,278,564,564,564,444,
921,722,667,667,722,611,556,722, // 40
722,333,389,722,611,889,722,722,
556,722,667,556,611,722,722,944, // 50
722,722,611,333,278,333,469,500,
333,444,500,444,500,444,333,500, // 60
500,278,278,500,278,778,500,500,
500,500,333,389,278,500,500,722, // 70
500,500,444,480,200,480,541,500,
};
// PS/SVG user definitions
// This structure is dynamically created by malloc
// so that the field 'text' has the exact size of the user string.
// The size of this field in the structure is big enough
// to avoid most warnings of the C compiler.
static struct u_ps {
struct u_ps *next;
char text[100000];
} *user_ps;
/* -- print message for internal error and maybe stop -- */
void bug(char *msg, int fatal)
{
error(1, NULL, "Internal error: %s.", msg);
if (fatal) {
fprintf(stderr, "Emergency stop.\n\n");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Trying to continue...\n");
}
/* -- print an error message -- */
void error(int sev, /* 0: warning, 1: error */
struct SYMBOL *s,
char *fmt, ...)
{
va_list args;
if (s) {
if (s->fn)
fprintf(stderr, "%s:%d:%d: ", s->fn,
s->linenum, s->colnum);
s->flags |= ABC_F_ERROR;
}
fprintf(stderr, sev == 0 ? "warning: " : "error: ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
if (sev > severity)
severity = sev;
}
/* -- capitalize a string -- */
static void cap_str(char *p)
{
while (*p != '\0') {
#if 1
/* pb with toupper - works with ASCII and some latin characters only */
unsigned char c;
c = (unsigned char) *p;
if (c >= 'a' && c <= 'z') {
*p = c & ~0x20;
} else if (c == 0xc3) {
p++;
c = *p;
if (c >= 0xa0 && c <= 0xbe && c != 0xb7)
*p = c & ~0x20;
} else if (c == 0xc4) {
p++;
c = *p;
if (c >= 0x81 && c <= 0xb7 && (c & 0x01))
(*p)--;
}
#else
*p = toupper((unsigned char) *p);
#endif
p++;
}
}
/* -- return the character width -- */
float cwid(unsigned char c)
{
if (c >= 0x80) {
if (c < 0xc0)
return 0; // not start of utf8 character
c = 'a';
}
return (float) cw_tb[c] / 1000.;
}
/* -- change string taking care of some tex-style codes -- */
/* Return an estimated width of the string. */
float tex_str(char *s)
{
char *d, *p;
unsigned char c1;
unsigned maxlen, i;
float w, swfac;
w = 0;
d = tex_buf;
maxlen = sizeof tex_buf - 1; /* have room for EOS */
if ((i = curft) <= 0)
i = defft;
swfac = cfmt.font_tb[i].swfac;
while (1) {
c1 = (unsigned char) *s++;
if (c1 == '\0')
break;
switch (c1) {
case '\\':
c1 = *s++;
if (c1 == '\0') {
*d = '\0';
return w;
}
switch (c1) {
case 'n':
c1 = '\n';
break;
case 't':
c1 = '\t';
break;
}
break;
case '$':
if (isdigit((unsigned char) *s)
&& (unsigned) (*s - '0') < FONT_UMAX) {
i = *s - '0';
if (i == 0)
i = defft;
swfac = cfmt.font_tb[i].swfac;
if (--maxlen <= 0)
break;
*d++ = c1;
c1 = *s++;
goto addchar_nowidth;
}
if (*s == '$') {
if (--maxlen <= 0)
break;
*d++ = c1;
s++;
}
break;
case '&': /* treat XML characters */
if (svg || epsf > 1) {
p = strchr(s, ';');
if (!p || p - s >= 10)
break;
*d++ = c1;
while (s <= p)
*d++ = *s++;
w += cwid('a') * swfac;
continue;
}
if (*s == '#') {
int j;
long v;
if (s[1] == 'x')
i = sscanf(s, "#x%lx;%n", &v, &j);
else
i = sscanf(s, "#%ld;%n", &v, &j);
if (i != 1) {
error(0, NULL, "Bad XML char reference");
break;
}
if (v < 0x80) { /* convert to UTF-8 */
*d++ = v;
} else if (v < 0x800) {
*d++ = 0xc0 | (v >> 6);
*d++ = 0x80 | (v & 0x3f);
} else if (v < 0x10000) {
*d++ = 0xe0 | (v >> 12);
*d++ = 0x80 | ((v >> 6) & 0x3f);
*d++ = 0x80 | (v & 0x3f);
} else {
*d++ = 0xf0 | (v >> 18);
*d++ = 0x80 | ((v >> 12) & 0x3f);
*d++ = 0x80 | ((v >> 6) & 0x3f);
*d++ = 0x80 | (v & 0x3f);
}
w += cwid('a') * swfac;
s += j;
continue;
}
if (strncmp(s, "lt;", 3) == 0) {
c1 = '<';
s += 3;
} else if (strncmp(s, "gt;", 3) == 0) {
c1 = '>';
s += 3;
} else if (strncmp(s, "amp;", 4) == 0) {
c1 = '&';
s += 4;
} else if (strncmp(s, "apos;", 5) == 0) {
c1 = '\'';
s += 5;
} else if (strncmp(s, "quot;", 5) == 0) {
c1 = '"';
s += 5;
}
break;
}
if (c1 >= 0x80) {
if (c1 >= 0xc0)
w += cwid('a') * swfac; // start of unicode char
} else if (c1 <= 5) { /* accidentals from gchord */
if (--maxlen < 4)
break;
switch (c1) {
case 1:
*d++ = 0xe2;
*d++ = 0x99;
*d++ = 0xaf;
break;
case 2:
*d++ = 0xe2;
*d++ = 0x99;
*d++ = 0xad;
break;
case 3:
*d++ = 0xe2;
*d++ = 0x99;
*d++ = 0xae;
break;
case 4:
*d++ = 0xf0;
*d++ = 0x9d;
*d++ = 0x84;
*d++ = 0xaa;
break;
case 5:
*d++ = 0xf0;
*d++ = 0x9d;
*d++ = 0x84;
*d++ = 0xab;
break;
}
w += cwid('a') * swfac;
continue;
} else {
w += cwid(c1) * swfac;
}
addchar_nowidth:
if (--maxlen <= 0)
break;
*d++ = c1;
}
*d = '\0';
if (maxlen <= 0)
error(0, NULL, "Text too large - ignored part: '%s'", s);
return w;
}
#ifdef HAVE_PANGO
#define PG_SCALE (PANGO_SCALE * 72 / 96) /* 96 DPI */
static PangoFontDescription *desc_tb[MAXFONTS];
static PangoLayout *layout = (PangoLayout *) -1;
static PangoAttrList *attrs;
static int out_pg_ft = -1; /* current pango font */
static GString *pg_str;
/* -- initialize the pango mechanism -- */
void pg_init(void)
{
static PangoContext *context;
context = pango_font_map_create_context(
pango_cairo_font_map_get_default());
if (context)
layout = pango_layout_new(context);
if (!layout) {
error(0, NULL, "pango disabled\n");
cfmt.pango = 0;
} else {
pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
pg_str = g_string_sized_new(256);
}
}
void pg_reset_font(void)
{
out_pg_ft = -1;
}
static void desc_font(int fnum)
{
int i;
char font_name[128], *p, *q;
static char *bad_tb[] = {
"Times-Roman", "Times", "Helvetica", "Courier"
};
static char *good_tb[] = {
"serif", "serif", "sans-serif", "monospace"
};
// build a font description replacing X11 font names by generic names
if (!desc_tb[fnum]) {
p = font_name;
q = fontnames[fnum];
for (i = 0; i < sizeof bad_tb / sizeof bad_tb[0]; i++) {
if (strncmp(q, bad_tb[i], strlen(bad_tb[i])) == 0) {
p += sprintf(p, "%s", good_tb[i]);
q += strlen(bad_tb[i]);
break;
}
}
snprintf(p, sizeof font_name - 5, "%s 10", q); // (dummy size)
// change '-' to a space
while (*p != '\0') {
if (*p == '-')
*p = ' ';
if (isupper(*p))
*p = tolower(*p);
p++;
}
// add spaces between styles/weight
p = strstr(&font_name[1], "italic");
if (p && p[-1] != ' ') {
memmove(p + 1, p, strlen(p) + 1);
*p = ' ';
}
p = strstr(&font_name[1], "oblique");
if (p && p[-1] != ' ') {
memmove(p + 1, p, strlen(p) + 1);
*p = ' ';
}
p = strstr(&font_name[1], "bold");
if (p && p[-1] != ' ') {
memmove(p + 1, p, strlen(p) + 1);
*p = ' ';
}
// create the font description
desc_tb[fnum] = pango_font_description_from_string(font_name);
}
}
/* output a line */
static void pg_line_output(PangoLayoutLine *line)
{
GSList *runs_list;
PangoGlyphInfo *glyph_info;
char tmp[256];
const char *fontname = NULL;
int ret, glypharray;
outft = -1;
glypharray = 0;
for (runs_list = line->runs; runs_list; runs_list = runs_list->next) {
PangoLayoutRun *run = runs_list->data;
PangoItem *item = run->item;
PangoGlyphString *glyphs = run->glyphs;
PangoAnalysis *analysis = &item->analysis;
PangoFont *font = analysis->font;
PangoFcFont *fc_font = PANGO_FC_FONT(font);
FT_Face face = pango_fc_font_lock_face(fc_font);
PangoFontDescription *ftdesc = pango_font_describe(font);
int wi = pango_font_description_get_size(ftdesc);
int i, c;
for (i = 0; i < glyphs->num_glyphs; i++) {
glyph_info = &glyphs->glyphs[i];
c = glyph_info->glyph;
if (c == PANGO_GLYPH_EMPTY)
continue;
if (c & PANGO_GLYPH_UNKNOWN_FLAG) {
c &= ~PANGO_GLYPH_UNKNOWN_FLAG;
error(0, NULL, "char %04x not treated\n", c);
continue;
}
ret = FT_Load_Glyph(face,
c, // PangoGlyph = index
FT_LOAD_NO_SCALE);
if (ret != 0) {
error(0, NULL, "freetype error %d\n", ret);
} else if (FT_HAS_GLYPH_NAMES(face)) {
if (FT_Get_Postscript_Name(face) != fontname) {
fontname = FT_Get_Postscript_Name(face);
if (glypharray)
a2b("]glypharray");
a2b("\n/%s %.1f selectfont[",
fontname,
(float) wi / PG_SCALE);
glypharray = 1;
}
FT_Get_Glyph_Name(face, c,
tmp, sizeof tmp);
a2b("/%s", tmp);
} else {
error(0, NULL, "!! no glyph %d in %s-%s\n",
c, face->family_name, face->style_name);
}
}
pango_fc_font_unlock_face(fc_font);
}
if (glypharray)
a2b("]glypharray");
}
static void str_font_change(int start,
int end)
{
struct FONTSPEC *f;
int fnum;
PangoAttribute *attr1, *attr2;
f = &cfmt.font_tb[curft];
fnum = f->fnum;
if (f->size == 0) {
error(0, NULL, "Font \"%s\" with a null size - set to 8",
fontnames[fnum]);
f->size = 8;
}
desc_font(fnum);
attr1 = pango_attr_font_desc_new(desc_tb[fnum]);
attr1->start_index = start;
attr1->end_index = end;
pango_attr_list_insert(attrs, attr1);
attr2 = pango_attr_size_new((int) (f->size * PG_SCALE));
attr2->start_index = start;
attr2->end_index = end;
pango_attr_list_insert(attrs, attr2);
}
static void str_set_font(char *p)
{
GString *str;
char *q;
int start;
str = pg_str;
start = str->len;
q = p;
while (*p != '\0') {
switch (*p) {
case '$':
if (isdigit((unsigned char) p[1])
&& (unsigned) (p[1] - '0') < FONT_UMAX) {
if (p > q)
str = g_string_append_len(str, q, p - q);
if (curft != p[1] - '0') {
str_font_change(start, str->len);
start = str->len;
curft = p[1] - '0';
if (curft == 0)
curft = defft;
}
p += 2;
q = p;
continue;
}
if (p[1] == '$') {
str = g_string_append_len(str, q, p - q);
q = ++p;
}
break;
}
p++;
}
if (p > q) {
str = g_string_append_len(str, q, p - q);
str_font_change(start, str->len);
}
pg_str = str;
}
/* -- output a string using the pango and freetype libraries -- */
static void str_pg_out(char *p, int action)
{
PangoLayoutLine *line;
int wi;
float w;
if (out_pg_ft != curft)
out_pg_ft = -1;
/* guitar chord with TABs */
if (action == A_GCHEXP) {
char *q;
/* get the inter TAB width (see draw_gchord) */
q = mbf - 1;
while (q[-1] != ' ')
q--;
mbf = q;
w = atof(q);
for (;;) {
q = strchr(p, '\t');
if (!q)
break;
*q = '\0';
str_pg_out(p, A_LEFT);
a2b(" %.1f 0 RM ", w);
p = q + 1;
}
}
attrs = pango_attr_list_new();
str_set_font(p);
pango_layout_set_text(layout, pg_str->str, pg_str->len);
pango_layout_set_attributes(layout, attrs);
/* only one line */
line = pango_layout_get_line_readonly(layout, 0);
switch (action) {
case A_CENTER:
case A_RIGHT:
pango_layout_get_size(layout, &wi, NULL);
if (action == A_CENTER)
wi /= 2;
// w = (float) wi / PG_SCALE;
w = (float) wi / PANGO_SCALE;
a2b(" -%.1f 0 RM", w);
break;
}
pg_line_output(line);
pango_layout_set_attributes(layout, NULL);
pg_str = g_string_truncate(pg_str, 0);
pango_attr_list_unref(attrs);
}
/* output a justified or filled paragraph */
static void pg_para_output(int job)
{
GSList *lines, *runs_list;
PangoLayoutLine *line;
PangoGlyphInfo *glyph_info;
char tmp[256];
const char *fontname = NULL;
int ret, glypharray;
int wi;
float y;
pango_layout_set_text(layout, pg_str->str,
pg_str->len - 1); /* remove the last space */
pango_layout_set_attributes(layout, attrs);
outft = -1;
glypharray = 0;
wi = 0;
y = 0;
lines = pango_layout_get_lines_readonly(layout);
for (; lines; lines = lines->next) {
PangoRectangle pos;
line = lines->data;
pango_layout_line_get_extents(line, NULL, &pos);
y += (float) pos.height
* .87 /* magic! */
/ PANGO_SCALE;
for (runs_list = line->runs; runs_list; runs_list = runs_list->next) {
PangoLayoutRun *run = runs_list->data;
PangoItem *item = run->item;
PangoGlyphString *glyphs = run->glyphs;
PangoAnalysis *analysis = &item->analysis;
PangoFont *font = analysis->font;
PangoFcFont *fc_font = PANGO_FC_FONT(font);
FT_Face face = pango_fc_font_lock_face(fc_font);
PangoFontDescription *ftdesc =
pango_font_describe(font);
int i, g, set_move, x;
if (pango_font_description_get_size(ftdesc) != wi) {
wi = pango_font_description_get_size(ftdesc);
fontname = NULL;
}
pango_layout_index_to_pos(layout, item->offset, &pos);
x = pos.x;
set_move = 1;
for (i = 0; i < glyphs->num_glyphs; i++) {
glyph_info = &glyphs->glyphs[i];
g = glyph_info->glyph;
if (g == PANGO_GLYPH_EMPTY)
continue;
if (set_move) {
set_move = 0;
if (glypharray) {
a2b("]glypharray");
glypharray = 0;
}
a2b("\n");
a2b("%.2f %.2f M ",
(float) x / PANGO_SCALE, -y);
}
x += glyph_info->geometry.width;
if (g & PANGO_GLYPH_UNKNOWN_FLAG) {
g &= ~PANGO_GLYPH_UNKNOWN_FLAG;
error(0, NULL, "char %04x not treated\n", g);
continue;
}
ret = FT_Load_Glyph(face,
g, // PangoGlyph = index
FT_LOAD_NO_SCALE);
if (ret != 0) {
fprintf(stdout, "%%%% freetype error %d\n", ret);
} else if (FT_HAS_GLYPH_NAMES(face)) {
if (FT_Get_Postscript_Name(face) != fontname) {
fontname = FT_Get_Postscript_Name(face);
if (glypharray)
a2b("]glypharray");
a2b("\n/%s %.1f selectfont[",
fontname,
(float) wi / PG_SCALE);
glypharray = 1;
}
FT_Get_Glyph_Name((FT_FaceRec *) face, g,
tmp, sizeof tmp);
if (job == T_JUSTIFY
&& strcmp(tmp, "space") == 0) {
set_move = 1;
continue;
}
if (!glypharray) {
a2b("[");
glypharray = 1;
}
a2b("/%s", tmp);
} else {
a2b("%% glyph: %s %d\n",
FT_Get_Postscript_Name(face), g);
}
}
pango_fc_font_unlock_face(fc_font);
if (glypharray) {
a2b("]glypharray\n");
glypharray = 0;
}
}
if (glypharray) {
a2b("]glypharray\n");
glypharray = 0;
}
}
bskip(y);
pango_layout_set_attributes(layout, NULL);
pg_str = g_string_truncate(pg_str, 0);
}
/* output of filled / justified text */
static void pg_write_text(char *s, int job, float parskip)
{
char *p;
curft = defft;
pango_layout_set_width(layout, strlw * PANGO_SCALE);
pango_layout_set_justify(layout, job == T_JUSTIFY);
attrs = pango_attr_list_new();
p = s;
while (*p != '\0') {
if (*p++ != '\n')
continue;
if (*p == '\n') { /* if empty line */
p[-1] = '\0';
tex_str(s);
str_set_font(tex_buf);
if (pg_str->len > 0)
pg_para_output(job);
bskip(parskip);
buffer_eob(0);
s = ++p;
continue;
}
//fixme: maybe not useful
p[-1] = ' ';
}
tex_str(s);
str_set_font(tex_buf);
if (pg_str->len)
pg_para_output(job);
pango_attr_list_unref(attrs);
}
/* check if pango is needed */
static int is_latin(unsigned char *p)
{
while (*p != '\0') {
if (*p >= 0xc6) {
if (*p == 0xe2) {
if (p[1] != 0x99
|| p[2] < 0xad || p[2] > 0xaf)
return 0;
p += 2;
} else if (*p == 0xf0) {
if (p[1] != 0x9d
|| p[2] != 0x84
|| p[3] < 0xaa || p[3] > 0xab)
return 0;
} else {
return 0;
}
}
p++;
}
return 1;
}
#endif /* HAVE_PANGO */
/* -- set the default font of a string -- */
void str_font(int ft)
{
curft = defft = ft;
}
/* -- get the current and default fonts -- */
void get_str_font(int *cft, int *dft)
{
*cft = curft;
*dft = defft;
}
/* -- set the current and default fonts -- */
void set_str_font(int cft, int dft)
{
curft = cft;
defft = dft;
}
static char *strop_tb[] = { /* index = action (A_xxxx) * 2 */
"show", "arrayshow", // left
"showc", "arrayshow", // center
"showr", "arrayshow", // right
"lyshow", "alyshow", // lyric
"gcshow", "agcshow", // gchord
"anshow", "aanshow", // annot
"gxshow", "arrayshow", // gchexp
"strop", "arrayshow", // (7 = justify)
};
/* close a string */
static void str_end(int end)
{
if (strtx & TX_STR) {
a2b(")");
strtx &= ~TX_STR;
if (!(strtx & TX_ARR)) {
a2b("%s", strop_tb[stropx]);
return;
}
}
if (!end || !(strtx & TX_ARR))
return;
strtx &= ~TX_ARR;
a2b("]%s", strop_tb[stropx + 1]);
}
/* check if some non ASCII characters */
static int non_ascii_p(char *p)
{
while (*p != '\0') {
if ((signed char) *p++ < 0)
return 1;
}
return 0;
}
/* -- output one string -- */
static void str_ft_out1(char *p, int l)
{
if (curft != outft) {
str_end(1);
a2b(" ");
set_font(curft);
}
if (!(strtx & TX_STR)) {
a2b("(");
strtx |= TX_STR;
}
a2b("%.*s", l, p);
}
/* -- output a string handling the font changes -- */
static void str_ft_out(char *p, int end)
{
int use_glyph;
char *q;
use_glyph = !svg && epsf <= 1 && /* not SVG */
get_font_encoding(curft) == 0; /* utf-8 font */
if (use_glyph && non_ascii_p(p)) {
if (curft != outft) {
str_end(1);
a2b(" ");
set_font(curft);
}
str_end(0);
if (!(strtx & TX_ARR)) {
a2b("[");
strtx |= TX_ARR;
}
}
q = p;
while (*p != '\0') {
if ((signed char) *p < 0
&& use_glyph) {
if (p > q)
str_ft_out1(q, p - q);
str_end(0);
if (curft != outft) {
str_end(1);
a2b(" ");
set_font(curft);
}
if (!(strtx & TX_ARR)) {
a2b("[");
strtx |= TX_ARR;
}
q = p = glyph_out(p);
continue;
}
switch (*p) {
case '$':
if (isdigit((unsigned char) p[1])
&& (unsigned) (p[1] - '0') < FONT_UMAX) {
if (p > q)
str_ft_out1(q, p - q);
if (curft != p[1] - '0') {
curft = p[1] - '0';
if (curft == 0)
curft = defft;
use_glyph = !svg && epsf <= 1 &&
get_font_encoding(curft) == 0;
}
p += 2;
q = p;
continue;
}
if (p[1] == '$') {
str_ft_out1(q, p - q);
q = ++p;
}
break;
case '(':
case ')':
case '\\':
if (p > q)
str_ft_out1(q, p - q);
str_ft_out1("\\", 1);
q = p;
break;
}
p++;
}
if (p > q)
str_ft_out1(q, p - q);
if (end && strtx)
str_end(1);
}
/* -- output a string, handling the font changes -- */
void str_out(char *p, int action)
{
if (curft <= 0) /* first call */
curft = defft;
/* special case when font change at start of text */
if (*p == '$' && isdigit((unsigned char) p[1])
&& (unsigned) (p[1] - '0') < FONT_UMAX) {
if (curft != p[1] - '0') {
curft = p[1] - '0';
if (curft == 0)
curft = defft;
}
p += 2;
}
#ifdef HAVE_PANGO
//fixme: pango KO if user modification of ly/gc/an/gxshow
/* use pango if some characters are out of the utf-array (in syms.c) */
if (cfmt.pango) {
if (cfmt.pango == 2 || !is_latin((unsigned char *) p)) {
str_pg_out(p, action); /* output the string */
return;
}
}
#endif
stropx = action * 2;
/* direct output if no font change and only ASCII characters */
if (!strchr(p, '$')
&& !non_ascii_p(p)) {
str_ft_out(p, 1); /* output the string */
return;
}
/* if not left aligned, build a PS function */
switch (action) {
case A_CENTER:
case A_RIGHT:
if (!svg && epsf <= 1) {
a2b("/str{");
outft = -1;
stropx = 0;
}
/* fall thru */
// default:
// if (!svg && epsf <= 1) /* if not SVG */
// stropx++;
break;
}
str_ft_out(p, 1); /* output the string */
/* if not left aligned, call the PS function */
if (svg || epsf > 1) /* not for SVG */
return;
if (action == A_CENTER || action == A_RIGHT) {
a2b("}def\n"
"strw w");
if (action == A_CENTER)
a2b(" 0.5 mul");
a2b(" neg 0 RM str");
}
}
/* -- output a string with TeX translation -- */
void put_str(char *str, int action)
{
tex_str(str);
str_out(tex_buf, action);
a2b("\n");
}
/* -- output a header information -- */
static void put_inf(struct SYMBOL *s)
{
char *p;
p = s->text;
if (p[1] == ':')
p += 2;
while (isspace((unsigned char) *p))
p++;
put_str(p, A_LEFT);
}