• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import os
2import shutil
3import unittest
4import tempfile
5from io import open
6from fontTools.ufoLib import UFOReader, UFOWriter, UFOLibError
7from fontTools.ufoLib import plistlib
8from .testSupport import fontInfoVersion1, fontInfoVersion2
9
10
11class TestInfoObject: pass
12
13
14class ReadFontInfoVersion1TestCase(unittest.TestCase):
15
16	def setUp(self):
17		self.dstDir = tempfile.mktemp()
18		os.mkdir(self.dstDir)
19		metaInfo = {
20			"creator": "test",
21			"formatVersion": 1
22		}
23		path = os.path.join(self.dstDir, "metainfo.plist")
24		with open(path, "wb") as f:
25			plistlib.dump(metaInfo, f)
26
27	def tearDown(self):
28		shutil.rmtree(self.dstDir)
29
30	def _writeInfoToPlist(self, info):
31		path = os.path.join(self.dstDir, "fontinfo.plist")
32		with open(path, "wb") as f:
33			plistlib.dump(info, f)
34
35	def testRead(self):
36		originalData = dict(fontInfoVersion1)
37		self._writeInfoToPlist(originalData)
38		infoObject = TestInfoObject()
39		reader = UFOReader(self.dstDir, validate=True)
40		reader.readInfo(infoObject)
41		for attr in dir(infoObject):
42			if attr not in fontInfoVersion2:
43				continue
44			originalValue = fontInfoVersion2[attr]
45			readValue = getattr(infoObject, attr)
46			self.assertEqual(originalValue, readValue)
47
48	def testFontStyleConversion(self):
49		fontStyle1To2 = {
50			64 : "regular",
51			1  : "italic",
52			32 : "bold",
53			33 : "bold italic"
54		}
55		for old, new in list(fontStyle1To2.items()):
56			info = dict(fontInfoVersion1)
57			info["fontStyle"] = old
58			self._writeInfoToPlist(info)
59			reader = UFOReader(self.dstDir, validate=True)
60			infoObject = TestInfoObject()
61			reader.readInfo(infoObject)
62			self.assertEqual(new, infoObject.styleMapStyleName)
63
64	def testWidthNameConversion(self):
65		widthName1To2 = {
66			"Ultra-condensed" : 1,
67			"Extra-condensed" : 2,
68			"Condensed"		  : 3,
69			"Semi-condensed"  : 4,
70			"Medium (normal)" : 5,
71			"Semi-expanded"	  : 6,
72			"Expanded"		  : 7,
73			"Extra-expanded"  : 8,
74			"Ultra-expanded"  : 9
75		}
76		for old, new in list(widthName1To2.items()):
77			info = dict(fontInfoVersion1)
78			info["widthName"] = old
79			self._writeInfoToPlist(info)
80			reader = UFOReader(self.dstDir, validate=True)
81			infoObject = TestInfoObject()
82			reader.readInfo(infoObject)
83			self.assertEqual(new, infoObject.openTypeOS2WidthClass)
84
85
86class WriteFontInfoVersion1TestCase(unittest.TestCase):
87
88	def setUp(self):
89		self.tempDir = tempfile.mktemp()
90		os.mkdir(self.tempDir)
91		self.dstDir = os.path.join(self.tempDir, "test.ufo")
92
93	def tearDown(self):
94		shutil.rmtree(self.tempDir)
95
96	def makeInfoObject(self):
97		infoObject = TestInfoObject()
98		for attr, value in list(fontInfoVersion2.items()):
99			setattr(infoObject, attr, value)
100		return infoObject
101
102	def readPlist(self):
103		path = os.path.join(self.dstDir, "fontinfo.plist")
104		with open(path, "rb") as f:
105			plist = plistlib.load(f)
106		return plist
107
108	def testWrite(self):
109		infoObject = self.makeInfoObject()
110		writer = UFOWriter(self.dstDir, formatVersion=1)
111		writer.writeInfo(infoObject)
112		writtenData = self.readPlist()
113		for attr, originalValue in list(fontInfoVersion1.items()):
114			newValue = writtenData[attr]
115			self.assertEqual(newValue, originalValue)
116
117	def testFontStyleConversion(self):
118		fontStyle1To2 = {
119			64 : "regular",
120			1  : "italic",
121			32 : "bold",
122			33 : "bold italic"
123		}
124		for old, new in list(fontStyle1To2.items()):
125			infoObject = self.makeInfoObject()
126			infoObject.styleMapStyleName = new
127			writer = UFOWriter(self.dstDir, formatVersion=1)
128			writer.writeInfo(infoObject)
129			writtenData = self.readPlist()
130			self.assertEqual(writtenData["fontStyle"], old)
131
132	def testWidthNameConversion(self):
133		widthName1To2 = {
134			"Ultra-condensed" : 1,
135			"Extra-condensed" : 2,
136			"Condensed"		  : 3,
137			"Semi-condensed"  : 4,
138			"Medium (normal)" : 5,
139			"Semi-expanded"	  : 6,
140			"Expanded"		  : 7,
141			"Extra-expanded"  : 8,
142			"Ultra-expanded"  : 9
143		}
144		for old, new in list(widthName1To2.items()):
145			infoObject = self.makeInfoObject()
146			infoObject.openTypeOS2WidthClass = new
147			writer = UFOWriter(self.dstDir, formatVersion=1)
148			writer.writeInfo(infoObject)
149			writtenData = self.readPlist()
150			self.assertEqual(writtenData["widthName"], old)
151