java.lang.Object java.awt.Font
public class Font
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.
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 2 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 Internationalization FAQ document.
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 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 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.
The Map-based constructor and deriveFont APIs ignore the FONT attribute, and it is not retained by the Font; the static getFont(java.util.Map extends java.text.AttributedCharacterIterator.Attribute, ?> ) method should be used if the FONT attribute might be present. See 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.'
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 2 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
Internationalization FAQ
document.
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
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.
Field Summary | |
---|---|
static int |
BOLD
The bold style constant. |
static int |
CENTER_BASELINE
The baseline used in ideographic scripts like Chinese, Japanese, and Korean when laying out text. |
static String |
DIALOG
A String constant for the canonical family name of the logical font "Dialog". |
static String |
DIALOG_INPUT
A String constant for the canonical family name of the logical font "DialogInput". |
static int |
HANGING_BASELINE
The baseline used in Devanigiri and similar scripts when laying out text. |
static int |
ITALIC
The italicized style constant. |
static int |
LAYOUT_LEFT_TO_RIGHT
A flag to layoutGlyphVector indicating that text is left-to-right as determined by Bidi analysis. |
static int |
LAYOUT_NO_LIMIT_CONTEXT
A flag to layoutGlyphVector indicating that text in the char array after the indicated limit should not be examined. |
static int |
LAYOUT_NO_START_CONTEXT
A flag to layoutGlyphVector indicating that text in the char array before the indicated start should not be examined. |
static int |
LAYOUT_RIGHT_TO_LEFT
A flag to layoutGlyphVector indicating that text is right-to-left as determined by Bidi analysis. |
static 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. |
static int |
PLAIN
The plain style constant. |
protected float |
pointSize
The point size of this Font in float. |
static int |
ROMAN_BASELINE
The baseline used in most Roman scripts when laying out text. |
static String |
SANS_SERIF
A String constant for the canonical family name of the logical font "SansSerif". |
static 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. |
protected int |
style
The style of this Font, as passed to the constructor. |
static int |
TRUETYPE_FONT
Identify a font resource of type TRUETYPE. |
static int |
TYPE1_FONT
Identify a font resource of type TYPE1. |
Constructor Summary | |
---|---|
protected |
Font
Creates a new Font from |
Font
Creates a new Font with |
|
Font
(
String
Creates a new Font from the specified name, style and point size. |
Method Summary | |
---|---|
boolean |
canDisplay
(char c) Checks if this Font has a glyph for the specified character. |
boolean |
canDisplay
(int codePoint) Checks if this Font has a glyph for the specified character. |
int |
canDisplayUpTo
(char[] text, int start, int limit) Indicates whether or not this Font can display the characters in the specified text starting at start and ending at limit. |
int |
canDisplayUpTo
(
CharacterIterator
iter, int start, int limit) Indicates whether or not this Font can display the text specified by the iter starting at start and ending at limit. |
int |
canDisplayUpTo
(
String
str) Indicates whether or not this Font can display a specified String. |
static Font |
createFont
(int fontFormat,
File
fontFile) Returns a new Font using the specified font type and the specified font file. |
static Font |
createFont
(int fontFormat,
InputStream
fontStream) Returns a new Font using the specified font type and input data. |
GlyphVector |
createGlyphVector
(
FontRenderContext
frc, char[] chars) Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font. |
GlyphVector |
createGlyphVector
(
FontRenderContext
frc,
CharacterIterator
ci) Creates a GlyphVector by mapping the specified characters to glyphs one-to-one based on the Unicode cmap in this Font. |
GlyphVector |
createGlyphVector
(
FontRenderContext
frc, int[] glyphCodes) Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font. |
GlyphVector |
createGlyphVector
(
FontRenderContext
frc,
String
str) Creates a GlyphVector by mapping characters to glyphs one-to-one based on the Unicode cmap in this Font. |
static Font |
decode
(
String
str) Returns the Font that the str argument describes. |
Font |
deriveFont
(
AffineTransform
trans) Creates a new Font object by replicating the current Font object and applying a new transform to it. |
Font |
deriveFont
(float size) Creates a new Font object by replicating the current Font object and applying a new size to it. |
Font |
deriveFont
(int style) Creates a new Font object by replicating the current Font object and applying a new style to it. |
Font |
deriveFont
(int style,
AffineTransform
trans) Creates a new Font object by replicating this Font object and applying a new style and transform. |
Font |
deriveFont
(int style, float size) Creates a new Font object by replicating this Font object and applying a new style and size. |
Font |
deriveFont
(
Map
<? extends
AttributedCharacterIterator.Attribute
,?> attributes) Creates a new Font object by replicating the current Font object and applying a new set of font attributes to it. |
boolean |
equals
(
Object
obj) Compares this Font object to the specified Object. |
protected void |
finalize
() Disposes the native Font object. |
Map < TextAttribute ,?> |
getAttributes
() Returns a map of font attributes available in this Font. |
AttributedCharacterIterator.Attribute [] |
getAvailableAttributes
() Returns the keys of all the attributes supported by this Font. |
byte |
getBaselineFor
(char c) Returns the baseline appropriate for displaying this character. |
String |
getFamily
() Returns the family name of this Font. |
String |
getFamily
(
Locale
l) Returns the family name of this Font, localized for the specified locale. |
static Font |
getFont
(
Map
<? extends
AttributedCharacterIterator.Attribute
Returns a Font appropriate to the attributes. |
static Font |
getFont
(
String
nm) Returns a Font object from the system properties list. |
static Font |
getFont
(
String
nm,
Font
font) Gets the specified Font from the system properties list. |
String |
getFontName
() Returns the font face name of this Font. |
String |
getFontName
(
Locale
l) Returns the font face name of the Font, localized for the specified locale. |
float |
getItalicAngle
() Returns the italic angle of this Font. |
LineMetrics |
getLineMetrics
(char[] chars, int beginIndex, int limit,
FontRenderContext
frc) Returns a LineMetrics object created with the specified arguments. |
LineMetrics |
getLineMetrics
(
CharacterIterator
ci, int beginIndex, int limit,
FontRenderContext
frc) Returns a LineMetrics object created with the specified arguments. |
LineMetrics |
getLineMetrics
(
String
str,
FontRenderContext
frc) Returns a LineMetrics object created with the specified String and FontRenderContext . |
LineMetrics |
getLineMetrics
(
String
str, int beginIndex, int limit,
FontRenderContext
frc) Returns a LineMetrics object created with the specified arguments. |
Rectangle2D |
getMaxCharBounds
(
FontRenderContext
frc) Returns the bounds for the character with the maximum bounds as defined in the specified FontRenderContext. |
int |
getMissingGlyphCode
() Returns the glyphCode which is used when this Font does not have a glyph for a specified unicode code point. |
String |
getName
() Returns the logical name of this Font. |
int |
getNumGlyphs
() Returns the number of glyphs in this Font. |
java.awt.peer.FontPeer |
getPeer
() Deprecated. Font rendering is now platform independent. |
String |
getPSName
() Returns the postscript name of this Font. |
int |
getSize
() Returns the point size of this Font, rounded to an integer. |
float |
getSize2D
() Returns the point size of this Font in float value. |
Rectangle2D |
getStringBounds
(char[] chars, int beginIndex, int limit,
FontRenderContext
frc) Returns the logical bounds of the specified array of characters in the specified FontRenderContext. |
Rectangle2D |
getStringBounds
(
CharacterIterator
ci, int beginIndex, int limit,
FontRenderContext
frc) Returns the logical bounds of the characters indexed in the specified CharacterIterator in the specified FontRenderContext. |
Rectangle2D |
getStringBounds
(
String
str,
FontRenderContext
frc) Returns the logical bounds of the specified String in the specified FontRenderContext. |
Rectangle2D |
getStringBounds
(
String
str, int beginIndex, int limit,
FontRenderContext
frc) Returns the logical bounds of the specified String in the specified FontRenderContext. |
int |
getStyle
() Returns the style of this Font. |
AffineTransform |
getTransform
() Returns a copy of the transform associated with this Font. |
int |
hashCode
() Returns a hashcode for this Font. |
boolean |
hasLayoutAttributes
()
Return true if this Font contains attributes that require extra layout processing. |
boolean |
hasUniformLineMetrics
() Checks whether or not this Font has uniform line metrics. |
boolean |
isBold
() Indicates whether or not this Font object's style is BOLD. |
boolean |
isItalic
() Indicates whether or not this Font object's style is ITALIC. |
boolean |
isPlain
() Indicates whether or not this Font object's style is PLAIN. |
boolean |
isTransformed
() Indicates whether or not this Font object has a transform that affects its size in addition to the Size attribute. |
GlyphVector |
layoutGlyphVector
(
FontRenderContext
frc, char[] text, int start, int limit, int flags) Returns a new GlyphVector object, performing full layout of the text if possible. |
String |
toString
() Converts this Font object to a String representation. |
Methods inherited from class java.lang. Object |
---|
clone , getClass , notify , notifyAll , wait , wait , wait |
Field Detail |
---|
public static final StringDIALOG
public static final StringDIALOG_INPUT
public static final StringSANS_SERIF
public static final StringSERIF
public static final StringMONOSPACED
public static final int PLAIN
public static final int BOLD
public static final int ITALIC
public static final int ROMAN_BASELINE
public static final int CENTER_BASELINE
public static final int HANGING_BASELINE
public static final int TRUETYPE_FONT
public static final int TYPE1_FONT
protected String name
protected int style
protected int size
protected float pointSize
public static final int LAYOUT_LEFT_TO_RIGHT
public static final int LAYOUT_RIGHT_TO_LEFT
public static final int LAYOUT_NO_START_CONTEXT
public static final int LAYOUT_NO_LIMIT_CONTEXT
Constructor Detail |
---|
public Font(String name, int style, int size)
The font name can be a font face name or a font family name. It is used together with the style to find an appropriate font face. When a font family name is specified, the style argument is used to select the most appropriate face from the family. When a font face name is specified, the face's style and the style argument are merged to locate the best matching font from the same family. For example if face name "Arial Bold" is specified with style Font.ITALIC, the font system looks for a face in the "Arial" family that is bold and italic, and may associate the font instance with the physical font face "Arial Bold Italic". The style argument is merged with the specified face's style, not added or subtracted. This means, specifying a bold face and a bold style does not double-embolden the font, and specifying a bold face and a plain style does not lighten the font.
If no face for the requested style can be found, the font system may apply algorithmic styling to achieve the desired style. For example, if ITALIC is requested, but no italic face is available, glyphs from the plain face may be algorithmically obliqued (slanted).
Font name lookup is case insensitive, using the case folding rules of the US locale.
If the name parameter represents something other than a logical font, i.e. is interpreted as a physical font face or family, and this cannot be mapped by the implementation to a physical font or a compatible alternative, then the font system will map the Font instance to "Dialog", such that for example, the family as reported by getFamily will be "Dialog".
public Font(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
If attributes is null, a new Font is initialized with default values.
protected Font ( Font font)
Method Detail |
---|
@Deprecated public java.awt.peer.FontPeer getPeer()
public static Font getFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
public static Font createFont(int fontFormat, InputStream fontStream) throws FontFormatException, IOException
To make the Font available to Font constructors the returned Font must be registered in the GraphicsEnviroment by calling GraphicsEnvironment#registerFont().
public static Font createFont(int fontFormat, File fontFile) throws FontFormatException, IOException
To make the Font available to Font constructors the returned Font must be registered in the GraphicsEnviroment by calling GraphicsEnvironment#registerFont().
public AffineTransform getTransform()
Typically, fonts will not be transformed. Clients generally should call isTransformed() first, and only call this method if isTransformed returns true.
public String getFamily()
The family name of a font is font specific. Two fonts such as Helvetica Italic and Helvetica Bold have the same family name, Helvetica , whereas their font face names are Helvetica Bold and Helvetica Italic . The list of available family names may be obtained by using the GraphicsEnvironment.getAvailableFontFamilyNames() method.
Use getName to get the logical name of the font. Use getFontName to get the font face name of the font.
public String getFamily(Locale l)
The family name of a font is font specific. Two fonts such as Helvetica Italic and Helvetica Bold have the same family name, Helvetica , whereas their font face names are Helvetica Bold and Helvetica Italic . The list of available family names may be obtained by using the GraphicsEnvironment.getAvailableFontFamilyNames() method.
Use getFontName to get the font face name of the font.
public String getPSName()
public String getName()
public String getFontName()
public String getFontName(Locale l)
public int getStyle()
public int getSize()
The Java(tm)2D API adopts the convention that one point is equivalent to one unit in user coordinates. When using a normalized transform for converting user space coordinates to device space coordinates 72 user space units equal 1 inch in device space. In this case one point is 1/72 of an inch.
public float getSize2D()
public boolean isPlain()
public boolean isBold()
public boolean isItalic()
public boolean isTransformed()
public boolean hasLayoutAttributes ()
public static Font getFont(String nm)
public static Font decode(String str)
A valid trailing decimal field is always interpreted as the pointsize. Therefore a fontname containing a trailing decimal value should not be used in the fontname only form.
If a style name field is not one of the valid style strings, it is interpreted as part of the font name, and the default style is used.
Only one of ' ' or '-' may be used to separate fields in the input. The identified separator is the one closest to the end of the string which separates a valid pointsize, or a valid style name from the rest of the string. Null (empty) pointsize and style fields are treated as valid fields with the default value for that field.
Some font names may include the separator characters ' ' or '-'. If str is not formed with 3 components, e.g. such that style or pointsize fields are not present in str, and fontname also contains a character determined to be the separator character then these characters where they appear as intended to be part of fontname may instead be interpreted as separators so the font name may not be properly recognised.
The default size is 12 and the default style is PLAIN. If str does not specify a valid size, the returned Font has a size of 12. If str does not specify a valid style, the returned Font has a style of PLAIN. If you do not specify a valid font name in the str argument, this method will return a font with the family name "Dialog". To determine what font family names are available on your system, use the GraphicsEnvironment.getAvailableFontFamilyNames() method. If str is null, a new Font is returned with the family name "Dialog", a size of 12 and a PLAIN style.
public static Font getFont(String nm, Font font)
The property value should be one of the forms accepted by Font.decode(String) If the specified property is not found, or the executing code does not have permission to read the property, the font argument is returned instead.
public int hashCode()
public boolean equals(Object obj)
public String toString()
public int getNumGlyphs()
public int getMissingGlyphCode()
public byte getBaselineFor(char c)
Large fonts can support different writing systems, and each system can use a different baseline. The character argument determines the writing system to use. Clients should not assume all characters use the same baseline.
public Map<TextAttribute,?> getAttributes()
public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
public Font deriveFont(int style, float size)
public Font deriveFont(int style, AffineTransform trans)
public Font deriveFont(float size)
public Font deriveFont(AffineTransform trans)
public Font deriveFont(int style)
public Font deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
public boolean canDisplay(char c)
Note: This method cannot handle supplementary characters . To support all Unicode characters, including supplementary characters, use the canDisplay(int) method or canDisplayUpTo methods.
public boolean canDisplay(int codePoint)
public int canDisplayUpTo(String str)
public int canDisplayUpTo(char[] text, int start, int limit)
public int canDisplayUpTo(CharacterIterator iter, int start, int limit)
public float getItalicAngle()
public boolean hasUniformLineMetrics()
public LineMetrics getLineMetrics(String str, FontRenderContext frc)
public LineMetrics getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc)
public LineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc)
public LineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)
public Rectangle2D getStringBounds(String str, FontRenderContext frc)
Note: The returned bounds is in baseline-relative coordinates (see class notes ).
public Rectangle2D getStringBounds(String str, int beginIndex, int limit, FontRenderContext frc)
Note: The returned bounds is in baseline-relative coordinates (see class notes ).
public Rectangle2D getStringBounds(char[] chars, int beginIndex, int limit, FontRenderContext frc)
Note: The returned bounds is in baseline-relative coordinates (see class notes ).
public Rectangle2D getStringBounds(CharacterIterator ci, int beginIndex, int limit, FontRenderContext frc)
Note: The returned bounds is in baseline-relative coordinates (see class notes ).
public Rectangle2D getMaxCharBounds(FontRenderContext frc)
Note: The returned bounds is in baseline-relative coordinates (see class notes ).
public GlyphVector createGlyphVector(FontRenderContext frc, String str)
public GlyphVector createGlyphVector(FontRenderContext frc, char[] chars)
public GlyphVector createGlyphVector(FontRenderContext frc, CharacterIterator ci)
public GlyphVector createGlyphVector(FontRenderContext frc, int[] glyphCodes)
public GlyphVector layoutGlyphVector(FontRenderContext frc, char[] text, int start, int limit, int flags)
In addition, some operations, such as Arabic shaping, require context, so that the characters at the start and limit can have the proper shapes. Sometimes the data in the buffer outside the provided range does not have valid data. The values LAYOUT_NO_START_CONTEXT and LAYOUT_NO_LIMIT_CONTEXT can be added to the flags parameter to indicate that the text before start, or after limit, respectively, should not be examined for context.
All other values for the flags parameter are reserved.
protected void finalize() throws Throwable