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

Author Topic: Failed to load image with right quote  (Read 3509 times)

0 Members and 2 Guests are viewing this topic.

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Failed to load image with right quote
« on: September 25, 2016, 09:38:46 pm »
Hello,

I'm facing some weird behaviour when trying to load some files. I'm working in a french windows environment, and when i press the windows + print screen buttons it takes a screenshot and save it as "Capture d’écran (4).png"

Then when I try to load it using the loadFromFile method of the sf::Texture class, it fails. After many test, it seems that the problem is coming from the ’ character, which is different from the usual ' (quote character). And when i replace this character with any other (in the loading code AND in the image name of course), I successfully manage to load the texture.

Is this some kind of bug or am i doing something bad ?

Here is the minimal example code to reproduce my problem :

#include <string>
#include <SFML\Graphics.hpp>

int main(int argc, char* argv[])
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!");
       
        std::wstring path(L"Capture d’écran.png"); //DOES NOT WORK
        //std::wstring path(L"Capture d'écran.png"); //WORKS
        sf::String sfPath(path);

        sf::Texture texture;
        texture.loadFromFile(sfPath);
        //texture.loadFromFile("Capture d’écran.png"); //WORKS
        sf::Sprite sprite(texture);

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

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

        return 0;
}

EDIT: in case you are wondering, I HAVE TO use the std::wstring intermediary in my context (converting QString to sf::String)

Thank you
« Last Edit: September 25, 2016, 09:44:41 pm by Yohdu »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Failed to load image with right quote
« Reply #1 on: September 26, 2016, 01:29:24 pm »
Might be related to https://github.com/SFML/SFML/issues/647

EDIT: in case you are wondering, I HAVE TO use the std::wstring intermediary in my context (converting QString to sf::String)
Is it though? QString offers a lot of conversion options, e.g. data() returns a UTF-16 byte array which you can load into an sf::String.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Failed to load image with right quote
« Reply #2 on: September 26, 2016, 02:14:26 pm »
Quote
Might be related to https://github.com/SFML/SFML/issues/647
Indeed it seems related, thank you for the link.

EDIT: Actually, I don't know. My code works well with characters like éèàë or even the classic apostroph character. The problematic character is the french apostroph (and I assume there must be other similar problematic characters). I really don't know what to think.

Quote
QString offers a lot of conversion options, e.g. data() returns a UTF-16 byte array which you can load into an sf::String
Can you show me the piece of code that performs this operation ? I'm not quite sure how to manipulate the data() method  with the sf::String class.

EDIT 2: I tried with other QString conversion operations (tostd16 / to std32), it indeed works as to stdwstring, but I still have the problem that the french apostroph character is not recognized properly.
« Last Edit: September 26, 2016, 02:57:26 pm by Yohdu »

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Failed to load image with right quote
« Reply #3 on: September 29, 2016, 09:08:16 am »
Sorry to up this but I really need help on this one.

I've tried about anything I could imagine, and I do not manage to properly convert a QString to a sf::String in order to load a file with SFML. A lot of special characters are well-handled (éèàë etc.), but this character    ’    is not, and I don't know why.

I'm summoning you, Ô honorable SFML experts  ;D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Failed to load image with right quote
« Reply #4 on: September 29, 2016, 09:12:24 am »
Btw. why don't you just rename the file?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Failed to load image with right quote
« Reply #5 on: September 29, 2016, 11:48:43 am »
Because I'm implementing a environment where the user can load any image file (using Qt) ; I could indeed warn him that the loaded images should not contain any weird characters, but meh I'm not really satisfied with this solution, he should be able to load any file which name has been approved by the OS he uses.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Failed to load image with right quote
« Reply #6 on: October 01, 2016, 04:07:42 pm »
This code works fine.

#include <SFML/System.hpp>
#include <QString>

int main()
{
    QString qstring = QString::fromStdWString(L"’");
    auto u32string = qstring.toStdU32String();
    sf::String sfstring = sf::String::fromUtf32(u32string.begin(), u32string.end());

    auto c = sfstring[0]; // c == 8217 which is the correct UTF-32 value of the character

    return 0;
}

This is for the conversion. I didn't test loading an image.
Laurent Gomila - SFML developer

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Failed to load image with right quote
« Reply #7 on: October 01, 2016, 09:07:14 pm »
I actually already tried this solution, but loading the image using it fails. Still, thank you Laurent.

Note that the image is properly loaded by Qt using QPixmap.

I guess the problem is not coming from the convertion operation, but from the loading step. Could it be possible that the loadfromfile method is not properly handling sf::String containing some UTF-32 characters (or this particular ’ character)?

I also need to try my code using another compiler (like MSVC), even though it would be weird that the problem comes from the compiler... Still open to suggestions :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Failed to load image with right quote
« Reply #8 on: October 01, 2016, 10:59:30 pm »
Quote
Could it be possible that the loadfromfile method is not properly handling sf::String containing some UTF-32 characters (or this particular ’ character)?
Yes. Some time ago we changed all filename strings to be sf::String, so that in the future we can handle Unicode correctly, but it doesn't mean that all functions use them properly. It usually depends on the underlying library (stb_image in case of image loading), and it is even possible that Unicode filenames cannot be implemented because of the library's limtations.
Laurent Gomila - SFML developer

Yohdu

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Failed to load image with right quote
« Reply #9 on: October 03, 2016, 10:31:47 am »
OK then :) Thank you again.
If I ever manage to make it work and/or simply have new informations, I'll share it with you guys.

 

anything