Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make imgui font glyph initializers more compiler and binary size friendly #77300

Merged
merged 4 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading