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

Author Topic: Get the x-height of a sf::Text  (Read 3800 times)

0 Members and 1 Guest are viewing this topic.

Max

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Get the x-height of a sf::Text
« on: May 03, 2017, 10:41:05 pm »
In my current project I noticed that some of my buttons had their text at different heights. It took awhile to figure out what the problem was. I had used sf::Text getLocalBounds().height
The height returned was its current bounds for that particular text. So I made my self a little helper function. It may not be the best solution but it at least it works (I think).

int Button::GetXHeight()
{
        // Info about x-height: https://en.wikipedia.org/wiki/X-height
        text.setString("a");
        int xheight = (int)this->text.getLocalBounds().height;
        text.setString(buttonText);
        return xheight;
}

Note: must used before using text dimensions.

By setting the text to temporally to "a" then query the getLocalBounds().height for its x-height. Then set the text back.

I am wondering if this could be a feature to be added. Lets say: getXHeight() perhaps?
That said, I am not an expert so I might have overlooked something essential.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Get the x-height of a sf::Text
« Reply #1 on: May 03, 2017, 10:45:57 pm »
Have I missed something or could you not just make sure everything is set to the same character size and scale to show the same size?

I find it amusing that you chose to find the x height using the letter a instead of x. ;D

I'm not sure if this could be technically implemented globally, mainly because not all fonts have an x.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Max

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Get the x-height of a sf::Text
« Reply #2 on: May 03, 2017, 11:24:55 pm »
Yes if I make the text upper cased that would probably work. If it is not feasible to implement, I would understand.
I have made an image showing what the problem was.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Get the x-height of a sf::Text
« Reply #3 on: May 03, 2017, 11:27:41 pm »
Ah. This is not a size problem; it is a position problem.
Consider ignoring the text height. Texts' baselines should then line up automatically.

p.s. if you want the centre of most texts, consider the centre between the top and bottom of a string such as "Ay". Even then, you only need the value once; it is the same for all texts.
« Last Edit: May 03, 2017, 11:30:38 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Max

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Get the x-height of a sf::Text
« Reply #4 on: May 03, 2017, 11:36:00 pm »
Ahh!! Why didn't I think of that  ;D
Thanks.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Get the x-height of a sf::Text
« Reply #5 on: May 03, 2017, 11:41:27 pm »
You are welcome! :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*