diff --git a/fontthing/__init__.py b/fontthing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fontthing/__main__.py b/fontthing/__main__.py new file mode 100644 index 0000000..ab3c617 --- /dev/null +++ b/fontthing/__main__.py @@ -0,0 +1,19 @@ +from fontthing.input import parse_mode1 + +if __name__ == '__main__': + print(parse_mode1("""TXTF 1 0 6 6 .* +! +..*... +..*... +..*... +...... +..*... +...... +# +.*.*.. +*****. +.*.*.. +*****. +.*.*.. +...... +""")) diff --git a/fontthing/data.py b/fontthing/data.py new file mode 100644 index 0000000..7224e06 --- /dev/null +++ b/fontthing/data.py @@ -0,0 +1,15 @@ +from dataclasses import dataclass + + +@dataclass +class Glyph: + width: int + height: int + glyph: list[list[bool]] + + +@dataclass +class Font: + mode: int + comment: str + glyphs: dict[str, Glyph] diff --git a/fontthing/input/__init__.py b/fontthing/input/__init__.py new file mode 100644 index 0000000..b4daf5f --- /dev/null +++ b/fontthing/input/__init__.py @@ -0,0 +1 @@ +from .mode1 import (parse as parse_mode1) diff --git a/fontthing/input/_utils.py b/fontthing/input/_utils.py new file mode 100644 index 0000000..3f2e9c6 --- /dev/null +++ b/fontthing/input/_utils.py @@ -0,0 +1,12 @@ +from more_itertools import grouper + + +def strip_ws(inp: str) -> str: + return "".join(filter(lambda x: not x.isspace(), inp)) + + +def desc2glyph_list(w: int, a: str, d: str) -> list[list[bool]]: + out = [] + for c in d: + out.append(c == a[1]) + return list(map(list, grouper(out, w, False))) diff --git a/fontthing/input/mode1.py b/fontthing/input/mode1.py new file mode 100644 index 0000000..0191996 --- /dev/null +++ b/fontthing/input/mode1.py @@ -0,0 +1,22 @@ +from ..data import Glyph, Font +from ._utils import strip_ws, desc2glyph_list +import re + + +def parse(inp: str) -> Font: + m: re.Match = re.match(r"TXTF 1 (\d+) (\d+) (\d+) (..)", inp) + cl, w, h, a = m.groups() + cl, w, h = int(cl), int(w), int(h) + inp = inp[m.end()+1:] + comment = [] + for _ in range(cl): + m = re.match(fr"(^.*$)", inp, re.MULTILINE) + comment.append(m.groups()[0]) + inp = inp[m.end()+1:] + inp = strip_ws(inp) + glyphs: dict[str, Glyph] = {} + while (m := re.match(r".{" + str(w * h + 1) + "}", inp)) is not None: + d = m.group(0) + glyphs[d[0]] = Glyph(width=w, height=h, glyph=desc2glyph_list(w, a, d[1:])) + inp = inp[m.end():] + return Font(mode=1,comment="\n".join(comment),glyphs=glyphs) diff --git a/fontthing/output/__init__.py b/fontthing/output/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fontthing/output/ppm.py b/fontthing/output/ppm.py new file mode 100644 index 0000000..ac67de8 --- /dev/null +++ b/fontthing/output/ppm.py @@ -0,0 +1,11 @@ +from ..data import Font + + +def toPBM(f: Font, t: str): + w = 0 + h = 0 + for c in t: + w += f.glyphs[c].width + h = h if h > (nh := f.glyphs[c].height) else nh + out: str = f"P1 {w} {h}\n" +