#!/usr/bin/env python3 """24x30-Pixelsatz für die 3x-Basis (1920x1080) — abgeleitet aus `font8.py`. Warum nicht einfach `font8.draw_text(..., scale=3)`: dann bekommt jeder 8x10-Punkt einen 3x3-Block, und jede Ecke ist ein Treppenabsatz aus drei Pixeln. Das liest sich als Zoom, nicht als Schrift. Hier wird der dreifach gesetzte Satz anschließend **an den Ecken um 1 px abgerundet** (`_chamfer`) — 3-px-Stämme bleiben, die Ecken werden zu 1-px-Fasen. Das ist der Unterschied zwischen „vergrößert" und „bei 24 px gezeichnet". Maße sind exakt 3x von `font8`, damit das 1x-Layout des Dialogs erhalten bleibt: Zelle 24x30, Vorschub 24, Zeile 36, links 3 px Rand. from font24 import draw_text, text_width, wrap Der Satz deckt dieselben 93 Zeichen wie `font8` (ASCII plus ÄÖÜäöüß). """ from __future__ import annotations from PIL import Image, ImageDraw import font8 S = 3 ADVANCE = font8.ADVANCE * S # 24 LINE = font8.LINE * S # 36 ROWS = font8.ROWS * S # 30 COLS = font8.ADVANCE * S # 24 (Zelle inkl. Rand, nicht Glyphenbreite) RAND = S # linker Rand wie in font8 (1 px * 3) def _roh(ch: str) -> list[list[bool]]: """Dreifach gesetztes Bitmap aus den 6x10-Glyphen.""" b = [[False] * COLS for _ in range(ROWS)] for ry, row in enumerate(font8._rows(ch)): for rx, c in enumerate(row): if c == "#": for dy in range(S): for dx in range(S): b[ry * S + dy][rx * S + dx + RAND] = True return b def _chamfer(b: list[list[bool]]) -> list[list[bool]]: """Ecken um 1 px abrunden: konvexe Ecken raus, konkave rein. Ein Pixel fliegt nur, wenn beide orthogonalen Nachbarn gesetzt sind und die Diagonale leer ist — Treppen von Diagonalen bleiben also stehen. """ h, w = len(b), len(b[0]) out = [row[:] for row in b] for y in range(h): for x in range(w): up = y > 0 and b[y - 1][x] dn = y < h - 1 and b[y + 1][x] lf = x > 0 and b[y][x - 1] rt = x < w - 1 and b[y][x + 1] ul = y > 0 and x > 0 and b[y - 1][x - 1] ur = y > 0 and x < w - 1 and b[y - 1][x + 1] dl = y < h - 1 and x > 0 and b[y + 1][x - 1] dr = y < h - 1 and x < w - 1 and b[y + 1][x + 1] if b[y][x]: if (up and lf and not ul) or (up and rt and not ur) or \ (dn and lf and not dl) or (dn and rt and not dr): out[y][x] = False else: if (up and lf and ul) or (up and rt and ur) or \ (dn and lf and dl) or (dn and rt and dr): out[y][x] = True return out _CACHE: dict[str, Image.Image] = {} def glyph(ch: str) -> Image.Image: """Glyphe als 24x30-Maske (L-Modus, 255 = Pixel).""" if ch not in _CACHE: b = _chamfer(_roh(ch)) im = Image.new("L", (COLS, ROWS), 0) px = im.load() for y in range(ROWS): for x in range(COLS): if b[y][x]: px[x, y] = 255 _CACHE[ch] = im return _CACHE[ch] def text_width(text: str) -> int: return ADVANCE * len(text) def draw_text(d, x: int, y: int, text: str, color) -> int: """Zeichnet Text bei 1:1 (24-px-Zellen); gibt die x-Position danach zurück. `d` darf ein `ImageDraw` (wie bei `font8`) oder direkt ein `Image` sein. """ ziel = d if isinstance(d, Image.Image) else getattr(d, "_image", None) if not isinstance(ziel, Image.Image): raise TypeError("draw_text braucht ein Image oder ImageDraw") rgba = ziel.convert("RGBA") if ziel.mode != "RGBA" else ziel a = color[3] if len(color) > 3 else 255 for ch in text: farbe = Image.new("RGBA", (COLS, ROWS), tuple(color[:3]) + (255,)) maske = glyph(ch) farbe.putalpha(maske if a == 255 else maske.point(lambda v: v * a // 255)) rgba.alpha_composite(farbe, (x, y)) x += ADVANCE return x def wrap(text: str, max_px: int) -> list[str]: words, lines, cur = text.split(), [], "" for w in words: cand = w if not cur else f"{cur} {w}" if text_width(cand) <= max_px: cur = cand else: if cur: lines.append(cur) cur = w if cur: lines.append(cur) return lines