Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

5.0 KB
Raw Blame
#!/usr/bin/env python3
"""Bake small display fallbacks; no Unicode library runs on the device.

Generated with Python's Unicode 16.0.0 data. Compatibility decomposition gives
text equivalents for fractions, ligatures, fullwidth and styled letters/digits.
Only ASCII results are admitted. Never strip a combining negation from math.
Regeneration requires the same Unicode data version; ordinary builds use the
checked-in table. https://www.unicode.org/reports/tr15/
"""
from pathlib import Path
import argparse
import json
import unicodedata as ud

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--check', action='store_true')
args = parser.parse_args()
if ud.unidata_version != '16.0.0':
    raise SystemExit('Regeneration requires Unicode 16.0.0 (Python 3.14).')

values = {}
for cp in range(160, 0x110000):
    source = chr(cp)
    text = ud.normalize('NFKD', source).replace('⁄', '/')
    # Latin-letter accents may fall back to the base letter if absent from the
    # atlas. Marks on operators, numbers and other scripts are never removed.
    if ud.name(source, '').startswith('LATIN '):
        text = ''.join(c for c in text if not ud.combining(c))
    if text != source and text and all(32 <= ord(c) <= 126 for c in text):
        if cp in (0xb2, 0xb3, 0xb9) or 0x2070 <= cp <= 0x207f:
            text = '^' + text
        elif 0x2080 <= cp <= 0x209f:
            text = '_' + text
        values[cp] = text

# Semantic equivalents not supplied by compatibility decomposition. Existing
# native glyphs win, so the proper arrows/check/cross remain visible on the dial.
groups = {
    ' ': '\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000',
    '': '\u00ad\u200b\u200c\u200d\u2060\ufeff',
    '-': '‐‑‒–—―−', "'": '‘’‚‛′', '"': '“”„‟″',
    '...': '…', '*': '•‣⁃∙⋅', '/': '⁄∕', 'x': '×✕',
    '<=': '≤≦', '>=': '≥≧', '!=': '≠', '!<': '≮', '!>': '≯',
    '!<=': '≰', '!>=': '≱', '~=': '≈≃≅', '+/-': '±', '-/+': '∓',
    'infinity': '∞', 'sqrt': '√', 'sum': '∑', 'pi': 'π',
    '<-': '←⇐', '->': '→➜➔➝➞➟➠➡➤➥➦➧➨➩➪➫➬➭➮➯➱⇢',
    '=>': '⇒', '<->': '↔', '<=>': '⇔', '^': '↑⇧', 'v': '↓⇩',
    '^>': '↗', '<^': '↖', 'v>': '↘', '<v': '↙', '[return]': '↵⏎',
    '(C)': '©', '(R)': '®', '(TM)': '™', 'EUR': '€', 'GBP': '£', 'JPY': '¥',
    '[ok]': '✓✔✅☑', '[x]': '✗✘❌❎', '[!]': '⚠❗‼', '[?]': '❓❔',
    '[bell]': '🔔', '[locked]': '🔒', '[unlocked]': '🔓', '[pin]': '📌',
    '[idea]': '💡', '[bug]': '🐛', '[test]': '🧪', '[tool]': '🔧',
    '[settings]': '⚙', '[note]': '📝', '[file]': '📄', '[folder]': '📁📂',
    '[launch]': '🚀', '[celebrate]': '🎉', '[fire]': '🔥', '[stop]': '🛑',
    '[up]': '👍', '[down]': '👎', '[heart]': '♥❤', '[computer]': '💻',
}
for text, chars in groups.items():
    for ch in chars:
        values[ord(ch)] = text

# Consecutive one-character folds (e.g. fullwidth A-Z) need one range, not 26
# entries. Remaining strings are pooled/deduplicated. All arrays stay in flash.
ranges, entries = [], []
points = sorted(values)
i = 0
while i < len(points):
    cp = points[i]
    text = values[cp]
    end = i + 1
    if len(text) == 1:
        while end < len(points) and points[end] == points[end - 1] + 1 and \
              len(values[points[end]]) == 1 and ord(values[points[end]]) - points[end] == ord(text) - cp:
            end += 1
    if end - i >= 3:
        ranges.append((cp, points[end - 1], ord(text) - cp))
        i = end
    else:
        entries.append((cp, text))
        i += 1

pool, offsets = '', {}
for _, text in entries:
    if text not in offsets:
        offsets[text] = len(pool)
        pool += text + '\0'
offset_bits = len(pool).bit_length()
assert (max(cp for cp, _ in entries) << offset_bits) <= 0xffffffff
output = ['// Generated by scripts/gen_display_fallbacks.py; Unicode 16.0.0.\n',
          '// Display-only equivalents. Original messages and identities stay unchanged.\n',
          'static const struct { uint32_t first, last; int32_t delta; } display_ranges[] = {\n']
output += [f'    {{0x{a:x},0x{b:x},{delta}}},\n' for a,b,delta in ranges]
output += ['};\nstatic const char display_pool[] =\n']
for text in offsets:
    output.append('    ' + json.dumps(text + '\0', ensure_ascii=True).replace('\\u0000', '\\000') + '\n')
output += [f';\nenum {{ DISPLAY_OFFSET_BITS = {offset_bits}, DISPLAY_OFFSET_MASK = {(1 << offset_bits)-1} }};\n',
           'static const uint32_t display_entries[] = {\n']
output += [f'    0x{(cp << offset_bits) | offsets[text]:x},\n' for cp,text in entries]
output += ['};\n']
data = ''.join(output)
path = Path(__file__).resolve().parents[1] / 'main/ui/habitat/display_fallbacks.inc'
if args.check:
    if path.read_text() != data:
        raise SystemExit('Display fallback table is stale')
else:
    path.write_text(data)
print(f'{len(values)} equivalents: {len(ranges)} ranges, {len(entries)} entries, {len(pool)} pooled bytes')