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

Error loading font for Chinese path #2297

Open
monkeycc opened this issue Mar 5, 2024 · 3 comments
Open

Error loading font for Chinese path #2297

monkeycc opened this issue Mar 5, 2024 · 3 comments

Comments

@monkeycc
Copy link

monkeycc commented Mar 5, 2024

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
Copy link
Contributor

v-ein commented Mar 5, 2024

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 (see mvFont::handleSpecificRequiredArgs -> ToString -> _PyUnicode_AsString -> PyUnicode_AsUTF8).
  • mvFont then attempts to check whether the font file exists. It does so by opening the file with std::ifstream::open, which receives a char* - 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 to ImFontAtlas::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 (and ImFontAtlas::AddFontFromFileTTF) UTF-8 aware.

@v-ein
Copy link
Contributor

v-ein commented May 24, 2024

Just curious, why are you closing it as completed? I believe the issue still exists and needs a fix.

@monkeycc monkeycc reopened this May 25, 2024
@monkeycc
Copy link
Author

Thank you, I thought nobody paid attention to this issue anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants