RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
#!/usr/bin/env python3
"""Regenerate the checked-in, offline search-fold table (Unicode 16.0.0).
python3 tools/generate_fold.py > src/ui/fold_data.zig
Python's bundled Unicode database is the default input. To use downloaded UCD
files instead, pass --unicode-data UnicodeData.txt --case-folding CaseFolding.txt
--version VERSION. Ordinary builds only read the checked-in Zig file.
"""
import argparse
from pathlib import Path
import unicodedata as ucd
SCRIPT_RANGES = (
(0x0041, 0x024F), (0x0370, 0x052F), (0x1D00, 0x1DBF),
(0x1E00, 0x1FFF), (0x2C60, 0x2C7F), (0xA640, 0xA69F),
(0xA720, 0xA7FF), (0xAB30, 0xAB6F),
)
EXTRAS = {ord(a): b for a, b in (
("ł", "l"), ("ø", "o"), ("ß", "ss"), ("æ", "ae"), ("œ", "oe"),
)}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--unicode-data", type=Path)
parser.add_argument("--case-folding", type=Path)
parser.add_argument("--version", default=ucd.unidata_version)
args = parser.parse_args()
if bool(args.unicode_data) != bool(args.case_folding):
parser.error("supply both UnicodeData.txt and CaseFolding.txt")
records = {}
folds = {}
if args.unicode_data:
for line in args.unicode_data.read_text().splitlines():
row = line.split(";")
records[int(row[0], 16)] = row
for line in args.case_folding.read_text().splitlines():
row = line.split("#", 1)[0].split(";")
if len(row) >= 3 and row[1].strip() in ("C", "S"):
folds[int(row[0], 16)] = int(row[2].strip(), 16)
def combining(cp):
return int(records.get(cp, ["", "", "", "0"])[3]) if records else ucd.combining(chr(cp))
def simple_fold(cp):
if records:
return folds.get(cp, cp)
folded = chr(cp).casefold()
if len(folded) == 1:
return ord(folded)
lower = chr(cp).lower()
return ord(lower) if len(lower) == 1 else cp
def decompose(cp):
if not records:
return map(ord, ucd.normalize("NFD", chr(cp)))
row = records.get(cp)
decomposition = row[5] if row else ""
if not decomposition or decomposition.startswith("<"):
return [cp]
return [child for part in decomposition.split() for child in decompose(int(part, 16))]
table = []
for start, end in SCRIPT_RANGES:
for cp in range(start, end + 1):
# Only the named scripts are folded; punctuation and other scripts
# sharing a block pass through unchanged.
name = records.get(cp, ["", ""])[1] if records else ucd.name(chr(cp), "")
if not name.startswith(("LATIN ", "GREEK ", "CYRILLIC ")):
continue
folded = simple_fold(cp)
result = EXTRAS.get(folded)
if result is None:
result = "".join(chr(simple_fold(c)) for c in decompose(folded) if not combining(c))
if result != chr(cp):
assert 1 <= len(result) <= 2, (hex(cp), result)
table.append((cp, [ord(c) for c in result]))
marks = []
for cp in range(0x110000):
if combining(cp):
if marks and cp == marks[-1][1] + 1:
marks[-1][1] = cp
else:
marks.append([cp, cp])
print("// Generated by tools/generate_fold.py; do not edit.")
print(f'pub const unicode_version = "{args.version}";')
print("pub const Mapping = packed struct(u64) { from: u21, first: u21, second: u21 = 0, padding: u1 = 0 };")
print("pub const mappings = [_]Mapping{")
for cp, result in table:
second = f", .second = 0x{result[1]:x}" if len(result) == 2 else ""
print(f" .{{ .from = 0x{cp:x}, .first = 0x{result[0]:x}{second} }},")
print("};")
print("pub const combining = [_][2]u21{")
for start, end in marks:
print(f" .{{ 0x{start:x}, 0x{end:x} }},")
print("};")
if __name__ == "__main__":
main()