blob: b5990516b644ea51a349e91b8564ba74e4a5f962 [file] [log] [blame]
Behdad Esfahbodff2dd562014-01-28 16:42:24 -05001from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
Just7842e561999-12-16 21:34:53 +00003
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +08004def _makeunicodes(f):
Just7842e561999-12-16 21:34:53 +00005 import re
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +08006 lines = iter(f.readlines())
jvr13325c62004-09-25 07:47:41 +00007 unicodes = {}
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +08008 for line in lines:
9 if not line: continue
10 num, name = line.split(';')[:2]
11 if name[0] == '<': continue # "<control>", etc.
jvr13325c62004-09-25 07:47:41 +000012 num = int(num, 16)
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080013 unicodes[num] = name
jvr13325c62004-09-25 07:47:41 +000014 return unicodes
Just7842e561999-12-16 21:34:53 +000015
16
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080017class _UnicodeCustom(object):
Just1c1d0592000-03-28 10:33:58 +000018
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080019 def __init__(self, f):
20 if isinstance(f, basestring):
21 f = open(f)
22 self.codes = _makeunicodes(f)
Just1c1d0592000-03-28 10:33:58 +000023
Just7842e561999-12-16 21:34:53 +000024 def __getitem__(self, charCode):
Just7842e561999-12-16 21:34:53 +000025 try:
26 return self.codes[charCode]
jvr13325c62004-09-25 07:47:41 +000027 except KeyError:
Just7842e561999-12-16 21:34:53 +000028 return "????"
29
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080030class _UnicodeBuiltin(object):
Just7842e561999-12-16 21:34:53 +000031
Behdad Esfahbodfea81ee2014-01-15 23:33:59 +080032 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)