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