summaryrefslogtreecommitdiff
path: root/Build/source/libs/harfbuzz/harfbuzz-src/src/gen-arabic-joining-list.py
blob: 7ec7425fd8cc79cbe168623e40c8de59b9242a8a (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3

"""usage: ./gen-arabic-joining-table.py ArabicShaping.txt Scripts.txt

Input files:
* https://unicode.org/Public/UCD/latest/ucd/ArabicShaping.txt
* https://unicode.org/Public/UCD/latest/ucd/Scripts.txt
"""

import os.path, sys

if len (sys.argv) != 3:
	sys.exit (__doc__)

files = [open (x, encoding='utf-8') for x in sys.argv[1:]]

headers = [[f.readline (), f.readline ()] for f in files]
while files[0].readline ().find ('##################') < 0:
	pass

def read (f):
	mapping = {}
	for line in f:

		j = line.find ('#')
		if j >= 0:
			line = line[:j]

		fields = [x.strip () for x in line.split (';')]
		if len (fields) == 1:
			continue

		uu = fields[0].split ('..')
		start = int (uu[0], 16)
		if len (uu) == 1:
			end = start
		else:
			end = int (uu[1], 16)

		t = fields[1]

		for u in range (start, end + 1):
			mapping[u] = t

	return mapping

def read_joining_uu (f):
	values = set ()
	for line in f:

		if line[0] == '#':
			continue

		fields = [x.strip () for x in line.split (';')]
		if len (fields) == 1:
			continue
		if fields[2] in {'T', 'U'}:
			continue

		values.add (int (fields[0], 16))

	return sorted (values)

def print_has_arabic_joining (scripts, joining_uu):

	print ("static bool")
	print ("has_arabic_joining (hb_script_t script)")
	print ("{")
	print ("  /* List of scripts that have data in arabic-table. */")
	print ("  switch ((int) script)")
	print ("  {")

	for script in sorted ({scripts[u] for u in joining_uu if scripts[u] not in {'Common', 'Inherited'}}):
		print ("    case HB_SCRIPT_{}:".format (script.upper ()))

	print ("      return true;")
	print ()
	print ("    default:")
	print ("      return false;")
	print ("  }")
	print ("}")
	print ()

print ("/* == Start of generated function == */")
print ("/*")
print (" * The following function is generated by running:")
print (" *")
print (" *   ./gen-arabic-joining-list.py ArabicShaping.txt Scripts.txt")
print (" *")
print (" * on files with these headers:")
print (" *")
for h in headers:
	for l in h:
		print (" * %s" % (l.strip ()))
print (" */")
print ()
print ("#ifndef HB_OT_SHAPER_ARABIC_JOINING_LIST_HH")
print ("#define HB_OT_SHAPER_ARABIC_JOINING_LIST_HH")
print ()

print_has_arabic_joining (read (files[1]), read_joining_uu (files[0]))

print ()
print ("#endif /* HB_OT_SHAPER_ARABIC_JOINING_LIST_HH */")
print ()
print ("/* == End of generated function == */")