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