48 lines
2.2 KiB
Python
48 lines
2.2 KiB
Python
from pathlib import Path
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
from fontTools.ttLib import TTFont
|
|
root=Path(__file__).parent
|
|
# CC0 fonts; original files and license notices are retained in third_party.
|
|
c64=ImageFont.truetype(str(root/'third_party/c64.ttf'),8)
|
|
ascii_font=ImageFont.truetype(str(root/'third_party/Bescii-Mono.ttf'),8)
|
|
def raster(font,code,baseline):
|
|
im=Image.new('1',(8,8));ImageDraw.Draw(im).text((0,baseline),chr(code),font=font,anchor='ls',fill=1)
|
|
return bytes(sum((128>>x) for x in range(8) if im.getpixel((x,y))) for y in range(8))
|
|
# Read the CC0 C64 outline coordinates directly. FreeType applies the
|
|
# hmtx left bearing and grid fitting at 8px, which shifts partial blocks
|
|
# and loses rows. Sample the original 125-unit pixel grid without hints.
|
|
outline=TTFont(root/'third_party/c64.ttf')
|
|
cmap=outline.getBestCmap()
|
|
def exact_c64(code):
|
|
glyph=outline['glyf'][cmap[code]]
|
|
coords,ends,flags=glyph.getCoordinates(outline['glyf'])
|
|
polygons=[];start=0
|
|
for end in ends:
|
|
polygons.append(list(coords[start:end+1]));start=end+1
|
|
def inside(poly,x,y):
|
|
hit=False
|
|
for i,(ax,ay) in enumerate(poly):
|
|
bx,by=poly[i-1]
|
|
if (ay>y)!=(by>y) and x<(bx-ax)*(y-ay)/(by-ay)+ax:hit=not hit
|
|
return hit
|
|
return bytes(sum(128>>x for x in range(8)
|
|
if any(inside(poly,62.5+x*125,1062.5-y*125) for poly in polygons))
|
|
for y in range(8))
|
|
upper=[bytes(v^255 for v in exact_c64(0xe000+i)) for i in range(128)]
|
|
lower=upper.copy()
|
|
# The shifted bank's alphabet uses the same exact grid coordinates.
|
|
for i in range(26):
|
|
lower[i+1]=exact_c64(97+i)
|
|
lower[i+65]=upper[i+1]
|
|
# Shifted C64 bank has distinct checker/diagonal patterns and a tick.
|
|
def pattern(predicate):
|
|
return bytes(sum(128>>x for x in range(8) if predicate(x,y)) for y in range(8))
|
|
lower[0x5e]=pattern(lambda x,y: ((x//2+y//2)&1)==1)
|
|
lower[0x5f]=pattern(lambda x,y: (x-y)%4>=2)
|
|
lower[0x69]=pattern(lambda x,y: (x+y)%4<2)
|
|
lower[0x7a]=pattern(lambda x,y: (y<=4 and 7-y<=x<=8-y) or
|
|
(3<=y<=6 and x in (1,2)) or (y==5 and x==3))
|
|
ascii_glyphs=[raster(ascii_font,ord(bytes([i]).decode('cp437')),7) for i in range(256)]
|
|
for i in range(32):ascii_glyphs[i]=raster(ascii_font,0x2580+i,7)
|
|
(root/'assets/font.bin').write_bytes(b''.join(upper+lower+ascii_glyphs))
|