#!/usr/bin/env python3 # ===================================================== # Usage # Install Python 3 if you don't have it, and then install the Pillow package like this: # $ pip install Pillow # Then, run the program like this, for example: # $ python font-converter.py font.ttf font # The above command will take a font file called "font.ttf" and slice it out into a directory called "font". # ===================================================== # Licensing # Copyright © 2025 baltdev # Permission is hereby granted, free of # charge, to any person obtaining a copy of this software and associated documentation # files (the “Software”), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, distribute, # sublicense, and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: The above # copyright notice and this permission notice shall be included in all copies or # substantial portions of the Software. # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT # WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO # EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. # ===================================================== from PIL import Image, ImageDraw, ImageFont import sys import os FONT_SIZE = 256 def main(): if len(sys.argv) <= 2: print("Usage: python font-converter.py ") return if not os.path.exists(sys.argv[2]): os.mkdir(sys.argv[2]) font = ImageFont.truetype(sys.argv[1], FONT_SIZE, encoding="unic") min_x, min_y, max_u, max_v = 99999999, 99999999, -99999999, -99999999 for char_unic in range(32, 128): char = chr(char_unic) x, y, u, v = font.getbbox(char) min_x, min_y, max_u, max_v = min(x, min_x), min(y, min_y), max(u, max_u), max(v, max_v) width, height = max_u - min_x, max_v - min_y print(f"w: {width}, h: {height}, x: {min_x}, y: {min_y}, u: {max_u}, v: {max_v}") blank = Image.new("L", (width, height), 'white') for char_unic in range(32, 128): char = chr(char_unic) x, y, u, v = font.getbbox(char) print(u - x, v - y, width, height) canvas = Image.new("L", (width, height), "black") draw = ImageDraw.Draw(canvas) draw.text((-min_x, -min_y), char, 'white', font) glyph = Image.merge("RGBA", (blank, blank, blank, canvas)) glyph.save(f"{sys.argv[2]}/{char_unic}.png") if __name__ == "__main__": main()