mirror of
https://github.com/libretro/libretro-puzzlescript.git
synced 2026-11-10 11:01:32 -10:00
Status: files running, incorrect logic, incorrect graphics. Not sure what's going on at the moment.
52 lines
2.3 KiB
Python
52 lines
2.3 KiB
Python
'''
|
||
Generate font file for PuzzleScript given TTF font
|
||
|
||
'''
|
||
|
||
import argparse
|
||
from PIL import Image, ImageFont, ImageDraw
|
||
|
||
if __name__ == '__main__':
|
||
parser = argparse.ArgumentParser()
|
||
parser.add_argument('--font')
|
||
parser.add_argument('--width', type=int, default=11)
|
||
parser.add_argument('--height', type=int, default=25)
|
||
parser.add_argument('--baseline', type=int, default=20)
|
||
parser.add_argument('--size', type=int, default=18)
|
||
parser.add_argument('-o', required=True)
|
||
args = parser.parse_args()
|
||
chars = "0123456789abcdefghijklmnopqrstuvwx×yzABCDEFGHIJKLMNOPQRSTUVWXYZ.·•…†‡ƒ‚„,;:?¿!¡@£$%‰^&*()+÷±-–—_= {}[]'‘’“”\"/\\|¦<‹«>›»~˜`#ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßẞàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽž€™¢¤¥§¨©®ªº¬¯°"
|
||
scale = 3
|
||
colors = 5
|
||
colormap = "02341"
|
||
font = ImageFont.truetype(args.font, args.size * scale)
|
||
with open(args.o, 'wt', encoding='utf8') as outfile:
|
||
outfile.write('var font = {\n')
|
||
for c in chars:
|
||
escapedc = c
|
||
if c == "'":
|
||
escapedc = "\\'"
|
||
if c == "\\":
|
||
escapedc = "\\\\"
|
||
outfile.write(f"'{escapedc}':'")
|
||
image = Image.new('L', (args.width * scale, args.height * scale))
|
||
draw = ImageDraw.Draw(image)
|
||
draw.text(((args.width * scale) // 2, args.baseline * scale), c, font=font, fill=255, anchor='ms')
|
||
image = image.resize((args.width, args.height), resample=Image.LANCZOS)
|
||
for y in range(args.height):
|
||
line = ''
|
||
for x in range(args.width):
|
||
pixel = image.getpixel((x, y))
|
||
pixel = (pixel * colors) // 256
|
||
line += colormap[pixel]
|
||
outfile.write(f'\\n{line}')
|
||
outfile.write("',\n")
|
||
outfile.write('''}
|
||
var fontKeys = Object.keys(font);
|
||
var fontIndex = {};
|
||
|
||
for (var i = 0; i < fontKeys.length; i++) {
|
||
fontIndex[fontKeys[i]] = i;
|
||
}
|
||
''')
|