-
Notifications
You must be signed in to change notification settings - Fork 52
/
TTFTemplate.bt
executable file
·991 lines (898 loc) · 39 KB
/
TTFTemplate.bt
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
//--- 010 Editor v2.0 Binary Template
//
// File: TTFTemplate.bt
// Author: James Newton of massmind.org
// Revisions:
// .1 reads the offsetTable
// .2 reads some tables
// .3 actually reads simple glyphs
// .31 reads simple glyphs with the points in order
// .32 reads simple glyphs with the points in CORRECT order
// .4 added prep and DSIG tables
// - fixed serious bug with offset and length from wrong
// table being used in table structures.
// .5 added GSUB and GPOS tables.
// Purpose: Easy examination and editing
// of TTF binary data
//--------------------------------------
BigEndian();
typedef signed long TT_Fixed; /* Signed Fixed 16.16 Float */
typedef signed short TT_FWord; /* Distance in FUnits */
typedef unsigned short TT_UFWord; /* Unsigned distance */
typedef signed short TT_Short;
typedef unsigned short TT_UShort;
typedef signed long TT_Long;
typedef unsigned long TT_ULong;
typedef unsigned long TT_Offset;
typedef OLETIME LONGDATETIME;
typedef signed short F2Dot14;
typedef struct tOffsetTable {
TT_Fixed SFNT_Ver; //sfnt version 0x00010000 for version 1.0.
USHORT numTables; //Number of tables.
USHORT searchRange; //(Maximum power of 2 <= numTables) x 16.
USHORT entrySelector; // Log2(maximum power of 2 <= numTables).
USHORT rangeShift; // NumTables x 16-searchRange.
};
typedef struct tTable {
union {
char asChar[4]; // 4 -byte identifier.
ULONG asLong;
} Tag;
ULONG checkSum; // CheckSum for this table.
ULONG offset; // Offset from beginning of TrueType font file.
ULONG length; // Length of this table.
};
string tTableRead( tTable &d ) {
char s[20];
SPrintf( s, " (%u) at %u for %u", d.Tag.asLong, d.offset, d.length);
s = d.Tag.asChar + s;
// s = "if (findTable(\"" + d.Tag.asChar + "\")) {struct t" + d.Tag.asChar + " " + d.Tag.asChar + " <read=t"+ d.Tag.asChar + "Read>";
return s;
}
local quad cmap_table;
local quad cmap_record;
local quad cmap_subtable;
local quad next_cmap_record;
typedef struct tcmap_format0 {
cmap_subtable = FTell();
USHORT format; // Format number is set to 0.
USHORT length; // This is the length in bytes of the subtable.
USHORT language; // Please see "Note on the language field in 'cmap' subtables" in this document.
BYTE glyphIdArray[256]; // An array that maps character codes to glyph index values.
};
typedef struct tcmap_format4 {
cmap_subtable = FTell();
USHORT format; // Format number is set to 4.
USHORT length; // This is the length in bytes of the subtable.
USHORT language; // Please see "Note on the language field in 'cmap' subtables" in this document.
USHORT segCountX2; // 2 x segCount.
USHORT searchRange; // 2 x (2**floor(log2(segCount)))
USHORT entrySelector; // log2(searchRange/2)
USHORT rangeShift; // 2 x segCount - searchRange
USHORT endCount[segCountX2 / 2]; // End characterCode for each segment, last=0xFFFF.
USHORT reservedPad; // Set to 0.
USHORT startCount[segCountX2 / 2]; // Start character code for each segment.
SHORT idDelta[segCountX2 / 2]; // Delta for all character codes in segment.
USHORT idRangeOffset[segCountX2 / 2]; // Offsets into glyphIdArray or 0
USHORT glyphIdArray[(length-(FTell()-cmap_subtable))/2 ]; // Glyph index array (arbitrary length)
//Thank you ever so much for giving us a simple count of the array entires... Jerks.
};
typedef struct tcmap_format6 {
cmap_subtable = FTell();
USHORT format; // Format number is set to 6.
USHORT length; // This is the length in bytes of the subtable.
USHORT language; // Please see "Note on the language field in 'cmap' subtables" in this document.
USHORT firstCode; // First character code of subrange.
USHORT entryCount; // Number of character codes in subrange.
USHORT glyphIdArray [entryCount]; // Array of glyph index values for character codes in the range.
};
typedef struct tcmap_format8 {
cmap_subtable = FTell();
USHORT format; // Subtable format; set to 8.
USHORT reserved; // Reserved; set to 0
ULONG length; // Byte length of this subtable (including the header)
ULONG language; // Please see "Note on the language field in 'cmap' subtables" in this document.
BYTE is32[8192]; // Tightly packed array of bits (8K bytes total) indicating whether the particular 16-bit (index) value is the start of a 32-bit character code
ULONG nGroups; // Number of groupings which follow
struct {
ULONG startCharCode; // First character code in this group; note that if this group is for one or more 16-bit character codes (which is determined from the is32 array), this 32-bit value will have the high 16-bits set to zero
ULONG endCharCode; // Last character code in this group; same condition as listed above for the startCharCode
ULONG startGlyphID; // Glyph index corresponding to the starting character code
} groupings[nGroups];
};
typedef struct tcmap_format12 {
USHORT format; // Subtable format; set to 12.
USHORT reserved; // Reserved; set to 0
ULONG length; // Byte length of this subtable (including the header)
ULONG language; // Please see "Note on the language field in 'cmap' subtables" in this document.
ULONG nGroups; // Number of groupings which follow
struct {
ULONG startCharCode; // First character code in this group
ULONG endCharCode; // Last character code in this group
ULONG startGlyphID; // Glyph index corresponding to the starting character code
} groupings[nGroups];
};
typedef struct tcmap {
cmap_table = FTell();
USHORT version; // Table version number (0).
USHORT numTables; // Number of encoding tables that follow.
struct tEncodingRecord {
cmap_record = FTell();
USHORT platformID; // Platform ID.
USHORT encodingID; // Platform-specific encoding ID.
ULONG offset; // Byte offset from beginning of table to the subtable for this encoding.
next_cmap_record = FTell();
FSeek(cmap_table+offset);
switch (ReadUShort(FTell())) {
case 0 : struct tcmap_format0 format0; break;
// case 2 : struct tcmap_format2 format2; break; //TODO
case 4 : struct tcmap_format4 format4; break;
case 6 : struct tcmap_format6 format6; break;
case 8 : struct tcmap_format8 format8; break;
// case 10 : struct tcmap_format10 format10; break; //TODO
case 12 : struct tcmap_format12 format12; break;
default : ;
}
FSeek(next_cmap_record);
} EncodingRecord[numTables] <optimize=false>;
};
typedef struct thdmx {
USHORT version; // Table version number (0)
SHORT numRecords; // Number of device records.
LONG sizeDeviceRecord; // Size of a device record, long aligned.
struct {
local quad hdmx_DeviceRecord = FTell();
UBYTE pixelSize; // Pixel size for following widths (as ppem).
UBYTE maxWidth; // Maximum width.
local quad numGlyphs = (sizeDeviceRecord - (FTell() - hdmx_DeviceRecord) ) / 1;
UBYTE widths[numGlyphs]; // Array of widths (numGlyphs is from the 'maxp' table).
} DeviceRecord[numRecords] <optimize=false>;
};
string thdmxRead( thdmx &d ) {
string s;
SPrintf( s, "V%i %u records %u bytes", d.version, d.numRecords, d.sizeDeviceRecord);
return s;
// return d.name; //use this instead to get the Unicode, etc... characters.
}
typedef struct thead {
TT_Fixed version; //Table version number 0x00010000 for version 1.0.
TT_Fixed fontRevision; // Set by font manufacturer.
ULONG checkSumAdjustment; // To compute: set it to 0, sum the entire font as ULONG, then store 0xB1B0AFBA - sum.
ULONG magicNumber; // Set to 0x5F0F3CF5.
USHORT flags; // lots of flags...
/*
Bit 0: Baseline for font at y=0;
Bit 1: Left sidebearing point at x=0;
Bit 2: Instructions may depend on point size;
Bit 3: Force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear;
Bit 4: Instructions may alter advance width (the advance widths might not scale linearly);
Bits 5-10: These should be set according to Apple's specification . However, they are not implemented in OpenType.
Bit 11: Font data is 'lossless,' as a result of having been compressed and decompressed with the Agfa MicroType Express engine.
Bit 12: Font converted (produce compatible metrics)
Bit 13: Font optimised for ClearType
Bit 14: Reserved, set to 0
Bit 15: Reserved, set to 0
*/
USHORT unitsPerEm; // Valid range is from 16 to 16384. This value should be a power of 2 for fonts that have TrueType outlines.
LONGDATETIME created; // Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer
LONGDATETIME modified; // Number of seconds since 12:00 midnight, January 1, 1904. 64-bit integer
SHORT xMin; // For all glyph bounding boxes.
SHORT yMin; // For all glyph bounding boxes.
SHORT xMax; // For all glyph bounding boxes.
SHORT yMax; // For all glyph bounding boxes.
USHORT macStyle; //
/*
Bit 0: Bold (if set to 1);
Bit 1: Italic (if set to 1)
Bit 2: Underline (if set to 1)
Bit 3: Outline (if set to 1)
Bit 4: Shadow (if set to 1)
Bit 5: Condensed (if set to 1)
Bit 6: Extended (if set to 1)
Bits 7-15: Reserved (set to 0).
*/
USHORT lowestRecPPEM; //Smallest readable size in pixels.
SHORT fontDirectionHint; //
/*
0: Fully mixed directional glyphs;
1: Only strongly left to right;
2: Like 1 but also contains neutrals;
-1: Only strongly right to left;
-2: Like -1 but also contains neutrals. 1
*/
SHORT indexToLocFormat; // 0 for short offsets, 1 for long.
SHORT glyphDataFormat; // 0 for current format.
};
typedef struct thhea {
TT_Fixed version; // Table version number 0x00010000 for version 1.0.
TT_FWord Ascender; // Typographic ascent. (Distance from baseline of highest ascender)
TT_FWord Descender; // Typographic descent. (Distance from baseline of lowest descender)
TT_FWord LineGap; // Typographic line gap.
//Negative LineGap values are treated as zero
//in Windows 3.1, System 6, and
//System 7.
TT_UFWord advanceWidthMax; // Maximum advance width value in 'hmtx' table.
TT_FWord minLeftSideBearing; // Minimum left sidebearing value in 'hmtx' table.
TT_FWord minRightSideBearing; // Minimum right sidebearing value; calculated as Min(aw - lsb - (xMax - xMin)).
TT_FWord xMaxExtent; // Max(lsb + (xMax - xMin)).
SHORT caretSlopeRise; // Used to calculate the slope of the cursor (rise/run); 1 for vertical.
SHORT caretSlopeRun; // 0 for vertical.
SHORT caretOffset; // The amount by which a slanted highlight on a glyph needs to be shifted to produce the best appearance. Set to 0 for non-slanted fonts
SHORT reserved; // set to 0
SHORT reserved; // set to 0
SHORT reserved; // set to 0
SHORT reserved; // set to 0
SHORT metricDataFormat; // 0 for current format.
USHORT numberOfHMetrics; // Number of hMetric entries in 'hmtx' table
};
string thheaRead( thhea &d ) {
string s;
SPrintf( s, "v%.2f %u hmtx records", d.version/65535.0, d.numberOfHMetrics);
return s;
// return d.name; //use this instead to get the Unicode, etc... characters.
}
typedef struct tlongHorMetric {
USHORT advanceWidth;
SHORT lsb;
};
typedef struct thmtx {
// Oh... My... GOD! Who came up with this one? It would kill someone to put numberOfHMetrics in hmtx?
// What if the hhea tag is after hhea must be unpacked first.
local ulong numberOfHMetrics = hhea.numberOfHMetrics;
struct tlongHorMetric hMetrics[numberOfHMetrics];
// Paired advance width and left side bearing values for each glyph. The value numOfHMetrics comes from the 'hhea' table.
// If the font is monospaced, only one entry need be in the array, but that entry is required. The last entry applies to all subsequent glyphs.
local ulong numLeftSideBearing = ( Table[currentTable].length - (FTell()-Table[currentTable].offset) )/2;
SHORT leftSideBearing[ numLeftSideBearing ];
// Here the advanceWidth is assumed to be the same as the advanceWidth for the last entry above.
// The number of entries in this array is derived from numGlyphs (from 'maxp' table) minus numberOfHMetrics.
// This generally is used with a run of monospaced glyphs (e.g., Kanji fonts or Courier fonts).
// Only one run is allowed and it must be at the end. This allows a monospaced font to vary the left side bearing values for each glyph.
};
string thmtxRead( thmtx &d ) {
string s;
SPrintf( s, "%u HMetrics %u leftSideBearing", d.numberOfHMetrics, d.numLeftSideBearing);
return s;
}
typedef struct tmaxp {
TT_Fixed version; //Table version number 0x00005000 for version 0.5
//(Note the difference in the representation of a non-zero fractional part, in Fixed numbers.)
if (version == 0x00005000) {
USHORT numGlyphs; // The number of glyphs in the font.
}
else {
// TT_Fixed version; // Table version number 0x00010000 for version 1.0.
USHORT numGlyphs; // The number of glyphs in the font.
USHORT maxPoints; // Maximum points in a non-composite glyph.
USHORT maxContours; // Maximum contours in a non-composite glyph.
USHORT maxCompositePoints; // Maximum points in a composite glyph.
USHORT maxCompositeContours; // Maximum contours in a composite glyph.
USHORT maxZones; // 1 if instructions do not use the twilight zone (Z0), or 2 if instructions do use Z0; should be set to 2 in most cases.
USHORT maxTwilightPoints; // Maximum points used in Z0.
USHORT maxStorage; // Number of Storage Area locations.
USHORT maxFunctionDefs; // Number of FDEFs.
USHORT maxInstructionDefs; // Number of IDEFs.
USHORT maxStackElements; // Maximum stack depth2.
USHORT maxSizeOfInstructions; // Maximum byte count for glyph instructions.
USHORT maxComponentElements; // Maximum number of components referenced at "top level" for any composite glyph.
USHORT maxComponentDepth; // Maximum levels of recursion; 1 for simple components.
}
};
string tmaxpRead( tmaxp &d ) {
string s;
if (d.version == 0x00005000) {
SPrintf( s, "v%.2f %u glyphs", d.version/65535.0, d.numGlyphs);
}
else {
SPrintf( s, "v%.2f %u glyphs %u points %u contours", d.version/65535.0, d.numGlyphs,d.maxPoints,d.maxContours);
}
return s;
}
typedef struct tname {
// http://www.microsoft.com/typography/OTSPEC/name.htm
local quad name_table = FTell();
USHORT format; // Format selector (=0).
USHORT count; // Number of name records.
USHORT stringOffset; // Offset to start of string storage (from start of table).
local quad NextNameRecord;
struct tNameRecord {
USHORT platformID; // Platform ID.
USHORT encodingID; // Platform-specific encoding ID.
USHORT languageID; // Language ID.
USHORT nameID; // Name ID.
USHORT length; // String length (in bytes).
USHORT offset; // String offset from start of storage area (in bytes).
NextNameRecord = FTell();
FSeek(name_table + stringOffset + offset);
char name[length];
FSeek(NextNameRecord);
} NameRecord[count] <read=NameRecordRead, optimize = false>;
};
string NameRecordRead( tNameRecord &d ) {
string s;
SPrintf( s, "p%i E%i L%i %s", d.platformID, d.encodingID, d.languageID, d.name );
return s;
// return d.name; //use this instead to get the Unicode, etc... characters.
}
string tnameRead( tname &name ) {
string s;
SPrintf( s, "%li Names", name.count );
return s;
}
typedef struct tOS_2 {
// http://www.microsoft.com/typography/OTSPEC/os2.htm
USHORT version; // 0x0003
SHORT xAvgCharWidth; //
USHORT usWeightClass; //
USHORT usWidthClass ; //
USHORT fsType; //
SHORT ySubscriptXSize; //
SHORT ySubscriptYSize; //
SHORT ySubscriptXOffset; //
SHORT ySubscriptYOffset; //
SHORT ySuperscriptXSize; //
SHORT ySuperscriptYSize; //
SHORT ySuperscriptXOffset; //
SHORT ySuperscriptYOffset; //
SHORT yStrikeoutSize; //
SHORT yStrikeoutPosition; //
SHORT sFamilyClass; //
struct tpanose {
UBYTE bFamilyType;
UBYTE bSerifStyle;
UBYTE bWeight;
UBYTE bProportion;
UBYTE bContrast;
UBYTE bStrokeVariation;
UBYTE bArmStyle;
UBYTE bLetterform;
UBYTE bMidline;
UBYTE bXHeight;
} panose;
ULONG ulUnicodeRange1; // Bits 0-31
ULONG ulUnicodeRange2; // Bits 32-63
ULONG ulUnicodeRange3; // Bits 64-95
ULONG ulUnicodeRange4; // Bits 96-127
CHAR achVendID[4]; //
USHORT fsSelection; //
USHORT usFirstCharIndex; //
USHORT usLastCharIndex; //
SHORT sTypoAscender; //
SHORT sTypoDescender; //
SHORT sTypoLineGap; //
USHORT usWinAscent; //
USHORT usWinDescent; //
ULONG ulCodePageRange1; // Bits 0-31
ULONG ulCodePageRange2; // Bits 32-63
SHORT sxHeight; //
SHORT sCapHeight; //
USHORT usDefaultChar; //
USHORT usBreakChar; //
USHORT usMaxContext; //
};
string tOS_2Read( tOS_2 &d ) {
string s;
SPrintf( s, "v%i chars %i to %i from %4s", d.version, d.usFirstCharIndex, d.usLastCharIndex, d.achVendID);
return s;
}
typedef struct tpost {
local quad post = FTell();
TT_Fixed version;
//0x00010000 for version 1.0
//0x00020000 for version 2.0
//0x00025000 for version 2.5 (deprecated)
//0x00030000 for version 3.0
TT_Fixed italicAngle; // Italic angle in counter-clockwise degrees from the vertical. Zero for upright text, negative for text that leans to the right (forward).
TT_FWord underlinePosition; // This is the suggested distance of the top of the underline from the baseline (negative values indicate below baseline).
// The PostScript definition; // of this FontInfo dictionary key (the y coordinate of the center of the stroke) is not used for historical reasons. The value of the PostScript key may be calculated by subtracting half the underlineThickness from the value of this field.
TT_FWord underlineThickness; // Suggested values for the underline thickness.
ULONG isFixedPitch; // Set to 0 if the font is proportionally spaced, non-zero if the font is not proportionally spaced (i.e. monospaced).
ULONG minMemType42; // Minimum memory usage when an OpenType font is downloaded.
ULONG maxMemType42; // Maximum memory usage when an OpenType font is downloaded.
ULONG minMemType1; // Minimum memory usage when an OpenType font is downloaded as a Type 1 font.
ULONG maxMemType1; // Maximum memory usage when an OpenType font is downloaded as a Type 1 font.
if (version == 0x00010000) {
}
if (version == 0x00020000) {
USHORT numberOfGlyphs; // Number of glyphs (this should be the same as numGlyphs in 'maxp' table).
local ushort numGlyphs = numberOfGlyphs; //? Assumption TODO: Verify. Seems to work
USHORT glyphNameIndex[numGlyphs]; //This is not an offset, but is the ordinal number of the glyph in 'post' string tables.
local ushort numberNewGlyphs = numberOfGlyphs; //? Assumption FALSE TODO: FIX
local quad end_name = post+Table[currentTable].length;
local quad next_name = FTell();
while (next_name < end_name) {
struct tpostName {
UBYTE length;
CHAR text[length];
} name <read=tpostNameRead>;
next_name = FTell();
}; // Glyph names with length bytes [variable] (a Pascal string).
}
if (version == 0x00025000) {
USHORT numberOfGlyphs; // Number of glyphs (this should be the same as numGlyphs in 'maxp' table).
local ushort numGlyphs = numberOfGlyphs; //? Assumption TODO: Verify. Seems to work
USHORT offset[numGlyphs]; //This is not an offset, but is the ordinal number of the glyph in 'post' string tables.
}
if (version == 0x00030000) {
}
};
string tpostNameRead( tpostName &d ) {
return d.text; //use this instead to get the Unicode, etc... characters.
}
string tpostRead( tpost &d ) {
string s;
SPrintf( s, "v%.2f %u glyphs", d.version/65535.0, d.numberOfGlyphs);
if (d.isFixedPitch) { s += " fixed pitch"; } else { s += " proportional"; };
return s;
};
typedef struct tcvt {
local quad n = Table[currentTable].length / sizeof(TT_FWord);
TT_FWord value[ n ]; // List of n values referenceable by instructions. n is the number of FWORD items that fit in the size of the table.
};
typedef struct tfpgm {
local quad n = Table[currentTable].length / sizeof(UBYTE);
UBYTE bytecode[ n ]; // Instructions. n is the number of BYTE items that fit in the size of the table.
};
typedef struct tloca {
local ulong n = maxp.numGlyphs + 1 ;
local short format = head.indexToLocFormat;
if (format == 0) {
USHORT offsets[n];
}
else {
ULONG offsets[n];
}
};
string tlocaRead( tloca &d ) {
string s;
if (d.format == 0) SPrintf( s, "%u short offsets", d.n);
else SPrintf( s, "%u long offsets", d.n);
return s;
}
//the following locals are masks for "flags" 010 v2 doesn't support #define
local USHORT ARG_1_AND_2_ARE_WORDS = 1<<0; // If this is set, the arguments are words; otherwise, they are bytes.
local USHORT ARGS_ARE_XY_VALUES = 1<<1; // If this is set, the arguments are xy values; otherwise, they are points.
local USHORT ROUND_XY_TO_GRID = 1<<2; // For the xy values if the preceding is true.
local USHORT WE_HAVE_A_SCALE = 1<<3; // This indicates that there is a simple scale for the component. Otherwise, scale = 1.0.
local USHORT RESERVED = 1<<4; // This bit is reserved. Set it to 0.
local USHORT MORE_COMPONENTS = 1<<5; // Indicates at least one more glyph after this one.
local USHORT WE_HAVE_AN_X_AND_Y_SCALE = 1<<6; // The x direction will use a different scale from the y direction.
local USHORT WE_HAVE_A_TWO_BY_TWO = 1<<7; // There is a 2 by 2 transformation that will be used to scale the component.
local USHORT WE_HAVE_INSTRUCTIONS = 1<<8; // Following the last component are instructions for the composite character.
local USHORT USE_MY_METRICS = 1<<9; // If set, this forces the aw and lsb (and rsb) for the composite to be equal to those from this original glyph. This works for hinted and unhinted characters.
local USHORT OVERLAP_COMPOUND = 1<<10; // Used by Apple in GX fonts.
local USHORT SCALED_COMPONENT_OFFSET = 1<<11; // Composite designed to have the component offset scaled (designed for Apple rasterizer).
local USHORT UNSCALED_COMPONENT_OFFSET = 1<<12; // Composite designed not to have the component offset scaled (designed for the Microsoft TrueType rasterizer).
// these flags tell us:
// bit name description
local UBYTE ON_CURVE = 1<<0; // If set, the point is on the curve; otherwise, it is off the curve.
local UBYTE X_BYTE = 1<<1; // aka "X-Short" If set, the corresponding x-coordinate is 1 byte long. If not set, 2 bytes.
local UBYTE Y_BYTE = 1<<2; // aka "Y-Short" If set, the corresponding y-coordinate is 1 byte long. If not set, 2 bytes.
//I believe the name x-Short was intended to mean "small" rather than the type SHORT.
//I've changed the names to X_BYTE and Y_BYTE since they are BYTES if set to 1
local UBYTE REPEAT = 1<<3; // If set, the next byte specifies the number of additional times this set of flags is to be repeated.
// In this way, the number of flags listed can be smaller than the number of points in a character.
local UBYTE X_TYPE = 1<<4; // This flag has two meanings, depending on how the x-Short flag is set.
// If x-Short is set, this bit describes the sign of the value,
// with 1 equalling positive and 0 negative.
// If the x-Short bit is not set and this bit is set, then
// the current x-coordinate is the same as the previous x-coordinate.
// If the x-Short bit is not set and this bit is also not set,
// the current x-coordinate is a signed 16-bit delta vector.
local UBYTE Y_TYPE = 1<<5; // This flag has two meanings, depending on how the y-Short Vector flag is set.
// If y-Short Vector is set, this bit describes the sign of the value,
// with 1 equalling positive and 0 negative.
// If the y-Short Vector bit is not set and this bit is set, then
// the current y-coordinate is the same as the previous y-coordinate.
// If the y-Short Vector bit is not set and this bit is also not set,
// the current y-coordinate is a signed 16-bit delta vector.
local UBYTE RES_1 = 1<<6; // 6 Res This bit is reserved. Set it to zero.
local UBYTE RES_2 = 1<<7; // 7 Res This bit is reserved. Set it to zero.
typedef struct tSimpleGlyphPoints {
local ulong i;
local quad xStart, xLast; xStart = xLast = FTell();
local quad yStart, yLast; yStart = FTell(); // Printf("xStart: %u\n",yStart);
yLast = 0;
local byte xLastByte = 0;
local byte yLastByte = 0;
// Printf ("%u\n",numPoints);
for (i = 0; i < numPoints; i++) {
// Printf("%u, %u X_TYPE:%u",i,flag[i],(flag[i] & X_TYPE));
if ( !( flag[i] & X_BYTE ) && ( flag[i] & X_TYPE ) ) { }//Printf ("R");}
else {
yStart++; //Printf("x");
if ( !(flag[i] & X_BYTE) ) {yStart++; }//Printf("big");}
// when flag bit 3 is not set, the x coordinate for that point is another byte long
} // Printf("\n");
} Printf("yStart: %u\n",yStart);
// Now we can decode points in pairs...
for (i = 0; i < numPoints; i++) {
struct tPoints {
FSeek(xStart);
if ( flag[i] & X_BYTE ) {
xLast = FTell(); xLastByte=1;
UBYTE xDelta; xStart = FTell();
}
else {
if ( ( flag[i] & X_TYPE ) ) {
FSeek(xLast);
if (xLast>0) {if (xLastByte) UBYTE xDeltaRepeat; else SHORT xDeltaRepeat;}
}
else {
xLast = FTell();xLastByte=0;
SHORT xDelta; xStart = FTell();
}
}
FSeek(yStart);
if ( flag[i] & Y_BYTE ) {
yLast = FTell(); yLastByte=1;
UBYTE yDelta; yStart = FTell();
}
else {
if ( ( flag[i] & Y_TYPE ) ) {
FSeek(yLast);
if (yLast>0) {if (yLastByte) UBYTE yDeltaRepeat; else SHORT yDeltaRepeat;}
}
else {
yLast = FTell(); yLastByte=0;
SHORT yDelta; yStart = FTell();
}
}
FSeek(xStart);
//First coordinates relative to (0,0); others are relative to previous point.
} points <optimize=false>;
}
};
string tPointsRead ( tSimpleGlyphPoints &d ) {
string s;
s="hello";
// long x;
// if (exists(d.xDelta)) x = d.xDelta; else x = d.xDeltaRepeat;
// if (exists(d.yDelta)) y = d.yDelta; else y = d.yDeltaRepeat;
// SPrintf(s, "%u",x);
return s;
}
typedef UBYTE bitFlag;
local bitFlag flag_repeat=0;
typedef struct tSimpleGlyphFlags {
if (flag_repeat) {
UBYTE count;
flag_repeat = 0;
}
else {
BitfieldRightToLeft();
bitFlag onCurve :1;
bitFlag xByte :1;
bitFlag yByte :1;
bitFlag repeat :1;
bitFlag xType :1;
bitFlag yType :1;
if (repeat) flag_repeat = 1;
}
};
string tSimpleGlyphFlagsRead ( tSimpleGlyphFlags &d ) {
string s;
if (exists(d.count)) {
SPrintf(s, " repeat %u times", d.count);
}
else {
s="Point";
if (d.onCurve) s += " OnCurve";
if (!d.xByte) {s += " X"; if (d.xType) s += "R";}
if (d.xByte) {s += " x"; if (d.xType) s += "+"; else s += "-";}
if (!d.yByte) {s += " Y"; if (d.yType) s += "R";}
if (d.yByte) {s += " y"; if (d.yType) s += "+"; else s += "-";}
if (d.repeat) s += " REPEAT:";
}
return s;
}
typedef struct tSimpleGlyph {
SHORT numberOfContours;
SHORT xMin; // Minimum x for coordinate data.
SHORT yMin; // Minimum y for coordinate data.
SHORT xMax; // Maximum x for coordinate data.
SHORT yMax; // Maximum y for coordinate data.
USHORT endPtsOfContours[numberOfContours]; // Array of last points of each contour; n is the number of contours.
USHORT instructionLength; // Total number of bytes for instructions.
if (instructionLength > 0) {
UBYTE instructions[instructionLength]; // Array of instructions for each glyph
}
local USHORT numPoints;
numPoints = endPtsOfContours[numberOfContours-1]+1;
//why +1? Because that works...
//,-----------------------------------
// Unpack the compressed flags table
local quad glyf_flag_table = FTell();
local quad glyf_flag_index = FTell();
local ushort i;
local ubyte repeat = 0;
local ubyte flag[numPoints];
// we have to do this in a local, 'cause of the run length compression from the "repeat" flag
local ubyte flag_value;
for (i = 0; i < numPoints; i++) {
if (repeat > 0) repeat--;
else {
flag_value = ReadUByte(glyf_flag_index++);
//only increment the pointer to the number of flags if the count on a repeat is 0
if (flag[i] & 8) repeat = ReadUByte(glyf_flag_index++);
}
flag[i] = flag_value;
}
local ushort numFlags = glyf_flag_index - glyf_flag_table;
struct {
tSimpleGlyphFlags flag[numFlags] <optimize=false, read=tSimpleGlyphFlagsRead>;
} compressedFlags;
// Array of flags for each coordinate in outline; n is the number of flags.
//`-----------------------------------
struct tSimpleGlyphPoints contours;
} ;
typedef struct tglyf {
local quad glyf_table = FTell();
local quad glyf_offset;
//The indexToLoc table stores the offsets to the locations of the glyphs in the font,
// relative to the beginning of the glyphData table.
//In order to compute the length of the last glyph element, there is an extra entry
// after the last valid index.
local ulong n;
for (n = 0; n <= 9; n++) { //BOGUS! Just to keep it real time. Use the next line instead
// for (n = 0; n <= maxp.numGlyphs; n++) {
//The (maximum) value of n is numGlyphs + 1. The value for numGlyphs is found in the 'maxp' table.
glyf_offset = loca.offsets[n];
//The actual local offset is stored, assuming the long version
//The version is specified by 'indexToLocFormat' in the head table. 0 for Short, 1 for Long.
if (head.indexToLocFormat == 0) glyf_offset *= 2;
//In the short version, the actual local offset divided by 2 is stored.
FSeek( glyf_table + glyf_offset );
if (ReadShort(FTell()) > 0) {
// If the number of contours is greater than or equal to zero, this is a single glyph;
struct tSimpleGlyph SimpleGlyph <read = tSimpleGlyphRead>;
}
if (ReadShort(FTell()) < 0 & 0==1) { // UNTESTED CODE. TODO: test
// If the number of contours is negative, this is a composite glyph.
//-------------------------------
SHORT numberOfContours;
SHORT xMin; // Minimum x for coordinate data.
SHORT yMin; // Minimum y for coordinate data.
SHORT xMax; // Maximum x for coordinate data.
SHORT yMax; // Maximum y for coordinate data.
do {
USHORT flags;
struct tGlyph {
USHORT glyphIndex;
if ( flags & ARG_1_AND_2_ARE_WORDS) {
SHORT argument1;
SHORT argument2;
}
else {
USHORT arg1and2; /* (arg1 << 8) | arg2 */
}
if ( flags & WE_HAVE_A_SCALE ) {
F2Dot14 scale; /* Format 2.14 */
}
else if ( flags & WE_HAVE_AN_X_AND_Y_SCALE ) {
F2Dot14 xscale; /* Format 2.14 */
F2Dot14 yscale; /* Format 2.14 */
}
else if ( flags & WE_HAVE_A_TWO_BY_TWO ) {
F2Dot14 xscale; /* Format 2.14 */
F2Dot14 scale01; /* Format 2.14 */
F2Dot14 scale10; /* Format 2.14 */
F2Dot14 yscale; /* Format 2.14 */
}
if (flags & WE_HAVE_INSTRUCTIONS){
USHORT numInstr;
BYTE instr[numInstr];
}
} Glyph;
} while ( flags & MORE_COMPONENTS ) ;
//-------------------------------
};
}
};
string tSimpleGlyphRead( tSimpleGlyph &d ) {
string s;
SPrintf( s, "%u contours %u insts %u flags %u points", d.numberOfContours, d.instructionLength, d.numFlags, d.numPoints);
return s;
}
typedef struct tGDEF {
TT_Fixed Version; // Version of the GDEF table-initially 0x00010000
TT_Offset GlyphClassDef; // Offset to class definition table for glyph type-from beginning of GDEF header (may be NULL)
TT_Offset AttachList; // Offset to list of glyphs with attachment points-from beginning of GDEF header (may be NULL)
TT_Offset LigCaretList; // Offset to list of positioning points for ligature carets-from beginning of GDEF header (may be NULL)
TT_Offset MarkAttachClassDef; // Offset to class definition table for mark attachment type-from beginning of GDEF header (may be NULL)
};
typedef struct tprep {
local quad n = Table[currentTable].length / sizeof(UBYTE);
UBYTE bytecode[ n ]; // Instructions. n is the number of BYTE items that fit in the size of the table.
};
typedef struct tDSIG {
local quad DSIG_table = FTell();
local quad nextSig ;
ULONG ulVersion; // Version number of the DSIG table (0x00000001)
USHORT usNumSigs; // Number of signatures in the table
USHORT usFlag; // permission flags
//Bit 0: cannot be resigned
//Bits 1-7: Reserved (Set to 0)
struct {
nextSig = FTell(); FSeek(nextSig);
ULONG ulFormat; //format of the signature
ULONG ulLength; //Length of signature in bytes
ULONG ulOffset; //Offset to the signature block from the beginning of the table
nextSig = FTell(); FSeek(DSIG_table + ulOffset);
USHORT usReserved1; // Reserved for later use; 0 for now
USHORT usReserved2; // Reserved for later use; 0 for now
ULONG cbSignature; // Length (in bytes) of the PKCS#7 packet in pbSignature
UBYTE bSignature[cbSignature]; // PKCS#7 packet
} Sigs[usNumSigs] <optimize = false>;
};
string tDSIGRead( tDSIG &d ) {
string s;
SPrintf( s, "v%u %u signature(s)", d.ulVersion, d.usNumSigs);
return s;
}
typedef struct tLangSysTable{
USHORT LookupOrder; // = NULL (reserved for an offset to a reordering table)
uint16 ReqFeatureIndex; // Index of a feature required for this language system- if no required features = 0xFFFF
uint16 FeatureCount; // Number of FeatureIndex values for this language system-excludes the required feature
uint16 FeatureIndex[FeatureCount]; // Array of indices into the FeatureList-in arbitrary order
};
typedef struct tLangSysRecord {
char LangSysTag[4]; // 4-byte LangSysTag identifier
USHORT Offset; // LangSys Offset to LangSys table-from beginning of Script table
local quad next = FTell();FSeek(ScriptTable_table + Offset);
local quad LangSys=FTell();
tLangSysTable LangSysTable;
FSeek(next);
};
string tLangSysRecordRead (tLangSysRecord &d ) {
return d.LangSysTag;
}
typedef struct tScriptRecord {
char ScriptTag[4]; //4-byte ScriptTag identifier
USHORT Offset; // to Script table-from beginning of ScriptList
local quad next = FTell();FSeek(ScriptList + Offset);
local quad ScriptTable_table=FTell();
struct {
USHORT DefaultLangSys; // Offset to DefaultLangSys table-from beginning of Script table-may be NULL
uint16 LangSysCount; // Number of LangSysRecords for this script-excluding the DefaultLangSys
tLangSysRecord LangSysRecord[LangSysCount] <optimize=false, read=tLangSysRecordRead>;
//Array of LangSysRecords-listed alphabetically by LangSysTag
} ScriptTable;
FSeek(ScriptTable_table + ScriptTable.DefaultLangSys);
tLangSysTable DefaultLangSysTable;
FSeek(next);
};
string tScriptRecordRead (tScriptRecord &d ) {
return d.ScriptTag;
}
typedef struct tScriptList {
USHORT Offset; //Offset to ScriptList table-from beginning of GSUB table
local quad next = FTell();
FSeek(GSUBorGPOS_table + Offset);
local quad ScriptList=FTell();
uint16 ScriptCount; //Number of ScriptRecords
tScriptRecord ScriptRecord[ScriptCount]<read=tScriptRecordRead, optimize = false>;
//Array of ScriptRecords -listed alphabetically by ScriptTag
FSeek(next);
};
string tScriptListRead (tScriptList &d ) {
string s;
SPrintf( s, "%u scripts", d.ScriptCount);
return s;
}
typedef struct tFeatureRecord {
char FeatureTag[4]; //4-byte FeatureTag identifier
USHORT Offset; // to Feature table-from beginning of FeatureList
local quad next = FTell();FSeek(FeatureList + Offset);
local quad FeatureTable_table=FTell();
struct {
uint16 FeatureParams; //reserved null
uint16 LookupListCount; // Number of LangSysRecords for this script-excluding the DefaultLangSys
uint16 LookupListIndex[LookupListCount] <optimize = false>;
} FeatureTable <optimize = false>;
FSeek(next);
};
string tFeatureRecordRead (tFeatureRecord &d ) {
return d.FeatureTag;
}
typedef struct tFeatureList {
USHORT Offset; //Offset to FeatureList table-from beginning of GSUB table
local quad next = FTell();
FSeek(GSUBorGPOS_table + Offset);
local quad FeatureList=FTell();
uint16 FeatureCount; //Number of FeatureRecords
tFeatureRecord FeatureRecord[FeatureCount] <read= tFeatureRecordRead, optimize = false>;
//Array of FeatureRecords -listed alphabetically by FeatureTag
FSeek(next);
};
string tFeatureListRead (tFeatureList &d ) {
string s;
SPrintf( s, "%u Features", d.FeatureCount);
return s;
}
typedef struct tLookupRecord {
USHORT Offset; //Offset to LookupList table-from beginning of GSUB table
local quad next = FTell();
FSeek(LookupList_table + Offset);
uint16 LookupType; //Different enumerations for GSUB and GPOS
uint16 LookupFlag; //Lookup qualifiers
uint16 SubTableCount; //Number of SubTables for this lookup
USHORT SubTable[SubTableCount]; //Array of offsets to SubTables-from beginning of Lookup table
FSeek(next);
};
typedef struct tLookupList {
USHORT Offset; //Offset to LookupList table-from beginning of GSUB table
local quad next = FTell();
FSeek(GSUBorGPOS_table + Offset);
local quad LookupList_table=FTell();
uint16 LookupCount; //Number of FeatureRecords
tLookupRecord LookupRecord[LookupCount] <optimize = false>;
//Array of LookupRecords -listed alphabetically by LookupTag
FSeek(next);
};
typedef struct tGSUBorGPOS {
local quad GSUBorGPOS_table = FTell();
TT_Fixed Version; //Version of the GSUB table-initially set to 0x00010000
tScriptList ScriptList <read=tScriptListRead>; //Offset to ScriptList table-from beginning of GSUB table
tFeatureList FeatureList <read=tFeatureListRead>; //Offset to FeatureList table-from beginning of GSUB table
tLookupList LookupList; //Offset to LookupList table-from beginning of GSUB table
//LookupType Enumeration table for glyph substitution
//Value Type Description
//1 LookupType_Single Replace one glyph with one glyph
//2 LookupType_Multiple Replace one glyph with more than one glyph
//3 LookupType_Alternate Replace one glyph with one of many glyphs
//4 LookupType_Ligature Replace multiple glyphs with one glyph
//5 LookupType_Context Replace one or more glyphs in context
//6 LookupType_Chaining Context Replace one or more glyphs in chained context
//7 LookupType_Extension Substitution Extension mechanism for other substitutions (i.e. this excludes the Extension type substitution itself)
//8 LookupType_Reverse chaining context single Applied in reverse order, replace single glyph in chaining context
//9+ Reserved For future use
};
string tGSUBorGPOSRead( tGSUBorGPOS &d ) {
string s;
SPrintf( s, "v%.2f", d.Version/65535.0);
return s;
}
//==================================
struct tOffsetTable OffsetTable;
struct tTable Table[OffsetTable.numTables] <read=tTableRead>;
local int currentTable; //set by findTable to index Table[] array.
int findTable( char tag[] ) {
//search the Table[] array for a matching character table tag
// set currentTable and FSeek Table[currentTable].offset
// return the table index+1 or 0 if no table found.
local int i=0;
for( i = 0; i < OffsetTable.numTables; i++ ) {
if ( Strncmp(Table[i].Tag.asChar, tag, 4)==0 ) {
currentTable = i;
FSeek(Table[i].offset);
return i+1;
}
}
return 0;
}
if (findTable("head")) {struct thead head;};
if (findTable("hdmx")) {struct thdmx hdmx <read=thdmxRead>; };
if (findTable("hhea")) {struct thhea hhea <read=thheaRead>; };
if (findTable("hmtx")) {struct thmtx hmtx <read=thmtxRead>; };
if (findTable("maxp")) {struct tmaxp maxp <read=tmaxpRead>; };
if (findTable("name")) {struct tname name <read=tnameRead>; };
if (findTable("post")) {struct tpost post <read=tpostRead>; };
//bytecode programs
if (findTable("cvt ")) {struct tcvt cvt ; };
if (findTable("fpgm")) {struct tfpgm fpgm; };
if (findTable("prep")) {struct tprep prep;};
if (findTable("GDEF")) {struct tGDEF GDEF; };
if (findTable("OS/2")) {struct tOS_2 OS_2 <read=tOS_2Read>; };
if (findTable("cmap")) {struct tcmap cmap; };
if (findTable("loca")) {struct tloca loca <read=tlocaRead>; };
if (findTable("glyf")) {struct tglyf glyf; };
if (findTable("DSIG")) {struct tDSIG DSIG <read=tDSIGRead>; };
if (findTable("GSUB")) {struct tGSUBorGPOS GSUB <read=tGSUBorGPOSRead>; };
if (findTable("GPOS")) {struct tGSUBORGPOS GPOS <read=tGSUBorGPOSRead>; };
//if (findTable("JSTF")) {struct tJSTF JSTF <read=tJSTFRead>};
//if (findTable("LTSH")) {struct tLTSH LTSH <read=tLTSHRead>};
//if (findTable("PCLT")) {struct tPCLT PCLT <read=tPCLTRead>};
//if (findTable("VDMX")) {struct tVDMX VDMX <read=tVDMXRead>};
//if (findTable("gasp")) {struct tgasp gasp <read=tgaspRead>};
//if (findTable("kern")) {struct tkern kern <read=tkernRead>};