SFML community forums

Help => Graphics => Topic started by: nietaki on March 18, 2012, 11:03:07 pm

Title: Width of a space in sf::Text
Post by: nietaki 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"?
Title: Width of a space in sf::Text
Post by: Laurent 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.
Title: Width of a space in sf::Text
Post by: nietaki on March 19, 2012, 10:44:05 am
...and I missed that in a class with 3 members :evil:

Thanks, Laurent!
Title: Width of a space in sf::Text
Post by: Laurent on March 19, 2012, 10:49:25 am
You should have looked at the source code of sf::Text ;)
Title: Width of a space in sf::Text
Post by: nietaki 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 ;)
Title: Width of a space in sf::Text
Post by: Laurent on March 19, 2012, 02:36:42 pm
Especially SFML, which has a small, simple and well-written code :mrgreen: