xref: /aosp_15_r20/external/fonttools/Tests/cffLib/cffLib_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1*e1fe3e4aSElliott Hughesfrom fontTools.cffLib import TopDict, PrivateDict, CharStrings
2*e1fe3e4aSElliott Hughesfrom fontTools.misc.testTools import parseXML, DataFilesHandler
3*e1fe3e4aSElliott Hughesfrom fontTools.ttLib import TTFont
4*e1fe3e4aSElliott Hughesimport copy
5*e1fe3e4aSElliott Hughesimport os
6*e1fe3e4aSElliott Hughesimport sys
7*e1fe3e4aSElliott Hughesimport unittest
8*e1fe3e4aSElliott Hughes
9*e1fe3e4aSElliott Hughes
10*e1fe3e4aSElliott Hughesclass CffLibTest(DataFilesHandler):
11*e1fe3e4aSElliott Hughes    def test_topDict_recalcFontBBox(self):
12*e1fe3e4aSElliott Hughes        topDict = TopDict()
13*e1fe3e4aSElliott Hughes        topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None)
14*e1fe3e4aSElliott Hughes        topDict.CharStrings.fromXML(
15*e1fe3e4aSElliott Hughes            None,
16*e1fe3e4aSElliott Hughes            None,
17*e1fe3e4aSElliott Hughes            parseXML(
18*e1fe3e4aSElliott Hughes                """
19*e1fe3e4aSElliott Hughes            <CharString name=".notdef">
20*e1fe3e4aSElliott Hughes              endchar
21*e1fe3e4aSElliott Hughes            </CharString>
22*e1fe3e4aSElliott Hughes            <CharString name="foo"><!-- [100, -100, 300, 100] -->
23*e1fe3e4aSElliott Hughes              100 -100 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar
24*e1fe3e4aSElliott Hughes            </CharString>
25*e1fe3e4aSElliott Hughes            <CharString name="bar"><!-- [0, 0, 200, 200] -->
26*e1fe3e4aSElliott Hughes              0 0 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar
27*e1fe3e4aSElliott Hughes            </CharString>
28*e1fe3e4aSElliott Hughes            <CharString name="baz"><!-- [-55.1, -55.1, 55.1, 55.1] -->
29*e1fe3e4aSElliott Hughes              -55.1 -55.1 rmoveto 110.2 hlineto 110.2 vlineto -110.2 hlineto endchar
30*e1fe3e4aSElliott Hughes            </CharString>
31*e1fe3e4aSElliott Hughes        """
32*e1fe3e4aSElliott Hughes            ),
33*e1fe3e4aSElliott Hughes        )
34*e1fe3e4aSElliott Hughes
35*e1fe3e4aSElliott Hughes        topDict.recalcFontBBox()
36*e1fe3e4aSElliott Hughes        self.assertEqual(topDict.FontBBox, [-56, -100, 300, 200])
37*e1fe3e4aSElliott Hughes
38*e1fe3e4aSElliott Hughes    def test_topDict_recalcFontBBox_empty(self):
39*e1fe3e4aSElliott Hughes        topDict = TopDict()
40*e1fe3e4aSElliott Hughes        topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None)
41*e1fe3e4aSElliott Hughes        topDict.CharStrings.fromXML(
42*e1fe3e4aSElliott Hughes            None,
43*e1fe3e4aSElliott Hughes            None,
44*e1fe3e4aSElliott Hughes            parseXML(
45*e1fe3e4aSElliott Hughes                """
46*e1fe3e4aSElliott Hughes            <CharString name=".notdef">
47*e1fe3e4aSElliott Hughes              endchar
48*e1fe3e4aSElliott Hughes            </CharString>
49*e1fe3e4aSElliott Hughes            <CharString name="space">
50*e1fe3e4aSElliott Hughes              123 endchar
51*e1fe3e4aSElliott Hughes            </CharString>
52*e1fe3e4aSElliott Hughes        """
53*e1fe3e4aSElliott Hughes            ),
54*e1fe3e4aSElliott Hughes        )
55*e1fe3e4aSElliott Hughes
56*e1fe3e4aSElliott Hughes        topDict.recalcFontBBox()
57*e1fe3e4aSElliott Hughes        self.assertEqual(topDict.FontBBox, [0, 0, 0, 0])
58*e1fe3e4aSElliott Hughes
59*e1fe3e4aSElliott Hughes    def test_topDict_set_Encoding(self):
60*e1fe3e4aSElliott Hughes        ttx_path = self.getpath("TestOTF.ttx")
61*e1fe3e4aSElliott Hughes        font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
62*e1fe3e4aSElliott Hughes        font.importXML(ttx_path)
63*e1fe3e4aSElliott Hughes
64*e1fe3e4aSElliott Hughes        topDict = font["CFF "].cff.topDictIndex[0]
65*e1fe3e4aSElliott Hughes        encoding = [".notdef"] * 256
66*e1fe3e4aSElliott Hughes        encoding[0x20] = "space"
67*e1fe3e4aSElliott Hughes        topDict.Encoding = encoding
68*e1fe3e4aSElliott Hughes
69*e1fe3e4aSElliott Hughes        self.temp_dir()
70*e1fe3e4aSElliott Hughes        save_path = os.path.join(self.tempdir, "TestOTF.otf")
71*e1fe3e4aSElliott Hughes        font.save(save_path)
72*e1fe3e4aSElliott Hughes
73*e1fe3e4aSElliott Hughes        font2 = TTFont(save_path)
74*e1fe3e4aSElliott Hughes        topDict2 = font2["CFF "].cff.topDictIndex[0]
75*e1fe3e4aSElliott Hughes        self.assertEqual(topDict2.Encoding[32], "space")
76*e1fe3e4aSElliott Hughes
77*e1fe3e4aSElliott Hughes    def test_CFF_deepcopy(self):
78*e1fe3e4aSElliott Hughes        """Test that deepcopying a TTFont with a CFF table does not recurse
79*e1fe3e4aSElliott Hughes        infinitely."""
80*e1fe3e4aSElliott Hughes        ttx_path = os.path.join(
81*e1fe3e4aSElliott Hughes            os.path.dirname(__file__),
82*e1fe3e4aSElliott Hughes            "..",
83*e1fe3e4aSElliott Hughes            "varLib",
84*e1fe3e4aSElliott Hughes            "data",
85*e1fe3e4aSElliott Hughes            "master_ttx_interpolatable_otf",
86*e1fe3e4aSElliott Hughes            "TestFamily2-Master0.ttx",
87*e1fe3e4aSElliott Hughes        )
88*e1fe3e4aSElliott Hughes        font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
89*e1fe3e4aSElliott Hughes        font.importXML(ttx_path)
90*e1fe3e4aSElliott Hughes        copy.deepcopy(font)
91*e1fe3e4aSElliott Hughes
92*e1fe3e4aSElliott Hughes    def test_FDSelect_format_4(self):
93*e1fe3e4aSElliott Hughes        ttx_path = self.getpath("TestFDSelect4.ttx")
94*e1fe3e4aSElliott Hughes        font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
95*e1fe3e4aSElliott Hughes        font.importXML(ttx_path)
96*e1fe3e4aSElliott Hughes
97*e1fe3e4aSElliott Hughes        self.temp_dir()
98*e1fe3e4aSElliott Hughes        save_path = os.path.join(self.tempdir, "TestOTF.otf")
99*e1fe3e4aSElliott Hughes        font.save(save_path)
100*e1fe3e4aSElliott Hughes
101*e1fe3e4aSElliott Hughes        font2 = TTFont(save_path)
102*e1fe3e4aSElliott Hughes        topDict2 = font2["CFF2"].cff.topDictIndex[0]
103*e1fe3e4aSElliott Hughes        self.assertEqual(topDict2.FDSelect.format, 4)
104*e1fe3e4aSElliott Hughes        self.assertEqual(topDict2.FDSelect.gidArray, [0, 0, 1])
105*e1fe3e4aSElliott Hughes
106*e1fe3e4aSElliott Hughes    def test_unique_glyph_names(self):
107*e1fe3e4aSElliott Hughes        font_path = self.getpath("LinLibertine_RBI.otf")
108*e1fe3e4aSElliott Hughes        font = TTFont(font_path, recalcBBoxes=False, recalcTimestamp=False)
109*e1fe3e4aSElliott Hughes
110*e1fe3e4aSElliott Hughes        glyphOrder = font.getGlyphOrder()
111*e1fe3e4aSElliott Hughes        self.assertEqual(len(glyphOrder), len(set(glyphOrder)))
112*e1fe3e4aSElliott Hughes
113*e1fe3e4aSElliott Hughes        self.temp_dir()
114*e1fe3e4aSElliott Hughes        save_path = os.path.join(self.tempdir, "TestOTF.otf")
115*e1fe3e4aSElliott Hughes        font.save(save_path)
116*e1fe3e4aSElliott Hughes
117*e1fe3e4aSElliott Hughes        font2 = TTFont(save_path)
118*e1fe3e4aSElliott Hughes        glyphOrder = font2.getGlyphOrder()
119*e1fe3e4aSElliott Hughes        self.assertEqual(len(glyphOrder), len(set(glyphOrder)))
120*e1fe3e4aSElliott Hughes
121*e1fe3e4aSElliott Hughes
122*e1fe3e4aSElliott Hughesif __name__ == "__main__":
123*e1fe3e4aSElliott Hughes    sys.exit(unittest.main())
124