-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathz37.c
2133 lines (1922 loc) · 87.3 KB
/
z37.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
/*@z37.c:Font Service:Declarations@*******************************************/
/* */
/* THE LOUT DOCUMENT FORMATTING SYSTEM (VERSION 3.43) */
/* COPYRIGHT (C) 1991, 2008 Jeffrey H. Kingston */
/* */
/* Jeffrey H. Kingston (jeff@it.usyd.edu.au) */
/* School of Information Technologies */
/* The University of Sydney 2006 */
/* AUSTRALIA */
/* */
/* 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 3, 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., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA */
/* */
/* FILE: z37.c */
/* MODULE: Font Service */
/* EXTERNS: FontInit(), FontDefine(), FontChange(), FontWordSize(), */
/* FontSize(), FontHalfXHeight(), FontEncoding(), */
/* FontMapping(), FontFamilyAndFace(), FontNeeded() */
/* */
/* This module implements fonts, using encoding vectors and Adobe font */
/* metrics files (.AFM files, version 2). */
/* */
/*****************************************************************************/
#include "externs.h"
#define DEFAULT_XHEIGHT 500 /* the default XHeight if font has none */
#define NO_FONT 0 /* the not-a-font font number */
#define SZ_DFT 1000 /* default lout size is 50p */
#define INIT_FINFO_SIZE 100 /* initial number of sized fonts set aside */
/*****************************************************************************/
/* */
/* These definitions have been moved to "externs.h" since z24.c needs them: */
/* */
/* struct metrics { */
/* FULL_LENGTH up; */
/* FULL_LENGTH down; */
/* FULL_LENGTH left; */
/* FULL_LENGTH right; */
/* FULL_LENGTH last_adjust; */
/* }; */
/* */
/* typedef struc composite_rec { */
/* FULL_CHAR char_code; */
/* FULL_LENGTH x_offset; */
/* FULL_LENGTH y_offset; */
/* } COMPOSITE; */
/* */
/* typedef struct font_rec { */
/* struct metrics *size_table; metrics of sized fonts */
/* FULL_CHAR *lig_table; ligatures */
/* unsigned short *composite; non-zero means composite */
/* COMPOSITE *cmp_table; composites to build */
/* int cmp_top; length of cmp_table */
/* OBJECT font_table; record of sized fonts */
/* OBJECT original_face; face object of font */
/* FULL_LENGTH underline_pos; position of underline */
/* FULL_LENGTH underline_thick; thickness of underline */
/* unsigned short *kern_table; first kerning chars */
/* FULL_CHAR *kern_chars; second kerning chars */
/* unsigned char *kern_value; points into kern_lengths */
/* FULL_LENGTH *kern_sizes; sizes of kernings */
/* FULL_LENGTH bbox_lly; lly of font bbox */
/* FULL_LENGTH bbox_ury; ury of font bbox */
/* } FONT_INFO; */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Private data structures of this module */
/* */
/* +++++++++++++++++++++++++++ */
/* + + */
/* root -> + ACAT + */
/* + + */
/* + + */
/* +++++++++++++++++++++++++++ */
/* | font families... */
/* | */
/* +-----+-----------------------------------------------+ ... */
/* | | */
/* | | */
/* +++++++++++++++++++++++++++ */
/* + + */
/* family -> + WORD + */
/* + string (family name) + */
/* + + */
/* +++++++++++++++++++++++++++ */
/* | faces of this family... */
/* | */
/* +-----+-----------------------------------------------+ ... */
/* | | */
/* | | */
/* +++++++++++++++++++++++++++++++++ */
/* + + */
/* face -> + WORD + */
/* + string (face name) + */
/* + font_recoded + */
/* + font_mapping + */
/* + font_page + */
/* + + */
/* +++++++++++++++++++++++++++++++++ */
/* | size records... */
/* | */
/* +----------+---------+--------------------+-----------------------+ */
/* | | | | */
/* | | | | */
/* +++++++++++++++++++ +++++++++++++++++++ +++++++++++++++++++++ */
/* + + + + + + */
/* + WORD + + WORD + + WORD + */
/* + string (font + + string (AFM + + string (short + */
/* + name) + + file name) + + font name) + */
/* + + + + + font_num + */
/* +++++++++++++++++++ +++++++++++++++++++ + font_size + */
/* | + font_xheight2 + */
/* | + font_recoded + */
/* ++++++++++++++++++++ + font_mapping + */
/* + + + font_spacewidth + */
/* (optional) + WORD + + font_bbox_lly + */
/* + string (extra + + font_bbox_ury + */
/* + AFM file name) + + + */
/* + + +++++++++++++++++++++ */
/* ++++++++++++++++++++ */
/* */
/*****************************************************************************/
OBJECT FontDefSym; /* symtab entry for @FontDef */
int font_curr_page; /* current page number */
FONT_INFO *finfo; /* all the font table info */
static int finfo_size; /* current finfo array size */
static OBJECT font_root; /* root of tree of fonts */
static OBJECT font_used; /* fonts used on this page */
static FONT_NUM font_count; /* number of sized fonts */
static int font_seqnum; /* unique number for a font */
static OBJECT fd_tag; /* @FontDef @Tag entry */
static OBJECT fd_family; /* @FontDef @Family entry */
static OBJECT fd_face; /* @FontDef @Face entry */
static OBJECT fd_name; /* @FontDef @Name entry */
static OBJECT fd_metrics; /* @FontDef @Metrics entry */
static OBJECT fd_extra_metrics; /* @FontDef @ExtraMetrics */
static OBJECT fd_mapping; /* @FontDef @Mapping entry */
static OBJECT fd_recode; /* @FontDef @Recode entry */
/*@::FontInit(), FontDebug()@*************************************************/
/* */
/* FontInit() */
/* */
/* Initialise this module. */
/* */
/*****************************************************************************/
static OBJECT load(FULL_CHAR *name, unsigned dtype, OBJECT encl, BOOLEAN compulsory)
{ OBJECT res;
res = InsertSym(name, dtype, no_fpos, DEFAULT_PREC, FALSE, FALSE, 0, encl,
MakeWord(WORD, STR_EMPTY, no_fpos));
if( dtype == NPAR ) visible(res) = TRUE;
if( compulsory )
{ has_compulsory(encl)++;
is_compulsory(res) = TRUE;
}
return res;
}
void FontInit(void)
{
debug0(DFT, D, "FontInit()");
font_curr_page = 1;
font_count = 0;
New(font_root, ACAT);
New(font_used, ACAT);
font_seqnum = 0;
finfo = (FONT_INFO *) malloc(INIT_FINFO_SIZE * sizeof(FONT_INFO));
finfo_size = INIT_FINFO_SIZE;
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, 1, INIT_FINFO_SIZE * sizeof(FONT_INFO)));
/* set up FontDefSym */
FontDefSym = load(KW_FONTDEF, LOCAL, StartSym, FALSE);
fd_tag = load(KW_TAG, NPAR, FontDefSym, TRUE);
fd_family = load(KW_FAMILY, NPAR, FontDefSym, TRUE);
fd_face = load(KW_FACE, NPAR, FontDefSym, TRUE);
fd_name = load(KW_NAME, NPAR, FontDefSym, TRUE);
fd_metrics = load(KW_METRICS, NPAR, FontDefSym, TRUE);
fd_extra_metrics = load(KW_EXTRA_METRICS, NPAR, FontDefSym, FALSE);
fd_mapping = load(KW_MAPPING, NPAR, FontDefSym, TRUE);
fd_recode = load(KW_RECODE, NPAR, FontDefSym, FALSE);
debug0(DFT, D, "FontInit returning.");
}
/*****************************************************************************/
/* */
/* FontDebug() */
/* */
/* Print out font tree (not currectly used). */
/* */
/*****************************************************************************/
#if DEBUG_ON
static void FontDebug(void)
{ OBJECT family, face, link, flink, zlink, z; int i;
assert(font_root!=nilobj && type(font_root)==ACAT, "FontDebug: font_root!");
for( link = Down(font_root); link != font_root; link = NextDown(link) )
{ Child(family, link)
;
assert( is_word(type(family)), "FontDebug: family!" );
debug1(DFS, D, "family %s:", string(family));
for( flink = Down(family); flink != family; flink = NextDown(flink) )
{ Child(face, flink)
;
assert( is_word(type(face)), "FontDebug: face!" );
debug1(DFS, D, " face %s:", string(face));
for( zlink = Down(face); zlink != face; zlink = NextDown(zlink) )
{ Child(z, zlink)
;
if( is_word(type(z)) )
{ debug2(DFS, D, " %s%s", string(z), Down(z) != z ? " child" : "");
}
else
{ debug1(DFS, D, " %s", Image(type(z)));
}
}
}
}
for( i = 1; i <= font_count; i++ )
fprintf(stderr, " finfo[%d].font_table = %s%s", i,
EchoObject(finfo[i].font_table), STR_NEWLINE);
} /* end FontDebug */
/*****************************************************************************/
/* */
/* DebugKernTable(fnum) */
/* */
/* Print debug output of kern table for font fnum. */
/* */
/*****************************************************************************/
static void DebugKernTable(FONT_NUM fnum)
{ int i, j;
unsigned short *kt = finfo[fnum].kern_table;
FULL_CHAR *kc = finfo[fnum].kern_chars;
unsigned char *kv = finfo[fnum].kern_value;
FULL_LENGTH *ks = finfo[fnum].kern_sizes;
debug1(DFT, DD, "DebugKernTable(%d)", fnum);
for( i = 0; i < MAX_CHARS; i++ )
{ if( kt[i] != 0 )
{ debug1(DFT, DD, "kt[%d]:", i);
for( j = kt[i]; kc[j] != '\0'; j++ )
{ debug3(DFT, DD, "KPX %c %c %d", i, kc[j], ks[kv[j]]);
}
}
}
debug1(DFT, DD, "DebugKernTable(%d) returning", fnum);
} /* DebugKernTable */
#endif
/*****************************************************************************/
/* */
/* ReadCharMetrics(face, fixed_pitch, xheight2,lig,ligtop,fnum,fnt,lnum,fp) */
/* */
/* Read a sequence of character metrics lines. The font record is */
/* face, its ligatures are lig[0..ligtop], font number fnum, metrics fnt. */
/* The line number is lnum; input is to be read from file fp. */
/* */
/*****************************************************************************/
static void ReadCharMetrics(OBJECT face, BOOLEAN fixed_pitch, int xheight2,
FULL_CHAR *lig, int *ligtop, FILE_NUM fnum, struct metrics *fnt,
int *lnum, FILE *fp)
{ FULL_CHAR buff[MAX_BUFF], command[MAX_BUFF], ch, ligchar;
int prev_ligtop, prev_lig, i, wx = 0, llx = 0, lly = 0, urx = 0, ury = 0;
float fl_wx, fl_llx, fl_lly, fl_urx, fl_ury;
BOOLEAN wxfound, bfound;
OBJECT AFMfilename;
Child(AFMfilename, NextDown(Down(face)))
;
while( ReadOneLine(fp, buff, MAX_BUFF) != 0 &&
!StringBeginsWith(buff, AsciiToFull("EndCharMetrics")) &&
!StringBeginsWith(buff, AsciiToFull("EndExtraCharMetrics")) )
{
/* read one line containing metric info for one character */
debug1(DFT, DD, " ReadCharMetrics: %s", buff);
(*lnum)++; ch = '\0';
wxfound = bfound = FALSE;
i = 0; while( buff[i] == ' ' ) i++;
while( buff[i] != '\0' )
{
debug2(DFT, DDD, " ch = %d, &buff[i] = %s", ch, &buff[i]);
sscanf( (char *) &buff[i], "%s", command);
if( StringEqual(command, "N") )
{ sscanf( (char *) &buff[i], "N %s", command);
ch = MapCharEncoding(command, font_mapping(face));
}
else if( StringEqual(command, "WX") )
{ sscanf( (char *) &buff[i], "WX %f", &fl_wx);
wx = fl_wx;
wxfound = TRUE;
}
else if( StringEqual(command, "B") )
{ sscanf( (char *) &buff[i], "B %f %f %f %f",
&fl_llx, &fl_lly, &fl_urx, &fl_ury);
llx = fl_llx;
lly = fl_lly;
urx = fl_urx;
ury = fl_ury;
bfound = TRUE;
}
else if( StringEqual(command, "L") &&
BackEnd->uses_font_metrics && ch != '\0' )
{
prev_ligtop = *ligtop;
prev_lig = lig[ch];
if( lig[ch] == 1 ) lig[ch] = (*ligtop) - MAX_CHARS;
lig[(*ligtop)++] = ch;
i++; /* skip L */
while( buff[i] == ' ' ) i++;
while( buff[i] != ';' && buff[i] != '\0' )
{ sscanf( (char *) &buff[i], "%s", command);
ligchar = MapCharEncoding(command, font_mapping(face));
if( ligchar != '\0' ) lig[(*ligtop)++] = ligchar;
else
{ Error(37, 1, "ignoring unencoded ligature character %s in font file %s (line %d)",
WARN, &fpos(AFMfilename), command, FileName(fnum), *lnum);
lig[ch] = prev_lig; /* patch by Ludovic Courtes, added v. 3.31 */
*ligtop = prev_ligtop;
}
if( *ligtop > 2*MAX_CHARS - 5 )
Error(37, 2, "too many ligature characters in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), *lnum);
while( buff[i] != ' ' && buff[i] != ';' ) i++;
while( buff[i] == ' ' ) i++;
}
if( *ligtop != prev_ligtop ) /* this test by Ludovic Courtes, 3.31 */
lig[(*ligtop)++] = '\0';
}
while( buff[i] != ';' && buff[i] != '\0' ) i++;
if( buff[i] == ';' )
{ i++; while( buff[i] == ' ' ) i++;
}
}
if( ch > '\0' )
{
if( !wxfound )
{ Error(37, 3, "WX missing in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), *lnum);
}
if( !bfound )
{ Error(37, 4, "B missing in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), *lnum);
}
if( lig[ch] == 1 ) lig[ch] = 0; /* set to known if unknown */
else if( lig[ch] > 1 ) /* add '\0' to end of ligs */
lig[(*ligtop)++] = '\0';
if( BackEnd->uses_font_metrics )
{
fnt[ch].left = llx;
fnt[ch].down = lly - xheight2;
fnt[ch].right = wx;
fnt[ch].up = ury - xheight2;
fnt[ch].last_adjust = (urx==0 || wx==0 || fixed_pitch) ? 0 : urx - wx;
}
else
{
fnt[ch].left = 0;
fnt[ch].down = - PlainCharHeight / 2;
fnt[ch].right = PlainCharWidth;
fnt[ch].up = PlainCharHeight / 2;
fnt[ch].last_adjust = 0;
}
debug6(DFT, DDD, " fnt[%c] = (%d,%d,%d,%d,%d)",ch, fnt[ch].left,
fnt[ch].down, fnt[ch].right, fnt[ch].up, fnt[ch].last_adjust);
}
}
} /* end ReadCharMetrics */
/*****************************************************************************/
/* */
/* ReadCompositeMetrics(face, Extrafilename, extra_fnum, lnum, composite, */
/* cmp, cmptop, fp) */
/* */
/* Read a sequence of composite metrics lines. The font record is face. */
/* The line number is lnum; input is to be read from file fp. */
/* */
/*****************************************************************************/
static void ReadCompositeMetrics(OBJECT face, OBJECT Extrafilename,
FILE_NUM extra_fnum, int *lnum, unsigned short composite[],
COMPOSITE cmp[], int *cmptop, FILE *fp)
{ int status;
FULL_CHAR buff[MAX_BUFF], composite_name[100], name[100];
int composite_num, x_offset, y_offset, i, count;
FULL_CHAR composite_code, code;
/* build composites */
while( (status = ReadOneLine(fp, buff, MAX_BUFF)) != 0
&& StringBeginsWith(buff, AsciiToFull("CC")) )
{
(*lnum)++;
debug1(DFT, DD, " composite: %s", buff);
/* read CC <charname> <number_of_pieces> ; and move i to after it */
if( sscanf((char *)buff, "CC %s %d ", composite_name, &composite_num) != 2 )
Error(37, 5, "syntax error in extra font file %s (line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
for( i = 0; buff[i] != ';' && buff[i] != '\0'; i++ );
if( buff[i] != ';' )
Error(37, 5, "syntax error in extra font file %s (line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
i++;
/* add entry for this character to composite */
composite_code = MapCharEncoding(composite_name,font_mapping(face));
if( composite_code == (FULL_CHAR) '\0' )
Error(37, 6, "unknown character name %s in font file %s (line %d)",
FATAL, &fpos(Extrafilename), composite_name, FileName(extra_fnum),
*lnum);
composite[composite_code] = *cmptop;
for( count = 0; count < composite_num; count++ )
{
/* read one PCC <charname> <xoffset> <yoffset> ; and move i to after it */
if( sscanf((char *)&buff[i]," PCC %s %d %d",name,&x_offset,&y_offset)!=3 )
Error(37, 5, "syntax error in extra font file %s (line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
for( ; buff[i] != ';' && buff[i] != '\0'; i++ );
if( buff[i] != ';' )
Error(37, 5, "syntax error in extra font file %s (line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
i++;
/* load this piece into cmp */
if( *cmptop >= MAX_CHARS )
Error(37, 7, "too many composites in file %s (at line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
code = MapCharEncoding(name, font_mapping(face));
cmp[*cmptop].char_code = code;
cmp[*cmptop].x_offset = x_offset;
cmp[*cmptop].y_offset = y_offset;
(*cmptop)++;
}
/* add null terminating component */
if( *cmptop >= MAX_CHARS )
Error(37, 8, "too many composites in file %s (at line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
cmp[*cmptop].char_code = (FULL_CHAR) '\0';
(*cmptop)++;
}
if( status == 0 ||
!StringBeginsWith(buff, AsciiToFull("EndBuildComposites")) )
Error(37, 9, "missing EndBuildComposites in extra font file %s (line %d)",
FATAL, &fpos(Extrafilename), FileName(extra_fnum), *lnum);
} /* end ReadCompositeMetrics */
/*@::FontRead()@**************************************************************/
/* */
/* static OBJECT FontRead(FULL_CHAR *family_name, *face_name, OBJECT err) */
/* */
/* Search the font databases for a font with this family and face name. */
/* If found, read the font and update this module's data structures, then */
/* return the face object. */
/* */
/* If an error occurs, use fpos(err) for reporting its location if nothing */
/* better suggests itself. */
/* */
/*****************************************************************************/
static OBJECT FontRead(FULL_CHAR *family_name, FULL_CHAR *face_name, OBJECT err)
{
OBJECT cs, link, db, fontdef_obj, y, ylink;
FULL_CHAR tag[100], seq[100];
FILE_NUM dfnum; long dfpos, cont; int dlnum;
BOOLEAN font_name_found, font_bbox_found;
OBJECT family, face, font_name, AFMfilename, Extrafilename, LCMfilename;
OBJECT recode, first_size;
FULL_CHAR buff[MAX_BUFF], command[MAX_BUFF], ch;
int status;
int xheight2, i, lnum, ligtop, cmptop;
float fl_xheight2, fl_under_pos, fl_under_thick;
int under_pos, under_thick;
BOOLEAN upfound, utfound, xhfound;
BOOLEAN fixed_pitch = FALSE;
FILE_NUM fnum, extra_fnum; FILE *fp, *extra_fp;
struct metrics *fnt;
FULL_CHAR *lig; unsigned short *composite; COMPOSITE *cmp;
unsigned short *kt; FULL_CHAR *kc; unsigned char *kv; FULL_LENGTH *ks;
FULL_LENGTH bbox_llx, bbox_lly, bbox_urx, bbox_ury;
debug2(DFT, D, "FontRead(%s, %s)", family_name, face_name);
/***************************************************************************/
/* */
/* Get the @FontDef object with tag family_name-face_name from databases */
/* */
/***************************************************************************/
/* if no databases available, fatal error */
cs = cross_sym(FontDefSym);
if( cs == nilobj )
{ Error(37, 10, "unable to set font %s %s (no font databases loaded)",
FATAL, no_fpos, family_name, face_name);
}
/* search the databases for @FontDef @Tag { family-face } */
sprintf( (char *) tag, "%s-%s", family_name, face_name);
for( link = NextUp(Up(cs)); link != cs; link = NextUp(link) )
{ Parent(db, link)
;
if( DbRetrieve(db, FALSE, FontDefSym,tag,seq,&dfnum,&dfpos,&dlnum,&cont) )
break;
}
/* if not found, return nilobj */
if( link == cs )
{ debug0(DFT, D, "FontRead returning nilobj (not in any database)");
return nilobj;
}
/* found it; read @FontDef object from database file */
SwitchScope(nilobj);
fontdef_obj = ReadFromFile(dfnum, dfpos, dlnum);
UnSwitchScope(nilobj);
if( fontdef_obj == nilobj )
Error(37, 11, "cannot read %s for %s", INTERN, no_fpos, KW_FONTDEF, tag);
/***************************************************************************/
/* */
/* Extract the attributes of fontdef_obj, and check that they are OK. */
/* */
/***************************************************************************/
/* extract the various attributes */
family = face = font_name = AFMfilename = nilobj;
Extrafilename = LCMfilename = recode = nilobj;
for( ylink=Down(fontdef_obj); ylink != fontdef_obj; ylink=NextDown(ylink) )
{ Child(y, ylink)
;
if( type(y) == PAR )
{
assert( type(y) == PAR, "FontRead: type(y) != PAR!" );
if( actual(y) == fd_tag )
{
/* do nothing with this one */
}
else if( actual(y) == fd_family )
{ Child(family, Down(y))
;
if( !is_word(type(family)) || !StringEqual(string(family), family_name) )
Error(37, 12, "font family name %s incompatible with %s value %s",
FATAL, &fpos(fontdef_obj), string(family), KW_TAG, tag);
}
else if( actual(y) == fd_face )
{ Child(face, Down(y))
;
if( !is_word(type(face)) || !StringEqual(string(face), face_name) )
Error(37, 13, "font face name %s incompatible with %s value %s",
FATAL, &fpos(fontdef_obj), string(face), KW_TAG, tag);
}
else if( actual(y) == fd_name )
{ Child(font_name, Down(y))
;
font_name = ReplaceWithTidy(font_name, WORD_TIDY);
if( !is_word(type(font_name)) )
Error(37, 14, "illegal font name (quotes needed?)",
FATAL, &fpos(font_name));
}
else if( actual(y) == fd_metrics )
{ Child(AFMfilename, Down(y))
;
AFMfilename = ReplaceWithTidy(AFMfilename, WORD_TIDY);
if( !is_word(type(AFMfilename)) )
Error(37, 15, "illegal font metrics file name (quotes needed?)",
FATAL, &fpos(AFMfilename));
}
else if( actual(y) == fd_extra_metrics )
{ Child(Extrafilename, Down(y))
;
Extrafilename = ReplaceWithTidy(Extrafilename, WORD_TIDY);
if( !is_word(type(Extrafilename)) )
Error(37, 16, "illegal font extra metrics file name (quotes needed?)",
FATAL, &fpos(Extrafilename));
}
else if( actual(y) == fd_mapping )
{ Child(LCMfilename, Down(y))
;
LCMfilename = ReplaceWithTidy(LCMfilename, WORD_TIDY);
if( !is_word(type(LCMfilename)) )
Error(37, 17, "illegal mapping file name (quotes needed?)",
FATAL, &fpos(LCMfilename));
}
else if( actual(y) == fd_recode )
{ Child(recode, Down(y))
;
recode = ReplaceWithTidy(recode, WORD_TIDY);
if( !is_word(type(recode)) )
Error(37, 18, "illegal value of %s", FATAL, &fpos(recode),
SymName(fd_recode));
}
else
{ assert(FALSE, "FontRead: cannot identify component of FontDef")
}
}
}
/* check that all the compulsory ones were found */
/* a warning message will have already been given if not */
if( family == nilobj || face == nilobj || font_name == nilobj ||
AFMfilename == nilobj || LCMfilename == nilobj )
{
debug0(DFT, D, "FontRead returning nilobj (missing compulsory)");
return nilobj;
}
/***************************************************************************/
/* */
/* Update font tree to have this family, face and first_size. */
/* */
/***************************************************************************/
/* insert family into font tree if not already present */
for( link = Down(font_root); link != font_root; link = NextDown(link) )
{ Child(y, link)
;
if( StringEqual(string(y), string(family)) )
{ family = y;
break;
}
}
if( link == font_root )
MoveLink(Up(family), font_root, PARENT);
/* insert face into family, or error if already present */
for( link = Down(family); link != family; link = NextDown(link) )
{ Child(y, link)
;
if( StringEqual(string(y), string(face)) )
{ Error(37, 19, "font %s %s already defined, at%s", WARN, &fpos(face),
string(family), string(face), EchoFilePos(&fpos(y)));
debug0(DFT, D, "FontRead returning: font already defined");
DisposeObject(fontdef_obj);
return y;
}
}
MoveLink(Up(face), family, PARENT);
/* PostScript name and AFM file name are first two children of face */
Link(face, font_name);
Link(face, AFMfilename);
/* AFM file name has extra file name as optional child */
if( Extrafilename != nilobj )
Link(AFMfilename, Extrafilename);
/* load character mapping file */
if( recode != nilobj && StringEqual(string(recode), AsciiToFull("No")) )
{ font_recoded(face) = FALSE;
font_mapping(face) = MapLoad(LCMfilename, FALSE);
}
else if( recode == nilobj || StringEqual(string(recode), AsciiToFull("Yes")) )
{ font_recoded(face) = TRUE;
font_mapping(face) = MapLoad(LCMfilename, TRUE);
}
else Error(37, 20, "expecting either Yes or No here", FATAL, &fpos(recode));
/* say that this font is currently unused on any page */
font_page(face) = 0;
/* get a new number for this (default) font size */
if( ++font_count >= finfo_size )
{ if( font_count >= MAX_FONT )
Error(37, 21, "too many different fonts and sizes (maximum is %d)",
FATAL, &fpos(err),MAX_FONT);
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, -1, -finfo_size * sizeof(FONT_INFO)));
finfo_size *= 2;
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, 1, finfo_size * sizeof(FONT_INFO)));
finfo = (FONT_INFO *) realloc(finfo, finfo_size * sizeof(FONT_INFO));
if( finfo == (FONT_INFO *) NULL )
Error(37, 22, "run out of memory when increasing font table size",
FATAL, &fpos(err));
}
/* build the first size record, and initialize it with what we know now */
first_size = MakeWordTwo(WORD, AsciiToFull("fnt"), StringInt(++font_seqnum),
no_fpos);
Link(face, first_size);
font_num(first_size) = font_count;
font_size(first_size) = BackEnd->uses_font_metrics ? SZ_DFT : PlainCharHeight;
font_recoded(first_size) = font_recoded(face);
font_mapping(first_size) = font_mapping(face);
font_num(face) = font_num(first_size); /* Uwe's suggestion, helps PDF */
/* font_xheight2, font_bbox_lly, font_bbox_ury, font_spacewidth still to do */
/***************************************************************************/
/* */
/* Read the Adobe font metrics file, and record what's in it. */
/* */
/***************************************************************************/
/* open the Adobe font metrics (AFM) file of the font */
debug0(DFS, D, " calling DefineFile from FontRead");
fnum = DefineFile(string(AFMfilename), STR_EMPTY, &fpos(AFMfilename),
FONT_FILE, FONT_PATH);
fp = OpenFile(fnum, FALSE, FALSE);
if( fp == NULL )
Error(37, 23, "cannot open font file %s", FATAL, &fpos(AFMfilename),
FileName(fnum));
/* check that the AFM file begins, as it should, with "StartFontMetrics" */
if( ReadOneLine(fp, buff, MAX_BUFF) == 0 ||
sscanf( (char *) buff, "%s", command) != 1 ||
!StringEqual(command, "StartFontMetrics") )
{ debug1(DFT, DD, "first line of AFM file:%s", buff);
debug1(DFT, DD, "command:%s", command);
Error(37, 24, "font file %s does not begin with StartFontMetrics",
FATAL, &fpos(AFMfilename), FileName(fnum));
}
/* initialise font metrics table for the new font */
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, 1, MAX_CHARS * sizeof(struct metrics)));
fnt = (struct metrics *) malloc(MAX_CHARS * sizeof(struct metrics));
if( fnt == (struct metrics *) NULL )
Error(37, 25, "run out of memory while reading font file %s",
FATAL, &fpos(err), FileName(fnum));
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, 0, 2*MAX_CHARS*sizeof(FULL_CHAR)));
/* initialise ligature table for the new font */
lig = (FULL_CHAR *) malloc(2*MAX_CHARS*sizeof(FULL_CHAR));
if( lig == (FULL_CHAR *) NULL )
Error(37, 25, "run out of memory while reading font file %s",
FATAL, &fpos(err), FileName(fnum));
for( i = 0; i < MAX_CHARS; i++ ) lig[i] = 1; /* i.e. char unknown */
ligtop = MAX_CHARS+2; /* must avoid ligtop - MAX_CHARS == 0 or 1 */
/* initialise composites table for the new font */
composite = (unsigned short *) malloc(MAX_CHARS * sizeof(unsigned short));
if( composite == (unsigned short *) NULL )
Error(37, 25, "run out of memory while reading font file %s",
FATAL, &fpos(err), FileName(fnum));
cmp = (COMPOSITE *) malloc(MAX_CHARS * sizeof(COMPOSITE));
if( cmp == (COMPOSITE *) NULL )
Error(37, 25, "run out of memory while reading font file %s",
FATAL, &fpos(err), FileName(fnum));
for( i = 0; i < MAX_CHARS; i++ ) composite[i] = 0; /* i.e. not composite */
cmptop = 1; /* must avoid cmptop == 0 */
/* initialise kerning table for the new font */
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, 0, MAX_CHARS * sizeof(unsigned short)));
kt = (unsigned short *) malloc(MAX_CHARS * sizeof(unsigned short));
if( kt == (unsigned short *) NULL )
Error(37, 25, "run out of memory while reading font file %s",
FATAL, &fpos(err), FileName(fnum));
for( i = 0; i < MAX_CHARS; i++ ) kt[i] = 0; /* i.e. no kerns */
ks = (FULL_LENGTH *) NULL; /* i.e. no kern sizes */
/* read font metrics file fp */
xhfound = upfound = utfound = FALSE;
xheight2 = under_thick = under_pos = 0;
kc = (FULL_CHAR *) NULL;
kv = (unsigned char *) NULL;
ks = (FULL_LENGTH *) NULL;
font_name_found = font_bbox_found = FALSE; lnum = 1;
bbox_llx = bbox_lly = bbox_urx = bbox_ury = 0;
while( (status = ReadOneLine(fp, buff, MAX_BUFF)) != 0 &&
!(buff[0] == 'E' && StringEqual(buff, AsciiToFull("EndFontMetrics"))) )
{
lnum++;
if( sscanf( (char *) buff, "%s", command) != EOF ) switch( command[0] )
{
case 'U':
if( StringEqual(command, AsciiToFull("UnderlinePosition")) )
{ if( upfound )
{ Error(37, 26, "UnderlinePosition found twice in font file (line %d)",
FATAL, &fpos(AFMfilename), lnum);
}
sscanf( (char *) buff, "UnderlinePosition %f", &fl_under_pos);
under_pos = fl_under_pos;
upfound = TRUE;
}
else if( StringEqual(command, AsciiToFull("UnderlineThickness")) )
{ if( utfound )
{ Error(37, 27, "UnderlineThickness found twice in font file (line %d)",
FATAL, &fpos(AFMfilename), lnum);
}
sscanf( (char *) buff, "UnderlineThickness %f", &fl_under_thick);
under_thick = fl_under_thick;
utfound = TRUE;
}
break;
case 'X':
if( StringEqual(command, AsciiToFull("XHeight")) )
{ if( xhfound )
{ Error(37, 28, "XHeight found twice in font file (line %d)",
FATAL, &fpos(AFMfilename), lnum);
}
sscanf( (char *) buff, "XHeight %f", &fl_xheight2);
xheight2 = fl_xheight2 / 2;
xhfound = TRUE;
}
break;
case 'F':
if( StringEqual(command, AsciiToFull("FontName")) )
{ if( font_name_found )
{ Error(37, 29, "FontName found twice in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum);
}
sscanf( (char *) buff, "FontName %s", command);
if( StringEqual(command, STR_EMPTY) )
{ Error(37, 30, "FontName empty in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum);
}
Child(y, Down(face))
;
if( !StringEqual(command, string(y)) )
Error(37, 31, "FontName in font file (%s) and %s (%s) disagree",
WARN, &fpos(AFMfilename), command, KW_FONTDEF, string(y));
font_name_found = TRUE;
}
else if( StringEqual(command, AsciiToFull("FontBBox")) )
{
if( font_bbox_found )
{ Error(37, 69, "FontBBox found twice in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum);
}
if( sscanf( (char *) buff, "FontBBox %d %d %d %d",
&bbox_llx, &bbox_lly, &bbox_urx, &bbox_ury) != 4 )
{ Error(37, 70, "FontBBox format error in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum);
}
font_bbox_found = TRUE;
}
break;
case 'I':
if( StringEqual(command, AsciiToFull("IsFixedPitch")) )
{
sscanf( (char *) buff, "IsFixedPitch %s", command);
if( StringEqual(command, AsciiToFull("true")) )
{ fixed_pitch = TRUE;
}
}
break;
case 'S':
if( StringEqual(command, AsciiToFull("StartCharMetrics")) )
{
if( !font_name_found )
Error(37, 32, "FontName missing in file %s",
FATAL, &fpos(AFMfilename), FileName(fnum));
if( !xhfound ) xheight2 = DEFAULT_XHEIGHT / 2;
ReadCharMetrics(face, fixed_pitch, xheight2, lig, &ligtop,
fnum, fnt, &lnum, fp);
}
else if( BackEnd->uses_font_metrics && Kern &&
StringEqual(command, AsciiToFull("StartKernPairs")) )
{ FULL_CHAR ch1, ch2, last_ch1;
FULL_CHAR name1[30], name2[30];
int kc_top, ks_top, pos, num_pairs, ksize; float fl_ksize;
if( sscanf( (char *) buff, "StartKernPairs %d", &num_pairs) != 1 )
Error(37, 33, "syntax error on StartKernPairs line in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum);
kc_top = 1; ks_top = 1;
ifdebug(DMA, D,
DebugRegisterUsage(MEM_FONTS, 0, 2*num_pairs * sizeof(FULL_CHAR)));
kc = (FULL_CHAR *) malloc(2 * num_pairs * sizeof(FULL_CHAR));
ifdebug(DMA, D, DebugRegisterUsage(MEM_FONTS, 0,
2 * num_pairs * sizeof(unsigned char)));
kv = (unsigned char *) malloc(2 * num_pairs * sizeof(unsigned char));
ifdebug(DMA, D, DebugRegisterUsage(MEM_FONTS, 0,
num_pairs * sizeof(FULL_LENGTH)));
ks = (FULL_LENGTH *) malloc(num_pairs * sizeof(FULL_LENGTH));
last_ch1 = '\0';
while( ReadOneLine(fp, buff, MAX_BUFF) != 0 &&
!StringBeginsWith(buff, AsciiToFull("EndKernPairs")) )
{
debug1(DFT, DD, "FontRead reading %s", buff);
lnum++;
if( StringBeginsWith(buff, AsciiToFull("KPX")) )
{
/* get the two character names and kern size from buff */
if( sscanf((char *)buff, "KPX %s %s %f",name1,name2,&fl_ksize)!=3 )
Error(37, 34, "syntax error in font file %s (line %d): %s",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum, buff);
/* ignore size 0 kern pairs (they are frequent, why?) */
ksize = fl_ksize;
if( ksize == 0 ) continue;
/* check that both characters are encoded */
ch1 = MapCharEncoding(name1, font_mapping(face));
if( ch1 == '\0' )
{
continue;
}
ch2 = MapCharEncoding(name2, font_mapping(face));
if( ch2 == '\0' )
{
continue;
}
/* check that ch1 is contiguous with previous occurrences */
if( ch1 != last_ch1 && kt[ch1] != 0 )
{ Error(37, 35, "ignoring out-of-order kerning pair %s %s in font file %s (line %d)",
WARN, &fpos(AFMfilename), name1, name2, FileName(fnum), lnum);
continue;
}
last_ch1 = ch1;
/* if ch1 never seen before, make new entry in kt[] and kc[] */
if( kt[ch1] == 0 )
{ debug2(DFT, DD, " kt[%d] = %d", ch1, kc_top);
kt[ch1] = kc_top;
kc[kc_top] = (FULL_CHAR) '\0';
kv[kc_top] = 0;
kc_top++;
}
/* find kerning size in ks[] or else add it to the end */
for( pos = 1; pos < ks_top; pos++ )
{ if( ks[pos] == ksize ) break;
}
if( pos == ks_top )
{ if( ks_top == num_pairs )
Error(37, 36, "too many kerning pairs in font file %s (line %d)",
FATAL, &fpos(AFMfilename), FileName(fnum), lnum);
debug2(DFT, DD, " ks[%d] = %d", pos, ksize);
ks[pos] = ksize;
ks_top++;
}
/* insert ch2 into the kc entries (sorted decreasing) for ch1 */
for( i = kc_top-1; i >= kt[ch1] && kc[i] < ch2; i-- )
{ kc[i+1] = kc[i];
kv[i+1] = kv[i];
}
if( i >= kt[ch1] && kc[i] == ch2 )
Error(37, 37, "kerning pair %s %s appears twice in font file %s (line %d)",
FATAL, &fpos(AFMfilename), name1, name2, FileName(fnum), lnum);
kc[i+1] = ch2;
kv[i+1] = pos;
kc_top++;
}
}
ks[0] = ks_top;
}
break;
default:
break;
}
}
/* make sure we terminated the font metrics file gracefully */
if( status == 0 )
Error(37, 38, "EndFontMetrics missing from font file %s",
FATAL, &fpos(AFMfilename), FileName(fnum));
fclose(fp);
fp = (FILE *) NULL;