-
Notifications
You must be signed in to change notification settings - Fork 0
/
bdf.py
executable file
·33 lines (23 loc) · 1.01 KB
/
bdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/python3
from bdflib import reader
import numpy as np
import os, sys
class bdf:
def __init__(self, fontfilename):
self.bdffont = reader.read_bdf(open(fontfilename, 'rb'))
def letter(self, let):
character = self.bdffont.glyphs_by_codepoint[ord(let)].data
return [list('{0:0=8b}'.format(row).replace('0','.').replace('1','x')) for row in reversed(character)]
def trim_letter(self, let):
im = np.array(self.letter(let))
height, width = np.shape(im)
# find the first and last column with a black pixel
# np.where or something like that would probably work better but who cares
for column_first in range(width):
if 'x' in im[:, column_first]:
break
for column_last in reversed(range(width)):
if 'x' in im[:, column_last]:
break
assert(column_last + 1 <= width)
return np.hstack((im[:, column_first:column_last+1],np.array(list('.' * height))[:, np.newaxis]))