[gen-ucd-table.py] Towards generating --rust output

This commit is contained in:
Behdad Esfahbod committed 2025-05-12 15:24:38 -06:00
1 parent f71391fe8d
commit d5c4b1eed7
1 file changed
+53 -33
+53 -33
View File
@@ -1,22 +1,31 @@
#!/usr/bin/env python3
"""usage: ./gen-ucd-table ucd.nounihan.grouped.xml [/path/to/hb-script-list.h]
"""usage: ./gen-ucd-table [--rust] ucd.nounihan.grouped.xml [/path/to/hb-script-list.h]
Input file:
* https://unicode.org/Public/UCD/latest/ucdxml/ucd.nounihan.grouped.zip
"""
# https://github.com/harfbuzz/packtab
import packTab
import packTab.ucdxml
import sys, re
import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
if len(sys.argv) > 1 and sys.argv[1] == '--rust':
del sys.argv[1]
logging.info('Generating Rust code...')
language = "rust"
else:
logging.info('Generating C code...')
language = "c"
language = packTab.languages[language]
if len (sys.argv) not in (2, 3):
sys.exit (__doc__)
# https://github.com/harfbuzz/packtab
import packTab
import packTab.ucdxml
logging.info('Loading UCDXML...')
ucdxml = packTab.ucdxml.load_ucdxml(sys.argv[1])
ucd = packTab.ucdxml.ucdxml_get_repertoire(ucdxml)
@@ -118,27 +127,34 @@ print("/* == Start of generated table == */")
print("/*")
print(" * The following table is generated by running:")
print(" *")
print(" * ./gen-ucd-table.py ucd.nounihan.grouped.xml")
print(" * ./gen-ucd-table.py ucd.nounihan.grouped.xml hb-script-list.h")
print(" *")
print(" * on file with this description:", ucdxml.description)
print(" */")
print()
print("#ifndef HB_UCD_TABLE_HH")
print("#define HB_UCD_TABLE_HH")
print()
print('#include "hb.hh"')
print()
if language.name == "c":
print("#ifndef HB_UCD_TABLE_HH")
print("#define HB_UCD_TABLE_HH")
print()
print('#include "hb.hh"')
print()
else:
print("use hb::*;")
# Write mapping data
uint16_t = language.type_name("u16")
uint32_t = language.type_name("u32")
uint64_t = language.type_name("u64")
code = packTab.Code('_hb_ucd')
sc_array, _ = code.addArray('hb_script_t', 'sc_map', sc_array)
dm1_p0_array, _ = code.addArray('uint16_t', 'dm1_p0_map', dm1_p0_array)
dm1_p2_array, _ = code.addArray('uint16_t', 'dm1_p2_map', dm1_p2_array)
dm2_u32_array, _ = code.addArray('uint32_t', 'dm2_u32_map', dm2_u32_array)
dm2_u64_array, _ = code.addArray('uint64_t', 'dm2_u64_map', dm2_u64_array)
code.print_c(linkage='static inline')
dm1_p0_array, _ = code.addArray(uint16_t, 'dm1_p0_map', dm1_p0_array)
dm1_p2_array, _ = code.addArray(uint16_t, 'dm1_p2_map', dm1_p2_array)
dm2_u32_array, _ = code.addArray(uint32_t, 'dm2_u32_map', dm2_u32_array)
dm2_u64_array, _ = code.addArray(uint64_t, 'dm2_u64_map', dm2_u64_array)
code.print_code(linkage=language.private_function_linkage, language=language)
datasets = [
('gc', gc, 'Cn', gc_order),
@@ -151,19 +167,25 @@ datasets = [
# Write main data
for step in (DEFAULT, COMPACT, SLOPPY):
modes = {}
if language.name == "c":
modes[DEFAULT] = '#ifndef HB_OPTIMIZE_SIZE'
modes[COMPACT] = '#elif !defined(HB_NO_UCD_UNASSIGNED)'
modes[SLOPPY] = '#else'
modes[None] = '#endif'
else:
modes[DEFAULT] = ''
for step, text in modes.items():
print()
if text:
print(text)
print()
if step is None:
continue
compression = compression_level[step]
logging.info(' Compression=%d:' % compression)
print()
if step == DEFAULT:
print('#ifndef HB_OPTIMIZE_SIZE')
elif step == COMPACT:
print('#elif !defined(HB_NO_UCD_UNASSIGNED)')
elif step == SLOPPY:
print('#else')
else:
assert False
print()
if step == SLOPPY:
for i in range(len(gc)):
@@ -187,16 +209,14 @@ for step in (DEFAULT, COMPACT, SLOPPY):
logging.info(' Dataset=%-8s FullCost=%d' % (name, sol.fullCost))
sol.genCode(code, name)
code.print_c(linkage='static inline')
code.print_code(linkage=language.private_function_linkage, language=language)
print()
print('#endif')
print()
print()
print("#endif /* HB_UCD_TABLE_HH */")
if language.name == "c":
print()
print("#endif /* HB_UCD_TABLE_HH */")
print()
print("/* == End of generated table == */")
logging.info('Done.')