72 lines
1.6 KiB
Python
Executable file
72 lines
1.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
from PIL import Image
|
|
import sys
|
|
|
|
if len(sys.argv) < 3:
|
|
print("usage: {} <bitmap> <characters> <output>")
|
|
exit(1)
|
|
|
|
inputfile = sys.argv[1]
|
|
charfile = sys.argv[2]
|
|
outputfile = sys.argv[3]
|
|
|
|
im = Image.open(inputfile)
|
|
|
|
cols, rows = im.size
|
|
|
|
offset = 4 # hardcoded 'space'
|
|
startpos = offset
|
|
|
|
fixedwidth = 9
|
|
|
|
charpos = [0] # hardcoded 'space'
|
|
chardata = [0x00] * offset # hardcoded 'space'
|
|
charwidth = [offset] # hardcoded 'space'
|
|
|
|
for col in range(cols):
|
|
coldata = 0
|
|
|
|
for row in range(rows):
|
|
px = 0 if im.getpixel( (col, row) ) else 1
|
|
coldata |= (px << row)
|
|
|
|
print("{:016b}".format(coldata))
|
|
|
|
width = offset - startpos
|
|
|
|
chardata.append(coldata)
|
|
|
|
if width == fixedwidth:
|
|
charpos.append(startpos)
|
|
charwidth.append(width)
|
|
startpos = offset
|
|
else:
|
|
offset += 1
|
|
|
|
dataarray = ", ".join([hex(n) for n in chardata])
|
|
posarray = ", ".join([str(n) for n in charpos])
|
|
widtharray = ", ".join([str(n) for n in charwidth])
|
|
|
|
asciimap = [0 for i in range(256)]
|
|
|
|
with open(charfile, "r") as cfile:
|
|
s = " " + cfile.read().strip()
|
|
for i in range(len(s)):
|
|
asciimap[ord(s[i])] = i
|
|
|
|
asciimaparray = ", ".join([str(n) for n in asciimap])
|
|
|
|
with open(outputfile, "w") as ofile:
|
|
ofile.write("""
|
|
#include "font16_data.h"
|
|
|
|
const uint16_t font_data[{}] = {{ {} }};
|
|
const uint16_t font_pos[{}] = {{ {} }};
|
|
const uint8_t font_width[{}] = {{ {} }};
|
|
|
|
const uint8_t font_lut[256] = {{ {} }};
|
|
|
|
const uint8_t font_height = {};
|
|
""".format(len(chardata), dataarray, len(charpos), posarray, len(charwidth), widtharray, asciimaparray, rows))
|