gff/gff

71 lines
1.6 KiB
Python
Executable File

#!/usr/bin/env python3
from sys import argv
from PIL import Image, ImageDraw, ImageFont
if (len(argv) != 4):
raise BaseException("Three arguments expected: " + \
"<font path> <font size> <output image>")
font_path = argv[1]
font_size = int(argv[2])
image_out = argv[3]
mode = '1' # 1-bit depth
background_color = (1) # white
foreground_color = (0) # black
# load font
font = ImageFont.truetype(font_path, font_size)
# find max char size
char_width = 0
char_height = 0
for i in range(95):
bbox = list(font.getbbox(chr(32 + i)))
# don't you dare overlap
if (bbox[0] < 0):
bbox[2] = -bbox[0]
if (bbox[1] < 0):
bbox[3] -= bbox[1]
if bbox[2] > char_width:
char_width = bbox[2]
if bbox[3] > char_height:
char_height = bbox[3]
image_out_width = char_width * 16
image_out_height = char_height * 6
# fxconv metadata
print(f"{image_out.split('/')[-1]}:")
print(" type: font")
print(" charset: print")
print(f" grid.size: {char_width}x{char_height}")
# create image
im = Image.new(mode, (image_out_width, image_out_height))
# draw
draw = ImageDraw.Draw(im)
draw.rectangle((0, 0, image_out_width, image_out_height), fill=background_color)
x = -char_width
y = 0
for i in range(95):
x += char_width
char = chr(32 + i)
bbox = font.getbbox(char)
x_mod = 0
y_mod = 0
# don't you dare overlap
if (bbox[0] < 0):
x_mod = -bbox[0]
if (bbox[1] < 0):
y_mod = -bbox[1]
if i != 0 and i % 16 == 0:
x = 0
y += char_height
draw.text((x + x_mod, y + y_mod), char, fill=foreground_color, font=font)
# save
im.save(image_out)