Runtime problems with custom font


#1

I pulled a random .ttf file off the internet to try and embedd it in preperation for the real custom font that we will end up using. I searched on here for how to embedd the font within the program, and ended up doing the following after using the introjucer to convert the .ttf to a data array.

    MemoryInputStream mis(BinaryData::arial_ttf, BinaryData::arial_ttfSize, false);
    Typeface* testTypeface = new CustomTypeface(mis);
    testFont = new Font(testTypeface); //program breaks here
    testFont->setHeight(10.0f);

the program breaks at the assert “jassert (typefaceName.isNotEmpty());” so I assume the custom typeface is not getting initialised correctly. If I continue past the break the program will run and display the default font with the top broken off a bit. Where am I going wrong?


#2

Always read the small-print:

/** Loads a typeface from a previously saved stream. The stream must have been created by writeToStream(). @see writeToStream */ explicit CustomTypeface (InputStream& serialisedTypefaceStream);

CustomTypeface doesn’t load TTFs - like it says, it only loads data that it stored in its own format.


#3

I’ve been doing this quite a lot in my current project and funnily enough I uploaded a helper method to do exactly this to my module yesterday. Its pretty standard code but the docs give and examples of how to use the font in a LookAndFeel as well.

For some reason I think this has to be set as the default look and feel as the getTypefaceForFont wasn’t being called if it was set for just a component but I’ll look into that later. Of course you could always load the serialised typeface directly to a Font as you have done in your example but I like to have a global look and feel that holds all my custom fonts.


#4

ah so i need to serialise the font file first? ill do a search for a font serialiser. im sure i saw one in my searches.


#5

Here is the code to the docs I just posted, does exactly what you need with an example. Line 166 the function starts.


#6

that will do nicely, thank you very much.


#7
    enum FontStyleFlags
    {
        plain       = 0,    /**< indicates a plain, non-bold, non-italic version of the font. @see setStyleFlags */
        bold        = 1,    /**< boldens the font. @see setStyleFlags */
        italic      = 2,    /**< finds an italic version of the font. @see setStyleFlags */
        underlined  = 4     /**< underlines the font. @see setStyleFlags */
    };

I have just been given some custom font that contains different styles like extra thin italic, am I right in thinking I can’t load in any style other than the 4 defined in FontStyleFlags? Only way I can imagine working around this is editing the ttf files I have been given, install them 4 at a time, and serialize them that way, so extra thin italic would temporarliy be one of the 4 above.


#8

Are you using the latest version of JUCE i.e. the tip. The Font class has a constructor that takes a Typeface style, you can also use the static Font::findAllTypefaceStyles (const String &family) method to get these names.

The FontStyleFlags enum is provided for the 3 basic styles and for backwards compatibility as extended styles area relatively new feature.


#9

thats the constructor i use, thats not a problem. i just have different styles of font not directly supported in juce. i can load them in no bother if i edit the ttf afaik.