Skip to content

Commit

Permalink
Merge pull request #77300 from ehughsbaird/use-a-for-loop
Browse files Browse the repository at this point in the history
Make imgui font glyph initializers more compiler and binary size friendly
  • Loading branch information
Night-Pryanik authored Nov 30, 2024
2 parents 9b7b74c + 55c4215 commit 221440c
Show file tree
Hide file tree
Showing 2 changed files with 1,567 additions and 41,477 deletions.
78 changes: 60 additions & 18 deletions build-scripts/get_translation_characters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,41 @@
# creates ImGui glyph ranges for each locale, so that every locale can
# have properly rendered text.

import itertools
import sys
from cldr_language_helpers import alphabets


def add_preprocessor():
print("#if defined(__GNUC__) or defined(__clang__)\n"
"#define NOINLINE __attribute__ ((noinline))\n"
"#else\n"
"#define NOINLINE __declspec(noinline)\n"
"#endif\n"
"#if defined(__clang__)\n"
"#define NOUNROLL _Pragma(\"clang loop unroll(disable)\")\n"
"#elif defined(__GNUC__)\n"
"#define NOUNROLL #pragma GCC unroll 0\n"
"#else\n"
"#define NOUNROLL\n"
"#endif\n")


def main():
print("// generated by get_translation_characters.py; example:")
print("// ./build-scripts/get_translation_characters.py en ar cs da de "
print("// generated by get_translation_characters.py; example:\n"
"// ./build-scripts/get_translation_characters.py en ar cs da de "
"el es fr hu id is it ja ko nb nl pl pt ru sr tr uk_UA zh_Hans "
"zh_Hant > src/cldr/imgui-glyph-ranges.cpp\n")
print("// NOLINTBEGIN(cata-static-declarations,readability-function-size)")
"zh_Hant > src/cldr/imgui-glyph-ranges.cpp\n\n"
"// NOLINTBEGIN(cata-static-declarations,readability-function-size,"
"modernize-avoid-c-arrays)\n")
add_preprocessor()
print("static NOINLINE void AddGlyphs( ImFontGlyphRangesBuilder *b, "
"ImWchar const *glyphp, ImWchar const *end) {\n"
" NOUNROLL\n"
" for( ; glyphp != end; ++glyphp ) {\n"
" b->AddChar(*glyphp);\n"
" }\n"
"}\n")
try:
for language in sys.argv[1:]:
print_func(language)
Expand All @@ -25,22 +50,39 @@ def main():
return 1


def chunks(xs, n):
n = max(1, n)
return (xs[i:i + n] for i in range(0, len(xs), n))


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")


def print_add_char(c):
return '\n'.join([f"b->AddChar({hex(ord(c))});" for c in c])
"ImFontGlyphRangesBuilder *b) {")
# 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 = [hex(c) for c in sorted(list(set(chars)))]

print(" static constexpr ImWchar glyphs[] = {")
char_chunks = list(chunks(chars, 16))
for cs in itertools.islice(char_chunks, len(char_chunks) - 1):
print(", ".join(cs) + ",")
print(", ".join(char_chunks[-1]))
print(" };")
print(" AddGlyphs(b, glyphs, glyphs + std::extent_v<decltype(glyphs)>);")
print("}")


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit 221440c

Please sign in to comment.