From 72e513e9c0f58cd676016f0980ae61505e8da3dd Mon Sep 17 00:00:00 2001 From: Richard Wheeler <2762690+zephyris@users.noreply.github.com> Date: Fri, 1 Mar 2024 23:56:52 +0000 Subject: [PATCH] Add: Script to check character coverage against OpenTTD translations Also adds running this script to the build process; assumes the OpenTTD source exists in `../openttd`. --- build.sh | 2 ++ checkOpenTTDStrings.py | 67 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 checkOpenTTDStrings.py diff --git a/build.sh b/build.sh index 40342b5..95f195a 100644 --- a/build.sh +++ b/build.sh @@ -15,3 +15,5 @@ cd .. cd openttd-mono bash build.sh cd .. + +python3 checkOpenTTDStrings.py ../openttd/src/lang diff --git a/checkOpenTTDStrings.py b/checkOpenTTDStrings.py new file mode 100644 index 0000000..98ee632 --- /dev/null +++ b/checkOpenTTDStrings.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 + +# Checks characters used in OpenTTD strings against characters present in these fonts + +# Usage: +# python3 checkTranslationChars.py path/to/openttd/src/lang +# python3 path/to/checkTranslationChars.py [run in openttd/src/lang] + +import glob, os, sys +from fontTools.ttLib import TTFont + +if len(sys.argv) > 1: + langpath = sys.argv[1] +else: + langpath = "." + +# maximum number of missing characters to list in full +maxlist = 100 + +# find all ttfs by suffix in directory +suffix = ".ttf" +scriptpath = os.path.dirname(os.path.realpath(__file__)) +files = [ + os.path.join(scriptpath, "openttd-sans", "OpenTTD-Sans.ttf"), + os.path.join(scriptpath, "openttd-serif", "OpenTTD-Serif.ttf"), + os.path.join(scriptpath, "openttd-mono", "OpenTTD-Mono.ttf"), + os.path.join(scriptpath, "openttd-small", "OpenTTD-Small.ttf"), + os.path.join(scriptpath, "openttd-small", "OpenTTD-SmallCaps.ttf") +] +fonts = [] +for file in files: + # for each font, record name and list of character codes available + font = TTFont(file) + fonts.append({ + "name": os.path.basename(file[:-len(suffix)]), + "codes": list(font.getBestCmap().keys()) + }) + +# find all lang files by suffix in directory +suffix = ".txt" +files = glob.glob(os.path.join(langpath, "*" + suffix)) +print(os.path.join(langpath, "*" + suffix)) +for file in files: + # for each language + lang = os.path.basename(file[:-len(suffix)]) + with open(file, "r") as handle: + # read lines and remove empty lines + strings = [x for x in handle.read().splitlines() if x] + # extract translated strings + strings = [x[x.find(":") + 1:] for x in strings if x[0] != "#"] + # list all character codes and characters across strings + codes = sorted(list(set([ord(x) for x in "".join(strings)]))) + chars = [chr(x) for x in codes] + # check if fully supported by each font + print(lang) + completecount = 0 + for font in fonts: + missing = [x for x in codes if x not in font["codes"]] + if len(missing) == 0: + completecount += 1 + else: + if len(missing) <= maxlist: + print("", font["name"], "missing "+str(len(missing))+":", "("+"".join([chr(x) for x in missing])+")") + else: + print("", font["name"], "missing "+str(len(missing))+":", "[too many to list]") + if completecount == len(fonts): + print("", "[complete]") \ No newline at end of file