Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-fonts

Source of release 0.7.2

0.7.2 / tools / cell_widths.py

#!/usr/bin/env python3
"""Generate src/fonts/cell_table.lucb, the terminal cell width of every scalar.

usage: tools/cell_widths.py

Python's `unicodedata` is the source; the generated file records its Unicode version.
Each scalar takes 0, 1 or 2 cells of a monospace grid, as wcwidth counts them:

- 0: combining marks (general category Mn and Me), U+200B..U+200F, U+2060..U+2064,
  U+FEFF, the variation selectors (Mn already) and the Hangul medial and final jamo
  U+1160..U+11FF;
- 2: East Asian Width W or F;
- 1: everything else, control characters included (callers handle those).

The table is a step function: entry `start << 2 | cells` says that every scalar from
`start` up to the next entry's start takes `cells` cells.
"""
from pathlib import Path
import sys
import unicodedata

ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / "src/fonts/cell_table.lucb"
ZERO_RANGES = [(0x200B, 0x200F), (0x2060, 0x2064), (0xFEFF, 0xFEFF), (0x1160, 0x11FF)]


def cells(scalar):
    if any(first <= scalar <= last for first, last in ZERO_RANGES):
        return 0
    character = chr(scalar)
    if unicodedata.category(character) in ("Mn", "Me"):
        return 0
    if unicodedata.east_asian_width(character) in ("W", "F"):
        return 2
    return 1


def steps():
    entries = []
    previous = None
    for scalar in range(0x110000):
        width = cells(scalar)
        if width != previous:
            entries.append(scalar << 2 | width)
            previous = width
    return entries


def render(entries):
    lines = [
        "#" + "=" * 94,
        "#",
        "#   cell_table - Terminal cell widths of Unicode scalars",
        "#",
        "#   DESCRIPTION:",
        "#       Generated by tools/cell_widths.py from Python's unicodedata; do not edit.",
        f"#       Unicode {unicodedata.unidata_version}. Entry `start << 2 | cells`: every scalar from",
        "#       `start` to the next entry's start takes `cells` (0, 1 or 2) grid cells.",
        "#",
        "#" + "=" * 94,
        "",
        "## The Unicode version the cell widths follow.",
        f'pub let cell_unicode_version: str = "{unicodedata.unidata_version}"',
        "",
        f"let cell_steps: u32[{len(entries)}] = [",
    ]
    for at in range(0, len(entries), 8):
        lines.append("    " + ", ".join(f"0x{value:06x}" for value in entries[at:at + 8]) + ",")
    lines.append("]")
    return "\n".join(lines) + "\n"


def main():
    if len(sys.argv) != 1:
        sys.exit(__doc__)
    entries = steps()
    OUTPUT.write_text(render(entries))
    print(f"wrote {OUTPUT.relative_to(ROOT)}: {len(entries)} steps, Unicode {unicodedata.unidata_version}")


if __name__ == "__main__":
    main()