4 from __future__ import print_function, division, absolute_import
9 if len (sys.argv) != 5:
10 print ("usage: ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt", file=sys.stderr)
13 BLACKLISTED_BLOCKS = ["Thai", "Lao"]
15 files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]]
17 headers = [[f.readline () for i in range (2)] for j,f in enumerate(files) if j != 2]
18 headers.append (["UnicodeData.txt does not have a header."])
20 data = [{} for f in files]
21 values = [{} for f in files]
22 for i, f in enumerate (files):
29 fields = [x.strip () for x in line.split (';')]
33 uu = fields[0].split ('..')
34 start = int (uu[0], 16)
40 t = fields[1 if i != 2 else 2]
42 for u in range (start, end + 1):
44 values[i][t] = values[i].get (t, 0) + end - start + 1
46 defaults = ('Other', 'Not_Applicable', 'Cn', 'No_Block')
48 # TODO Characters that are not in Unicode Indic files, but used in USE
49 data[0][0x034F] = defaults[0]
50 data[0][0x2060] = defaults[0]
51 data[0][0x20F0] = defaults[0]
52 # TODO https://github.com/roozbehp/unicode-data/issues/9
53 data[0][0x11C44] = 'Consonant_Placeholder'
54 data[0][0x11C45] = 'Consonant_Placeholder'
55 # TODO https://github.com/harfbuzz/harfbuzz/pull/1399
56 data[0][0x111C8] = 'Consonant_Placeholder'
57 for u in range (0xFE00, 0xFE0F + 1):
58 data[0][u] = defaults[0]
60 # Merge data into one dict:
61 for i,v in enumerate (defaults):
62 values[i][v] = values[i].get (v, 0) + 1
64 for i,d in enumerate (data):
65 for u,v in d.items ():
66 if i >= 2 and not u in combined:
69 combined[u] = list (defaults)
71 combined = {k:v for k,v in combined.items() if v[3] not in BLACKLISTED_BLOCKS}
79 'Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc',
80 'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po',
81 'Ps', 'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs',
82 # Indic_Syllabic_Category
94 'Consonant_Placeholder',
97 'Consonant_With_Stacker',
99 'Consonant_Preceding_Repha',
100 'Consonant_Succeeding_Repha',
101 'Consonant_Subjoined',
104 'Consonant_Head_Letter',
105 'Consonant_Initial_Postfixed',
118 'Brahmi_Joining_Number',
119 # Indic_Positional_Category
130 'Top_And_Left_And_Right',
133 'Top_And_Bottom_And_Right',
142 class PropertyValue(object):
143 def __init__(self, name_):
147 def __eq__(self, other):
148 return self.name == (other if isinstance(other, basestring) else other.name)
149 def __ne__(self, other):
150 return not (self == other)
152 return hash(str(self))
156 for name in property_names:
157 value = PropertyValue(name)
158 assert value not in property_values
159 assert value not in globals()
160 property_values[name] = value
161 globals().update(property_values)
164 def is_BASE(U, UISC, UGC):
165 return (UISC in [Number, Consonant, Consonant_Head_Letter,
166 #SPEC-DRAFT Consonant_Placeholder,
168 Vowel_Independent #SPEC-DRAFT
170 (UGC == Lo and UISC in [Avagraha, Bindu, Consonant_Final, Consonant_Medial,
171 Consonant_Subjoined, Vowel, Vowel_Dependent]))
172 def is_BASE_IND(U, UISC, UGC):
173 #SPEC-DRAFT return (UISC in [Consonant_Dead, Modifying_Letter] or UGC == Po)
174 return (UISC in [Consonant_Dead, Modifying_Letter] or
175 (UGC == Po and not U in [0x104B, 0x104E, 0x2022, 0x111C8, 0x11A3F, 0x11A45, 0x11C44, 0x11C45]) or
176 False # SPEC-DRAFT-OUTDATED! U == 0x002D
178 def is_BASE_NUM(U, UISC, UGC):
179 return UISC == Brahmi_Joining_Number
180 def is_BASE_OTHER(U, UISC, UGC):
181 if UISC == Consonant_Placeholder: return True #SPEC-DRAFT
182 #SPEC-DRAFT return U in [0x00A0, 0x00D7, 0x2015, 0x2022, 0x25CC, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
183 return U in [0x2015, 0x2022, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
184 def is_CGJ(U, UISC, UGC):
186 def is_CONS_FINAL(U, UISC, UGC):
187 # Consonant_Initial_Postfixed is new in Unicode 11; not in the spec.
188 return ((UISC == Consonant_Final and UGC != Lo) or
189 UISC == Consonant_Initial_Postfixed or
190 UISC == Consonant_Succeeding_Repha)
191 def is_CONS_FINAL_MOD(U, UISC, UGC):
192 #SPEC-DRAFT return UISC in [Consonant_Final_Modifier, Syllable_Modifier]
193 return UISC == Syllable_Modifier
194 def is_CONS_MED(U, UISC, UGC):
195 return UISC == Consonant_Medial and UGC != Lo
196 def is_CONS_MOD(U, UISC, UGC):
197 return UISC in [Nukta, Gemination_Mark, Consonant_Killer]
198 def is_CONS_SUB(U, UISC, UGC):
199 #SPEC-DRAFT return UISC == Consonant_Subjoined
200 return UISC == Consonant_Subjoined and UGC != Lo
201 def is_CONS_WITH_STACKER(U, UISC, UGC):
202 return UISC == Consonant_With_Stacker
203 def is_HALANT(U, UISC, UGC):
204 return UISC in [Virama, Invisible_Stacker] and not is_HALANT_OR_VOWEL_MODIFIER(U, UISC, UGC)
205 def is_HALANT_OR_VOWEL_MODIFIER(U, UISC, UGC):
206 # https://github.com/harfbuzz/harfbuzz/issues/1102
207 # https://github.com/harfbuzz/harfbuzz/issues/1379
208 return U in [0x11046, 0x1134D]
209 def is_HALANT_NUM(U, UISC, UGC):
210 return UISC == Number_Joiner
211 def is_ZWNJ(U, UISC, UGC):
212 return UISC == Non_Joiner
213 def is_ZWJ(U, UISC, UGC):
214 return UISC == Joiner
215 def is_Word_Joiner(U, UISC, UGC):
217 def is_OTHER(U, UISC, UGC):
218 #SPEC-OUTDATED return UGC == Zs # or any other SCRIPT_COMMON characters
219 return (UISC == Other
220 and not is_SYM_MOD(U, UISC, UGC)
221 and not is_CGJ(U, UISC, UGC)
222 and not is_Word_Joiner(U, UISC, UGC)
223 and not is_VARIATION_SELECTOR(U, UISC, UGC)
225 def is_Reserved(U, UISC, UGC):
227 def is_REPHA(U, UISC, UGC):
228 return UISC in [Consonant_Preceding_Repha, Consonant_Prefixed]
229 def is_SYM(U, UISC, UGC):
230 if U == 0x25CC: return False #SPEC-DRAFT
231 #SPEC-DRAFT return UGC in [So, Sc] or UISC == Symbol_Letter
232 return UGC in [So, Sc]
233 def is_SYM_MOD(U, UISC, UGC):
234 return U in [0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E, 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73]
235 def is_VARIATION_SELECTOR(U, UISC, UGC):
236 return 0xFE00 <= U <= 0xFE0F
237 def is_VOWEL(U, UISC, UGC):
238 # https://github.com/roozbehp/unicode-data/issues/6
239 return (UISC == Pure_Killer or
240 (UGC != Lo and UISC in [Vowel, Vowel_Dependent] and U not in [0xAA29]))
241 def is_VOWEL_MOD(U, UISC, UGC):
242 # https://github.com/roozbehp/unicode-data/issues/6
243 return (UISC in [Tone_Mark, Cantillation_Mark, Register_Shifter, Visarga] or
244 (UGC != Lo and (UISC == Bindu or U in [0xAA29])))
253 'FM': is_CONS_FINAL_MOD,
257 'CS': is_CONS_WITH_STACKER,
259 'HVM': is_HALANT_OR_VOWEL_MODIFIER,
263 'WJ': is_Word_Joiner,
269 'VS': is_VARIATION_SELECTOR,
282 'Blw': [Bottom, Bottom_And_Left],
291 'Abv': [Top, Top_And_Bottom, Top_And_Bottom_And_Right, Top_And_Right],
292 'Blw': [Bottom, Overstruck, Bottom_And_Right],
293 'Pst': [Right, Top_And_Left, Top_And_Left_And_Right, Left_And_Right],
298 'Blw': [Bottom, Overstruck],
313 def map_to_use(data):
315 items = use_mapping.items()
316 for U,(UISC,UIPC,UGC,UBlock) in data.items():
318 # Resolve Indic_Syllabic_Category
320 # TODO: These don't have UISC assigned in Unicode 8.0, but have UIPC
321 if U == 0x17DD: UISC = Vowel_Dependent
322 if 0x1CE2 <= U <= 0x1CE8: UISC = Cantillation_Mark
325 # TODO: These don't have UISC assigned in Unicode 11.0, but have UIPC
326 if 0x0F18 <= U <= 0x0F19 or 0x0F3E <= U <= 0x0F3F: UISC = Vowel_Dependent
327 if 0x0F86 <= U <= 0x0F87: UISC = Tone_Mark
328 # Overrides to allow NFC order matching syllable
329 # https://github.com/harfbuzz/harfbuzz/issues/1012
330 if UBlock == 'Tibetan' and is_VOWEL (U, UISC, UGC):
334 # TODO: https://github.com/harfbuzz/harfbuzz/pull/982
335 # also https://github.com/harfbuzz/harfbuzz/issues/1012
336 if UBlock == 'Chakma' and is_VOWEL (U, UISC, UGC):
342 # TODO: https://github.com/harfbuzz/harfbuzz/pull/627
343 if 0x1BF2 <= U <= 0x1BF3: UISC = Nukta; UIPC = Bottom
345 # TODO: U+1CED should only be allowed after some of
346 # the nasalization marks, maybe only for U+1CE9..U+1CF1.
347 if U == 0x1CED: UISC = Tone_Mark
349 # TODO: https://github.com/harfbuzz/harfbuzz/issues/525
350 if U == 0x1A7F: UISC = Consonant_Final; UIPC = Bottom
352 # TODO: https://github.com/harfbuzz/harfbuzz/pull/609
353 if U == 0x20F0: UISC = Cantillation_Mark; UIPC = Top
355 # TODO: https://github.com/harfbuzz/harfbuzz/pull/626
356 if U == 0xA8B4: UISC = Consonant_Medial
358 # TODO: https://github.com/harfbuzz/harfbuzz/issues/1105
359 if U == 0x11134: UISC = Gemination_Mark
361 # TODO: https://github.com/harfbuzz/harfbuzz/pull/1399
362 if U == 0x111C9: UISC = Consonant_Final
364 values = [k for k,v in items if v(U,UISC,UGC)]
365 assert len(values) == 1, "%s %s %s %s" % (hex(U), UISC, UGC, values)
368 # Resolve Indic_Positional_Category
370 # TODO: Not in Unicode 8.0 yet, but in spec.
371 if U == 0x1B6C: UIPC = Bottom
373 # TODO: These should die, but have UIPC in Unicode 8.0
374 if U in [0x953, 0x954]: UIPC = Not_Applicable
376 # TODO: In USE's override list but not in Unicode 11.0
377 if U == 0x103C: UIPC = Left
379 # TODO: These are not in USE's override list that we have, nor are they in Unicode 11.0
380 if 0xA926 <= U <= 0xA92A: UIPC = Top
381 if U == 0x111CA: UIPC = Bottom
382 if U == 0x11300: UIPC = Top
383 # TODO: https://github.com/harfbuzz/harfbuzz/pull/1037
384 if U == 0x11302: UIPC = Top
385 if U == 0x1133C: UIPC = Bottom
386 if U == 0x1171E: UIPC = Left # Correct?!
387 if 0x1CF2 <= U <= 0x1CF3: UIPC = Right
388 if 0x1CF8 <= U <= 0x1CF9: UIPC = Top
389 # https://github.com/roozbehp/unicode-data/issues/8
390 if U == 0x0A51: UIPC = Bottom
392 assert (UIPC in [Not_Applicable, Visual_Order_Left] or
393 USE in use_positions), "%s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC)
395 pos_mapping = use_positions.get(USE, None)
397 values = [k for k,v in pos_mapping.items() if v and UIPC in v]
398 assert len(values) == 1, "%s %s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC, values)
399 USE = USE + values[0]
401 out[U] = (USE, UBlock)
404 defaults = ('O', 'No_Block')
405 data = map_to_use(data)
407 print ("/* == Start of generated table == */")
409 print (" * The following table is generated by running:")
411 print (" * ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt")
413 print (" * on files with these headers:")
417 print (" * %s" % (l.strip()))
420 print ('#include "hb-ot-shape-complex-use.hh"')
426 def print_block (block, start, end, data):
427 global total, used, last_block
428 if block and block != last_block:
431 print (" /* %s */" % block)
433 print (' ' * (20 + (start % 16 * 6)), end='')
435 assert start % 8 == 0
436 assert (end+1) % 8 == 0
437 for u in range (start, end+1):
440 print (" /* %04X */" % u, end='')
443 d = data.get (u, defaults)
444 print ("%6s," % d[0], end='')
446 total += end - start + 1
451 uu = sorted (data.keys ())
458 print ('#pragma GCC diagnostic push')
459 print ('#pragma GCC diagnostic ignored "-Wunused-macros"')
460 for k,v in sorted(use_mapping.items()):
461 if k in use_positions and use_positions[k]: continue
462 print ("#define %s USE_%s /* %s */" % (k, k, v.__name__[3:]))
463 for k,v in sorted(use_positions.items()):
467 print ("#define %s USE_%s" % (tag, tag))
468 print ('#pragma GCC diagnostic pop')
470 print ("static const USE_TABLE_ELEMENT_TYPE use_table[] = {")
478 while end in uu and block == data[end][1]:
480 end = (end-1)//8*8 + 7
482 if start != last + 1:
483 if start - last <= 1+16*3:
484 print_block (None, last+1, start-1, data)
488 ends.append (last + 1)
489 offset += ends[-1] - starts[-1]
492 print ("#define use_offset_0x%04xu %d" % (start, offset))
493 starts.append (start)
495 print_block (block, start, end, data)
497 ends.append (last + 1)
498 offset += ends[-1] - starts[-1]
501 occupancy = used * 100. / total
503 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
505 print ("USE_TABLE_ELEMENT_TYPE")
506 print ("hb_use_get_category (hb_codepoint_t u)")
508 print (" switch (u >> %d)" % page_bits)
510 pages = set([u>>page_bits for u in starts+ends])
511 for p in sorted(pages):
512 print (" case 0x%0Xu:" % p)
513 for (start,end) in zip (starts, ends):
514 if p not in [start>>page_bits, end>>page_bits]: continue
515 offset = "use_offset_0x%04xu" % start
516 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))
522 print (" return USE_O;")
525 for k in sorted(use_mapping.keys()):
526 if k in use_positions and use_positions[k]: continue
527 print ("#undef %s" % k)
528 for k,v in sorted(use_positions.items()):
532 print ("#undef %s" % tag)
534 print ("/* == End of generated table == */")
536 # Maintain at least 50% occupancy in the table */
538 raise Exception ("Table too sparse, please investigate: ", occupancy)