ice1000 / jimgui

:sparkling_heart: Pure Java binding for dear-imgui
Apache License 2.0
186 stars 13 forks source link

Unicode character support #47

Closed vitalline closed 4 years ago

vitalline commented 4 years ago

When using Unicode characters (such as Cyrillic letters) in JImGui widgets, they are displayed in native encoding. For example, on Windows 10 (with Russian locale), after adding Cyrillic font ranges to font atlas, calling text("Шахматы") displays Шахматы (UTF-8 representation of "Шахматы" incorrectly read as a CP1251 string). I'm assuming that the string's encoding is by default interpreted as a Unicode in the Java bindings, but as the system default encoding in ImGui's C++ implementation.

ice1000 commented 4 years ago

Chinese characters used to work for me. Can you reproduce this in pure C++?

vitalline commented 4 years ago

I cannot, even though I was sure it happened to me before when I was working with C++ ImGui. I can send you a reproducible Java example. See if that helps. example.zip

ice1000 commented 4 years ago

Do you understand how the u8"bla" things work in C++? How does it convert your string into utf8 sequences? Is your Java source code UTF-8-encoded?

vitalline commented 4 years ago
  1. Yes. I expected the string not to display properly without the u8 literal, but it worked as normal both with the literal and without it.
  2. The string is a default Java string literal. I haven't used any additional consersions, including custom string to bytes conversions.
  3. Yes. Both Notepad++ and HxD confirm that. See attached screenshot for an example of how it is displayed. The original string (included in the demo) was "Съешь же ещё этих мягких французских булок, да выпей чаю." demo
ice1000 commented 4 years ago

image

Is this what you've expected?

vitalline commented 4 years ago

Yes, that is exactly what I expected.

ice1000 commented 4 years ago

Yes. Both Notepad++ and HxD confirm that.

You should compile your Java source as UTF-8, in addition to ensuring your Java source is UTF-8 encoded. Here's how:

compileJava.options.encoding = 'UTF-8'
ice1000 commented 4 years ago

If you're willing to help document this, please pull request me! So other people like you can save time.

vitalline commented 4 years ago

Alright, thanks! Should I add a note to README.md, or document it inside JImFont.java/JImFontAtlas.java?

ice1000 commented 4 years ago

Probably README

vitalline commented 4 years ago

Done.

seb321 commented 3 years ago

Hi,

I'm having a problem with selecting glyphs to load — the procedure takes glyph ranges, but no range provided by getGlyphRangesFor*() is suitable for me and I do not see a way to create them through the API. Is it possible?

ice1000 commented 3 years ago

Hi,

I'm having a problem with selecting glyphs to load — the procedure takes glyph ranges, but no range provided by getGlyphRangesFor*() is suitable for me and I do not see a way to create them through the API. Is it possible?

It is, but I haven't yet adapted the API for custom ranges. It's hard in general -- imagine you passes a two-dim array from Java to C++. There are some design choices to be made.

ice1000 commented 3 years ago

Btw, I suggest opening new issue. @seb321