Struggles Implementing Default Custom Font

Hello all,

I’m attempting to integrate a custom font into my plugin. Currently, I’m overriding the getTypefaceForFont method, creating a member variable in PluginEditor class called font and passing it to the desired components. I then set the font in the paint method using the juce::Graphics setFont method.

`
juce::Typeface::Ptr getTypefaceForFont(const juce::Font& f) override
{
juce::ignoreUnused(f);
static juce::Typeface::Ptr myFont = juce::Typeface::createSystemTypefaceFor(BinaryData::MegrimRegular_ttf, BinaryData::MegrimRegular_ttfSize);
return myFont;
};

void paint (juce::Graphics& g) override
{
g.setFont(font);
g.fillAll(juce::Colours::black);
g.setColour (juce::Colours::white);
g.drawRect (bounds, 1.0); // draw an outline around the component
g.setFont (fontHeight);
g.drawText (text, bounds, juce::Justification::centred, true); // draw some placeholder text
}
`
This works but feels wrong. My initial approach was to override the default LookAndFeel with my getTypefaceForFont function, then ensure that the juce::Graphics class uses this function whenever it paints.

I tried doing this using the following constructor with the overridden getTypefaceForFont method
CustomLookAndFeel () { setDefaultSansSerifTypeface(getTypefaceForFont(juce::Font ())); };
but I still end up with the default font rather than my custom font.

  1. When is the getTypefaceForFont() used?
  2. When and where is the Font set for the Graphics object passed into a component’s paint method?

The answer to these questions + any other insight would be greatly appreciated. Thank you for your time.

This may help:

1 Like

[quote=“[Guide] Juce Font Embedding (2019), post:1, topic:35041”]
There are MANY threads about fonts and embedding. But the information is pretty scattered around the forum.
[/quote]

Almost like there ought to be an official wiki where documentation could be collaboratively updated with tips, hints and examples :slight_smile:

1 Like