| 1 | from gftools.instancer import gen_static_font |
| 2 | from fontTools.ttLib import TTFont |
| 3 | import pytest |
| 4 | import os |
| 5 | |
| 6 | TEST_DATA = os.path.join("data", "test") |
| 7 | |
| 8 | |
| 9 | @pytest.fixture |
| 10 | def var_font(): |
| 11 | """VF family consisting of a single font with two axes, wdth, wght""" |
| 12 | return TTFont(os.path.join(TEST_DATA, "Inconsolata[wdth,wght].ttf")) |
| 13 | |
| 14 | |
| 15 | def _name_record(ttFont, nameID): |
| 16 | nametable = ttFont["name"] |
| 17 | record = nametable.getName(nameID, 3, 1, 0x409) |
| 18 | if record: |
| 19 | return record.toUnicode() |
| 20 | return None |
| 21 | |
| 22 | |
| 23 | def test_gen_static_font(var_font): |
| 24 | static_font = gen_static_font(var_font, {"wght": 600, "wdth": 75}) |
| 25 | assert _name_record(static_font, 1) == "Inconsolata Condensed SemiBold" |
| 26 | assert _name_record(static_font, 2) == "Regular" |
| 27 | assert _name_record(static_font, 16) == "Inconsolata Condensed" |
| 28 | assert _name_record(static_font, 17) == "SemiBold" |
| 29 | |
| 30 | assert static_font["OS/2"].usWeightClass == 600 |
| 31 | assert static_font["OS/2"].usWidthClass == 5 |
| 32 | assert static_font["OS/2"].fsSelection & (1 << 6) |
| 33 | assert static_font["head"].macStyle == 0 |
| 34 | |
| 35 | |
| 36 | def test_gen_static_font_custom_names(var_font): |
| 37 | static_font = gen_static_font(var_font, {"wght": 900}, "Custom Family", "Black") |
| 38 | assert _name_record(static_font, 1) == "Custom Family Black" |
| 39 | assert _name_record(static_font, 2) == "Regular" |
| 40 | assert _name_record(static_font, 16) == "Custom Family" |
| 41 | assert _name_record(static_font, 17) == "Black" |
| 42 | |
| 43 | |
| 44 | def test_gen_static_font_custom_names_without_declaring_wght(var_font): |
| 45 | static_font = gen_static_font( |
| 46 | var_font, {"wght": 900}, "Custom Family", "8pt SemiCondensed" |
| 47 | ) |
| 48 | assert _name_record(static_font, 1) == "Custom Family 8pt SemiCondensed" |
| 49 | assert _name_record(static_font, 2) == "Regular" |
| 50 | assert _name_record(static_font, 16) == None |
| 51 | assert _name_record(static_font, 17) == None |
| 52 | |
| 53 | |
| 54 | def test_gen_static_font_custom_names_ribbi(var_font): |
| 55 | static_font = gen_static_font( |
| 56 | var_font, {"wght": 900}, "Custom Family", "8pt SemiCondensed Bold Italic" |
| 57 | ) |
| 58 | assert _name_record(static_font, 1) == "Custom Family 8pt SemiCondensed" |
| 59 | assert _name_record(static_font, 2) == "Bold Italic" |
| 60 | assert _name_record(static_font, 16) == None |
| 61 | assert _name_record(static_font, 17) == None |
| 62 | |
| 63 | |
| 64 | def test_gen_static_font_custom_names_non_ribbi(var_font): |
| 65 | static_font = gen_static_font( |
| 66 | var_font, {"wght": 900}, "Custom Family", "8pt SemiCondensed Medium" |
| 67 | ) |
| 68 | assert _name_record(static_font, 1) == "Custom Family 8pt SemiCondensed Medium" |
| 69 | assert _name_record(static_font, 2) == "Regular" |
| 70 | assert _name_record(static_font, 16) == "Custom Family 8pt SemiCondensed" |
| 71 | assert _name_record(static_font, 17) == "Medium" |