#!/usr/bin/env python3 # Copyright (C) 2019 Max Regan # 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. import argparse import io from PIL import ImageFont, ImageDraw import logging logger = logging.getLogger(__name__) def generate_font_file(ttf, output, size, characters): font = ImageFont.truetype(ttf, size) logger.debug("Width of all chars is %d", font.getsize(characters)[0]) characters = list(characters) characters.sort() size = None total = 0 for char in characters: total += font.getsize(char)[0] if size is None: size = font.getsize(str(char)) else: pass logger.debug("Width of %c is %d", char, font.getsize(char)[0]) logger.debug("Total width of %d chars is %d", len(characters), total) logger.debug("Average of %f pixels per char", float(total) / len(characters)) logger.debug("%s", float(total) / len(characters)) def main(): parser = argparse.ArgumentParser("") parser.add_argument("-o", "--output", help="Output file", action="store", type=str) parser.add_argument("-t", "--ttf", help="TrueType font file", action="store", type=str) parser.add_argument("-s", "--size", help="Font point size", action="store", type=int) parser.add_argument("-c", "--characters", help="Font point size", default= "abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789.,!?<>", action="store", type=str) parser.add_argument("-v", "--verbose", help="increase output verbosity", action="store_true") args = parser.parse_args() logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s') generate_font_file(args.ttf, args.output, args.size, args.characters) if __name__ == "__main__": main()