mwt
Class Font

java.lang.Object
  extended by mwt.Font

public class Font
extends java.lang.Object

The font class represents fonts, which are used to render text in a visible way.

MWT allows to use custom bitmap fonts or system fonts, depending on how the font is created.



System Native Fonts

System native fonts are created using Font(int, int, int, int).
You can change the size, color, face and style using the appropriate values.

The main drawback of system fonts is that they may not fit with the look and feel of your application as they are device dependent.

A system native font uses internally Font.



Custom Bitmap Fonts

Custom bitmap fonts are created using Font(Image, char[], int[], int) or Font(Image[], char[], int).

A custom bitmap font is an immutable object, that means, you can't change the size or color.
Please check the MicroFont-Makercommand line tool.
 
System Native Fonts
Custom Bitmap Fonts
Size

Accepts constants values

SIZE_MEDIUM Any value 17 (pixels)
SIZE_SMALL 15 (pixels)
SIZE_LARGE 19 (pixels)
Styles
Accepts constants values STYLE_PLAIN Not available (you can use different bitmap fonts for each style)
STYLE_BOLD
STYLE_ITALIC
STYLE_UNDERLINED
Face

Accepts constants values

FACE_SYSTEM Not available, however, you don't need this when using custom fonts
FACE_MONOSPACE
FACE_PROPORTIONAL
Color
Any 0xA1C632 Any 0xA1C632
Anchor
Accepts constants values Component.ALIGN_TOP_LEFT Accepts constants values Component.ALIGN_TOP_LEFT
Component.ALIGN_TOP_CENTER Component.ALIGN_TOP_CENTER
Component.ALIGN_TOP_RIGHT Component.ALIGN_TOP_RIGHT
Component.ALIGN_MIDDLE_LEFT Component.ALIGN_MIDDLE_LEFT
Component.ALIGN_MIDDLE_CENTER Component.ALIGN_MIDDLE_CENTER
Component.ALIGN_MIDDLE_RIGHT Component.ALIGN_MIDDLE_RIGHT
Component.ALIGN_BOTTOM_LEFT Component.ALIGN_BOTTOM_LEFT
Component.ALIGN_BOTTOM_CENTER Component.ALIGN_BOTTOM_CENTER
Component.ALIGN_BOTTOM_RIGHT Component.ALIGN_BOTTOM_RIGHT

Extending

Classes that extend a Font must override the following methods:

The contructor must call the system native font Font(InputStream, char[], int, int, int).

Example:
 class MyFont extends Font {
   int newAttribute = ...;
   
   public MyFont(int newAttribute) {
     super(0,0,0,0);
     this.newAttribute = newAttribute;
   }
   
   protected void copy(MyFont f) {
     super.copy(f);
     f.newAttribute = this.newAttribute;
   }
   
   private MyFont() {}
   public Font clone() {
     MyFont f = new MyFont();
     copy(f);
     return f;
   }
   
   public int getHeight() {
     // new implementation
   }
   
   public int getWidth(String s) {
     // new implementation
   }
   
   protected void write(Graphics g, String text, int x, int y, int anchor) {
     // new implementation
   }
 }
 


Field Summary
static int FACE_MONOSPACE
          The "monospace" font face.
static int FACE_PROPORTIONAL
          The "proportional" font face.
static int FACE_SYSTEM
          The "system" font face.
static int SIZE_LARGE
          The "large" system-dependent font size.
static int SIZE_MEDIUM
          The "medium" system-dependent font size.
static int SIZE_SMALL
          The "small" system-dependent font size.
static int STYLE_BOLD
          The bold style constant.
static int STYLE_ITALIC
          The italicized style constant.
static int STYLE_PLAIN
          The plain style constant.
static int STYLE_UNDERLINED
          The underlined style constant.
static int TYPE_MAPPED
           
static int TYPE_STRIP
           
static int TYPE_SYSTEM
           
 
Constructor Summary
Font(javax.microedition.lcdui.Image[] images, char[] charset, int charSpacing)
          Creates a font giving the char images and specifying the charset.
Font(javax.microedition.lcdui.Image img, char[] charset, int[] widths, int charSpacing)
          Creates a font-strip.
Font(java.io.InputStream is, char[] charset, int charSpacing, int size, int color)
          Deprecated.  
Font(int color, int face, int size, int style)
          Creates a system native font.
 
Method Summary
 Font clone()
          Gets a clone of this font.
protected  void copy(Font font)
          Copies this font into the given font.
 void getCharset(char[] buffer, int offset, int length)
          Copies the charset into the given buffer at the current offset.
 int getCharsetLength()
          Gets the charset length or -1 if it is not available.
 int getColor()
          Gets the color of the font.
 int getFace()
          Gets the face of the font.
 int getHeight()
          Gets the font height.
 void getImages(javax.microedition.lcdui.Image[] images, int offset, int length)
          Only for TYPE_MAPPED fonts, otherwise throws a RuntimeException Copies the images into the given image array at the current offset.
 int getSize()
          Gets the size of the font.
 int getStyle()
          Gets the style of the font.
 int getType()
          Gets the font type: TYPE_MAPPED, TYPE_STRIP or TYPE_SYSTEM
 int getWidth(java.lang.String s)
          Gets the width for the given string.
 boolean isBitmapFont()
          Checks if this font is a bitmap font.
 void setColor(int color)
          Sets the color of the font if it's not a bitmap font
 void setFace(int face)
          Sets the face of the font.
 void setSize(int size)
          Sets the size of the font if it's not a bitmap font
 void setStyle(int style)
          Sets the style of the font.
protected  void write(javax.microedition.lcdui.Graphics g, java.lang.String s, int x, int y, int anchor)
          Writes the string into the given graphics object.
 void write(javax.microedition.lcdui.Graphics g, java.lang.String s, int x, int y, int width, int height, int anchor)
          Writes the string into the given graphics object.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STYLE_PLAIN

public static final int STYLE_PLAIN
The plain style constant.

See Also:
Constant Field Values

STYLE_BOLD

public static final int STYLE_BOLD
The bold style constant.

See Also:
Constant Field Values

STYLE_ITALIC

public static final int STYLE_ITALIC
The italicized style constant.

See Also:
Constant Field Values

STYLE_UNDERLINED

public static final int STYLE_UNDERLINED
The underlined style constant.

See Also:
Constant Field Values

SIZE_SMALL

public static final int SIZE_SMALL
The "small" system-dependent font size.

See Also:
Constant Field Values

SIZE_MEDIUM

public static final int SIZE_MEDIUM
The "medium" system-dependent font size.

See Also:
Constant Field Values

SIZE_LARGE

public static final int SIZE_LARGE
The "large" system-dependent font size.

See Also:
Constant Field Values

FACE_SYSTEM

public static final int FACE_SYSTEM
The "system" font face.

See Also:
Constant Field Values

FACE_MONOSPACE

public static final int FACE_MONOSPACE
The "monospace" font face.

See Also:
Constant Field Values

FACE_PROPORTIONAL

public static final int FACE_PROPORTIONAL
The "proportional" font face.

See Also:
Constant Field Values

TYPE_SYSTEM

public static final int TYPE_SYSTEM
See Also:
Constant Field Values

TYPE_MAPPED

public static final int TYPE_MAPPED
See Also:
Constant Field Values

TYPE_STRIP

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

Font

public Font(int color,
            int face,
            int size,
            int style)
Creates a system native font.


Font

public Font(java.io.InputStream is,
            char[] charset,
            int charSpacing,
            int size,
            int color)
     throws java.io.IOException
Deprecated. 

Creates a bitmap font using an InputStream formatted using MicroFont-Maker tool.
Optionally, you can specify which chars must be loaded.
NOTE: this method uses Image.createImage(javax.microedition.lcdui.Image) implementation which in some devices causes problems. Un such case, use Font(Image[], char[], int) instead.

Parameters:
is - the InputStream formatted using MicroFont-Maker tool
charset - The charset specifying which chars must be loaded or null to load the entire charset
charSpacing - The space within chars (negative values are valid)
size - The size value or 0 to use default size
color - The color value
Throws:
java.io.IOException - might be thrown by the InputStream

Font

public Font(javax.microedition.lcdui.Image[] images,
            char[] charset,
            int charSpacing)
Creates a font giving the char images and specifying the charset.

Parameters:
images -
charset - The charset
charSpacing - The space within chars (negative values are valid)
Since:
1.1

Font

public Font(javax.microedition.lcdui.Image img,
            char[] charset,
            int[] widths,
            int charSpacing)
Creates a font-strip.

Parameters:
img -
widths - array of each char width
charset -
charSpacing -
Since:
1.2
Method Detail

clone

public Font clone()
Gets a clone of this font.

Overrides:
clone in class java.lang.Object
See Also:
Extending a Font

copy

protected void copy(Font font)
Copies this font into the given font.

See Also:
Extending a Font

isBitmapFont

public final boolean isBitmapFont()
Checks if this font is a bitmap font.


getType

public final int getType()
Gets the font type: TYPE_MAPPED, TYPE_STRIP or TYPE_SYSTEM


getColor

public final int getColor()
Gets the color of the font.


setColor

public final void setColor(int color)
Sets the color of the font if it's not a bitmap font


getFace

public final int getFace()
Gets the face of the font.


setFace

public final void setFace(int face)
Sets the face of the font.


getSize

public final int getSize()
Gets the size of the font.


setSize

public final void setSize(int size)
Sets the size of the font if it's not a bitmap font


getStyle

public final int getStyle()
Gets the style of the font.


setStyle

public final void setStyle(int style)
Sets the style of the font.


getCharsetLength

public int getCharsetLength()
Gets the charset length or -1 if it is not available.
The default implementation, returns -1 if this is a system native font, otherwise this returns the charset length.


getCharset

public void getCharset(char[] buffer,
                       int offset,
                       int length)
Copies the charset into the given buffer at the current offset.
If getCharsetLength() returns -1, this does nothing.


getImages

public void getImages(javax.microedition.lcdui.Image[] images,
                      int offset,
                      int length)
Only for TYPE_MAPPED fonts, otherwise throws a RuntimeException Copies the images into the given image array at the current offset.
The images length is the same as getCharsetLength()

Parameters:
images -
offset -
length -
Since:
1.2

getHeight

public int getHeight()
Gets the font height.


getWidth

public int getWidth(java.lang.String s)
Gets the width for the given string.


write

public final void write(javax.microedition.lcdui.Graphics g,
                        java.lang.String s,
                        int x,
                        int y,
                        int width,
                        int height,
                        int anchor)
Writes the string into the given graphics object.
It uses the width and height parameters to fit the string according to the anchor.
The anchor value must be a Component ALIGN constant. This method uses write(Graphics, String, int, int, int).


write

protected void write(javax.microedition.lcdui.Graphics g,
                     java.lang.String s,
                     int x,
                     int y,
                     int anchor)
Writes the string into the given graphics object. The anchor value must be a Component ALIGN constant.



Copyright © 2007 MWT-Team. All Rights Reserved.