glapp
Class GLFont

java.lang.Object
  extended by glapp.GLFont

public class GLFont
extends java.lang.Object

GLFont uses the Java Font class to create character sets and render text. GLFont can generate text in nearly any size, and in any font that Java supports.


Constructor Summary
GLFont(java.awt.Font f)
          Dynamically create a texture mapped character set with the given Font.
GLFont(java.awt.Font f, float[] fgColor, float[] bgColor)
          Create a texture mapped character set with the given Font, Text color and background color.
 
Method Summary
 void buildFont(int fontTxtrHandle, int textureSize, int fontSize)
          Build the character set display list from the given texture.
static java.awt.image.BufferedImage createCharImage(java.lang.String text, java.awt.Font font, float[] fgColor, float[] bgColor)
          return a BufferedImage containing the given character drawn with the given font.
 java.awt.image.BufferedImage createFontImage(java.awt.Font font, float[] fgColor, float[] bgColor)
          Return a BufferedImage containing 100 printable characters drawn with the given font.
 void destroyFont()
          Clean up the allocated display lists for the character set.
static int getFontSize(java.awt.Font font)
          Return the maximum character size of the given Font.
 int getTexture()
          Return the handle to the texture holding the character set.
static int makeCharTexture(java.awt.Font f, java.lang.String onechar, float[] fgColor, float[] bgColor)
          Return a texture containing the given single character with the Courier font.
 void makeFont(java.awt.Font f, float[] fgColor, float[] bgColor)
          Prepare a texture mapped character set with the given Font, text color and background color.
 int makeFontTexture(java.awt.Font f, float[] fgColor, float[] bgColor)
          Return a texture containing a character set with the given Font arranged in a 10x10 grid of printable characters.
 void print(int x, int y, java.lang.String msg)
          Render a text string in 2D over the scene, using the character set created by this GLFont object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GLFont

public GLFont(java.awt.Font f)
Dynamically create a texture mapped character set with the given Font. Text color will be white on a transparent background.

Parameters:
f - Java Font object

GLFont

public GLFont(java.awt.Font f,
              float[] fgColor,
              float[] bgColor)
Create a texture mapped character set with the given Font, Text color and background color.

Parameters:
f - Java Font object
fgColor - foreground (text) color as rgb or rgba values in range 0-1
bgColor - background color as rgb or rgba values in range 0-1 (set alpha to 0 to make transparent)
Method Detail

getTexture

public int getTexture()
Return the handle to the texture holding the character set.


makeFont

public void makeFont(java.awt.Font f,
                     float[] fgColor,
                     float[] bgColor)
Prepare a texture mapped character set with the given Font, text color and background color. Characters will be textured onto quads and stored in display lists. The base display list id is stored in the fontListBase variable. After makeFont() is run the print() function can be used to render text in this font.

Parameters:
f - the font to draw characters
fgColor - foreground (text) color as rgb or rgba values in range 0-1
bgColor - background color as rgb or rgba values in range 0-1 (set alpha to 0 to make transparent)
See Also:
createFontImage(), print()

makeFontTexture

public int makeFontTexture(java.awt.Font f,
                           float[] fgColor,
                           float[] bgColor)
Return a texture containing a character set with the given Font arranged in a 10x10 grid of printable characters.

Parameters:
f - the font to draw characters
fgColor - foreground (text) color as rgb or rgba values in range 0-1
bgColor - background color as rgb or rgba values in range 0-1 (set alpha to 0 to make transparent)
See Also:
createFontImage(), print()

makeCharTexture

public static int makeCharTexture(java.awt.Font f,
                                  java.lang.String onechar,
                                  float[] fgColor,
                                  float[] bgColor)
Return a texture containing the given single character with the Courier font. TO DO: pass Font as a parameter.

Parameters:
onechar - character to draw into texture

createCharImage

public static java.awt.image.BufferedImage createCharImage(java.lang.String text,
                                                           java.awt.Font font,
                                                           float[] fgColor,
                                                           float[] bgColor)
return a BufferedImage containing the given character drawn with the given font. Character will be drawn on its baseline, and centered horizontally in the image.

Parameters:
text - a single character to render
font - the font to render with
fgColor - foreground (text) color as rgb or rgba values in range 0-1
bgColor - background color as rgb or rgba values in range 0-1 (set alpha to 0 to make transparent)
Returns:

createFontImage

public java.awt.image.BufferedImage createFontImage(java.awt.Font font,
                                                    float[] fgColor,
                                                    float[] bgColor)
Return a BufferedImage containing 100 printable characters drawn with the given font. Characters will be arranged in a 10x10 grid.

Parameters:
text -
font -
imgSize - a power of two (32 64 256 etc)
fgColor - foreground (text) color as rgb or rgba values in range 0-1
bgColor - background color as rgb or rgba values in range 0-1 (set alpha to 0 to make transparent)
Returns:

getFontSize

public static int getFontSize(java.awt.Font font)
Return the maximum character size of the given Font. This will be the max of the vertical and horizontal font dimensions, so can be used to create a square image large enough to hold any character rendered with this Font.

Creates a BufferedImage and Graphics2D graphics context to get font sizes (is there a more efficient way to do this?).

Parameters:
font - Font object describes the font to render with
Returns:
power-of-two texture size large enough to hold the character set

buildFont

public void buildFont(int fontTxtrHandle,
                      int textureSize,
                      int fontSize)
Build the character set display list from the given texture. Creates one quad for each character, with one letter textured onto each quad. Assumes the texture is a 256x256 image containing every character of the charset arranged in a 16x16 grid. Each character is 16x16 pixels. Call destroyFont() to release the display list memory. Should be in ORTHO (2D) mode to render text (see setOrtho()). Special thanks to NeHe and Giuseppe D'Agata for the "2D Texture Font" tutorial (http://nehe.gamedev.net).

Parameters:
charSetImage - texture image containing 100 characters in a 10x10 grid
fontWidth - how many pixels to allow per character on screen
See Also:
destroyFont()

destroyFont

public void destroyFont()
Clean up the allocated display lists for the character set.


print

public void print(int x,
                  int y,
                  java.lang.String msg)
Render a text string in 2D over the scene, using the character set created by this GLFont object.

See Also:
makeFont()