[Solved] Possible font::getGlyphAspectRatio bug?

Problems building or running the engine, queries about how to use features etc.
Post Reply
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58
Contact:

[Solved] Possible font::getGlyphAspectRatio bug?

Post by betajaen »

Hello there,

I'm trying to get the width in pixels of a piece of text shown onto an overlay. I'm using this piece of code to do so:

Code: Select all

float textWidth = 0;
Ogre::Font* f = dynamic_cast<Ogre::Font*>(Ogre::FontManager::getSingletonPtr()->getByName(fontName).get());
f->setTrueTypeSize(fontSize);
f->setTrueTypeResolution(72);
				
for(unsigned int i=0; i < text.length();i++) {	
	textWidth += (f->getGlyphAspectRatio(text[i]) * fontSize);
}
Which produces a semi-correct figure, shown in this image:

Image

The white bar over each GUI element indicates the figure given off by the textWidth using the caption of each button as the text. If the code was correct, then the white bar would end exactly at the end of the text. But as you can see with shorter text the difference isn't noticeable but with that really long one it's really off.

So the question is; Is my code wrong or is this a bug in Eihort?

Many thanks in advance.
Last edited by betajaen on Mon Apr 23, 2007 10:39 am, edited 1 time in total.
P
OGRE Expert User
OGRE Expert User
Posts: 421
Joined: Fri Jan 07, 2005 9:49 pm
Location: UK
x 2
Contact:

Post by P »

By looking at the API ref from Dagon, it seems that the size of the font isn't the screen size, but the point size of the font used to generate the texture.

Instead, it looks to me that you should use the charHeight ( from TextAreaOverlayElement ) instead of the fontSize to calculate the text width.


EDIT :

You can look at the code in

Code: Select all

 void TextAreaOverlayElement::updatePositionGeometry()
Here the length of the text is calculated for applying the proper alignement.
User avatar
betajaen
OGRE Moderator
OGRE Moderator
Posts: 3447
Joined: Mon Jul 18, 2005 4:15 pm
Location: Wales, UK
x 58
Contact:

Post by betajaen »

Image

Thank you! That function really helped.

For anyone who has the same problems in the future. This is the correct code:

Code: Select all

float textWidth = 0;

for(unsigned int i=0; i < text.length();i++) {	
	if (text[i] == 0x0020)
		textWidth += f->getGlyphAspectRatio(0x0030);
	else
		textWidth += (f->getGlyphAspectRatio(text[i]));
}
textWidth *= fontSize;
:D
Post Reply