3 """usage: ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt
6 * https://unicode.org/Public/UCD/latest/ucd/IndicSyllabicCategory.txt
7 * https://unicode.org/Public/UCD/latest/ucd/IndicPositionalCategory.txt
8 * https://unicode.org/Public/UCD/latest/ucd/Blocks.txt
13 if len (sys
.argv
) != 4:
16 ALLOWED_SINGLES
= [0x00A0, 0x25CC]
32 'General Punctuation',
33 'Superscripts and Subscripts',
34 'Devanagari Extended',
39 files
= [open (x
, encoding
='utf-8') for x
in sys
.argv
[1:]]
41 headers
= [[f
.readline () for i
in range (2)] for f
in files
]
43 unicode_data
= [{} for _
in files
]
44 for i
, f
in enumerate (files
):
51 fields
= [x
.strip () for x
in line
.split (';')]
55 uu
= fields
[0].split ('..')
56 start
= int (uu
[0], 16)
64 for u
in range (start
, end
+ 1):
65 unicode_data
[i
][u
] = t
67 # Merge data into one dict:
68 defaults
= ('Other', 'Not_Applicable', 'No_Block')
70 for i
,d
in enumerate (unicode_data
):
71 for u
,v
in d
.items ():
72 if i
== 2 and not u
in combined
:
75 combined
[u
] = list (defaults
)
77 combined
= {k
:v
for k
,v
in combined
.items() if k
in ALLOWED_SINGLES
or v
[2] in ALLOWED_BLOCKS
}
80 # Convert categories & positions types
137 'Avagraha' : 'Symbol',
139 'Brahmi_Joining_Number' : 'PLACEHOLDER', # Don't care.
140 'Cantillation_Mark' : 'A',
142 'Consonant_Dead' : 'C',
143 'Consonant_Final' : 'CM',
144 'Consonant_Head_Letter' : 'C',
145 'Consonant_Initial_Postfixed' : 'C', # TODO
146 'Consonant_Killer' : 'M', # U+17CD only.
147 'Consonant_Medial' : 'CM',
148 'Consonant_Placeholder' : 'PLACEHOLDER',
149 'Consonant_Preceding_Repha' : 'Repha',
150 'Consonant_Prefixed' : 'X', # Don't care.
151 'Consonant_Subjoined' : 'CM',
152 'Consonant_Succeeding_Repha' : 'CM',
153 'Consonant_With_Stacker' : 'CS',
154 'Gemination_Mark' : 'SM', # https://github.com/harfbuzz/harfbuzz/issues/552
155 'Invisible_Stacker' : 'H',
157 'Modifying_Letter' : 'X',
158 'Non_Joiner' : 'ZWNJ',
160 'Number' : 'PLACEHOLDER',
161 'Number_Joiner' : 'PLACEHOLDER', # Don't care.
162 'Pure_Killer' : 'M', # Is like a vowel matra.
163 'Register_Shifter' : 'RS',
164 'Syllable_Modifier' : 'SM',
170 'Vowel_Dependent' : 'M',
171 'Vowel_Independent' : 'V',
174 'Not_Applicable' : 'END',
178 'Bottom' : 'BELOW_C',
181 # These should resolve to the position of the last part of the split sequence.
182 'Bottom_And_Right' : 'POST_C',
183 'Left_And_Right' : 'POST_C',
184 'Top_And_Bottom' : 'BELOW_C',
185 'Top_And_Bottom_And_Left' : 'BELOW_C',
186 'Top_And_Bottom_And_Right' : 'POST_C',
187 'Top_And_Left' : 'ABOVE_C',
188 'Top_And_Left_And_Right' : 'POST_C',
189 'Top_And_Right' : 'POST_C',
191 'Overstruck' : 'AFTER_MAIN',
192 'Visual_order_left' : 'PRE_M',
195 category_overrides
= {
197 # These are the variation-selectors. They only appear in the Myanmar grammar
198 # but are not Myanmar-specific
216 # These appear in the OT Myanmar spec, but are not Myanmar-specific
217 0x2015: 'PLACEHOLDER',
218 0x2022: 'PLACEHOLDER',
219 0x25FB: 'PLACEHOLDER',
220 0x25FC: 'PLACEHOLDER',
221 0x25FD: 'PLACEHOLDER',
222 0x25FE: 'PLACEHOLDER',
227 0x0930: 'Ra', # Devanagari
228 0x09B0: 'Ra', # Bengali
229 0x09F0: 'Ra', # Bengali
230 0x0A30: 'Ra', # Gurmukhi No Reph
231 0x0AB0: 'Ra', # Gujarati
232 0x0B30: 'Ra', # Oriya
233 0x0BB0: 'Ra', # Tamil No Reph
234 0x0C30: 'Ra', # Telugu Reph formed only with ZWJ
235 0x0CB0: 'Ra', # Kannada
236 0x0D30: 'Ra', # Malayalam No Reph, Logical Repha
238 # The following act more like the Bindus.
242 # U+0A40 GURMUKHI VOWEL SIGN II may be preceded by U+0A02 GURMUKHI SIGN BINDI.
245 # The following act like consonants.
251 # TODO: The following should only be allowed after a Visarga.
252 # For now, just treat them like regular tone marks.
261 # TODO: The following should only be allowed after some of
262 # the nasalization marks, maybe only for U+1CE9..U+1CF1.
263 # For now, just treat them like tone marks.
266 # The following take marks in standalone clusters, similar to Avagraha.
282 0x0A51: 'M', # https://github.com/harfbuzz/harfbuzz/issues/524
284 # According to ScriptExtensions.txt, these Grantha marks may also be used in Tamil,
285 # so the Indic shaper needs to know their categories.
292 0x0AFB: 'N', # https://github.com/harfbuzz/harfbuzz/issues/552
293 0x0B55: 'N', # https://github.com/harfbuzz/harfbuzz/issues/2849
295 0x09FC: 'PLACEHOLDER', # https://github.com/harfbuzz/harfbuzz/pull/1613
296 0x0C80: 'PLACEHOLDER', # https://github.com/harfbuzz/harfbuzz/pull/623
297 0x0D04: 'PLACEHOLDER', # https://github.com/harfbuzz/harfbuzz/pull/3511
299 0x25CC: 'DOTTEDCIRCLE',
321 0x17D3: 'Ygroup', # Just guessing. Uniscribe doesn't categorize it.
323 0x17D9: 'PLACEHOLDER', # https://github.com/harfbuzz/harfbuzz/issues/2384
328 # https://docs.microsoft.com/en-us/typography/script-development/myanmar#analyze
330 0x104E: 'C', # The spec says C, IndicSyllableCategory says Consonant_Placeholder
341 #0x1040: 'D0', # XXX The spec says D0, but Uniscribe doesn't seem to do.
374 0x104A: 'PLACEHOLDER',
376 position_overrides
= {
378 0x0A51: 'BELOW_C', # https://github.com/harfbuzz/harfbuzz/issues/524
380 0x0B01: 'BEFORE_SUB', # Oriya Bindu is BeforeSub in the spec.
383 def matra_pos_left(u
, block
):
385 def matra_pos_right(u
, block
):
386 if block
== 'Devanagari': return 'AFTER_SUB'
387 if block
== 'Bengali': return 'AFTER_POST'
388 if block
== 'Gurmukhi': return 'AFTER_POST'
389 if block
== 'Gujarati': return 'AFTER_POST'
390 if block
== 'Oriya': return 'AFTER_POST'
391 if block
== 'Tamil': return 'AFTER_POST'
392 if block
== 'Telugu': return 'BEFORE_SUB' if u
<= 0x0C42 else 'AFTER_SUB'
393 if block
== 'Kannada': return 'BEFORE_SUB' if u
< 0x0CC3 or u
> 0x0CD6 else 'AFTER_SUB'
394 if block
== 'Malayalam': return 'AFTER_POST'
396 def matra_pos_top(u
, block
):
397 # BENG and MLYM don't have top matras.
398 if block
== 'Devanagari': return 'AFTER_SUB'
399 if block
== 'Gurmukhi': return 'AFTER_POST' # Deviate from spec
400 if block
== 'Gujarati': return 'AFTER_SUB'
401 if block
== 'Oriya': return 'AFTER_MAIN'
402 if block
== 'Tamil': return 'AFTER_SUB'
403 if block
== 'Telugu': return 'BEFORE_SUB'
404 if block
== 'Kannada': return 'BEFORE_SUB'
406 def matra_pos_bottom(u
, block
):
407 if block
== 'Devanagari': return 'AFTER_SUB'
408 if block
== 'Bengali': return 'AFTER_SUB'
409 if block
== 'Gurmukhi': return 'AFTER_POST'
410 if block
== 'Gujarati': return 'AFTER_POST'
411 if block
== 'Oriya': return 'AFTER_SUB'
412 if block
== 'Tamil': return 'AFTER_POST'
413 if block
== 'Telugu': return 'BEFORE_SUB'
414 if block
== 'Kannada': return 'BEFORE_SUB'
415 if block
== 'Malayalam': return 'AFTER_POST'
417 def indic_matra_position(u
, pos
, block
): # Reposition matra
418 if pos
== 'PRE_C': return matra_pos_left(u
, block
)
419 if pos
== 'POST_C': return matra_pos_right(u
, block
)
420 if pos
== 'ABOVE_C': return matra_pos_top(u
, block
)
421 if pos
== 'BELOW_C': return matra_pos_bottom(u
, block
)
424 def position_to_category(pos
):
425 if pos
== 'PRE_C': return 'VPre'
426 if pos
== 'ABOVE_C': return 'VAbv'
427 if pos
== 'BELOW_C': return 'VBlw'
428 if pos
== 'POST_C': return 'VPst'
432 defaults
= (category_map
[defaults
[0]], position_map
[defaults
[1]], defaults
[2])
435 for k
, (cat
, pos
, block
) in combined
.items():
436 cat
= category_map
[cat
]
437 pos
= position_map
[pos
]
438 indic_data
[k
] = (cat
, pos
, block
)
440 for k
,new_cat
in category_overrides
.items():
441 (cat
, pos
, _
) = indic_data
.get(k
, defaults
)
442 indic_data
[k
] = (new_cat
, pos
, unicode_data
[2][k
])
444 # We only expect position for certain types
445 positioned_categories
= ('CM', 'SM', 'RS', 'H', 'M', 'MPst')
446 for k
, (cat
, pos
, block
) in indic_data
.items():
447 if cat
not in positioned_categories
:
449 indic_data
[k
] = (cat
, pos
, block
)
451 # Position overrides are more complicated
453 # Keep in sync with CONSONANT_FLAGS in the shaper
454 consonant_categories
= ('C', 'CS', 'Ra','CM', 'V', 'PLACEHOLDER', 'DOTTEDCIRCLE')
455 matra_categories
= ('M', 'MPst')
456 smvd_categories
= ('SM', 'VD', 'A', 'Symbol')
457 for k
, (cat
, pos
, block
) in indic_data
.items():
458 if cat
in consonant_categories
:
460 elif cat
in matra_categories
:
461 if block
.startswith('Khmer') or block
.startswith('Myanmar'):
462 cat
= position_to_category(pos
)
464 pos
= indic_matra_position(k
, pos
, block
)
465 elif cat
in smvd_categories
:
467 indic_data
[k
] = (cat
, pos
, block
)
469 for k
,new_pos
in position_overrides
.items():
470 (cat
, pos
, _
) = indic_data
.get(k
, defaults
)
471 indic_data
[k
] = (cat
, new_pos
, unicode_data
[2][k
])
474 values
= [{_
: 1} for _
in defaults
]
475 for vv
in indic_data
.values():
476 for i
,v
in enumerate(vv
):
477 values
[i
][v
] = values
[i
].get (v
, 0) + 1
482 # Move the outliers NO-BREAK SPACE and DOTTED CIRCLE out
484 for u
in ALLOWED_SINGLES
:
485 singles
[u
] = indic_data
[u
]
488 print ("/* == Start of generated table == */")
490 print (" * The following table is generated by running:")
492 print (" * ./gen-indic-table.py IndicSyllabicCategory.txt IndicPositionalCategory.txt Blocks.txt")
494 print (" * on files with these headers:")
498 print (" * %s" % (l
.strip()))
501 print ('#include "hb.hh"')
503 print ('#ifndef HB_NO_OT_SHAPE')
505 print ('#include "hb-ot-shaper-indic.hh"')
507 print ('#pragma GCC diagnostic push')
508 print ('#pragma GCC diagnostic ignored "-Wunused-macros"')
512 for shaper
in categories
:
513 print ('#include "hb-ot-shaper-%s-machine.hh"' % shaper
)
516 for shaper
, shaper_cats
in categories
.items():
517 print ('/* %s */' % shaper
)
518 for cat
in shaper_cats
:
519 v
= shaper
[0].upper()
521 print ("#define OT_%s %s_Cat(%s)" % (cat
, v
, cat
))
524 print ('static_assert (OT_%s == %s_Cat(%s), "");' % (cat
, v
, cat
))
531 "DOTTEDCIRCLE": 'DC',
554 # Add some of the values, to make them more readable, and to avoid duplicates
557 for v
,s
in short
[i
].items ():
561 what_short
= ["_OT", "_POS"]
564 vv
= sorted (values
[i
].keys ())
566 v_no_and
= v
.replace ('_And_', '_')
570 s
= ''.join ([c
for c
in v_no_and
if ord ('A') <= ord (c
) <= ord ('Z')])
571 if s
in all_shorts
[i
]:
572 raise Exception ("Duplicate short value alias", v
, all_shorts
[i
][s
])
575 cat_defs
.append ((what_short
[i
] + '_' + s
, what
[i
] + '_' + (v
.upper () if i
else v
), str (values
[i
][v
]), v
))
577 maxlen_s
= max ([len (c
[0]) for c
in cat_defs
])
578 maxlen_l
= max ([len (c
[1]) for c
in cat_defs
])
579 maxlen_n
= max ([len (c
[2]) for c
in cat_defs
])
582 for c
in [c
for c
in cat_defs
if s
in c
[0]]:
583 print ("#define %s %s /* %s chars; %s */" %
584 (c
[0].ljust (maxlen_s
), c
[1].ljust (maxlen_l
), c
[2].rjust (maxlen_n
), c
[3]))
586 print ('#pragma GCC diagnostic pop')
588 print ("#define INDIC_COMBINE_CATEGORIES(S,M) ((S) | ((M) << 8))")
590 print ("#define _(S,M) INDIC_COMBINE_CATEGORIES (%s_##S, %s_##M)" % tuple(what_short
))
597 def print_block (block
, start
, end
, data
):
598 global total
, used
, last_block
599 if block
and block
!= last_block
:
602 print (" /* %s */" % block
)
604 assert start
% 8 == 0
605 assert (end
+1) % 8 == 0
606 for u
in range (start
, end
+1):
609 print (" /* %04X */" % u
, end
="")
612 d
= data
.get (u
, defaults
)
613 print ("%9s" % ("_(%s,%s)," % (short
[0][d
[0]], short
[1][d
[1]])), end
="")
615 total
+= end
- start
+ 1
620 uu
= sorted (indic_data
)
627 print ("static const uint16_t indic_table[] = {")
631 block
= indic_data
[u
][2]
635 while end
in uu
and block
== indic_data
[end
][2]:
637 end
= (end
-1)//8*8 + 7
639 if start
!= last
+ 1:
640 if start
- last
<= 1+16*2:
641 print_block (None, last
+1, start
-1, indic_data
)
644 ends
.append (last
+ 1)
645 offset
+= ends
[-1] - starts
[-1]
648 print ("#define indic_offset_0x%04xu %d" % (start
, offset
))
649 starts
.append (start
)
651 print_block (block
, start
, end
, indic_data
)
653 ends
.append (last
+ 1)
654 offset
+= ends
[-1] - starts
[-1]
657 occupancy
= used
* 100. / total
659 print ("}; /* Table items: %d; occupancy: %d%% */" % (offset
, occupancy
))
662 print ("hb_indic_get_categories (hb_codepoint_t u)")
664 print (" switch (u >> %d)" % page_bits
)
666 pages
= set ([u
>>page_bits
for u
in starts
+ends
+list (singles
.keys ())])
667 for p
in sorted(pages
):
668 print (" case 0x%0Xu:" % p
)
669 for u
,d
in singles
.items ():
670 if p
!= u
>>page_bits
: continue
671 print (" if (unlikely (u == 0x%04Xu)) return _(%s,%s);" % (u
, short
[0][d
[0]], short
[1][d
[1]]))
672 for (start
,end
) in zip (starts
, ends
):
673 if p
not in [start
>>page_bits
, end
>>page_bits
]: continue
674 offset
= "indic_offset_0x%04xu" % start
675 print (" if (hb_in_range<hb_codepoint_t> (u, 0x%04Xu, 0x%04Xu)) return indic_table[u - 0x%04Xu + %s];" % (start
, end
-1, start
, offset
))
681 print (" return _(X,X);")
685 print ("#undef INDIC_COMBINE_CATEGORIES")
688 vv
= sorted (values
[i
].keys ())
690 print ("#undef %s_%s" %
691 (what_short
[i
], short
[i
][v
]))
695 print ("/* == End of generated table == */")
697 # Maintain at least 50% occupancy in the table */
699 raise Exception ("Table too sparse, please investigate: ", occupancy
)