#!/usr/bin/env python3
"""bigfuture : chiffres dans le style de la police toilet « future », à taille réglable.

Usage :
  bigfuture.py "15:59"              # hauteur 7 par défaut
  bigfuture.py "15:59" -H 11        # plus grand (hauteur impaire >= 3)
  bigfuture.py "15:59" -H 9 -W 10   # largeur des chiffres forcée
  bigfuture.py --tlf 9 > bigfuture9.tlf   # exporte une police pour toilet
Caractères gérés : 0-9 : . - et espace.
"""
import argparse, sys

CHARS = {
    frozenset(): " ",
    frozenset("L"): "╸", frozenset("R"): "╺", frozenset("U"): "╹", frozenset("D"): "╻",
    frozenset("LR"): "━", frozenset("UD"): "┃",
    frozenset("RD"): "┏", frozenset("LD"): "┓", frozenset("RU"): "┗", frozenset("LU"): "┛",
    frozenset("UDR"): "┣", frozenset("UDL"): "┫", frozenset("LRD"): "┳", frozenset("LRU"): "┻",
    frozenset("LRUD"): "╋",
}

def glyph(c, H, W):
    x0, x1, y0, ym, y1 = 0, W - 1, 0, H // 2, H - 1
    if c == " ":
        return [" " * max(2, W // 2)] * H
    if c in ":.":
        col = [" "] * H
        dots = [ym - 1, ym + 1] if c == ":" else [y1]
        if c == ":" and H >= 7:
            dots = [ym - 2, ym - 1, ym + 1, ym + 2]
            col[ym - 2], col[ym - 1], col[ym + 1], col[ym + 2] = "╻", "╹", "╻", "╹"
        else:
            for d in dots:
                col[d] = "╹"
        return col
    if c == "-":
        g = [[set() for _ in range(W)] for _ in range(H)]
        segs = [("h", ym, x0, x1)]
        hooks = []
    else:
        top, mid, bot = ("h", y0, x0, x1), ("h", ym, x0, x1), ("h", y1, x0, x1)
        L_full, R_full = ("v", x0, y0, y1), ("v", x1, y0, y1)
        L_up, L_dn = ("v", x0, y0, ym), ("v", x0, ym, y1)
        R_up, R_dn = ("v", x1, y0, ym), ("v", x1, ym, y1)
        xc = W // 2
        defs = {
            "0": ([top, bot, L_full, R_full], []),
            "1": ([("h", y0, xc - 1, xc), ("v", xc, y0, y1), ("h", y1, max(0, xc - 2), min(x1, xc + 2))], []),
            "2": ([top, R_up, mid, L_dn, bot], [(x0, y0, "D")]),
            "3": ([top, R_full, mid, bot], [(x0, y0, "D"), (x0, y1, "U")]),
            "4": ([L_up, R_full, mid], []),
            "5": ([top, L_up, mid, R_dn, bot], [(x0, y1, "U")]),
            "6": ([top, L_full, mid, R_dn, bot], [(x1, y0, "D")]),
            "7": ([top, R_full], [(x0, y0, "D")]),
            "8": ([top, mid, bot, L_full, R_full], []),
            "9": ([top, L_up, R_full, mid, bot], [(x0, y1, "U")]),
        }
        if c not in defs:
            raise SystemExit(f"caractère non géré : {c!r}")
        segs, hooks = defs[c]
        g = [[set() for _ in range(W)] for _ in range(H)]
    for kind, fixed, a, b in segs:
        for t in range(a, b + 1):
            cell = g[fixed][t] if kind == "h" else g[t][fixed]
            if t > a: cell.add("L" if kind == "h" else "U")
            if t < b: cell.add("R" if kind == "h" else "D")
    for x, y, d in hooks:
        g[y][x].add(d)
    return ["".join(CHARS[frozenset(s)] for s in row) for row in g]

def render(text, H, W, gap=1):
    rows = [""] * H
    for c in text:
        gl = glyph(c, H, W)
        for i in range(H):
            rows[i] += gl[i] + " " * gap
    return "\n".join(r.rstrip() for r in rows)

def tlf(H, W):
    out = [f"tlf2a$ {H} {H} {W + 2} -1 1", f"bigfuture {H} lignes, style toilet future"]
    # toilet exige aussi les 7 lettres allemandes (Ä Ö Ü ä ö ü ß) : glyphes vides
    for code in list(range(32, 127)) + [196, 214, 220, 228, 246, 252, 223]:
        c = chr(code)
        try:
            gl = glyph(c, H, W)
            gl = [l + " " for l in gl]
        except SystemExit:
            gl = [""] * H
        lines = [l.replace(" ", "$") for l in gl]
        out += [l + "@" for l in lines[:-1]] + [lines[-1] + "@@"]
    return "\n".join(out) + "\n"

if __name__ == "__main__":
    p = argparse.ArgumentParser()
    p.add_argument("text", nargs="?")
    p.add_argument("-H", "--height", type=int, default=7)
    p.add_argument("-W", "--width", type=int)
    p.add_argument("--tlf", type=int, metavar="H")
    a = p.parse_args()
    if a.tlf:
        H = a.tlf | 1
        sys.stdout.write(tlf(H, a.width or max(3, round(H * 0.85))))
        sys.exit()
    H = max(3, a.height | 1)
    W = a.width or max(3, round(H * 0.85))
    print(render(a.text if a.text is not None else sys.stdin.read().strip(), H, W))
