gen-use-table.py 9.6 KB
Newer Older
1 2 3 4
#!/usr/bin/python

import sys

5 6
if len (sys.argv) != 5:
	print >>sys.stderr, "usage: ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt"
7 8 9 10 11 12
	sys.exit (1)

BLACKLISTED_BLOCKS = ["Thai", "Lao", "Tibetan"]

files = [file (x) for x in sys.argv[1:]]

13 14
headers = [[f.readline () for i in range (2)] for j,f in enumerate(files) if j != 2]
headers.append (["UnicodeData.txt does not have a header."])
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

data = [{} for f in files]
values = [{} for f in files]
for i, f in enumerate (files):
	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)

36
		t = fields[1 if i != 2 else 2]
37 38 39 40 41 42

		for u in range (start, end + 1):
			data[i][u] = t
		values[i][t] = values[i].get (t, 0) + end - start + 1

# Merge data into one dict:
43
defaults = ('Other', 'Not_Applicable', 'Cn', 'No_Block')
44 45 46 47 48
for i,v in enumerate (defaults):
	values[i][v] = values[i].get (v, 0) + 1
combined = {}
for i,d in enumerate (data):
	for u,v in d.items ():
49
		if i >= 2 and not u in combined:
50 51 52 53
			continue
		if not u in combined:
			combined[u] = list (defaults)
		combined[u][i] = v
54
combined = {k:v for k,v in combined.items() if v[3] not in BLACKLISTED_BLOCKS}
55 56 57 58
data = combined
del combined
num = len (data)

59 60 61 62 63 64 65

property_names = [
	# General_Category
	'Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc',
	'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po',
	'Ps', 'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs',
	# Indic_Syllabic_Category
66
	'Other',
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
	'Bindu',
	'Visarga',
	'Avagraha',
	'Nukta',
	'Virama',
	'Pure_Killer',
	'Invisible_Stacker',
	'Vowel_Independent',
	'Vowel_Dependent',
	'Vowel',
	'Consonant_Placeholder',
	'Consonant',
	'Consonant_Dead',
	'Consonant_With_Stacker',
	'Consonant_Prefixed',
	'Consonant_Preceding_Repha',
	'Consonant_Succeeding_Repha',
	'Consonant_Subjoined',
	'Consonant_Medial',
	'Consonant_Final',
	'Consonant_Head_Letter',
	'Modifying_Letter',
	'Tone_Letter',
	'Tone_Mark',
	'Gemination_Mark',
	'Cantillation_Mark',
	'Register_Shifter',
	'Syllable_Modifier',
	'Consonant_Killer',
	'Non_Joiner',
	'Joiner',
	'Number_Joiner',
	'Number',
	'Brahmi_Joining_Number',
	# Indic_Positional_Category
102
	'Not_Applicable'
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
	'Right',
	'Left',
	'Visual_Order_Left',
	'Left_And_Right',
	'Top',
	'Bottom',
	'Top_And_Bottom',
	'Top_And_Right',
	'Top_And_Left',
	'Top_And_Left_And_Right',
	'Bottom_And_Right',
	'Top_And_Bottom_And_Right',
	'Overstruck',
]

class PropertyValue(object):
	def __init__(self, name_):
		self.name = name_
121 122 123 124 125 126 127
	def __str__(self):
		return self.name
	def __eq__(self, other):
		assert isinstance(other, basestring)
		return self.name == other
	def __ne__(self, other):
		return not (self == other)
128 129 130 131 132 133 134 135 136 137 138 139

property_values = {}

for name in property_names:
	value = PropertyValue(name)
	assert value not in property_values
	assert value not in globals()
	property_values[name] = value
globals().update(property_values)


def is_BASE(U, UISC, UGC):
140 141 142
	return (UISC in [Number, Consonant, Consonant_Head_Letter,
			#SPEC-OUTDATED Consonant_Placeholder,
			Tone_Letter] or
143 144 145 146 147
		(UGC == Lo and UISC in [Avagraha, Bindu, Consonant_Final, Consonant_Medial,
					Consonant_Subjoined, Vowel, Vowel_Dependent]))
def is_BASE_VOWEL(U, UISC, UGC):
	return UISC == Vowel_Independent
def is_BASE_IND(U, UISC, UGC):
148 149 150
	#SPEC-BROKEN return (UISC in [Consonant_Dead, Modifying_Letter] or UGC == Po)
	return (UISC in [Consonant_Dead, Modifying_Letter] or
		(UGC == Po and not is_BASE_OTHER(U, UISC, UGC))) # for 104E
151 152 153
def is_BASE_NUM(U, UISC, UGC):
	return UISC == Brahmi_Joining_Number
def is_BASE_OTHER(U, UISC, UGC):
154
	if UISC == Consonant_Placeholder: return True #SPEC-OUTDATED
155 156 157 158 159 160 161 162
	return U in [0x00A0, 0x00D7, 0x2015, 0x2022, 0x25CC,
		     0x25FB, 0x25FC, 0x25FD, 0x25FE]
def is_CGJ(U, UISC, UGC):
	return U == 0x034F
def is_CONS_FINAL(U, UISC, UGC):
	return ((UISC == Consonant_Final and UGC != Lo) or
		UISC == Consonant_Succeeding_Repha)
def is_CONS_FINAL_MOD(U, UISC, UGC):
163 164
	#SPEC-OUTDATED return  UISC in [Consonant_Final_Modifier, Syllable_Modifier]
	return  UISC == Syllable_Modifier
165 166 167 168 169
def is_CONS_MED(U, UISC, UGC):
	return UISC == Consonant_Medial and UGC != Lo
def is_CONS_MOD(U, UISC, UGC):
	return UISC in [Nukta, Gemination_Mark, Consonant_Killer]
def is_CONS_SUB(U, UISC, UGC):
170 171
	#SPEC-OUTDATED return UISC == Consonant_Subjoined
	return UISC == Consonant_Subjoined and UGC != Lo
172 173 174 175 176 177 178 179 180 181 182
def is_HALANT(U, UISC, UGC):
	return UISC in [Virama, Invisible_Stacker]
def is_HALANT_NUM(U, UISC, UGC):
	return UISC == Number_Joiner
def is_ZWNJ(U, UISC, UGC):
	return UISC == Non_Joiner
def is_ZWJ(U, UISC, UGC):
	return UISC == Joiner
def is_Word_Joiner(U, UISC, UGC):
	return U == 0x2060
def is_OTHER(U, UISC, UGC):
183 184
	#SPEC-OUTDATED return UGC == Zs # or any other SCRIPT_COMMON characters
	return UISC == Other and not is_SYM_MOD(U, UISC, UGC)
185 186 187
def is_Reserved(U, UISC, UGC):
	return UGC == 'Cn'
def is_REPHA(U, UISC, UGC):
188 189 190
	#return UISC == Consonant_Preceding_Repha
	#SPEC-OUTDATED hack to categorize Consonant_With_Stacker and Consonant_Prefixed
	return UISC in [Consonant_Preceding_Repha, Consonant_With_Stacker, Consonant_Prefixed]
191
def is_SYM(U, UISC, UGC):
192 193 194
	if U == 0x25CC: return False #SPEC-OUTDATED
	#SPEC-OUTDATED return UGC in [So, Sc] or UISC == Symbol_Letter
	return UGC in [So, Sc]
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
def is_SYM_MOD(U, UISC, UGC):
	return U in [0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E, 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73]
def is_VARIATION_SELECTOR(U, UISC, UGC):
	return 0xFE00 <= U <= 0xFE0F
def is_VOWEL(U, UISC, UGC):
	return (UISC == Pure_Killer or
		(UGC != Lo and UISC in [Vowel, Vowel_Dependent]))
def is_VOWEL_MOD(U, UISC, UGC):
	return (UISC in [Tone_Mark, Cantillation_Mark, Register_Shifter, Visarga] or
		(UGC != Lo and UISC == Bindu))

use_mapping = {
	'B':	is_BASE,
	'IV':	is_BASE_VOWEL,
	'IND':	is_BASE_IND,
	'N':	is_BASE_NUM,
	'GB':	is_BASE_OTHER,
	'CGJ':	is_CGJ,
	'F':	is_CONS_FINAL,
	'FM':	is_CONS_FINAL_MOD,
	'M':	is_CONS_MED,
	'CM':	is_CONS_MOD,
	'SUB':	is_CONS_SUB,
	'H':	is_HALANT,
	'HN':	is_HALANT_NUM,
	'ZWNJ':	is_ZWNJ,
	'ZWJ':	is_ZWJ,
	'WJ':	is_Word_Joiner,
	'O':	is_OTHER,
	'Rsv':	is_Reserved,
	'R':	is_REPHA,
	'S':	is_SYM,
	'SM':	is_SYM_MOD,
	'VS':	is_VARIATION_SELECTOR,
	'V':	is_VOWEL,
	'VM':	is_VOWEL_MOD,
}

233 234 235 236 237 238 239 240 241 242 243 244
def map_to_use(data):
	out = {}
	items = use_mapping.items()
	for U,(UISC,UIPC,UGC,UBlock) in data.items():
		evals = [(k, v(U,UISC,UGC)) for k,v in items]
		values = [k for k,v in evals if v]
		assert len(values) == 1, "%s %s %s %s" % (hex(U), UISC, UGC, values)
		out[U] = (values[0], UBlock)
	return out

defaults = ('O', 'No_Block')
data = map_to_use(data)
245

246
# Remove the outliers
247
singles = {}
248
for u in [0x25CC, 0x1107F]:
249 250 251 252 253 254 255
	singles[u] = data[u]
	del data[u]

print "/* == Start of generated table == */"
print "/*"
print " * The following table is generated by running:"
print " *"
256
print " *   ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt"
257 258 259 260 261 262 263 264
print " *"
print " * on files with these headers:"
print " *"
for h in headers:
	for l in h:
		print " * %s" % (l.strip())
print " */"
print
265
print '#include "hb-ot-shape-complex-use-private.hh"'
266 267 268 269 270 271 272 273 274 275 276
print

total = 0
used = 0
last_block = None
def print_block (block, start, end, data):
	global total, used, last_block
	if block and block != last_block:
		print
		print
		print "  /* %s */" % block
277 278
		if start % 16:
			print ' ' * (20 + (start % 16 * 4)),
279 280 281 282
	num = 0
	assert start % 8 == 0
	assert (end+1) % 8 == 0
	for u in range (start, end+1):
283
		if u % 16 == 0:
284 285 286 287 288
			print
			print "  /* %04X */" % u,
		if u in data:
			num += 1
		d = data.get (u, defaults)
289
		sys.stdout.write ("%4s," % d[0])
290 291 292 293 294 295 296 297 298 299 300 301 302 303

	total += end - start + 1
	used += num
	if block:
		last_block = block

uu = data.keys ()
uu.sort ()

last = -100000
num = 0
offset = 0
starts = []
ends = []
304 305 306
for k,v in sorted(use_mapping.items()):
	print "#define %s	USE_%s	/* %s */" % (k, k, v.__name__[3:])
print ""
307
print "static const USE_TABLE_ELEMENT_TYPE use_table[] = {"
308 309 310
for u in uu:
	if u <= last:
		continue
311
	block = data[u][1]
312 313 314

	start = u//8*8
	end = start+1
315
	while end in uu and block == data[end][1]:
316 317 318 319 320 321 322 323 324 325 326 327 328
		end += 1
	end = (end-1)//8*8 + 7

	if start != last + 1:
		if start - last <= 1+16*3:
			print_block (None, last+1, start-1, data)
			last = start-1
		else:
			if last >= 0:
				ends.append (last + 1)
				offset += ends[-1] - starts[-1]
			print
			print
329
			print "#define use_offset_0x%04xu %d" % (start, offset)
330 331 332 333 334 335 336 337 338 339 340 341
			starts.append (start)

	print_block (block, start, end, data)
	last = end
ends.append (last + 1)
offset += ends[-1] - starts[-1]
print
print
occupancy = used * 100. / total
page_bits = 12
print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
print
342 343
print "USE_TABLE_ELEMENT_TYPE"
print "hb_use_get_categories (hb_codepoint_t u)"
344 345 346 347 348 349 350 351
print "{"
print "  switch (u >> %d)" % page_bits
print "  {"
pages = set([u>>page_bits for u in starts+ends+singles.keys()])
for p in sorted(pages):
	print "    case 0x%0Xu:" % p
	for (start,end) in zip (starts, ends):
		if p not in [start>>page_bits, end>>page_bits]: continue
352 353
		offset = "use_offset_0x%04xu" % start
		print "      if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return use_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset)
354 355
	for u,d in singles.items ():
		if p != u>>page_bits: continue
356
		print "      if (unlikely (u == 0x%04Xu)) return %s;" % (u, d[0])
357 358 359 360 361 362 363
	print "      break;"
	print ""
print "    default:"
print "      break;"
print "  }"
print "  return _(x,x);"
print "}"
364 365 366
print ""
for k in sorted(use_mapping.keys()):
	print "#undef %s" % k
367 368 369
print
print "/* == End of generated table == */"

370 371
# Maintain at least 50% occupancy in the table */
if occupancy < 50:
372
	raise Exception ("Table too sparse, please investigate: ", occupancy)