-
Notifications
You must be signed in to change notification settings - Fork 14
/
opentype.fathom
3301 lines (3032 loc) · 136 KB
/
opentype.fathom
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
//! # OpenType Font File Format
//~ example-data = [
//~ "data/opentype/aots/*.otf",
//~ "data/opentype/woff/*.ttf",
//~ "data/opentype/woff2/*.ttf",
//~ ]
// -----------------------------------------------------------------------------
// # OpenType Top Level Organization
//
// Formats that form the top-level of an OpenType font file.
//
// ## References
//
// - [Microsoft's OpenType Spec: Organization of an OpenType Font](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font)
// - [Apple's TrueType Reference Manual: TrueType Font files](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Overview)
/// # OpenType file
///
/// The main entrypoint of an OpenType font file.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Organization of an OpenType Font](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font)
/// - [Apple's TrueType Reference Manual: TrueType Font files](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Overview)
def main = {
/// The start of the font file.
start <- stream_pos,
/// The directory of tables in the font.
font <- overlap {
magic <- u32be,
directory <- match magic {
// TrueType font
0x00010000 => table_directory start,
// CFF font
"OTTO" => table_directory start,
// OpenType Font Collection
"ttcf" => ttc_header start,
_ => unknown_table,
},
},
};
/// # Table Directory
///
/// A directory of the top-level tables in the font.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Table Directory](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#table-directory)
/// - [Apple's TrueType Reference Manual: The Font Directory](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Directory)
def table_directory (file_start : Pos) = {
/// Version of the font.
///
/// | Value | Meaning |
/// | ------------- | ----------------------------------------- |
/// | `0x00010000` | for fonts containing TrueType outlines |
/// | `0x4F54544F` | (`'OTTO'`) for fonts containing CFF data |
///
/// Apple allows 'true' and 'typ1', but this should not be found in OpenType files.
sfnt_version <- u32be where
bool_or (sfnt_version == (0x00010000 : U32)) (sfnt_version == ("OTTO" : U32)),
/// Number of tables in the directory.
num_tables <- u16be,
/// For enabling quick binary searches.
search_range <- u16be, // TODO: (Maximum power of 2 <= num_tables) x 16
/// For enabling quick binary searches.
entry_selector <- u16be, // TODO: Log2(maximum power of 2 <= num_tables)
/// For enabling quick binary searches.
range_shift <- u16be, // TODO: NumTables x 16-searchRange
/// An array of table records
// FIXME: sorted in ascending order by tag
table_records <- repeat_len16 num_tables table_record,
/// Font table links
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Font Tables](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#font-tables)
/// - [Apple's TrueType Reference Manual: TrueType Font files](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Overview)
table_links <- (
let required_table =
fun (table_id : tag) =>
fun (table_format : Format) => {
// TODO: let formats
table_record <- unwrap (find_table table_records table_id),
link <- link_table file_start table_record table_format,
};
let required_table_with_len =
fun (table_id : tag) =>
fun (table_format : (U32 -> Format)) => {
// TODO: let formats
table_record <- unwrap (find_table table_records table_id),
link <- link_table file_start table_record (table_format table_record.length),
};
let optional_table =
fun (table_id : tag) =>
fun (table_format : Format) =>
option_fold ({} : Format)
(fun record => link_table file_start record table_format)
(find_table table_records table_id);
{
// Required Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#required-tables
cmap <- required_table "cmap" cmap_table,
head <- required_table "head" head_table,
hhea <- required_table "hhea" hhea_table,
maxp <- required_table "maxp" maxp_table,
htmx <- required_table "hmtx" {
// TODO: let formats
hhea <- deref hhea.link,
maxp <- deref maxp.link,
table <- htmx_table
hhea.number_of_long_horizontal_metrics
maxp.num_glyphs,
},
name <- required_table "name" name_table,
os2 <- required_table_with_len "OS/2" os2_table,
post <- required_table "post" post_table,
// TrueType Outline Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#tables-related-to-truetype-outlines
cvt <- optional_table "cvt " unknown_table,
fpgm <- optional_table "fpgm" unknown_table,
glyf <- optional_table "glyf" {
// TODO: let formats
maxp <- deref maxp.link,
table <- glyf_table 1,
// TODO: use `loca` entries when parsing the glyphs
// table <- glyf_table maxp.num_glyphs,
},
loca <- optional_table "loca" {
// TODO: let formats
maxp <- deref maxp.link,
head <- deref head.link,
table <- loca_table maxp.num_glyphs head.index_to_loc_format,
},
prep <- optional_table "prep" unknown_table,
gasp <- optional_table "gasp" unknown_table,
// CFF Outline Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#tables-related-to-cff-outlines
cff <- optional_table "CFF " unknown_table,
cff2 <- optional_table "CFF2" unknown_table,
vorg <- optional_table "VORG" unknown_table,
// SVG Outline Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#table-related-to-svg-outlines
svg <- optional_table "SVG " unknown_table,
// Bitmap Glyph Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#tables-related-to-bitmap-glyphs
ebdt <- optional_table "EBDT" unknown_table,
eblc <- optional_table "EBLC" unknown_table,
ebsc <- optional_table "EBSC" unknown_table,
// Color Bitmap Glyph Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#tables-related-to-bitmap-glyphs
cbdt <- optional_table "CBDT" unknown_table,
cblc <- optional_table "CBLC" unknown_table,
sbix <- optional_table "sbix" unknown_table,
// Advanced Typographic Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#advanced-typographic-tables
base <- optional_table "BASE" base_table,
gdef <- optional_table "GDEF" gdef_table,
gpos <- optional_table "GPOS" gpos_table,
gsub <- optional_table "GSUB" gsub_table,
jstf <- optional_table "JSTF" jstf_table,
math <- optional_table "MATH" math_table,
// OpenType Font Variation Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#tables-used-for-opentype-font-variations
avar <- optional_table "avar" unknown_table,
cvar <- optional_table "cvar" unknown_table,
fvar <- optional_table "fvar" unknown_table,
gvar <- optional_table "gvar" unknown_table,
hvar <- optional_table "HVAR" unknown_table,
mvar <- optional_table "MVAR" unknown_table,
stat <- optional_table "STAT" unknown_table,
vvar <- optional_table "VVAR" unknown_table,
// Color Font Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#tables-related-to-color-fonts
colr <- optional_table "COLR" unknown_table,
cpal <- optional_table "CPAL" unknown_table,
// Other OpenType Tables
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/otff#other-opentype-tables
dsig <- optional_table "DSIG" unknown_table,
hdmx <- optional_table "hdmx" unknown_table,
kern <- optional_table "kern" unknown_table,
ltsh <- optional_table "LTSH" unknown_table,
merg <- optional_table "MERG" unknown_table,
meta <- optional_table "meta" unknown_table,
pclt <- optional_table "PCLT" unknown_table,
vdmx <- optional_table "VDMX" unknown_table,
vhea <- optional_table "vhea" unknown_table,
vmtx <- optional_table "vmtx" unknown_table,
}
),
};
/// # TTC Header (OpenType Font Collection)
///
/// ## References
///
/// - [Microsoft's OpenType Spec: TTC Header](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#ttc-header)
def ttc_header (start : Pos) = (
/// TTC Header Version 1.0
let ttc_header1 = fun (start : Pos) => {
/// Number of fonts in TTC
num_fonts <- u32be,
/// Array of offsets to the TableDirectory for each font from the beginning of the file
table_directories <- repeat_len32 num_fonts (offset32 start (table_directory start)),
};
/// TTC Header Version 2.0
let ttc_header2 = fun (start : Pos) => {
/// Number of fonts in TTC
num_fonts <- u32be,
/// Array of offsets to the TableDirectory for each font from the beginning of the file
table_directories <- repeat_len32 num_fonts (offset32 start (table_directory start)),
/// Tag indicating that a DSIG table exists, 0x44534947 ('DSIG') (null if no signature)
dsig_tag <- u32be,
/// The length (in bytes) of the DSIG table (null if no signature)
dsig_length <- u32be,
/// The offset (in bytes) of the DSIG table from the beginning of the TTC file (null if no
/// signature)
dsig_offset <- u32be,
};
{
/// Font Collection ID string: 'ttcf' (used for fonts with CFF or CFF2 outlines as well as
/// TrueType outlines)
ttc_tag <- tag,
/// Major version of the TTC Header
major_version <- u16be,
/// Minor version of the TTC Header
minor_version <- u16be,
/// Version specific fields
header <- match major_version {
1 => ttc_header1 start,
2 => ttc_header2 start,
_ => unknown_table,
},
}
);
/// # Table Record
///
/// A record that stores an offset to another table in the font file.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Table Directory](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#table-directory)
/// - [Apple's TrueType Reference Manual: The Font Directory](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Directory)
def table_record = {
/// Table identifier.
table_id <- tag,
/// CheckSum for this table.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Calculating Checksums](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#calculating-checksums)
checksum <- u32be,
/// Offset from the beginning of the TrueType font file.
offset <- u32be,
/// Length of this table.
length <- u32be,
};
/// Find a table record using the given `table_id`.
def find_table =
fun (@num_tables : U16) =>
fun (table_records : Array16 num_tables (Repr table_record)) =>
fun (table_id : Repr tag) =>
// TODO: accelerate using binary search
// TODO: make use of `table_record.search_range`
// TODO: make use of `table_record.entry_selector`
// TODO: make use of `table_record.range_shift`
array16_find
(fun (table_record : Repr table_record) => table_record.table_id == table_id)
table_records;
/// Create a link to the given `table_format`.
def link_table =
fun (file_start : Pos) =>
fun (table_record : table_record) =>
fun (table_format : Format) =>
// TODO: make use of `table_record.checksum`
link (pos_add_u32 file_start table_record.offset)
(limit32 table_record.length table_format);
// -----------------------------------------------------------------------------
/// Reserved formats
def reserved (format : Format) (default : format) =
format; // TODO: set to `default` during serialisation
/// Deprecated formats
def deprecated (format : Format) (default : format) =
format; // TODO: set to `default` during serialisation
// -----------------------------------------------------------------------------
// # Common Formats
//
// Common formats to be used in the OpenType specification.
//
// ## References
//
// - [Microsoft's OpenType Spec: Data Types](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#data-types)
// - [Apple's TrueType Reference Manual: Data Types](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6.html#Types)
// TODO: move to separate module
/// Signed 32-bit fixed-point number (16.16)
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Fixed](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_Fixed)
def fixed : Format = u32be;
/// Signed, 16-bit integer that describes a quantity in font design units.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: FWORD](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_FWORD)
def fword : Format = s16be;
/// Unsigned, 16-bit integer that describes a quantity in font design units.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: UFWORD](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_UFWORD)
def ufword : Format = u16be;
/// Signed 16-bit fixed number with the low 14 bits of fraction (2.14).
///
/// ## References
///
/// - [Microsoft's OpenType Spec: F2DOT14](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_F2DOT14)
def f2dot14 : Format = s16be;
/// Unsigned 24-bit integer
///
/// ## References
///
/// - [Microsoft's OpenType Spec: uint24](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_uint24)
def u24be : Format = repeat_len8 3 u8;
/// Date represented in number of seconds since 12:00 midnight, January 1, 1904.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: LONGDATETIME](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_LONGDATETIME)
def long_date_time : Format = s64be;
/// Array of four `U8`s used to identify a table, design-variation axis, script,
/// language system, feature, or baseline.
///
/// The elements of the array are expected to be in the range [0x20, 0x7E].
/// This corresponds to the range of printable ASCII characters.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Tag](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_Tag)
def tag : Format =
// TODO: constrain array elements to the range 0x20 to 0x7E.
// TODO: pattern matching on arrays
// repeat_len8 4 u8;
u32be;
/// # Unknown table format
///
/// This is a placeholder for a table that has an unknown identifier (due to the
/// file conforming to a newer version of the specification), or for a table has
/// not yet been implemented.
def unknown_table : Format = {};
/// A format that consumes no input.
def empty : Format = {};
/// 16-bit offset to a `format`, relative to some `base` position.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Offset16](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_Offset16)
def offset16 (base : Pos) (format : Format) = {
offset <- u16be,
link <- match offset {
0 => empty,
_ => link (pos_add_u16 base offset) format, // TODO: Use an option type?
},
};
/// 32-bit offset to a `format`, relative to some `base` position.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Offset32](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_Offset32)
def offset32 (base : Pos) (format : Format) = {
offset <- u32be,
link <- match offset {
0 => empty,
_ => link (pos_add_u32 base offset) format, // TODO: Use an option type?
},
};
/// Packed 32-bit value with major and minor version numbers.
///
/// Used only in the 'maxp', 'post' and 'vhea' tables, for backward
/// compatibility reasons.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Version16Dot16](https://docs.microsoft.com/en-us/typography/opentype/spec/otff#dt_Version16Dot16)
def version16dot16 = u32be;
// -----------------------------------------------------------------------------
/// # Platform identifiers
///
/// | Value | Meaning |
/// | ------------- | --------------------------------- |
/// | `0` | Unicode |
/// | `1` | Macintosh |
/// | `2` | ISO (deprecated in OpenType v1.3) |
/// | `3` | Windows |
/// | `4` | Custom |
/// | `5..<240` | Reserved |
/// | `240..<256` | User-defined |
///
/// Value `1` (Macintosh) is discouraged on current platforms – prefer a value
/// of `3` (Windows) for maximum compatibility.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Platform IDs](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#platform-ids)
/// - [Microsoft's OpenType Spec: Platform, encoding and language](https://docs.microsoft.com/en-us/typography/opentype/spec/name#platform-encoding-and-language)
/// - [Apple's TrueType Reference Manual: The `'cmap'` encoding subtables](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html)
/// - [Apple's TrueType Reference Manual: The platform identifier](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6name.html)
def platform_id =
u16be;
/// # Platform-specific encoding identifiers
///
// TODO: document encoding IDs
def encoding_id (platform : platform_id) =
u16be;
/// # Language identifiers
///
/// This must be set to `0` for all subtables that have a platform ID other than
/// ‘Macintosh’.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Use of the language field in 'cmap' subtables](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#use-of-the-language-field-in-cmap-subtables)
/// - [Apple's TrueType Reference Manual: The `'cmap'` table and language codes](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html)
///
// TODO: add more details to docs
def language_id =
u16be;
def language_id32 =
u32be;
// # Common Table Formats
/// # Class Definition Table
///
/// | Class | Description |
/// |-------|-----------------------------------------------------------|
/// | 1 | Base glyph (single character, spacing glyph) |
/// | 2 | Ligature glyph (multiple character, spacing glyph) |
/// | 3 | Mark glyph (non-spacing combining glyph) |
/// | 4 | Component glyph (part of single character, spacing glyph) |
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Class Definition Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#class-definition-table)
/// # Class Definition Table Format 1
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Class Definition Table Format 1](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#class-definition-table-format-1)
def class_def_format_1 = {
/// First glyph ID of the class_value_array
start_glyph_id <- u16be,
/// Size of the class_value_array
glyph_count <- u16be,
/// Array of Class Values — one per glyph ID
class_value_array <- repeat_len16 glyph_count u16be,
};
/// # Class Definition Table Format 2
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Class Definition Table Format 2](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#class-definition-table-format-2)
def class_def_format_2 = (
/// ClassRangeRecord
let class_range_record = {
/// First glyph ID in the range
start_glyph_id <- u16be,
/// Last glyph ID in the range
end_glyph_id <- u16be,
/// Applied to all glyphs in the range
class <- u16be,
};
{
/// Number of ClassRangeRecords
class_range_count <- u16be,
/// Array of ClassRangeRecords — ordered by startGlyphID
class_range_records <- repeat_len16 class_range_count class_range_record,
}
);
/// # Class Definition Table
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Class Definition Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#class-definition-table)
def class_def = {
/// Format identifier
class_format <- u16be,
/// Format specific data
data <- match class_format {
1 => class_def_format_1,
2 => class_def_format_2,
_ => unknown_table,
},
};
/// # Coverage Format 1
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Coverage Format 1](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#coverage-format-1)
def coverage_format_1 = {
/// Number of glyphs in the glyph array
glyph_count <- u16be,
/// Array of glyph IDs — in numerical order
glyph_array <- repeat_len16 glyph_count u16be,
};
/// # Coverage Format 2
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Coverage Format 2](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#coverage-format-2)
def coverage_format_2 = (
/// RangeRecord
let range_record = {
/// First glyph ID in the range
start_glyph_id <- u16be,
/// Last glyph ID in the range
end_glyph_id <- u16be,
/// Coverage Index of first glyph ID in range
start_coverage_index <- u16be,
};
{
/// Number of RangeRecords
range_count <- u16be,
/// Array of glyph ranges — ordered by startGlyphID
range_records <- repeat_len16 range_count range_record,
}
);
/// # Coverage Table
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Coverage Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#coverageTbl)
def coverage_table = {
/// Format identifier
coverage_format <- u16be,
/// Format specific data
data <- match coverage_format {
1 => coverage_format_1,
2 => coverage_format_2,
_ => unknown_table,
}
};
/// # Sequence Lookup Record
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Sequence Lookup Record](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#sequence-lookup-record)
def sequence_lookup_record = {
/// Index (zero-based) into the input glyph sequence
sequence_index <- u16be,
/// Index (zero-based) into the LookupList
lookup_list_index <- u16be,
};
/// # Device and VariationIndex Tables
///
/// Device tables and VariationIndex tables are used to provide adjustments to font-unit values in
/// GPOS, JSTF, GDEF or BASE tables, such as the X and Y coordinates of an attachment anchor
/// position.
///
/// Curiously the table has two interpretations. The second interprtation appears to be have been
/// tacked on for variable fonts. The gist being that if the delta format is 0x8000 then the table
/// is a VariationIndex table, which names the fields differently and does not contain a delta
/// value array. E.g.
///
/// let variation_index_table = {
/// /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
/// delta_set_outer_index <- u16be,
/// /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
/// delta_set_inner_index <- u16be,
/// /// Format, = 0x8000
/// delta_format <- u16be,
/// };
///
/// We only define `device_table` and have it conditionally read the delta value array.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Device and VariationIndex Tables](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#device-and-variationindex-tables)
///
def device_table = (
// quotient = numerator / denominator # int division
// if quotient * denominator < numerator:
// quotient + 1
// else:
// quotient
let u16_div_ceil = fun (numerator : U16) (denominator : U16) => (
let quotient = u16_div numerator denominator;
match ((u16_mul quotient denominator) < numerator) {
true => u16_add quotient 1,
false => quotient,
}
);
let delta_bits = fun (delta_format : U16) (num_sizes : U16) =>
match delta_format {
// Signed 2-bit value, 8 values per u16be
0x0001 => u16_mul num_sizes 2,
// Signed 4-bit value, 4 values per u16be
0x0002 => u16_mul num_sizes 4,
// Signed 8-bit value, 2 values per u16be
0x0003 => u16_mul num_sizes 8,
// Unreachable due to match done in device_or_variation_index_table
_ => 0,
};
let num_sizes = fun (start : U16) (end : U16) =>
u16_add (u16_sub end start) 1;
{
/// Smallest size to correct, in ppem
start_size <- u16be,
/// Largest size to correct, in ppem
end_size <- u16be,
/// Format of deltaValue array data
delta_format <- u16be,
/// Array of compressed data
delta_values <-
let delta_bits = delta_bits delta_format (num_sizes start_size end_size);
repeat_len16 (u16_div_ceil delta_bits 16) u16be,
}
);
/// VariationIndex table
def variation_index_table = {
/// A delta-set outer index — used to select an item variation data subtable within the item
/// variation store.
delta_set_outer_index <- u16be,
/// A delta-set inner index — used to select a delta-set row within an item variation data
/// subtable.
delta_set_inner_index <- u16be,
};
def device_or_variation_index_table = overlap {
// Initial pass to figure out the table format
init <- {
_skipped <- repeat_len8 4 u8,
table_format <- u16be,
},
// Device and VariationIndex Tables
table <- match init.table_format {
0x0001 => device_table,
0x0002 => device_table,
0x0003 => device_table,
0x8000 => variation_index_table,
_ => unknown_table,
},
};
/// # Language System Table
///
/// Also known as LangSys table.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Language System Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#language-system-table)
def lang_sys = {
/// = NULL (reserved for an offset to a reordering table)
lookup_order_offset <- u16be,
/// Index of a feature required for this language system; if no required features = 0xFFFF
required_feature_index <- u16be,
/// Number of feature index values for this language system — excludes the required feature
feature_index_count <- u16be,
/// Array of indices into the FeatureList, in arbitrary order
feature_indices <- repeat_len16 feature_index_count u16be,
};
/// # Language System Record
///
/// Also known as LangSys record.
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Script Table and Language System Record](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-table-and-language-system-record)
def lang_sys_record (script_start : Pos) = {
/// 4-byte LangSysTag identifier
lang_sys_tag <- tag,
/// Offset to LangSys table, from beginning of Script table
lang_sys <- offset16 script_start lang_sys,
};
/// # Script table
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Script Table and Language System Record](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-table-and-language-system-record)
def script_table = {
/// The start of the script table
table_start <- stream_pos,
/// Offset to default LangSys table, from beginning of Script table — may be NULL
default_lang_sys <- offset16 table_start lang_sys,
/// Number of LangSysRecords for this script — excluding the default LangSys
lang_sys_count <- u16be,
/// Array of LangSysRecords, listed alphabetically by LangSys tag
lang_sys_records <- repeat_len16 lang_sys_count (lang_sys_record table_start),
};
/// # Script list table
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Script List Table and Script Record](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-list-table-and-script-record)
def script_list = (
/// ScriptRecord
let script_record = fun (script_list_start : Pos) => {
/// 4-byte script tag identifier
script_tag <- tag,
/// Offset to Script table, from beginning of ScriptList
script <- offset16 script_list_start script_table,
};
{
/// The start of the script list table
table_start <- stream_pos,
/// Number of ScriptRecords
script_count <- u16be,
/// Array of ScriptRecords, listed alphabetically by script tag
script_records <- repeat_len16 script_count (script_record table_start),
}
);
/// # Feature Table
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Feature Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#feature-table)
def feature_table = {
/// The start of the feature table
table_start <- stream_pos,
/// Offset from start of Feature table to FeatureParams table, if defined for the feature and
/// present, else NULL
feature_params <- u16be, // TODO: The format of the params table depends on the feature tag
/// Number of LookupList indices for this feature
lookup_index_count <- u16be,
/// Array of indices into the LookupList — zero-based (first lookup is LookupListIndex = 0)
lookup_list_indices <- repeat_len16 lookup_index_count u16be,
};
/// # Feature List Table
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Feature List Table](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#feature-list-table)
def feature_list = (
let feature_record = fun (feature_list_start : Pos) => {
/// 4-byte feature identification tag
feature_tag <- tag,
/// Offset to Feature table, from beginning of FeatureList
feature <- offset16 feature_list_start feature_table,
};
{
/// The start of the feature list table
table_start <- stream_pos,
/// Number of FeatureRecords in this table
feature_count <- u16be,
/// Array of FeatureRecords — zero-based (first feature has FeatureIndex = 0), listed
/// alphabetically by feature tag
feature_records <- repeat_len16 feature_count (feature_record table_start),
}
);
/// # Sequence Context Format 1: simple glyph contexts
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Sequence Context Format 1](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#sequence-context-format-1-simple-glyph-contexts)
def sequence_context_format1 = (
/// SequenceRule table
let sequence_rule = {
/// Number of glyphs in the input glyph sequence
glyph_count <- u16be,
/// Number of SequenceLookupRecords
seq_lookup_count <- u16be,
/// Array of input glyph IDs—starting with the second glyph
input_sequence <- repeat_len16 (u16_sub glyph_count 1) u16be,
/// Array of Sequence lookup records
seq_lookup_records <- repeat_len16 seq_lookup_count sequence_lookup_record,
};
/// SequenceRuleSet table—all contexts beginning with the same glyph
let sequence_rule_set = {
/// The start of the table
table_start <- stream_pos,
/// Number of SequenceRule tables
seq_rule_count <- u16be,
/// Array of offsets to SequenceRule tables, from beginning of the SequenceRuleSet table
seq_rules <- repeat_len16 seq_rule_count (offset16 table_start sequence_rule),
};
{
/// The start of the table
table_start <- stream_pos,
/// Offset to Coverage table, from beginning of SequenceContextFormat1 table
coverage <- offset16 table_start coverage_table,
/// Number of SequenceRuleSet tables
seq_rule_set_count <- u16be,
/// Array of offsets to SequenceRuleSet tables, from beginning of
/// SequenceContextFormat1 table (offsets may be NULL)
seq_rule_sets <- repeat_len16 seq_rule_set_count (offset16 table_start sequence_rule_set),
}
);
/// # Sequence Context Format 2: class-based glyph contexts
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Sequence Context Format 2](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#sequence-context-format-2-class-based-glyph-contexts)
def sequence_context_format2 = (
/// ClassSequenceRule table
let class_sequence_rule = {
/// Number of glyphs to be matched
glyph_count <- u16be,
/// Number of SequenceLookupRecords
seq_lookup_count <- u16be,
/// Sequence of classes to be matched to the input glyph sequence, beginning with the
/// second glyph position
input_sequence <- repeat_len16 (u16_sub glyph_count 1) u16be,
/// Array of SequenceLookupRecords
seqLookupRecords <- repeat_len16 seq_lookup_count sequence_lookup_record,
};
/// ClassSequenceRuleSet table
let class_sequence_rule_set = {
/// The start of the table
table_start <- stream_pos,
/// Number of ClassSequenceRule tables
class_seq_rule_count <- u16be,
/// Array of offsets to ClassSequenceRule tables, from beginning of ClassSequenceRuleSet
/// table
class_seq_rules <- repeat_len16 class_seq_rule_count (offset16 table_start class_sequence_rule),
};
{
/// The start of the table
table_start <- stream_pos,
/// Offset to Coverage table, from beginning of SequenceContextFormat1 table
coverage <- offset16 table_start coverage_table,
/// Offset to ClassDef table, from beginning of SequenceContextFormat2 table
class_def <- (offset16 table_start class_def),
/// Number of ClassSequenceRuleSet tables
class_seq_rule_set_count <- u16be,
/// Array of offsets to ClassSequenceRuleSet tables, from beginning of
/// SequenceContextFormat2 table (may be NULL)
class_seq_rule_sets <- repeat_len16 class_seq_rule_set_count (offset16 table_start class_sequence_rule_set),
}
);
/// # Sequence Context Format 3: coverage-based glyph contexts
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Sequence Context Format 3](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#sequence-context-format-3-coverage-based-glyph-contexts)
def sequence_context_format3 = {
/// The start of the table
table_start <- stream_pos,
/// Number of glyphs in the input sequence
glyph_count <- u16be,
/// Number of SequenceLookupRecords
seq_lookup_count <- u16be,
/// Array of offsets to Coverage tables, from beginning of SequenceContextFormat3 subtable
coverage_tables <- repeat_len16 glyph_count (offset16 table_start coverage_table),
/// Array of SequenceLookupRecords
seq_lookup_records <- repeat_len16 seq_lookup_count sequence_lookup_record,
};
def sequence_context = {
/// Format identifier
format <- u16be,
/// Format specific substitutions
subst <- match format {
1 => sequence_context_format1,
2 => sequence_context_format2,
3 => sequence_context_format3,
_ => unknown_table,
}
};
/// # Chained Sequence Context Format 1: simple glyph contexts
///
/// ## References
///
/// - [Microsoft's OpenType Spec: Chained Sequence Context Format 1](https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#chained-sequence-context-format-1-simple-glyph-contexts)
def chained_sequence_context_format_1 = (
let chained_sequence_rule = {
/// Number of glyphs in the backtrack sequence
backtrack_glyph_count <- u16be,
/// Array of backtrack glyph IDs
backtrack_sequence <- repeat_len16 backtrack_glyph_count u16be,
/// Number of glyphs in the input sequence
input_glyph_count <- u16be,
/// Array of input glyph IDs—start with second glyph
input_sequence <- repeat_len16 (u16_sub input_glyph_count 1) u16be,
/// Number of glyphs in the lookahead sequence
lookahead_glyph_count <- u16be,
/// Array of lookahead glyph IDs
lookahead_sequence <- repeat_len16 lookahead_glyph_count u16be,
/// Number of SequenceLookupRecords
seq_lookup_count <- u16be,
/// Array of SequenceLookupRecords
seq_lookup_records <- repeat_len16 seq_lookup_count sequence_lookup_record,
};
let chained_sequence_rule_set = {
/// The start of the table
table_start <- stream_pos,
/// Number of ChainedSequenceRule tables
chained_seq_rule_count <- u16be,
/// Array of offsets to ChainedSequenceRule tables, from beginning of
/// ChainedSequenceRuleSet table
chained_seq_rules <- repeat_len16 chained_seq_rule_count (offset16 table_start chained_sequence_rule),
};
{
/// The start of the table
table_start <- stream_pos,
/// Offset to Coverage table, from beginning of ChainSequenceContextFormat1 table
coverage <- (offset16 table_start coverage_table),
/// Number of ChainedSequenceRuleSet tables
chained_seq_rule_set_count <- u16be,
/// Array of offsets to ChainedSeqRuleSet tables, from beginning of
/// ChainedSequenceContextFormat1 table (may be NULL)
chained_seq_rule_sets <- repeat_len16 chained_seq_rule_set_count (offset16 table_start chained_sequence_rule_set),
}
);
/// # Chained Sequence Context Format 2: class-based glyph contexts
///
/// ## References
///