Behdad Esfahbod | ff2dd56 | 2014-01-28 16:42:24 -0500 | [diff] [blame] | 1 | from __future__ import print_function, division, absolute_import |
2 | from fontTools.misc.py23 import * | ||||
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 3 | |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 4 | def _makeunicodes(f): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 5 | import re |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 6 | lines = iter(f.readlines()) |
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 7 | unicodes = {} |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 8 | for line in lines: |
9 | if not line: continue | ||||
10 | num, name = line.split(';')[:2] | ||||
11 | if name[0] == '<': continue # "<control>", etc. | ||||
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 12 | num = int(num, 16) |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 13 | unicodes[num] = name |
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 14 | return unicodes |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 15 | |
16 | |||||
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 17 | class _UnicodeCustom(object): |
Just | 1c1d059 | 2000-03-28 10:33:58 +0000 | [diff] [blame] | 18 | |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 19 | def __init__(self, f): |
20 | if isinstance(f, basestring): | ||||
21 | f = open(f) | ||||
22 | self.codes = _makeunicodes(f) | ||||
Just | 1c1d059 | 2000-03-28 10:33:58 +0000 | [diff] [blame] | 23 | |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 24 | def __getitem__(self, charCode): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 25 | try: |
26 | return self.codes[charCode] | ||||
jvr | 13325c6 | 2004-09-25 07:47:41 +0000 | [diff] [blame] | 27 | except KeyError: |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 28 | return "????" |
29 | |||||
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 30 | class _UnicodeBuiltin(object): |
Just | 7842e56 | 1999-12-16 21:34:53 +0000 | [diff] [blame] | 31 | |
Behdad Esfahbod | fea81ee | 2014-01-15 23:33:59 +0800 | [diff] [blame] | 32 | def __getitem__(self, charCode): |
33 | import unicodedata | ||||
34 | try: | ||||
35 | return unicodedata.name(unichr(charCode)) | ||||
36 | except ValueError: | ||||
37 | return "????" | ||||
38 | |||||
39 | Unicode = _UnicodeBuiltin() | ||||
40 | |||||
41 | def setUnicodeData(f): | ||||
42 | global Unicode | ||||
43 | Unicode = _UnicodeCustom(f) |