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

Author Topic: sf::Text overflow?  (Read 4458 times)

0 Members and 1 Guest are viewing this topic.

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
sf::Text overflow?
« on: June 12, 2015, 10:30:41 am »
Hello,

it seems that my sf::Text instance won't manage to put its string within its borders. I put its origin at half its width and half its height, then set its position. I also created four 1x1 red dots and set their positions to each (supposed) corner of the rectangle of the text. This way I can visualize the problem more easily. I also created a fifth 1x1 red dot to show where the center/origin of my text is supposed to be.

(click to show/hide)


As you can see, the text is displaced by 1 pixel on the right, and by 7 pixels downwards. I checked everything in my code and nothing explains it. So my question is, is there an option somewhere to "make sure" that the text remains within its borders, or something?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text overflow?
« Reply #1 on: June 12, 2015, 10:34:21 am »
sf::Text bounds have a top and left coordinates which are not zero.
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: sf::Text overflow?
« Reply #2 on: June 12, 2015, 10:49:02 am »
sf::Text bounds have a top and left coordinates which are not zero.
How can I set them to 0? Out of curiosity, why is that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text overflow?
« Reply #3 on: June 12, 2015, 10:54:04 am »
You can't set them, but you have to take them in account in your calculations.

They are not zero because text is not aligned on its top, like other SFML entites are. Text is aligned on its baseline, therefore the top coordinate depends on the maximum height of the first line's characters. If you put an accentuated capital letter, for example, top will most likely be zero.
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: sf::Text overflow?
« Reply #4 on: June 12, 2015, 11:01:12 am »
It's strange though because I've had the size of the font decrease until the height of the sf::Text becomes less than some threshold, and eventually it's still greater than that threshold.

Fortunately the sf::Texts I'm working on only use numbers, so that shouldn't be too much of a headache to figure out. Would you have any tips on how to determine the vertical spacing for digits?

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: sf::Text overflow?
« Reply #5 on: June 12, 2015, 03:46:04 pm »
None? I'm basically asking how to deduce the vertical spacing digits have, based on the size of the font.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Text overflow?
« Reply #6 on: June 13, 2015, 04:12:01 am »
how to deduce the vertical spacing digits have
Spacing? The spacing between each line?

You say your text is displaced by (1, 7). My bet is on the text having left and top values of 1 and 7. Have you tried to use the information Laurent has given you? (it's pretty much the answer)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: sf::Text overflow?
« Reply #7 on: June 13, 2015, 03:44:10 pm »
I'm not fond of putting magic numbers in my code. The thing is, I am about to generate different sf::Text instances of different sizes, so it'd be much simpler if I could somehow deduce the vertical spacing between the digits of the first (and only) line and the top.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text overflow?
« Reply #8 on: June 13, 2015, 04:50:47 pm »
Quote
I'm not fond of putting magic numbers in my code. The thing is, I am about to generate different sf::Text instances of different sizes, so it'd be much simpler if I could somehow deduce the vertical spacing between the digits of the first (and only) line and the top.
text.getLocalBounds().top. That's basically what we are trying to tell you in this whole thread :P
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: sf::Text overflow?
« Reply #9 on: June 14, 2015, 01:01:55 pm »
How interesting, I didn't realize that. So the "true" size of a sf::Text is actually (width + left, height + top) ?

That might not be instinctive, but that does solve my problem.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Text overflow?
« Reply #10 on: June 14, 2015, 03:21:26 pm »
No, the true size is (width, height). But it starts at (left, top), not (0, 0).
Laurent Gomila - SFML developer

Law

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: sf::Text overflow?
« Reply #11 on: June 14, 2015, 08:06:27 pm »
Thanks a lot !