5 if len (sys.argv) != 5:
6 print >>sys.stderr, "usage: ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt"
9 BLACKLISTED_BLOCKS = ["Thai", "Lao", "Tibetan"]
11 files = [file (x) for x in sys.argv[1:]]
13 headers = [[f.readline () for i in range (2)] for j,f in enumerate(files) if j != 2]
14 headers.append (["UnicodeData.txt does not have a header."])
16 data = [{} for f in files]
17 values = [{} for f in files]
18 for i, f in enumerate (files):
25 fields = [x.strip () for x in line.split (';')]
29 uu = fields[0].split ('..')
30 start = int (uu[0], 16)
36 t = fields[1 if i != 2 else 2]
38 for u in range (start, end + 1):
40 values[i][t] = values[i].get (t, 0) + end - start + 1
42 defaults = ('Other', 'Not_Applicable', 'Cn', 'No_Block')
44 # TODO Characters that are not in Unicode Indic files, but used in USE
45 data[0][0x034F] = defaults[0]
46 data[0][0x2060] = defaults[0]
47 for u in range (0xFE00, 0xFE0F + 1):
48 data[0][u] = defaults[0]
50 # Merge data into one dict:
51 for i,v in enumerate (defaults):
52 values[i][v] = values[i].get (v, 0) + 1
54 for i,d in enumerate (data):
55 for u,v in d.items ():
56 if i >= 2 and not u in combined:
59 combined[u] = list (defaults)
61 combined = {k:v for k,v in combined.items() if v[3] not in BLACKLISTED_BLOCKS}
69 'Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc',
70 'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po',
71 'Ps', 'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs',
72 # Indic_Syllabic_Category
84 'Consonant_Placeholder',
87 'Consonant_With_Stacker',
89 'Consonant_Preceding_Repha',
90 'Consonant_Succeeding_Repha',
91 'Consonant_Subjoined',
94 'Consonant_Head_Letter',
107 'Brahmi_Joining_Number',
108 # Indic_Positional_Category
119 'Top_And_Left_And_Right',
121 'Top_And_Bottom_And_Right',
125 class PropertyValue(object):
126 def __init__(self, name_):
130 def __eq__(self, other):
131 return self.name == (other if isinstance(other, basestring) else other.name)
132 def __ne__(self, other):
133 return not (self == other)
137 for name in property_names:
138 value = PropertyValue(name)
139 assert value not in property_values
140 assert value not in globals()
141 property_values[name] = value
142 globals().update(property_values)
145 def is_BASE(U, UISC, UGC):
146 return (UISC in [Number, Consonant, Consonant_Head_Letter,
147 #SPEC-DRAFT Consonant_Placeholder,
149 Vowel_Independent #SPEC-DRAFT
151 (UGC == Lo and UISC in [Avagraha, Bindu, Consonant_Final, Consonant_Medial,
152 Consonant_Subjoined, Vowel, Vowel_Dependent]))
153 def is_BASE_IND(U, UISC, UGC):
154 #SPEC-DRAFT return (UISC in [Consonant_Dead, Modifying_Letter] or UGC == Po)
155 return (UISC in [Consonant_Dead, Modifying_Letter] or
156 (UGC == Po and not U in [0x104E, 0x2022]) or
157 False # SPEC-DRAFT-OUTDATED! U == 0x002D
159 def is_BASE_NUM(U, UISC, UGC):
160 return UISC == Brahmi_Joining_Number
161 def is_BASE_OTHER(U, UISC, UGC):
162 if UISC == Consonant_Placeholder: return True #SPEC-DRAFT
163 #SPEC-DRAFT return U in [0x00A0, 0x00D7, 0x2015, 0x2022, 0x25CC, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
164 return U in [0x2015, 0x2022, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
165 def is_CGJ(U, UISC, UGC):
167 def is_CONS_FINAL(U, UISC, UGC):
168 return ((UISC == Consonant_Final and UGC != Lo) or
169 UISC == Consonant_Succeeding_Repha)
170 def is_CONS_FINAL_MOD(U, UISC, UGC):
171 #SPEC-DRAFT return UISC in [Consonant_Final_Modifier, Syllable_Modifier]
172 return UISC == Syllable_Modifier
173 def is_CONS_MED(U, UISC, UGC):
174 return UISC == Consonant_Medial and UGC != Lo
175 def is_CONS_MOD(U, UISC, UGC):
176 return UISC in [Nukta, Gemination_Mark, Consonant_Killer]
177 def is_CONS_SUB(U, UISC, UGC):
178 #SPEC-DRAFT return UISC == Consonant_Subjoined
179 return UISC == Consonant_Subjoined and UGC != Lo
180 def is_HALANT(U, UISC, UGC):
181 return UISC in [Virama, Invisible_Stacker]
182 def is_HALANT_NUM(U, UISC, UGC):
183 return UISC == Number_Joiner
184 def is_ZWNJ(U, UISC, UGC):
185 return UISC == Non_Joiner
186 def is_ZWJ(U, UISC, UGC):
187 return UISC == Joiner
188 def is_Word_Joiner(U, UISC, UGC):
190 def is_OTHER(U, UISC, UGC):
191 #SPEC-OUTDATED return UGC == Zs # or any other SCRIPT_COMMON characters
192 return (UISC == Other
193 and not is_SYM_MOD(U, UISC, UGC)
194 and not is_CGJ(U, UISC, UGC)
195 and not is_Word_Joiner(U, UISC, UGC)
196 and not is_VARIATION_SELECTOR(U, UISC, UGC)
198 def is_Reserved(U, UISC, UGC):
200 def is_REPHA(U, UISC, UGC):
201 #return UISC == Consonant_Preceding_Repha
202 #SPEC-OUTDATED hack to categorize Consonant_With_Stacker and Consonant_Prefixed
203 return UISC in [Consonant_Preceding_Repha, Consonant_With_Stacker, Consonant_Prefixed]
204 def is_SYM(U, UISC, UGC):
205 if U == 0x25CC: return False #SPEC-DRAFT
206 #SPEC-DRAFT return UGC in [So, Sc] or UISC == Symbol_Letter
207 return UGC in [So, Sc]
208 def is_SYM_MOD(U, UISC, UGC):
209 return U in [0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E, 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73]
210 def is_VARIATION_SELECTOR(U, UISC, UGC):
211 return 0xFE00 <= U <= 0xFE0F
212 def is_VOWEL(U, UISC, UGC):
213 return (UISC == Pure_Killer or
214 (UGC != Lo and UISC in [Vowel, Vowel_Dependent]))
215 def is_VOWEL_MOD(U, UISC, UGC):
216 return (UISC in [Tone_Mark, Cantillation_Mark, Register_Shifter, Visarga] or
217 (UGC != Lo and UISC == Bindu))
226 'FM': is_CONS_FINAL_MOD,
234 'WJ': is_Word_Joiner,
240 'VS': is_VARIATION_SELECTOR,
262 'Abv': [Top, Top_And_Bottom, Top_And_Bottom_And_Right, Top_And_Right],
263 'Blw': [Bottom, Overstruck, Bottom_And_Right],
265 'Pre': [Left, Top_And_Left, Top_And_Left_And_Right, Left_And_Right],
269 'Blw': [Bottom, Overstruck],
283 def map_to_use(data):
285 items = use_mapping.items()
286 for U,(UISC,UIPC,UGC,UBlock) in data.items():
288 # Resolve Indic_Syllabic_Category
290 # TODO: These don't have UISC assigned in Unicode 8.0, but
292 if U == 0x17DD: UISC = Vowel_Dependent
293 if 0x1CE2 <= U <= 0x1CE8: UISC = Cantillation_Mark
295 # TODO: U+1CED should only be allowed after some of
296 # the nasalization marks, maybe only for U+1CE9..U+1CF1.
297 if U == 0x1CED: UISC = Tone_Mark
299 evals = [(k, v(U,UISC,UGC)) for k,v in items]
300 values = [k for k,v in evals if v]
301 assert len(values) == 1, "%s %s %s %s" % (hex(U), UISC, UGC, values)
304 # Resolve Indic_Positional_Category
306 # TODO: Not in Unicode 8.0 yet, but in spec.
307 if U == 0x1B6C: UIPC = Bottom
309 # TODO: These should die, but have UIPC in Unicode 8.0
310 if U in [0x953, 0x954]: UIPC = Not_Applicable
312 # TODO: In USE's override list but not in Unicode 8.0
313 if U == 0x103C: UIPC = Left
315 # TODO: These are not in USE's override list that we have, nor are they in Unicode 8.0
316 if 0xA926 <= U <= 0xA92A: UIPC = Top
317 if U == 0x111CA: UIPC = Bottom
318 if U == 0x11300: UIPC = Top
319 if U == 0x1133C: UIPC = Bottom
320 if U == 0x1171E: UIPC = Left # Correct?!
321 if 0x1CF2 <= U <= 0x1CF3: UIPC = Right
322 if 0x1CF8 <= U <= 0x1CF9: UIPC = Top
324 assert (UIPC in [Not_Applicable, Visual_Order_Left] or
325 USE in use_positions), "%s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC)
327 pos_mapping = use_positions.get(USE, None)
329 values = [k for k,v in pos_mapping.items() if v and UIPC in v]
330 assert len(values) == 1, "%s %s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC, values)
331 USE = USE + values[0]
333 out[U] = (USE, UBlock)
336 defaults = ('O', 'No_Block')
337 data = map_to_use(data)
339 # Remove the outliers
341 for u in [0x034F, 0x25CC, 0x1107F]:
345 print "/* == Start of generated table == */"
347 print " * The following table is generated by running:"
349 print " * ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt"
351 print " * on files with these headers:"
355 print " * %s" % (l.strip())
358 print '#include "hb-ot-shape-complex-use-private.hh"'
364 def print_block (block, start, end, data):
365 global total, used, last_block
366 if block and block != last_block:
369 print " /* %s */" % block
371 print ' ' * (20 + (start % 16 * 6)),
373 assert start % 8 == 0
374 assert (end+1) % 8 == 0
375 for u in range (start, end+1):
378 print " /* %04X */" % u,
381 d = data.get (u, defaults)
382 sys.stdout.write ("%6s," % d[0])
384 total += end - start + 1
397 for k,v in sorted(use_mapping.items()):
398 if k in use_positions and use_positions[k]: continue
399 print "#define %s USE_%s /* %s */" % (k, k, v.__name__[3:])
400 for k,v in sorted(use_positions.items()):
404 print "#define %s USE_%s" % (tag, tag)
406 print "static const USE_TABLE_ELEMENT_TYPE use_table[] = {"
414 while end in uu and block == data[end][1]:
416 end = (end-1)//8*8 + 7
418 if start != last + 1:
419 if start - last <= 1+16*3:
420 print_block (None, last+1, start-1, data)
424 ends.append (last + 1)
425 offset += ends[-1] - starts[-1]
428 print "#define use_offset_0x%04xu %d" % (start, offset)
429 starts.append (start)
431 print_block (block, start, end, data)
433 ends.append (last + 1)
434 offset += ends[-1] - starts[-1]
437 occupancy = used * 100. / total
439 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
441 print "USE_TABLE_ELEMENT_TYPE"
442 print "hb_use_get_categories (hb_codepoint_t u)"
444 print " switch (u >> %d)" % page_bits
446 pages = set([u>>page_bits for u in starts+ends+singles.keys()])
447 for p in sorted(pages):
448 print " case 0x%0Xu:" % p
449 for (start,end) in zip (starts, ends):
450 if p not in [start>>page_bits, end>>page_bits]: continue
451 offset = "use_offset_0x%04xu" % start
452 print " if (hb_in_range (u, 0x%04Xu, 0x%04Xu)) return use_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset)
453 for u,d in singles.items ():
454 if p != u>>page_bits: continue
455 print " if (unlikely (u == 0x%04Xu)) return %s;" % (u, d[0])
461 print " return USE_O;"
464 for k in sorted(use_mapping.keys()):
465 if k in use_positions and use_positions[k]: continue
466 print "#undef %s" % k
467 for k,v in sorted(use_positions.items()):
471 print "#undef %s" % tag
473 print "/* == End of generated table == */"
475 # Maintain at least 50% occupancy in the table */
477 raise Exception ("Table too sparse, please investigate: ", occupancy)