SFML community forums

Help => System => Topic started by: Matticus on June 11, 2012, 02:54:16 am

Title: Unable to display Japanese characters
Post by: Matticus on June 11, 2012, 02:54:16 am
Hello, I've been trying to get Japanese characters to render in SFML 2 RC to no avail.  I've used the font from  this thread (http://en.sfml-dev.org/forums/index.php?topic=5927.0) so that I know the font is compatible. Direct link for convenience (http://www.fonts2u.com/m-1p-medium.font).

The Japanese text comes up as question marks while English text displays normally.  Sample code is below, thank you for reading:

#include <SFML/Graphics.hpp>
#include <SFML/System/Utf.hpp>
#include <iostream>

int main()
{
        sf::Font font;
        sf::String msg, hdr;
        sf::Text text;

        if(!font.loadFromFile("test.ttf"))
        {
                text.setString("Load failed"); 
                hdr = "Warning!";
        }
        else
        {
                font.loadFromFile("test.ttf");
                //char jp[2] = { '\u70B9','\u83DC' }; //error even worse here
                //msg = jp;
                msg = "&#12371;&#12435;&#12395;&#12385;&#12399;SMFL"; //tested this first, Japanese characters come up as "??????SMFL"
                hdr = "&#12371;&#12435;&#12395;&#12385;&#12399;SMFL";
       
                //Set up for potential conversion from different utf types
                /*sf::Utf<8>::toUtf32(msg.begin(), msg.end(), msg.begin());
                sf::Utf<8>::toUtf32(hdr.begin(), hdr.end(), hdr.begin());*/


                text.setFont(font);
                text.setString(msg);
        }

        sf::RenderWindow window(sf::VideoMode(300, 200), hdr);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}
Title: Re: Unable to display Japanese characters
Post by: Laurent on June 11, 2012, 08:05:16 am
You can't define Unicode characters in a "regular" string literal (made of 8-bit chars), you need at least a wide string literal.
msg = L"&#38;#12371;&#38;#12435;&#38;#12395;&#38;#12385;&#38;#12399;SMFL";
hdr = L"&#38;#12371;&#38;#12435;&#38;#12395;&#38;#12385;&#38;#12399;SMFL";

And by the way, it is SFML.
Title: Re: Unable to display Japanese characters
Post by: Matticus on June 11, 2012, 01:14:07 pm
You can't define Unicode characters in a "regular" string literal (made of 8-bit chars), you need at least a wide string literal.
msg = L"&#38;#12371;&#38;#12435;&#38;#12395;&#38;#12385;&#38;#12399;SMFL";
hdr = L"&#38;#12371;&#38;#12435;&#38;#12395;&#38;#12385;&#38;#12399;SMFL";

That was the solution, thank you for your help!

And by the way, it is SFML.

Ah, sorry.  Slip of the index ._.
Title: Re: Unable to display Japanese characters
Post by: Silvah on June 11, 2012, 02:44:11 pm
You can't define Unicode characters in a "regular" string literal (made of 8-bit chars), you need at least a wide string literal.
And the reason is…?
Title: Re: Unable to display Japanese characters
Post by: pdinklag on June 21, 2012, 09:24:38 am
You can't define Unicode characters in a "regular" string literal (made of 8-bit chars), you need at least a wide string literal.
And the reason is…?
16 / 32 bit characters hardly fit into 8 bits.
Title: Re: Unable to display Japanese characters
Post by: Laurent on June 21, 2012, 09:31:27 am
Quote
16 / 32 bit characters hardly fit into 8 bits.
His question was just a hidden way to say "it's wrong, you can use UTF-8" ;)
But the corresopnding code would probably be more (too?) complicated.
Title: Re: Unable to display Japanese characters
Post by: MorleyDev on June 24, 2012, 08:20:19 pm
FYI msg = jp will fail because you haven't null terminated the jp string of characters. It'd fail even without the unicode :P

It's a shame SFML doesn't support the u8/u/U string literals....(yet?), although I experimented and you can still put \uXXXX into wide strings so there is that.
Title: Re: Unable to display Japanese characters
Post by: Laurent on June 24, 2012, 10:48:58 pm
Quote
It's a shame SFML doesn't support the u8/u/U string literals....(yet?)
A shame? Dude, the new standard was released not even a year ago, let SFML catch up ;)
https://github.com/SFML/SFML/issues/129#issuecomment-6536223
Title: Re: Unable to display Japanese characters
Post by: MorleyDev on June 24, 2012, 11:56:48 pm
Quote
It's a shame SFML doesn't support the u8/u/U string literals....(yet?)
A shame? Dude, the new standard was released not even a year ago, let SFML catch up ;)
https://github.com/SFML/SFML/issues/129#issuecomment-6536223

Just a turn of phrase ^^ Not shame xD Pity? Cause of sorrow? Understandable absence? Really the shame is it took this long for Unicode support in C++ to be standardised -_-