From 6db322ae5cc652ef96b5c2bc6f6adbfd01fd7db5 Mon Sep 17 00:00:00 2001
From: Peter Evans <pevans@activecampaign.com>
Date: Tue, 19 Dec 2017 15:28:42 -0600
Subject: [PATCH] I wrote a tool to build fonts from grid files.

This commit also adds the first such output, which is the apple II
system font.
---
 fonts/apple2-system.png | Bin 0 -> 1067 bytes
 tools/build-fonts       | 162 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 162 insertions(+)
 create mode 100644 fonts/apple2-system.png
 create mode 100755 tools/build-fonts

diff --git a/fonts/apple2-system.png b/fonts/apple2-system.png
new file mode 100644
index 0000000000000000000000000000000000000000..b5a476af779ede115480c63d69b12f29c2956532
GIT binary patch
literal 1067
zcmV+`1l0S9P)<h;3K|Lk000e1NJLTq003|R002M;0ssI2P55N7000B>Nkl<Zc%1E8
z%aR*02$Ws^|6f)PP82MlX(Y{fQ=t!ASxpNNgLurQwL&38?|u4N*hP8sBq~DBqr7<%
z6`_Sfp-?Ck3WY+U@b5s+ZRR}Z4SAYo4ualv$dglIFU9jZ(ZkF5w7vJo<3S$&P1fs^
z1n=Ma*G;6QF%jB7;n6&?fSL7$X;pZlP2>dffuqU%ky2WCf@hC+X%?8IVL2*LZ-%p}
zg89^TU4-t3?cu=3c|dE`$$_*9aJ)3a<c%ZlpoHvPd9YnOnu$;#bH`07Zyry<g#f=>
zkm2~LDe0<B(ah<k0SSOPiS?MKou2V2=#!EhL%UZrydtCj?Jasa6z}q$dg+*y!>GtI
zZS7{Q2sMQnGh3-bEm?<>iPc;&O=ZO{)I`q+y)lf+l<6iDNC>eO)Pa|zDWOC8>xs{~
z5Mp6me=9-_hmtmhCSqlqca~QKKzcc~T_RUKCvmv=jz?id_O|y-kHM69Es%K>a+tk^
zMdbQW8KHbE;{Xq%UNYrVGDrv0+-?R}$PBdOG`E%n!$ngjzsMwaPdiiG#@VySd}`A8
z_3d*0>u=keRqg?q@7|~OGC061yCGEs$nCsL{?&ThcV4;a<q5CpcQ0dV!kTg3A22-r
z+lE2#(lM`{H=Frei^fw@ClQZ7;H+(JJAyPe?|~_B)5thNCmnc$zMgVB-*RF_sbh}<
z5}G8R;)a9-R#Z5Exf5LIE<@&6=li?;MN_+8G{G}9Uelhik0qI)ZN34Zw1(4d^TUF{
z;s5AxsryF4GSg)^^I(}ILh@_k@tq)90d}C>VfH;<Gqlsg1A8#}IfR-Wl71obM$5%F
zj<-$P9#Tsx+b}Z8(J{F4AR?ru*yKn>Q!dcH<EP<;+#Sl40)g;(Qql^-TgG2`kLjss
z`5ZUEHzb<26C%}%ZozILvNOr1R*%DEvFsxzH8HBwASGVp&_M_@<AQc(`$32PZS2A4
zzqJY7v=V1qUXflj1$xy9S1nmQhUA5$?81y799AJyE)Fbg2R85qo6_V@-Xyom11oo6
zHnz7-s34Y4!F6c=oX}6{BM)=YjBvvjhPV%%fJe^;`$v-r445=}&~pKH*>oq&u_822
zijw@F+lQ_sd$s*5-@5)I=;}AtN!nDYZpC-B`Tz~Qm)vv_52t`<6EDPjQB#*-b~%7)
z+MR7jvWwoKmN9BWoJO*Sg6(*H(@jmhCoGe+oH;`FeWbtGn;&V;#=?LWm}*-4=ZAe$
zV5mJ?*4!`lsZJ30D|%5%9zJ3`rJ10;`&x+)9cSJ|`P7S0FE49RfEMNf(@xVWV5fN^
lt%3sY(4UF@gmdtKzW}xbuK5oTJrn={002ovPDHLkV1ldX2}u9|

literal 0
HcmV?d00001

diff --git a/tools/build-fonts b/tools/build-fonts
new file mode 100755
index 0000000..ace2b02
--- /dev/null
+++ b/tools/build-fonts
@@ -0,0 +1,162 @@
+#!/usr/bin/env python2
+
+import os
+from PIL import Image
+
+glyphmap = {
+        "ampersand": "&",
+        "apostrophe": "'",
+        "asterisk": "*",
+        "at": "@",
+        "backslash": "\\",
+        "bar": "|",
+        "blank": " ",
+        "caret": "^",
+        "colon": ":",
+        "comma": ",",
+        "dollar": "$",
+        "emark": "!",
+        "equal": "=",
+        "grave": "`",
+        "hyphen": "-",
+        "langle": "<",
+        "lbrace": "{",
+        "lbracket": "[",
+        "lparen": "(",
+        "percent": "%",
+        "perfcurs": 10,
+        "period": ".",
+        "plus": "+",
+        "pound": "#",
+        "qmark": "?",
+        "quote": '"',
+        "rangle": ">",
+        "rbrace": "}",
+        "rbracket": "]", 
+        "rparen": ")",
+        "semicolon": ";",
+        "slash": "/",
+        "tilde": "~",
+        "underscore": "_",
+        "0": "0",
+        "1": "1",
+        "2": "2",
+        "3": "3",
+        "4": "4",
+        "5": "5",
+        "6": "6",
+        "7": "7",
+        "8": "8",
+        "9": "9",
+        "al": "a",
+        "bl": "b",
+        "cl": "c",
+        "dl": "d",
+        "el": "e",
+        "fl": "f",
+        "gl": "g",
+        "hl": "h",
+        "il": "i",
+        "jl": "j",
+        "kl": "k",
+        "ll": "l",
+        "ml": "m",
+        "nl": "n",
+        "ol": "o",
+        "pl": "p",
+        "ql": "q",
+        "rl": "r",
+        "sl": "s",
+        "tl": "t",
+        "ul": "u",
+        "vl": "v",
+        "wl": "w",
+        "xl": "x",
+        "yl": "y",
+        "zl": "z",
+        "au": "A",
+        "bu": "B",
+        "cu": "C",
+        "du": "D",
+        "eu": "E",
+        "fu": "F",
+        "gu": "G",
+        "hu": "H",
+        "iu": "I",
+        "ju": "J",
+        "ku": "K",
+        "lu": "L",
+        "mu": "M",
+        "nu": "N",
+        "ou": "O",
+        "pu": "P",
+        "qu": "Q",
+        "ru": "R",
+        "su": "S",
+        "tu": "T",
+        "uu": "U",
+        "vu": "V",
+        "wu": "W",
+        "xu": "X",
+        "yu": "Y",
+        "zu": "Z",
+        }
+
+# recurse through fonts/machines
+# each subdir from there is the designation 
+
+def build_font(name, path, gridfiles):
+    img = Image.new("RGB", (112, 64))
+    pix = img.load()
+    for gridfile in gridfiles:
+        with open(os.path.join(path, gridfile)) as openf:
+            glyphname = gridfile.split('.')[0]
+            glyphchar = name_to_char(glyphname)
+            render(glyph_coords(glyphchar), glyphchar, openf.read(), pix)
+    img.save('./fonts/' + name + '.png')
+
+def name_to_char(name):
+    if not glyphmap[name]:
+        raise Exception('unknown glyph')
+    return glyphmap[name]
+
+
+# Return the row and column that a character should be saved in our bitmap
+# font.
+def glyph_coords(ch):
+    if type(ch) == int:
+        code = ch
+    else:
+        code = ord(ch)
+
+    row = (code & 0xf0) >> 4
+    col = code & 0x0f
+    return (row, col)
+
+def render_row(offset, row, line, pix):
+    row_offset, col_offset = offset
+    row_offset = row_offset * 8
+    col_offset = col_offset * 7
+    col = 0
+    for c in line[1:]:
+        if c == 'o':
+            pix[col_offset + col, row_offset + row] = (255, 255, 255)
+        col = col + 1
+
+def render(offset, ch, grid, pix):
+    row, col = glyph_coords(ch)
+    lines = grid.split("\n")
+    render_row(offset, 0, lines[1], pix)
+    render_row(offset, 1, lines[2], pix)
+    render_row(offset, 2, lines[3], pix)
+    render_row(offset, 3, lines[4], pix)
+    render_row(offset, 4, lines[5], pix)
+    render_row(offset, 5, lines[6], pix)
+    render_row(offset, 6, lines[7], pix)
+    render_row(offset, 7, lines[8], pix)
+
+for root, subdirs, subfiles in os.walk('./fonts'):
+    if subdirs == []:
+        fontname = root.replace('./fonts/', '')
+        fontname = fontname.replace('/', '-')
+        build_font(fontname, root, subfiles)