Loading custom TTF fonts


#1

I’m a bit new to JUCE, but an experienced iPhone developer. Just starting using JUCE for an iPad project and ran into a problem that I was hoping somebody would have some experience with.

I’m trying to load a custom TTF font that I would like to be embedded in the project and then use via the standard JUCE labels and drawing… but not sure how to make it available.

In my iPad build, I know how to load the font from the OSX application bundle and hence render glyphs from it in a typical OSX way:

CGFontRef loadFont() {
// Get the path to our custom font and create a data provider.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@“MySpecialFont” ofType:@“ttf”];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);

// Create the font with the data provider, then release the data provider.
CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);
CGDataProviderRelease(fontDataProvider);
return customFont;
}

Is there a way I can add this to my JUCE application in a standard way? Ideally, we would would use the same mechanism for our windows build as well, but I would be happy enough just to get this working on the iPad build for a start.

I didn’t see a call to load a local font file in the JUCE API, but it is still a bit new to me, so I could have missed it. Are there API calls somewhere to load font files?


#2

There’s nothing in there to load a platform-specific font (and I’m not sure if it’d be possible on Windows anyway), but you can save fonts into a juce-specific format and reload them from binary data without going near the plaform-specific font code at all, using the CustomTypeface class.


#3

Great… this sounds like the right approach. We definitely want to keep everything in JUCE as much as possible. I’ll be giving this a try shortly, and I’ll be sure to report back any problems and hopefully solutions that we encounter on this adventure.


#4

Actually, a couple follow-on questions:

  1. Is there information on the JUCE font file format?

  2. I was just experimenting with exporting the glyphs from the font set as SVG files using FontForge — this seems to work well, and produces simple SVG files which are simply elements containing a single element components mostly of quadratic bezier curves. Would this be a reasonable approach to import these elements into JUCE as Drawable elements? I’m going to experiment with this shortly, but if somebody predicts this will end badly, I would rather save the time.


#5

The only info about the format would be for you to have a look in the code that reads and writes it! It’s pretty simple.

And yes, your SVG approach would work fine. A more direct way to do it might be to convert the drawable into a ValueTree, and store that as xml or binary.