Embedding a custom TTF



I was following the method described at: http://www.rawmaterialsoftware.com/viewtopic.php?f=2&t=2119

CustomTypeface customTypeface;
customTypeface.setCharacteristics(font.getTypefaceName(), font.getAscent(), font.isBold(), font.isItalic(), ’ ');
customTypeface.addGlyphsFromOtherTypeface( *font.getTypeface(), 0, 256);

File file = File(outputFilePath);
FileOutputStream* os = file.createOutputStream();


I am using this method to create the font itself (after the file has been added to BinaryBuilder’s input dir):

It works fine, until I delete the font from the system (go to Windows Fonts folder and delete it).

What am I doing wrong?


I managed to load it using the Typeface::Ptr in LookAndFeel.h and returning it in getTypefaceForFont().

Now my problem is that my text in AttributedString::draw looks messed up (random letters) when loading the embedded font (plain Font in Label/TextEditor/… and even GlyphArrangement looks good):

When loading the system font, it looks good.

Why is it bad only when using AttributedString::draw?


[code]File file = File(outputFilePath);
FileOutputStream* os = file.createOutputStream();


Are you leaking that stream? If so, that’d leave you with a corrupted file.

I’ve no idea if that code snippet is causing the real problem, but it should have been written like this:

{ FileOutputStream os (File (outputFilePath)); customTypeface.writeToStream (os); }

I’m getting so bored of endlessly repeating myself about using RAII/scoped pointers…


I am not leaking the stream:

Besides - why would it work for GlyphArrangement if its corrupted?


AttributedStrings are rendered using TextLayout.
TextLayout is unlike all the other text rendering code found in Juce. It allows for the rendering of complex languages like Arabic, Hebrew, Hindi, Thai, etc.
TextLayout uses Operating System APIs on Mac OS X 10.5 (and later) and Windows Vista SP2 PU (and later) for text layout. These APIs only access fonts that are in the operating system font collection.
CustomTypefaces are not apart of the OS font collection so I don’t think you’ll be able to render them properly unless you make modifications in juce_TextLayout.cpp to disable using native OS text APIs.

You could try the following modifications:

void TextLayout::createLayout (const AttributedString& text, float maxWidth)
    width = maxWidth;
    justification = text.getJustification();

    //if (! createNativeLayout (text))
        createStandardLayout (text);


By making this modification, you are eliminating Complex Text support. You should still be able to render simple text with various fonts and various colors.
Setting JUCE_USE_DIRECTWRITE to 0 will have a similar effect as the modifications above on Windows.


Thank you for the elaborate explanation.
Since I only need this font for 2 lines I will stick to GA and not tamper with Juce’s font rendering system.


I have the same problem. It would be nice to be able to disable the complex text support without the need to hack internal JUCE code.


One thing that I still don’t get - can I load my custom typeface without overriding LookAndFeel::getTypefaceForFont()?