Having trouble loading font


#1

Hi, having trouble loading a TTF font. This is the code I’m using:

m_p_typeface = Typeface::createSystemTypefaceFor( BinaryData::NunitoRegular_ttf, BinaryData::NunitoRegular_ttfSize );
String name = m_p_typeface->getName();

jassert( name.isNotEmpty() );

LookAndFeel::getDefaultLookAndFeel().setDefaultSansSerifTypefaceName( name );

But it has no effect. If I create a font from the typeface and set it individually on a component, it does work, but I don’t want to have to set each element individually.

m_my_font = Font( name, 12, Font::FontStyleFlags::plain );
combo.setFont( my_font );

And pointers welcome, thx.


#2

Where is m_p_typeface declared and what is its lifetime? You’ll also need to repaint the components after calling LookAndFeel:: setDefaultSansSerifTypefaceName() to see the change.


#3

Hi ed. It’s a member variable. I’m pretty sure nothing has been painted when I set the default font, but I’ll check later when back at my machine.


#4

OK, here is what I am using to test:

class MainComponent   : public Component
{
public:
    //==============================================================================
    MainComponent()
    {
        tptr = Typeface::createSystemTypefaceFor (BinaryData::Everson_Mono_Bold_ttf, BinaryData::Everson_Mono_Bold_ttfSize);
        LookAndFeel::getDefaultLookAndFeel().setDefaultSansSerifTypefaceName (tptr->getName());
        
        for (int i = 1; i <= 5; ++i)
            addAndMakeVisible (buttons.add (new TextButton ("Button" + String (i))));
        
        for (int i = 1; i < 10; ++i)
            box.addItem ("Item " + String (i), i);
        
        addAndMakeVisible (box);
        
        setSize (600, 400);
    }

    ~MainComponent()
    {
    }

    //==============================================================================
    void paint (Graphics& g) override
    {
        // (Our component is opaque, so we must completely fill the background with a solid colour)
        g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
    }

    void resized() override
    {
        auto bounds = getLocalBounds().reduced (10);
        
        for (auto* b : buttons)
            b->setBounds (bounds.removeFromTop (50).reduced (50, 5));
        
        box.setBounds (bounds);
    }


private:
    //==============================================================================
    OwnedArray<TextButton> buttons;
    ComboBox box;
    
    Typeface::Ptr tptr;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
};

and it seems to be working OK.


#5

ok, thanks - is working, I think the font I’ve been given is just closed to the standard font so it was hard to tell.