• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.ttLib import TTFont
2from fontTools.ttLib import ttGlyphSet
3from fontTools.pens.recordingPen import RecordingPen
4import os
5import pytest
6
7
8class TTGlyphSetTest(object):
9
10    @staticmethod
11    def getpath(testfile):
12        path = os.path.dirname(__file__)
13        return os.path.join(path, "data", testfile)
14
15    @pytest.mark.parametrize(
16        "location, expected",
17        [
18            (
19                None,
20                [
21                 ('moveTo', ((175, 0),)),
22                 ('lineTo', ((367, 0),)),
23                 ('lineTo', ((367, 1456),)),
24                 ('lineTo', ((175, 1456),)),
25                 ('closePath', ())
26                ]
27            ),
28            (
29                {},
30                [
31                 ('moveTo', ((175, 0),)),
32                 ('lineTo', ((367, 0),)),
33                 ('lineTo', ((367, 1456),)),
34                 ('lineTo', ((175, 1456),)),
35                 ('closePath', ())
36                ]
37            ),
38            (
39                {'wght': 100},
40                [
41                 ('moveTo', ((175, 0),)),
42                 ('lineTo', ((271, 0),)),
43                 ('lineTo', ((271, 1456),)),
44                 ('lineTo', ((175, 1456),)),
45                 ('closePath', ())
46                ]
47            ),
48            (
49                {'wght': 1000},
50                [
51                 ('moveTo', ((128, 0),)),
52                 ('lineTo', ((550, 0),)),
53                 ('lineTo', ((550, 1456),)),
54                 ('lineTo', ((128, 1456),)),
55                 ('closePath', ())
56                ]
57            ),
58            (
59                {'wght': 1000, 'wdth': 25},
60                [
61                 ('moveTo', ((140, 0),)),
62                 ('lineTo', ((553, 0),)),
63                 ('lineTo', ((553, 1456),)),
64                 ('lineTo', ((140, 1456),)),
65                 ('closePath', ())
66                ]
67            ),
68            (
69                {'wght': 1000, 'wdth': 50},
70                [
71                 ('moveTo', ((136, 0),)),
72                 ('lineTo', ((552, 0),)),
73                 ('lineTo', ((552, 1456),)),
74                 ('lineTo', ((136, 1456),)),
75                 ('closePath', ())
76                ]
77            ),
78        ]
79    )
80    def test_glyphset(
81        self, location, expected
82    ):
83        # TODO: also test loading CFF-flavored fonts
84        font = TTFont(self.getpath("I.ttf"))
85        glyphset = font.getGlyphSet(location=location)
86
87        assert isinstance(glyphset, ttGlyphSet._TTGlyphSet)
88        if location:
89            assert isinstance(glyphset, ttGlyphSet._TTVarGlyphSet)
90
91        assert list(glyphset.keys()) == [".notdef", "I"]
92
93        assert "I" in glyphset
94        assert glyphset.has_key("I")  # we should really get rid of this...
95
96        assert len(glyphset) == 2
97
98        pen = RecordingPen()
99        glyph = glyphset['I']
100
101        assert glyphset.get("foobar") is None
102
103        assert isinstance(glyph, ttGlyphSet._TTGlyph)
104        if location:
105            assert isinstance(glyph, ttGlyphSet._TTVarGlyphGlyf)
106        else:
107            assert isinstance(glyph, ttGlyphSet._TTGlyphGlyf)
108
109        glyph.draw(pen)
110        actual = pen.value
111
112        assert actual == expected, (location, actual, expected)
113