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

Author Topic: [Linux] UTF-8 on window titlebar (UTF-8 encoded file)  (Read 6331 times)

0 Members and 1 Guest are viewing this topic.

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« on: February 07, 2011, 01:11:48 am »
Hi there.

I've just started using SFML and one of the first problems I've come across is some weird characters on the the titlebar whenever I try to use accents or any other extended char.

For instance, I've got:
Code: [Select]
   sf::RenderWindow Ventana(sf::VideoMode(800, 600, 32), "Año nuevóóó");

And the titlebar renders like: "AÂ+o nuevoA³A³A³".

This ONLY HAPPENS if my source code file is enconded in UTF-8. If I change encoding to ISO-8859-1, it shows properly. Obviously all of my files use UTF-8, as its the system-wide encoding.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #1 on: February 07, 2011, 07:45:32 am »
The string that you pass is decoded with the current locale, so if your source file encoding doesn't match you get this kind of weird result.

You can convert your string to the current locale's encoding with the sf::Utf8::ToAnsi function.
Laurent Gomila - SFML developer

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #2 on: February 07, 2011, 08:35:13 am »
Thanks. How would I do it under SFML 1.6? Looks like sf::UTF8 is only part of 2.0.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #3 on: February 07, 2011, 09:19:42 am »
Oops, sorry. With SFML 1.6 you can use stuff that is in sf::Unicode.
Laurent Gomila - SFML developer

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #4 on: February 07, 2011, 12:32:24 pm »
Looks like there's no UTF8 to Ansi in 1.6 :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #5 on: February 07, 2011, 01:28:05 pm »
Ah, you're right. You'll have to convert to UTF-32 first. You can also use sf::Unicode::Text, which allows a simpler syntax and can deduce the source encoding from the characters type.
Laurent Gomila - SFML developer

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #6 on: February 14, 2011, 02:17:11 am »
I haven't had any luck with this, and tried everything:

Code: [Select]
#include <SFML/Graphics.hpp>

int main(int argc, char *argv[])
{
    sf::RenderWindow MyWindow;

    // MyWindow.Create(sf::VideoMode(800, 600), "ááá"); // Appears "Ä¡Ä¡Ä¡

    // sf::Unicode::Text Titlebar("ááá");
    // MyWindow.Create(sf::VideoMode(800, 600), Titlebar); // Blank title

    // sf::Unicode::Text Titlebar(L"ááá"); // Note the L
    // MyWindow.Create(sf::VideoMode(800, 600), Titlebar); // Blank title as well

    std::string caption = "ááá", destAnsi;
    sf::Unicode::UTF32String cap32;
    sf::Unicode::UTF8ToUTF32(caption.begin(), caption.end(), cap32.begin());
    sf::Unicode::UTF32ToANSI(cap32.begin(), cap32.end(), destAnsi.begin()); // Gets frozen!!
    MyWindow.Create(sf::VideoMode(800, 600), destAnsi);

    while(MyWindow.IsOpened()){
sf::Event Event;
while(MyWindow.GetEvent(Event)){
   if(Event.Type == sf::Event::Closed){
MyWindow.Close();
   }
}

MyWindow.Clear();
MyWindow.Display();
    }

    return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #7 on: February 14, 2011, 07:39:56 am »
You have to allocate space for your result strings, the functions will just write to the iterator you pass them, assuming they're pointing to valid memory. No memory allocation is made by the SFML functions.

Code: [Select]
sf::Unicode::UTF32String cap32;
cap32.resize(caption.size());
sf::Unicode::UTF32String::iterator end = sf::Unicode::UTF8ToUTF32(caption.begin(), caption.end(), cap32.begin());
cap32.erase(end, cap32.end());

Written from scratch without compiling/testing, should work.
Do the same for the UTF-32 -> ANSI conversion.
Laurent Gomila - SFML developer

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #8 on: February 14, 2011, 04:18:44 pm »
This is really weird... after the first resize, it gets frozen when the first conversion starts:

Code: [Select]
string caption = "ááá";
sf::Unicode::UTF32String cap32;
cap32.resize(caption.size());
cout << "1st resize" << endl;
sf::Unicode::UTF32String::iterator end = sf::Unicode::UTF8ToUTF32(caption.begin(), caption.end(), cap32.begin());
cout << "1st conversion" << endl;
cap32.erase(end, cap32.end());
cout << "1st erase" << endl;

string final;
final.resize(cap32.size());
cout << "2nd resize" << endl;
string::iterator endA = sf::Unicode::UTF32ToANSI(cap32.begin(), cap32.end(), final.begin());
cout << "2nd conversion" << endl;
final.erase(endA, final.end());
cout << "2nd erase" << endl;


That only prints "1st resize" and there it stays forever.

By the way, if I remove all the non-ascii chracters from the string caption, for instance "string caption = "aaa", it works (but hey, I want those accents back xD)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #9 on: February 14, 2011, 04:38:25 pm »
I don't know how UTF-8 and std::string mix together (I don't even know if they can), so I'm not sure what's wrong here.

What does caption.size() returns?
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #10 on: February 14, 2011, 04:39:36 pm »
Another option, if you don't want to bother with the size:
Code: [Select]
string caption = "ááá";
sf::Unicode::UTF32String cap32;
sf::Unicode::UTF8ToUTF32(caption.begin(), caption.end(), std::back_inserter<char>(cap32));
Laurent Gomila - SFML developer

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #11 on: February 14, 2011, 05:45:45 pm »
caption.size() returns 6, because the character "á" is represented using two bytes.

With the back inserter code it freezes too.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #12 on: February 14, 2011, 07:15:20 pm »
Ok, the code is definitely broken here (it's already fixed in SFML 2).

It works if you use unsigned char instead of char for your UTF-8 string.
Laurent Gomila - SFML developer

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #13 on: February 14, 2011, 08:58:54 pm »
I'll wait for V.2 to get to the Ubuntu repositories then  :lol:

TheOm3ga

  • Newbie
  • *
  • Posts: 15
    • View Profile
[Linux] UTF-8 on window titlebar (UTF-8 encoded file)
« Reply #14 on: February 14, 2011, 09:24:48 pm »
However, I've downloaded and compiled the SVN Snapshot of SFML 2 and the problem remains... I don't know :(