Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Unable to display Japanese characters  (Read 5893 times)

0 Members and 1 Guest are viewing this topic.

Matticus

  • Newbie
  • *
  • Posts: 2
    • View Profile
Unable to display Japanese characters
« 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 so that I know the font is compatible. Direct link for convenience.

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;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Unable to display Japanese characters
« Reply #1 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.
Laurent Gomila - SFML developer

Matticus

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Unable to display Japanese characters
« Reply #2 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 ._.

Silvah

  • Guest
Re: Unable to display Japanese characters
« Reply #3 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…?

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Unable to display Japanese characters
« Reply #4 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.
JSFML - The Java binding to SFML.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Unable to display Japanese characters
« Reply #5 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.
Laurent Gomila - SFML developer

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Unable to display Japanese characters
« Reply #6 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.
« Last Edit: June 24, 2012, 08:45:42 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Unable to display Japanese characters
« Reply #7 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
Laurent Gomila - SFML developer

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Unable to display Japanese characters
« Reply #8 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 -_-
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.