from array import array data = array('B', [0] * 256 * 5) infile = open("5x8.bdf", 'r') state = 0 for line in infile: if state == 0 and line.startswith("STARTCHAR"): state = 1 continue if state == 1 and line.startswith("ENCODING"): charno = int(line.split(' ')[1]) if charno >= 0 and charno < 256: state = 2 else: state = 0 continue if state == 2 and line.startswith("BITMAP"): lines = [] state = 3 continue if state == 3: lines.append(int(line, 16)) if len(lines) == 8: print charno for x in range(5): coldata = 0 for y in range(8): if lines[y] & (0x08 << x): coldata |= 1 << y print "*", else: print " ", data[charno * 5 + (4 - x)] = coldata print "" state = 0 def chartrim(chardata): rs = ''.join(map(chr, chardata)) return map(ord, rs.strip("\x00")) outfile = open("output", 'w') outfile.write(""" /* Font data */ #ifndef _FONT #define _FONT struct fontchar { unsigned char width; unsigned char data[5]; }; struct fontchar font_data[256] = { \t""") for char in range(256): chardata = chartrim(data[(char * 5):(char * 5 + 5)]) charlen = len(chardata) chardata += [0] * (5 - len(chardata)) if char == 32: charlen = 4 charstring = ', '.join(["0x%02x" % c for c in chardata]) outfile.write("{%d, {%s}}, " % (charlen, charstring)) if char % 2 == 1: outfile.write("//%d\n\t" % char) outfile.write("""}; #endif """)