Font
class represents fonts, which are used to
render text in a visible way.
A font provides the information needed to map sequences of
characters to sequences of glyphs
and to render sequences of glyphs on Graphics
and
Component
objects.
'g'
,
LATIN SMALL LETTER G, is a character.
A glyph is a shape used to render a character or a sequence of characters. In simple writing systems, such as Latin, typically one glyph represents one character. In general, however, characters and glyphs do not have one-to-one correspondence. For example, the character 'á' LATIN SMALL LETTER A WITH ACUTE, can be represented by two glyphs: one for 'a' and one for '´'. On the other hand, the two-character string "fi" can be represented by a single glyph, an "fi" ligature. In complex writing systems, such as Arabic or the South and South-East Asian writing systems, the relationship between characters and glyphs can be more complicated and involve context-dependent selection of glyphs as well as glyph reordering. A font encapsulates the collection of glyphs needed to render a selected set of characters as well as the tables needed to map sequences of characters to corresponding sequences of glyphs.
Physical fonts are the actual font libraries containing glyph data
and tables to map from character sequences to glyph sequences, using a font
technology such as TrueType or PostScript Type 1.
All implementations of the Java Platform must support TrueType fonts;
support for other font technologies is implementation dependent.
Physical fonts may use names such as Helvetica, Palatino, HonMincho, or
any number of other font names.
Typically, each physical font supports only a limited set of writing
systems, for example, only Latin characters or only Japanese and Basic
Latin.
The set of available physical fonts varies between configurations.
Applications that require specific fonts can bundle them and instantiate
them using the createFont
method.
Logical fonts are the five font families defined by the Java platform which must be supported by any Java runtime environment: Serif, SansSerif, Monospaced, Dialog, and DialogInput. These logical fonts are not actual font libraries. Instead, the logical font names are mapped to physical fonts by the Java runtime environment. The mapping is implementation and usually locale dependent, so the look and the metrics provided by them vary. Typically, each logical font name maps to several physical fonts in order to cover a large range of characters.
Peered AWT components, such as Label
and
TextField
, can only use logical fonts.
For a discussion of the relative advantages and disadvantages of using physical or logical fonts, see the Physical and Logical Fonts in The Java Tutorials document.
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.
Glyphs may not always be rendered with the requested properties (e.g, font and style) due to platform limitations such as the absence of suitable platform fonts to implement a logical font.
The getAllFonts
method
of 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
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 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 getFont
method should
be used if the FONT attribute might be present. See java.
for more information.
Several attributes will cause additional rendering overhead
and potentially invoke layout. If a Font
has such
attributes, the
method
will return true.hasLayoutAttributes()
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.'
Modifier and Type | Class and Description |
---|---|
private static class |
Modifier and Type | Field and Description |
---|---|
public static final int | BOLD
The bold style constant. |
public static final int | CENTER_BASELINE
The baseline used in ideographic scripts like Chinese, Japanese, and Korean when laying out text. |
private transient boolean | |
public static final String | DIALOG
A String constant for the canonical family name of the logical font "Dialog". |
public static final String | DIALOG_INPUT
A String constant for the canonical family name of the logical font "DialogInput". |
private static final int | |
private transient SoftReference | |
private transient Font2DHandle | |
private int | fontSerializedDataVersion
The |
private Hashtable | fRequestedAttributes
This is now only used during serialization. |
public static final int | HANGING_BASELINE
The baseline used in Devanagari and similar scripts when laying out text. |
pack-priv transient int | |
private transient boolean | |
private static final AffineTransform | |
public static final int | ITALIC
The italicized style constant. |
public static final int | LAYOUT_LEFT_TO_RIGHT
A flag to layoutGlyphVector indicating that text is left-to-right as determined by Bidi analysis. |
private static final int | LAYOUT_MASK
These attributes are handled by layout. |
public static final int | LAYOUT_NO_LIMIT_CONTEXT
A flag to layoutGlyphVector indicating that text in the char array after the indicated limit should not be examined. |
public static final int | LAYOUT_NO_START_CONTEXT
A flag to layoutGlyphVector indicating that text in the char array before the indicated start should not be examined. |
public static final int | LAYOUT_RIGHT_TO_LEFT
A flag to layoutGlyphVector indicating that text is right-to-left as determined by Bidi analysis. |
public static final String | MONOSPACED
A String constant for the canonical family name of the logical font "Monospaced". |
protected String | name
The logical name of this |
private transient boolean | |
private transient long | |
private transient FontPeer | peer
The platform specific font information. |
public static final int | PLAIN
The plain style constant. |
protected float | pointSize
The point size of this |
private static final int | PRIMARY_MASK
These attributes are considered primary by the FONT attribute. |
private static final int | RECOGNIZED_MASK
Font recognizes all attributes except FONT. |
public static final int | ROMAN_BASELINE
The baseline used in most Roman scripts when laying out text. |
public static final String | SANS_SERIF
A String constant for the canonical family name of the logical font "SansSerif". |
private static final int | SECONDARY_MASK
These attributes are considered secondary by the FONT attribute. |
private static final long | serialVersionUID
Use serialVersionUID from JDK 1.1 for interoperability. |
public static final String | SERIF
A String constant for the canonical family name of the logical font "Serif". |
protected int | size
The point size of this |
private static final float[] | |
protected int | style
The style of this |
public static final int | TRUETYPE_FONT
Identify a font resource of type TRUETYPE. |
public static final int | TYPE1_FONT
Identify a font resource of type TYPE1. |
private transient AttributeValues |
Access | Constructor and Description |
---|---|
public | Font(String
the font name. This can be a font face name or a font
family name, and may represent either a logical font or a physical
font found in this name, int GraphicsEnvironment .
The family names for logical fonts are: Dialog, DialogInput,
Monospaced, Serif, or SansSerif. Pre-defined String constants exist
for all of these names, for example, DIALOG . If name is
null , the logical font name of the new
Font as returned by getName() is set to
the name "Default".the style constant for the style, int Font
The style argument is an integer bitmask that may
be PLAIN , or a bitwise union of BOLD and/or
ITALIC (for example, ITALIC or BOLD|ITALIC ).
If the style argument does not conform to one of the expected
integer bitmasks then the style is set to PLAIN .the point size of the size)Font Creates a new |
private | |
private | |
private | |
private | |
public | Font(Map<? extends AttributedCharacterIterator.
the attributes to assign to the new
attributes)Font , or null Creates a new |
protected | |
private |
Modifier and Type | Method and Description |
---|---|
private static void | |
private static void | |
public boolean | Returns: true if this Font has a glyph for this
character; false otherwise.the character for which a glyph is needed c)Checks if this |
public boolean | Returns: true if this Font has a glyph for the
character; false otherwise.the character (Unicode code point) for which a glyph
is needed. codePoint)Checks if this |
public int | Returns: an offset intostr that points
to the first character in str that this
Font cannot display; or -1 if
this Font can display all characters in
str .a str)String objectIndicates whether or not this |
public int | Returns: an offset intotext that points
to the first character in text that this
Font cannot display; or -1 if
this Font can display all characters in
text .the specified array of text, int char valuesthe specified starting offset (in
start, int char s) into the specified array of
char valuesthe specified ending offset (in
limit)char s) into the specified array of
char valuesIndicates whether or not this |
public int | Returns: an offset intoiter that points
to the first character in iter that this
Font cannot display; or -1 if
this Font can display all characters in
iter .a iter, int CharacterIterator objectthe specified starting offset into the specified
start, int CharacterIterator .the specified ending offset into the specified
limit)CharacterIterator .Indicates whether or not this |
private static File | |
public static Font | Returns: a newFont created with the specified font type.the type of the fontFormat, InputStream Font , which is
TRUETYPE_FONT if a TrueType resource is specified.
or TYPE1_FONT if a Type 1 resource is specified.an fontStream)InputStream object representing the
input data for the font.Returns a new |
public static Font | Returns: a newFont created with the specified font type.the type of the fontFormat, File Font , which is
TRUETYPE_FONT if a TrueType resource is
specified or TYPE1_FONT if a Type 1 resource is
specified.
So long as the returned font, or its derived fonts are referenced
the implementation may continue to access fontFile
to retrieve font data. Thus the results are undefined if the file
is changed, or becomes inaccessible.
To make the a fontFile)File object representing the
input data for the font.Returns a new |
private static Font[] | |
public static Font[] | Returns: a newFont[] .an fontStream)InputStream object representing the
input data for the font or font collection.Returns a new array of |
public static Font[] | Returns: a newFont[] .a fontFile)File object containing the
input data for the font or font collection.Returns a new array of |
public GlyphVector | Returns: a newGlyphVector created with the
specified String and the specified
FontRenderContext .the specified frc, String FontRenderContext the specified str)String Creates a |
public GlyphVector | Returns: a newGlyphVector created with the
specified array of characters and the specified
FontRenderContext .the specified frc, char[] FontRenderContext the specified array of characters chars)Creates a |
public GlyphVector | Returns: a newGlyphVector created with the
specified CharacterIterator and the specified
FontRenderContext .the specified frc, CharacterIterator FontRenderContext the specified ci)CharacterIterator Creates a |
public GlyphVector | Returns: a newGlyphVector created with the
specified integer array and the specified
FontRenderContext .the specified frc, int[] FontRenderContext the specified integer array glyphCodes)Creates a |
public static Font | |
private FontLineMetrics | |
public Font | Returns: a newFont object.the style for the new style, float Font the size for the new size)Font Creates a new |
public Font | Returns: a newFont object.the style for the new style, AffineTransform Font the trans)AffineTransform associated with the
new Font Creates a new |
public Font | Returns: a newFont object.the size for the new size)Font .Creates a new |
public Font | Returns: a newFont object.the trans)AffineTransform associated with the
new Font Creates a new |
public Font | Returns: a newFont object.the style for the new style)Font Creates a new |
public Font | Returns: a newFont object.a map of attributes enabled for the new
attributes)Font Creates a new |
public boolean | Returns: true if the objects are the same
or if the argument is a Font object
describing the same font as this object;
false otherwise.the obj)Object to compareOverrides java. Font object to the specified
Object .
|
public Map | Returns: the attributes map of thisFont .Returns a map of font attributes available in this
|
private AttributeValues | |
public AttributedCharacterIterator. | Returns: an array containing the keys of all the attributes supported by thisFont .Returns the keys of all the attributes supported by this
|
public byte | Returns: the baseline appropriate for the specified character.a character used to identify the writing system c)Returns the baseline appropriate for displaying this character. |
public String | Returns: aString that is the family name of this
Font .Returns the family name of this |
public String | |
pack-priv final String | |
public static Font | Returns: a newFont created with the specified
attributesthe attributes to assign to the new
attributes)Font Returns a |
public static Font | |
public static Font | |
private Font2D | |
public String | Returns: aString representing the font face name of
this Font .Returns the font face name of this |
public String | Returns: aString representing the font face name,
localized for the specified locale.a locale for which to get the font face name l)Returns the font face name of the |
private FontPeer | |
public float | Returns: the angle of the ITALIC style of thisFont .Returns the italic angle of this |
private float | |
public LineMetrics | getLineMetrics(String
the specified str, FontRenderContext String the specified frc)FontRenderContext Returns a |
public LineMetrics | Returns: aLineMetrics object created with the
specified arguments.the specified str, int String the initial offset of beginIndex, int str the end offset of limit, FontRenderContext str the specified frc)FontRenderContext Returns a |
public LineMetrics | Returns: aLineMetrics object created with the
specified arguments.an array of characters chars, int the initial offset of beginIndex, int chars the end offset of limit, FontRenderContext chars the specified frc)FontRenderContext Returns a |
public LineMetrics | Returns: aLineMetrics object created with the
specified arguments.the specified ci, int CharacterIterator the initial offset in beginIndex, int ci the end offset of limit, FontRenderContext ci the specified frc)FontRenderContext Returns a |
public Rectangle2D | Returns: aRectangle2D that is the bounding box
for the character with the maximum bounds.the specified frc)FontRenderContext Returns the bounds for the character with the maximum
bounds as defined in the specified |
public int | Returns: the glyphCode of thisFont .Returns the glyphCode which is used when this |
public String | Returns: aString representing the logical name of
this Font .Returns the logical name of this |
public int | |
public String | Returns: aString representing the postscript name of
this Font .Returns the postscript name of this |
public int | Returns: the point size of thisFont in 1/72 of an
inch units.Returns the point size of this |
public float | Returns: the point size of thisFont as a
float value.Returns the point size of this |
public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
specified String in the specified
FontRenderContext .the specified str, FontRenderContext String the specified frc)FontRenderContext Returns the logical bounds of the specified |
public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
specified String in the specified
FontRenderContext .the specified str, int String the initial offset of beginIndex, int str the end offset of limit, FontRenderContext str the specified frc)FontRenderContext Returns the logical bounds of the specified |
public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
specified array of characters in the specified
FontRenderContext .an array of characters chars, int the initial offset in the array of
characters beginIndex, int the end offset in the array of characters limit, FontRenderContext the specified frc)FontRenderContext Returns the logical bounds of the specified array of characters
in the specified |
public Rectangle2D | Returns: aRectangle2D that is the bounding box of the
characters indexed in the specified CharacterIterator
in the specified FontRenderContext .the specified ci, int CharacterIterator the initial offset in beginIndex, int ci the end offset in limit, FontRenderContext ci the specified frc)FontRenderContext Returns the logical bounds of the characters indexed in the
specified |
public int | |
public AffineTransform | |
public int | Returns: a hashcode value for thisFont .Overrides java. Font .
|
public boolean | Returns: true if the font has layout attributesReturn true if this Font contains attributes that require extra layout processing. |
private static boolean | |
public boolean | Returns: true if this Font has
uniform line metrics; false otherwise.Checks whether or not this |
private void | |
private static native void | |
public boolean | Returns: true if this Font object's
style is BOLD;
false otherwise.Indicates whether or not this |
public boolean | Returns: true if this Font object's
style is ITALIC;
false otherwise.Indicates whether or not this |
public boolean | Returns: true if this Font has a
PLAIN style;
false otherwise.Indicates whether or not this |
public boolean | Returns: true if this Font object
has a non-identity AffineTransform attribute.
false otherwise.Indicates whether or not this |
public GlyphVector | Returns: a newGlyphVector representing the text between
start and limit, with glyphs chosen and positioned so as to best represent
the textthe specified frc, char[] FontRenderContext the text to layout text, int the start of the text to use for the start, int GlyphVector the limit of the text to use for the limit, int GlyphVector control flags as described above flags)Returns a new |
private void | |
public static boolean | Returns: true if the specified text will need special layout.the text. chars, int the index of the first char to examine. start, int the ending index, exclusive. end)Returns true if any part of the specified text is from a
complex script for which the implementation will need to invoke
layout processing in order to render correctly when using
|
public String | Returns: aString representation of this
Font object.Overrides java. Font object to a String
representation.
|
private void | writeObject(ObjectOutputStream
the s)ObjectOutputStream to writeWrites default serializable fields to a stream. |