-
Notifications
You must be signed in to change notification settings - Fork 89
How to load fonts with specific glyph ranges (e.g., Chinese, Japanese, Korean) using hello_imgui.load_font #196
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
Comments
I don't understand your question (there is none in your message, in fact). Does your work around work? |
ImFontConfig.GlyphRanges
in python binding
Hello, I come back to this question after a few months, sorry for the delay. I understand that you needed a way to translate the glyph ranges coming from Dear ImGui, into ranges used by hello_imgui.load_font. This commit adds support for this. Example usage: """Demonstrates how to load a font with Chinese characters and display them in the GUI,
using the common glyph ranges defined in by ImGui.
"""
from imgui_bundle import imgui, hello_imgui, imgui_ctx
from imgui_bundle.demos_python import demo_utils
demo_utils.set_hello_imgui_demo_assets_folder()
font_cn: imgui.ImFont = None
def load_font():
global font_cn
if not hello_imgui.asset_exists("fonts/NotoSerifSC-VariableFont_wght.ttf"):
return
# Note: this font is not provided with the ImGui bundle (too large).
# You will need to provide it yourself, or use another font.
font_filename = "fonts/NotoSerifSC-VariableFont_wght.ttf"
# The range of Chinese characters is defined by ImGui as a single list of characters (List[ImWchar]), with a terminating 0.
# (each range is a pair of successive characters in this list, with the second character being the last one in the range)
cn_glyph_ranges_imgui = imgui.get_io().fonts.get_glyph_ranges_chinese_simplified_common()
# We need to convert this list into a list of pairs (List[ImWcharPair]), where each pair is a range of characters.
cn_glyph_ranges_pair = hello_imgui.translate_common_glyph_ranges(cn_glyph_ranges_imgui)
font_loading_params = hello_imgui.FontLoadingParams()
font_loading_params.glyph_ranges = cn_glyph_ranges_pair
font_cn = hello_imgui.load_font(font_filename, 40.0, font_loading_params)
def gui():
if font_cn is not None:
with imgui_ctx.push_font(font_cn):
imgui.text("Hello world")
imgui.text("你好,世界")
else:
imgui.text("Font file not found")
imgui.text_wrapped("""
This font is not provided with the ImGui bundle (too large).
You will need to provide it yourself, or use another font.
""")
runner_params = hello_imgui.RunnerParams()
runner_params.callbacks.load_additional_fonts = load_font
runner_params.callbacks.show_gui = gui
hello_imgui.run(runner_params) ![]() |
Did you call hello_imgui.set_assets_folder |
|
From the code: Did you add it ? |
I have tried many different suffixes for Chinese font files, and I have also experimented with various file sizes, but there was no response. |
@pthom #include ImFont* font_cn = nullptr; using VoidFunction = std::function<void(void)>; void ToAssetsFolder() void LoadFont()
} void RunMyWindow(VoidFunction demoFunction) int main(int, char**)
#define Window_Details(label, function_name) DemoDetails{ label, function_name }
} |
I apologize for my mistake here. When I added "# pragma executioner_character_st (" utf-8 ")" at the beginning of the code, everything in Chinese was displayed normally. |
env:
imgui-bundle
1.3.0imgui_bundle/bindings/imgui_bundle/imgui/__init__.pyi
Lines 9451 to 9460 in 541ed1a
https://github.com/ocornut/imgui/blame/d3c3514a59bb31406c954c2b525f330e9d167845/imgui.h#L2889
Context
I want to load Chinese fonts and specify glyph_range.
test code
Workaround
The text was updated successfully, but these errors were encountered: