From d3bde893e3248e3bb59c3dc68b226d0c4378d74f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 9 Jan 2021 00:57:50 +0100 Subject: [PATCH] added a Utility script for sprite font creation --- Utilities/GetSupportedCharacters.py | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Utilities/GetSupportedCharacters.py diff --git a/Utilities/GetSupportedCharacters.py b/Utilities/GetSupportedCharacters.py new file mode 100644 index 0000000..d93e259 --- /dev/null +++ b/Utilities/GetSupportedCharacters.py @@ -0,0 +1,45 @@ +""" +This script prints out a xml element that contains efficiently calculated regions for +all of the font's supported unicode characters. Pass the font file (not the spritefont xml) as an argument. +This script requires FontTools. Install using: pip install fonttools +""" + +""" The amount of characters that can be missing in a sequence to still put them in one region """ +leeway = 0 + +from fontTools.ttLib import TTFont +from sys import argv + + +def print_region(start, end): + print(f" ") + print(f" &#{start};") + print(f" &#{end};") + print(f" ") + + +# get all supported characters and sort them +ttf = TTFont(argv[1]) +chars = list(set(y[0] for x in ttf["cmap"].tables for y in x.cmap.items())) +chars.sort() +ttf.close() + +# split them into regions based on the leeway +start = -1 +last = 0 +total = 0 +print("") +for char in chars: + if char - last > leeway + 1: + if start != -1: + print_region(start, last) + total += last - start + 1 + start = char + last = char +# print the remaining region +print_region(start, last) +total += last - start + 1 +print("") + +print(f"The font contains {len(chars)} characters") +print(f"The spritefont will contain {total} characters") \ No newline at end of file