Browse Source

Did stuff

master
Antoine COMBET 3 years ago
parent
commit
e81790194b
  1. 0
      fontthing/__init__.py
  2. 19
      fontthing/__main__.py
  3. 15
      fontthing/data.py
  4. 1
      fontthing/input/__init__.py
  5. 12
      fontthing/input/_utils.py
  6. 22
      fontthing/input/mode1.py
  7. 0
      fontthing/output/__init__.py
  8. 11
      fontthing/output/ppm.py

0
fontthing/__init__.py

19
fontthing/__main__.py

@ -0,0 +1,19 @@
from fontthing.input import parse_mode1
if __name__ == '__main__':
print(parse_mode1("""TXTF 1 0 6 6 .*
!
..*...
..*...
..*...
......
..*...
......
#
.*.*..
*****.
.*.*..
*****.
.*.*..
......
"""))

15
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]

1
fontthing/input/__init__.py

@ -0,0 +1 @@
from .mode1 import (parse as parse_mode1)

12
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)))

22
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)

0
fontthing/output/__init__.py

11
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"