createSystemTypefaceFor example?


#1

Hi everyone :)

As my plugin development goes fine, I am willing to include a Google Font in my VST. However, although I checked kind of all the threads about it on the forum, it's still not working.

Since the examples I found on the forum aren't often complete, it is not clear for me if I am doing a mistake or if the problem comes from something else (although it is probably the former :) ).

Here's what I am doing :

First, I loaded the TTF file in the Projucer

Then, the code (I am planning to move it to my custom Look&Feel class later but for now I am including everything in the paint() method of my PluginEditor)

g.setColour(Colours::white);

Typeface::Ptr tface = Typeface::createSystemTypefaceFor(BinaryData::MontserratRegular_ttf, BinaryData::MontserratRegular_ttfSize);

String tfaceName = tface->getName();
Font myFont(tface);
myFont.setHeight(20.0f);

g.setFont(myFont);

g.drawText("Hello, World!", 2, 2, 200, 40, true);

It's compiling perfectly but no text is displayed. If someone can enlighten me about what is wrong it would be super useful :)

Thanks a lot,

Adrien


#2

It's not a good idea to load the font inside the paint routine. Loading a font will usually take quite some time. If your paint routine takes too long then OS X will decide to abort refreshing the window and just show white instead.


#3

Alright, problem (half-)solved, thank you very much Fabian ! :)

Indeed, if I move the Typeface declaration in the Editor's constructor, it is working.

However, now, I am encountering another problem if I move it to my custom Look and Feel.

The problem itself is a bit like in this thread :

http://www.juce.com/forum/topic/use-embeded-font

, and I am following this other thread :

http://www.juce.com/forum/topic/example-code-creating-using-custom-typeface

to set up my custom font.

Here is my code :

1/ in the editor calling the Look&Feel :

LookAndFeelCustom* customLookAndFeel = new LookAndFeelCustom();
setLookAndFeel(customLookAndFeel);

2/ In my LookAndFeelCustom.h

Typeface::Ptr tf; // in public

3/ In my LookAndFeelCustom.cpp constructor :

tf = Typeface::createSystemTypefaceFor(BinaryData::IndieFlower_ttf, BinaryData::IndieFlower_ttfSize);

4/ The overriding the getTypefaceForFont() method :

Typeface::Ptr LookAndFeelCustom::getTypefaceForFont(const Font& font)
{
    return tf;
}

And I see no difference in my GUI... Maybe I should do something to trigger the getTypefaceForFont() ? It's not so clear for me.

Thanks a lot by advance,

 


#4

Have you set a breakpoint at your getTypefaceForFont override? Does it actually get called? Where exactly do you call 
setLookAndFeel in your editor?


#5

I'm not sure if it's a bug or intended behavior but I think getTypefaceForFont only gets called for the default look and feel. I.e. you have to set it with LookAndFeel::setDefaultLookAndFeel. To be fair, if this is the case it should probably be added to the docs as I've seen this come up a few times.

This is usually fine in practice though as your default font will contain all the app-wide stuff like fonts. You can still return different typefaces for different font names and styles.


#6

I had not set a break point, probably should have :D

the setLookAndFeel() was called in the editor's constructor.


#7

Ah, thank you so much Dave ! You saved my life haha !

Indeed, it's a bit tricky - indeed it should be added in the docs. Just to clarify if other people stumble upon that thread, you should call :

LookAndFeel_V3::setDefaultLookAndFeel(customLookAndFeel);

to call the getTypefaceForFont() method. Otherwise if you just do 

setLookAndFeel(customLookAndFeel);

It won't work.

Yes, anyway I will probably use only one font so in practice this little trick is working perfectly for me. I assume that for people who want to propose different layouts/look and feel it might be a bit more complex.

Thanks again for the help,