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

Author Topic: Font Metrics  (Read 2120 times)

0 Members and 1 Guest are viewing this topic.

schragnasher

  • Newbie
  • *
  • Posts: 6
    • View Profile
Font Metrics
« on: June 17, 2011, 08:35:21 pm »
I am writing my own text render routine for the sf::Font class, but im having troubles with metrics. What is the rectangle relative to? The Character Size?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Font Metrics
« Reply #1 on: June 17, 2011, 09:55:36 pm »
Quote from: "schragnasher"
What is the rectangle relative to? The Character Size?
No, that's what makes sf::Text::GetRect() complicated to use in a generic way ;)

The rectangle depends on the single character's heights (the sf::Glyph class contains those values). So, a text with a "y" is higher than with a "m".

I wrote a function to compute the rect size of a sf::Text independent of its characters. It is probably bugged, I haven't used it for a long time (in fact I have hardly ever used it :D), but maybe you can adapt it:
Code: [Select]
sf::Vector2f GetSize(const sf::Text& text)
{
const sf::String& str = text.GetString();
unsigned int lines = std::count(str.Begin(), str.End(), '\n') + 1;

float height = lines * text.GetScale().y * (text.GetFont().GetLineSpacing(text.GetCharacterSize()) + 2);
return sf::Vector2f(text.GetRect().Width, height);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything