From 6db322ae5cc652ef96b5c2bc6f6adbfd01fd7db5 Mon Sep 17 00:00:00 2001 From: Peter Evans 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)NkldffuqU%ky2WCf@hC+X%?8IVL2*LZ-%p} zg89^TU4-t3?cu=3c|dE`$$_*9aJ)3a zkm2~LDe0GtI 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;aVfH;Q!dcHoq&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)