aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/unicode.py
blob: 23589db7bd97d8cd7d1b97e57b3773a320388785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from fontTools.misc.py23 import *

def _makeunicodes(f):
	import re
	lines = iter(f.readlines())
	unicodes = {}
	for line in lines:
		if not line: continue
		num, name = line.split(';')[:2]
		if name[0] == '<': continue # "<control>", etc.
		num = int(num, 16)
		unicodes[num] = name
	return unicodes


class _UnicodeCustom(object):

	def __init__(self, f):
		if isinstance(f, basestring):
			with open(f) as fd:
				codes = _makeunicodes(fd)
		else:
			codes = _makeunicodes(f)
		self.codes = codes

	def __getitem__(self, charCode):
		try:
			return self.codes[charCode]
		except KeyError:
			return "????"

class _UnicodeBuiltin(object):

	def __getitem__(self, charCode):
		try:
			# use unicodedata backport to python2, if available:
			# https://github.com/mikekap/unicodedata2
			import unicodedata2 as unicodedata
		except ImportError: 
			import unicodedata
		try:
			return unicodedata.name(unichr(charCode))
		except ValueError:
			return "????"

Unicode = _UnicodeBuiltin()

def setUnicodeData(f):
	global Unicode
	Unicode = _UnicodeCustom(f)