Use embeded Font


#1

I am trying to use am embeded font.

I searched in the forum and found this topic by Jules http://www.juce.com/forum/topic/embedded-font-support

 

What I have done:

- Added the TTF file with Introjucer, so its accessed by the BinaryData
- Override the "getTypefaceForFont()" in my custom LookAndFeel like so:

Typeface::Ptr getTypefaceForFont(const Font& font) override
{
    return Typeface::createSystemTypefaceFor(BinaryData::myFont_ttf, BinaryData::myFont_ttfSize);
}

so it returns this font eitherway.

I can't see any change in my GUI though.
I know I did something wrong, but don't know what.

Any advice?


#2

Can't think of anything obvious, except maybe you're drawing with a Font object that you already created before you set the new L+F?


#3

What I've done is to set my LookAndFeel in the begining of the MainComponent constructor.
Other than that, I just overriden the getTypefaceForFont(), as mentioned above, and the drawButtonBackground().

If I set the font on every componenten separately, it works fine (the font changes).

Is there another way to se embeded fonts?


#4

It should work perfectly well like that, we use the same technique in various apps.

I'm actually just about to fix a related bug where embedded fonts don't draw correctly if you use an AttributedString but forget to set its font. But as long as you use them properly, it should all work OK.


#5

I am doing this on Windows (if it makes any difference), I'll check this on my Mac too, and see if I have something else wrong.

I'll post if i figure it out.

Cheers


#6

Hi all,


I have the same problem too.
I have embed a new font named "BroadcastTitling" at the beggining of the constructor of my MainComponent.cpp  but it has no effect at all.
In order to check if the problem comes from fonts, I installed it in my system (windows 10) and it works fine.

Here is a piece of code from MainComponent.cpp :
where Laf_V3 : private variable of type LookAndFeel_V3, in MainComponent.h
 

MainComponent::MainComponent ()
{
 //[Constructor_pre] You can add your own custom stuff here..
   Typeface::Ptr FontPtr = Typeface::createSystemTypefaceFor(BinaryData::BroadcastTitling_ttf,   BinaryData::BroadcastTitling_ttfSize);
    String Sf = FontPtr->getName();
    Laf_V3.setDefaultSansSerifTypefaceName(Sf);
    LookAndFeel::setDefaultLookAndFeel(&Laf_V3);
  //[/Constructor_pre]
 

  addAndMakeVisible (Lbl_DoTest = new Label ("Lbl_DoTest",
                                               TRANS("Do Test")));
    Lbl_DoTest->setFont (Font (15.00f, Font::plain));
    Lbl_DoTest->setJustificationType (Justification::centredLeft);
    Lbl_DoTest->setEditable (false, false, false);
    Lbl_DoTest->setColour (TextEditor::textColourId, Colours::black);
    Lbl_DoTest->setColour (TextEditor::backgroundColourId, Colour (0x00000000));
 

 ....more code

}

 

Any suggestion please? 

 

George

 

Edit.: Here are the BinaryData and .ttf file itself, if anyone wants to reproduce the problem..


#7

That won't work - you can't just ask for a custom typeface by name, as it won't be found by the OS, which won't know about its existence. You'd need to do like the OP and create a Font object directly from the Typeface object.


#8

I change my code like this :

MainComponent.cpp (constructor) :

where General_Laf : my custom LookAndFeel_V3 in order to override only the getTypefaceForFont(0 function.


    Laf_V3 = new General_Laf();
    Laf_V3->setDefaultSansSerifTypefaceName(Sf);
    LookAndFeel_V3::setDefaultLookAndFeel(Laf_V3);
    this->setLookAndFeel(Laf_V3);
 

.. more code for addAndMakeVisible the gui components

Custom LookAndFeel :


    Typeface::Ptr getTypefaceForFont(const Font& font) override
    {
        return Typeface::createSystemTypefaceFor(BinaryData::BroadcastTitling_ttf,
                                                                             BinaryData::BroadcastTitling_ttfSize);

    }

 

This function is called subsequently, but has no effect.

But, if i change this function as follows :

  
    Typeface::Ptr getTypefaceForFont(const Font& font) override
    {
        Font CurrentFont("Comic Sans MS", 15.0f, Font::plain);
        return Font::getDefaultTypefaceForFont(CurrentFont);
    }


everything goes ok and all fonts are turned to the specified font family.
I can't realize what's really going wrong.

I appreciate any help

George


#9

Have you tried with the latest version from today?

Someone else reported an identical problem, which turned out to be due to a completely unrelated mistake involving their use of an AttributedString. I've tightened that class up now so if that sounds like your situation, try again with the newest code..


#10


I just downloaded the latest version, and it works perfectly now.

Thanks a lot Jules.

 

George 
 


#11

OK, thanks.

FYI the problem I fixed was nothing to do with embedded fonts, that was just a symptom of a bug you get if you create an AttributedString but don't bother specifying its font.

Even though it seems to work for you now, you probably still should fix your code so that you specify a font for your AttributedString, otherwise they're just using some arbitrary default size, which probably isn't what you want.


#12

Hi Jules,

I tested my app, with various fonts that they not installed in the system (windows 10).
It seems that everything works fine.
I would assume (fully knowing to whom I say this) that somewhere deeply in Juce, the two issues (embedded fonts and Attributedstring) ends up in a same common procedure. 
I attempted twice to follow the sequence of the two procedures through debugger, but I lost the control and I stopped the try. Sorry...
In any case I will observe the behaviour of my app and I will inform you for any change.
I think the OP will take the same care.  

Thanks again 

George