[Guide] Juce Font Embedding (2019)

There are MANY threads about fonts and embedding.
But the information is pretty scattered around the forum.

Few notes regarding fonts and cross-platform:

  • Fonts by themselves aren’t assured to be available between OSes. also sometimes an OS would replace the default font or fallbacks. so a UI design with standard fonts might show up differently not only for different platforms but also for different OS versions.
  • TTF seems to be more cross platform at least from what’s written in the forum.
    (Ubuntu - JUCE Not Reading OTF Fonts, Embedding Custom Fonts OTF, TTF)

Embedded Fonts Cheat-Sheet:

Return a static Font (you can also return Typeface::Ptr) to be used each time you need the Typeface/Font object.

       static const Font getCustomFont()
        {
            static auto typeface = Typeface::createSystemTypefaceFor (BinaryData::CustomFont, BinaryData::CustomFont_size);
            return Font (typeface);
        }

Set a custom type to default (can be used from a LookAndFeel constructor)

setDefaultSansSerifTypeface (customTypeface);

If needed for DRY / LookAndFeel:

  1. override Typeface::Ptr getTypefaceForFont (const Font& f)

  2. IMPORTANT! apply your LookAndFeel using parent LookAndFeel:
    LookAndFeel::setDefaultLookAndFeel (&customLookAndFeel);

  • Note: you can call this from your LookAndFeel constructor (this) or from another component constructor as written above.

Here is a PIP (projucer) demo project:
CustomFontPIP.h (198.1 KB)

P.S. - This code was tested under Windows 10, macOS 10.7, macOS 10.14 and Linux (Debian).

Here is how to should look (this can be used to check if it ever got broken):
image


14 Likes

Thank you!! Very clear and concise.

1 Like