1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3import unittest
4import os
5import sys
6from fontTools import t1Lib
7from fontTools.pens.basePen import NullPen
8import random
9
10
11CWD = os.path.abspath(os.path.dirname(__file__))
12DATADIR = os.path.join(CWD, 'data')
13# I used `tx` to convert PFA to LWFN (stored in the data fork)
14LWFN = os.path.join(DATADIR, 'TestT1-Regular.lwfn')
15PFA = os.path.join(DATADIR, 'TestT1-Regular.pfa')
16PFB = os.path.join(DATADIR, 'TestT1-Regular.pfb')
17
18
19class FindEncryptedChunksTest(unittest.TestCase):
20
21	def test_findEncryptedChunks(self):
22		with open(PFA, "rb") as f:
23			data = f.read()
24		chunks = t1Lib.findEncryptedChunks(data)
25		self.assertEqual(len(chunks), 3)
26		self.assertFalse(chunks[0][0])
27		# the second chunk is encrypted
28		self.assertTrue(chunks[1][0])
29		self.assertFalse(chunks[2][0])
30
31
32class DecryptType1Test(unittest.TestCase):
33
34	def test_decryptType1(self):
35		with open(PFA, "rb") as f:
36			data = f.read()
37		decrypted = t1Lib.decryptType1(data)
38		self.assertNotEqual(decrypted, data)
39
40
41class ReadWriteTest(unittest.TestCase):
42
43	def test_read_pfa_write_pfb(self):
44		font = t1Lib.T1Font(PFA)
45		data = self.write(font, 'PFB')
46		self.assertEqual(font.getData(), data)
47
48	def test_read_pfb_write_pfa(self):
49		font = t1Lib.T1Font(PFB)
50		# 'OTHER' == 'PFA'
51		data = self.write(font, 'OTHER', dohex=True)
52		self.assertEqual(font.getData(), data)
53
54	@unittest.skipIf(sys.version_info[:2] < (3, 6), "pathlib is only tested on 3.6 and up")
55	def test_read_with_path(self):
56		import pathlib
57		font = t1Lib.T1Font(pathlib.Path(PFB))
58
59	@staticmethod
60	def write(font, outtype, dohex=False):
61		temp = os.path.join(DATADIR, 'temp.' + outtype.lower())
62		try:
63			font.saveAs(temp, outtype, dohex=dohex)
64			newfont = t1Lib.T1Font(temp)
65			data = newfont.getData()
66		finally:
67			if os.path.exists(temp):
68				os.remove(temp)
69		return data
70
71
72class T1FontTest(unittest.TestCase):
73
74	def test_parse_lwfn(self):
75		# the extended attrs are lost on git so we can't auto-detect 'LWFN'
76		font = t1Lib.T1Font(LWFN, kind="LWFN")
77		font.parse()
78		self.assertEqual(font['FontName'], 'TestT1-Regular')
79		self.assertTrue('Subrs' in font['Private'])
80
81	def test_parse_pfa(self):
82		font = t1Lib.T1Font(PFA)
83		font.parse()
84		self.assertEqual(font['FontName'], 'TestT1-Regular')
85		self.assertTrue('Subrs' in font['Private'])
86
87	def test_parse_pfb(self):
88		font = t1Lib.T1Font(PFB)
89		font.parse()
90		self.assertEqual(font['FontName'], 'TestT1-Regular')
91		self.assertTrue('Subrs' in font['Private'])
92
93	def test_getGlyphSet(self):
94		font = t1Lib.T1Font(PFA)
95		glyphs = font.getGlyphSet()
96		i = random.randrange(len(glyphs))
97		aglyph = list(glyphs.values())[i]
98		self.assertTrue(hasattr(aglyph, 'draw'))
99		self.assertFalse(hasattr(aglyph, 'width'))
100		aglyph.draw(NullPen())
101		self.assertTrue(hasattr(aglyph, 'width'))
102
103
104if __name__ == '__main__':
105	import sys
106	sys.exit(unittest.main())
107