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:

2 Likes

I figured this out a while ago! Here’s my solution.

  1. I defined a custom LookAndFeel class with a member called customTypeface_ which I initialized using createSystemTypefaceFor
juce::Typeface::Ptr customTypeface_ {juce::Typeface::createSystemTypefaceFor (BinaryData::RobotoMonoLight_ttf, BinaryData::RobotoMonoLight_ttfSize)};
  1. In my LookAndFeel’s constructor, I set the default san serif font to my custom typeface and set the default LookAndFeel to the custom LookAndFeel instance. I also made sure to set the default LookAndFeel to nullptr in the destructor.
CustomLAF::CustomLAF ()
{
    setDefaultSansSerifTypeface (customTypeface_);

    juce::LookAndFeel::setDefaultLookAndFeel(this);

}

CustomLAF::~CustomLAF ()
{
    juce::LookAndFeel::setDefaultLookAndFeel(nullptr);
}



  1. I then created an instance of my custom LookAndFeel as a member in my PluginEditor. No need to do anything except define it as a member variable!

I did get a weird issue when my look and feel class would randomly not propagate to PopUpMenus. I resolved this by manually setting the LookAndFeel of my PopUpMenu to the default LookAndFeel.

// in the constructor where the popup is a child
popupMenu_.setLookAndFeel(&juce::LookAndFeel::getDefaultLookAndFeel());

// in the destructor
popupMenu_.setLookAndFeel(nullptr);

This was all clearly described in the JUCE Font Embedding Guide that I skimmed rather than read (I was missing the setDefaultLookAndFeel step). TIL: when in doubt, slow down. Thanks for the help everybody!

1 Like