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