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.
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.
When is the getTypefaceForFont() used?
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.
[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
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.
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!