File: test_builder.py

package info (click to toggle)
gftools 0.9.991%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,568 kB
  • sloc: python: 15,959; sh: 33; makefile: 6
file content (143 lines) | stat: -rw-r--r-- 5,864 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
import tempfile
import pytest
import shutil
import os
import subprocess

from gftools.builder import GFBuilder


CWD = os.path.dirname(__file__)
TEST_DIR = os.path.join(CWD, "..", "data", "test", "builder")


@pytest.mark.parametrize(
    "fp,font_paths",
    [
        # Tests our basic Glyphs setup. By default, otf, ttf, variable and
        # webfonts are generated.
        (
            os.path.join(TEST_DIR, "basic_family_glyphs_0"),
            [
                os.path.join("variable", "TestFamily[wght].ttf"),
                os.path.join("ttf", "TestFamily-Black.ttf"),
                os.path.join("ttf", "TestFamily-Regular.ttf"),
                os.path.join("ttf", "TestFamily-Thin.ttf"),
                os.path.join("otf", "TestFamily-Black.otf"),
                os.path.join("otf", "TestFamily-Regular.otf"),
                os.path.join("otf", "TestFamily-Thin.otf"),
                os.path.join("webfonts", "TestFamily[wght].woff2"),
                os.path.join("webfonts", "TestFamily-Black.woff2"),
                os.path.join("webfonts", "TestFamily-Regular.woff2"),
                os.path.join("webfonts", "TestFamily-Thin.woff2"),
            ],
        ),
        # Family consists of ufos which are not MM compatible. Tests
        # https://github.com/googlefonts/gftools/pull/669
        (
            os.path.join(TEST_DIR, "check_compatibility_ufo_1"),
            [
                os.path.join("ttf", "TestFamily-Black.ttf"),
                os.path.join("ttf", "TestFamily-Thin.ttf"),
                os.path.join("otf", "TestFamily-Black.otf"),
                os.path.join("otf", "TestFamily-Thin.otf"),
                os.path.join("webfonts", "TestFamily-Black.woff2"),
                os.path.join("webfonts", "TestFamily-Thin.woff2"),
            ],
        ),
        # Testing a custom recipe provider
        (
            os.path.join(TEST_DIR, "recipeprovider_noto"),
            [
                os.path.join("TestFamily", "unhinted", "ttf", "TestFamily-Regular.ttf"),
                os.path.join(
                    "TestFamily", "googlefonts", "ttf", "TestFamily-Black.ttf"
                ),
            ],
        ),
        # Testing VF split
        (
            os.path.join(TEST_DIR, "split_italic"),
            [
                os.path.join("variable", "TestFamily[wght].ttf"),
                os.path.join("variable", "TestFamily-Italic[wght].ttf"),
            ],
        ),
    ],
)
def test_builder(fp, font_paths):
    with tempfile.TemporaryDirectory() as tmp_dir:
        src_dir = os.path.join(tmp_dir, "sources")
        font_dir = os.path.join(tmp_dir, "fonts")
        shutil.copytree(fp, src_dir)
        build_path = os.path.join(src_dir, "config.yaml")
        subprocess.run(["gftools", "builder", build_path])
        for font_path in font_paths:
            font_path = os.path.join(font_dir, font_path)
            assert os.path.exists(font_path), f"{font_path} is missing"


@pytest.mark.parametrize(
    "fp,font_paths",
    [
        (
            # Tests our basic Glyphs setup. By default, otf, ttf, variable and
            # webfonts are generated.
            os.path.join(TEST_DIR, "basic_family_glyphs_glyphData"),
            [
                os.path.join("variable", "TestFamily[wght].ttf"),
                os.path.join("ttf", "TestFamily-Black.ttf"),
                os.path.join("ttf", "TestFamily-Regular.ttf"),
                os.path.join("ttf", "TestFamily-Thin.ttf"),
                os.path.join("otf", "TestFamily-Black.otf"),
                os.path.join("otf", "TestFamily-Regular.otf"),
                os.path.join("otf", "TestFamily-Thin.otf"),
            ],
        ),
        # Testing a custom recipe provider
        (
            os.path.join(TEST_DIR, "recipeprovider_noto_glyphData"),
            [
                os.path.join(
                    "TestFamily", "unhinted", "variable-ttf", "TestFamily[wght].ttf"
                ),
                os.path.join("TestFamily", "unhinted", "ttf", "TestFamily-Black.ttf"),
                os.path.join("TestFamily", "unhinted", "ttf", "TestFamily-Regular.ttf"),
                os.path.join("TestFamily", "unhinted", "ttf", "TestFamily-Thin.ttf"),
                os.path.join("TestFamily", "unhinted", "otf", "TestFamily-Black.otf"),
                os.path.join("TestFamily", "unhinted", "otf", "TestFamily-Regular.otf"),
                os.path.join("TestFamily", "unhinted", "otf", "TestFamily-Thin.otf"),
                os.path.join("TestFamily", "hinted", "ttf", "TestFamily-Black.ttf"),
                os.path.join("TestFamily", "hinted", "ttf", "TestFamily-Regular.ttf"),
                os.path.join("TestFamily", "hinted", "ttf", "TestFamily-Thin.ttf"),
                os.path.join(
                    "TestFamily", "googlefonts", "variable-ttf", "TestFamily[wght].ttf"
                ),
            ],
        ),
    ],
)
def test_builder_glyphData(fp, font_paths):
    from fontTools.ttLib import TTFont

    with tempfile.TemporaryDirectory() as tmp_dir:
        src_dir = os.path.join(tmp_dir, "sources")
        font_dir = os.path.join(tmp_dir, "fonts")
        shutil.copytree(fp, src_dir)
        build_path = os.path.join(src_dir, "config.yaml")
        subprocess.run(["gftools", "builder", build_path])
        for font_path in font_paths:
            font_path = os.path.join(font_dir, font_path)
            assert os.path.exists(font_path), f"{font_path} is missing"
            ttFont = TTFont(font_path)
            assert "GDEF" in ttFont
            GDEF = ttFont["GDEF"].table
            assert GDEF.GlyphClassDef
            assert "foo" in GDEF.GlyphClassDef.classDefs
            assert GDEF.GlyphClassDef.classDefs["foo"] == 3


def test_bad_configs():
    config = {"Sources": ["foo.glyphs"]}
    with pytest.raises(ValueError):
        GFBuilder(config)