1from __future__ import print_function, division, absolute_import 2from fontTools.misc.py23 import * 3 4def _makeunicodes(f): 5 import re 6 lines = iter(f.readlines()) 7 unicodes = {} 8 for line in lines: 9 if not line: continue 10 num, name = line.split(';')[:2] 11 if name[0] == '<': continue # "<control>", etc. 12 num = int(num, 16) 13 unicodes[num] = name 14 return unicodes 15 16 17class _UnicodeCustom(object): 18 19 def __init__(self, f): 20 if isinstance(f, basestring): 21 f = open(f) 22 self.codes = _makeunicodes(f) 23 24 def __getitem__(self, charCode): 25 try: 26 return self.codes[charCode] 27 except KeyError: 28 return "????" 29 30class _UnicodeBuiltin(object): 31 32 def __getitem__(self, charCode): 33 import unicodedata 34 try: 35 return unicodedata.name(unichr(charCode)) 36 except ValueError: 37 return "????" 38 39Unicode = _UnicodeBuiltin() 40 41def setUnicodeData(f): 42 global Unicode 43 Unicode = _UnicodeCustom(f) 44