com.sun.lwuit
Class Font

java.lang.Object
  extended by com.sun.lwuit.Font

public class Font
extends java.lang.Object

A simple abstraction of platform fonts and library fonts that enables the library to use more elaborate fonts unsupported by a specific device. This abstraction also supports bitmap fonts using an Ant task (more details about the unifier are explained in the javadoc overview document).

A bitmap font can be created manually but that is tedious, normally you would use the Ant task as illustrated bellow to produce a resource file containing the supported bitmap font. For further detail read the overview document and Resources.

<target name="pre-init">
     <taskdef classpath="ImageUnifier.jar" classname="com.sun.jwt.resource.Builder" name="build" />
     <build dest="src/font.res">
        <font src="images/arial.ttf" bold="true" italic="true" size="11" />
        <font logicalName="Dialog" />
    </build>
</target>

The following attributes can be expressed for a font ant task:


Field Summary
static int FACE_MONOSPACE
          Constant allowing us to author portable system fonts
static int FACE_PROPORTIONAL
          Constant allowing us to author portable system fonts
static int FACE_SYSTEM
          Constant allowing us to author portable system fonts
static int SIZE_LARGE
          Constant allowing us to author portable system fonts
static int SIZE_MEDIUM
          Constant allowing us to author portable system fonts
static int SIZE_SMALL
          Constant allowing us to author portable system fonts
static int STYLE_BOLD
          Constant allowing us to author portable system fonts
static int STYLE_ITALIC
          Constant allowing us to author portable system fonts
static int STYLE_PLAIN
          Constant allowing us to author portable system fonts
static int STYLE_UNDERLINED
          Constant allowing us to author portable system fonts
 
Method Summary
 void addContrast(byte value)
          Increase the contrast of the bitmap font for rendering on top of a surface whose color is darker.
 int charsWidth(char[] ch, int offset, int length)
          Return the width of the given characters in this font instance
 int charWidth(char ch)
          Return the width of the specific character when rendered alone
static void clearBitmapCache()
          Bitmap fonts are cached this method allows us to flush the cache thus allows us to reload a font
static Font create(java.lang.String lookup)
          Creates a new font instance based on the platform specific string name of the font.
static Font createBitmapFont(Image bitmap, int[] cutOffsets, int[] charWidth, java.lang.String charsets)
          Creates a bitmap font with the given arguments
static Font createBitmapFont(java.lang.String name, Image bitmap, int[] cutOffsets, int[] charWidth, java.lang.String charsets)
          Creates a bitmap font with the given arguments and places said font in the cache
static Font createSystemFont(int face, int style, int size)
          Creates a system native font in a similar way to common MIDP fonts
static Font createTrueTypeFont(java.io.InputStream stream)
          Creates a true type font from the given stream if the underlying platform supports truetype font loading.
 boolean equals(java.lang.Object o)
           
static Font getBitmapFont(java.lang.String fontName)
          Returns a previously loaded bitmap font from cache
 java.lang.String getCharset()
          Returns a string containing all the characters supported by this font.
static Font getDefaultFont()
          Return the global default font instance
 int getFace()
          Return Optional operation returning the font face for system fonts
 int getHeight()
          Return the total height of the font
 java.lang.Object getNativeFont()
          Returns the internal implementation specific font object
 int getSize()
          Return Optional operation returning the font size for system fonts
 int getStyle()
          Return Optional operation returning the font style for system fonts
static boolean isBitmapFontEnabled()
          Indicates whether bitmap fonts should be enabled when loading or the fallback system font should be used instead.
static boolean isCreationByStringSupported()
          Returns true if the underlying platform allows creating a font based on a user submitted string.
static boolean isTrueTypeFileSupported()
          Returns true if the underlying platform supports loading truetype fonts from a file stream.
static void setBitmapFontEnabled(boolean enabled)
          Indicates whether bitmap fonts should be enabled by default when loading or the fallback system font should be used instead.
static void setDefaultFont(Font f)
          Sets the global default font instance
 int stringWidth(java.lang.String str)
          Return the width of the given string in this font instance
 int substringWidth(java.lang.String str, int offset, int len)
          Return the width of the given string subset in this font instance
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FACE_MONOSPACE

public static final int FACE_MONOSPACE
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

FACE_PROPORTIONAL

public static final int FACE_PROPORTIONAL
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

FACE_SYSTEM

public static final int FACE_SYSTEM
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

SIZE_LARGE

public static final int SIZE_LARGE
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

SIZE_MEDIUM

public static final int SIZE_MEDIUM
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

SIZE_SMALL

public static final int SIZE_SMALL
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

STYLE_BOLD

public static final int STYLE_BOLD
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

STYLE_ITALIC

public static final int STYLE_ITALIC
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

STYLE_UNDERLINED

public static final int STYLE_UNDERLINED
Constant allowing us to author portable system fonts

See Also:
Constant Field Values

STYLE_PLAIN

public static final int STYLE_PLAIN
Constant allowing us to author portable system fonts

See Also:
Constant Field Values
Method Detail

getBitmapFont

public static Font getBitmapFont(java.lang.String fontName)
Returns a previously loaded bitmap font from cache

Parameters:
fontName - the font name is the logical name of the font
Returns:
the font object
See Also:
clearBitmapCache()

clearBitmapCache

public static void clearBitmapCache()
Bitmap fonts are cached this method allows us to flush the cache thus allows us to reload a font


isTrueTypeFileSupported

public static boolean isTrueTypeFileSupported()
Returns true if the underlying platform supports loading truetype fonts from a file stream.

Returns:
true if the underlying platform supports loading truetype fonts from a file stream

isCreationByStringSupported

public static boolean isCreationByStringSupported()
Returns true if the underlying platform allows creating a font based on a user submitted string.

Returns:
true if the underlying platform allows creating a font based on a user submitted string

createTrueTypeFont

public static Font createTrueTypeFont(java.io.InputStream stream)
                               throws java.io.IOException
Creates a true type font from the given stream if the underlying platform supports truetype font loading.

Parameters:
stream - input stream containing the font
Returns:
the font object to create
Throws:
java.io.IOException - if font loading fails

create

public static Font create(java.lang.String lookup)
Creates a new font instance based on the platform specific string name of the font. This method isn't supported on some platforms.

Parameters:
lookup - a set of platform specific names delimited by commas, the first succefully loaded font will be used
Returns:
newly created font or null if creation failed

addContrast

public void addContrast(byte value)
Increase the contrast of the bitmap font for rendering on top of a surface whose color is darker. This is useful when drawing anti-aliased bitmap fonts using a light color (e.g. white) on top of a dark surface (e.g. black), the font often breaks down if its contrast is not increased due to the way alpha blending appears to the eye.

Notice that this method only works in one way, contrast cannot be decreased properly in a font and it should be cleared and reloaed with a Look and Feel switch.

Parameters:
value - the value to increase

createBitmapFont

public static Font createBitmapFont(java.lang.String name,
                                    Image bitmap,
                                    int[] cutOffsets,
                                    int[] charWidth,
                                    java.lang.String charsets)
Creates a bitmap font with the given arguments and places said font in the cache

Parameters:
name - the name for the font in the cache
bitmap - a transparency map in red and black that indicates the characters
cutOffsets - character offsets matching the bitmap pixels and characters in the font
charWidth - The width of the character when drawing... this should not be confused with the number of cutOffset[o + 1] - cutOffset[o]. They are completely different since a character can be "wider" and "seep" into the next region. This is especially true with italic characters all of which "lean" outside of their bounds.
charsets - the set of characters in the font
Returns:
a font object to draw bitmap fonts

createBitmapFont

public static Font createBitmapFont(Image bitmap,
                                    int[] cutOffsets,
                                    int[] charWidth,
                                    java.lang.String charsets)
Creates a bitmap font with the given arguments

Parameters:
bitmap - a transparency map in red and black that indicates the characters
cutOffsets - character offsets matching the bitmap pixels and characters in the font
charWidth - The width of the character when drawing... this should not be confused with the number of cutOffset[o + 1] - cutOffset[o]. They are completely different since a character can be "wider" and "seep" into the next region. This is especially true with italic characters all of which "lean" outside of their bounds.
charsets - the set of characters in the font
Returns:
a font object to draw bitmap fonts

createSystemFont

public static Font createSystemFont(int face,
                                    int style,
                                    int size)
Creates a system native font in a similar way to common MIDP fonts

Parameters:
face - One of FACE_SYSTEM, FACE_PROPORTIONAL, FACE_MONOSPACE
style - one of STYLE_PLAIN, STYLE_ITALIC, STYLE_BOLD
size - One of SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE
Returns:
A newly created system font instance

charsWidth

public int charsWidth(char[] ch,
                      int offset,
                      int length)
Return the width of the given characters in this font instance

Parameters:
ch - array of characters
offset - characters offsets
length - characters length
Returns:
the width of the given characters in this font instance

substringWidth

public int substringWidth(java.lang.String str,
                          int offset,
                          int len)
Return the width of the given string subset in this font instance

Parameters:
str - the given string
offset - the string offset
len - the len od string
Returns:
the width of the given string subset in this font instance

stringWidth

public int stringWidth(java.lang.String str)
Return the width of the given string in this font instance

Parameters:
str - the given string *
Returns:
the width of the given string in this font instance

charWidth

public int charWidth(char ch)
Return the width of the specific character when rendered alone

Parameters:
ch - the specific character
Returns:
the width of the specific character when rendered alone

getHeight

public int getHeight()
Return the total height of the font

Returns:
the total height of the font

getDefaultFont

public static Font getDefaultFont()
Return the global default font instance

Returns:
the global default font instance

setDefaultFont

public static void setDefaultFont(Font f)
Sets the global default font instance

Parameters:
f - the global default font instance

getFace

public int getFace()
Return Optional operation returning the font face for system fonts

Returns:
Optional operation returning the font face for system fonts

getSize

public int getSize()
Return Optional operation returning the font size for system fonts

Returns:
Optional operation returning the font size for system fonts

getStyle

public int getStyle()
Return Optional operation returning the font style for system fonts

Returns:
Optional operation returning the font style for system fonts

getCharset

public java.lang.String getCharset()
Returns a string containing all the characters supported by this font. Will return null for system fonts.

Returns:
String containing the characters supported by a bitmap font or null otherwise.

setBitmapFontEnabled

public static void setBitmapFontEnabled(boolean enabled)
Indicates whether bitmap fonts should be enabled by default when loading or the fallback system font should be used instead. This allows easy toggling of font loading.

Parameters:
enabled - true to enable bitmap font loading (if they exist in the resource)

isBitmapFontEnabled

public static boolean isBitmapFontEnabled()
Indicates whether bitmap fonts should be enabled when loading or the fallback system font should be used instead. This allows easy toggling of font loading.

Returns:
true by default indicating that bitmap font loading is enabled

getNativeFont

public java.lang.Object getNativeFont()
Returns the internal implementation specific font object

Returns:
platform specific font object for use by implementation classes or native code

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object