Fontexturer - make .png images from TTF fonts for OpenGL textures

I whipped this up for my own use and decided to share it with java-gaming! It allows you to select an installed font on your system and generate a .png image of that font, so you can use it to draw text with OpenGL. There’s probably already a tool like this out there. Oh well.

Obligatory screenshot:

Download here.

I’ll post the source code if someone requests it.

Awesome idea. Yes, source code would be appreciated.

Can you imagine I once created a font sheet from a real font… through Photoshop? :frowning:

BTW: I believe Font has some method with which you can check if a particular character can actually be rendered by that font; using it you can skip all entries that would result in a square. Of course you may have a good reason to leave them in, like having a predictable character location.

I kept the nonprintable characters in there so that I could directly look up the coordinates based on the char value without manipulating it (lazy, I know…) but here’s the source so people can make any changes they’d like!

Also notably absent is support for bold/italic fonts and there’re probably still a few bugs lingering around.

Is it for monospace (fixed width) fonts only?

Wow, thanks! It can be very useful! Now I only have to learn how to use texture fonts in java! I’ll try! ;D

It works with any font. But, it’s hard to use it with proportional fonts because it doesn’t save the dimension information for the characters.

If anyone’s interested, here’s a modified PreviewPanel::save method which writes another file called “metrics.ftx” containing the x/y/width/height of the bounding box for all the characters it puts in the .png image.


public void save(File file, String fmt) throws IOException {
	BufferedImage buf = new BufferedImage(sideLength, sideLength, BufferedImage.TYPE_INT_ARGB);
	Graphics2D g = (Graphics2D) buf.getGraphics();
	
	drawCharacters(g, Color.white);
	ImageIO.write(buf, fmt, file);
	
	DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(file.getParent(), "metrics.ftx")));
	
	for (char ch = MIN_CH; ch <= MAX_CH; ch++) {
		TextLayout layout = new TextLayout(String.valueOf(ch), font,
				g.getFontRenderContext());
		Rectangle2D rect = layout.getBounds();
		
		float cx = (float) rect.getX();
		float cy = boxSize - g.getFontMetrics().getDescent();
		
		dos.writeFloat(cx);
		dos.writeFloat(cy);
		dos.writeFloat((float) rect.getWidth());
		dos.writeFloat((float) rect.getHeight());
	}
	
	dos.close();
	g.dispose();
}

For a more complete font support you can take a look at the BMFont format which also supports kerning.
The TWL Theme Editor has a font generator tool (Tools -> create font) with 3 different rendering methods:

  • freetype2: best quality
  • AWT drawString
  • AWT vector: most effects

Well, way to show me up. :stuck_out_tongue:

Everyone go use that one now!

I don’t like webstart. Will use OP’s ;D

Thanks for sharing :slight_smile: