Roughing out first pass on making auto font tables

This commit is contained in:
Ben V. Brown
2019-06-17 18:41:27 +10:00
parent c70a63b44b
commit 35ac6d2455
4 changed files with 2586 additions and 542 deletions

View File

@@ -5,10 +5,10 @@ import json
import os
import io
import sys
from font import getFontMap
TRANSLATION_CPP = "Translation.cpp"
try :
try:
to_unicode = unicode
except NameError:
to_unicode = str
@@ -28,13 +28,13 @@ def loadJson(fileName, skipFirstLine):
# Reading all language translations into a dictionary by langCode
def readTranslations(jsonDir):
langDict = {}
# Read all translation files from the input dir
for fileName in os.listdir(jsonDir):
fileWithPath = os.path.join(jsonDir, fileName)
lf = fileName.lower()
# Read only translation_XX.json
if lf.startswith("translation_") and lf.endswith(".json"):
try:
@@ -54,15 +54,18 @@ def readTranslations(jsonDir):
# ...cause they should be the same!
if langCode != langCodeFromJson:
raise ValueError("Invalid languageCode " + langCodeFromJson + " in file " + fileName)
raise ValueError("Invalid languageCode " + langCodeFromJson +
" in file " + fileName)
langDict[langCode] = lang
return langDict
def writeStart(f):
f.write(to_unicode("""// WARNING: THIS FILE WAS AUTO GENERATED BY make_translation.py. PLEASE DO NOT EDIT.
f.write(
to_unicode(
"""// WARNING: THIS FILE WAS AUTO GENERATED BY make_translation.py. PLEASE DO NOT EDIT.
#include "Translation.h"
#ifndef LANG
@@ -75,16 +78,76 @@ def escapeC(s):
return s.replace("\"", "\\\"")
def getLetterCounts(defs, lang):
textList = []
#iterate over all strings
obj = lang['menuOptions']
for mod in defs['menuOptions']:
eid = mod['id']
textList.append(obj[eid]['desc'])
# collapse all strings down into the composite letters and store totals for these
symbolCounts = {}
for line in textList:
for letter in line:
symbolCounts[letter] = symbolCounts.get(letter, 0) + 1
symbolCounts = sorted(
symbolCounts.items(),
key=lambda kv: kv[1]) # swap to Big -> little sort order
symbolCounts = list(map(lambda x: x[0], symbolCounts))
symbolCounts.reverse()
return symbolCounts
def getFontMapAndTable(textList):
# the text list is sorted
# allocate out these in their order as number codes
symbolMap = {}
index = 1
for sym in textList:
symbolMap[sym] = "\\x%0.2X" % index
index = index + 1
# Get the font table
fontTableStrings = []
fontTable = getFontMap()
for sym in textList:
if sym not in fontTable:
print(f'Missing font element for {sym}')
exit(1)
fontLine = fontTable[sym]
fontTableStrings.append(fontLine)
outputTable = "const uint8_t userFont_12[] = {" + to_unicode("\n")
for line in fontTableStrings:
# join font table int one large string
outputTable = outputTable + line + to_unicode("\n")
outputTable = outputTable + "};" + to_unicode("\n")
return (outputTable, symbolMap)
def convStr(symbolConversionTable,text):
# convert all of the symbols from the string into escapes for their content
outputString = ""
for c in text:
if c not in symbolConversionTable:
print(f'Missing font definition for {c}')
else:
outputString = outputString + symbolConversionTable[c]
return outputString
def writeLanguage(languageCode, defs, f):
print("Generating block for " + languageCode)
lang = langDict[languageCode]
#Iterate over all of the text to build up the symbols & counts
textList = getLetterCounts(defs, lang)
# From the letter counts, need to make a symbol translator & write out the font
(fontTableText, symbolConversionTable) = getFontMapAndTable(textList)
f.write(to_unicode("\n#ifdef LANG_" + languageCode + "\n"))
f.write(fontTableText)
try:
langName = lang['languageLocalName']
except KeyError:
langName = languageCode
f.write(to_unicode("// ---- " + langName + " ----\n\n"))
try:
@@ -92,54 +155,59 @@ def writeLanguage(languageCode, defs, f):
except KeyError:
cyrillic = False
if cyrillic :
if cyrillic:
f.write(to_unicode("#define CYRILLIC_GLYPHS\n\n"))
# ----- Writing SettingsDescriptions
obj = lang['menuOptions']
f.write(to_unicode("const char* SettingsDescriptions[] = {\n"))
maxLen = 25
for mod in defs['menuOptions']:
eid = mod['id']
if 'feature' in mod:
f.write(to_unicode("#ifdef " + mod['feature'] + "\n"))
f.write(to_unicode(" /* " + eid.ljust(maxLen)[:maxLen] + " */ "))
f.write(to_unicode("\"" + escapeC(obj[eid]['desc']) + "\",\n"))
f.write(to_unicode("\"" + convStr(symbolConversionTable,(obj[eid]['desc'])) + "\",\n"))
if 'feature' in mod:
f.write(to_unicode("#endif\n"))
f.write(to_unicode("};\n\n"))
# ----- Writing Message strings
obj = lang['messages']
for mod in defs['messages']:
eid = mod['id']
f.write(to_unicode("const char* " + eid + " = \"" + escapeC(obj[eid]) + "\";\n"))
f.write(
to_unicode("const char* " + eid + " = \"" + convStr(symbolConversionTable,(obj[eid])) +
"\";\n"))
f.write(to_unicode("\n"))
# ----- Writing Characters
obj = lang['characters']
for mod in defs['characters']:
eid = mod['id']
f.write(to_unicode("const char " + eid + " = '" + obj[eid] + "';\n"))
f.write(to_unicode("\n"))
# ----- Menu Options
# Menu type
f.write(to_unicode("const enum ShortNameType SettingsShortNameType = SHORT_NAME_" + ("DOUBLE" if lang['menuDouble'] else "SINGLE") + "_LINE;\n"))
f.write(
to_unicode(
"const enum ShortNameType SettingsShortNameType = SHORT_NAME_" +
("DOUBLE" if lang['menuDouble'] else "SINGLE") + "_LINE;\n"))
# ----- Writing SettingsDescriptions
obj = lang['menuOptions']
f.write(to_unicode("const char* SettingsShortNames[][2] = {\n"))
maxLen = 25
for mod in defs['menuOptions']:
eid = mod['id']
@@ -147,39 +215,47 @@ def writeLanguage(languageCode, defs, f):
f.write(to_unicode("#ifdef " + mod['feature'] + "\n"))
f.write(to_unicode(" /* " + eid.ljust(maxLen)[:maxLen] + " */ "))
if lang['menuDouble']:
f.write(to_unicode("{ \"" + escapeC(obj[eid]['text2'][0]) + "\", \"" + escapeC(obj[eid]['text2'][1]) + "\" },\n"))
f.write(
to_unicode("{ \"" + escapeC(obj[eid]['text2'][0]) + "\", \"" +
escapeC(obj[eid]['text2'][1]) + "\" },\n"))
else:
f.write(to_unicode("{ \"" + escapeC(obj[eid]['text']) + "\" },\n"))
if 'feature' in mod:
f.write(to_unicode("#endif\n"))
f.write(to_unicode("};\n\n"))
# ----- Writing Menu Groups
obj = lang['menuGroups']
f.write(to_unicode("const char* SettingsMenuEntries[" + str(len(obj)) + "] = {\n"))
f.write(
to_unicode("const char* SettingsMenuEntries[" + str(len(obj)) +
"] = {\n"))
maxLen = 25
for mod in defs['menuGroups']:
eid = mod['id']
f.write(to_unicode(" /* " + eid.ljust(maxLen)[:maxLen] + " */ "))
f.write(to_unicode("\"" + escapeC(obj[eid]['text2'][0] + "\\n" + obj[eid]['text2'][1]) + "\",\n"))
f.write(
to_unicode("\"" + escapeC(obj[eid]['text2'][0] + "\\n" +
obj[eid]['text2'][1]) + "\",\n"))
f.write(to_unicode("};\n\n"))
# ----- Writing Menu Groups Descriptions
obj = lang['menuGroups']
f.write(to_unicode("const char* SettingsMenuEntriesDescriptions[" + str(len(obj)) + "] = {\n"))
f.write(
to_unicode("const char* SettingsMenuEntriesDescriptions[" +
str(len(obj)) + "] = {\n"))
maxLen = 25
for mod in defs['menuGroups']:
eid = mod['id']
f.write(to_unicode(" /* " + eid.ljust(maxLen)[:maxLen] + " */ "))
f.write(to_unicode("\"" + escapeC(obj[eid]['desc']) + "\",\n"))
f.write(to_unicode("};\n\n"))
# ----- Block end
# ----- Block end
f.write(to_unicode("#endif\n"))
@@ -218,7 +294,7 @@ def orderOutput(langDict):
mandatoryOrder.append(key)
return mandatoryOrder
def writeTarget(outFile, defs, langCodes):
# Start writing the file