public class Font extends Object
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:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Dialog, DialogInput, Monospaced, Serif, or SansSerif
Modifier and Type | Field and Description |
---|---|
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
|
Modifier and Type | Method and Description |
---|---|
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
|
int |
charWidth(int codePoint)
Return the width of the specific code point 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(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,
String charsets)
Creates a bitmap font with the given arguments
|
static Font |
createBitmapFont(String name,
Image bitmap,
int[] cutOffsets,
int[] charWidth,
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(InputStream stream)
Creates a true type font from the given stream if the underlying platform supports
truetype font loading.
|
static Font |
createTrueTypeFont(String fontName,
String fileName)
Creates a true type font with the given name/filename (font name might be different from the file name
and is required by some devices e.g.
|
Font |
derive(float sizePixels,
int weight)
Creates a font based on this truetype font with the given pixel, WARNING! This method
will only work in the case of truetype fonts!
Important some platforms e.g. |
boolean |
equals(Object o)
Indicates whether some other object is "equal to" this one.
|
static Font |
getBitmapFont(String fontName)
Returns a previously loaded bitmap font from cache
|
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
|
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(String str)
Return the width of the given string in this font instance
|
int |
substringWidth(String str,
int offset,
int len)
Return the width of the given string subset in this font instance
|
public static final int FACE_MONOSPACE
public static final int FACE_PROPORTIONAL
public static final int FACE_SYSTEM
public static final int SIZE_LARGE
public static final int SIZE_MEDIUM
public static final int SIZE_SMALL
public static final int STYLE_BOLD
public static final int STYLE_ITALIC
public static final int STYLE_UNDERLINED
public static final int STYLE_PLAIN
public Font(Object nativeFont)
public static Font getBitmapFont(String fontName)
fontName
- the font name is the logical name of the fontclearBitmapCache()
public static void clearBitmapCache()
public static boolean isTrueTypeFileSupported()
public static boolean isCreationByStringSupported()
public static Font createTrueTypeFont(InputStream stream) throws IOException
stream
- input stream containing the fontIOException
- if font loading failspublic static Font createTrueTypeFont(String fontName, String fileName)
fontName
- the name of the fontfileName
- the file name of the font as it appears in the src directory of the project, it MUST end with the .ttf extension!public Font derive(float sizePixels, int weight)
sizePixels
- the size of the font in pixelsweight
- PLAIN, BOLD or ITALIC weight based on the constants in this classpublic static Font create(String lookup)
lookup
- a set of platform specific names delimited by commas, the first succefully
loaded font will be usedpublic void addContrast(byte value)
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.
value
- the value to increasepublic static Font createBitmapFont(String name, Image bitmap, int[] cutOffsets, int[] charWidth, String charsets)
name
- the name for the font in the cachebitmap
- a transparency map in red and black that indicates the characterscutOffsets
- character offsets matching the bitmap pixels and characters in the fontcharWidth
- 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 fontpublic static Font createBitmapFont(Image bitmap, int[] cutOffsets, int[] charWidth, String charsets)
bitmap
- a transparency map in red and black that indicates the characterscutOffsets
- character offsets matching the bitmap pixels and characters in the fontcharWidth
- 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 fontpublic static Font createSystemFont(int face, int style, int size)
face
- One of FACE_SYSTEM, FACE_PROPORTIONAL, FACE_MONOSPACEstyle
- one of STYLE_PLAIN, STYLE_ITALIC, STYLE_BOLDsize
- One of SIZE_SMALL, SIZE_MEDIUM, SIZE_LARGEpublic int charsWidth(char[] ch, int offset, int length)
ch
- array of charactersoffset
- characters offsetslength
- characters lengthpublic int substringWidth(String str, int offset, int len)
str
- the given stringoffset
- the string offsetlen
- the len od stringpublic int stringWidth(String str)
str
- the given string *public int charWidth(char ch)
ch
- the specific characterpublic int charWidth(int codePoint)
codePoint
- the specific code pointpublic int getHeight()
public static Font getDefaultFont()
public static void setDefaultFont(Font f)
f
- the global default font instancepublic int getFace()
public int getSize()
public int getStyle()
public String getCharset()
public static void setBitmapFontEnabled(boolean enabled)
enabled
- true to enable bitmap font loading (if they exist in the resource)public static boolean isBitmapFontEnabled()
public boolean equals(Object o)
Object
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
equals
in class Object
o
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap