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

Author Topic: Width of a space in sf::Text  (Read 2743 times)

0 Members and 1 Guest are viewing this topic.

nietaki

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • http://almost-done.net
Width of a space in sf::Text
« on: March 18, 2012, 11:03:07 pm »
I am finishing my TextArea widget and to make things easier for me to implement quickly I treat each word in my TextArea as a separate object. I know the space I should leave between the lines thanks to the sf::Font::getLineSpacing(characterSize) and I figured out I could get the space I should leave between the words just by getting the width of the "space" Glyph. But...

Code: [Select]
 float space_size = 1.0f * font.getGlyph(0x20, characterSize, false).bounds.width;
  float bold_space_size = 1.0f * font.getGlyph(0x20, characterSize, true).bounds.width;

  std::cout << "spaces " <<  space_size << " " << bold_space_size <<std::endl;


...it turns out the space character (0x20) has width of a zero.

Is there a good way of getting the width of a space I should leave between two words? Apart from imperfect ideas such as "getting the width of an 'a' glyph" or "getting width of of two words separated with a space and subtracting the sum of the lengths of the individual words"?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Width of a space in sf::Text
« Reply #1 on: March 19, 2012, 08:13:33 am »
The "bounds" member of a glyph is its visual bounding rectangle, so it's normal that it's empty for spaces. You must use the "advance" member instead.
Laurent Gomila - SFML developer

nietaki

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • http://almost-done.net
Width of a space in sf::Text
« Reply #2 on: March 19, 2012, 10:44:05 am »
...and I missed that in a class with 3 members :evil:

Thanks, Laurent!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Width of a space in sf::Text
« Reply #3 on: March 19, 2012, 10:49:25 am »
You should have looked at the source code of sf::Text ;)
Laurent Gomila - SFML developer

nietaki

  • Newbie
  • *
  • Posts: 30
    • View Profile
    • http://almost-done.net
Width of a space in sf::Text
« Reply #4 on: March 19, 2012, 02:35:27 pm »
Yeah, I guess it's a clear sign I should stop treating the libraries I use so intensely like a black box ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Width of a space in sf::Text
« Reply #5 on: March 19, 2012, 02:36:42 pm »
Especially SFML, which has a small, simple and well-written code :mrgreen:
Laurent Gomila - SFML developer

 

anything