# Use embeded Font

**URL:** https://forum.juce.com/t/use-embeded-font/16288
**Category:** General JUCE discussion
**Created:** [December 29, 2015, 7:11pm UTC](https://forum.juce.com/t/use-embeded-font/16288 "2015-12-29T19:11:18Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![nikolas-k](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nikolas-k/32/2307_2.png) [@nikolas-k](https://forum.juce.com/u/nikolas-k)
#### Post date: [December 29, 2015, 7:11pm UTC](https://forum.juce.com/t/use-embeded-font/16288/1 "2015-12-29T19:11:18Z")

</div>

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

&nbsp;

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?

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)
#### Post date: [December 30, 2015, 10:33am UTC](https://forum.juce.com/t/use-embeded-font/16288/2 "2015-12-30T10:33:10Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![nikolas-k](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nikolas-k/32/2307_2.png) [@nikolas-k](https://forum.juce.com/u/nikolas-k)
#### Post date: [December 30, 2015, 7:15pm UTC](https://forum.juce.com/t/use-embeded-font/16288/3 "2015-12-30T19:15:00Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)
#### Post date: [December 30, 2015, 7:19pm UTC](https://forum.juce.com/t/use-embeded-font/16288/4 "2015-12-30T19:19:50Z")

</div>

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&nbsp;where embedded fonts don't draw correctly if you use an AttributedString&nbsp;but&nbsp;forget to set its font. But as long as you use them properly, it should all work OK.

---

<div class="post-metadata">

### Author: ![nikolas-k](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/nikolas-k/32/2307_2.png) [@nikolas-k](https://forum.juce.com/u/nikolas-k)
#### Post date: [December 30, 2015, 7:35pm UTC](https://forum.juce.com/t/use-embeded-font/16288/5 "2015-12-30T19:35:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)
#### Post date: [January 3, 2016, 4:39pm UTC](https://forum.juce.com/t/use-embeded-font/16288/6 "2016-01-03T16:39:00Z")

</div>

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 &nbsp;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  
&nbsp;

> MainComponent::MainComponent ()  
> {  
> &nbsp;//[Constructor\_pre] You can add your own custom stuff here..  
> &nbsp; &nbsp;Typeface::Ptr FontPtr = Typeface::createSystemTypefaceFor(BinaryData::BroadcastTitling\_ttf, &nbsp; BinaryData::BroadcastTitling\_ttfSize);  
> &nbsp;&nbsp; &nbsp;String Sf = FontPtr-\>getName();  
> &nbsp;&nbsp; &nbsp;Laf\_V3.setDefaultSansSerifTypefaceName(Sf);  
> &nbsp;&nbsp; &nbsp;LookAndFeel::setDefaultLookAndFeel(&Laf\_V3);  
> &nbsp;&nbsp;//[/Constructor\_pre]  
> &nbsp;
> 
> &nbsp; addAndMakeVisible (Lbl\_DoTest = new Label ("Lbl\_DoTest",  
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRANS("Do Test")));  
> &nbsp; &nbsp; Lbl\_DoTest-\>setFont (Font (15.00f, Font::plain));  
> &nbsp; &nbsp; Lbl\_DoTest-\>setJustificationType (Justification::centredLeft);  
> &nbsp; &nbsp; Lbl\_DoTest-\>setEditable (false, false, false);  
> &nbsp; &nbsp; Lbl\_DoTest-\>setColour (TextEditor::textColourId, Colours::black);  
> &nbsp; &nbsp; Lbl\_DoTest-\>setColour (TextEditor::backgroundColourId, Colour (0x00000000));  
> &nbsp;
> 
> &nbsp;....more code
> 
> }
> 
> &nbsp;

Any suggestion please?&nbsp;

&nbsp;

George

&nbsp;

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

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)
#### Post date: [January 3, 2016, 4:56pm UTC](https://forum.juce.com/t/use-embeded-font/16288/7 "2016-01-03T16:56:15Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)
#### Post date: [January 3, 2016, 8:22pm UTC](https://forum.juce.com/t/use-embeded-font/16288/8 "2016-01-03T20:22:58Z")

</div>

I change my code like this :

MainComponent.cpp (constructor) :

where General\_Laf : my custom LookAndFeel\_V3 in order to override only the&nbsp;getTypefaceForFont(0 function.

> &nbsp;&nbsp; &nbsp;Laf\_V3 = new General\_Laf();  
> &nbsp;&nbsp; &nbsp;Laf\_V3-\>setDefaultSansSerifTypefaceName(Sf);  
> &nbsp;&nbsp; &nbsp;LookAndFeel\_V3::setDefaultLookAndFeel(Laf\_V3);  
> &nbsp;&nbsp; &nbsp;this-\>setLookAndFeel(Laf\_V3);  
> &nbsp;
> 
> .. more code for addAndMakeVisible the gui components

Custom LookAndFeel :

> &nbsp;&nbsp; &nbsp;Typeface::Ptr getTypefaceForFont(const Font& font) override  
> &nbsp;&nbsp; &nbsp;{  
> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return Typeface::createSystemTypefaceFor(BinaryData::BroadcastTitling\_ttf,  
> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BinaryData::BroadcastTitling\_ttfSize);
> 
> &nbsp; &nbsp; }
> 
> &nbsp;

This function is called subsequently, but has no effect.

But, if i change this function as follows :

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

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

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)
#### Post date: [January 3, 2016, 8:32pm UTC](https://forum.juce.com/t/use-embeded-font/16288/9 "2016-01-03T20:32:30Z")

</div>

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..

---

<div class="post-metadata">

### Author: ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)
#### Post date: [January 3, 2016, 8:56pm UTC](https://forum.juce.com/t/use-embeded-font/16288/10 "2016-01-03T20:56:36Z")

</div>

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

Thanks a lot Jules.

&nbsp;

George&nbsp;  
&nbsp;

---

<div class="post-metadata">

### Author: ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)
#### Post date: [January 3, 2016, 10:01pm UTC](https://forum.juce.com/t/use-embeded-font/16288/11 "2016-01-03T22:01:33Z")

</div>

OK, thanks.

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

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

---

<div class="post-metadata">

### Author: ![xrousaios](https://avatars.discourse-cdn.com/v4/letter/x/e47c2d/32.png) [@xrousaios](https://forum.juce.com/u/xrousaios)
#### Post date: [January 4, 2016, 8:36am UTC](https://forum.juce.com/t/use-embeded-font/16288/12 "2016-01-04T08:36:00Z")

</div>

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&nbsp;somewhere deeply in Juce, the two issues (embedded fonts and Attributedstring) ends up in&nbsp;a same common procedure.&nbsp;  
I attempted twice&nbsp;to follow the sequence of the two procedures&nbsp;through debugger, but&nbsp;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&nbsp;the OP will take the same care. &nbsp;

Thanks again&nbsp;

George
