2017-01-23 21:21:45 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2017-04-10 03:36:04 +00:00
|
|
|
import sys,string,argparse
|
2017-01-23 21:21:45 +00:00
|
|
|
|
2017-04-10 03:36:04 +00:00
|
|
|
#lochar = 0x20 #48
|
|
|
|
#hichar = 0x5e #57
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('-s', '--start', type=int, default=0, help="index of first character")
|
|
|
|
parser.add_argument('-e', '--end', type=int, default=255, help="index of last character")
|
|
|
|
parser.add_argument('-H', '--height', type=int, default=8, help="character height")
|
|
|
|
parser.add_argument('-i', '--invert', action="store_true", help="invert bits")
|
2017-05-18 02:33:56 +00:00
|
|
|
parser.add_argument('-r', '--rotate', action="store_true", help="rotate bits")
|
2018-08-13 03:29:05 +00:00
|
|
|
parser.add_argument('-f', '--flip', action="store_true", help="flip bits (vertically)")
|
|
|
|
parser.add_argument('-m', '--mirror', action="store_true", help="mirror bits (horizontally)")
|
2017-04-10 03:36:04 +00:00
|
|
|
outfmtgroup = parser.add_mutually_exclusive_group()
|
|
|
|
outfmtgroup.add_argument("-A", "--asmhex", action="store_true", help="DASM-compatible hex")
|
2017-05-18 02:33:56 +00:00
|
|
|
outfmtgroup.add_argument("-B", "--asmdb", action="store_true", help="Z80ASM-compatible hex")
|
2017-04-10 03:36:04 +00:00
|
|
|
outfmtgroup.add_argument("-C", "--carray", action="store_true", help="Nested C array")
|
|
|
|
outfmtgroup.add_argument("-F", "--flatcarray", action="store_true", help="Flat C array")
|
2018-02-03 20:20:56 +00:00
|
|
|
outfmtgroup.add_argument("-V", "--verilog", action="store_true", help="Verilog-compatible hex")
|
2017-04-10 03:36:04 +00:00
|
|
|
parser.add_argument('bdffile', help="BDF bitmap file")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
height = args.height
|
|
|
|
lochar = args.start
|
|
|
|
hichar = args.end
|
|
|
|
invert = args.invert
|
|
|
|
flip = args.flip
|
|
|
|
rotate = args.rotate
|
2018-08-13 03:29:05 +00:00
|
|
|
mirror = args.mirror
|
2017-01-23 21:21:45 +00:00
|
|
|
|
|
|
|
chars = {}
|
|
|
|
inbitmap = 0
|
2017-04-15 04:12:21 +00:00
|
|
|
with open(args.bdffile,'r') as f:
|
2017-01-23 21:21:45 +00:00
|
|
|
lines = f.readlines()
|
|
|
|
for l in lines:
|
|
|
|
l = l.strip()
|
|
|
|
toks = l.split()
|
|
|
|
#print l,toks
|
|
|
|
if toks[0] == 'ENCODING':
|
|
|
|
chord = int(toks[1])
|
|
|
|
elif toks[0] == 'BITMAP':
|
|
|
|
inbitmap = True
|
|
|
|
bytes = []
|
|
|
|
elif toks[0] == 'ENDCHAR':
|
|
|
|
inbitmap = False
|
|
|
|
if chord >= lochar and chord <= hichar:
|
|
|
|
while len(bytes) < height:
|
|
|
|
bytes.insert(0,0)
|
|
|
|
assert(len(bytes) == height)
|
|
|
|
bytes.reverse()
|
2017-04-10 03:36:04 +00:00
|
|
|
#print chord,bytes
|
2017-01-23 21:21:45 +00:00
|
|
|
chars[chord] = bytes
|
|
|
|
elif inbitmap and len(toks) == 1:
|
|
|
|
byte = int(toks[0],16)
|
|
|
|
bytes.append(byte)
|
|
|
|
|
2018-08-13 03:29:05 +00:00
|
|
|
def revbits(n):
|
|
|
|
r = 0
|
|
|
|
for i in range(0,8):
|
|
|
|
if (n & (1<<i)):
|
|
|
|
r |= (1<<(7-i))
|
|
|
|
return r
|
|
|
|
|
2017-01-23 21:21:45 +00:00
|
|
|
# output font table
|
|
|
|
x = 0
|
|
|
|
output = []
|
2017-02-01 18:21:17 +00:00
|
|
|
revoutput = []
|
2017-01-23 21:21:45 +00:00
|
|
|
rotoutput = []
|
|
|
|
rot2output = []
|
|
|
|
for ch in range(lochar,hichar+1):
|
|
|
|
bytes = chars.get(ch)
|
|
|
|
if not bytes:
|
|
|
|
bytes = [0] * height
|
2017-04-10 03:36:04 +00:00
|
|
|
if flip:
|
|
|
|
bytes.reverse()
|
2018-08-13 03:29:05 +00:00
|
|
|
if mirror:
|
|
|
|
for i in range(0,height):
|
|
|
|
bytes[i] = revbits(bytes[i])
|
2017-04-10 03:36:04 +00:00
|
|
|
if rotate:
|
|
|
|
rotbytes = [0] * height
|
|
|
|
for x in range(0,height):
|
|
|
|
for y in range(0,height):
|
|
|
|
rotbytes[-1-x] |= (((bytes[-1-y]>>x)&1)<<y)
|
|
|
|
bytes = rotbytes
|
|
|
|
#rotoutput[-7+x] |= (((output[-1-y]>>x)&1)<<y)
|
|
|
|
#rotoutput[-1-x] |= (((output[-1-y]>>x)&1)<<y)
|
|
|
|
#rot2output[-1-x] |= (((output[-7+y]>>x)&1)<<y)
|
2017-01-23 21:21:45 +00:00
|
|
|
for b in bytes:
|
|
|
|
output.append(b)
|
|
|
|
|
|
|
|
def tohex(v):
|
|
|
|
return '%02x'%v
|
|
|
|
def tohex2(v):
|
|
|
|
return '0x%02x'%v
|
2018-02-26 21:42:20 +00:00
|
|
|
def tohexv(v):
|
|
|
|
return "8'h%02x"%v
|
2017-01-23 21:21:45 +00:00
|
|
|
|
2017-04-10 03:36:04 +00:00
|
|
|
for arr in [output]:
|
|
|
|
if args.asmhex:
|
|
|
|
print '\thex ' + string.join(map(tohex,arr),'')
|
2017-05-18 02:33:56 +00:00
|
|
|
if args.asmdb:
|
|
|
|
for i in range(0,len(output),height):
|
|
|
|
print '.DB', string.join(map(tohex2,arr[i:i+height]),','), ';%d'%(i/height+lochar)
|
2017-04-10 03:36:04 +00:00
|
|
|
if args.carray:
|
|
|
|
print "static char FONT[%d][%d] = {" % (hichar-lochar+1, height)
|
|
|
|
for i in range(0,len(output),height):
|
|
|
|
print '{', string.join(map(tohex2,arr[i:i+height]),','), '},',
|
|
|
|
print
|
|
|
|
print "};"
|
|
|
|
if args.flatcarray:
|
|
|
|
print "static char FONT[%d] = {" % ((hichar-lochar+1) * height)
|
|
|
|
print string.join(map(tohex2,arr),',')
|
|
|
|
print "}";
|
2018-02-03 20:20:56 +00:00
|
|
|
if args.verilog:
|
|
|
|
j = 0
|
|
|
|
for i in range(0,len(output),height):
|
|
|
|
#print "rom["+str(j)+"] = 32'h" + string.join(map(tohex,arr[i:i+height/2]),'') + ";"
|
|
|
|
#j += 1
|
|
|
|
#print "rom["+str(j)+"] = 32'h" + string.join(map(tohex,arr[i+height/2:i+height]),'') + ";"
|
|
|
|
#j += 1
|
2018-02-26 21:42:20 +00:00
|
|
|
#print "rom["+str(j)+"] = 64'h" + string.join(map(tohex,arr[i:i+height]),'') + ";"
|
|
|
|
print string.join(map(tohexv,arr[i:i+height]),',') + ", //%d" % (i/height)
|
2018-02-03 20:20:56 +00:00
|
|
|
j += 1
|