de.enough.polish.util
Class BitMapFontViewer

java.lang.Object
  extended by de.enough.polish.util.BitMapFontViewer

public class BitMapFontViewer
extends Object

Is used for a performant showing of String with a bitmap font.

Copyright Enough Software 2004 - 2009

Author:
Robert Virkus, j2mepolish@enough.de

Field Summary
static int ABSOLUTE_LINE_BREAK
           
static int ARTIFICAL_LINE_BREAK
           
static int COLORIZE_MASK
           
 
Constructor Summary
BitMapFontViewer(Image image, int[] indeces, short[] xPositions, byte[] characterWidths, int fontHeight, int spaceIndex, int verticalPadding)
          Views a specific input string with a specific bitmap font.
BitMapFontViewer(Image image, int color, int[] indeces, short[] xPositions, byte[] characterWidths, int fontHeight, int spaceIndex, int verticalPadding)
          Views a specific input string with a specific bitmap font.
 
Method Summary
protected  Image applyColor(Image img, int mask, int color)
          Applies the color to the pixels in the image that fits the mask
protected  int colorizePixel(int argb, int color)
          Colorizes the given argb pixel to the given color while maintaining the alpha value
 int getFontHeight()
          Returns the height of used font.
 int getHeight()
          Retrieves the height needed for this viewer.
 int getNumberOfLines()
          Retrieves the number of lines which are used to display the embedded text.
 byte[] getUsedCharactersWidths()
          Returns the used character widths.
 int getWidth()
          Retrieves the width needed for this viewer.
 boolean isInitialized()
           
protected  boolean isValidPixel(int argb, int mask)
           
 void layout(int firstLineWidth, int lineWidth, int paddingVertical, int orientationSetting)
          Layouts this text-viewer.
 void layout(int firstLineWidth, int lineWidth, int paddingVertical, int orientationSetting, int maxLines, String maxLinesAppendix, BitMapFont maxLinesAppendixFont)
          Layouts this text-viewer.
 void paint(int x, int y, Graphics g)
          Paints this viewer on the screen.
 void setHorizontalOrientation(int orientation)
          Sets the orientation of the text
 String[] wrap(String text)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ABSOLUTE_LINE_BREAK

public static final int ABSOLUTE_LINE_BREAK
See Also:
Constant Field Values

ARTIFICAL_LINE_BREAK

public static final int ARTIFICAL_LINE_BREAK
See Also:
Constant Field Values

COLORIZE_MASK

public static final int COLORIZE_MASK
See Also:
Constant Field Values
Constructor Detail

BitMapFontViewer

public BitMapFontViewer(Image image,
                        int[] indeces,
                        short[] xPositions,
                        byte[] characterWidths,
                        int fontHeight,
                        int spaceIndex,
                        int verticalPadding)
Views a specific input string with a specific bitmap font.

Parameters:
image - the basic font-image
indeces - array of the x-positions of the to-be-displayed characters
xPositions - array of the x-positions of the to-be-displayed characters
characterWidths - array of the widths of the to-be-displayed characters
fontHeight - the height of the font
spaceIndex - the index of the space character
verticalPadding - the padding between two lines

BitMapFontViewer

public BitMapFontViewer(Image image,
                        int color,
                        int[] indeces,
                        short[] xPositions,
                        byte[] characterWidths,
                        int fontHeight,
                        int spaceIndex,
                        int verticalPadding)
Views a specific input string with a specific bitmap font.

Parameters:
image - the basic font-image
color - the color for the font; -1 when it should be ignored
indeces - array of the x-positions of the to-be-displayed characters
xPositions - array of the x-positions of the to-be-displayed characters
characterWidths - array of the widths of the to-be-displayed characters
fontHeight - the height of the font
spaceIndex - the index of the space character
verticalPadding - the padding between two lines
Method Detail

applyColor

protected Image applyColor(Image img,
                           int mask,
                           int color)
Applies the color to the pixels in the image that fits the mask

Parameters:
img - the image to get the pixels from
mask - the mask
color - the color
Returns:
the resulting image with the applied color

isValidPixel

protected boolean isValidPixel(int argb,
                               int mask)
Parameters:
argb - the argb value
mask - the mask
Returns:
true, if the argb value matches the mask, otherwise false

colorizePixel

protected int colorizePixel(int argb,
                            int color)
Colorizes the given argb pixel to the given color while maintaining the alpha value

Parameters:
argb - the argb pixel
color - the color
Returns:
the colorized pixel

paint

public void paint(int x,
                  int y,
                  Graphics g)
Paints this viewer on the screen.

Parameters:
x - the x-position for the text. When the orientation is LEFT, x defines the left-border; when the orientation is RIGHT, x defines the rigth border; when the orientation is HCENTER, x defines the middle between left and right border.
y - the top y-position of the first line.
g - the graphics object

getWidth

public int getWidth()
Retrieves the width needed for this viewer.

Returns:
the width in pixels

getHeight

public int getHeight()
Retrieves the height needed for this viewer.

Returns:
the height in pixels

layout

public void layout(int firstLineWidth,
                   int lineWidth,
                   int paddingVertical,
                   int orientationSetting)
Layouts this text-viewer.

Parameters:
firstLineWidth - the available width for the first line
lineWidth - the available width for the following lines
paddingVertical - the space between lines
orientationSetting - the orientation of this viewer, either Grapics.LEFT, Graphics.RIGHT or Graphics.HCENTER
See Also:
getHeight(), getWidth()

layout

public void layout(int firstLineWidth,
                   int lineWidth,
                   int paddingVertical,
                   int orientationSetting,
                   int maxLines,
                   String maxLinesAppendix,
                   BitMapFont maxLinesAppendixFont)
Layouts this text-viewer.

Parameters:
firstLineWidth - the available width for the first line
lineWidth - the available width for the following lines
paddingVertical - the space between lines
orientationSetting - the orientation of this viewer, either Grapics.LEFT, Graphics.RIGHT or Graphics.HCENTER
maxLines - the maximum number of lines, -1 when this should be ignored
maxLinesAppendix - the appendix that should be added to the last line when the line number is greater than maxLines, null when it should be ignored
maxLinesAppendixFont - the font that should be used for the appendix
See Also:
getHeight(), getWidth()

setHorizontalOrientation

public void setHorizontalOrientation(int orientation)
Sets the orientation of the text

Parameters:
orientation - the horizontal text orientation like Graphics.LEFT, Graphics.RIGHT or Graphics.HCENTER

getFontHeight

public int getFontHeight()
Returns the height of used font.

Returns:
the height of the bitmap-font in pixels.

getNumberOfLines

public int getNumberOfLines()
Retrieves the number of lines which are used to display the embedded text.

Returns:
the number of lines.

getUsedCharactersWidths

public byte[] getUsedCharactersWidths()
Returns the used character widths. This is usefull to have information about the actual position of each character. You can modify the array because only a copy is returned.

Returns:
a copy of the internal byte array representing the widths of the included characters

isInitialized

public boolean isInitialized()
Returns:
true when this font is loaded correctly

wrap

public String[] wrap(String text)
Parameters:
text -
Returns:
an array of strings