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 data[0][0x20F0] = defaults[0]
48 for u in range (0xFE00, 0xFE0F + 1):
49 data[0][u] = defaults[0]
51 # Merge data into one dict:
52 for i,v in enumerate (defaults):
53 values[i][v] = values[i].get (v, 0) + 1
55 for i,d in enumerate (data):
56 for u,v in d.items ():
57 if i >= 2 and not u in combined:
60 combined[u] = list (defaults)
62 combined = {k:v for k,v in combined.items() if v[3] not in BLACKLISTED_BLOCKS}
70 'Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc',
71 'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po',
72 'Ps', 'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs',
73 # Indic_Syllabic_Category
85 'Consonant_Placeholder',
88 'Consonant_With_Stacker',
90 'Consonant_Preceding_Repha',
91 'Consonant_Succeeding_Repha',
92 'Consonant_Subjoined',
95 'Consonant_Head_Letter',
108 'Brahmi_Joining_Number',
109 # Indic_Positional_Category
120 'Top_And_Left_And_Right',
123 'Top_And_Bottom_And_Right',
127 class PropertyValue(object):
128 def __init__(self, name_):
132 def __eq__(self, other):
133 return self.name == (other if isinstance(other, basestring) else other.name)
134 def __ne__(self, other):
135 return not (self == other)
139 for name in property_names:
140 value = PropertyValue(name)
141 assert value not in property_values
142 assert value not in globals()
143 property_values[name] = value
144 globals().update(property_values)
147 def is_BASE(U, UISC, UGC):
148 return (UISC in [Number, Consonant, Consonant_Head_Letter,
149 #SPEC-DRAFT Consonant_Placeholder,
151 Vowel_Independent #SPEC-DRAFT
153 (UGC == Lo and UISC in [Avagraha, Bindu, Consonant_Final, Consonant_Medial,
154 Consonant_Subjoined, Vowel, Vowel_Dependent]))
155 def is_BASE_IND(U, UISC, UGC):
156 #SPEC-DRAFT return (UISC in [Consonant_Dead, Modifying_Letter] or UGC == Po)
157 return (UISC in [Consonant_Dead, Modifying_Letter] or
158 (UGC == Po and not U in [0x104E, 0x2022, 0x11A3F, 0x11A45]) or
159 False # SPEC-DRAFT-OUTDATED! U == 0x002D
161 def is_BASE_NUM(U, UISC, UGC):
162 return UISC == Brahmi_Joining_Number
163 def is_BASE_OTHER(U, UISC, UGC):
164 if UISC == Consonant_Placeholder: return True #SPEC-DRAFT
165 #SPEC-DRAFT return U in [0x00A0, 0x00D7, 0x2015, 0x2022, 0x25CC, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
166 return U in [0x2015, 0x2022, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
167 def is_CGJ(U, UISC, UGC):
169 def is_CONS_FINAL(U, UISC, UGC):
170 return ((UISC == Consonant_Final and UGC != Lo) or
171 UISC == Consonant_Succeeding_Repha)
172 def is_CONS_FINAL_MOD(U, UISC, UGC):
173 #SPEC-DRAFT return UISC in [Consonant_Final_Modifier, Syllable_Modifier]
174 return UISC == Syllable_Modifier
175 def is_CONS_MED(U, UISC, UGC):
176 return UISC == Consonant_Medial and UGC != Lo
177 def is_CONS_MOD(U, UISC, UGC):
178 return UISC in [Nukta, Gemination_Mark, Consonant_Killer]
179 def is_CONS_SUB(U, UISC, UGC):
180 #SPEC-DRAFT return UISC == Consonant_Subjoined
181 return UISC == Consonant_Subjoined and UGC != Lo
182 def is_CONS_WITH_STACKER(U, UISC, UGC):
183 return UISC == Consonant_With_Stacker
184 def is_HALANT(U, UISC, UGC):
185 return UISC in [Virama, Invisible_Stacker]
186 def is_HALANT_NUM(U, UISC, UGC):
187 return UISC == Number_Joiner
188 def is_ZWNJ(U, UISC, UGC):
189 return UISC == Non_Joiner
190 def is_ZWJ(U, UISC, UGC):
191 return UISC == Joiner
192 def is_Word_Joiner(U, UISC, UGC):
194 def is_OTHER(U, UISC, UGC):
195 #SPEC-OUTDATED return UGC == Zs # or any other SCRIPT_COMMON characters
196 return (UISC == Other
197 and not is_SYM_MOD(U, UISC, UGC)
198 and not is_CGJ(U, UISC, UGC)
199 and not is_Word_Joiner(U, UISC, UGC)
200 and not is_VARIATION_SELECTOR(U, UISC, UGC)
202 def is_Reserved(U, UISC, UGC):
204 def is_REPHA(U, UISC, UGC):
205 return UISC in [Consonant_Preceding_Repha, Consonant_Prefixed]
206 def is_SYM(U, UISC, UGC):
207 if U == 0x25CC: return False #SPEC-DRAFT
208 #SPEC-DRAFT return UGC in [So, Sc] or UISC == Symbol_Letter
209 return UGC in [So, Sc]
210 def is_SYM_MOD(U, UISC, UGC):
211 return U in [0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E, 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73]
212 def is_VARIATION_SELECTOR(U, UISC, UGC):
213 return 0xFE00 <= U <= 0xFE0F
214 def is_VOWEL(U, UISC, UGC):
215 # https://github.com/roozbehp/unicode-data/issues/6
216 return (UISC == Pure_Killer or
217 (UGC != Lo and UISC in [Vowel, Vowel_Dependent] and U not in [0xAA29]))
218 def is_VOWEL_MOD(U, UISC, UGC):
219 # https://github.com/roozbehp/unicode-data/issues/6
220 return (UISC in [Tone_Mark, Cantillation_Mark, Register_Shifter, Visarga] or
221 (UGC != Lo and (UISC == Bindu or U in [0xAA29])))
230 'FM': is_CONS_FINAL_MOD,
234 'CS': is_CONS_WITH_STACKER,
239 'WJ': is_Word_Joiner,
245 'VS': is_VARIATION_SELECTOR,
258 'Blw': [Bottom, Bottom_And_Left],
267 'Abv': [Top, Top_And_Bottom, Top_And_Bottom_And_Right, Top_And_Right],
268 'Blw': [Bottom, Overstruck, Bottom_And_Right],
270 'Pre': [Left, Top_And_Left, Top_And_Left_And_Right, Left_And_Right],
274 'Blw': [Bottom, Overstruck],
288 def map_to_use(data):
290 items = use_mapping.items()
291 for U,(UISC,UIPC,UGC,UBlock) in data.items():
293 # Resolve Indic_Syllabic_Category
295 # TODO: These don't have UISC assigned in Unicode 8.0, but
297 if U == 0x17DD: UISC = Vowel_Dependent
298 if 0x1CE2 <= U <= 0x1CE8: UISC = Cantillation_Mark
300 # TODO: https://github.com/harfbuzz/harfbuzz/pull/627
301 if 0x1BF2 <= U <= 0x1BF3: UISC = Nukta; UIPC = Bottom
303 # TODO: U+1CED should only be allowed after some of
304 # the nasalization marks, maybe only for U+1CE9..U+1CF1.
305 if U == 0x1CED: UISC = Tone_Mark
307 # TODO: https://github.com/harfbuzz/harfbuzz/issues/525
308 if U == 0x1A7F: UISC = Consonant_Final; UIPC = Bottom
310 # TODO: https://github.com/harfbuzz/harfbuzz/pull/609
311 if U == 0x20F0: UISC = Cantillation_Mark; UIPC = Top
313 # TODO: https://github.com/harfbuzz/harfbuzz/pull/626
314 if U == 0xA8B4: UISC = Consonant_Medial
316 values = [k for k,v in items if v(U,UISC,UGC)]
317 assert len(values) == 1, "%s %s %s %s" % (hex(U), UISC, UGC, values)
320 # Resolve Indic_Positional_Category
322 # TODO: Not in Unicode 8.0 yet, but in spec.
323 if U == 0x1B6C: UIPC = Bottom
325 # TODO: These should die, but have UIPC in Unicode 8.0
326 if U in [0x953, 0x954]: UIPC = Not_Applicable
328 # TODO: In USE's override list but not in Unicode 8.0
329 if U == 0x103C: UIPC = Left
331 # TODO: These are not in USE's override list that we have, nor are they in Unicode 8.0
332 if 0xA926 <= U <= 0xA92A: UIPC = Top
333 if U == 0x111CA: UIPC = Bottom
334 if U == 0x11300: UIPC = Top
335 if U == 0x1133C: UIPC = Bottom
336 if U == 0x1171E: UIPC = Left # Correct?!
337 if 0x1CF2 <= U <= 0x1CF3: UIPC = Right
338 if 0x1CF8 <= U <= 0x1CF9: UIPC = Top
340 assert (UIPC in [Not_Applicable, Visual_Order_Left] or
341 USE in use_positions), "%s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC)
343 pos_mapping = use_positions.get(USE, None)
345 values = [k for k,v in pos_mapping.items() if v and UIPC in v]
346 assert len(values) == 1, "%s %s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC, values)
347 USE = USE + values[0]
349 out[U] = (USE, UBlock)
352 defaults = ('O', 'No_Block')
353 data = map_to_use(data)
355 print "/* == Start of generated table == */"
357 print " * The following table is generated by running:"
359 print " * ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt"
361 print " * on files with these headers:"
365 print " * %s" % (l.strip())
368 print '#include "hb-ot-shape-complex-use-private.hh"'
374 def print_block (block, start, end, data):
375 global total, used, last_block
376 if block and block != last_block:
379 print " /* %s */" % block
381 print ' ' * (20 + (start % 16 * 6)),
383 assert start % 8 == 0
384 assert (end+1) % 8 == 0
385 for u in range (start, end+1):
388 print " /* %04X */" % u,
391 d = data.get (u, defaults)
392 sys.stdout.write ("%6s," % d[0])
394 total += end - start + 1
407 for k,v in sorted(use_mapping.items()):
408 if k in use_positions and use_positions[k]: continue
409 print "#define %s USE_%s /* %s */" % (k, k, v.__name__[3:])
410 for k,v in sorted(use_positions.items()):
414 print "#define %s USE_%s" % (tag, tag)
416 print "static const USE_TABLE_ELEMENT_TYPE use_table[] = {"
424 while end in uu and block == data[end][1]:
426 end = (end-1)//8*8 + 7
428 if start != last + 1:
429 if start - last <= 1+16*3:
430 print_block (None, last+1, start-1, data)
434 ends.append (last + 1)
435 offset += ends[-1] - starts[-1]
438 print "#define use_offset_0x%04xu %d" % (start, offset)
439 starts.append (start)
441 print_block (block, start, end, data)
443 ends.append (last + 1)
444 offset += ends[-1] - starts[-1]
447 occupancy = used * 100. / total
449 print "}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy)
451 print "USE_TABLE_ELEMENT_TYPE"
452 print "hb_use_get_category (hb_codepoint_t u)"
454 print " switch (u >> %d)" % page_bits
456 pages = set([u>>page_bits for u in starts+ends])
457 for p in sorted(pages):
458 print " case 0x%0Xu:" % p
459 for (start,end) in zip (starts, ends):
460 if p not in [start>>page_bits, end>>page_bits]: continue
461 offset = "use_offset_0x%04xu" % start
462 print " if (hb_in_range<hb_codepoint_t> (u, 0x%04Xu, 0x%04Xu)) return use_table[u - 0x%04Xu + %s];" % (start, end-1, start, offset)
468 print " return USE_O;"
471 for k in sorted(use_mapping.keys()):
472 if k in use_positions and use_positions[k]: continue
473 print "#undef %s" % k
474 for k,v in sorted(use_positions.items()):
478 print "#undef %s" % tag
480 print "/* == End of generated table == */"
482 # Maintain at least 50% occupancy in the table */
484 raise Exception ("Table too sparse, please investigate: ", occupancy)