• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from io import BytesIO
2import sys
3import os
4import tempfile
5import unittest
6from fontTools.misc.textTools import deHexStr
7from fontTools.misc.macRes import ResourceReader
8
9
10# test resource data in DeRez notation
11"""
12data 'TEST' (128, "name1") { $"4865 6C6C 6F" };                   /* Hello */
13data 'TEST' (129, "name2") { $"576F 726C 64" };                   /* World */
14data 'test' (130, "name3") { $"486F 7720 6172 6520 796F 753F" };  /* How are you? */
15"""
16# the same data, compiled using Rez
17# $ /usr/bin/Rez testdata.rez -o compiled
18# $ hexdump -v compiled/..namedfork/rsrc
19TEST_RSRC_FORK = deHexStr(
20	"00 00 01 00 00 00 01 22 00 00 00 22 00 00 00 64 "  # 0x00000000
21	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000010
22	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000020
23	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000030
24	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000040
25	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000050
26	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000060
27	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000070
28	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000080
29	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000090
30	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x000000A0
31	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x000000B0
32	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x000000C0
33	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x000000D0
34	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x000000E0
35	"00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x000000F0
36	"00 00 00 05 48 65 6c 6c 6f 00 00 00 05 57 6f 72 "  # 0x00000100
37	"6c 64 00 00 00 0c 48 6f 77 20 61 72 65 20 79 6f "  # 0x00000110
38	"75 3f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "  # 0x00000120
39	"00 00 00 00 00 00 00 00 00 00 00 1c 00 52 00 01 "  # 0x00000130
40	"54 45 53 54 00 01 00 12 74 65 73 74 00 00 00 2a "  # 0x00000140
41	"00 80 00 00 00 00 00 00 00 00 00 00 00 81 00 06 "  # 0x00000150
42	"00 00 00 09 00 00 00 00 00 82 00 0c 00 00 00 12 "  # 0x00000160
43	"00 00 00 00 05 6e 61 6d 65 31 05 6e 61 6d 65 32 "  # 0x00000170
44	"05 6e 61 6d 65 33                               "  # 0x00000180
45)
46
47
48class ResourceReaderTest(unittest.TestCase):
49
50	def test_read_file(self):
51		infile = BytesIO(TEST_RSRC_FORK)
52		reader = ResourceReader(infile)
53		resources = [res for typ in reader.keys() for res in reader[typ]]
54		self.assertExpected(resources)
55
56	def test_read_datafork(self):
57		with tempfile.NamedTemporaryFile(delete=False) as tmp:
58			tmp.write(TEST_RSRC_FORK)
59		try:
60			reader = ResourceReader(tmp.name)
61			resources = [res for typ in reader.keys() for res in reader[typ]]
62			reader.close()
63			self.assertExpected(resources)
64		finally:
65			os.remove(tmp.name)
66
67	def test_read_namedfork_rsrc(self):
68		if sys.platform != 'darwin':
69			self.skipTest('Not supported on "%s"' % sys.platform)
70		tmp = tempfile.NamedTemporaryFile(delete=False)
71		tmp.close()
72		try:
73			with open(tmp.name + '/..namedfork/rsrc', 'wb') as fork:
74				fork.write(TEST_RSRC_FORK)
75			reader = ResourceReader(tmp.name)
76			resources = [res for typ in reader.keys() for res in reader[typ]]
77			reader.close()
78			self.assertExpected(resources)
79		finally:
80			os.remove(tmp.name)
81
82	def assertExpected(self, resources):
83		self.assertRezEqual(resources[0], 'TEST', b'Hello', 128, 'name1')
84		self.assertRezEqual(resources[1], 'TEST', b'World', 129, 'name2')
85		self.assertRezEqual(
86			resources[2], 'test', b'How are you?', 130, 'name3')
87
88	def assertRezEqual(self, res, type_, data, id, name):
89		self.assertEqual(res.type, type_)
90		self.assertEqual(res.data, data)
91		self.assertEqual(res.id, id)
92		self.assertEqual(res.name, name)
93
94
95if __name__ == '__main__':
96	sys.exit(unittest.main())
97