font_test_data/
ift.rs

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
//! Test data for the IFT table
//!
//! Used for incremental font transfer. Specification:
//! <https://w3c.github.io/IFT/Overview.html>

use read_fonts::types::Tag;
use read_fonts::{be_buffer, be_buffer_add, test_helpers::BeBuffer, types::Int24, types::Uint24};
use write_fonts::{
    tables::{head::Head, loca::Loca, maxp::Maxp},
    FontBuilder,
};

pub static IFT_BASE: &[u8] = include_bytes!("../test_data/ttf/ift_base.ttf");

// Format specification: https://w3c.github.io/IFT/Overview.html#patch-map-format-1
pub fn simple_format1() -> BeBuffer {
    let mut buffer = be_buffer! {
        /* ### Header ### */
        1u8,                    // format
        0u32,                   // reserved
        [1u32, 2, 3, 4],        // compat id
        2u16,                   // max entry id
        {2u16: "max_glyph_map_entry_id"},
        (Uint24::new(7)),       // glyph count
        {0u32: "glyph_map_offset"},
        0u32,                   // feature map offset
        0b00000010u8,           // applied entry bitmap (entry 1)

        8u16,                   // uri template length
        {b'A': "uri_template[0]"},
        {b'B': "uri_template[1]"},
        [b'C', b'D', b'E', b'F', 0xc9, 0xa4], // uri_template[2..7]

        {3u8: "patch_encoding"}, // = glyph keyed

        /* ### Glyph Map ### */
        {1u16: "glyph_map"},     // first mapped glyph
        {2u8: "entry_index[1]"},
        [1u8, 0, 1, 0, 0]        // entry index[2..6]
    };

    let offset = buffer.offset_for("glyph_map") as u32;
    buffer.write_at("glyph_map_offset", offset);

    buffer
}

pub fn u16_entries_format1() -> BeBuffer {
    let mut buffer = be_buffer! {
      1u8,              // format
      0u32,             // reserved
      [1, 2, 3, 4u32],  // compat id

      300u16,           // max entry id
      300u16,           // max glyph map entry id

      (Uint24::new(7)), // glyph count

      {0u32: "glyph_map_offset"},
      {0u32: "feature_map_offset"},

      // applied entry bitmap (38 bytes)
      [
        0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0,
        0, 0u8
      ],

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      3u8,                 // patch encoding = glyph keyed

      /* ### Glyph Map ### */
      {2u16: "glyph_map"}, // first mapped glyph

      // entryIndex[2..6]
      [80, 81, 300, 300, 80u16]
    };

    let offset = buffer.offset_for("glyph_map") as u32;
    buffer.write_at("glyph_map_offset", offset);

    buffer
}

pub fn feature_map_format1() -> BeBuffer {
    let mut buffer = be_buffer! {
      /* ### Header ### */
      1u8,                    // format

      0u32,  // reserved

      [1u32, 2u32, 3u32, 4u32], // compat id

      400u16, // max entry id
      300u16, // max glyph map entry id
      (Uint24::new(7)), // glyph count

      {0u32: "glyph_map_offset"},
      {0u32: "feature_map_offset"},

      // applied entry bitmap (51 bytes) - 300 is applied
      [
        0, 0, 0, 0, 0, 0, 0, 0,           // [0, 64)
        0, 0, 0, 0, 0, 0, 0, 0,           // [64, 128)
        0, 0, 0, 0, 0, 0, 0, 0,           // [128, 192)
        0, 0, 0, 0, 0, 0, 0, 0,           // [192, 256)
        0, 0, 0, 0, 0, 0b00001000, 0, 0,  // [256, 320)
        0, 0, 0, 0, 0, 0, 0, 0,           // [320, 384)
        0, 0, 0u8                         // [384, 400)
      ],

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      3u8,                    // patch encoding = glyph keyed

      /* ### Glyph Map ### */
      {2u16: "glyph_map"}, // first mapped glyph

      // entryIndex[2..6]
      [
        80,  // gid 2
        81,  // gid 3
        300, // gid 4
        299, // gid 5
        80u16   // gid 6
      ],

      // ## Feature Map ##
      {3u16: "feature_map"}, // feature count

      // FeatureRecord[0]
      {(Tag::new(b"dlig")): "FeatureRecord[0]"}, // feature tag
      400u16,                   // first new entry index
      1u16,                     // entry map count

      // FeatureRecord[1]
      {(Tag::new(b"liga")): "FeatureRecord[1]"}, // feature tag
      384u16,                   // first new entry index
      2u16,                     // entry map count

      // FeatureRecord[2]
      [b'n', b'u', b'l', b'l'], // feature tag
      301u16,                   // first new entry index
      1u16,                     // entry map count

      // EntryMapRecord[0]: "dlig" + entry 81 => entry 400
      81u16,                    // first_entry_index
      81u16,                    // last_entry_index

      // EntryMapRecord[1]: "liga" + entry [80, 81] => entry 384
      80u16,                    // first_entry_index
      81u16,                    // last_entry_index

      // EntryMapRecord[2]: "liga" + entry [299, 300] => entry 385
      299u16,                   // first_entry_index
      300u16,                   // last_entry_index

      // EntryMapRecord[3]: "null" + entry 0 => entry 301
      0u16,                     // first_entry_index
      0u16                      // last_entry_index
    };

    let offset = buffer.offset_for("glyph_map") as u32;
    buffer.write_at("glyph_map_offset", offset);

    let offset = buffer.offset_for("feature_map") as u32;
    buffer.write_at("feature_map_offset", offset);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#patch-map-format-2
pub fn codepoints_only_format2() -> BeBuffer {
    let mut buffer = be_buffer! {
      2u8,                // format

      0u32,               // reserved

      [1, 2, 3, 4u32],    // compat id

      3u8,                // default patch encoding
      (Uint24::new(4)),   // entry count
      {0u32: "entries_offset"},
      0u32,               // entry string data offset

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      /* ### Entries Array ### */
      // Entry id = 1
      {0b00010000u8: "entries"},              // format = CODEPOINT_BIT_1
      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [0..17]

      // Entry id = 2
      0b01100000u8,                           // format = IGNORED | CODEPOINT_BIT_2
      5u16,                                   // bias
      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [5..22]

      // Entry id = 3
      0b00100000u8,                           // format = CODEPOINT_BIT_2
      5u16,                                   // bias
      [0b00001101, 0b00000011, 0b00110001u8],  // codepoints = [5..22]

      // Entry id = 4
      0b00110000u8,                           // format = CODEPOINT_BIT_1 | CODEPOINT_BIT_2
      (Uint24::new(80_000)),                  // bias
      [0b00001101, 0b00000011, 0b00110001u8]  // codepoints = [80_005..80_022]
    };

    let offset = buffer.offset_for("entries") as u32;
    buffer.write_at("entries_offset", offset);

    buffer
}

pub fn features_and_design_space_format2() -> BeBuffer {
    let mut buffer = be_buffer! {
      2u8, // format

      0u32, // reserved

      [1, 2, 3, 4u32], // compat id

      3u8, // default patch encoding
      (Uint24::new(3)), // entry count
      {0u32: "entries_offset"},
      0u32, // entry id string data offset

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      /* ### Entries Array ### */
      // Entry id = 1
      {0b00010001u8: "entries"},          // format = CODEPOINT_BIT_1 | FEATURES_AND_DESIGN_SPACE

      2u8,                                // feature count = 2
      (Tag::new(b"liga")),                // feature[0] = liga
      (Tag::new(b"smcp")),                // feature[1] = smcp

      1u16,                               // design space count
      (Tag::new(b"wdth")),                // tag = wdth
      {0x8000u32: "wdth start"},          // start = 0.5
      0x10000u32,                         // end = 1.0

      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [0..17]

      // Entries Array
      // Entry id = 2
      0b00010001u8,                       // format = CODEPOINT_BIT_1 | FEATURES_AND_DESIGN_SPACE

      1u8,                                // feature count
      (Tag::new(b"rlig")),                // feature[0]

      0u16,                               // design space count

      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [0..17]

      // Entry id = 3
      0b000100001u8,                      // format = CODEPOINT_BIT_2 | FEATURES_AND_DESIGN_SPACE

      1u8,                                // feature count = 1
      (Tag::new(b"smcp")),                // feature[0] = smcp

      3u16,                               // design space count
      (Tag::new(b"wght")),                // tag = wght
      0x00C8_0000u32,                     // start = 200
      0x02BC_0000u32,                     // end = 700

      (Tag::new(b"wdth")),                // tag = wdth
      0x0u32,                             // start = 0
      0x8000,                             // end = 0

      (Tag::new(b"wdth")),                // tag = wdth
      0x0002_0000,                        // start = 2.0
      0x0002_8000,                        // end = 2.5

      5u16,                               // bias = 5
      0b00001101, 0b00000011, 0b00110001  // codepoints = [5..22]
    };

    let offset = buffer.offset_for("entries") as u32;
    buffer.write_at("entries_offset", offset);

    buffer
}

pub fn copy_indices_format2() -> BeBuffer {
    let mut buffer = be_buffer! {
      2u8,                      // format

      0u32,                     // reserved

      [1, 2, 3, 4u32],          // compat id

      3u8,                      // default patch encoding = glyph keyed
      (Uint24::new(9)),         // entry count
      {0u32: "entries_offset"}, // entries offset
      0u32,                     // entry id string data offset

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      // Entries Array

      // Entry id = 1
      {0b01100000u8: "entries"},              // format = CODEPOINT_BIT_2 | IGNORED
      5u16,                                   // bias = 5
      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [5..22]

      // Entry id = 2
      0b00100000u8,                           // format = CODEPOINT_BIT_2
      50u16,                                  // bias
      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [50..67]

      // Entry id = 3
      0b00000001u8,                           // format = FEATURES_AND_DESIGN_SPACE

      1u8,                                    // feature count = 1
      (Tag::new(b"rlig")),                    // feature[0] = rlig

      1u16,                                   // design space count = 1
      (Tag::new(b"wght")),                    // tag = wght
      0x00C8_0000u32,                         // start = 200
      0x02BC_0000u32,                         // end = 700

      // Entry id = 4
      0b00000001u8,                           // format = FEATURES_AND_DESIGN_SPACE

      1u8,                                    // feature count
      (Tag::new(b"liga")),                    // feature[0] = liga

      1u16,                                   // design space count
      (Tag::new(b"wght")),                    // tag = wght
      0x0032_0000,                            // start = 50
      0x0064_0000,                            // end = 100

      // Entry id = 5
      0b00000010u8,                           // format = COPY_INDICES
      1u8,                                    // copy count
      (Uint24::new(0)),                       // copy

      // Entry id = 6
      0b00000010u8,                           // format = COPY_INDICES
      1u8,                                    // copy count
      (Uint24::new(2)),                       // copy

      // Entry id = 7
      0b00000010u8,                           // format = COPY_INDICES
      4u8,                                    // copy count
      (Uint24::new(3)),                       // copy[0]
      (Uint24::new(2)),                       // copy[1]
      (Uint24::new(1)),                       // copy[2]
      (Uint24::new(0)),                       // copy[3]

      // Entry id = 8
      0b00000010u8,                           // format = COPY_INDICES
      2u8,                                    // copy count
      (Uint24::new(4)),                       // copy[0]
      (Uint24::new(5)),                       // copy[1]

      // Entry id = 9
      0b00100010u8,                           // format = CODEPOINT_BIT_2 | COPY_INDICES
      1u8,                                    // copy count
      (Uint24::new(0)),                       // copy[0]
      100u16,                                 // bias
      [0b00001101, 0b00000011, 0b00110001u8]  // codepoints = [100..117]
    };

    let offset = buffer.offset_for("entries") as u32;
    buffer.write_at("entries_offset", offset);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#patch-map-format-2
pub fn custom_ids_format2() -> BeBuffer {
    let mut buffer = be_buffer! {
      2u8,                               // format

      0u32,                              // reserved

      [1, 2, 3, 4u32],                   // compat id

      3u8,                               // default patch encoding = glyph keyed
      {(Uint24::new(4)): "entry_count"}, // entry count
      {0u32: "entries_offset"},          // entries offset
      0u32,                              // entry id string data offset

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      // Entries Array
      // Entry id = 0
      {0b00010100u8: "entries"},              // format = CODEPOINT_BIT_1 | ID_DELTA
      (Int24::new(-1)),                       // id delta
      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [0..17]

      // Entry id = 6
      0b00100100u8,                           // format = CODEPOINT_BIT_2 | ID_DELTA
      {(Int24::new(5)): "id delta"},            // id delta
      5u16,                                   // bias
      [0b00001101, 0b00000011, 0b00110001u8], // codepoints = [5..22]

      // Entry id = 14
      0b01000100u8,                           // format = ID_DELTA | IGNORED
      {(Int24::new(7)): "id delta - ignored entry"}, // id delta

      // Entry id = 15
      0b00101000u8,                           // format = CODEPOINT_BIT_2 | PATCH_ENCODING
      {3u8: "entry[4] encoding"},             // patch encoding = Glyph Keyed
      10u16,                                  // bias
      [0b00001101, 0b00000011, 0b00110001u8]  // codepoints = [10..27]
    };

    let offset = buffer.offset_for("entries") as u32;
    buffer.write_at("entries_offset", offset);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#patch-map-format-2
pub fn string_ids_format2() -> BeBuffer {
    let mut buffer = be_buffer! {
      2u8,                      // format

      0u32,                     // reserved

      [1, 2, 3, 4u32],          // compat id

      3u8,                      // default patch encoding = glyph keyed
      (Uint24::new(6)),         // entry count
      {0u32: "entries_offset"}, // entries offset
      {0u32: "string_data_offset"},                     // entry id string data offset

      8u16, // uriTemplateLength
      [b'A', b'B', b'C', b'D', b'E', b'F', 0xc9, 0xa4],  // uriTemplate[8]

      /* ### Entry Data ### */

      // Entry id = ""
      {0b00000000u8: "entries"},              // format = {}

      // Entry id = abc
      0b00000100u8,                           // format = ID_DELTA
      3u16,                                   // id length

      // Entry id = defg
      0b00000100u8,                           // format = ID_DELTA
      4u16,                                   // id length

      // Entry id = defg
      0b00000000u8,                           // format = {}

      // Entry id = hij
      0b00000100u8,                           // format = ID_DELTA
      {3u16: "entry[4] id length"},           // id length

      // Entry id = ""
      0b00000100u8,                           // format = ID_DELTA
      0u16,                                   // id length

      /* ### String Data ### */
      {b'a': "string_data"},
      [b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j']
    };

    let offset = buffer.offset_for("string_data") as u32;
    buffer.write_at("string_data_offset", offset);

    let offset = buffer.offset_for("entries") as u32;
    buffer.write_at("entries_offset", offset);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#table-keyed
pub fn table_keyed_patch() -> BeBuffer {
    let mut buffer = be_buffer! {
        {(Tag::new(b"iftk")): "tag"},
        0u32,                 // reserved
        [1, 2, 3, 4u32],       // compat id
        3u16,                 // patch count

        // patch_offsets[3]
        {0u32: "patch_off[0]"},
        {0u32: "patch_off[1]"},
        {0u32: "patch_off[2]"},
        {0u32: "patch_off[3]"},

        // patch[0]
        {(Tag::new(b"tab1")): "patch[0]"},
        0u8,       // flags
        {29u32: "decompressed_len[0]"},     // max decompressed length
        // brotli stream (w/ shared dict)
        [0xa1, 0xe0, 0x00, 0xc0, 0x2f, 0x3a, 0x38, 0xf4, 0x01, 0xd1, 0xaf, 0x54, 0x84, 0x14, 0x71,
         0x2a, 0x80, 0x04, 0xa2, 0x1c, 0xd3, 0xdd, 0x07u8],

         // patch[1]
        {(Tag::new(b"tab2")): "patch[1]"},
        {1u8: "flags[1]"},  // flags (REPLACEMENT)
        30u32,              // max decompressed length
        // brotli stream (w/o shared dict)
        [0xa1, 0xe8, 0x00, 0xc0, 0xef, 0x48, 0x9d, 0xfa, 0xdc, 0xf1, 0xc2, 0xac, 0xc5, 0xde, 0xe4, 0xf4,
         0xb4, 0x02, 0x48, 0x98, 0x98, 0x52, 0x64, 0xa8, 0x50, 0x20, 0x29, 0x75, 0x0bu8],

         // patch[2]
        {(Tag::new(b"tab3")): "patch[2]"},
        {2u8: "flags[2]"}, // flags (DROP)
        {0u32: "end"}      // max decompressed length
    };

    let offset = buffer.offset_for("patch[0]") as u32;
    buffer.write_at("patch_off[0]", offset);

    let offset = buffer.offset_for("patch[1]") as u32;
    buffer.write_at("patch_off[1]", offset);

    let offset = buffer.offset_for("patch[2]") as u32;
    buffer.write_at("patch_off[2]", offset);

    let offset = (buffer.offset_for("end") + 4) as u32;
    buffer.write_at("patch_off[3]", offset);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#table-keyed
pub fn noop_table_keyed_patch() -> BeBuffer {
    be_buffer! {
        {(Tag::new(b"iftk")): "tag"},
        0u32,                 // reserved
        [1, 2, 3, 4u32],       // compat id
        0u16,                 // patch count

        // patch_offsets[1]
        {0u32: "patch_off[0]"}
    }
}

pub fn test_font_for_patching_with_loca_mod<F>(loca_mod: F) -> Vec<u8>
where
    F: Fn(&mut Vec<u32>),
{
    let mut font_builder = FontBuilder::new();

    let maxp = Maxp {
        num_glyphs: 15,
        ..Default::default()
    };
    font_builder.add_table(&maxp).unwrap();

    let head = Head {
        index_to_loc_format: 0,
        ..Default::default()
    };
    font_builder.add_table(&head).unwrap();

    // ## glyf ##
    // glyphs are padded to even number of bytes since loca format will be short.
    let glyf = BeBuffer::new()
        .push_with_tag(1u8, "gid_0")
        .extend([2, 3, 4, 5u8, 0u8])
        .push_with_tag(6u8, "gid_1")
        .extend([7, 8u8, 0u8])
        .push_with_tag(9u8, "gid_8")
        .extend([10, 11, 12u8]);

    // ## loca ##
    let gid_1 = glyf.offset_for("gid_1") as u32;
    let gid_8 = glyf.offset_for("gid_8") as u32;
    let end = gid_8 + 4;

    let mut loca = vec![
        0,     // gid 0
        gid_1, // gid 1
        gid_8, // gid 2
        gid_8, // gid 3
        gid_8, // gid 4
        gid_8, // gid 5
        gid_8, // gid 6
        gid_8, // gid 7
        gid_8, // gid 8
        end,   // gid 9
        end,   // gid 10
        end,   // gid 11
        end,   // gid 12
        end,   // gid 13
        end,   // gid 14
        end,   // end
    ];

    loca_mod(&mut loca);

    let loca = Loca::new(loca);
    font_builder.add_table(&loca).unwrap();

    let glyf: &[u8] = &glyf;
    font_builder.add_raw(Tag::new(b"glyf"), glyf);

    font_builder.build()
}

pub fn test_font_for_patching() -> Vec<u8> {
    test_font_for_patching_with_loca_mod(|_| {})
}

// Format specification: https://w3c.github.io/IFT/Overview.html#glyph-keyed
pub fn glyph_keyed_patch_header() -> BeBuffer {
    be_buffer! {
      {(Tag::new(b"ifgk")): "format"}, // format
      0u32,                // reserved
      0u8,                 // flags (0 = u16 gids)
      [6, 7, 8, 9u32],     // compatibility id
      {0u32: "max_uncompressed_length"}
    }
}

// Format specification: https://w3c.github.io/IFT/Overview.html#glyphpatches
pub fn noop_glyf_glyph_patches() -> BeBuffer {
    be_buffer! {
      0u32,       // glyph count
      {1u8: "table_count"},        // table count

      (Tag::new(b"glyf")),   // tables * 1

      // glyph data offsets * 1
      0u32
    }
}

// Format specification: https://w3c.github.io/IFT/Overview.html#glyphpatches
pub fn glyf_u16_glyph_patches() -> BeBuffer {
    let mut buffer = be_buffer! {
      5u32,       // glyph count
      {1u8: "table_count"},        // table count

      // glyph ids * 5
      [2, 7u16],
      {8u16: "gid_8"},
      [9u16],
      {13u16: "gid_13"},

      (Tag::new(b"glyf")),   // tables * 1

      // glyph data offsets * 6
      {0u32: "gid_2_offset"},
      {0u32: "gid_7_offset"},
      {0u32: "gid_8_offset"},
      {0u32: "gid_9_offset"},
      {0u32: "gid_13_offset"},
      {0u32: "end_offset"},

      // data blocks
      {b'a': "gid_2_data"},
      [b'b', b'c'],

      {b'd': "gid_7_data"},
      [b'e', b'f', b'g'],

      {b'h': "gid_8_and_9_data"},
      [b'i', b'j', b'k', b'l'],

      {b'm': "gid_13_data"},
      [b'n']
    };

    let offset = buffer.offset_for("gid_2_data") as u32;
    buffer.write_at("gid_2_offset", offset);

    let offset = buffer.offset_for("gid_7_data") as u32;
    buffer.write_at("gid_7_offset", offset);

    let offset = buffer.offset_for("gid_8_and_9_data") as u32;
    buffer.write_at("gid_8_offset", offset);

    let offset = buffer.offset_for("gid_8_and_9_data") as u32;
    buffer.write_at("gid_9_offset", offset);

    let offset = buffer.offset_for("gid_13_data") as u32;
    buffer.write_at("gid_13_offset", offset);
    buffer.write_at("end_offset", offset + 2);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#glyphpatches
pub fn glyf_u16_glyph_patches_2() -> BeBuffer {
    let mut buffer = be_buffer! {
      3u32,       // glyph count
      {1u8: "table_count"},        // table count

      // glyph ids * 3
      7u16,
      12u16,
      14u16,

      (Tag::new(b"glyf")),   // tables * 1

      // glyph data offsets * 6
      {0u32: "gid_7_offset"},
      {0u32: "gid_12_offset"},
      {0u32: "gid_14_offset"},
      {0u32: "end_offset"},

      // data blocks
      {b'q': "gid_7_data"},
      [b'r'],

      {b's': "gid_12_data"},
      [b't', b'u'],

      {b'v': "gid_14_data"}
    };

    let offset = buffer.offset_for("gid_7_data") as u32;
    buffer.write_at("gid_7_offset", offset);

    let offset = buffer.offset_for("gid_12_data") as u32;
    buffer.write_at("gid_12_offset", offset);

    let offset = buffer.offset_for("gid_14_data") as u32;
    buffer.write_at("gid_14_offset", offset);
    buffer.write_at("end_offset", offset + 1);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#glyphpatches
pub fn glyf_u24_glyph_patches() -> BeBuffer {
    let mut buffer = be_buffer! {
      5u32,       // glyph count
      1u8,        // table count
      (Uint24::new(2)), (Uint24::new(7)), (Uint24::new(8)), (Uint24::new(9)), (Uint24::new(13)),   // glyph ids * 5
      (Tag::new(b"glyf")),   // tables * 1

      // glyph data offsets * 6
      {0u32: "gid_2_offset"},
      {0u32: "gid_7_offset"},
      {0u32: "gid_8_offset"},
      {0u32: "gid_9_offset"},
      {0u32: "gid_13_offset"},
      {0u32: "end_offset"},

      // data blocks
      {b'a': "gid_2_data"},
      [b'b', b'c'],

      {b'd': "gid_7_data"},
      [b'e', b'f', b'g'],

      {b'h': "gid_8_and_9_data"},
      [b'i', b'j', b'k', b'l'],

      {b'm': "gid_13_data"},
      [b'n']
    };

    let offset = buffer.offset_for("gid_2_data") as u32;
    buffer.write_at("gid_2_offset", offset);

    let offset = buffer.offset_for("gid_7_data") as u32;
    buffer.write_at("gid_7_offset", offset);

    let offset = buffer.offset_for("gid_8_and_9_data") as u32;
    buffer.write_at("gid_8_offset", offset);

    let offset = buffer.offset_for("gid_8_and_9_data") as u32;
    buffer.write_at("gid_9_offset", offset);

    let offset = buffer.offset_for("gid_13_data") as u32;
    buffer.write_at("gid_13_offset", offset);
    buffer.write_at("end_offset", offset + 2);

    buffer
}

// Format specification: https://w3c.github.io/IFT/Overview.html#glyphpatches
pub fn glyf_and_gvar_u16_glyph_patches() -> BeBuffer {
    let mut buffer = be_buffer! {
      3u32,       // glyph count
      2u8,        // table count
      [2, 7, 8u16],   // glyph ids * 3
      (Tag::new(b"glyf")),               // tables[0]
      {(Tag::new(b"gvar")): "gvar_tag"}, // tables[1]

      // glyph data offsets * 7
      {0u32: "glyf_gid_2_offset"},
      {0u32: "glyf_gid_7_offset"},
      {0u32: "glyf_gid_8_offset"},
      {0u32: "gvar_gid_2_offset"},
      {0u32: "gvar_gid_7_offset"},
      {0u32: "gvar_gid_8_offset"},
      {0u32: "end_offset"},

      // data blocks
      {b'a': "glyf_gid_2_data"},
      [b'b', b'c'],

      {b'd': "glyf_gid_7_data"},
      [b'e', b'f', b'g'],

      {b'h': "glyf_gid_8_data"},
      [b'i', b'j', b'k', b'l'],

      {b'm': "gvar_gid_2_data"},
      [b'n'],

      {b'o': "gvar_gid_7_data"},
      [b'p', b'q'],

      {b'r': "gvar_gid_8_data"}
    };

    let offset = buffer.offset_for("glyf_gid_2_data") as u32;
    buffer.write_at("glyf_gid_2_offset", offset);
    let offset = buffer.offset_for("glyf_gid_7_data") as u32;
    buffer.write_at("glyf_gid_7_offset", offset);
    let offset = buffer.offset_for("glyf_gid_8_data") as u32;
    buffer.write_at("glyf_gid_8_offset", offset);

    let offset = buffer.offset_for("gvar_gid_2_data") as u32;
    buffer.write_at("gvar_gid_2_offset", offset);
    let offset = buffer.offset_for("gvar_gid_7_data") as u32;
    buffer.write_at("gvar_gid_7_offset", offset);
    let offset = buffer.offset_for("gvar_gid_8_data") as u32;
    buffer.write_at("gvar_gid_8_offset", offset);
    buffer.write_at("end_offset", offset + 1);

    buffer
}