Embedding fonts in JUCE 8

Disclaimer: I’m am relatively new to JUCE and C++ in general so I may be missing something obvious here.

I am attempting to embed a custom font in my JUCE project to use in the GUI but since I’m running a newer version of the framework I get deprecated warnings for Font. I have successfully used FontOptions to set a system font like Helvetica but I’m wondering how to embed and set up a custom font from a .ttf file.

Is it possible for someone to walk me through the process of how embedded fonts are set up in the latest versions of JUCE?

Thanks

I have the .ttf files in an assets folder together with all the other plugin assets. In your CMakeLists you can add something like

file(GLOB plugin_assets
    "assets/*.*")

juce_add_binary_data(plugin_data
    SOURCES 
    ${plugin_assets})

to create a static library with your asset files.

In your editor, or wherever else you need the font, you can do something like

#include "BinaryData.h"

[...]

juce::FontOptions fontOptions {juce::Typeface::createSystemTypefaceFor(BinaryData::Helvetica_ttf, BinaryData::Helvetica_ttfSize)};

std::unique_ptr<juce::Font> myFont = std::make_unique<juce::Font>(fontOptions.withHeight(fontHeight).withKerningFactor(kerningFactor));

Not sure if it’s the best approach but it works fine for me.

2 Likes

Thank you jacopoeftilo, you are a life saver! The up to date documentation of this is basically non-existent. Ended up going with a slightly simplified version along the lines of this:

juce::FontOptions titleFont {juce::Typeface::createSystemTypefaceFor(BinaryData::CustomFont_ttf, BinaryData::CustomFont_ttfSize)};

titleLabel.setFont(titleFont.withHeight(85.0f));

Didn’t need to add anything to CMakeLists either. Just dropped my assets folder into projucer and made sure binary resource was ticked for the font file. I’m using Xcode so this is probably different for windows users.
Thanks, appreciate the helpful response!

Good that it worked out!

Yes that should be the procedure to add the binary resources if you are using Projucer and not cmake,

1 Like