/
getfontname.c
6908 lines (6585 loc) · 248 KB
/
getfontname.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
/*
* This is showttf.c from contrib/fonttools/showttf.c in the FontForge
* github repo, hacked to only show the PostScript name of the font.
*/
#if 0
#include "basics.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef unsigned int uint32;
typedef int int32;
typedef short int16;
typedef signed char int8;
typedef unsigned short uint16;
typedef unsigned char uint8;
#define true 1
#define false 0
#endif
#include <string.h>
static int verbose = false;
static int max_lig_nest = 10000;
static int just_headers = false;
static int head_check = false;
struct dup {
int glyph;
int enc;
struct dup *prev;
};
struct features {
int feature;
int nsettings;
struct settings { uint16 setting; int16 nameid; char *name; } *settings;
int featureflags;
char *name;
int nameIndex;
};
struct ttfinfo {
int emsize; /* ascent + descent? from the head table */
int ascent, descent; /* from the hhea table */
/* not the usWinAscent from the OS/2 table */
int width_cnt; /* from the hhea table, in the hmtx table */
int glyph_cnt; /* from maxp table */
unsigned int index_to_loc_is_long:1; /* in head table */
/* Mac fonts platform=0/1, platform specific enc id, roman=0, english is lang code 0 */
/* iso platform=2, platform specific enc id, latin1=0/2, no language */
/* microsoft platform=3, platform specific enc id, 1, english is lang code 0x??09 */
char *copyright; /* from the name table, nameid=0 */
char *familyname; /* nameid=1 */
char *fullname; /* nameid=4 */
char *version; /* nameid=5 */
char *fontname; /* postscript font name, nameid=6 */
double italicAngle; /* from post table */
int upos, uwidth; /* underline pos, width from post table */
int ttccnt; /* if a ttc file, number of fonts */
int *ttcoffsets; /* Offsets to font headers */
int numtables;
/* BASE */
int base_start;
int base_length;
/* CFF */
int cff_start;
int cff_length;
/* cmap */
int encoding_start; /* Offset from sof to start of encoding table */
/* cvt */
int cvt_start;
int cvt_length;
/* glyf */
int glyph_start; /* Offset from sof to start of glyph table */
/* bdat */
/* EBDT */
int bitmapdata_start; /* Offset to start of bitmap data */
/* bloc */
/* EBLT */
int bitmaploc_start; /* Offset to start of bitmap locator data */
int bitmaploc_length;
/* EBSC */
int bitmapscale_start; /* Offset to start of bitmap scaling data */
/* feat */
int feat_start;
/* fdsc */
int fdsc_start;
/* gasp */
int gasp_start;
/* GDEF */
int gdef_start; /* OpenType glyph definitions */
/* GPOS */
int gpos_start; /* OpenType glyph positioning, kerning etc. */
/* GSUB */
int gsub_start; /* OpenType glyph substitution, ligatures etc. */
/* hdmx */
int hdmx_start;
/* head */
int head_start;
/* hhea */
int hhea_start;
/* hmtx */
int hmetrics_start;
/* JSFT */
int JSTF_start;
/* kern */
int kern_start;
int lcar_start;
/* loca */
int glyphlocations_start; /* there are glyph_cnt of these, from maxp tab */
int loca_length; /* actually glypn_cnt is wrong. Use the table length (divided by size) instead */
/* MATH */
int math_start;
/* maxp */
int maxp_start; /* maximum number of glyphs */
int maxp_length;
/* mort */
int mort_start; /* Apple metamorph tab, ligs, etc. */
int mort_length;
/* morx */
int morx_start; /* Apple extended metamorph tab, ligs, etc. */
int morx_length;
/* name */
int copyright_start; /* copyright and fontname */
int opbd_start;
/* post */
int postscript_start; /* names for the glyphs, italic angle, etc. */
int postscript_len; /* names for the glyphs, italic angle, etc. */
/* OS/2 */
int os2_start;
int os2_len;
/* fvar */
int fvar_start;
int fvar_length;
/* gvar */
int gvar_start;
int gvar_length;
int fpgm_start;
int fpgm_length;
int prep_start;
int prep_length;
int prop_start;
/* head */
int fftm_start;
unsigned int *glyph_unicode;
char **glyph_names;
struct dup *dups;
struct features *features;
uint16 *morx_classes;
int fvar_axiscount;
};
static int getushort(FILE *ttf) {
int ch1 = getc(ttf);
int ch2 = getc(ttf);
if ( ch2==EOF )
return( EOF );
return( (ch1<<8)|ch2 );
}
static int32 getlong(FILE *ttf) {
int ch1 = getc(ttf);
int ch2 = getc(ttf);
int ch3 = getc(ttf);
int ch4 = getc(ttf);
if ( ch4==EOF )
return( EOF );
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
}
static int32 get3byte(FILE *ttf) {
int ch1 = getc(ttf);
int ch2 = getc(ttf);
int ch3 = getc(ttf);
if ( ch3==EOF )
return( EOF );
return( (ch1<<16)|(ch2<<8)|ch3 );
}
static int32 getoffset(FILE *ttf, int offsize) {
int ch1, ch2, ch3, ch4;
ch1 = getc(ttf);
if ( offsize==1 )
return( ch1 );
ch2 = getc(ttf);
if ( offsize==2 ) {
if ( ch2==EOF )
return( EOF );
return( (ch1<<8)|ch2);
}
ch3 = getc(ttf);
if ( offsize==3 ) {
if ( ch3==EOF )
return( EOF );
return( (ch1<<16)|(ch2<<8)|ch3 );
}
ch4 = getc(ttf);
if ( ch4==EOF )
return( EOF );
return( (ch1<<24)|(ch2<<16)|(ch3<<8)|ch4 );
}
static double getfixed(FILE *ttf) {
int32 val = getlong(ttf);
int mant = val&0xffff;
/* This oddity may be needed to deal with the first 16 bits being signed */
/* and the low-order bits unsigned */
return( (double) (val>>16) + (mant/65536.0) );
}
static double long2fixed(int32 val) {
int mant = val&0xffff;
/* This oddity may be needed to deal with the first 16 bits being signed */
/* and the low-order bits unsigned */
return( (double) (val>>16) + (mant/65536.0) );
}
static int32 filecheck(FILE *file, int start, int len) {
uint32 sum = 0, chunk;
fseek(file,start,SEEK_SET);
if ( len!=-1 ) len=(len+3)>>2;
while ( len==-1 || --len>=0 ) {
chunk = getlong(file);
if ( feof(file))
break;
sum += chunk;
}
return( sum );
}
#define CHR(ch1,ch2,ch3,ch4) (((ch1)<<24)|((ch2)<<16)|((ch3)<<8)|(ch4))
static int readcff(FILE *ttf,FILE *util, struct ttfinfo *info);
static int readttfheader(FILE *ttf, FILE *util, struct ttfinfo *info) {
int i;
int tag, checksum, offset, length, sr, es, rs, cs; uint32 v;
int e_sr, e_es, e_rs;
double version;
v = getlong(ttf);
if ( v==CHR('t','t','c','f')) {
v = getlong(ttf);
info->ttccnt = getlong(ttf);
printf( "This is a TrueType Font Collection file (version=%x), with %d fonts\n", v, info->ttccnt );
info->ttcoffsets = calloc(info->ttccnt,sizeof(int));
for ( i = 0; i<info->ttccnt; ++i ) {
info->ttcoffsets[i] = getlong(ttf);
printf( " Offset to font %d header: %d\n", i, info->ttcoffsets[i]);
}
return(true);
}
version = long2fixed(v);
info->numtables = getushort(ttf);
sr = getushort(ttf),
es = getushort(ttf),
rs = getushort(ttf);
if ( v==CHR('O','T','T','O'))
printf( "version='OTTO', " );
else if ( v==CHR('t','r','u','e'))
printf( "version='true', " );
else if ( v==CHR('S','p','l','i')) {
fprintf(stderr, "This looks like one of fontforge's spline font databases, and not a truetype font.\n" );
exit ( 1 );
} else if ( v==CHR('%','!','P','S')) {
fprintf(stderr, "This looks like a postscript (pfa) file, and not a truetype font.\n" );
exit ( 1 );
} else if ( v>=0x80000000 && info->numtables==0 ) {
fprintf(stderr, "This looks like a postscript (pfb) file, and not a truetype font.\n" );
exit ( 1 );
} else if ( (v>>24)==1 && ((v>>16)&0xff)==0 && ((v>>8)&0xff)==4 ) {
fprintf( stderr, "This looks like a bare CFF file. Proceeding under that assumption.\n");
info->cff_start = 0;
fseek(ttf,0,SEEK_END);
info->cff_length = ftell(ttf);
rewind(ttf);
readcff(ttf,util,info);
exit(0);
} else if ( v==CHR('t','y','p','1')) {
printf( "version='typ1', " );
/* This is apple's embedding of a type1 font in a truetype file. I don't know how to parse it. I'd like a copy to look at if you don't mind sending me one... gww@silcom.com */
} else
printf( "version=%g, ", version );
printf( "numtables=%d, searchRange=%d entrySel=%d rangeshift=%d\n",
info->numtables, sr, es, rs);
e_sr = (info->numtables<8?4:info->numtables<16?8:info->numtables<32?16:info->numtables<64?32:64)*16;
e_es = (info->numtables<8?2:info->numtables<16?3:info->numtables<32?4:info->numtables<64?5:6);
e_rs = info->numtables*16-e_sr;
if ( e_sr!=sr || e_es!=es || e_rs!=rs )
printf( "!!!! Unexpected values for binsearch header. Based on the number of tables I\n!!!!! expect searchRange=%d (not %d), entrySel=%d (not %d) rangeShift=%d (not %d)\n",
e_sr, sr, e_es, es, e_rs, rs );
/* The one example I have of a ttc file has the file checksum set to: 0xdcd07d3e */
/* I don't know if that's magic or not (docs don't say), but it vaguely follows */
/* the same pattern as 0xb1b0afba so it might be */
/* All the fonts used the same head table so one difficulty was eased */
printf( "File Checksum =%x (should be 0xb1b0afba), diff=%x\n",
(unsigned int)(filecheck(util,0,-1)), (unsigned int)((0xb1b0afba-filecheck(util,0,-1))) );
for ( i=0; i<info->numtables; ++i ) {
tag = getlong(ttf);
checksum = getlong(ttf);
offset = getlong(ttf);
length = getlong(ttf);
cs = filecheck(util,offset,length);
printf( "%c%c%c%c checksum=%08x actual=%08x diff=%x offset=%d len=%d\n",
(char)((tag>>24)&0xff), (char)((tag>>16)&0xff),
(char)((tag>>8)&0xff), (char)(tag&0xff),
(unsigned int)(checksum), (unsigned int)(cs), (unsigned int)((checksum^cs)),
offset, length );
switch ( tag ) {
case CHR('B','A','S','E'):
info->base_start = offset;
info->base_length = length;
break;
case CHR('C','F','F',' '):
info->cff_start = offset;
info->cff_length = length;
break;
case CHR('c','m','a','p'):
info->encoding_start = offset;
break;
case CHR('c','v','t',' '):
info->cvt_start = offset;
info->cvt_length = length;
break;
case CHR('F','F','T','M'):
info->fftm_start = offset;
break;
case CHR('f','p','g','m'):
info->fpgm_start = offset;
info->fpgm_length = length;
break;
case CHR('f','v','a','r'):
info->fvar_start = offset;
info->fvar_length = length;
break;
case CHR('f','e','a','t'):
info->feat_start = offset;
break;
case CHR('f','d','s','c'):
info->fdsc_start = offset;
break;
case CHR('g','a','s','p'):
info->gasp_start = offset;
break;
case CHR('g','l','y','f'):
info->glyph_start = offset;
break;
case CHR('G','D','E','F'):
info->gdef_start = offset;
break;
case CHR('G','P','O','S'):
info->gpos_start = offset;
break;
case CHR('G','S','U','B'):
info->gsub_start = offset;
break;
case CHR('g','v','a','r'):
info->gvar_start = offset;
info->gvar_length = length;
break;
case CHR('m','o','r','t'):
info->mort_start = offset;
info->mort_length = length;
break;
case CHR('m','o','r','x'):
info->morx_start = offset;
info->morx_length = length;
break;
case CHR('b','d','a','t'):
case CHR('E','B','D','T'):
info->bitmapdata_start = offset;
break;
case CHR('b','l','o','c'):
case CHR('E','B','L','C'):
info->bitmaploc_start = offset;
info->bitmaploc_length = length;
break;
case CHR('E','B','S','C'): /* Apple uses the same as MS on this one */
info->bitmapscale_start = offset;
break;
case CHR('h','d','m','x'):
info->hdmx_start = offset;
break;
case CHR('b','h','e','d'): /* Fonts with bitmaps but no outlines get bhea */
case CHR('h','e','a','d'):
info->head_start = offset;
if ( length!=54 ) fprintf( stderr, "> ! Unexpected length for head table, was %d expected 54 ! <\n", length );
break;
case CHR('h','h','e','a'):
info->hhea_start = offset;
if ( length!=36 ) fprintf( stderr, "> ! Unexpected length for hhea table, was %d expected 36 ! <\n", length );
break;
case CHR('h','m','t','x'):
info->hmetrics_start = offset;
break;
case CHR('k','e','r','n'):
info->kern_start = offset;
break;
case CHR('J','S','T','F'):
info->JSTF_start = offset;
break;
case CHR('l','o','c','a'):
info->glyphlocations_start = offset;
info->loca_length = length;
info->glyph_cnt = length/2-1;
break;
case CHR('M','A','T','H'):
info->math_start = offset;
break;
case CHR('m','a','x','p'):
info->maxp_start = offset;
if ( length!=32 && length!=6 ) fprintf( stderr, "> ! Unexpected length for maxp table, was %d expected 32 ! <\n", length );
info->maxp_length = length;
break;
case CHR('n','a','m','e'):
info->copyright_start = offset;
break;
case CHR('o','p','b','d'):
info->opbd_start = offset;
break;
case CHR('p','o','s','t'):
info->postscript_start = offset;
info->postscript_len = length;
break;
case CHR('O','S','/','2'):
info->os2_start = offset;
info->os2_len = length;
if ( length!=78 && length!=86 && length!=96 )
fprintf( stderr, "> ! Unexpected length for OS/2 table, was %d expected either 78 or 86 ! <\n", length );
break;
case CHR('p','r','e','p'):
info->prep_start = offset;
info->prep_length = length;
break;
case CHR('p','r','o','p'):
info->prop_start = offset;
break;
}
}
if ( (info->encoding_start!=0 && info->glyph_start==0 && info->head_start!=0 &&
info->hhea_start!=0 && info->hmetrics_start!=0 && info->glyphlocations_start == 0 &&
info->maxp_start!=0 && info->copyright_start!=0 && info->postscript_start!=0 &&
info->os2_start!=0 ) && info->cff_start!=0 ) {
fprintf( stderr, "Required tables: glyf and loca have been replaced by CFF => OpenType\n" );
} else if ( info->encoding_start==0 || info->glyph_start==0 || info->head_start==0 ||
info->hhea_start==0 || info->hmetrics_start==0 || info->glyphlocations_start == 0 ||
info->maxp_start==0 || info->copyright_start==0 || info->postscript_start==0 ||
info->os2_start==0 ) {
fprintf( stderr, "Required table(s) missing: " );
if ( info->encoding_start==0 ) fprintf(stderr,"cmap ");
if ( info->glyph_start==0 ) fprintf(stderr,"glyf ");
if ( info->head_start==0 ) fprintf(stderr,"head ");
if ( info->hhea_start==0 ) fprintf(stderr,"hhea ");
if ( info->hmetrics_start==0 ) fprintf(stderr,"hmtx ");
if ( info->glyphlocations_start==0 ) fprintf(stderr,"loca ");
if ( info->maxp_start==0 ) fprintf(stderr,"maxp ");
if ( info->copyright_start==0 ) fprintf(stderr,"name ");
if ( info->postscript_start==0 ) fprintf(stderr,"post ");
if ( info->os2_start==0 ) fprintf(stderr,"OS/2");
fprintf(stderr,"\n");
}
return( true );
}
static time_t readdate(FILE *ttf) {
int date[4], date1970[4], year[2];
int i;
/* Dates in sfnt files are seconds since 1904. I adjust to unix time */
/* seconds since 1970 by figuring out how many seconds were in between */
date[3] = getushort(ttf);
date[2] = getushort(ttf);
date[1] = getushort(ttf);
date[0] = getushort(ttf);
memset(date1970,0,sizeof(date1970));
year[0] = (60*60*24*365L)&0xffff;
year[1] = (60*60*24*365L)>>16;
for ( i=1904; i<1970; ++i ) {
date1970[0] += year[0];
date1970[1] += year[1];
if ( (i&3)==0 && (i%100!=0 || i%400==0))
date1970[0] += 24*60*60L; /* Leap year */
date1970[1] += (date1970[0]>>16);
date1970[0] &= 0xffff;
date1970[2] += date1970[1]>>16;
date1970[1] &= 0xffff;
date1970[3] += date1970[2]>>16;
date1970[2] &= 0xffff;
}
for ( i=0; i<3; ++i ) {
date[i] -= date1970[i];
date[i+1] += date[i]>>16;
date[i] &= 0xffff;
}
date[3] -= date1970[3];
return( (date[1]<<16) | date[0] );
}
static void readttfFFTM(FILE *ttf, FILE *util, struct ttfinfo *info) {
time_t unix_date;
struct tm *tm;
static const char *months[] = { "Jan", "Feb", "March", "April", "May",
"June", "July", "August", "Sept", "Oct", "Nov", "Dec", NULL };
fseek(ttf,info->fftm_start,SEEK_SET);
printf( "\n\nCreated by FontForge " );
if ( getlong(ttf)!=0x00000001 ) {
fprintf( stderr, "Unknown version for 'FFTM' table\n" );
} else {
unix_date = readdate(ttf);
tm = localtime(&unix_date);
printf( "%d:%02d %02d-%s-%d\n",
tm->tm_hour, tm->tm_min, tm->tm_mday, months[tm->tm_mon],
tm->tm_year+1900 );
unix_date = readdate(ttf);
printf( "\tFont created: %s", ctime(&unix_date));
unix_date = readdate(ttf);
printf( "\tFont modified: %s", ctime(&unix_date));
}
printf( "\n" );
}
static void readttfhead(FILE *ttf, FILE *util, struct ttfinfo *info) {
int mn, flags, fd;
time_t unix_date;
fseek(ttf,info->head_start,SEEK_SET);
printf( "\nHEAD table (at %d)\n", info->head_start );
printf( "\tVersion=%g\n", getfixed(ttf));
printf( "\tfontRevision=%g\n", getfixed(ttf));
printf( "\tchecksumAdj=%x\n", (unsigned int)(getlong(ttf)) );
mn = getlong(ttf);
printf( "\tmagicNumber=%x (0x5f0f3cf5, diff=%x)\n",
(unsigned int)(mn), (unsigned int)(mn-0x5f0f3cf5) );
printf( "\tflags=%x ", (unsigned int)((flags=getushort(ttf))) );
if ( flags&1 ) printf( "baseline_at_0 " );
if ( flags&2 ) printf( "lsb_at_0 " );
if ( flags&4 ) printf( "instrs_depend_on_size " );
if ( flags&8 ) printf( "ppem_to_int " );
if ( flags&16 ) printf( "instr_set_width " );
printf( "\n" );
printf( "\tunitsPerEm=%d\n", getushort(ttf));
printf( "\tcreate[0]=%x\n", (unsigned int)(getlong(ttf)) );
printf( "\t create[1]=%x\n", (unsigned int)(getlong(ttf)) );
fseek(ttf,-8,SEEK_CUR);
unix_date = readdate(ttf);
printf( "\tFile created: %s", ctime(&unix_date));
printf( "\tmodtime[0]=%x\n", (unsigned int)(getlong(ttf)) );
printf( "\t modtime[1]=%x\n", (unsigned int)(getlong(ttf)) );
fseek(ttf,-8,SEEK_CUR);
unix_date = readdate(ttf);
printf( "\tFile modified: %s", ctime(&unix_date));
printf( "\txmin=%d\n", (short) getushort(ttf));
printf( "\tymin=%d\n", (short) getushort(ttf));
printf( "\txmax=%d\n", (short) getushort(ttf));
printf( "\tymax=%d\n", (short) getushort(ttf));
printf( "\tmacstyle=%x\n", (unsigned int)(getushort(ttf)) );
printf( "\tlowestppem=%d\n", getushort(ttf));
printf( "\tfontdirhint=%d ", fd = getushort(ttf));
switch ( fd ) {
case 0: printf("mixed directional glyphs\n" ); break;
case 1: printf("strongly left to right glyphs\n" ); break;
case 2: printf("left to right and neutrals\n" ); break;
case -1: printf("strongly right to left glyphs\n" ); break;
case -2: printf("right to left and neutrals\n" ); break;
default: printf("???\n" ); break;
}
printf( "\tloca_is_32=%d\n", info->index_to_loc_is_long = getushort(ttf));
if ( info->index_to_loc_is_long )
info->glyph_cnt = info->loca_length/4-1;
printf( "\tglyphdataformat=%d\n", getushort(ttf));
}
static void readttfhhead(FILE *ttf, FILE *util, struct ttfinfo *info) {
fseek(ttf,info->hhea_start,SEEK_SET);
printf( "\nHHEAD table (at %d)\n", info->hhea_start );
printf( "\tVersion=%g\n", getfixed(ttf));
printf( "\tascender=%d\n", (short) getushort(ttf));
printf( "\tdescender=%d\n", (short) getushort(ttf));
printf( "\tlinegap=%d\n", (short) getushort(ttf));
printf( "\tadvanceWidthMax=%d\n", getushort(ttf));
printf( "\tminlsb=%d\n", (short) getushort(ttf));
printf( "\tminrsb=%d\n", (short) getushort(ttf));
printf( "\tmaxextent=%d\n", (short) getushort(ttf));
printf( "\tcaretsloperise=%d\n", (short) getushort(ttf));
printf( "\tcaretsloperun=%d\n", (short) getushort(ttf));
printf( "\tmbz=%d\n", (short) getushort(ttf));
printf( "\tmbz=%d\n", (short) getushort(ttf));
printf( "\tmbz=%d\n", (short) getushort(ttf));
printf( "\tmbz=%d\n", (short) getushort(ttf));
printf( "\tmbz=%d\n", (short) getushort(ttf));
printf( "\tmetricdataformat=%d\n", (short) getushort(ttf));
printf( "\tnumberOfHMetrics=%d\n", getushort(ttf));
}
static char *getttfname(FILE *util, struct ttfinfo *info, int nameid) {
int nrec, taboff, stroff, strlen, platform;
int i,j,id;
fseek(util,info->copyright_start,SEEK_SET);
/* format */ getushort(util);
nrec=getushort(util);
taboff=getushort(util);
for ( i=0; i<nrec; ++i ) {
platform = getushort(util);
/* plat spec encoding */ getushort(util);
/* language */ getushort(util);
id=getushort(util);
strlen=getushort(util);
stroff=getushort(util);
if ( platform==1 && id==nameid ) {
char *ret = malloc(strlen+2); /* In case it's unicode */
fseek(util,info->copyright_start+taboff+stroff,SEEK_SET);
for ( j=0; j<strlen; ++j )
ret[j] = getc(util);
ret[strlen] = '\0';
ret[strlen+1] = '\0';
return( ret );
}
}
return( NULL );
}
static void readttfname(FILE *ttf, FILE *util, struct ttfinfo *info) {
int nrec, taboff, stroff, strlen;
int i,j,id;
char *psname;
fseek(ttf,info->copyright_start,SEEK_SET);
printf( "\nNAME table (at %d)\n", info->copyright_start );
printf( "\tformat=%d\n", getushort(ttf));
printf( "\tnrecords=%d\n", nrec=getushort(ttf));
printf( "\ttaboff=%d\n", taboff=getushort(ttf));
for ( i=0; i<nrec; ++i ) {
printf( "\t platform=%d", getushort(ttf));
printf( " plat spec encoding=%d", getushort(ttf));
printf( " language=%x", (unsigned int)(getushort(ttf)) );
printf( " name=%d ", id=getushort(ttf));
/* Name IDs defined in original TTF docs */
printf( id==0?"Copyright\n":id==1?"Family\n":id==2?"Subfamily\n":id==3?
"UniqueID\n":id==4?"FullName\n":id==5?"Version\n":id==6?"Postscript\n":
id==7?"Trademark\n":
/* OpenType extensions. documented: http://partners.adobe.com/asn/developer/opentype/recom.html */
id==8?"Manufacturer\n":
id==9?"Designer\n":
id==10?"Descriptor\n":
id==11?"Vendor URL\n":
id==12?"Designer URL\n":
/* Guesse based on ARIAL.TTF usage. Not documented that I've seen */
id==13?"License\n":
/* Guesses based on OpenType usage. Not documented that I've seen */
id==14?"License URL\n":
/* 15 is reserved */
id==16?"Preferred Family Name\n":
id==17?"Preferred Subfamily\n":
id==18?"Compatible full\n":
id==19?"Sample Text\n":
/* End guesses */
"???????\n");
printf( "\t strlen=%d ", strlen=getushort(ttf));
printf( " stroff=%d\t ", stroff=getushort(ttf));
psname = id == 6 ? malloc(strlen + 1) : NULL;
fseek(util,info->copyright_start+taboff+stroff,SEEK_SET);
for ( j=0; j<strlen; ++j ) {
int ch = getc(util);
if (psname != NULL) psname[j] = ch;
if ( ch<' ' ) {
putchar('^');
putchar(ch+'@');
} else if ( ch=='\177' ) {
putchar('^');
putchar('?');
} else if ( ch>=0x80 && ch<0xa0 ) {
printf("<%2X>", (unsigned int)(ch) );
} else
putchar(ch);
}
putchar('\n');
if (psname != NULL) {
psname[strlen] = '\0';
info->fontname = psname;
}
}
}
static void readttfmaxp(FILE *ttf, FILE *util, struct ttfinfo *info) {
int ng;
fseek(ttf,info->maxp_start,SEEK_SET);
printf( "\nMAXP table (at %d)\n", info->maxp_start );
printf( "\tVersion=%g\n", getfixed(ttf));
printf( "\t numGlyphs=%d\n", ng = getushort(ttf));
info->glyph_cnt = ng; /* Open type doesn't have a loca table */
if ( info->maxp_length==6 ) /* Open Type */
return;
printf( "\t maxPoints=%d\n", getushort(ttf));
printf( "\t maxContours=%d\n", getushort(ttf));
printf( "\t maxCompositPoints=%d\n", getushort(ttf));
printf( "\t maxCompositContours=%d\n", getushort(ttf));
printf( "\t maxZones=%d\n", getushort(ttf));
printf( "\t maxTwilightPoints=%d\n", getushort(ttf));
printf( "\t maxStorage=%d\n", getushort(ttf));
printf( "\t maxFunctionDefs=%d\n", getushort(ttf));
printf( "\t maxInstructionDefs=%d\n", getushort(ttf));
printf( "\t maxStackElements=%d\n", getushort(ttf));
printf( "\t maxSizeOfInstructions=%d\n", getushort(ttf));
printf( "\t maxComponentElements=%d\n", getushort(ttf));
printf( "\t maxComponentDepth=%d\n", getushort(ttf));
}
static void readttfcvt(FILE *ttf, FILE *util, struct ttfinfo *info) {
int i;
if ( info->cvt_start==0 )
return;
fseek(ttf,info->cvt_start,SEEK_SET);
printf( "\nCVT table (at %d, len=%d)\n", info->cvt_start, info->cvt_length );
for ( i=0; i<info->cvt_length/2; ++i )
printf( " CVT[%d] = %d\n", i, (short) getushort(ttf));
}
static void readttfos2(FILE *ttf, FILE *util, struct ttfinfo *info) {
int i, val;
static const char *weights[] = { "Unspecified", "Thin", "Extra-Light", "Light", "Normal", "Medium", "Semi-Bold", "Bold", "Extra-Bold", "Black", "???" };
static const char *widths[] = { "Unspecified", "Ultra-Condensed", "Extra-Condensed", "Condensed", "Semi-Condensed", "Medium", "Semi-Expanded", "Expanded", "Extra-Expanded", "Ultra-Expanded", "???" };
static const char *class[16] = { "No classification", "Old Style Serifs", "Transitional Serifs", "Modern Serifs", "Clarendon Serifs", "Slab Serifs", "???", "Freeform Serifs", "Sans Serif", "Ornamentals", "Scripts", "???", "Symbolic", "???", "???", "???" };
static const char *subclass0[16] = { "", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass1[16] = { "", "ibm rounded", "garalde", "venetian", "modified venetian", "dutch modern", "dutch traditional", "contemporary", "caligraphic", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass2[16] = { "", "direct line", "script", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass3[16] = { "", "italian", "script", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass4[16] = { "", "clarendon", "modern", "traditional", "newspaper", "stub", "monotone", "typewriter", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass5[16] = { "", "monotone", "humanist", "geometric", "swiss", "typewriter", "???", "???", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass7[16] = { "", "modern", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass8[16] = { "", "ibm neogrotesque", "humanist", "low-x rounded", "high-x rounded", "neo-grotesque", "modified neo-grotesque", "???", "???", "typewriter", "matrix", "???", "???", "???", "???", "Misc" };
static const char *subclass9[16] = { "", "engraver", "black letter", "decorative", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass10[16] = { "", "???", "uncial", "brush joined", "formal joined", "monotone joined", "calligraphic", "brush unjoined", "formal unjoined", "monotone unjoined", "???", "???", "???", "???", "???", "Misc" };
static const char *subclass12[16] = { "", "???", "???", "mixed serif", "???", "???", "old style serif", "neo-grotesque sans", "???", "???", "???", "???", "???", "???", "???", "Misc" };
static const char **subclasses[16] = { subclass0, subclass1, subclass2, subclass3,
subclass4, subclass5, subclass0, subclass7, subclass8, subclass9,
subclass10, subclass0, subclass12, subclass0, subclass0, subclass0 };
static const char *panose0[] = { "Any", "No Fit", "Text & Display", "Script", "Decorative", "Pictoral" };
static const char *panose1[] = { "Any", "No Fit", "Cove", "Obtuse Cove", "Square Cove", "Obtuse Square Cove", "Square", "Thin", "Bone", "Exaggerated", "Triangle", "Normal Sans", "Obtuse Sans", "Perp Sans", "Flared", "Rounded" };
static const char *panose2[] = { "Any", "No Fit", "Very Light", "Light", "Thin", "Book", "Medium", "Demi", "Bold", "Heavy", "Black", "Nord" };
static const char *panose3[] = { "Any", "No Fit", "Old Style", "Modern", "Even Width", "Expanded", "Condensed", "Very Expanded", "Very Condensed", "Monospaced" };
static const char *panose4[] = { "Any", "No Fit", "None", "Very Low", "Low", "Medium Low", "Medium", "Medium High", "High", "Very High" };
static const char *panose5[] = { "Any", "No Fit", "No Variation", "Gradual/Diagonal", "Gradual/Transitional", "Gradual/Vertical", "Gradual/Horizontal", "Rapid/Vertical", "Rapid/Horizontal", "Instant/Vertical", "Instant/Horizontal" };
static const char *panose6[] = { "Any", "No Fit", "Straight Arms/Horizontal", "Straight Arms/Wedge", "Straight Arms/Vertical", "Straight Arms/Single Serif", "Straight Arms/Double Serif", "Non-Straight Arms/Horizontal", "Non-Straight Arms/Wedge", "Non-Straight Arms/Vertical", "Non-Straight Arms/Single Serif", "Non-Straight Arms/Double Serif" };
static const char *panose7[] = { "Any", "No Fit", "Normal/Contact", "Normal/Weighted", "Normal/Boxed", "Normal/Flattened", "Normal/Rounded", "Normal/Off-Center", "Normal/Square", "Oblique/Contact", "Oblique/Weighted", "Oblique/Boxed", "Oblique/Flattened", "Oblique/Rounded", "Oblique/Off-Center", "Oblique/Square" };
static const char *panose8[] = { "Any", "No Fit", "Standard/Trimmed", "Standard/Pointed", "Standard/Serifed", "High/Trimmed", "High/Pointed", "High/Serifed", "Constant/Trimmed", "Constant/Pointed", "Constant/Serifed", "Low/Trimmed", "Low/Pointed", "Low/Serifed" };
static const char *panose9[] = { "Any", "No Fit", "Constant/Small", "Constant/Standard", "Constant/Large", "Ducking/Small", "Ducking/Standard", "Ducking/Large" };
static struct { const char *name; char const *const *const choices; int cnt; } panose[10] = {
{ "Family", panose0, sizeof(panose0) },
{ "Serif Type", panose1, sizeof(panose1) },
{ "Weight", panose2, sizeof(panose2) },
{ "Proportion", panose3, sizeof(panose3) },
{ "Contrast", panose4, sizeof(panose4) },
{ "Stroke Variation", panose5, sizeof(panose5) },
{ "Arm Style", panose6, sizeof(panose6) },
{ "Letterform", panose7, sizeof(panose7) },
{ "Midline", panose8, sizeof(panose8) },
{ "X-Height", panose9, sizeof(panose9) } };
fseek(ttf,info->os2_start,SEEK_SET);
printf( "\nOS/2 table (at %d for %d bytes)\n", info->os2_start, info->os2_len );
printf( "\tVersion=%d\n", getushort(ttf));
printf( "\t avgWidth=%d\n", (short)getushort(ttf));
printf( "\t weightClass=%d ", val = getushort(ttf));
if ( val>=0 && val<=999 ) printf( "%s\n", weights[val/100] ); else printf( "???\n");
printf( "\t widthClass=%d ", val = getushort(ttf));
if ( val>=0 && val<=9 ) printf( "%s\n", widths[val] ); else printf( "???\n");
printf( "\t fstype=0x%x ", (unsigned int)((val = (short) getushort(ttf))) );
if ( val&0x8 ) printf( "Editable embedding\n");
else if ( val&0x4 ) printf( "Preview & Print embedding\n");
else if ( val&0x2 ) printf( "Restricted license embedding\n");
else printf( "\n");
printf( "\t ySubscript XSize=%d\n", (short) getushort(ttf));
printf( "\t ySubscript YSize=%d\n", (short) getushort(ttf));
printf( "\t ySubscript XOffset=%d\n", (short) getushort(ttf));
printf( "\t ySubscript YOffset=%d\n", (short) getushort(ttf));
printf( "\t ySupscript XSize=%d\n", (short) getushort(ttf));
printf( "\t ySupscript YSize=%d\n", (short) getushort(ttf));
printf( "\t ySupscript XOffset=%d\n", (short) getushort(ttf));
printf( "\t ySupscript YOffset=%d\n", (short) getushort(ttf));
printf( "\t yStrikeoutSize=%d\n", (short) getushort(ttf));
printf( "\t yStrikeoutPos=%d\n", (short) getushort(ttf));
printf( "\t sFamilyClass=%04x ", (unsigned int)((val = getushort(ttf))) );
if ( (val>>8)>=0 && (val>>8)<16 ) {
printf( "%s ", class[val>>8]);
if ( (val&0xff)<16 )
printf( "%s\n", subclasses[val>>8][val&0xff]);
else
printf( "???\n" );
} else printf ( "??? ???\n" );
printf( "\t Panose\n" );
for ( i=0; i<10; ++i ) {
printf( "\t\t%s: %02x ", panose[i].name, (unsigned int)(val= getc(ttf)) );
if ( val>0 && val<panose[i].cnt ) printf( "%s\n", panose[i].choices[val]);
else printf( "???\n" );
}
printf( "\n");
printf( "\t UnicodeRange=%08x ", (unsigned int)(getlong(ttf)) );
printf( "%08x ", (unsigned int)(getlong(ttf)) );
printf( "%08x ", (unsigned int)(getlong(ttf)) );
printf( "%08x\n", (unsigned int)(getlong(ttf)) );
printf( "\t achVendId " );
for ( i=0; i<4; ++i )
printf( "%02x ", (unsigned int)(getc(ttf)) );
printf( "\n");
printf( "\t fsSelection=%d\n", getushort(ttf));
printf( "\t firstcharindex=%d\n", getushort(ttf));
printf( "\t lastcharindex=%d\n", getushort(ttf));
printf( "\t stypeascender=%d\n", getushort(ttf));
printf( "\t stypedescender=%d\n", (short) getushort(ttf));
printf( "\t stypelinegap=%d\n", getushort(ttf));
printf( "\t usWinAscent=%d\n", getushort(ttf));
printf( "\t usWinDescent=%d\n", (short) getushort(ttf));
if ( info->os2_len==78 ) {
printf( "\t (no CodePageRange)\n" );
} else {
printf( "\t CodePageRange=%08x ", (unsigned int)(getlong(ttf)) );
printf( "%08x\n", (unsigned int)(getlong(ttf)) );
if ( info->os2_len==96 ) {
/* Open type additions */
printf( "\txHeight=%d\n", (short) getushort(ttf));
printf( "\tCapHeight=%d\n", (short) getushort(ttf));
printf( "\tDefaultChar=%d\n", getushort(ttf)); /* 0, or space I guess */
printf( "\tBreakChar=%d\n", getushort(ttf)); /* space */
printf( "\tMaxContext=%d\n", getushort(ttf)); /* Maximum look-ahead needed for processing. kerning=>2. 3letter ligature=>3, 4letter=>4 */
}
}
}
const unsigned short unicode_from_mac[] = {
0x0000, 0x00d0, 0x00f0, 0x0141, 0x0142, 0x0160, 0x0161, 0x00dd,
0x00fd, 0x0009, 0x000a, 0x00de, 0x00fe, 0x000d, 0x017d, 0x017e,
0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x00bd, 0x00bc, 0x00b9,
0x00be, 0x00b3, 0x00b2, 0x00a6, 0x00ad, 0x00d7, 0x001e, 0x001f,
0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f,
0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f,
0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057,
0x0058, 0x0059, 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f,
0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067,
0x0068, 0x0069, 0x006a, 0x006b, 0x006c, 0x006d, 0x006e, 0x006f,
0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077,
0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d, 0x007e, 0x007f,
0x00c4, 0x00c5, 0x00c7, 0x00c9, 0x00d1, 0x00d6, 0x00dc, 0x00e1,
0x00e0, 0x00e2, 0x00e4, 0x00e3, 0x00e5, 0x00e7, 0x00e9, 0x00e8,
0x00ea, 0x00eb, 0x00ed, 0x00ec, 0x00ee, 0x00ef, 0x00f1, 0x00f3,
0x00f2, 0x00f4, 0x00f6, 0x00f5, 0x00fa, 0x00f9, 0x00fb, 0x00fc,
0x2020, 0x00b0, 0x00a2, 0x00a3, 0x00a7, 0x2022, 0x00b6, 0x00df,
0x00ae, 0x00a9, 0x2122, 0x00b4, 0x00a8, 0x2260, 0x00c6, 0x00d8,
0x221e, 0x00b1, 0x2264, 0x2265, 0x00a5, 0x00b5, 0x2202, 0x2211,
0x220f, 0x03c0, 0x222b, 0x00aa, 0x00ba, 0x2126, 0x00e6, 0x00f8,
0x00bf, 0x00a1, 0x00ac, 0x221a, 0x0192, 0x2248, 0x2206, 0x00ab,
0x00bb, 0x2026, 0x00a0, 0x00c0, 0x00c3, 0x00d5, 0x0152, 0x0153,
0x2013, 0x2014, 0x201c, 0x201d, 0x2018, 0x2019, 0x00f7, 0x25ca,
0x00ff, 0x0178, 0x2044, 0x00a4, 0x2039, 0x203a, 0xfb01, 0xfb02,
0x2021, 0x00b7, 0x201a, 0x201e, 0x2030, 0x00c2, 0x00ca, 0x00c1,
0x00cb, 0x00c8, 0x00cd, 0x00ce, 0x00cf, 0x00cc, 0x00d3, 0x00d4,
0xf8ff, 0x00d2, 0x00da, 0x00db, 0x00d9, 0x0131, 0x02c6, 0x02dc,
0x00af, 0x02d8, 0x02d9, 0x02da, 0x00b8, 0x02dd, 0x02db, 0x02c7
};
static const char *standardnames[258] = {
".notdef",
".null",
"nonmarkingreturn",
"space",
"exclam",
"quotedbl",
"numbersign",
"dollar",
"percent",
"ampersand",
"quotesingle",
"parenleft",
"parenright",
"asterisk",
"plus",
"comma",
"hyphen",
"period",
"slash",
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"colon",
"semicolon",
"less",
"equal",
"greater",
"question",
"at",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"bracketleft",
"backslash",
"bracketright",
"asciicircum",
"underscore",
"grave",
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
"braceleft",
"bar",
"braceright",
"asciitilde",
"Adieresis",
"Aring",
"Ccedilla",
"Eacute",
"Ntilde",
"Odieresis",
"Udieresis",
"aacute",
"agrave",
"acircumflex",
"adieresis",
"atilde",
"aring",
"ccedilla",
"eacute",
"egrave",
"ecircumflex",
"edieresis",