Skip to main content
gamedev.net
🔒 Locked

[java] A Java BitmapFontGenerator

Started by Son of Cain Sep 14, 2006 at 2:38 PM 1 replies 1.3k views
Original Post
Son of Cain
Son of Cain
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&#47;Desktop&#<span class="java-number">47</span>;"</span>);
    }
    
    <span class="java-visibilitymodifier">public</span> <span class="java-primitives">void</span> buildFont(String fontName, <span class="java-primitives">int</span> fontSize, <span class="java-primitives">int</span> bitmapSize, String path) {
        
        Font font;
        BufferedImage fontImage;
        
        <span class="java-primitives">boolean</span> sizeFound = <span class="java-keyword">false</span>;
        <span class="java-primitives">boolean</span> directionSet = <span class="java-keyword">false</span>;
        <span class="java-primitives">int</span> delta = <span class="java-number">0</span>;
        
        <span class="java-keyword">while</span> ( !sizeFound ) {
            
            font = <span class="java-keyword">new</span> Font(fontName, Font.PLAIN, fontSize);
            fontImage = <span class="java-keyword">new</span> BufferedImage(bitmapSize, bitmapSize, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D g = (Graphics2D) fontImage.getGraphics();
            g.setFont(font);
            FontMetrics fm = g.getFontMetrics();
            <span class="java-primitives">int</span> width = fm.stringWidth(<span class="java-literal">"W"</span>);
            <span class="java-primitives">int</span> height = fm.getHeight();
            <span class="java-primitives">int</span> lineWidth = (width &amp;gt; height) ? width * <span class="java-number">16</span> : height * <span class="java-number">16</span>;
            <span class="java-keyword">if</span> ( !directionSet ) {
                delta = ( lineWidth &amp;gt; bitmapSize ) ? -<span class="java-number">2</span> : <span class="java-number">2</span>;
                directionSet = <span class="java-keyword">true</span>;
            }
            <span class="java-keyword">if</span> ( delta &amp;gt; <span class="java-number">0</span> ) {
                <span class="java-keyword">if</span> ( lineWidth &amp;lt; bitmapSize ) {
                    fontSize += delta;
                } <span class="java-keyword">else</span> {
                    sizeFound = <span class="java-keyword">true</span>;
                    fontSize -= delta;
                }
            } <span class="java-keyword">else</span> <span class="java-keyword">if</span> ( delta &amp;lt; <span class="java-number">0</span> ) {
                <span class="java-keyword">if</span> ( lineWidth &amp;gt; bitmapSize ) {
                    fontSize += delta;
                } <span class="java-keyword">else</span> {
                    sizeFound = <span class="java-keyword">true</span>;
                    fontSize -= delta;
                }
            }
        }
        
        font = <span class="java-keyword">new</span> Font(fontName, Font.BOLD, fontSize);  <span class="java-comment">// Font Name</span>
        fontImage = <span class="java-keyword">new</span> 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();
        <span class="java-keyword">for</span> ( <span class="java-primitives">int</span> i = <span class="java-number">0</span> ; i &amp;lt; <span class="java-number">256</span> ; i++ ) {
            <span class="java-primitives">int</span> x = i % <span class="java-number">16</span>;
            <span class="java-primitives">int</span> y = i / <span class="java-number">16</span>;
            <span class="java-primitives">char</span> ch[] = {(<span class="java-primitives">char</span>)i};
            String temp = <span class="java-keyword">new</span> String(ch);
            g.drawString(temp, (x * <span class="java-number">32</span>) + <span class="java-number">1</span>, (y * <span class="java-number">32</span>) + fm.getAscent());
        }
        
        <span class="java-keyword">try</span> {
            
            ImageIO.write(fontImage, <span class="java-literal">"png"</span>, <span class="java-keyword">new</span> FileOutputStream(path + fontName + <span class="java-literal">".png"</span>));
            
        } <span class="java-keyword">catch</span> (FileNotFoundException ex) {
            ex.printStackTrace();
        } <span class="java-keyword">catch</span> (IOException ex) {
            ex.printStackTrace();
        }
        
    }
}

</pre></div><!--ENDSCRIPT--> </code></pre></div>
a.k.a javabeats at yahoo.ca
Bimble Bob
Bimble Bob
Great! This will be very useful. Good work.
It's not a bug... it's a feature!
CaptainJester
CaptainJester
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. :)
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.