• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from fontTools.ttLib import TTFont, newTable, getTableModule
2from fontTools.ttLib.tables.O_S_2f_2 import *
3import unittest
4
5
6class OS2TableTest(unittest.TestCase):
7
8	def test_getUnicodeRanges(self):
9		table = table_O_S_2f_2()
10		table.ulUnicodeRange1 = 0xFFFFFFFF
11		table.ulUnicodeRange2 = 0xFFFFFFFF
12		table.ulUnicodeRange3 = 0xFFFFFFFF
13		table.ulUnicodeRange4 = 0xFFFFFFFF
14		bits = table.getUnicodeRanges()
15		for i in range(127):
16			self.assertIn(i, bits)
17
18	def test_setUnicodeRanges(self):
19		table = table_O_S_2f_2()
20		table.ulUnicodeRange1 = 0
21		table.ulUnicodeRange2 = 0
22		table.ulUnicodeRange3 = 0
23		table.ulUnicodeRange4 = 0
24		bits = set(range(123))
25		table.setUnicodeRanges(bits)
26		self.assertEqual(table.getUnicodeRanges(), bits)
27		with self.assertRaises(ValueError):
28			table.setUnicodeRanges([-1, 127, 255])
29
30	def test_recalcUnicodeRanges(self):
31		font = TTFont()
32		font['OS/2'] = os2 = newTable('OS/2')
33		font['cmap'] = cmap = newTable('cmap')
34		st = getTableModule('cmap').CmapSubtable.newSubtable(4)
35		st.platformID, st.platEncID, st.language = 3, 1, 0
36		st.cmap = {0x0041:'A', 0x03B1: 'alpha', 0x0410: 'Acyr'}
37		cmap.tables = []
38		cmap.tables.append(st)
39		os2.setUnicodeRanges({0, 1, 9})
40		# 'pruneOnly' will clear any bits for which there's no intersection:
41		# bit 1 ('Latin 1 Supplement'), in this case. However, it won't set
42		# bit 7 ('Greek and Coptic') despite the "alpha" character is present.
43		self.assertEqual(os2.recalcUnicodeRanges(font, pruneOnly=True), {0, 9})
44		# try again with pruneOnly=False: bit 7 is now set.
45		self.assertEqual(os2.recalcUnicodeRanges(font), {0, 7, 9})
46		# add a non-BMP char from 'Mahjong Tiles' block (bit 122)
47		st.cmap[0x1F000] = 'eastwindtile'
48		# the bit 122 and the special bit 57 ('Non Plane 0') are also enabled
49		self.assertEqual(os2.recalcUnicodeRanges(font), {0, 7, 9, 57, 122})
50
51	def test_intersectUnicodeRanges(self):
52		self.assertEqual(intersectUnicodeRanges([0x0410]), {9})
53		self.assertEqual(intersectUnicodeRanges([0x0410, 0x1F000]), {9, 57, 122})
54		self.assertEqual(
55			intersectUnicodeRanges([0x0410, 0x1F000], inverse=True),
56			(set(range(123)) - {9, 57, 122}))
57
58
59if __name__ == "__main__":
60	import sys
61	sys.exit(unittest.main())
62