# Runtime problems with custom font

**URL:** <https://forum.juce.com/t/runtime-problems-with-custom-font/9248>\
**Category:** Audio Plugins\
**Created:** [September 17, 2012, 8:47am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248 "2012-09-17T08:47:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cool-mr-croc](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cool-mr-croc](https://forum.juce.com/u/cool-mr-croc)\
**Post date:** [September 17, 2012, 8:47am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/1 "2012-09-17T08:47:12Z")

</div>

I pulled a random .ttf file off the internet to try and embedd it in preperation for the real custom font that we will end up using. I searched on here for how to embedd the font within the program, and ended up doing the following after using the introjucer to convert the .ttf to a data array.

```auto
    MemoryInputStream mis(BinaryData::arial_ttf, BinaryData::arial_ttfSize, false);
    Typeface* testTypeface = new CustomTypeface(mis);
    testFont = new Font(testTypeface); //program breaks here
    testFont->setHeight(10.0f);
```

the program breaks at the assert “jassert (typefaceName.isNotEmpty());” so I assume the custom typeface is not getting initialised correctly. If I continue past the break the program will run and display the default font with the top broken off a bit. Where am I going wrong?

---

<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:** [September 17, 2012, 9:41am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/2 "2012-09-17T09:41:07Z")

</div>

Always read the small-print:

` /** Loads a typeface from a previously saved stream.
        The stream must have been created by writeToStream().
        @see writeToStream
    */
    explicit CustomTypeface (InputStream& serialisedTypefaceStream);
`

CustomTypeface doesn’t load TTFs - like it says, it only loads data that it stored in its own format.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [September 17, 2012, 10:29am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/3 "2012-09-17T10:29:06Z")

</div>

I’ve been doing this quite a lot in my current project and funnily enough I uploaded a [helper method to do exactly this to my module](http://drowaudio.co.uk/docs/namespace_gui_helpers.html#aa01f2492ab5f1c1a1d95c64e8ea21f9c) yesterday. Its pretty standard code but the docs give and examples of how to use the font in a LookAndFeel as well.

For some reason I think this has to be set as the default look and feel as the getTypefaceForFont wasn’t being called if it was set for just a component but I’ll look into that later. Of course you could always load the serialised typeface directly to a Font as you have done in your example but I like to have a global look and feel that holds all my custom fonts.

---

<div class="post-metadata">

**Author:** ![cool-mr-croc](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cool-mr-croc](https://forum.juce.com/u/cool-mr-croc)\
**Post date:** [September 17, 2012, 11:37am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/4 "2012-09-17T11:37:06Z")

</div>

ah so i need to serialise the font file first? ill do a search for a font serialiser. im sure i saw one in my searches.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [September 17, 2012, 11:44am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/5 "2012-09-17T11:44:49Z")

</div>

[Here is the code](https://github.com/drowaudio/drowaudio/blob/master/dRowAudio/gui/dRowAudio_GuiHelpers.h) to the docs I just posted, does exactly what you need with an example. Line 166 the function starts.

---

<div class="post-metadata">

**Author:** ![cool-mr-croc](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cool-mr-croc](https://forum.juce.com/u/cool-mr-croc)\
**Post date:** [September 17, 2012, 12:08pm UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/6 "2012-09-17T12:08:59Z")

</div>

that will do nicely, thank you very much.

---

<div class="post-metadata">

**Author:** ![cool-mr-croc](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cool-mr-croc](https://forum.juce.com/u/cool-mr-croc)\
**Post date:** [September 19, 2012, 9:30am UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/7 "2012-09-19T09:30:40Z")

</div>

```auto
    enum FontStyleFlags
    {
        plain = 0, /**< indicates a plain, non-bold, non-italic version of the font. @see setStyleFlags */
        bold = 1, /**< boldens the font. @see setStyleFlags */
        italic = 2, /**< finds an italic version of the font. @see setStyleFlags */
        underlined = 4 /**< underlines the font. @see setStyleFlags */
    };
```

I have just been given some custom font that contains different styles like extra thin italic, am I right in thinking I can’t load in any style other than the 4 defined in FontStyleFlags? Only way I can imagine working around this is editing the ttf files I have been given, install them 4 at a time, and serialize them that way, so extra thin italic would temporarliy be one of the 4 above.

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [September 19, 2012, 12:13pm UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/8 "2012-09-19T12:13:29Z")

</div>

Are you using the latest version of JUCE i.e. [the tip](https://github.com/julianstorer/JUCE/commits/master). The Font class has a [constructor](http://juce.com/api/classFont.html#ab41a1accb2fbbd96b2e35d5ef4c548c9) that takes a Typeface style, you can also use the static [Font::findAllTypefaceStyles (const String &family)](http://juce.com/api/classFont.html#a4504ff88b0af4aec852736d267f837cc) method to get these names.

The FontStyleFlags enum is provided for the 3 basic styles and for backwards compatibility as extended styles area relatively new feature.

---

<div class="post-metadata">

**Author:** ![cool-mr-croc](https://avatars.discourse-cdn.com/v4/letter/c/958977/32.png) [@cool-mr-croc](https://forum.juce.com/u/cool-mr-croc)\
**Post date:** [September 19, 2012, 3:54pm UTC](https://forum.juce.com/t/runtime-problems-with-custom-font/9248/9 "2012-09-19T15:54:11Z")

</div>

thats the constructor i use, thats not a problem. i just have different styles of font not directly supported in juce. i can load them in no bother if i edit the ttf afaik.
