Calculating text legth (in pixels)

pin

11-05-2007 17:06:19

I found a page in Ogre help forum explaining how to do it but It's C++ code that I can't figure out how to port:

http://www.ogre3d.org/phpBB2/viewtopic. ... 1af3ffb756

the code
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;


Any ideas?

pin

14-05-2007 13:25:50

I found a solution after rubbing my head for a few days. Here's the method:


#region Initialization Code
Fnt = FontManager.Singleton.GetByName("BlueHighway");
#endregion

#region Shut Down code
Fnt.Dispose();
#endregion

#region TextPixelLength

Mogre.FontPtr Fnt;
float LeterSize = 0;
public float GetLeterPixelLength(int ASCIIChar, int FontSize)
{
LeterSize = 0;

try
{
if (ASCIIChar == 32)
LeterSize = Fnt.GetGlyphAspectRatio(48);
else
LeterSize = Fnt.GetGlyphAspectRatio( (uint) ASCIIChar);
}
catch(Exception e)
{
MessageBox.Show(e.ToString());

}

return LeterSize * FontSize;
}
#endregion


You can get the ascii codes for all characters here:
http://en.wikipedia.org/wiki/ASCII

Hope you find it useful