Skip to content

Commit

Permalink
Use for loops to enumerate glyphs needed by font
Browse files Browse the repository at this point in the history
Instead of having a line (or more than one) for each glyph that a
language will need in a font, sort them and collapse adding adjacent
characters into a for loop.

This removes about 34k LOC.

Don't collapse short spans, which will require as many or more lines
with a for loop.

There are perhaps other patterns that could be detected than immediately
adjacent, but detecting those are more difficult.
  • Loading branch information
ehughsbaird committed Oct 24, 2024
1 parent 2d90bfe commit 97e76a6
Show file tree
Hide file tree
Showing 2 changed files with 2,788 additions and 37,745 deletions.
51 changes: 39 additions & 12 deletions build-scripts/get_translation_characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,47 @@ def main():

def print_func(language):
print(f"static void AddGlyphRangesFromCLDRFor{language.upper()}("
"ImFontGlyphRangesBuilder *b) {{")
print('\n'.join([print_add_char(c)
for c in alphabets.ALPHABETS_BY_LANG_MAP[language]]))
print('\n'.join([print_add_char(c.upper())
for c in alphabets.ALPHABETS_BY_LANG_MAP[language]]))
print('\n'.join([print_add_char(c)
for c in alphabets.NUMBERS_BY_LANG_MAP[language]]))
print('\n'.join([print_add_char(c)
for c in alphabets.PUNCTUATION_BY_LANG_MAP[language]]))
print("}\n")
"ImFontGlyphRangesBuilder *b)\n{")
# All of the glyphs used this language
chars = []
for c in alphabets.ALPHABETS_BY_LANG_MAP[language]:
for g in c:
chars.append(ord(g))
for c in alphabets.ALPHABETS_BY_LANG_MAP[language]:
for g in c.upper():
chars.append(ord(g))
for c in alphabets.NUMBERS_BY_LANG_MAP[language]:
for g in c:
chars.append(ord(g))
for c in alphabets.PUNCTUATION_BY_LANG_MAP[language]:
for g in c:
chars.append(ord(g))
# Sort and remove duplicates, so we can detect sequences
chars = sorted(list(set(chars)))

# Detect if this character is just one greater than the last character,
# and so could be added with a for loop instead
last_char = chars[0]
output = [[last_char, 1]]
for char in chars[1:]:
if char - last_char == 1:
output[-1][1] += 1
else:
output.append([char, 1])
last_char = char

def print_add_char(c):
return '\n'.join([f"b->AddChar({hex(ord(c))});" for c in c])
for char, length in output:
if length == 1:
print(f" b->AddChar({hex(char)});")
# Don't add a for loop if it's neutral or requires more lines
elif length < 4:
for i in range(length):
print(f" b->AddChar({hex(char + i)});")
else:
print(f" for(int i = 0; i < {length}; ++i)" + " {\n" +
f" b->AddChar({hex(char)} + i);" + "\n" +
" }")
print("}\n")


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 97e76a6

Please sign in to comment.