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

Author Topic: sf::String and size errors  (Read 4563 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::String and size errors
« Reply #15 on: October 15, 2010, 10:07:43 pm »
Ok, in fact you can't use GetSize() alone. It gives you the maximum character size, but if you apply it from 0 you will only reach the baseline. Characters such as 'j' or 'g' are not bigger, they are offseted. So the size of your cursor must be GetSize() plus the amount of pixels in 'j' or 'g' that go under the baseline.

This can be done with the following formula:
Code: [Select]
const sf::Font& font = text.GetFont();

// assuming 'j' has the max extent under the baseline...
sf::Glyph glyph = font.GetGlyph('j');
float extent = glyph.Rectangle.Bottom;

// we have to compute the scale factor to apply to match the text size
float scale = text.GetSize() / font.GetCharacterSize();

float size = text.GetSize() + extent * scale;


Maybe not the clearest code ever, but it should work in all cases ;)
Laurent Gomila - SFML developer

NoIdea

  • Full Member
  • ***
  • Posts: 149
    • View Profile
sf::String and size errors
« Reply #16 on: October 16, 2010, 10:07:43 am »
Thanks, never had to use sf::Glyth but I'll try it...

 

anything