gint font factory first release

This commit is contained in:
KikooDX 2021-04-15 14:11:14 +02:00
commit 362916d3c0
4 changed files with 106 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.png

12
LICENSE Normal file
View File

@ -0,0 +1,12 @@
Copyright (C) 2021 KikooDX
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

23
README Normal file
View File

@ -0,0 +1,23 @@
gint font factory
=================
Create image files to be used with fxsdk's fxconv.
Dependencies
------------
Python 3, pillow
Usage
-----
$ gff <font path> <font size> <output image>
Example:
$ gff "$HOME/.fonts/Kenney Future Narrow" 16 kenney_future_narrow.png
SDOUT
-----
The script output is fxconv metadata.
License
-------
Copyright (C) 2021 KikooDX <kikoodx@paranoici.org>
The content of this repository is under the 0BSD license.
See LICENSE for more informations.

70
gff Executable file
View File

@ -0,0 +1,70 @@
#!/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)