Sign in to follow this  

[java] A Java BitmapFontGenerator

This topic is 4110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, Weeks ago I've been hitting the forums for a bitmap font generator, but unfortunately I've found only a few ones, and for windows :( But, I've stumbled across NeHe's Lesson 13, which was ported by Mark Bernard (known here as CaptainJester =). His code had to deal with the Font class in Java, and generate the bitmap on the fly to create a display list for each character, etc... So, I took the liberty of using his code and wrap just this bitmap generation bit into a single method. And I post it here in case someone is in need of something similar. I hope Capn' won't mind it ;) Here it is:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

public final class BitmapFontGenerator {
    
    static final Color OPAQUE_WHITE = new Color(0xFFFFFFFF, true);
    static final Color TRANSPARENT_BLACK = new Color(0x00000000, true);
    
    public static void main(String args[]) {
        BitmapFontGenerator instance = new BitmapFontGenerator();
        instance.buildFont("Courier New", 24, 512, "/root/Desktop/");
    }
    
    public void buildFont(String fontName, int fontSize, int bitmapSize, String path) {
        
        Font font;
        BufferedImage fontImage;
        
        boolean sizeFound = false;
        boolean directionSet = false;
        int delta = 0;
        
        while ( !sizeFound ) {
            
            font = new Font(fontName, Font.PLAIN, fontSize);
            fontImage = new BufferedImage(bitmapSize, bitmapSize, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = (Graphics2D) fontImage.getGraphics();
            g.setFont(font);
            FontMetrics fm = g.getFontMetrics();
            int width = fm.stringWidth("W");
            int height = fm.getHeight();
            int lineWidth = (width > height) ? width * 16 : height * 16;
            if ( !directionSet ) {
                delta = ( lineWidth > bitmapSize ) ? -2 : 2;
                directionSet = true;
            }
            if ( delta > 0 ) {
                if ( lineWidth < bitmapSize ) {
                    fontSize += delta;
                } else {
                    sizeFound = true;
                    fontSize -= delta;
                }
            } else if ( delta < 0 ) {
                if ( lineWidth > bitmapSize ) {
                    fontSize += delta;
                } else {
                    sizeFound = true;
                    fontSize -= delta;
                }
            }
        }
        
        font = new Font(fontName, Font.BOLD, fontSize);  // Font Name
        fontImage = new BufferedImage(bitmapSize, bitmapSize, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g = (Graphics2D)fontImage.getGraphics();
        g.setFont(font);
        g.setColor(OPAQUE_WHITE);
        g.setBackground(TRANSPARENT_BLACK);
        FontMetrics fm = g.getFontMetrics();
        for ( int i = 0 ; i < 256 ; i++ ) {
            int x = i % 16;
            int y = i / 16;
            char ch[] = {(char)i};
            String temp = new String(ch);
            g.drawString(temp, (x * 32) + 1, (y * 32) + fm.getAscent());
        }
        
        try {
            
            ImageIO.write(fontImage, "png", new FileOutputStream(path + fontName + ".png"));
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
    }
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Son of Cain
Hello,

But, I've stumbled across NeHe's Lesson 13, which was ported by Mark Bernard (known here as CaptainJester =). His code had to deal with the Font class in Java, and generate the bitmap on the fly to create a display list for each character, etc...

So, I took the liberty of using his code and wrap just this bitmap generation bit into a single method. And I post it here in case someone is in need of something similar. I hope Capn' won't mind it ;)


Not at all. I am glad you found it useful. I hope other people find it useful too. :)

Share this post


Link to post
Share on other sites

This topic is 4110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this