Loading TTF Fonts without installing on system


#1

Hi everyone,

I want to load any TTF font from file and use it in my application. I have read other posts in the forum and in all of them It says that your have first to install the font on system. Is there any way to load a TTF from file into a stream and create a CustomTypeface from it?

Thanx,


#2

See juce\extras\binarybuilder


#3

I have already used BinaryBuilder to generate cpp with some binary data previously written into FileOutputStream, but that is not what I want.
The problem is that in my app I have something like “Options Menu” where I want to select any TTF font, load it without installing on system, and change all fonts in the program with the new font. I have read CustomTypeface class like it is said in other posts, but in the constructor CustomTypeface::CustomTypeface (InputStream &serialisedTypefaceStream) you can only load a typeface from a previously saved stream, created by writeToStream(), not a general TTF file.

Thanks for the reply,


#4

OK, so let’s work backwards here and start with creating the custom typeface:

MemoryInputStream myFontStream ( myTtfData, myTtfData_Size, false); myTypeface = new CustomTypeface (myFontStream);

To get myTtfData, take a look at the addFile() method in Main.cpp of BinaryBuilder. It shows you how to load a file into a MemoryBlock (haven’t tried it myself, but it looks reasonably straight forward).


#5

Not so sure it will work, because de InputStream needs to be a Juce-formatted type input, not a bunch of TTF binary data, but I will try it. Apparently there is not any other obvious way to do.


#6

No, there’s currently no way to load a TTF from a file - to do so would require juce to have a TTF parser, which OSes don’t provide. It’d be possible using freetype: maybe Vinnie’s freetype utils provide a way to do this?


#7

Ok. Thanks!


#8

Oops, my bad. My example was from a font embedded after serialising from an installed font. My apps use one of these and the others are displayed using FreeType as Jules suggested. These others were definitely embedded using BinaryBuilder!

A word of warning though, I’ve found that FreeType fails to load some TTFs. There are a lot of options which can improve your hit rate I think, but what I usually do is groom my embedded fonts with a service like FontSquirrel (properly licensed fonts only of course!).


#9

At the risk of making myself look even more of an idiot - I’ve just realised that some of the failures I had with FreeType loading TTFs were because some of the embedded files were Juce-formatted fonts, not the original TTF. Just because there’s ttf in the resource variable name doesn’t mean it is a TTF!

I do have a vague memory of a TTF that wouldn’t load, but the probability of this is lower than I first thought.