1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-12-11 02:49:16 +00:00
erc-c/tools/build-fonts

192 lines
4.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python2
import os
from PIL import Image
glyphmap = {
2018-01-24 19:05:16 +00:00
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
"6": "6",
"7": "7",
"8": "8",
"9": "9",
"al": "a",
"ampersand": "&",
"apostrophe": "'",
2018-01-24 19:05:16 +00:00
"apple": 1,
"arrow-down": 3,
"arrow-down-lines": 2,
"arrow-left": 5,
"arrow-left-lines": 4,
"arrow-return": 6,
"arrow-right": 8,
"arrow-right-lines": 7,
"arrow-up": 11,
"arrow-up-lines": 9,
"asterisk": "*",
"at": "@",
2018-01-24 19:05:16 +00:00
"au": "A",
"backslash": "\\",
"bar": "|",
2018-01-24 19:05:16 +00:00
"bl": "b",
"blank": " ",
2018-01-24 19:05:16 +00:00
"bu": "B",
"caret": "^",
2018-01-24 19:05:16 +00:00
"check": 13,
"check-inverse": 12,
"checkerboard1": 14,
"checkerboard2": 15,
"cl": "c",
"colon": ":",
"comma": ",",
2018-01-24 19:05:16 +00:00
"crosshairs": 16,
"cu": "C",
"diamond": 17,
"dl": "d",
"dollar": "$",
2018-01-24 19:05:16 +00:00
"du": "D",
"el": "e",
"ellipsis": 18,
"emark": "!",
"equal": "=",
2018-01-24 19:05:16 +00:00
"eu": "E",
"file-left": 19,
"file-right": 20,
"finger": 21,
"fl": "f",
"fu": "F",
"gl": "g",
"grave": "`",
2018-01-24 19:05:16 +00:00
"gu": "G",
"hl": "h",
"hourglass": 22,
"hu": "H",
"hyphen": "-",
2018-01-24 19:05:16 +00:00
"il": "i",
"iu": "I",
"jl": "j",
"ju": "J",
"kl": "k",
"ku": "K",
"langle": "<",
"lbrace": "{",
"lbracket": "[",
2018-01-24 19:05:16 +00:00
"line-box": 23,
"line-left": 25,
"line-left-bottom": 24,
"line-middle": 26,
"line-right": 28,
"line-right-inverse": 27,
"line-top": 30,
"line-top-bottom": 29,
"ll": "l",
"lparen": "(",
2018-01-24 19:05:16 +00:00
"lu": "L",
"mailbox": 31,
"ml": "m",
"mu": "M",
"nl": "n",
"nu": "N",
"ol": "o",
2018-01-24 21:05:29 +00:00
"open-apple": 0,
2018-01-24 19:05:16 +00:00
"ou": "O",
"percent": "%",
"perfcurs": 10,
"period": ".",
2018-01-24 19:05:16 +00:00
"pl": "p",
"plus": "+",
2018-01-24 19:05:16 +00:00
"pointer": 127,
"pound": "#",
2018-01-24 19:05:16 +00:00
"pu": "P",
"ql": "q",
"qmark": "?",
2018-01-24 19:05:16 +00:00
"qu": "Q",
"quote": '"',
"rangle": ">",
"rbrace": "}",
"rbracket": "]",
2018-01-24 19:05:16 +00:00
"rl": "r",
"rparen": ")",
2018-01-24 19:05:16 +00:00
"ru": "R",
"semicolon": ";",
2018-01-24 19:05:16 +00:00
"sl": "s",
"slash": "/",
2018-01-24 19:05:16 +00:00
"su": "S",
"tilde": "~",
"tl": "t",
"tu": "T",
2018-01-24 19:05:16 +00:00
"ul": "u",
"underscore": "_",
"uu": "U",
2018-01-24 19:05:16 +00:00
"vl": "v",
"vu": "V",
2018-01-24 19:05:16 +00:00
"wl": "w",
"wu": "W",
2018-01-24 19:05:16 +00:00
"xl": "x",
"xu": "X",
2018-01-24 19:05:16 +00:00
"yl": "y",
"yu": "Y",
2018-01-24 19:05:16 +00:00
"zl": "z",
"zu": "Z",
}
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 + '.bmp')
def name_to_char(name):
2018-01-24 21:05:29 +00:00
if not glyphmap[name] and glyphmap[name] is not 0:
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)