Custom Font

I have a set of pngs I’d like to use as a font in my application.
Any implementation suggestions?

Here are three possible approaches:

  1. You need to vectorize your PNGs, so they represent shape data. In Java (and most other places?) a font is described purely in terms of glyphs, or shapes. It is not represented as image data. So you need to have shape data and not PNG data. Then… ugh… if you want a proper Font object you’ll have to override java.awt.Font. This requires making your own GlyphVector that returns your new magical shapes at the appropriate time in the appropriate context. In short: it’s not easy.

  2. So making a real java.awt.Font may be overkill; I’m not sure what your needs are. If you just have some letters that you want to render side-by-side, just make your own method similar to:

public void drawText(Graphics2D g,String text,int x,int y) {
    for(int a = 0; a<text.length(); a++) {
        char c = text.charAt(a);
        Image i = getImageForLetter(c);
        g.drawImage(i,x,y,null);
        x+=i.getWidth(null);
    }
}

Tada! you’re rendering your special PNGs. Although not nearly as flexible as shape data, this does have some advantages: if your images are rich in colors and textures, this will work great. But this won’t scale as well as a shape-based Font would.

  1. Or maybe this isn’t really a programming issue. Maybe you want to look at software that makes truetype fonts? Then you can create a real bonafide font to ship with your application? This also sounds like overkill, but I’m not sure what your needs are.

Currently I have something working very much like the second option you mentioned.
I’ll guess I’ll just stay with that for now.

Thanks.