1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-12 18:49:07 +00:00
trezor-firmware/gen/fonts/generate.py

35 lines
971 B
Python
Raw Normal View History

2014-04-29 12:26:51 +00:00
#!/usr/bin/python
from PIL import Image
class Img(object):
def __init__(self, fn):
im = Image.open(fn)
self.w, self.h = im.size
self.data = list(im.getdata())
def pixel(self, r, c):
p = self.data[ r + c * self.w ]
if p == (255, 255, 255):
return '0'
if p == (0, 0, 0):
return '1'
if p == (255, 0, 255):
return None
raise Exception('Unknown color', p)
img = Img('font.png')
cur = ''
2015-02-13 14:37:23 +00:00
idx = 0
2014-04-29 12:26:51 +00:00
for i in range(img.w):
if img.pixel(i, 0) == None:
cur = '\\x%02x' % (len(cur) / 4) + cur
2015-02-13 14:37:23 +00:00
ch = chr(idx) if idx >= 32 and idx <= 126 else '_'
print '\t/* 0x%02x %c */ (uint8_t *)"%s",' % (idx, ch , cur)
2014-04-29 12:26:51 +00:00
cur = ''
2015-02-13 14:37:23 +00:00
idx += 1
2014-04-29 12:26:51 +00:00
continue
val = img.pixel(i, 0) + img.pixel(i, 1) + img.pixel(i, 2) + img.pixel(i, 3) + img.pixel(i, 4) + img.pixel(i, 5) + img.pixel(i, 6) + img.pixel(i, 7)
cur += '\\x%02x' % int(val, 2)