File: instructionCompiler_test.py

package info (click to toggle)
ufo2ft 3.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,528 kB
  • sloc: python: 18,685; makefile: 10
file content (820 lines) | stat: -rw-r--r-- 28,911 bytes parent folder | download
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
import logging
from functools import partial

import pytest
from fontTools.cu2qu.ufo import font_to_quadratic
from fontTools.misc.fixedTools import floatToFixedToFloat
from fontTools.pens.hashPointPen import HashPointPen
from fontTools.pens.roundingPen import RoundingPointPen
from fontTools.ttLib.tables._g_l_y_f import (
    OVERLAP_COMPOUND,
    ROUND_XY_TO_GRID,
    USE_MY_METRICS,
    flagOverlapSimple,
)
from fontTools.ttLib.ttFont import TTFont

from ufo2ft.instructionCompiler import InstructionCompiler

from .outlineCompiler_test import getpath

TRUETYPE_INSTRUCTIONS_KEY = "public.truetype.instructions"


def expect_maxp(
    font,
    maxStorage=0,
    maxFunctionDefs=0,
    maxInstructionDefs=0,
    maxStackElements=0,
    maxSizeOfInstructions=0,
    maxZones=1,
    maxTwilightPoints=0,
):
    maxp = font["maxp"]
    assert maxp.maxStorage == maxStorage
    assert maxp.maxFunctionDefs == maxFunctionDefs
    assert maxp.maxInstructionDefs == maxInstructionDefs
    assert maxp.maxStackElements == maxStackElements
    assert maxp.maxSizeOfInstructions == maxSizeOfInstructions
    assert maxp.maxZones == maxZones
    assert maxp.maxTwilightPoints == maxTwilightPoints


def get_hash_ufo(glyph, ufo):
    hash_pen = HashPointPen(glyph.width, ufo)
    round_pen = RoundingPointPen(
        hash_pen, transformRoundFunc=partial(floatToFixedToFloat, precisionBits=14)
    )
    glyph.drawPoints(round_pen)
    return hash_pen.hash


def get_hash_ttf(glyph_name, ttf):
    aw, _lsb = ttf["hmtx"][glyph_name]
    gs = ttf.getGlyphSet()
    hash_pen = HashPointPen(aw, gs)
    ttf["glyf"][glyph_name].drawPoints(hash_pen, ttf["glyf"])
    return hash_pen.hash


@pytest.fixture
def quadfont():
    font = TTFont()
    font.importXML(getpath("TestFont-TTF-post3.ttx"))
    return font


@pytest.fixture
def quadufo(FontClass):
    font = FontClass(getpath("TestFont.ufo"))
    font_to_quadratic(font)
    return font


@pytest.fixture
def quaduforeversed(FontClass):
    font = FontClass(getpath("TestFont.ufo"))
    font_to_quadratic(font=font, reverse_direction=True)
    return font


@pytest.fixture
def testufo(FontClass):
    font = FontClass(getpath("TestFont.ufo"))
    del font.lib["public.postscriptNames"]
    return font


class InstructionCompilerTest:
    # _check_glyph_hash

    def test_check_glyph_hash_match(self, quaduforeversed, quadfont):
        glyph = quaduforeversed["a"]
        ufo_hash = get_hash_ufo(glyph, quaduforeversed)
        ttglyph = quadfont["glyf"]["a"]

        ic = InstructionCompiler(quaduforeversed, quadfont)
        result = ic._check_glyph_hash(glyph, ttglyph, ufo_hash)
        assert result

    def test_check_glyph_hash_missing(self, quaduforeversed, quadfont):
        glyph = quaduforeversed["a"]

        ic = InstructionCompiler(quaduforeversed, quadfont)
        result = ic._check_glyph_hash(
            glyph,
            quadfont["glyf"]["a"],
            None,
        )
        assert not result

    def test_check_glyph_hash_mismatch(self, testufo, quadfont):
        glyph = testufo["a"]
        ufo_hash = get_hash_ufo(glyph, testufo)
        ttglyph = quadfont["glyf"]["a"]

        # The contour direction is reversed in testufo vs. quadfont, so the
        # hash should not match

        ic = InstructionCompiler(testufo, quadfont)
        result = ic._check_glyph_hash(
            glyph,
            ttglyph,
            ufo_hash,
        )
        assert not result

    def test_check_glyph_hash_mismatch_composite(self, testufo, quadfont):
        glyph = testufo["h"]
        ufo_hash = get_hash_ufo(glyph, testufo)
        ttglyph = quadfont["glyf"]["h"]

        # The contour direction is reversed in testufo vs. quadfont, so the
        # hash should not match

        ic = InstructionCompiler(testufo, quadfont)
        result = ic._check_glyph_hash(
            glyph,
            ttglyph,
            ufo_hash,
        )
        assert not result

    def test_check_glyph_hash_mismatch_width(self, quaduforeversed, quadfont):
        glyph = quaduforeversed["a"]

        # Modify the glyph width in the UFO to trigger the mismatch
        glyph.width += 10

        ufo_hash = get_hash_ufo(glyph, quaduforeversed)
        ttglyph = quadfont["glyf"]["a"]

        ic = InstructionCompiler(quaduforeversed, quadfont)
        result = ic._check_glyph_hash(
            glyph,
            ttglyph,
            ufo_hash,
        )
        assert not result

    # _check_tt_data_format

    def test_check_tt_data_format_match_str(self):
        result = InstructionCompiler._check_tt_data_format(
            ttdata={"formatVersion": "1"},
            name="",
        )
        assert result is None

    def test_check_tt_data_format_type_error(self):
        with pytest.raises(
            TypeError,
            match=(
                "Illegal type 'int' instead of 'str' for formatVersion "
                "for instructions in location."
            ),
        ):
            InstructionCompiler._check_tt_data_format(
                ttdata={"formatVersion": 1},  # Spec requires a str
                name="location",
            )

    def test_check_tt_data_format_mismatch_str(self):
        with pytest.raises(
            NotImplementedError,
            match="Unknown formatVersion 1.5 for instructions in location.",
        ):
            InstructionCompiler._check_tt_data_format(
                ttdata={"formatVersion": "1.5"},  # Maps to the correct int
                name="location",
            )

    # _compile_program

    def test_compile_program_no_ttdata(self, quadufo):
        # UFO contains no "public.truetype.instructions" lib key
        ic = InstructionCompiler(quadufo, TTFont())
        for key, tag in (
            ("controlValueProgram", "prep"),
            ("fontProgram", "fpgm"),
        ):
            ic._compile_program(key=key, table_tag=tag)
        assert "fpgm" not in ic.otf
        assert "prep" not in ic.otf

    def test_compile_program_no_programs(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are not there. (They are optional)
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
        }
        for key, tag in (
            ("controlValueProgram", "prep"),
            ("fontProgram", "fpgm"),
        ):
            ic._compile_program(key=key, table_tag=tag)
        assert "fpgm" not in ic.otf
        assert "prep" not in ic.otf

    def test_compile_program_none(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are None.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValueProgram": None,
            "fontProgram": None,
        }
        for key, tag in (
            ("controlValueProgram", "prep"),
            ("fontProgram", "fpgm"),
        ):
            ic._compile_program(key=key, table_tag=tag)
        assert "fpgm" not in ic.otf
        assert "prep" not in ic.otf

    def test_compile_program_empty(self, quadufo, caplog):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are empty.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValueProgram": "",
            "fontProgram": "",
        }
        with caplog.at_level(logging.DEBUG, logger="ufo2ft.instructionCompiler"):
            for key, tag in (
                ("controlValueProgram", "prep"),
                ("fontProgram", "fpgm"),
            ):
                ic._compile_program(key=key, table_tag=tag)
        assert (
            "Assembly for table 'fpgm' is empty, table not added to font."
            in caplog.text
        )
        assert (
            "Assembly for table 'prep' is empty, table not added to font."
            in caplog.text
        )
        assert "fpgm" not in ic.otf
        assert "prep" not in ic.otf

    def test_compile_program(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, and the font and
        # control value programs are present.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValueProgram": "PUSHW[]\n511\nSCANCTRL[]",
            "fontProgram": "PUSHB[]\n0\nFDEF[]\nPOP[]\nENDF[]",
        }
        for key, tag in (
            ("controlValueProgram", "prep"),
            ("fontProgram", "fpgm"),
        ):
            ic._compile_program(key=key, table_tag=tag)

        assert "fpgm" in ic.otf
        assert "prep" in ic.otf

        # Check if the bytecode is correct, though this may be out of scope
        assert ic.otf["fpgm"].program.getBytecode() == b"\xb0\x00\x2c\x21\x2d"
        assert ic.otf["prep"].program.getBytecode() == b"\xb8\x01\xff\x85"

    # compileGlyphInstructions

    def test_compileGlyphInstructions_missing_glyph(self, caplog):
        # The method logs an info when trying to compile a glyph which is
        # missing in the UFO
        ic = InstructionCompiler(dict(), None)
        with caplog.at_level(logging.INFO, logger="ufo2ft.instructionCompiler"):
            ic.compileGlyphInstructions(None, "A")
        assert "Skipping compilation of instructions for glyph 'A'" in caplog.text
        # ... except for '.notdef' which is frequently generated
        with caplog.at_level(logging.INFO, logger="ufo2ft.instructionCompiler"):
            ic.compileGlyphInstructions(None, ".notdef")
        assert (
            "Skipping compilation of instructions for glyph '.notdef'"
            not in caplog.text
        )

    # _compile_tt_glyph_program

    def test_compile_tt_glyph_program_empty(self, quaduforeversed, quadfont):
        # UFO glyph contains no "public.truetype.instructions" lib key
        with pytest.raises(
            TypeError,
            match=(
                "Illegal type 'NoneType' instead of 'str' for formatVersion "
                "for instructions in glyph 'a'."
            ),
        ):
            InstructionCompiler(quaduforeversed, quadfont)._compile_tt_glyph_program(
                glyph=quaduforeversed["a"],
                ttglyph=quadfont["glyf"]["a"],
                ttdata={},
            )

    def test_compile_tt_glyph_program_no_asm(self, quaduforeversed, quadfont, caplog):
        # UFO glyph contains "public.truetype.instructions" lib key, but no
        # assembly code entry
        ic = InstructionCompiler(quaduforeversed, quadfont)

        assert not ic.otf["glyf"]["a"].isComposite()

        glyph = ic.ufo["a"]
        glyph_hash = get_hash_ufo(glyph, ic.ufo)

        with caplog.at_level(logging.ERROR, logger="ufo2ft.instructionCompiler"):
            ic._compile_tt_glyph_program(
                glyph=ic.ufo["a"],
                ttglyph=ic.otf["glyf"]["a"],
                ttdata={
                    "formatVersion": "1",
                    "id": glyph_hash,
                    # "assembly": "",
                },
            )
        assert (
            "Glyph assembly missing, glyph 'a' will have no instructions in font."
            in caplog.text
        )

    def test_compile_tt_glyph_program_empty_asm(
        self, quaduforeversed, quadfont, caplog
    ):
        # UFO glyph contains "public.truetype.instructions" lib key, but the
        # assembly code entry is empty
        ic = InstructionCompiler(quaduforeversed, quadfont)

        assert not ic.otf["glyf"]["a"].isComposite()

        glyph = ic.ufo["a"]
        glyph_hash = get_hash_ufo(glyph, ic.ufo)

        with caplog.at_level(logging.DEBUG, logger="ufo2ft.instructionCompiler"):
            ic._compile_tt_glyph_program(
                glyph=ic.ufo["a"],
                ttglyph=ic.otf["glyf"]["a"],
                ttdata={
                    "formatVersion": "1",
                    "id": glyph_hash,
                    "assembly": "",
                },
            )
        assert "Glyph 'a' has no instructions." in caplog.text
        assert not hasattr(ic.otf["glyf"]["h"], "program")

    def test_compile_tt_glyph_program_empty_asm_composite(
        self, quaduforeversed, quadfont
    ):
        # UFO glyph contains "public.truetype.instructions" lib key, but the
        # assembly code entry is empty. The glyph is a composite.
        ic = InstructionCompiler(quaduforeversed, quadfont)

        glyph = ic.ufo["h"]
        glyph_hash = get_hash_ufo(glyph, ic.ufo)

        assert ic.otf["glyf"]["h"].isComposite()

        ic._compile_tt_glyph_program(
            glyph=ic.ufo["h"],
            ttglyph=ic.otf["glyf"]["h"],
            ttdata={
                "formatVersion": "1",
                "id": glyph_hash,
                "assembly": "",
            },
        )
        # Components must not have an empty program
        assert not hasattr(ic.otf["glyf"]["h"], "program")

    def test_compile_tt_glyph_program(self, quaduforeversed, quadfont):
        # UFO glyph contains "public.truetype.instructions" lib key, and the
        # assembly code entry is present.
        ic = InstructionCompiler(quaduforeversed, quadfont)

        assert not ic.otf["glyf"]["a"].isComposite()

        glyph = ic.ufo["a"]
        glyph_hash = get_hash_ufo(glyph, ic.ufo)

        ic._compile_tt_glyph_program(
            glyph=ic.ufo["a"],
            ttglyph=ic.otf["glyf"]["a"],
            ttdata={
                "formatVersion": "1",
                "id": glyph_hash,
                "assembly": "PUSHB[]\n0\nMDAP[1]",
            },
        )
        assert ic.otf["glyf"]["a"].program.getBytecode() == b"\xb0\x00\x2f"

    def test_compile_tt_glyph_program_composite(self, quaduforeversed, quadfont):
        # UFO glyph contains "public.truetype.instructions" lib key, and the
        # assembly code entry is present. The glyph is a composite.
        name = "k"  # Name of the composite glyph
        ic = InstructionCompiler(quaduforeversed, quadfont)

        assert ic.otf["glyf"][name].isComposite()

        glyph_hash = get_hash_ufo(ic.ufo[name], ic.ufo)

        ic._compile_tt_glyph_program(
            glyph=ic.ufo[name],
            ttglyph=ic.otf["glyf"][name],
            ttdata={
                "formatVersion": "1",
                "id": glyph_hash,
                "assembly": "PUSHB[]\n0\nMDAP[1]",
            },
        )
        ttglyph = ic.otf["glyf"][name]
        assert hasattr(ttglyph, "program")
        assert ttglyph.program.getBytecode() == b"\xb0\x00\x2f"

    # _set_composite_flags

    def test_set_composite_flags_no_ttdata(self, quadufo, quadfont):
        name = "h"  # Name of the composite glyph
        ic = InstructionCompiler(quadufo, quadfont)

        glyph = quadufo[name]
        ttglyph = quadfont["glyf"][name]

        ic._set_composite_flags(
            glyph=glyph,
            ttglyph=ttglyph,
        )

        # Flags have been set by heuristics
        assert not ttglyph.components[0].flags & OVERLAP_COMPOUND
        assert ttglyph.components[0].flags & ROUND_XY_TO_GRID
        assert not ttglyph.components[0].flags & USE_MY_METRICS
        assert not ttglyph.components[1].flags & OVERLAP_COMPOUND
        assert ttglyph.components[1].flags & ROUND_XY_TO_GRID
        assert ttglyph.components[1].flags & USE_MY_METRICS

    def test_set_composite_flags_compound(self, quadufo, quadfont):
        name = "k"  # Name of the composite glyph
        ic = InstructionCompiler(quadufo, quadfont)

        glyph = quadufo[name]
        glyph.components[0].identifier = "component0"
        glyph.components[1].identifier = "component1"
        glyph.lib = {"public.truetype.overlap": True}
        ttglyph = quadfont["glyf"][name]

        ic._set_composite_flags(
            glyph=glyph,
            ttglyph=ttglyph,
        )
        # The OVERLAP_COMPOUND flag is only set on 1st component
        assert ttglyph.components[0].flags & OVERLAP_COMPOUND
        assert not ttglyph.components[1].flags & OVERLAP_COMPOUND

    def test_set_composite_flags_no_compound(self, quadufo, quadfont):
        name = "k"  # Name of the composite glyph
        ic = InstructionCompiler(quadufo, quadfont)

        glyph = quadufo[name]
        glyph.components[0].identifier = "component0"
        glyph.components[1].identifier = "component1"
        glyph.lib = {"public.truetype.overlap": False}
        ttglyph = quadfont["glyf"][name]

        ic._set_composite_flags(
            glyph=glyph,
            ttglyph=ttglyph,
        )
        assert not ttglyph.components[0].flags & OVERLAP_COMPOUND
        assert not ttglyph.components[1].flags & OVERLAP_COMPOUND

    def test_set_composite_flags(self, quadufo, quadfont):
        name = "h"  # Name of the composite glyph
        ic = InstructionCompiler(quadufo, quadfont)

        glyph = quadufo[name]
        glyph.components[0].identifier = "component0"
        glyph.components[1].identifier = "component1"
        glyph.lib = {
            "public.objectLibs": {
                "component0": {
                    "public.truetype.roundOffsetToGrid": False,
                    "public.truetype.useMyMetrics": False,
                },
                "component1": {
                    "public.truetype.roundOffsetToGrid": True,
                    "public.truetype.useMyMetrics": True,
                },
            },
        }
        ttglyph = quadfont["glyf"][name]

        ic._set_composite_flags(
            glyph=glyph,
            ttglyph=ttglyph,
        )

        assert not ttglyph.components[0].flags & OVERLAP_COMPOUND
        assert not ttglyph.components[0].flags & ROUND_XY_TO_GRID
        assert not ttglyph.components[0].flags & USE_MY_METRICS

        assert not ttglyph.components[1].flags & OVERLAP_COMPOUND
        assert ttglyph.components[1].flags & ROUND_XY_TO_GRID
        assert ttglyph.components[1].flags & USE_MY_METRICS

    def test_set_composite_flags_metrics_first_only(self, quadufo, quadfont):
        name = "h"  # Name of the composite glyph
        ic = InstructionCompiler(quadufo, quadfont)

        glyph = quadufo[name]
        glyph.components[0].identifier = "component0"
        glyph.components[1].identifier = "component1"
        glyph.lib = {
            "public.objectLibs": {
                "component0": {
                    "public.truetype.useMyMetrics": True,
                },
                "component1": {
                    "public.truetype.useMyMetrics": True,
                },
            },
        }
        ttglyph = quadfont["glyf"][name]

        ic._set_composite_flags(
            glyph=glyph,
            ttglyph=ttglyph,
        )

        # Flag on component 1 should have been ignored
        assert ttglyph.components[0].flags & USE_MY_METRICS
        assert not ttglyph.components[1].flags & USE_MY_METRICS

    def test_set_composite_flags_metrics_no_id(self, quadufo, quadfont):
        name = "h"  # Name of the composite glyph
        ic = InstructionCompiler(quadufo, quadfont)

        glyph = quadufo[name]
        # First component has no identifier
        glyph.components[0].identifier = None
        glyph.components[1].identifier = "component1"
        glyph.lib = {
            "public.objectLibs": {
                "component1": {
                    "public.truetype.useMyMetrics": False,
                },
            },
        }
        ttglyph = quadfont["glyf"][name]

        ic._set_composite_flags(
            glyph=glyph,
            ttglyph=ttglyph,
        )

        # Flag on both components should have been unset
        assert not ttglyph.components[0].flags & USE_MY_METRICS
        assert not ttglyph.components[1].flags & USE_MY_METRICS

    @pytest.mark.parametrize("autoUseMyMetrics", [True, False])
    def test_set_composite_flags_auto_use_my_metrics_warn_if_components_mismatch(
        self, quadufo, quadfont, autoUseMyMetrics, caplog
    ):
        ic = InstructionCompiler(quadufo, quadfont, autoUseMyMetrics=autoUseMyMetrics)
        name = "h"

        glyph = quadufo[name]
        ttglyph = quadfont["glyf"][name]
        assert len(glyph.components) == len(ttglyph.components)
        glyph.clearComponents()  # to produce an artificial len(components) mismatch
        assert len(glyph.components) != len(ttglyph.components)

        with caplog.at_level(logging.DEBUG, logger="ufo2ft.instructionCompiler"):
            ic._set_composite_flags(glyph=glyph, ttglyph=ttglyph)

        assert "Number of components differ" in caplog.text

    @pytest.mark.parametrize("overlap", [None, False, True])
    def test_set_simple_flags(self, quadufo, quadfont, overlap):
        ic = InstructionCompiler(quadufo, quadfont)
        name = "a"

        glyph = quadufo[name]
        if overlap is not None:
            glyph.lib = {"public.truetype.overlap": overlap}
        ttglyph = quadfont["glyf"][name]

        ic._set_simple_flags(glyph=glyph, ttglyph=ttglyph)

        if overlap:
            assert ttglyph.flags[0] & flagOverlapSimple
        else:
            assert not ttglyph.flags[0] & flagOverlapSimple

    # update_maxp

    def test_update_maxp_no_ttdata(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)

        ic.update_maxp()
        expect_maxp(ic.otf)

    def test_update_maxp(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "maxStorage": 1,
            "maxFunctionDefs": 1,
            "maxInstructionDefs": 1,
            "maxStackElements": 1,
            "maxSizeOfInstructions": 1,
            "maxZones": 2,
            "maxTwilightPoints": 1,
        }
        # Make a glyph program of size 3 in "a"
        self.test_compile_tt_glyph_program(quaduforeversed, quadfont)
        ic.update_maxp()
        # maxSizeOfInstructions should be 3 because it is calculated from the font
        expect_maxp(ic.otf, 1, 1, 1, 1, 3, 2, 1)

    # setupTable_cvt

    def test_setupTable_cvt(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValue": {
                "1": 500,
                "2": 750,
                "3": -250,
            },
        }
        ic.setupTable_cvt()
        assert "cvt " in ic.otf
        assert list(ic.otf["cvt "].values) == [0, 500, 750, -250]

    def test_setupTable_cvt_empty(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValue": {},
        }
        ic.setupTable_cvt()
        assert "cvt " not in ic.otf

    def test_setupTable_cvt_none(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValue": None,
        }
        ic.setupTable_cvt()
        assert "cvt " not in ic.otf

    def test_setupTable_cvt_missing(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
        }
        ic.setupTable_cvt()
        assert "cvt " not in ic.otf

    def test_setupTable_cvt_no_ttdata(self, quaduforeversed, quadfont):
        ic = InstructionCompiler(quaduforeversed, quadfont)
        ic.setupTable_cvt()
        assert "cvt " not in ic.otf

    # setupTable_fpgm

    def test_setupTable_fpgm_no_ttdata(self, quadufo):
        # UFO contains no "public.truetype.instructions" lib key
        ic = InstructionCompiler(quadufo, TTFont())
        ic.setupTable_fpgm()
        assert "fpgm" not in ic.otf

    def test_setupTable_fpgm_no_program(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are not there. (They are optional)
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
        }
        ic.setupTable_fpgm()
        assert "fpgm" not in ic.otf

    def test_setupTable_fpgm_none(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are None.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "fontProgram": None,
        }
        ic.setupTable_fpgm()
        assert "fpgm" not in ic.otf

    def test_setupTable_fpgm_empty(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are empty.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "fontProgram": "",
        }
        ic.setupTable_fpgm()
        assert "fpgm" not in ic.otf

    def test_setupTable_fpgm(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, and the font and
        # control value programs are present.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "fontProgram": "PUSHB[]\n0\nFDEF[]\nPOP[]\nENDF[]",
        }
        ic.setupTable_fpgm()

        assert "fpgm" in ic.otf

        # Check if the bytecode is correct, though this may be out of scope
        assert ic.otf["fpgm"].program.getBytecode() == b"\xb0\x00\x2c\x21\x2d"

    # setupTable_gasp

    def test_setupTable_gasp(self, testufo):
        ic = InstructionCompiler(testufo, TTFont())
        ic.setupTable_gasp()
        assert "gasp" in ic.otf
        assert ic.otf["gasp"].gaspRange == {7: 10, 65535: 15}

    def test_compile_without_gasp(self, testufo):
        testufo.info.openTypeGaspRangeRecords = None
        ic = InstructionCompiler(testufo, TTFont())
        ic.setupTable_gasp()
        assert "gasp" not in ic.otf

    def test_compile_empty_gasp(self, testufo):
        # ignore empty gasp
        testufo.info.openTypeGaspRangeRecords = []
        ic = InstructionCompiler(testufo, TTFont())
        ic.setupTable_gasp()
        assert "gasp" not in ic.otf

    # setupTable_prep

    def test_setupTable_prep_no_ttdata(self, quadufo):
        # UFO contains no "public.truetype.instructions" lib key
        ic = InstructionCompiler(quadufo, TTFont())
        ic.setupTable_prep()
        assert "prep" not in ic.otf

    def test_setupTable_prep_no_program(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are not there. (They are optional)
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
        }
        ic.setupTable_prep()
        assert "prep" not in ic.otf

    def test_setupTable_prep_none(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are None.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValueProgram": None,
        }
        ic.setupTable_prep()
        assert "prep" not in ic.otf

    def test_setupTable_prep_empty(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, but the font and
        # control value programs are empty.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValueProgram": "",
        }
        ic.setupTable_prep()
        assert "prep" not in ic.otf

    def test_setupTable_prep(self, quadufo):
        # UFO contains the "public.truetype.instructions" lib key, and the font and
        # control value programs are present.
        ic = InstructionCompiler(quadufo, TTFont())
        ic.ufo.lib[TRUETYPE_INSTRUCTIONS_KEY] = {
            "formatVersion": "1",
            "controlValueProgram": "PUSHW[]\n511\nSCANCTRL[]",
        }
        ic.setupTable_prep()

        assert "prep" in ic.otf

        # Check if the bytecode is correct, though this may be out of scope
        assert ic.otf["prep"].program.getBytecode() == b"\xb8\x01\xff\x85"