Fonts

The LWUIT library supports bitmap fonts, system fonts, and loadable fonts. System fonts use basic native fonts and are based on the common MIDP fonts. For more detailed information, please see the Font API in the API documentation.

Bitmap fonts generate fonts on the desktop as image files. These image can be used to draw desktop quality fonts on a device without requiring specific support from the device.

Loadable fonts support specifying a font as a name or even as a TrueType font file; if the underlying operating system supports such fonts, the font object would be created.

All fonts can be used in a theme file and are represented using the Font class in LWUIT.

System font

Three basic parameters define a system font:

Face

Valid values are FACE_SYSTEM, FACE_PROPORTIONAL and FACE_MONOSPACE.

Style

Valid values are STYLE_PLAIN, STYLE_ITALIC, STYLE_BOLD.

Size

Valid values are SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGE.

To create a system font, use the following code:

Font.createSystemFont(Font.FACE_SYSTEM,
                      Font.STYLE_BOLD,
                      Font.SIZE_MEDIUM);

To create a bold italic font style, use code similar to the following:

Font.createSystemFont(Font.FACE_SYSTEM,
                      Font.STYLE_BOLD | Font.STYLE_ITALIC,
                      Font.SIZE_MEDIUM);

Dynamic fonts

Different platforms have different font support. For example, phones usually only support system and bitmap fonts, while TVs usually support TrueType fonts but do not work well with bitmap fonts. LWUIT has support for defining fonts in resources that allow a resource to adapt for different devices. To support portability, LWUIT allows specifying a loadable font if such a font is supported by the underlying system, and also allows bundling bitmaps for increased portability. As a fallback a system font is always defined; thus if the native font is not supported or a developer is not interested in using a bitmap font, the system font fallback can always be used. It is possible to define such a font using the Ant task with the following syntax:

<build dest="src/myresourceFile.res">
    <font logicalName=”SansSerif” name=”myFont” size=”20” />
</build>

The following attributes are supported for the font Ant task:

name

Name of the font to load from the resource file (optional: defaults to logical name or file name).

charset

Defaults to the English alphabet, numbers and common signs. Should contain a list of all characters that are supported by a font. For example, if a font is always used for uppercase letters then it would save space to define the charset as: "ABCDEFGHIJKLMNOPQRSTUVWXYZ".

src

Font file in the case of using a file. Defaults to TrueType font. Size is the point size of the font.

bold

Defaults to False. Indicates whether the font should be bold.

trueType

Defaults to True, relevant only when src is used. If set to False, type 1 fonts are assumed.

antiAliasing

Defaults to True. If false, fonts are aliased.

logicalName

The logical name of the font as specified by java.awt.Font in Java SE: Dialog, DialogInput, Monospaced, Serif, or SansSerif.

createBitmap

Defaults to True. If false, no bitmap version of the font is created.

Using fonts in Resource Editor

Resource Editor can use device specific fonts or create bitmap fonts for the devices from any font installed in your desktop operating system. Figure 1 shows the font editing dialog that appears when adding a new font to the resource file.

Figure: Font editing view in Resource Editor

Note:

Using the Resource Editor does not grant you permission to use the fonts commercially in any way. Licensing the right to use a particular font within a mobile application is strictly your responsibility!

Make sure to specify the characters you need from the font (defaults to upper and lower case English with numbers and symbols). Notice that the more characters you pick in the character set, the more RAM the font will consume on the device. Anti-aliasing is built in to the bitmap font. When running under Java 5, the Resource Editor has two anti-aliasing options: "Off" indicates no anti-aliasing in the bitmap font, and "Simple" indicates standard anti-aliasing.