3 from __future__ import print_function, division, absolute_import
7 if len (sys.argv) != 5:
8 print ("usage: ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt", file=sys.stderr)
11 BLACKLISTED_BLOCKS = ["Thai", "Lao", "Tibetan"]
13 files = [io.open (x, encoding='utf-8') for x in sys.argv[1:]]
15 headers = [[f.readline () for i in range (2)] for j,f in enumerate(files) if j != 2]
16 headers.append (["UnicodeData.txt does not have a header."])
18 data = [{} for f in files]
19 values = [{} for f in files]
20 for i, f in enumerate (files):
27 fields = [x.strip () for x in line.split (';')]
31 uu = fields[0].split ('..')
32 start = int (uu[0], 16)
38 t = fields[1 if i != 2 else 2]
40 for u in range (start, end + 1):
42 values[i][t] = values[i].get (t, 0) + end - start + 1
44 defaults = ('Other', 'Not_Applicable', 'Cn', 'No_Block')
46 # TODO Characters that are not in Unicode Indic files, but used in USE
47 data[0][0x034F] = defaults[0]
48 data[0][0x2060] = defaults[0]
49 data[0][0x20F0] = defaults[0]
50 for u in range (0xFE00, 0xFE0F + 1):
51 data[0][u] = defaults[0]
53 # Merge data into one dict:
54 for i,v in enumerate (defaults):
55 values[i][v] = values[i].get (v, 0) + 1
57 for i,d in enumerate (data):
58 for u,v in d.items ():
59 if i >= 2 and not u in combined:
62 combined[u] = list (defaults)
64 combined = {k:v for k,v in combined.items() if v[3] not in BLACKLISTED_BLOCKS}
72 'Cc', 'Cf', 'Cn', 'Co', 'Cs', 'Ll', 'Lm', 'Lo', 'Lt', 'Lu', 'Mc',
73 'Me', 'Mn', 'Nd', 'Nl', 'No', 'Pc', 'Pd', 'Pe', 'Pf', 'Pi', 'Po',
74 'Ps', 'Sc', 'Sk', 'Sm', 'So', 'Zl', 'Zp', 'Zs',
75 # Indic_Syllabic_Category
87 'Consonant_Placeholder',
90 'Consonant_With_Stacker',
92 'Consonant_Preceding_Repha',
93 'Consonant_Succeeding_Repha',
94 'Consonant_Subjoined',
97 'Consonant_Head_Letter',
98 'Consonant_Initial_Postfixed',
111 'Brahmi_Joining_Number',
112 # Indic_Positional_Category
123 'Top_And_Left_And_Right',
126 'Top_And_Bottom_And_Right',
135 class PropertyValue(object):
136 def __init__(self, name_):
140 def __eq__(self, other):
141 return self.name == (other if isinstance(other, basestring) else other.name)
142 def __ne__(self, other):
143 return not (self == other)
145 return hash(str(self))
149 for name in property_names:
150 value = PropertyValue(name)
151 assert value not in property_values
152 assert value not in globals()
153 property_values[name] = value
154 globals().update(property_values)
157 def is_BASE(U, UISC, UGC):
158 return (UISC in [Number, Consonant, Consonant_Head_Letter,
159 #SPEC-DRAFT Consonant_Placeholder,
161 Vowel_Independent #SPEC-DRAFT
163 (UGC == Lo and UISC in [Avagraha, Bindu, Consonant_Final, Consonant_Medial,
164 Consonant_Subjoined, Vowel, Vowel_Dependent]))
165 def is_BASE_IND(U, UISC, UGC):
166 #SPEC-DRAFT return (UISC in [Consonant_Dead, Modifying_Letter] or UGC == Po)
167 return (UISC in [Consonant_Dead, Modifying_Letter] or
168 (UGC == Po and not U in [0x104B, 0x104E, 0x2022, 0x11A3F, 0x11A45]) or
169 False # SPEC-DRAFT-OUTDATED! U == 0x002D
171 def is_BASE_NUM(U, UISC, UGC):
172 return UISC == Brahmi_Joining_Number
173 def is_BASE_OTHER(U, UISC, UGC):
174 if UISC == Consonant_Placeholder: return True #SPEC-DRAFT
175 #SPEC-DRAFT return U in [0x00A0, 0x00D7, 0x2015, 0x2022, 0x25CC, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
176 return U in [0x2015, 0x2022, 0x25FB, 0x25FC, 0x25FD, 0x25FE]
177 def is_CGJ(U, UISC, UGC):
179 def is_CONS_FINAL(U, UISC, UGC):
180 # Consonant_Initial_Postfixed is new in Unicode 11; not in the spec.
181 return ((UISC == Consonant_Final and UGC != Lo) or
182 UISC == Consonant_Initial_Postfixed or
183 UISC == Consonant_Succeeding_Repha)
184 def is_CONS_FINAL_MOD(U, UISC, UGC):
185 #SPEC-DRAFT return UISC in [Consonant_Final_Modifier, Syllable_Modifier]
186 return UISC == Syllable_Modifier
187 def is_CONS_MED(U, UISC, UGC):
188 return UISC == Consonant_Medial and UGC != Lo
189 def is_CONS_MOD(U, UISC, UGC):
190 return UISC in [Nukta, Gemination_Mark, Consonant_Killer]
191 def is_CONS_SUB(U, UISC, UGC):
192 #SPEC-DRAFT return UISC == Consonant_Subjoined
193 return UISC == Consonant_Subjoined and UGC != Lo
194 def is_CONS_WITH_STACKER(U, UISC, UGC):
195 return UISC == Consonant_With_Stacker
196 def is_HALANT(U, UISC, UGC):
197 return UISC in [Virama, Invisible_Stacker]
198 def is_HALANT_NUM(U, UISC, UGC):
199 return UISC == Number_Joiner
200 def is_ZWNJ(U, UISC, UGC):
201 return UISC == Non_Joiner
202 def is_ZWJ(U, UISC, UGC):
203 return UISC == Joiner
204 def is_Word_Joiner(U, UISC, UGC):
206 def is_OTHER(U, UISC, UGC):
207 #SPEC-OUTDATED return UGC == Zs # or any other SCRIPT_COMMON characters
208 return (UISC == Other
209 and not is_SYM_MOD(U, UISC, UGC)
210 and not is_CGJ(U, UISC, UGC)
211 and not is_Word_Joiner(U, UISC, UGC)
212 and not is_VARIATION_SELECTOR(U, UISC, UGC)
214 def is_Reserved(U, UISC, UGC):
216 def is_REPHA(U, UISC, UGC):
217 return UISC in [Consonant_Preceding_Repha, Consonant_Prefixed]
218 def is_SYM(U, UISC, UGC):
219 if U == 0x25CC: return False #SPEC-DRAFT
220 #SPEC-DRAFT return UGC in [So, Sc] or UISC == Symbol_Letter
221 return UGC in [So, Sc]
222 def is_SYM_MOD(U, UISC, UGC):
223 return U in [0x1B6B, 0x1B6C, 0x1B6D, 0x1B6E, 0x1B6F, 0x1B70, 0x1B71, 0x1B72, 0x1B73]
224 def is_VARIATION_SELECTOR(U, UISC, UGC):
225 return 0xFE00 <= U <= 0xFE0F
226 def is_VOWEL(U, UISC, UGC):
227 # https://github.com/roozbehp/unicode-data/issues/6
228 return (UISC == Pure_Killer or
229 (UGC != Lo and UISC in [Vowel, Vowel_Dependent] and U not in [0xAA29]))
230 def is_VOWEL_MOD(U, UISC, UGC):
231 # https://github.com/roozbehp/unicode-data/issues/6
232 return (UISC in [Tone_Mark, Cantillation_Mark, Register_Shifter, Visarga] or
233 (UGC != Lo and (UISC == Bindu or U in [0xAA29])))
242 'FM': is_CONS_FINAL_MOD,
246 'CS': is_CONS_WITH_STACKER,
251 'WJ': is_Word_Joiner,
257 'VS': is_VARIATION_SELECTOR,
270 'Blw': [Bottom, Bottom_And_Left],
279 'Abv': [Top, Top_And_Bottom, Top_And_Bottom_And_Right, Top_And_Right],
280 'Blw': [Bottom, Overstruck, Bottom_And_Right],
282 'Pre': [Left, Top_And_Left, Top_And_Left_And_Right, Left_And_Right],
286 'Blw': [Bottom, Overstruck],
300 def map_to_use(data):
302 items = use_mapping.items()
303 for U,(UISC,UIPC,UGC,UBlock) in data.items():
305 # Resolve Indic_Syllabic_Category
307 # TODO: These don't have UISC assigned in Unicode 8.0, but
309 if U == 0x17DD: UISC = Vowel_Dependent
310 if 0x1CE2 <= U <= 0x1CE8: UISC = Cantillation_Mark
312 # TODO: https://github.com/harfbuzz/harfbuzz/pull/627
313 if 0x1BF2 <= U <= 0x1BF3: UISC = Nukta; UIPC = Bottom
315 # TODO: U+1CED should only be allowed after some of
316 # the nasalization marks, maybe only for U+1CE9..U+1CF1.
317 if U == 0x1CED: UISC = Tone_Mark
319 # TODO: https://github.com/harfbuzz/harfbuzz/issues/525
320 if U == 0x1A7F: UISC = Consonant_Final; UIPC = Bottom
322 # TODO: https://github.com/harfbuzz/harfbuzz/pull/609
323 if U == 0x20F0: UISC = Cantillation_Mark; UIPC = Top
325 # TODO: https://github.com/harfbuzz/harfbuzz/pull/626
326 if U == 0xA8B4: UISC = Consonant_Medial
328 values = [k for k,v in items if v(U,UISC,UGC)]
329 assert len(values) == 1, "%s %s %s %s" % (hex(U), UISC, UGC, values)
332 # Resolve Indic_Positional_Category
334 # TODO: Not in Unicode 8.0 yet, but in spec.
335 if U == 0x1B6C: UIPC = Bottom
337 # TODO: These should die, but have UIPC in Unicode 8.0
338 if U in [0x953, 0x954]: UIPC = Not_Applicable
340 # TODO: In USE's override list but not in Unicode 11.0
341 if U == 0x103C: UIPC = Left
343 # TODO: These are not in USE's override list that we have, nor are they in Unicode 11.0
344 if 0xA926 <= U <= 0xA92A: UIPC = Top
345 if U == 0x111CA: UIPC = Bottom
346 if U == 0x11300: UIPC = Top
347 if U == 0x1171E: UIPC = Left # Correct?!
348 if 0x1CF2 <= U <= 0x1CF3: UIPC = Right
349 if 0x1CF8 <= U <= 0x1CF9: UIPC = Top
350 # https://github.com/roozbehp/unicode-data/issues/8
351 if U == 0x0A51: UIPC = Bottom
353 assert (UIPC in [Not_Applicable, Visual_Order_Left] or
354 USE in use_positions), "%s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC)
356 pos_mapping = use_positions.get(USE, None)
358 values = [k for k,v in pos_mapping.items() if v and UIPC in v]
359 assert len(values) == 1, "%s %s %s %s %s %s" % (hex(U), UIPC, USE, UISC, UGC, values)
360 USE = USE + values[0]
362 out[U] = (USE, UBlock)
365 defaults = ('O', 'No_Block')
366 data = map_to_use(data)
368 print ("/* == Start of generated table == */")
370 print (" * The following table is generated by running:")
372 print (" * ./gen-use-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt UnicodeData.txt Blocks.txt")
374 print (" * on files with these headers:")
378 print (" * %s" % (l.strip()))
381 print ('#include "hb-ot-shape-complex-use-private.hh"')
387 def print_block (block, start, end, data):
388 global total, used, last_block
389 if block and block != last_block:
392 print (" /* %s */" % block)
394 print (' ' * (20 + (start % 16 * 6)), end='')
396 assert start % 8 == 0
397 assert (end+1) % 8 == 0
398 for u in range (start, end+1):
401 print (" /* %04X */" % u, end='')
404 d = data.get (u, defaults)
405 print ("%6s," % d[0], end='')
407 total += end - start + 1
412 uu = sorted (data.keys ())
419 for k,v in sorted(use_mapping.items()):
420 if k in use_positions and use_positions[k]: continue
421 print ("#define %s USE_%s /* %s */" % (k, k, v.__name__[3:]))
422 for k,v in sorted(use_positions.items()):
426 print ("#define %s USE_%s" % (tag, tag))
428 print ("static const USE_TABLE_ELEMENT_TYPE use_table[] = {")
436 while end in uu and block == data[end][1]:
438 end = (end-1)//8*8 + 7
440 if start != last + 1:
441 if start - last <= 1+16*3:
442 print_block (None, last+1, start-1, data)
446 ends.append (last + 1)
447 offset += ends[-1] - starts[-1]
450 print ("#define use_offset_0x%04xu %d" % (start, offset))
451 starts.append (start)
453 print_block (block, start, end, data)
455 ends.append (last + 1)
456 offset += ends[-1] - starts[-1]
459 occupancy = used * 100. / total
461 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset, occupancy))
463 print ("USE_TABLE_ELEMENT_TYPE")
464 print ("hb_use_get_category (hb_codepoint_t u)")
466 print (" switch (u >> %d)" % page_bits)
468 pages = set([u>>page_bits for u in starts+ends])
469 for p in sorted(pages):
470 print (" case 0x%0Xu:" % p)
471 for (start,end) in zip (starts, ends):
472 if p not in [start>>page_bits, end>>page_bits]: continue
473 offset = "use_offset_0x%04xu" % start
474 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))
480 print (" return USE_O;")
483 for k in sorted(use_mapping.keys()):
484 if k in use_positions and use_positions[k]: continue
485 print ("#undef %s" % k)
486 for k,v in sorted(use_positions.items()):
490 print ("#undef %s" % tag)
492 print ("/* == End of generated table == */")
494 # Maintain at least 50% occupancy in the table */
496 raise Exception ("Table too sparse, please investigate: ", occupancy)