Top Description Inners Fields Constructors Methods
java.awt

public Class Font

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Known Direct Subclasses
javax.swing.plaf.FontUIResource
Imports
java.awt.font.FontRenderContext, .GlyphVector, .LineMetrics, .TextAttribute, .TextLayout, java.awt.geom.AffineTransform, .Point2D, .Rectangle2D, java.awt.peer.FontPeer, java.io.File, .FileOutputStream, .FilePermission, .IOException, .InputStream, .ObjectOutputStream, .OutputStream, .Serial, java.lang.ref.SoftReference, java.nio.file.Files, java.security.AccessController, .PrivilegedExceptionAction, java.text.AttributedCharacterIterator.Attribute, .CharacterIterator, java.util.EventListener, .Hashtable, .Locale, .Map, sun.awt.ComponentFactory, sun.font.AttributeMap, .AttributeValues, .CompositeFont, .CoreMetrics, .CreatedFontTracker, .Font2D, .Font2DHandle, .FontAccess, .FontDesignMetrics, .FontLineMetrics, .FontManager, .FontManagerFactory, .FontUtilities, .GlyphLayout, .StandardGlyphVector

The 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.

Characters and Glyphs

A character is a symbol that represents an item such as a letter, a digit, or punctuation in an abstract way. For example, '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 and Logical Fonts

The Java Platform distinguishes between two kinds of fonts: physical fonts and logical fonts.

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 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.

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 and TextAttribute

Font supports most TextAttributes. 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.

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.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 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.'

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField 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<FontLineMetrics>
private transient Font2DHandle
private int
fontSerializedDataVersion

The Font Serializable Data Form.

private Hashtable<Object, Object>
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 Font, as passed to the constructor.

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 Font in float.

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 Font, rounded to integer.

private static final float[]
protected int
style

The style of this Font, as passed to the constructor.

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

Constructor Summary

AccessConstructor 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 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".
name
,
int
the style constant for the 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.
style
,
int
the point size of the Font
size
)

Creates a new Font from the specified name, style and point size.

private
Font(String name, int style, float sizePts)

private
Font(String name, int style, float sizePts, boolean created, Font2DHandle handle)

private
Font(File fontFile, int fontFormat, boolean isCopy, CreatedFontTracker tracker)

private
Font(AttributeValues values, String oldName, int oldStyle, boolean created, Font2DHandle handle)

public
Font(Map<? extends AttributedCharacterIterator.Attribute, ?>
the attributes to assign to the new Font, or null
attributes
)

Creates a new Font with the specified attributes.

protected
Font(Font
from which to create this Font.
font
)

Creates a new Font from the specified font.

private
Font(Font2D font2D)

Method Summary

Modifier and TypeMethod and Description
private static void
applyStyle(int style, AttributeValues values)

private static void
public boolean

Returns:

true if this Font has a glyph for this character; false otherwise.
canDisplay
(char
the character for which a glyph is needed
c
)

Checks if this Font has a glyph for the specified character.

public boolean

Returns:

true if this Font has a glyph for the character; false otherwise.
canDisplay
(int
the character (Unicode code point) for which a glyph is needed.
codePoint
)

Checks if this Font has a glyph for the specified character.

public int

Returns:

an offset into str that points to the first character in str that this Font cannot display; or -1 if this Font can display all characters in str.
canDisplayUpTo
(String
a String object
str
)

Indicates whether or not this Font can display a specified String.

public int

Returns:

an offset into text that points to the first character in text that this Font cannot display; or -1 if this Font can display all characters in text.
canDisplayUpTo
(char[]
the specified array of char values
text
,
int
the specified starting offset (in chars) into the specified array of char values
start
,
int
the specified ending offset (in chars) into the specified array of char values
limit
)

Indicates whether or not this Font can display the characters in the specified text starting at start and ending at limit.

public int

Returns:

an offset into iter that points to the first character in iter that this Font cannot display; or -1 if this Font can display all characters in iter.
canDisplayUpTo
(CharacterIterator iter, int
the specified starting offset into the specified CharacterIterator.
start
,
int
the specified ending offset into the specified CharacterIterator.
limit
)

Indicates whether or not this Font can display the text specified by the iter starting at start and ending at limit.

private static File
checkFontFile(int fontFormat, File fontFile)

public static Font

Returns:

a new Font created with the specified font type.
createFont
(int
the type of the Font, which is TRUETYPE_FONT if a TrueType resource is specified. or TYPE1_FONT if a Type 1 resource is specified.
fontFormat
,
InputStream
an InputStream object representing the input data for the font.
fontStream
)

Returns a new Font using the specified font type and input data.

public static Font

Returns:

a new Font created with the specified font type.
createFont
(int
the type of the 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 Font available to Font constructors the returned Font must be registered in the GraphicsEnvironment by calling registerFont(Font).

fontFormat
,
File
a File object representing the input data for the font.
fontFile
)

Returns a new Font using the specified font type and the specified font file.

private static Font[]
createFont0(int fontFormat, InputStream fontStream, boolean allFonts, CreatedFontTracker tracker)

public static Font[]

Returns:

a new Font[].
createFonts
(InputStream
an InputStream object representing the input data for the font or font collection.
fontStream
)

Returns a new array of Font decoded from the specified stream.

public static Font[]

Returns:

a new Font[].
createFonts
(File
a File object containing the input data for the font or font collection.
fontFile
)

Returns a new array of Font decoded from the specified file.

public GlyphVector

Returns:

a new GlyphVector created with the specified String and the specified FontRenderContext.
createGlyphVector
(FontRenderContext
the specified FontRenderContext
frc
,
String
the specified String
str
)

Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.

public GlyphVector

Returns:

a new GlyphVector created with the specified array of characters and the specified FontRenderContext.
createGlyphVector
(FontRenderContext
the specified FontRenderContext
frc
,
char[]
the specified array of characters
chars
)

Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.

public GlyphVector

Returns:

a new GlyphVector created with the specified CharacterIterator and the specified FontRenderContext.
createGlyphVector
(FontRenderContext
the specified FontRenderContext
frc
,
CharacterIterator
the specified CharacterIterator
ci
)

Creates a GlyphVector by mapping the specified characters to glyphs one-to-one based on the Unicode cmap in this Font.

public GlyphVector

Returns:

a new GlyphVector created with the specified integer array and the specified FontRenderContext.
createGlyphVector
(FontRenderContext
the specified FontRenderContext
frc
,
int[]
the specified integer array
glyphCodes
)

Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font.

public static Font

Returns:

the Font object that str describes, or a new default Font if str is null.
decode
(String
the name of the font, or null
str
)

Returns the Font that the str argument describes.

private FontLineMetrics
public Font

Returns:

a new Font object.
deriveFont
(int
the style for the new Font
style
,
float
the size for the new Font
size
)

Creates a new Font object by replicating this Font object and applying a new style and size.

public Font

Returns:

a new Font object.
deriveFont
(int
the style for the new Font
style
,
AffineTransform
the AffineTransform associated with the new Font
trans
)

Creates a new Font object by replicating this Font object and applying a new style and transform.

public Font

Returns:

a new Font object.
deriveFont
(float
the size for the new Font.
size
)

Creates a new Font object by replicating the current Font object and applying a new size to it.

public Font

Returns:

a new Font object.
deriveFont
(AffineTransform
the AffineTransform associated with the new Font
trans
)

Creates a new Font object by replicating the current Font object and applying a new transform to it.

public Font

Returns:

a new Font object.
deriveFont
(int
the style for the new Font
style
)

Creates a new Font object by replicating the current Font object and applying a new style to it.

public Font

Returns:

a new Font object.
deriveFont
(Map<? extends AttributedCharacterIterator.Attribute, ?>
a map of attributes enabled for the new Font
attributes
)

Creates a new Font object by replicating the current Font object and applying a new set of font attributes to it.

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.
equals
(Object
the Object to compare
obj
)

Overrides java.lang.Object.equals.

Compares this Font object to the specified Object.
public Map<TextAttribute, ?>

Returns:

the attributes map of this Font.
getAttributes
()

Returns a map of font attributes available in this Font.

private AttributeValues
getAttributeValues()

Return the AttributeValues object associated with this font.

public AttributedCharacterIterator.Attribute[]

Returns:

an array containing the keys of all the attributes supported by this Font.
getAvailableAttributes
()

Returns the keys of all the attributes supported by this Font.

public byte

Returns:

the baseline appropriate for the specified character.
getBaselineFor
(char
a character used to identify the writing system
c
)

Returns the baseline appropriate for displaying this character.

public String

Returns:

a String that is the family name of this Font.
getFamily
()

Returns the family name of this Font.

public String

Returns:

a String representing the family name of the font, localized for the specified locale.
getFamily
(Locale
locale for which to get the family name
l
)

Returns the family name of this Font, localized for the specified locale.

pack-priv final String
public static Font

Returns:

a new Font created with the specified attributes
getFont
(Map<? extends AttributedCharacterIterator.Attribute, ?>
the attributes to assign to the new Font
attributes
)

Returns a Font appropriate to the attributes.

public static Font

Returns:

a Font object that the property name describes, or null if no such property exists.
getFont
(String
the property name
nm
)

Returns a Font object from the system properties list.

public static Font

Returns:

the Font value of the property.
getFont
(String
the case-insensitive property name
nm
,
Font
a default Font to return if property nm is not defined
font
)

Gets the specified Font from the system properties list.

private Font2D
public String

Returns:

a String representing the font face name of this Font.
getFontName
()

Returns the font face name of this Font.

public String

Returns:

a String representing the font face name, localized for the specified locale.
getFontName
(Locale
a locale for which to get the font face name
l
)

Returns the font face name of the Font, localized for the specified locale.

private FontPeer

Returns:

the peer of the Font.
getFontPeer
()

Gets the peer of this Font.

public float

Returns:

the angle of the ITALIC style of this Font.
getItalicAngle
()

Returns the italic angle of this Font.

private float
public LineMetrics

Returns:

a LineMetrics object created with the specified String and FontRenderContext.
getLineMetrics
(String
the specified String
str
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns a LineMetrics object created with the specified String and FontRenderContext.

public LineMetrics

Returns:

a LineMetrics object created with the specified arguments.
getLineMetrics
(String
the specified String
str
,
int
the initial offset of str
beginIndex
,
int
the end offset of str
limit
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns a LineMetrics object created with the specified arguments.

public LineMetrics

Returns:

a LineMetrics object created with the specified arguments.
getLineMetrics
(char[]
an array of characters
chars
,
int
the initial offset of chars
beginIndex
,
int
the end offset of chars
limit
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns a LineMetrics object created with the specified arguments.

public LineMetrics

Returns:

a LineMetrics object created with the specified arguments.
getLineMetrics
(CharacterIterator
the specified CharacterIterator
ci
,
int
the initial offset in ci
beginIndex
,
int
the end offset of ci
limit
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns a LineMetrics object created with the specified arguments.

public Rectangle2D

Returns:

a Rectangle2D that is the bounding box for the character with the maximum bounds.
getMaxCharBounds
(FontRenderContext
the specified FontRenderContext
frc
)

Returns the bounds for the character with the maximum bounds as defined in the specified FontRenderContext.

public int

Returns:

the glyphCode of this Font.
getMissingGlyphCode
()

Returns the glyphCode which is used when this Font does not have a glyph for a specified unicode code point.

public String

Returns:

a String representing the logical name of this Font.
getName
()

Returns the logical name of this Font.

public int

Returns:

the number of glyphs in this Font.
getNumGlyphs
()

Returns the number of glyphs in this Font.

public String

Returns:

a String representing the postscript name of this Font.
getPSName
()

Returns the postscript name of this Font.

public int

Returns:

the point size of this Font in 1/72 of an inch units.
getSize
()

Returns the point size of this Font, rounded to an integer.

public float

Returns:

the point size of this Font as a float value.
getSize2D
()

Returns the point size of this Font in float value.

public Rectangle2D

Returns:

a Rectangle2D that is the bounding box of the specified String in the specified FontRenderContext.
getStringBounds
(String
the specified String
str
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns the logical bounds of the specified String in the specified FontRenderContext.

public Rectangle2D

Returns:

a Rectangle2D that is the bounding box of the specified String in the specified FontRenderContext.
getStringBounds
(String
the specified String
str
,
int
the initial offset of str
beginIndex
,
int
the end offset of str
limit
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns the logical bounds of the specified String in the specified FontRenderContext.

public Rectangle2D

Returns:

a Rectangle2D that is the bounding box of the specified array of characters in the specified FontRenderContext.
getStringBounds
(char[]
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 FontRenderContext
frc
)

Returns the logical bounds of the specified array of characters in the specified FontRenderContext.

public Rectangle2D

Returns:

a Rectangle2D that is the bounding box of the characters indexed in the specified CharacterIterator in the specified FontRenderContext.
getStringBounds
(CharacterIterator
the specified CharacterIterator
ci
,
int
the initial offset in ci
beginIndex
,
int
the end offset in ci
limit
,
FontRenderContext
the specified FontRenderContext
frc
)

Returns the logical bounds of the characters indexed in the specified CharacterIterator in the specified FontRenderContext.

public int

Returns:

the style of this Font
getStyle
()

Returns the style of this Font.

public AffineTransform

Returns:

an AffineTransform object representing the transform attribute of this Font object.
getTransform
()

Returns a copy of the transform associated with this Font.

public int

Returns:

a hashcode value for this Font.
hashCode
()

Overrides java.lang.Object.hashCode.

Returns a hashcode for this Font.
public boolean

Returns:

true if the font has layout attributes
hasLayoutAttributes
()

Return true if this Font contains attributes that require extra layout processing.

private static boolean
hasTempPermission()

Used with the byte count tracker for fonts created from streams.

public boolean

Returns:

true if this Font has uniform line metrics; false otherwise.
hasUniformLineMetrics
()

Checks whether or not this Font has uniform line metrics.

private void
initFromValues(AttributeValues values)

Initialize the standard Font fields from the values object.

private static native void
public boolean

Returns:

true if this Font object's style is BOLD; false otherwise.
isBold
()

Indicates whether or not this Font object's style is BOLD.

public boolean

Returns:

true if this Font object's style is ITALIC; false otherwise.
isItalic
()

Indicates whether or not this Font object's style is ITALIC.

public boolean

Returns:

true if this Font has a PLAIN style; false otherwise.
isPlain
()

Indicates whether or not this Font object's style is PLAIN.

public boolean

Returns:

true if this Font object has a non-identity AffineTransform attribute. false otherwise.
isTransformed
()

Indicates whether or not this Font object has a transform that affects its size in addition to the Size attribute.

public GlyphVector

Returns:

a new GlyphVector representing the text between start and limit, with glyphs chosen and positioned so as to best represent the text
layoutGlyphVector
(FontRenderContext
the specified FontRenderContext
frc
,
char[]
the text to layout
text
,
int
the start of the text to use for the GlyphVector
start
,
int
the limit of the text to use for the GlyphVector
limit
,
int
control flags as described above
flags
)

Returns a new GlyphVector object, performing full layout of the text if possible.

private void
readObject(ObjectInputStream
the ObjectInputStream to read
s
)

Reads the ObjectInputStream.

public static boolean

Returns:

true if the specified text will need special layout.
textRequiresLayout
(char[]
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 drawString(String,int,int) and other text rendering methods.

public String

Returns:

a String representation of this Font object.
toString
()

Overrides java.lang.Object.toString.

Converts this Font object to a String representation.
private void
writeObject(ObjectOutputStream
the ObjectOutputStream to write
s
)

Writes default serializable fields to a stream.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait