racle.com/technetwork/java/javase/tech/faq-jsp-138165.html">Internationalization FAQ document.
Font Faces and Names
A
Font
can have many faces, such as heavy, medium, oblique, gothic and regular. All of these faces have similar typographic design.
There are three different names that you can get from a Font
object. The logical font name is simply the name that was used to construct the font. The font face name, or just font name for short, is the name of a particular font face, like Helvetica Bold. The family name is the name of the font family that determines the typographic design across several faces, like Helvetica.
The Font
class represents an instance of a font face from a collection of font faces that are present in the system resources of the host system. As examples, Arial Bold and Courier Bold Italic are font faces. There can be several Font
objects associated with a font face, each differing in size, style, transform and font features.
The {@link GraphicsEnvironment#getAllFonts() getAllFonts} methodof the GraphicsEnvironment
class returns an array of all font faces available in the system. These font faces are returned as Font
objects with a size of 1, identity transform and default font features. These base fonts can then be used to derive new Font
objects with varying sizes, styles, transforms and font features via the deriveFont
methods in this class.
Font and TextAttribute
Font
supports most TextAttribute
s. This makes some operations, such as rendering underlined text, convenient since it is not necessary to explicitly construct a TextLayout
object. Attributes can be set on a Font by constructing or deriving it using a Map
of TextAttribute
values.
The values of some TextAttributes
are not serializable, and therefore attempting to serialize an instance of Font
that has such values will not serialize them. This means a Font deserialized from such a stream will not compare equal to the original Font that contained the non-serializable attributes. This should very rarely pose a problem since these attributes are typically used only in special circumstances and are unlikely to be serialized.
FOREGROUND
and BACKGROUND
use Paint
values. The subclass Color
is serializable, while GradientPaint
and TexturePaint
are not. CHAR_REPLACEMENT
uses GraphicAttribute
values. The subclasses ShapeGraphicAttribute
and ImageGraphicAttribute
are not serializable. INPUT_METHOD_HIGHLIGHT
uses InputMethodHighlight
values, which are not serializable. See {@link java.awt.im.InputMethodHighlight}.
Clients who create custom subclasses of Paint
and GraphicAttribute
can make them serializable and avoid this problem. Clients who use input method highlights can convert these to the platform-specific attributes for that highlight on the current platform and set them on the Font as a workaround.
The Map
-based constructor and deriveFont
APIs ignore the FONT attribute, and it is not retained by the Font; the static {@link #getFont} method shouldbe used if the FONT attribute might be present. See {@link java.awt.font.TextAttribute#FONT} for more information.
Several attributes will cause additional rendering overhead and potentially invoke layout. If a Font
has such attributes, the {@link #hasLayoutAttributes()}
method will return true.
Note: Font rotations can cause text baselines to be rotated. In order to account for this (rare) possibility, font APIs are specified to return metrics and take parameters 'in baseline-relative coordinates'. This maps the 'x' coordinate to the advance along the baseline, (positive x is forward along the baseline), and the 'y' coordinate to a distance along the perpendicular to the baseline at 'x' (positive y is 90 degrees clockwise from the baseline vector). APIs for which this is especially important are called out as having 'baseline-relative coordinates.'