xref: /aosp_15_r20/external/fonttools/Tests/ttLib/ttVisitor_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.ttLib import TTFont
2*e1fe3e4aSElliott Hughesfrom fontTools.ttLib.ttVisitor import TTVisitor
3*e1fe3e4aSElliott Hughesimport os
4*e1fe3e4aSElliott Hughesimport pytest
5*e1fe3e4aSElliott Hughes
6*e1fe3e4aSElliott Hughes
7*e1fe3e4aSElliott Hughesclass TestVisitor(TTVisitor):
8*e1fe3e4aSElliott Hughes    def __init__(self):
9*e1fe3e4aSElliott Hughes        self.value = []
10*e1fe3e4aSElliott Hughes        self.depth = 0
11*e1fe3e4aSElliott Hughes
12*e1fe3e4aSElliott Hughes    def _add(self, s):
13*e1fe3e4aSElliott Hughes        self.value.append(s)
14*e1fe3e4aSElliott Hughes
15*e1fe3e4aSElliott Hughes    def visit(self, obj, target_depth):
16*e1fe3e4aSElliott Hughes        if self.depth == target_depth:
17*e1fe3e4aSElliott Hughes            self._add(obj)
18*e1fe3e4aSElliott Hughes        self.depth += 1
19*e1fe3e4aSElliott Hughes        super().visit(obj, target_depth)
20*e1fe3e4aSElliott Hughes        self.depth -= 1
21*e1fe3e4aSElliott Hughes
22*e1fe3e4aSElliott Hughes
23*e1fe3e4aSElliott Hughesclass TTVisitorTest(object):
24*e1fe3e4aSElliott Hughes    @staticmethod
25*e1fe3e4aSElliott Hughes    def getpath(testfile):
26*e1fe3e4aSElliott Hughes        path = os.path.dirname(__file__)
27*e1fe3e4aSElliott Hughes        return os.path.join(path, "data", testfile)
28*e1fe3e4aSElliott Hughes
29*e1fe3e4aSElliott Hughes    def test_ttvisitor(self):
30*e1fe3e4aSElliott Hughes        font = TTFont(self.getpath("TestVGID-Regular.otf"))
31*e1fe3e4aSElliott Hughes        visitor = TestVisitor()
32*e1fe3e4aSElliott Hughes
33*e1fe3e4aSElliott Hughes        # Count number of objects at depth 1:
34*e1fe3e4aSElliott Hughes        # That is, number of font tables, including GlyphOrder.
35*e1fe3e4aSElliott Hughes        visitor.visit(font, 1)
36*e1fe3e4aSElliott Hughes
37*e1fe3e4aSElliott Hughes        assert len(visitor.value) == 14
38