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

Author Topic: Invisible vertical padding on text  (Read 3197 times)

0 Members and 1 Guest are viewing this topic.

unixorn

  • Newbie
  • *
  • Posts: 4
    • View Profile
Invisible vertical padding on text
« on: May 15, 2021, 08:57:01 pm »
So there I was trying to display a snippet of text XD
Unfortunately, there seems to be some "invisible" padding that does not display the text at the requested position (0,0).
The text seems to be shifted downwards like maybe 10px? (Even though it says its position is what I made it)
The font is mono spaced.
Here is some code that reproduces the "weird" behavior :)

    sf::Text text;
    text.setFont(Resources::getGameFont());
    text.setCharacterSize(50);
    text.setString("Hand");
    text.setPosition(0,0);
    text.setFillColor(sf::Color::Black);
    while (window.isOpen())
    {
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
                window.close();
        window.clear(sf::Color::Yellow);
        window.draw(text);
        window.display();
    }

From what I understand, the default origin of text objects is their top left corner?
I've also tried 3 other fonts and they all seem to have the same problem :(

I've linked an image that shows this strange behavior on one of the fonts :)

I edited the code a little (I inserted the following just before the while loop):
    std::cout << "gb_box.left = " << text.getGlobalBounds().left << std::endl;
    std::cout << "gb_box.top = " << text.getGlobalBounds().left << std::endl;
    std::cout << "lb_box.left = " << text.getLocalBounds().left << std::endl;
    std::cout << "lb_box.top = " << text.getLocalBounds().top << std::endl;

And I got:
gb_box.left = 1
gb_box.top = 1
lb_box.left = 1
lb_box.top = 19

Is this normal? I mean, shouldn't the 'left' and 'top' members be equivalent to the 'x' and 'y' positions of the text?

Okay, after playing around a little, I found a somewhat funny "solution" :)
The following snippet was also inserted right before the main while loop (or in other words, after the last insertion).

    while (text.getGlobalBounds().left != 0)
        text.move(-1,0);
    std::cout << "text_pos.x = " << text.getPosition().x << std::endl;
    while (text.getGlobalBounds().top != 0)
        text.move(0,-1);
    std::cout << "text_pos.y = " << text.getPosition().y << std::endl;

The interesting thing is the output though :)
gb_box.left = 1
gb_box.top = 1
lb_box.left = 1
lb_box.top = 19
text_pos.x = -1
text_pos.y = -19

I'm just really curious whether this is supposed to happen XD or if I'm doing something wrong.
So I learned that the position of text is not always equivalent to the position of its bounding box, which was a tiny bit of a shock to my eyes XD.
So I guess, It's good to know that if I want to set the position of text to (x,y), what I'm really trying to do is set the position of the bounding box of that text to (x,y).
« Last Edit: May 17, 2021, 10:33:56 am by eXpl0it3r »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Invisible vertical padding on text
« Reply #1 on: May 16, 2021, 01:14:29 pm »
Hi. i think this is the most asked question ever in this forum  :P
you need to use the text's 'top' variable. yes, it is 'normal'. i don't remember the exact explanation, but it has something to do with text alignment in the font itself. have a look:

https://en.sfml-dev.org/forums/index.php?topic=27840.msg175189#msg175189
https://en.sfml-dev.org/forums/index.php?topic=24985.msg166441#msg166441
https://en.sfml-dev.org/forums/index.php?topic=19004.msg136987#msg136987
« Last Edit: May 16, 2021, 01:17:12 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

kojack

  • Sr. Member
  • ****
  • Posts: 310
  • C++/C# game dev teacher.
    • View Profile
Re: Invisible vertical padding on text
« Reply #2 on: May 16, 2021, 01:18:45 pm »
    std::cout << "gb_box.left = " << text.getGlobalBounds().left << std::endl;
    std::cout << "gb_box.top = " << text.getGlobalBounds().left << std::endl;
    std::cout << "lb_box.left = " << text.getLocalBounds().left << std::endl;
    std::cout << "lb_box.top = " << text.getLocalBounds().top << std::endl;
The second line is printing the global bounds left again as the top value.

unixorn

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Invisible vertical padding on text
« Reply #3 on: May 16, 2021, 03:25:40 pm »
Hi. i think this is the most asked question ever in this forum  :P
you need to use the text's 'top' variable. yes, it is 'normal'. i don't remember the exact explanation, but it has something to do with text alignment in the font itself. have a look:

https://en.sfml-dev.org/forums/index.php?topic=27840.msg175189#msg175189
https://en.sfml-dev.org/forums/index.php?topic=24985.msg166441#msg166441
https://en.sfml-dev.org/forums/index.php?topic=19004.msg136987#msg136987
Yes, thank you! XD

sf::Text, unlike other SFML entities, is aligned on its baseline, not on its top. I'm pretty sure it is explained somewhere in the documentation. So no, you can't expect "top" to be always zero for this class.

Quote
It actually behaves funny with width when there are leading or trailing spaces. Laurent?
How exactly does it behave in this case?

I don't know how I missed that XD.
Gotta read the docs more carefully :)
Cheers!