Files
rosetta/tools/fixtures/hershey.py
T

60 lines
2.6 KiB
Python

# Hershey JHF fixtures (tools/fixtures.py): no package in nixpkgs ships the .jhf files, so JHF text is written here in
# the distribution's layout (5-column id, 3-column count, extents and vertex pairs offset by "R", " R" pen-up, records
# wrapped at 72 columns), with glyphs of known strokes (a Roman-style "A", "H", "O" approximations and random stroke
# glyphs); the expected outlines come from the independent parser below.
# tests/gen/hershey.luau { text, glyphs = { { id, advance, cmds = { { op, x, y } } } } }
BASELINE = 9
def record(gid, left, right, strokes):
pairs = [chr(left + 82) + chr(right + 82)]
for k, s in enumerate(strokes):
if k:
pairs.append(" R")
pairs += [chr(x + 82) + chr(y + 82) for x, y in s]
body = "".join(pairs)
line = "%5d%3d" % (gid, len(pairs)) + body
return "\n".join(line[i:i + 72] for i in range(0, len(line), 72))
def parse(text):
lines = text.replace("\r", "").split("\n")
out, i = [], 0
while i < len(lines):
line = lines[i]
i += 1
if not line.strip():
continue
gid, n = int(line[:5]), int(line[5:8])
data = line[8:]
while len(data) < 2 * n:
data += lines[i]
i += 1
left, right = ord(data[0]) - 82, ord(data[1]) - 82
cmds, pen = [], False
for k in range(1, n):
a, b = data[2 * k], data[2 * k + 1]
if a == " " and b == "R":
pen = False
continue
cmds.append({"op": "lineTo" if pen else "moveTo", "x": ord(a) - 82 - left, "y": BASELINE - (ord(b) - 82)})
pen = True
out.append({"id": gid, "advance": right - left, "cmds": cmds})
return out
def generate(ctx):
rng = ctx.rng(1967)
recs = [record(699, -8, 8, []), # space
record(501, -9, 9, [[(0, -12), (-8, 9)], [(0, -12), (8, 9)], [(-5, 2), (5, 2)]]), # A
record(508, -11, 11, [[(-7, -12), (-7, 9)], [(7, -12), (7, 9)], [(-7, -2), (7, -2)]])] # H
ring = [(round(8 * __import__("math").cos(a / 10 * 6.2832)), round(-1.5 + 10 * __import__("math").sin(a / 10 * 6.2832))) for a in range(11)]
recs.append(record(515, -11, 11, [ring])) # O
for k in range(40):
strokes = []
for _ in range(1 + rng.randrange(4)):
strokes.append([(rng.randrange(-15, 16), rng.randrange(-16, 17)) for _ in range(1 + rng.randrange(30))])
recs.append(record(2000 + k, -rng.randrange(3, 12), rng.randrange(3, 12), strokes))
text = "\n".join(recs) + "\n"
ctx.emit("hershey", {"text": text, "glyphs": parse(text)})