2020-02-19 12:26:55 +01:00
|
|
|
#!/usr/bin/env python3
|
2010-10-06 00:36:58 +02:00
|
|
|
|
2020-05-28 12:31:15 +02:00
|
|
|
"""usage: ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt
|
2020-02-10 14:49:23 +01:00
|
|
|
|
2020-03-15 20:59:31 +01:00
|
|
|
Input files:
|
2020-02-10 14:49:23 +01:00
|
|
|
* https://unicode.org/Public/UCD/latest/ucd/ArabicShaping.txt
|
|
|
|
* https://unicode.org/Public/UCD/latest/ucd/UnicodeData.txt
|
|
|
|
* https://unicode.org/Public/UCD/latest/ucd/Blocks.txt
|
2020-05-28 12:31:15 +02:00
|
|
|
"""
|
|
|
|
|
2020-05-28 21:41:19 +02:00
|
|
|
import os.path, sys
|
2020-05-28 12:31:15 +02:00
|
|
|
|
|
|
|
if len (sys.argv) != 4:
|
2020-05-28 20:21:29 +02:00
|
|
|
sys.exit (__doc__)
|
2011-06-02 02:52:00 +02:00
|
|
|
|
2020-05-28 21:41:19 +02:00
|
|
|
files = [open (x, encoding='utf-8') for x in sys.argv[1:]]
|
2011-06-02 02:52:00 +02:00
|
|
|
|
2014-06-20 22:17:42 +02:00
|
|
|
headers = [[files[0].readline (), files[0].readline ()], [files[2].readline (), files[2].readline ()]]
|
2012-04-10 22:25:08 +02:00
|
|
|
headers.append (["UnicodeData.txt does not have a header."])
|
|
|
|
while files[0].readline ().find ('##################') < 0:
|
2010-12-21 20:18:24 +01:00
|
|
|
pass
|
2010-10-06 00:36:58 +02:00
|
|
|
|
2014-06-20 22:17:42 +02:00
|
|
|
blocks = {}
|
|
|
|
def read_blocks(f):
|
|
|
|
global blocks
|
|
|
|
for line in f:
|
2010-11-17 22:52:58 +01:00
|
|
|
|
2014-06-20 22:17:42 +02:00
|
|
|
j = line.find ('#')
|
|
|
|
if j >= 0:
|
|
|
|
line = line[:j]
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2014-06-20 22:17:42 +02:00
|
|
|
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)
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2014-06-20 22:17:42 +02:00
|
|
|
t = fields[1]
|
|
|
|
|
|
|
|
for u in range (start, end + 1):
|
|
|
|
blocks[u] = t
|
|
|
|
|
|
|
|
def print_joining_table(f):
|
|
|
|
|
|
|
|
values = {}
|
2012-04-10 22:25:08 +02:00
|
|
|
for line in f:
|
|
|
|
|
|
|
|
if line[0] == '#':
|
|
|
|
continue
|
|
|
|
|
|
|
|
fields = [x.strip () for x in line.split (';')]
|
|
|
|
if len (fields) == 1:
|
|
|
|
continue
|
|
|
|
|
|
|
|
u = int (fields[0], 16)
|
|
|
|
|
|
|
|
if fields[3] in ["ALAPH", "DALATH RISH"]:
|
|
|
|
value = "JOINING_GROUP_" + fields[3].replace(' ', '_')
|
|
|
|
else:
|
|
|
|
value = "JOINING_TYPE_" + fields[2]
|
2014-06-20 22:30:10 +02:00
|
|
|
values[u] = value
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2014-06-20 22:20:59 +02:00
|
|
|
short_value = {}
|
2020-03-15 20:59:31 +01:00
|
|
|
for value in sorted (set ([v for v in values.values ()] + ['JOINING_TYPE_X'])):
|
2014-06-20 22:20:59 +02:00
|
|
|
short = ''.join(x[0] for x in value.split('_')[2:])
|
|
|
|
assert short not in short_value.values()
|
|
|
|
short_value[value] = short
|
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
2014-06-20 22:20:59 +02:00
|
|
|
for value,short in short_value.items():
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("#define %s %s" % (short, value))
|
2014-06-20 22:20:59 +02:00
|
|
|
|
2014-06-20 23:59:43 +02:00
|
|
|
uu = sorted(values.keys())
|
2014-06-20 22:17:42 +02:00
|
|
|
num = len(values)
|
2014-06-21 22:07:00 +02:00
|
|
|
all_blocks = set([blocks[u] for u in uu])
|
2014-06-20 23:59:43 +02:00
|
|
|
|
2014-06-22 19:29:59 +02:00
|
|
|
last = -100000
|
2014-06-20 23:59:43 +02:00
|
|
|
ranges = []
|
|
|
|
for u in uu:
|
2014-06-21 22:07:00 +02:00
|
|
|
if u - last <= 1+16*5:
|
2014-06-20 23:59:43 +02:00
|
|
|
ranges[-1][-1] = u
|
|
|
|
else:
|
|
|
|
ranges.append([u,u])
|
|
|
|
last = u
|
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print ("static const uint8_t joining_table[] =")
|
|
|
|
print ("{")
|
2014-06-20 22:17:42 +02:00
|
|
|
last_block = None
|
2014-06-20 23:59:43 +02:00
|
|
|
offset = 0
|
|
|
|
for start,end in ranges:
|
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print ("#define joining_offset_0x%04xu %d" % (start, offset))
|
2014-06-20 23:59:43 +02:00
|
|
|
|
|
|
|
for u in range(start, end+1):
|
|
|
|
|
|
|
|
block = blocks.get(u, last_block)
|
|
|
|
value = values.get(u, "JOINING_TYPE_X")
|
2014-06-20 22:30:10 +02:00
|
|
|
|
2014-06-20 23:59:43 +02:00
|
|
|
if block != last_block or u == start:
|
|
|
|
if u != start:
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
2014-06-21 22:07:00 +02:00
|
|
|
if block in all_blocks:
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("\n /* %s */" % block)
|
2014-06-21 22:07:00 +02:00
|
|
|
else:
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("\n /* FILLER */")
|
2014-06-20 23:59:43 +02:00
|
|
|
last_block = block
|
|
|
|
if u % 32 != 0:
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print (" /* %04X */" % (u//32*32), " " * (u % 32), end="")
|
2014-06-20 22:30:10 +02:00
|
|
|
|
2014-06-20 23:59:43 +02:00
|
|
|
if u % 32 == 0:
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print (" /* %04X */ " % u, end="")
|
|
|
|
print ("%s," % short_value[value], end="")
|
|
|
|
print ()
|
2014-06-20 22:30:10 +02:00
|
|
|
|
2014-06-20 23:59:43 +02:00
|
|
|
offset += end - start + 1
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
2014-06-20 23:59:43 +02:00
|
|
|
occupancy = num * 100. / offset
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
|
|
|
|
print ()
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2020-05-28 12:31:15 +02:00
|
|
|
page_bits = 12
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print ("static unsigned int")
|
|
|
|
print ("joining_type (hb_codepoint_t u)")
|
|
|
|
print ("{")
|
|
|
|
print (" switch (u >> %d)" % page_bits)
|
|
|
|
print (" {")
|
2014-06-20 23:59:43 +02:00
|
|
|
pages = set([u>>page_bits for u in [s for s,e in ranges]+[e for s,e in ranges]])
|
|
|
|
for p in sorted(pages):
|
2018-03-29 10:18:47 +02:00
|
|
|
print (" case 0x%0Xu:" % p)
|
2014-06-20 23:59:43 +02:00
|
|
|
for (start,end) in ranges:
|
|
|
|
if p not in [start>>page_bits, end>>page_bits]: continue
|
2014-07-11 21:05:36 +02:00
|
|
|
offset = "joining_offset_0x%04xu" % start
|
2018-03-29 10:18:47 +02:00
|
|
|
print (" if (hb_in_range<hb_codepoint_t> (u, 0x%04Xu, 0x%04Xu)) return joining_table[u - 0x%04Xu + %s];" % (start, end, start, offset))
|
|
|
|
print (" break;")
|
|
|
|
print ("")
|
|
|
|
print (" default:")
|
|
|
|
print (" break;")
|
|
|
|
print (" }")
|
|
|
|
print (" return X;")
|
|
|
|
print ("}")
|
|
|
|
print ()
|
2014-06-20 22:20:59 +02:00
|
|
|
for value,short in short_value.items():
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("#undef %s" % (short))
|
|
|
|
print ()
|
2014-06-20 22:20:59 +02:00
|
|
|
|
2012-04-10 22:25:08 +02:00
|
|
|
def print_shaping_table(f):
|
|
|
|
|
|
|
|
shapes = {}
|
|
|
|
ligatures = {}
|
|
|
|
names = {}
|
|
|
|
for line in f:
|
|
|
|
|
|
|
|
fields = [x.strip () for x in line.split (';')]
|
|
|
|
if fields[5][0:1] != '<':
|
|
|
|
continue
|
|
|
|
|
|
|
|
items = fields[5].split (' ')
|
|
|
|
shape, items = items[0][1:-1], tuple (int (x, 16) for x in items[1:])
|
|
|
|
|
|
|
|
if not shape in ['initial', 'medial', 'isolated', 'final']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
c = int (fields[0], 16)
|
|
|
|
if len (items) != 1:
|
|
|
|
# We only care about lam-alef ligatures
|
|
|
|
if len (items) != 2 or items[0] != 0x0644 or items[1] not in [0x0622, 0x0623, 0x0625, 0x0627]:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Save ligature
|
|
|
|
names[c] = fields[1]
|
|
|
|
if items not in ligatures:
|
|
|
|
ligatures[items] = {}
|
|
|
|
ligatures[items][shape] = c
|
|
|
|
else:
|
|
|
|
# Save shape
|
|
|
|
if items[0] not in names:
|
|
|
|
names[items[0]] = fields[1]
|
|
|
|
else:
|
|
|
|
names[items[0]] = os.path.commonprefix ([names[items[0]], fields[1]]).strip ()
|
|
|
|
if items[0] not in shapes:
|
|
|
|
shapes[items[0]] = {}
|
|
|
|
shapes[items[0]][shape] = c
|
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print ("static const uint16_t shaping_table[][4] =")
|
|
|
|
print ("{")
|
2012-04-10 22:25:08 +02:00
|
|
|
|
|
|
|
keys = shapes.keys ()
|
|
|
|
min_u, max_u = min (keys), max (keys)
|
|
|
|
for u in range (min_u, max_u + 1):
|
2012-09-06 07:16:39 +02:00
|
|
|
s = [shapes[u][shape] if u in shapes and shape in shapes[u] else 0
|
2012-04-10 22:25:08 +02:00
|
|
|
for shape in ['initial', 'medial', 'final', 'isolated']]
|
2014-07-11 21:05:36 +02:00
|
|
|
value = ', '.join ("0x%04Xu" % c for c in s)
|
2018-03-29 10:18:47 +02:00
|
|
|
print (" {%s}, /* U+%04X %s */" % (value, u, names[u] if u in names else ""))
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("};")
|
|
|
|
print ()
|
|
|
|
print ("#define SHAPING_TABLE_FIRST 0x%04Xu" % min_u)
|
|
|
|
print ("#define SHAPING_TABLE_LAST 0x%04Xu" % max_u)
|
|
|
|
print ()
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2012-04-10 23:20:05 +02:00
|
|
|
ligas = {}
|
2012-04-10 22:25:08 +02:00
|
|
|
for pair in ligatures.keys ():
|
|
|
|
for shape in ligatures[pair]:
|
|
|
|
c = ligatures[pair][shape]
|
|
|
|
if shape == 'isolated':
|
|
|
|
liga = (shapes[pair[0]]['initial'], shapes[pair[1]]['final'])
|
|
|
|
elif shape == 'final':
|
|
|
|
liga = (shapes[pair[0]]['medial'], shapes[pair[1]]['final'])
|
|
|
|
else:
|
|
|
|
raise Exception ("Unexpected shape", shape)
|
2012-04-10 23:20:05 +02:00
|
|
|
if liga[0] not in ligas:
|
|
|
|
ligas[liga[0]] = []
|
|
|
|
ligas[liga[0]].append ((liga[1], c))
|
|
|
|
max_i = max (len (ligas[l]) for l in ligas)
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print ("static const struct ligature_set_t {")
|
|
|
|
print (" uint16_t first;")
|
|
|
|
print (" struct ligature_pairs_t {")
|
|
|
|
print (" uint16_t second;")
|
|
|
|
print (" uint16_t ligature;")
|
|
|
|
print (" } ligatures[%d];" % max_i)
|
|
|
|
print ("} ligature_table[] =")
|
|
|
|
print ("{")
|
2018-03-29 18:52:47 +02:00
|
|
|
for first in sorted (ligas.keys ()):
|
2012-04-10 23:20:05 +02:00
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print (" { 0x%04Xu, {" % (first))
|
2012-04-10 23:20:05 +02:00
|
|
|
for liga in ligas[first]:
|
2018-03-29 10:18:47 +02:00
|
|
|
print (" { 0x%04Xu, 0x%04Xu }, /* %s */" % (liga[0], liga[1], names[liga[1]]))
|
|
|
|
print (" }},")
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("};")
|
|
|
|
print ()
|
2012-04-10 22:25:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ("/* == Start of generated table == */")
|
|
|
|
print ("/*")
|
|
|
|
print (" * The following table is generated by running:")
|
|
|
|
print (" *")
|
|
|
|
print (" * ./gen-arabic-table.py ArabicShaping.txt UnicodeData.txt Blocks.txt")
|
|
|
|
print (" *")
|
|
|
|
print (" * on files with these headers:")
|
|
|
|
print (" *")
|
2012-04-10 22:25:08 +02:00
|
|
|
for h in headers:
|
|
|
|
for l in h:
|
2018-03-29 10:18:47 +02:00
|
|
|
print (" * %s" % (l.strip()))
|
|
|
|
print (" */")
|
|
|
|
print ()
|
2022-06-03 10:54:33 +02:00
|
|
|
print ("#ifndef HB_OT_SHAPER_ARABIC_TABLE_HH")
|
|
|
|
print ("#define HB_OT_SHAPER_ARABIC_TABLE_HH")
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
2012-04-10 22:25:08 +02:00
|
|
|
|
2014-06-20 22:17:42 +02:00
|
|
|
read_blocks (files[2])
|
2012-04-10 22:25:08 +02:00
|
|
|
print_joining_table (files[0])
|
|
|
|
print_shaping_table (files[1])
|
|
|
|
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
2022-06-03 10:54:33 +02:00
|
|
|
print ("#endif /* HB_OT_SHAPER_ARABIC_TABLE_HH */")
|
2018-03-29 10:18:47 +02:00
|
|
|
print ()
|
|
|
|
print ("/* == End of generated table == */")
|