[2.1][SOLVED] Font loads after trying to use it Topic is solved

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

[2.1][SOLVED] Font loads after trying to use it

Post by gabbsson »

Hello!

I've been struggling with solving an issue regarding Overlays (I know its old but I don't have a choice for now).
The problem arises when I try to calculate the size of a string, when using a specific font, to align the string.

The specific line is this:

Code: Select all

Ogre::String text = // Some text
float fontHeight = // Some non zero/negative float

...

// for each character in the string
float characterWidth = font->getGlyphAspectRatio(static_cast<Ogre::Font::CodePoint>(text[i]))*fontHeight;
I noticed that it always ends up the same as the font size, i.e. the getGlyphAspectRatio was always 1. While doing something completely unrelated I noticed that if I "reloaded" the overlay object it suddenly worked as intended!
So looking into the code I find:

Code: Select all

  /** Gets the aspect ratio (width / height) of this character. */
        inline Real getGlyphAspectRatio(CodePoint id) const
        {
            CodePointMap::const_iterator i = mCodePointMap.find(id);
            if (i != mCodePointMap.end())
            {
                return i->second.aspectRatio;
            }
            else
            {
                return 1.0;
            }
        }
And naturally I find the relevant OgreLog message:


Font arial using texture size 4096x2048
Info: Freetype returned null for character 32 in font arial
Info: Freetype returned null for character 160 in font arial
Texture: arialTexture: Loading 1 faces(PF_RG8,4096x2048x1) Internal format is PF_RG8,4096x2048x1.


So it seems that the first time I attempt to use the Font it has not finished/loaded properly (since it returns null for some characters) but then after that works fine?
Am I loading the font incorrectly?

Code: Select all

   // Font stuff now that OverlayManager is created
    Ogre::FontManager* fontManager = Ogre::FontManager::getSingletonPtr();
    Ogre::FontPtr fontPtr = fontManager->create("arial", "General");
    Ogre::Font* font = fontPtr.getPointer();
    font->setType(Ogre::FT_TRUETYPE);
    font->setSource(fontFolderPath + "/arial.ttf");
    font->setTrueTypeSize(72);
    font->setTrueTypeResolution(128);
    font->addCodePointRange(Ogre::Font::CodePointRange(0, 255));
}
It's been a while since I wrote the above code so I wouldn't be surprised to find I missed something and can't remember it now.
Looked for this issue on the forum, the log message seems to be quite common but could not find an example of this specific issue.

Thanks for reading!
Last edited by gabbsson on Wed Jun 12, 2019 6:23 pm, edited 1 time in total.
gabbsson
Halfling
Posts: 65
Joined: Wed Aug 08, 2018 9:03 am
x 13

Re: [2.1] Font loads after trying to use it

Post by gabbsson »

Turns out i needed to call font->load(), not very surprising.
Which initially ended in segfault but was easily fixed by making sure i was initializing HLMS first.

Solved!
Post Reply