Error loading font for Chinese path
monkeycc opened this issue · 3 comments
monkeycc commented
ttf_path E:\中文\config\ttf\MiSans-Regular.ttf
ttf_path = r"E:\中文\config\ttf\MiSans-Regular.ttf"
with dpg.font_registry():
with dpg.font(
ttf_path, 40, default_font=True
) as default_font:
dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)
dpg.bind_font(default_font)
Exception: Error: [1000] Message: Font file could not be found
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\anaconda3\envs\gui\lib\site-packages\dearpygui\dearpygui.py", line 1905, in font_registry
yield widget
File "e:\code\main.py", line 440, in run
ttf_path, 40, default_font=True
File "E:\anaconda3\envs\gui\lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "E:\anaconda3\envs\gui\lib\site-packages\dearpygui\dearpygui.py", line 1878, in font
widget = internal_dpg.add_font(file, size, label=label, user_data=user_data, use_internal_label=use_internal_label, tag=tag, pixel_snapH=pixel_snapH, parent=parent, **kwargs)
SystemError: <built-in function add_font> returned a result with an error set
During handling of the above exception, another exception occurred:
Exception: Error: [1009] Message: No container to pop.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "e:/code/main.py", line 15, in <module>
main(parent)
File "e:/code/main.py", line 11, in main
dbok.run(dizhi)
File "e:\code\main.py", line 445, in run
dpg.add_font_range_hint(dpg.mvFontRangeHint_Chinese_Full)
File "E:\anaconda3\envs\gui\lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "E:\anaconda3\envs\gui\lib\site-packages\dearpygui\dearpygui.py", line 1907, in font_registry
internal_dpg.pop_container_stack()
SystemError: <built-in function pop_container_stack> returned a result with an error set
How to solve it ?
v-ein commented
This error occurs because DPG tries to interpret a UTF-8 string as an ANSI string. Unfortunately there's no workaround because further in the code, the same string is treated as UTF-8.
Here's what happens:
- The path to the font file is passed to
mvFont
as a Python object; - That object gets converted to a UTF-8 string, which is pointed to by a
char*
pointer (seemvFont::handleSpecificRequiredArgs
->ToString
->_PyUnicode_AsString
->PyUnicode_AsUTF8
). mvFont
then attempts to check whether the font file exists. It does so by opening the file withstd::ifstream::open
, which receives achar*
- and on Windows, treats that pointer as ANSI encoded string (correct me if I'm wrong).- This call fails for non-ASCII strings.
- If the
ifstream::open
check succeeded, the path would further be passed toImFontAtlas::AddFontFromFileTTF
->ImFileLoadToMemory
->ImFileOpen
. On Windows,ImFileOpen
explicitly converts the file name from UTF-8 to UTF-16 (wchar_t*
), thus treating making the entire call chain (andImFontAtlas::AddFontFromFileTTF
) UTF-8 aware.