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

Unable to render character symbols #565

Closed
anotherzooanimal opened this issue Mar 27, 2016 · 5 comments
Closed

Unable to render character symbols #565

anotherzooanimal opened this issue Mar 27, 2016 · 5 comments

Comments

@anotherzooanimal
Copy link

When attempting to render character symbols it seems to fail rendering. For example, lets say I were to attempt to render the trademark symbol:

/* This should render a ™ */
U+2122 (0x99): Trade Mark Sign
ImGui::Text("\u2122");

When I am loading my font, I am using:
arial16 = io.Fonts->AddFontFromFileTTF("C:\windows\fonts\arial.ttf", 16.0f, &config);

And the results are that nothing displays. If I were to change this to something like:
ImGui::Text("a");

Everything renders fine.

Is there something that I am missing for using a character like this?

@ocornut
Copy link
Owner

ocornut commented Mar 27, 2016

Hello,

1/
Fonts glyphs currently needs to be baked ahead, at the time of creating the texture atlas. You need to declare non-ascii characters in the ranges[] array passed to AddFont.

2/
Text encoding needs to be utf-8. I don't need know what "\u2122" encodes to, it might already be utf-8? I think modern c++ wants u8"\u2122" for utf-8 literals.

@Flix01
Copy link

Flix01 commented Mar 27, 2016

Sorry to interfere with this thread...

2/ Text encoding needs to be utf-8. I don't need know what "\u2122" encodes to, it might already be utf-8? I think modern c++ wants u8"\u2122" for utf-8 literals.

That's correct: 2122 is the hex code of the UTF8 character.
I'm on Linux, but if inside this post I keep SHIFT+CTRL pressed and in the meantime I type u2122 using the number pad on the right of the keyboard , I can see: ™.

For the answer, specifying the ranges this way works for me:

static const ImWchar ranges[] =
{
0x0020, 0x00FF, // Basic Latin + Latin Supplement
0x20AC, 0x20AC, // €
0x2122, 0x2122, // ™
0x2196, 0x2196, // ↖
0x21D6, 0x21D6, // ⇖
0x2B01, 0x2B01, // ⬁
0x2B09, 0x2B09, // ⬉
0x2921, 0x2922, // ⤡ ⤢
0x263A, 0x263A, // ☺
0x266A, 0x266A, // ♪
0,
};`

P.S. Here are some UTF8 glyphs that I find useful (but not all ttf files have all of them):

∆ // u0394
… // u2023
€ // u20AC
« // u00AB
» // u00BB
U+266A: ♪
U+2122: ™
U+263A: ☺
U+2328: ⌨
U+23CF: ⏏
U+25B2-U+25B5: ▲ △ ▴ ▵
U+25BC-U+25BF: ▼ ▽ ▾ ▿
U+25B6-U+25BB: ▶ ▷ ▸ ▹ ► ▻
U+25C0-U+25C5: ◀ ◁ ◂ ◃ ◄ ◅
U+25A0-25A3: ■ □ ▢ ▣

There are probably many more... it should be handy to have a complete list of the glyphs that could be useful inside ImGui.

@ocornut
Copy link
Owner

ocornut commented Mar 27, 2016

That's correct: 2122 is the hex code of the UTF8 character.

None of the value you are listing are UTF-8 representation of those characters. They are all Unicode codepoints not UTF-8 representation.

UTF-8 for U2122 is 0xE2 0x84 0xA2
http://www.fileformat.info/info/unicode/char/2122/index.htm

If you use "\u2122" in C++ as a literal you'll get weird shit but not an UTF-8 conversion.
C++11 has a new syntax for UTF-8 literral u8"\u2122" would work

@Flix01
Copy link

Flix01 commented Mar 27, 2016

Yes, you're right. u2122 seems to be UTF16, according to the link you posted!
And I was trying to fetch the printf docs in the C++ reference to see if there's a way to display a single codepoint by value, but it doesn't seem to be possible AFAICU (at least without C++11).

However, @anotherzooanimal, I suggest you use the encoded glyphs directly, e.g.:

ImGui::Text("This is the € glyph.");
unless this is not possible (for example if some very old compiler can handle only ASCII .cpp files).

@anotherzooanimal
Copy link
Author

Thank you so much for the help. You are correct about using the HEX representation Ocornut, that along with using the suggestion that Flix01 made about setting the range corrected the issue.

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

No branches or pull requests

3 participants