Jump to content
  • Advertisement
Sign in to follow this  
Son of Cain

[java] A Java BitmapFontGenerator

This topic is 4326 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!