org.jrabbit.base.graphics.font.renderer
Class UnicodeRenderer

java.lang.Object
  extended by org.jrabbit.base.graphics.font.renderer.AcceleratedFontRenderer
      extended by org.jrabbit.base.graphics.font.renderer.UnicodeRenderer
All Implemented Interfaces:
Createable, DataController, Destroyable, FontRenderer

public class UnicodeRenderer
extends AcceleratedFontRenderer

JRabbit uses a couple of resources from the Slick game library (with slight modifications). UnicodeRenderer is one of these. Basically everything is directly from Slick, so all credit goes there. However, I've made a few changes to A) suit jRabbit's needs more precisely, and to B) slim things down, make the class more OO and have less warnings. A few thoughts on the use of this class: A UnicodeRenderer is very flexible, capable class, but very heavy duty to load. It can load any .TTF file, or access any installed TrueType fonts on the user's computer, but it then has to calculate and render all resources needed to put that font in-game. This is obviously very expensive and will cause a noticeable pause in a game. UnicodeRenderer also has a limitation beyond initialization time. The font loaded is a solid white color. Color filters can be applied, but you cannot detail the source texture like you can with AngelCode fonts. However, the use of it does have benefits. As mentioned before, it can load .TTF files and put them into fonts. These can be at any scale, and can have basic text transforms (bold, italic, etc.) applied to them before conversion. The same TrueType font can be used for multiple fonts, as well. Additionally, a UnicodeRenderer can render virtually any character the font supports. This includes those outside of the ASCII range.

Author:
Nathan Sweet, modified by Chris Molini.

Nested Class Summary
protected  class UnicodeRenderer.Glyph
          Represents a character onscreen.
protected static class UnicodeRenderer.GlyphPage
          A GlyphPage controls a group of Glyphs.
static interface UnicodeRenderer.GlyphPainter
          GlyphPainter is an interface that lets the developer customize how Unicode fonts are rendered onto textures.
 
Nested classes/interfaces inherited from class org.jrabbit.base.graphics.font.renderer.AcceleratedFontRenderer
AcceleratedFontRenderer.TextDL
 
Field Summary
protected  int ascent
          The ascent of the font.
protected  int descent
          The decent of the font.
protected  java.awt.Font font
          The AWT font that is being rendered.
protected  int glyphPageHeight
          The height of the glyph page generated.
protected  java.util.List<UnicodeRenderer.GlyphPage> glyphPages
          The pages that have been loaded for this font.
protected  int glyphPageWidth
          The width of the glyph page generated.
protected  UnicodeRenderer.GlyphPainter glyphPainter
          The default glyph painter.
protected  UnicodeRenderer.Glyph[][] glyphs
          The glyphs that are available in this font.
protected static java.util.Comparator<UnicodeRenderer.Glyph> heightComparator
          A custom Comparator for sorting Glyphs by height, tallest first.
protected  int leading
          The leading edge of the font.
protected static int MAX_CHAR
          The highest character value allowed.
protected  UnicodeRenderer.Glyph missingGlyph
          The glyph to display for missing glyphs in code points.
protected  int paddingAdvanceX
          The padding applied in pixels to horizontal advance for each glyph.
protected  int paddingAdvanceY
          The padding applied in pixels to vertical advance for each glyph.
protected  int paddingBottom
          The padding applied in pixels to the bottom of the glyph rendered area.
protected  int paddingLeft
          The padding applied in pixels to the left of the glyph rendered area.
protected  int paddingRight
          The padding applied in pixels to the right of the glyph rendered area.
protected  int paddingTop
          The padding applied in pixels to the top of the glyph rendered area.
protected static int PAGE_SIZE
          The number of glyphs that can be on a page.
protected static int PAGES
          The number of pages allowed.
protected  java.util.List<UnicodeRenderer.Glyph> queuedGlyphs
          The glyphs queued up to be rendered.
protected  int spaceWidth
          The width of a space for the font.
protected  java.lang.String ttfFileRef
          The reference to the True Type Font file that has kerning information.
 
Fields inherited from class org.jrabbit.base.graphics.font.renderer.AcceleratedFontRenderer
accelerate, baseDL, displayLists, eldestDL, eldestDLID, maxDL
 
Constructor Summary
UnicodeRenderer(java.awt.Font font)
          Creates a new UnicodeRenderer.
UnicodeRenderer(java.awt.Font font, int size, boolean bold, boolean italic)
          Creates a new UnicodeRenderer.
UnicodeRenderer(java.lang.String filepath)
          Creates a UnicodeRenderer from the specified TrueTypeFont file.
UnicodeRenderer(java.lang.String filepath, FontProfile p)
          Create a new UnicodeRenderer based on a TTF file.
UnicodeRenderer(java.lang.String filepath, int size)
          Creates a UnicodeRenderer from the indicated file at the specified size.
 
Method Summary
 void addAsciiGlyphs()
          Queues the glyphs in the ASCII character set (codepoints 32 through 255) to be loaded.
 void addGlyphs(int startCodePoint, int endCodePoint)
          Queues the glyphs in the specified codepoint range (inclusive) to be loaded.
 void addGlyphs(java.lang.String text)
          Queues the glyphs in the specified text to be loaded.
 void addNeheGlyphs()
          Queues the glyphs in the NEHE character set (codepoints 32 through 128) to be loaded.
 void clearGlyphs()
          Clears all loaded and queued glyphs.
 void create()
          Loads all registered glyphs for this font, and then initializes all needed display lists.
 void destroy()
          Releases all resources used by this UnicodeFont.
protected  void drawString(java.lang.String text)
          Renders the supplied text.
 java.awt.Font getFont()
          Returns the TrueTypeFont for this UnicodeFont.
protected  UnicodeRenderer.Glyph getGlyph(int glyphCode, int codePoint, java.awt.Rectangle bounds, java.awt.font.GlyphVector vector, int index)
          Returns the glyph for the specified codePoint.
protected  java.awt.Rectangle getGlyphBounds(java.awt.font.GlyphVector vector, int index, int codePoint)
          Finds the dimensions of the specified glyph.
 java.util.List<UnicodeRenderer.GlyphPage> getGlyphPages()
          Returns the GlyphPages for this UnicodeRenderer.
 int getSpaceWidth()
          Finds the current width of the space character.
 UnicodeRenderer.GlyphPainter glyphPainter()
          Accesses the GlyphPainter.
protected  void initializeFont(java.awt.Font baseFont, int size, boolean bold, boolean italic)
          Initialize the font to be used based on the indicated configuration.
 float lineHeight()
          Accesses the height of each line of text.
static java.awt.Font loadFont(java.lang.String font)
          Loads a java Font from a supplied String.
 boolean loadGlyphs()
          Loads all queued glyphs to the backing textures.
 boolean loadGlyphs(int maxGlyphsToLoad)
          Loads up to the specified number of queued glyphs to the backing textures.
protected  float measureString(java.lang.String text)
          Directly measures a String of text to find out how wide it is.
 void setGlyphPainter(UnicodeRenderer.GlyphPainter painter)
          Redefines the GlyphPainter.
 void setSpaceWidth(int space)
          Forcibly defines the width of a space.
 boolean valid()
          Checks to see if the UnicodeRenderer is intact.
 
Methods inherited from class org.jrabbit.base.graphics.font.renderer.AcceleratedFontRenderer
render, useAcceleration, widthOf
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_CHAR

protected static final int MAX_CHAR
The highest character value allowed.

See Also:
Constant Field Values

PAGE_SIZE

protected static final int PAGE_SIZE
The number of glyphs that can be on a page.

See Also:
Constant Field Values

PAGES

protected static final int PAGES
The number of pages allowed.

See Also:
Constant Field Values

heightComparator

protected static final java.util.Comparator<UnicodeRenderer.Glyph> heightComparator
A custom Comparator for sorting Glyphs by height, tallest first.


font

protected java.awt.Font font
The AWT font that is being rendered.


ttfFileRef

protected java.lang.String ttfFileRef
The reference to the True Type Font file that has kerning information.


ascent

protected int ascent
The ascent of the font.


descent

protected int descent
The decent of the font.


leading

protected int leading
The leading edge of the font.


spaceWidth

protected int spaceWidth
The width of a space for the font.


glyphs

protected final UnicodeRenderer.Glyph[][] glyphs
The glyphs that are available in this font.


glyphPages

protected final java.util.List<UnicodeRenderer.GlyphPage> glyphPages
The pages that have been loaded for this font.


queuedGlyphs

protected final java.util.List<UnicodeRenderer.Glyph> queuedGlyphs
The glyphs queued up to be rendered.


paddingTop

protected int paddingTop
The padding applied in pixels to the top of the glyph rendered area.


paddingLeft

protected int paddingLeft
The padding applied in pixels to the left of the glyph rendered area.


paddingBottom

protected int paddingBottom
The padding applied in pixels to the bottom of the glyph rendered area.


paddingRight

protected int paddingRight
The padding applied in pixels to the right of the glyph rendered area.


paddingAdvanceX

protected int paddingAdvanceX
The padding applied in pixels to horizontal advance for each glyph.


paddingAdvanceY

protected int paddingAdvanceY
The padding applied in pixels to vertical advance for each glyph.


missingGlyph

protected UnicodeRenderer.Glyph missingGlyph
The glyph to display for missing glyphs in code points.


glyphPageWidth

protected int glyphPageWidth
The width of the glyph page generated.


glyphPageHeight

protected int glyphPageHeight
The height of the glyph page generated.


glyphPainter

protected UnicodeRenderer.GlyphPainter glyphPainter
The default glyph painter.

Constructor Detail

UnicodeRenderer

public UnicodeRenderer(java.lang.String filepath,
                       FontProfile p)
Create a new UnicodeRenderer based on a TTF file.

Parameters:
ttfFileRef - The file system or classpath location of the desired TrueTypeFont file.
p - a FontProfile which is applied to the loaded font.

UnicodeRenderer

public UnicodeRenderer(java.lang.String filepath)
Creates a UnicodeRenderer from the specified TrueTypeFont file. This uses the default font size of 20.

Parameters:
filepath - The path to the desired TrueTypeFont file.

UnicodeRenderer

public UnicodeRenderer(java.awt.Font font)
Creates a new UnicodeRenderer.

Parameters:
font - The AWT font to render

UnicodeRenderer

public UnicodeRenderer(java.awt.Font font,
                       int size,
                       boolean bold,
                       boolean italic)
Creates a new UnicodeRenderer.

Parameters:
font - The AWT font to render
size - The point size of the font to generated
bold - True if the font should be rendered in bold.
italic - True if the font should be rendered in italic.

UnicodeRenderer

public UnicodeRenderer(java.lang.String filepath,
                       int size)
Creates a UnicodeRenderer from the indicated file at the specified size.

Parameters:
filepath - The file to load a .TTF file from.
size - The font size to use for this font.
Method Detail

loadFont

public static final java.awt.Font loadFont(java.lang.String font)
Loads a java Font from a supplied String. The first attempt this method takes is to assume that the String references a .TTF file. If such a file is found, then the font described by the file is loaded. If that fails, then the method attempts to load a font from the system (e.g., if "Arial" is supplied, Java can load the font from the OS). If that fails, a default font is created.

Parameters:
font - The description of the font file.
Returns:
The font created from the reference.

initializeFont

protected void initializeFont(java.awt.Font baseFont,
                              int size,
                              boolean bold,
                              boolean italic)
Initialize the font to be used based on the indicated configuration.

Parameters:
baseFont - The AWT font to render
size - The point size of the font to generated
bold - True if the font should be rendered in bold.
italic - True if the font should be rendered in italic.

setGlyphPainter

public void setGlyphPainter(UnicodeRenderer.GlyphPainter painter)
Redefines the GlyphPainter.

Parameters:
painter - The painter to use for rendering the glyphs.

glyphPainter

public UnicodeRenderer.GlyphPainter glyphPainter()
Accesses the GlyphPainter.

Returns:
The object that is used to render the glyphs.

addGlyphs

public void addGlyphs(int startCodePoint,
                      int endCodePoint)
Queues the glyphs in the specified codepoint range (inclusive) to be loaded. Note that the glyphs are not actually loaded until loadGlyphs() is called. Some characters like combining marks and non-spacing marks can only be rendered with the context of other glyphs. In this case, use addGlyphs(String).

Parameters:
startCodePoint - The code point of the first glyph to add.
endCodePoint - The code point of the last glyph to add.

addGlyphs

public void addGlyphs(java.lang.String text)
Queues the glyphs in the specified text to be loaded. Note that the glyphs are not actually loaded until loadGlyphs() is called.

Parameters:
text - The text that contains all characters desired.

addAsciiGlyphs

public void addAsciiGlyphs()
Queues the glyphs in the ASCII character set (codepoints 32 through 255) to be loaded. Note that the glyphs are not actually loaded until loadGlyphs() is called.


addNeheGlyphs

public void addNeheGlyphs()
Queues the glyphs in the NEHE character set (codepoints 32 through 128) to be loaded. Note that the glyphs are not actually loaded until loadGlyphs() is called.


loadGlyphs

public boolean loadGlyphs()
Loads all queued glyphs to the backing textures. Glyphs that are typically displayed together should be added and loaded at the same time so that they are stored on the same backing texture. This reduces the number of backing texture binds required to draw glyphs.

Returns:
True if the glyphs were loaded entirely. false if not.

loadGlyphs

public boolean loadGlyphs(int maxGlyphsToLoad)
Loads up to the specified number of queued glyphs to the backing textures. This is typically called from the game loop to load glyphs on the fly that were requested for display but have not yet been loaded.

Parameters:
maxGlyphsToLoad - The maximum number of glyphs to be loaded.
Returns:
True if the glyphs were loaded entirely, false if not.

clearGlyphs

public void clearGlyphs()
Clears all loaded and queued glyphs. Also wipes existing OpenGL data.


valid

public boolean valid()
Checks to see if the UnicodeRenderer is intact. This is based on its GlyphPages.

Returns:
True if every GlyphPage's image is still valid; if any are false this returns false.

create

public void create()
Loads all registered glyphs for this font, and then initializes all needed display lists.

Specified by:
create in interface Createable
Overrides:
create in class AcceleratedFontRenderer

destroy

public void destroy()
Releases all resources used by this UnicodeFont. First deletes all display lists, then gets rid of every stored Glyph.


drawString

protected void drawString(java.lang.String text)
Renders the supplied text.

Specified by:
drawString in class AcceleratedFontRenderer
Parameters:
text - The String to render.

getGlyph

protected UnicodeRenderer.Glyph getGlyph(int glyphCode,
                                         int codePoint,
                                         java.awt.Rectangle bounds,
                                         java.awt.font.GlyphVector vector,
                                         int index)
Returns the glyph for the specified codePoint. If the glyph does not exist yet, it is created and queued to be loaded.

Parameters:
glyphCode - The code of the glyph to locate.
codePoint - The code point associated with the glyph.
bounds - The bounds of the glyph on the page.
vector - The vector the glyph is part of.
index - The index of the glyph within the vector.
Returns:
The requested glyph.

getGlyphBounds

protected java.awt.Rectangle getGlyphBounds(java.awt.font.GlyphVector vector,
                                            int index,
                                            int codePoint)
Finds the dimensions of the specified glyph.

Parameters:
vector - The vector the glyph is part of.
index - The index of the glyph within the vector.
codePoint - The code point associated with the glyph.
Returns:
The bounds of the specified glyph.

getSpaceWidth

public int getSpaceWidth()
Finds the current width of the space character.

Returns:
How wide a space is.

setSpaceWidth

public void setSpaceWidth(int space)
Forcibly defines the width of a space.

Parameters:
space - The gap created by the space character.

measureString

protected float measureString(java.lang.String text)
Directly measures a String of text to find out how wide it is.

Specified by:
measureString in class AcceleratedFontRenderer
Parameters:
text - The text to measure.
Returns:
The width of the String when rendered.

lineHeight

public float lineHeight()
Accesses the height of each line of text.

Returns:
The height of a line of text.

getFont

public java.awt.Font getFont()
Returns the TrueTypeFont for this UnicodeFont.

Returns:
The AWT Font being rendered.

getGlyphPages

public java.util.List<UnicodeRenderer.GlyphPage> getGlyphPages()
Returns the GlyphPages for this UnicodeRenderer.

Returns:
The glyph pages that have been loaded into this font.