[2.1] Text Width using Bluehighway font

Problems building or running the engine, queries about how to use features etc.
Post Reply
Posts: 33
Joined: Wed Aug 08, 2018 9:03 am
x 3

[2.1] Text Width using Bluehighway font

Post by gabbsson » Mon Feb 18, 2019 10:12 am


This is a fairly minor issue but I can't seem to calculate the width of a string using the Bluehighway font.
Here is the code I use to find the text width:

Code: Select all

     // FontName_ = BlueHighWay , loaded earlier. Tried one from arbitrary website and the other from a Ogre project on github

    Ogre::FontManager* fontManager = Ogre::FontManager::getSingletonPtr();
    Ogre::FontPtr font = fontManager->getByName(fontName_);
    Ogre::String text = ogreTextElement_->getCaption(); // Overlay Text Element caption to check width of

    float sp = font->getGlyphAspectRatio(0x0031)*fontSize_;
    Ogre::Real textWidth = 0; // Current line width 
    Ogre::Real prevLongest = 0; // Keep track of the longest line seen
    for(unsigned int i=0; i < text.length();i++)
        if (text[i] == 0x0a) // New line
            if (textWidth > prevLongest) prevLongest = textWidth;
            textWidth = 0;
        if (text[i] == 0x0020)	textWidth += sp;
        else textWidth += font->getGlyphAspectRatio(static_cast<Ogre::Font::CodePoint>(text[i]))*fontSize_;
    // Check last line
    if (textWidth > prevLongest) prevLongest = textWidth;

    textWidth_ = prevLongest;
This works fine for most fonts (read the handful I have tested) except BlueHighWay for which the width is roughly a factor 2 too big.
I use the width for custom alignment since I could never get the built in one to work the way I wanted.

Curious if someone has an alternative/better solution which works for ALL fonts.
Or if anyone has insight what might be causing this to happen for that font specifically.
If I can be confident it won't happen for most fonts I can live with that, but I am afraid other fonts may have the same issue unexpectedly.

0 x

Post Reply