I found the code provided to serialize a font was easy to use as long as you are sure the font is currently installed in your system. If the typeface is not installed on your system, it still completes successfully but you don’t have a real serialized font in the output file. I added the following lines of code just before the Typeface constructor to verify that the requested font exists before proceeding.
StringArray FontNames = Font::findAllTypefaceNames();
if(!FontNames.contains(FontName))
{
String error ("\nError: The font " + FontName + " does not exist in the system\n");
printf ((const char*) error);
return 0;
}
Here’s the entire program:
[code]// FontBuilder.cpp : Defines the entry point for the console application.
//
#include “stdafx.h”
#include “juce.h”
int main (int argc, char* argv[])
{
printf ("\n\n--------------------------------\n Font Serialiser by Niall Moody\n--------------------------------\n\n");
if (argc != 3)
{
printf (" Usage: FontSerialiser <filename> <fontname>\n\n");
printf (" FontSerialiser will turn a font into a compressed binary file.\n\n\n");
return 0;
}
// because we're not using the proper application startup procedure, we need to call
// this explicitly here to initialise some of the time-related stuff..
SystemStats::initialiseStats();
const File destDirectory (File::getCurrentWorkingDirectory().getChildFile (argv[1]));
String FontName(argv[2]);
OutputStream *fontfile = destDirectory.createOutputStream();
if (fontfile == 0)
{
String error ("\nError : Couldn't open ");
error << destDirectory.getFullPathName() << " for writing.\n\n";
printf ((const char*) error);
return 0;
}
Typeface *font = 0;
StringArray FontNames = Font::findAllTypefaceNames();
if(!FontNames.contains(FontName))
{
String error ("\nError: The font " + FontName + " does not exist in the system\n");
printf ((const char*) error);
return 0;
}
font = new Typeface(FontName, false, false);
if(!font)
{
String error ("\nError : Where’s the font?\n\n");
printf ((const char*) error);
return 0;
}
//Here's the important part.
for(int i=0;i<256;++i)
font->getGlyph(i);
String op("\nWrote font “);
op << FontName << " to file " << destDirectory.getFullPathName() << " successfully.\n\n”;
printf((const char *)op);
font->serialise(*fontfile);
delete font;
delete fontfile;
printf("\n(You might want to use Binary Builder to turn this file into a c++ file now)\n\n ");
return 0;
}
[/code]