Adding codegen tools
This commit is contained in:
92
codegen/font.py
Executable file
92
codegen/font.py
Executable file
@@ -0,0 +1,92 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
from matrix import Vector
|
||||
|
||||
glyphname = ["space", "exclamation", "hash", "dollar", "percent", "ampersand"
|
||||
"quotestart", "leftparentes", "rigtparentes", "star", "plus", "comma",
|
||||
"hyphen", "dot", "slash", "zero", "one", "two", "three", "four", "five",
|
||||
"six", "seven", "eight", "nine", "colon", "semicolon", "lessthan",
|
||||
"equals", "biggerthan", "questionmark", "at", "p2", "p3", "capa", "capb", "capc",
|
||||
"capd", "cape", "capf", "capg", "caph", "capi", "capj", "capk", "capl",
|
||||
"capm", "capn", "capo", "capp", "capq", "capr", "caps", "capt", "capu",
|
||||
"capv", "capw", "capx", "capy", "capz", "bracketleft", "backslash",
|
||||
"bracketright", "uparrow", "underscore", "quoteend", "a", "b", "c", "d",
|
||||
"e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
|
||||
"s", "t", "u", "v", "w", "x", "y", "z", "curlystart", "verticalbar",
|
||||
"curlyend", "tilde", "p1"]
|
||||
|
||||
def char2value (c):
|
||||
return ord(c) - ord('R')
|
||||
|
||||
def read_hershey (l):
|
||||
#
|
||||
# Format is like this:
|
||||
# ' 700 18H\QFNGLJKOKRLWNZQ[S[VZXWYRYOXJVGSFQF'
|
||||
# ^ ^^ |---- coordinates ---------------|
|
||||
# | |
|
||||
# | |
|
||||
# | Number of coordinates
|
||||
# Character Identifier - not ASCII
|
||||
points = list()
|
||||
ident = int(l[0:5])
|
||||
coords = int(l[5:8])
|
||||
left = char2value(l[8])
|
||||
right = char2value(l[9])
|
||||
vdata = l[10:]
|
||||
for t in vdata.split(" R"):
|
||||
c = iter(t)
|
||||
points.append(list())
|
||||
for x in c:
|
||||
y = next(c)
|
||||
points[-1].append((char2value(x), -char2value(y)))
|
||||
return (ident, left, right, points)
|
||||
|
||||
def glyph_map (file):
|
||||
m = dict()
|
||||
f = open(file, "r")
|
||||
i = 0
|
||||
for line in f:
|
||||
(ident, left, right, coords) = read_hershey(line.rstrip("\n"))
|
||||
m[glyphname[i]] = (left, right, coords)
|
||||
i = i + 1
|
||||
return m
|
||||
|
||||
def typeset(glyphmap, idents):
|
||||
vectors = list()
|
||||
offset = 0
|
||||
for i in idents:
|
||||
(left, right, coords) = glyphmap[i]
|
||||
xlate = offset - left
|
||||
for v in coords:
|
||||
vectors.append(list(map(lambda c: Vector(c[0]+offset, c[1]), v)))
|
||||
offset = offset + right - left
|
||||
return vectors, offset
|
||||
|
||||
#
|
||||
# lines: array of array of Vector
|
||||
def toKotlin(lines, stroke, color):
|
||||
def accumulative_range(vv):
|
||||
offset = 0
|
||||
for v in vv:
|
||||
yield range(offset, offset+len(v))
|
||||
offset = offset + len(v)
|
||||
|
||||
return " Design(listOf({}),\n listOf({}))".format(
|
||||
",\n ".join(map(lambda l: ", ".join(map(lambda v: "Vector({}F, {}F)".format(v.x, v.y), l)), lines)),
|
||||
",\n ".join(map(lambda t: "Design.Trace(listOf({}), {}, {})".format(", ".join(map(str, t)), stroke, color), accumulative_range(lines))))
|
||||
|
||||
|
||||
def main():
|
||||
f = open("rowmand.jhf", "r")
|
||||
number = 0
|
||||
for line in f:
|
||||
(_, left, right, coords) = read_hershey(line.rstrip('\n'))
|
||||
coords_text = "listOf(" + ", ".join(map(lambda cv : "listOf(" + ", ".join(map(lambda c : "Vector({}F, {}F)".format(c[0]/10, c[1]/10), cv)), coords)) + ")"
|
||||
print("val glyph_{} = \n Glyph({}, {}, {})".format(glyphname[number], coords_text, left, right))
|
||||
number = number + 1
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user