/ mkfontimg.py
mkfontimg.py
1 #!/usr/bin/python 2 import sys 3 import struct 4 import PIL.Image as Image 5 6 text = """\ 7 10 poke53280,0:poke53281,1 8 20 print"\xd0\xd3"; 9 30 fory=0to7 10 40 forx=0to15 11 50 poke49152+111+x+80*y,x+16*y 12 60 next 13 70 next 14 80 list""" 15 16 lowercase = False 17 if sys.argv[1] == '-l': 18 lowercase = True 19 del sys.argv[1] 20 21 def toscreencode(ch): 22 cc = ord(ch) 23 if '`' <= ch <= '~': return cc - 96 24 return cc 25 26 charset = open(sys.argv[1], "rb").read() 27 28 29 FG = 0xFFDE7886 30 BG = 0xFFAA3A48 31 32 def putcat(img, r, c, code, lowercase): 33 inverse = code & 128 34 code = code % 128 35 idx = code*8 36 for y in range(8): 37 data = charset[idx+y+lowercase*1024] 38 if not inverse: data = ~data 39 for x in range(4): 40 if ~data & (1<<(3-x)): 41 img.putpixel((8+4*c+x,8+8*r+y), FG) 42 43 BG_buf = struct.pack('<I', BG) 44 img = Image.frombytes('RGBA', (16+188,16+64), BG_buf * (16+188)*(16+64)) 45 46 for i in range(8): 47 for x in range(img.width): 48 img.putpixel((x, i), FG) 49 img.putpixel((x, img.height-i-1), FG) 50 for y in range(img.height): 51 img.putpixel((i, y), FG) 52 img.putpixel((img.width-i-1, y), FG) 53 r=c=0 54 for ch in text: 55 if ch == '\n': 56 r = r + 1 57 c = 0 58 continue 59 putcat(img, r, c, toscreencode(ch), lowercase) 60 c = c + 1 61 if c == 80: 62 r = r + 1 63 c = 0 64 for r in range(8): 65 for c in range(16): 66 putcat(img, r, c+31, r*16+c, lowercase) 67 68 img = img.resize((img.size[0]*3, img.size[1]*3), resample=Image.NEAREST) 69 img.save(sys.argv[2])