Embedding unicode string literals in your cpp files


#21

...maybe I'm struck with blindness, but when I put

g.setFont (Font ("MusicalSymbols", 32.0f, Font::plain));
g.setColour (Colours::white);
const juce_wchar myUnicodeString[] = { 0x00D11E, 0x000000, 0 }; // 1D11E
String s (myUnicodeString);
g.drawText (s, getLocalBounds(), Justification::centred, true);

into the Hello World, I don't see anything. And this is with the standard font and with some other free musical fonts I downloaded...

I also tried 0x001234, 0x005678 and several other combinations... Sorry, probably I think too complicated?


#22

That says 0xd11e, not 0x1d11e (?)

I've no idea about the unicode chars you actually need or which ones are in your font, but this is just showing you how to turn extended unicode chars into a string.


#23

..ok, typo in the example, from trying several combinations. But that didn't help. The symbols I am after are defined in unicode standard Rev. 8, as described in the link I posted before: http://www.unicode.org/charts/PDF/U1D100.pdf

What confuses me also is, that your example had three bytes. I remember that UTF32 is always multibyte, so that was the moment I was insecure. At least I know now, that it "should" print something.

Thanks for your time.


#24

I tried to write a geometric shape symbol inside a textbutton and didn't work as it worked when I used it before.

Why isn't this method working anymore ?

 

Edit : It works but not when I choose "Arial Unicode MS" font type.


#25

Did it use to work before with that specific font type?


#26

Yes, it worked well.

Anyway, now I'm using " Segoe UI Symbol " font type and it has a huge variety of symbols if anyone else needs it. ;)


#27

It does not work for me.


#28

I don’t think current versions of Windows have a Arial Unicode MS font, so you would use “Arial”.

Anyway it’s generally not possible to display arbitrary unicode in Juce, because (1) it uses a very outdated way to render text, and (2) the design of the look-and-feel system makes it hard to globally override the default font in widgets.


#29

It also does not work with “Arial”.
Windows 10 have a Arial Unicode MS font, but juce seems not to supoort it.


#30

Hi Folks,

Thought I’d pitch in on this old thread, as I’ve been unpicking all the UTF-8 / font issues for myself while localising Wotja.

To compile for Windows, such that the Source Code is compiled as UTF-8, simply define this compiler flag:
/utf-8

You can then do stuff like this (provided you’re compiling for C++17 or later).
auto myString = juce::String::fromUTF8(u8"放松");

That is Simplified Chinese, BTW.

This code works 100% fine for iOS, macOS, Windows and Android.

To force use of the correct font, implement something like this in your LookAndFeel class.
Sorry about the layout :slight_smile:

This works for all of iOS, macOS, Windows and Android (at least for me, anyhow!).
You’d have to expand it to handle other locales/fonts.

virtual juce::Typeface::Ptr getTypefaceForFont (const juce::Font& font) override {

  #if defined(IM_TARGET_IOS)
  juce::String locale =  juce::SystemStats::getDisplayLanguage();

  if (locale.startsWith("zh")) {
    juce::Font f(font);
    f.setTypefaceName("PingFang SC");
    return juce::Typeface::createSystemTypefaceFor(f);
  }

  #elif defined(IM_TARGET_MACOS)

  juce::String locale =  juce::SystemStats::getDisplayLanguage();

  if (locale.startsWith("zh")) {
    juce::Font f(font);
    f.setTypefaceName("PingFang SC");
    return juce::Typeface::createSystemTypefaceFor(f);
  }

  #elif defined(IM_TARGET_WINDOWS)

  // See https://forum.juce.com/t/font-woes-again-chinese/23832

  juce::String locale =  juce::SystemStats::getDisplayLanguage();

  if (locale.startsWith("zh")) {
    juce::Font f(font);
    f.setTypefaceName("Microsoft YaHei");
    return juce::Typeface::createSystemTypefaceFor(f);
  }
  #endif //

  return lookAndFeel.juce::LookAndFeel_V3::getTypefaceForFont(font);
}

Pete


Font woes again (chinese)
#31

Thanks Pete!

The other important thing for people to note is that they also need to make sure their text editor is saving the file as UTF8. There’s not much point telling the compiler to treat it as UTF8 if the editor actually dumped it as some other codepage format!

(And you can make your code readable by surrounding it with triple-backticks or just indenting it all be 4 spaces)


#32

Thanks Jules - I’ve just tidied-up my example code. I’d suggest you maybe add some or all of the above to the code generated by the Introjucer! :wink:

Pete


#33

Still this code is not working if you display Chinese characters on an English system…