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 144 145 146 147 148 149 150 151
|
import os
import sys
libdir = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))), "Lib"
)
sys.path.insert(0, libdir)
from fontTools.cffLib import TopDict, PrivateDict, CharStrings, CFFFontSet
from fontTools.misc.testTools import parseXML, DataFilesHandler
from fontTools.ttLib import TTFont
import copy
import unittest
from io import BytesIO
class CffLibTest(DataFilesHandler):
def test_topDict_recalcFontBBox(self):
topDict = TopDict()
topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None)
topDict.CharStrings.fromXML(
None,
None,
parseXML(
"""
<CharString name=".notdef">
endchar
</CharString>
<CharString name="foo"><!-- [100, -100, 300, 100] -->
100 -100 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar
</CharString>
<CharString name="bar"><!-- [0, 0, 200, 200] -->
0 0 rmoveto 200 hlineto 200 vlineto -200 hlineto endchar
</CharString>
<CharString name="baz"><!-- [-55.1, -55.1, 55.1, 55.1] -->
-55.1 -55.1 rmoveto 110.2 hlineto 110.2 vlineto -110.2 hlineto endchar
</CharString>
"""
),
)
topDict.recalcFontBBox()
self.assertEqual(topDict.FontBBox, [-56, -100, 300, 200])
def test_topDict_recalcFontBBox_empty(self):
topDict = TopDict()
topDict.CharStrings = CharStrings(None, None, None, PrivateDict(), None, None)
topDict.CharStrings.fromXML(
None,
None,
parseXML(
"""
<CharString name=".notdef">
endchar
</CharString>
<CharString name="space">
123 endchar
</CharString>
"""
),
)
topDict.recalcFontBBox()
self.assertEqual(topDict.FontBBox, [0, 0, 0, 0])
def test_topDict_set_Encoding(self):
ttx_path = self.getpath("TestOTF.ttx")
font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
font.importXML(ttx_path)
topDict = font["CFF "].cff.topDictIndex[0]
encoding = [".notdef"] * 256
encoding[0x20] = "space"
topDict.Encoding = encoding
self.temp_dir()
save_path = os.path.join(self.tempdir, "TestOTF.otf")
font.save(save_path)
font2 = TTFont(save_path)
topDict2 = font2["CFF "].cff.topDictIndex[0]
self.assertEqual(topDict2.Encoding[32], "space")
def test_CFF_deepcopy(self):
"""Test that deepcopying a TTFont with a CFF table does not recurse
infinitely."""
ttx_path = os.path.join(
os.path.dirname(__file__),
"..",
"varLib",
"data",
"master_ttx_interpolatable_otf",
"TestFamily2-Master0.ttx",
)
font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
font.importXML(ttx_path)
copy.deepcopy(font)
def test_FDSelect_format_4(self):
ttx_path = self.getpath("TestFDSelect4.ttx")
font = TTFont(recalcBBoxes=False, recalcTimestamp=False)
font.importXML(ttx_path)
self.temp_dir()
save_path = os.path.join(self.tempdir, "TestOTF.otf")
font.save(save_path)
font2 = TTFont(save_path)
topDict2 = font2["CFF2"].cff.topDictIndex[0]
self.assertEqual(topDict2.FDSelect.format, 4)
self.assertEqual(topDict2.FDSelect.gidArray, [0, 0, 1])
def test_unique_glyph_names(self):
font_path = self.getpath("LinLibertine_RBI.otf")
font = TTFont(font_path, recalcBBoxes=False, recalcTimestamp=False)
glyphOrder = font.getGlyphOrder()
self.assertEqual(len(glyphOrder), len(set(glyphOrder)))
self.temp_dir()
save_path = os.path.join(self.tempdir, "TestOTF.otf")
font.save(save_path)
font2 = TTFont(save_path)
glyphOrder = font2.getGlyphOrder()
self.assertEqual(len(glyphOrder), len(set(glyphOrder)))
def test_reading_supplement_encoding(self):
cff_path = self.getpath("TestSupplementEncoding.cff")
topDict = None
with open(cff_path, "rb") as fontfile:
cff = CFFFontSet()
cff.decompile(fontfile, None)
topDict = cff[0]
self.assertEqual(topDict.Encoding[9], "space")
self.assertEqual(topDict.Encoding[32], "space")
class CFFToCFF2Test(DataFilesHandler):
def test_conversion(self):
font_path = self.getpath("CFFToCFF2-1.otf")
font = TTFont(font_path)
from fontTools.cffLib.CFFToCFF2 import convertCFFToCFF2
convertCFFToCFF2(font)
f = BytesIO()
font.save(f)
if __name__ == "__main__":
sys.exit(unittest.main())
|