EasyGlyph – Another iPhone Font System

See the end of this article for the download link..

Update: 1.2 Is now available. It contains important bug fixes and support for drop shadows.

So Apple provides a number of fonts for use in your iPhone games and apps including:

  • American Typewriter
  • American Typewriter Condensed
  • Arial
  • Arial Rounded MT Bold
  • Courier New
  • Georgia
  • Helvetica
  • Marker Felt
  • Times New Roman
  • Trebuchet MS
  • Verdana
  • Zapfino

These are fine, but what if you want to use your own fonts in a game or application? There are a couple ways to do this (http://stackoverflow.com/questions/360751/can-i-embed-a-custom-font-in-an-iphone-application) – but I wasn’t entirely satisfied with these. To do this is somewhat tricky because you have to store the kerning (spacing) information as well as the bitmap for the font, and provide tools for slicing up sprite bitmaps directly on the iPhone. I took a day or so and wrote a tool to rip the windows font of your choice, and create a UIImage, which can then be converted easily to an OpenGL texture if needed. I call it EasyGlyph and the tool is free to use but devoid of any warranties.

First of all, there’s a Windows application that stitches together the font “sheet” image and calculates all the letter sizes and kerning. You start by generating a 32-bit (with Alpha) PNG graphic of the font using a font face, size, and color. You can of course size any font to whatever size you want once its on your iPhone, but depending on the situation you may want your font to have more or less bitmap detail (remember we’re converting it to a bitmap from a vector drawing). Here’s a screenshot of the tool:

EasyGlyph

Here’s what some Windows fonts look like rendered on the iPhone:

screen-capture-4

Mind the spaceships. They’re part of a game, and aren’t relevant to this article. Because of the kerning code, it can also handle complex overlapping fonts like this:

screen-capture-5

When you save a font to disk, you’ll get two files: a PNG file containing the bitmaps and a C document containing the class constructor along with all the coordinate and kerning information for the font:

myFont = [[EasyGlyph alloc] init];
[myFont initFont:@"chicdecay.png":@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890.,-=+:;<>?!'\"[]@#$%^&*()/\\":@"0,0,69,115.937,37;79.783,0,71,115.937,37;157.421,0,71,115.937,36;236.569,0,67,115.937,37;318.129,0,62,115.937,37; ETC...

You’ll need this along with two classes: “EasyGlyph” and “CustomFontChar” which are included. To generate a bitmap of some text that uses the font, use the instance method writeTextToTexture, which returns a UIImage instance:

[myFont writeTextToTexture:@"Hello World!"]

To convert this to a Texture2D, for use in OpenGL applications, you can do something like this:

textTexture = [[[Texture2D alloc] initWithImage:[myFont writeTextToTexture:@"Hello World!"]] retain];

.. and you’re off to the races.

I’m looking for feedback, improvements, insults – whatever you got basically. Once again this is free to use – I only ask you provide some feedback if you got some use out of it. Enjoy!


Tags: , , , , , ,

19 Responses to “EasyGlyph – Another iPhone Font System”

  1. fixyomac says:

    Pretty fucking cool.
    I probably wont use it for another six months, but its on the list.

  2. Ste says:

    Really useful code, and it great with the examples that came in the download (I’ve added in some word wrap stuff too)

    However, I can’t get the app to work properly. Every font I output ends up with the offsets being wrong and a lot of letters looking incorrect. I don’t have a windows machine so I’m running it on mac through mono, I’m wondering if Macs do something differently when it comes to fonts.

  3. Alexei says:

    Hey Ste,

    It’s brand new so there might be quirks. Can you send me the font you were using and I’ll look into it? alexei.white@gmail.com

  4. Alexei says:

    Hey Everyone. EasyGlyph 1.1 is now available.

  5. [...] really hard” to “it’s not actually possible”, with people doing things like rendering fonts into UIImages to do that kind of thing since they can’t sort it out from the documentation. Yoiks! [...]

  6. EasyGlyph – An iPhone Font System…

    You’ve been kicked (a good thing) – Trackback from iPhoneKicks.com – iPhone SDK links, community driven…

  7. [...] EasyGlyph – this is a windows only .NET program to create a spritemap out of a font, plus objective-c code to support using the spritemap in your iPhone application.  This solution is good enough for some users, mainly game developers who don’t mind the inherent limitations, and just need some nicely styled text in small amounts.  You are basically just blitting images instead of text, so you are unable to do anything that is not pre-rendered into the images.  Personally, I don’t like this approach, but it would be rude of me not to include it here, as many of you may find that this is just what you need.  It works very well for very simple needs. [...]

  8. I’ve been looking for something like this, but being a small-time iPhone developer means sticking exclusively to the Mac. A bit odd that you have to go through a Win-exclusive bit of software as long as the iPhone-developer-tools are OSX only.

  9. Alexei says:

    Granted, but the font ripping code seemed a lot easier to write in Win32 than on the mac. I wanted to get this done quickly so I did it that way.

    BTW guys I am about to release an update.

  10. Michael Kaye says:

    I would love to use this but every time I launch EasyGlyph it just crashes with the following message – “This application failed to initialise….”

    Windows XP running Parellels. Shame.

  11. Mariano Guadagnini says:

    Hi, Great work man, this is just what I needed. I had, however, some glitches with memory management, of the ObjC classes you provide.
    At least on SDK 3, an exception is thrown randomly. I traced it:

    On EasyGlyph.m file, line 51 and 56, changed:

    EasyGlyphChar *cf = [[characterDict objectForKey:@"m"] autorelease];

    EasyGlyphChar *cf = [[characterDict objectForKey:ch] autorelease];

    for

    EasyGlyphChar *cf = [[characterDict objectForKey:@"m"] retain];

    EasyGlyphChar *cf = [[characterDict objectForKey:ch] retain];

    As the autorelease pool was trying to release it twice.

    Thanks for your work,

  12. Alexei says:

    Thanks man. I’ll make that change and re-release. Got some general updates to make to the ripper too.

  13. Mr.Gando says:

    Hey! , thanks for your code, just one observation, in the example above, you say :

    textTexture = [[[Texture2D alloc] initWithImage:[myFont writeTextToTexture:@"Hello World!"]] retain];

    Just wanted to point that for most people, can skip the retain part, I am allocating and dealloc a lot of strings with your code, and the retain thing without autorelease pool caused a lot of memory overhead, just wanted to point that out :)

    Thanks again.

  14. Rajneesh says:

    Awsome tool…..

  15. Xartec says:

    Thanks for sharing this! Works like a charm. Colors, borders, shadows etc can easily be added to the PNG afterwards using PhotoShop or similar.

  16. Sam Nova says:

    I ran into a problem today, as we need to use a OpenType font for our project and for some reason it does not show up in the font dialog. Font works fine under other applications (such as Word). I was wondering if there are any possibilities to use these fonts with the tool?

  17. Alexei says:

    Your best bet is to convert the font to TTF using one of the tools out there for doing this.

  18. bubzy says:

    Ok, so im as dumb as a rock.
    i cannot get this included in my project, has anyone got a sample project i can peruse to check my implementation of this code. ive put the header and .m files into the classes folder and the .c and .png files into the resources folder. ive #import(ed) “EasyGlyph.h” but every time i take a stab at what to do im coming up with errors, surely im missing something obvious CURSE YOU OBJECTIVE C!!!

    thanks :) (intermediate c# developer)

  19. pixelkind says:

    Hej, thanks for this great tool.
    Needed to include some special characters (german umlaut) and extended the String in the Visual Studio Project. Maybe editing this String in the Frontend would be nice :) ?
    thanks again, you saved me a lot of headaches!

Leave a Reply



© All rights reserved.