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 with open(f) as fd: 22 codes = _makeunicodes(fd) 23 else: 24 codes = _makeunicodes(f) 25 self.codes = codes 26 27 def __getitem__(self, charCode): 28 try: 29 return self.codes[charCode] 30 except KeyError: 31 return "????" 32 33class _UnicodeBuiltin(object): 34 35 def __getitem__(self, charCode): 36 try: 37 # use unicodedata backport to python2, if available: 38 # https://github.com/mikekap/unicodedata2 39 import unicodedata2 as unicodedata 40 except ImportError: 41 import unicodedata 42 try: 43 return unicodedata.name(unichr(charCode)) 44 except ValueError: 45 return "????" 46 47Unicode = _UnicodeBuiltin() 48 49def setUnicodeData(f): 50 global Unicode 51 Unicode = _UnicodeCustom(f) 52