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][0x1B61] = defaults[0]
51 data[0][0x1B63] = defaults[0]
52 data[0][0x1B64] = defaults[0]
53 data[0][0x1B65] = defaults[0]
54 data[0][0x1B66] = defaults[0]
55 data[0][0x1B67] = defaults[0]
56 data[0][0x1B69] = defaults[0]
57 data[0][0x1B6A] = defaults[0]
58 data[0][0x2060] = defaults[0]
59 # TODO https://github.com/harfbuzz/harfbuzz/pull/1685
60 data[0][0x1B5B] = 'Consonant_Placeholder'
61 data[0][0x1B5C] = 'Consonant_Placeholder'
62 data[0][0x1B5F] = 'Consonant_Placeholder'
63 data[0][0x1B62] = 'Consonant_Placeholder'
64 data[0][0x1B68] = 'Consonant_Placeholder'
65 # TODO https://github.com/harfbuzz/harfbuzz/issues/1035
66 data[0][0x11C44] = 'Consonant_Placeholder'
67 data[0][0x11C45] = 'Consonant_Placeholder'
68 # TODO https://github.com/harfbuzz/harfbuzz/pull/1399
69 data[0][0x111C8] = 'Consonant_Placeholder'
70 for u in range (0xFE00, 0xFE0F + 1):
71 data[0][u] = defaults[0]
73 # Merge data into one dict:
74 for i,v in enumerate (defaults):
75 values[i][v] = values[i].get (v, 0) + 1
77 for i,d in enumerate (data):
78 for u,v in d.items ():
79 if i >= 2 and not u in combined:
82 combined[u] = list (defaults)
84 combined = {k:v for k,v in combined.items() if v[3] not in BLACKLISTED_BLOCKS}
92 'Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc',
93 'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po',
94 'Ps', 'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs',
95 # Indic_Syllabic_Category
107 'Consonant_Placeholder',
110 'Consonant_With_Stacker',
111 'Consonant_Prefixed',
112 'Consonant_Preceding_Repha',
113 'Consonant_Succeeding_Repha',
114 'Consonant_Subjoined',
117 'Consonant_Head_Letter',
118 'Consonant_Initial_Postfixed',
131 'Brahmi_Joining_Number',
132 # Indic_Positional_Category
143 'Top_And_Left_And_Right',
146 'Top_And_Bottom_And_Right',
155 class PropertyValue(object):
156 def __init__(self, name_):
160 def __eq__(self, other):
161 return self.name == (other if isinstance(other, basestring) else other.name)
162 def __ne__(self, other):
163 return not (self == other)
165 return hash(str(self))
169 for name in property_names:
170 value = PropertyValue(name)
171 assert value not in property_values
172 assert value not in globals()
173 property_values[name] = value
174 globals().update(property_values)
177 def is_BASE(U, UISC, UGC):
178 return (UISC in [Number, Consonant, Consonant_Head_Letter,
179 #SPEC-DRAFT Consonant_Placeholder,
181 Vowel_Independent #SPEC-DRAFT
183 (UGC == Lo and UISC in [Avagraha, Bindu, Consonant_Final, Consonant_Medial,
184 Consonant_Subjoined, Vowel, Vowel_Dependent]))
185 def is_BASE_IND(U, UISC, UGC):
186 #SPEC-DRAFT return (UISC in [Consonant_Dead, Modifying_Letter] or UGC == Po)
187 return (UISC in [Consonant_Dead, Modifying_Letter] or
188 (UGC == Po and not U in [0x104B, 0x104E, 0x1B5B, 0x1B5C, 0x1B5F, 0x2022, 0x111C8, 0x11A3F, 0x11A45, 0x11C44, 0x11C45]) or
189 False # SPEC-DRAFT-OUTDATED! U == 0x002D
191 def is_BASE_NUM(U, UISC, UGC):
192 return UISC == Brahmi_Joining_Number
193 def is_BASE_OTHER(U, UISC, UGC):
194 if UISC == Consonant_Placeholder: return True #SPEC-DRAFT
195 #SPEC-DRAFT return U in [0x00A0, 0x00D7, 0x2015, 0x2022, 0x25CC, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
196 return U in [0x2015, 0x2022, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
197 def is_CGJ(U, UISC, UGC):
199 def is_CONS_FINAL(U, UISC, UGC):
200 return ((UISC == Consonant_Final and UGC != Lo) or
201 UISC == Consonant_Succeeding_Repha)
202 def is_CONS_FINAL_MOD(U, UISC, UGC):
203 #SPEC-DRAFT return UISC in [Consonant_Final_Modifier, Syllable_Modifier]
204 return UISC == Syllable_Modifier
205 def is_CONS_MED(U, UISC, UGC):
206 # Consonant_Initial_Postfixed is new in Unicode 11; not in the spec.
207 return (UISC == Consonant_Medial and UGC != Lo or
208 UISC == Consonant_Initial_Postfixed)
209 def is_CONS_MOD(U, UISC, UGC):
210 return UISC in [Nukta, Gemination_Mark, Consonant_Killer]
211 def is_CONS_SUB(U, UISC, UGC):
212 #SPEC-DRAFT return UISC == Consonant_Subjoined
213 return UISC == Consonant_Subjoined and UGC != Lo
214 def is_CONS_WITH_STACKER(U, UISC, UGC):
215 return UISC == Consonant_With_Stacker
216 def is_HALANT(U, UISC, UGC):
217 return (UISC in [Virama, Invisible_Stacker]
218 and not is_HALANT_OR_VOWEL_MODIFIER(U, UISC, UGC)
219 and not is_SAKOT(U, UISC, UGC))
220 def is_HALANT_OR_VOWEL_MODIFIER(U, UISC, UGC):
221 # https://github.com/harfbuzz/harfbuzz/issues/1102
222 # https://github.com/harfbuzz/harfbuzz/issues/1379
223 return U in [0x11046, 0x1134D]
224 def is_HALANT_NUM(U, UISC, UGC):
225 return UISC == Number_Joiner
226 def is_ZWNJ(U, UISC, UGC):
227 return UISC == Non_Joiner
228 def is_ZWJ(U, UISC, UGC):
229 return UISC == Joiner
230 def is_Word_Joiner(U, UISC, UGC):
232 def is_OTHER(U, UISC, UGC):
233 #SPEC-OUTDATED return UGC == Zs # or any other SCRIPT_COMMON characters
234 return (UISC == Other
235 and not is_SYM(U, UISC, UGC)
236 and not is_SYM_MOD(U, UISC, UGC)
237 and not is_CGJ(U, UISC, UGC)
238 and not is_Word_Joiner(U, UISC, UGC)
239 and not is_VARIATION_SELECTOR(U, UISC, UGC)
241 def is_Reserved(U, UISC, UGC):
243 def is_REPHA(U, UISC, UGC):
244 return UISC in [Consonant_Preceding_Repha, Consonant_Prefixed]
245 def is_SAKOT(U, UISC, UGC):
247 def is_SYM(U, UISC, UGC):
248 if U == 0x25CC: return False #SPEC-DRAFT
249 #SPEC-DRAFT return UGC in [So, Sc] or UISC == Symbol_Letter
250 return UGC in [So, Sc] and U not in [0x1B62, 0x1B68]
251 def is_SYM_MOD(U, UISC, UGC):
252 return U in [0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E, 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73]
253 def is_VARIATION_SELECTOR(U, UISC, UGC):
254 return 0xFE00 <= U <= 0xFE0F
255 def is_VOWEL(U, UISC, UGC):
256 # https://github.com/harfbuzz/harfbuzz/issues/376
257 return (UISC == Pure_Killer or
258 (UGC != Lo and UISC in [Vowel, Vowel_Dependent] and U not in [0xAA29]))
259 def is_VOWEL_MOD(U, UISC, UGC):
260 # https://github.com/harfbuzz/harfbuzz/issues/376
261 return (UISC in [Tone_Mark, Cantillation_Mark, Register_Shifter, Visarga] or
262 (UGC != Lo and (UISC == Bindu or U in [0xAA29])))
271 'FM': is_CONS_FINAL_MOD,
275 'CS': is_CONS_WITH_STACKER,
277 'HVM': is_HALANT_OR_VOWEL_MODIFIER,
281 'WJ': is_Word_Joiner,
288 'VS': is_VARIATION_SELECTOR,
301 'Blw': [Bottom, Bottom_And_Left],
310 'Abv': [Top, Top_And_Bottom, Top_And_Bottom_And_Right, Top_And_Right],
311 'Blw': [Bottom, Overstruck, Bottom_And_Right],
312 'Pst': [Right, Top_And_Left, Top_And_Left_And_Right, Left_And_Right],
317 'Blw': [Bottom, Overstruck],
331 'Pst': [Not_Applicable],
336 def map_to_use(data):
338 items = use_mapping.items()
339 for U,(UISC,UIPC,UGC,UBlock) in data.items():
341 # Resolve Indic_Syllabic_Category
343 # TODO: These don't have UISC assigned in Unicode 12.0, but have UIPC
344 if 0x1CE2 <= U <= 0x1CE8: UISC = Cantillation_Mark
347 # TODO: These don't have UISC assigned in Unicode 12.0, but have UIPC
348 if 0x0F18 <= U <= 0x0F19 or 0x0F3E <= U <= 0x0F3F: UISC = Vowel_Dependent
349 if 0x0F86 <= U <= 0x0F87: UISC = Tone_Mark
350 # Overrides to allow NFC order matching syllable
351 # https://github.com/harfbuzz/harfbuzz/issues/1012
352 if UBlock == 'Tibetan' and is_VOWEL (U, UISC, UGC):
356 # TODO: https://github.com/harfbuzz/harfbuzz/pull/982
357 # also https://github.com/harfbuzz/harfbuzz/issues/1012
358 if UBlock == 'Chakma' and is_VOWEL (U, UISC, UGC):
364 # TODO: https://github.com/harfbuzz/harfbuzz/pull/627
365 if 0x1BF2 <= U <= 0x1BF3: UISC = Nukta; UIPC = Bottom
367 # TODO: U+1CED should only be allowed after some of
368 # the nasalization marks, maybe only for U+1CE9..U+1CF1.
369 if U == 0x1CED: UISC = Tone_Mark
371 # TODO: https://github.com/harfbuzz/harfbuzz/issues/1105
372 if U == 0x11134: UISC = Gemination_Mark
374 values = [k for k,v in items if v(U,UISC,UGC)]
375 assert len(values) == 1, "%s %s %s %s" % (hex(U), UISC, UGC, values)
378 # Resolve Indic_Positional_Category
380 # TODO: These should die, but have UIPC in Unicode 12.0
381 if U in [0x953, 0x954]: UIPC = Not_Applicable
383 # TODO: In USE's override list but not in Unicode 12.0
384 if U == 0x103C: UIPC = Left
386 # TODO: https://github.com/harfbuzz/harfbuzz/pull/2012
387 if U == 0x1C29: UIPC = Left
389 # TODO: These are not in USE's override list that we have, nor are they in Unicode 12.0
390 if 0xA926 <= U <= 0xA92A: UIPC = Top
391 # TODO: https://github.com/harfbuzz/harfbuzz/pull/1037
392 # and https://github.com/harfbuzz/harfbuzz/issues/1631
393 if U in [0x11302, 0x11303, 0x114C1]: UIPC = Top
394 if U == 0x1171E: UIPC = Left
395 if 0x1CF8 <= U <= 0x1CF9: UIPC = Top
397 assert (UIPC in [Not_Applicable, Visual_Order_Left] or
398 USE in use_positions), "%s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC)
400 pos_mapping = use_positions.get(USE, None)
402 values = [k for k,v in pos_mapping.items() if v and UIPC in v]
403 assert len(values) == 1, "%s %s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC, values)
404 USE = USE + values[0]
406 out[U] = (USE, UBlock)
409 defaults = ('O', 'No_Block')
410 data = map_to_use(data)
412 print ("/* == Start of generated table == */")
414 print (" * The following table is generated by running:")
416 print (" * ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt")
418 print (" * on files with these headers:")
422 print (" * %s" % (l.strip()))
425 print ('#include "hb.hh"')
427 print ('#ifndef HB_NO_OT_SHAPE')
429 print ('#include "hb-ot-shape-complex-use.hh"')
435 def print_block (block, start, end, data):
436 global total, used, last_block
437 if block and block != last_block:
440 print (" /* %s */" % block)
442 print (' ' * (20 + (start % 16 * 6)), end='')
444 assert start % 8 == 0
445 assert (end+1) % 8 == 0
446 for u in range (start, end+1):
449 print (" /* %04X */" % u, end='')
452 d = data.get (u, defaults)
453 print ("%6s," % d[0], end='')
455 total += end - start + 1
460 uu = sorted (data.keys ())
467 print ('#pragma GCC diagnostic push')
468 print ('#pragma GCC diagnostic ignored "-Wunused-macros"')
469 for k,v in sorted(use_mapping.items()):
470 if k in use_positions and use_positions[k]: continue
471 print ("#define %s USE_%s /* %s */" % (k, k, v.__name__[3:]))
472 for k,v in sorted(use_positions.items()):
476 print ("#define %s USE_%s" % (tag, tag))
477 print ('#pragma GCC diagnostic pop')
479 print ("static const USE_TABLE_ELEMENT_TYPE use_table[] = {")
487 while end in uu and block == data[end][1]:
489 end = (end-1)//8*8 + 7
491 if start != last + 1:
492 if start - last <= 1+16*3:
493 print_block (None, last+1, start-1, data)
497 ends.append (last + 1)
498 offset += ends[-1] - starts[-1]
501 print ("#define use_offset_0x%04xu %d" % (start, offset))
502 starts.append (start)
504 print_block (block, start, end, data)
506 ends.append (last + 1)
507 offset += ends[-1] - starts[-1]
510 occupancy = used * 100. / total
512 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
514 print ("USE_TABLE_ELEMENT_TYPE")
515 print ("hb_use_get_category (hb_codepoint_t u)")
517 print (" switch (u >> %d)" % page_bits)
519 pages = set([u>>page_bits for u in starts+ends])
520 for p in sorted(pages):
521 print (" case 0x%0Xu:" % p)
522 for (start,end) in zip (starts, ends):
523 if p not in [start>>page_bits, end>>page_bits]: continue
524 offset = "use_offset_0x%04xu" % start
525 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))
531 print (" return USE_O;")
534 for k in sorted(use_mapping.keys()):
535 if k in use_positions and use_positions[k]: continue
536 print ("#undef %s" % k)
537 for k,v in sorted(use_positions.items()):
541 print ("#undef %s" % tag)
545 print ("/* == End of generated table == */")
547 # Maintain at least 50% occupancy in the table */
549 raise Exception ("Table too sparse, please investigate: ", occupancy)