public class Font extends Object implements Serializable
Modifier and Type | Field and Description |
---|---|
static int |
BOLD
Constant indicating a "bold" font.
|
static int |
CENTER_BASELINE
Constant indicating the baseline mode characteristic of Chinese.
|
static String |
DIALOG |
static String |
DIALOG_INPUT |
static int |
HANGING_BASELINE
Constant indicating the baseline mode characteristic of Devanigri.
|
static int |
ITALIC
Constant indicating an "italic" font.
|
static int |
LAYOUT_LEFT_TO_RIGHT
A flag for
layoutGlyphVector , indicating that the
orientation of a text run is from left to right. |
static int |
LAYOUT_NO_LIMIT_CONTEXT
A flag for
layoutGlyphVector , indicating that the
text does not contain valid characters after the
limit position. |
static int |
LAYOUT_NO_START_CONTEXT
A flag for
layoutGlyphVector , indicating that the
text does not contain valid characters before the
start position. |
static int |
LAYOUT_RIGHT_TO_LEFT
A flag for
layoutGlyphVector , indicating that the
orientation of a text run is from right to left. |
static String |
MONOSPACED |
protected String |
name
The logical name of this font.
|
static int |
PLAIN
Constant indicating a "plain" font.
|
protected float |
pointSize
The size of this font in points.
|
static int |
ROMAN_BASELINE
Constant indicating the baseline mode characteristic of Roman.
|
static String |
SANS_SERIF |
static String |
SERIF |
protected int |
size
The size of this font in points, rounded.
|
protected int |
style
The style of this font -- PLAIN, BOLD, ITALIC or BOLD+ITALIC.
|
static int |
TRUETYPE_FONT
Indicates to
createFont that the supplied font data
is in TrueType format. |
static int |
TYPE1_FONT
Indicates to
createFont that the supplied font data
is in Type1 format. |
Modifier | Constructor and Description |
---|---|
protected |
Font(Font font) |
|
Font(Map<? extends AttributedCharacterIterator.Attribute,?> attrs) |
|
Font(String name,
int style,
int size)
Initializes a new instance of
Font with the specified
attributes. |
Modifier and Type | Method and Description |
---|---|
boolean |
canDisplay(char c)
Checks if specified character maps to a glyph in this font.
|
boolean |
canDisplay(int codePoint) |
int |
canDisplayUpTo(char[] text,
int start,
int limit)
Checks how much of a given sequence of text can be mapped to glyphs in
this font.
|
int |
canDisplayUpTo(CharacterIterator i,
int start,
int limit)
Checks how much of a given sequence of text can be mapped to glyphs in
this font.
|
int |
canDisplayUpTo(String s)
Checks how much of a given string can be mapped to glyphs in
this font.
|
static Font |
createFont(int fontFormat,
File file)
Creates a new font from a File object.
|
static Font |
createFont(int fontFormat,
InputStream is)
Creates a new font with point size 1 and
PLAIN style,
reading font data from the provided input stream. |
GlyphVector |
createGlyphVector(FontRenderContext ctx,
char[] chars)
Maps characters to glyphs in a one-to-one relationship, returning a new
GlyphVector with a mapped glyph for each input character. |
GlyphVector |
createGlyphVector(FontRenderContext ctx,
CharacterIterator i)
Maps characters to glyphs in a one-to-one relationship, returning a new
GlyphVector with a mapped glyph for each input character. |
GlyphVector |
createGlyphVector(FontRenderContext ctx,
int[] glyphCodes)
Extracts a sequence of glyphs from a font, returning a new
GlyphVector with a mapped glyph for each input glyph code. |
GlyphVector |
createGlyphVector(FontRenderContext ctx,
String str)
Maps characters to glyphs in a one-to-one relationship, returning a new
GlyphVector with a mapped glyph for each input character. |
static Font |
decode(String fontspec)
Creates a
Font object from the specified string, which
is in one of the following formats:
fontname-style-pointsize
fontname-style
fontname-pointsize
fontname
The style should be one of BOLD, ITALIC, or BOLDITALIC. |
Font |
deriveFont(AffineTransform a)
Produces a new
Font based on the current font, subjected
to a new affine transformation. |
Font |
deriveFont(float size)
Produces a new
Font based on the current font, adjusted to a
new size. |
Font |
deriveFont(int style)
Produces a new
Font based on the current font, adjusted to a
new style. |
Font |
deriveFont(int style,
AffineTransform a)
Produces a new
Font based on the current font, adjusted to a
new style and subjected to a new affine transformation. |
Font |
deriveFont(int style,
float size)
Produces a new
Font based on the current font, adjusted to a
new size and style. |
Font |
deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Produces a new
Font based on the current font, adjusted to a
new set of attributes. |
boolean |
equals(Object obj)
Tests whether or not the specified object is equal to this font.
|
Map<TextAttribute,?> |
getAttributes()
Returns a map of chracter attributes which this font currently has set.
|
AttributedCharacterIterator.Attribute[] |
getAvailableAttributes()
Returns an array of chracter attribute keys which this font understands.
|
byte |
getBaselineFor(char c)
Returns a baseline code (one of
ROMAN_BASELINE , CENTER_BASELINE or HANGING_BASELINE ) indicating which baseline
this font will measure baseline offsets for, when presenting glyph
metrics for a given character. |
String |
getFamily()
Returns the family name of this font.
|
String |
getFamily(Locale lc)
Returns the family name of this font.
|
static Font |
getFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Returns a font appropriate for the given attribute set.
|
static Font |
getFont(String propname)
Returns a
Font object from the passed property name. |
static Font |
getFont(String propname,
Font defval)
Returns a
Font object from the passed property name. |
String |
getFontName()
Returns the font face name of the font.
|
String |
getFontName(Locale lc)
Returns the font face name of the font.
|
float |
getItalicAngle()
Returns the italic angle of this font, a measurement of its slant when
style is
ITALIC . |
LineMetrics |
getLineMetrics(char[] chars,
int begin,
int limit,
FontRenderContext rc)
Returns a
LineMetrics object constructed with the specified
text and FontRenderContext . |
LineMetrics |
getLineMetrics(CharacterIterator ci,
int begin,
int limit,
FontRenderContext rc)
Returns a
LineMetrics object constructed with the specified
text and FontRenderContext . |
LineMetrics |
getLineMetrics(String str,
FontRenderContext frc)
Determines the line metrics for a run of text.
|
LineMetrics |
getLineMetrics(String text,
int begin,
int limit,
FontRenderContext rc)
Returns a
LineMetrics object constructed with the specified
text and FontRenderContext . |
Rectangle2D |
getMaxCharBounds(FontRenderContext rc)
Returns the maximal bounding box of all the bounding boxes in this
font, when the font's bounding boxes are evaluated in a given
FontRenderContext |
int |
getMissingGlyphCode()
Returns the glyph code this font uses to represent missing glyphs.
|
String |
getName()
Returns the logical name of the font.
|
int |
getNumGlyphs()
Returns the overall number of glyphs in this font.
|
FontPeer |
getPeer()
Deprecated.
|
String |
getPSName()
Returns the PostScript Name of this font.
|
int |
getSize()
Returns the size of the font, in typographics points (1/72 of an inch),
rounded to an integer.
|
float |
getSize2D()
Returns the size of the font, in typographics points (1/72 of an inch).
|
Rectangle2D |
getStringBounds(char[] chars,
int begin,
int limit,
FontRenderContext frc)
Returns the logical bounds of the specified string when rendered with this
font in the specified
FontRenderContext . |
Rectangle2D |
getStringBounds(CharacterIterator ci,
int begin,
int limit,
FontRenderContext frc)
Returns the logical bounds of the specified string when rendered with this
font in the specified
FontRenderContext . |
Rectangle2D |
getStringBounds(String str,
FontRenderContext frc)
Returns the logical bounds of the specified string when rendered with this
font in the specified
FontRenderContext . |
Rectangle2D |
getStringBounds(String str,
int begin,
int limit,
FontRenderContext frc)
Returns the logical bounds of the specified string when rendered with this
font in the specified
FontRenderContext . |
int |
getStyle()
|
AffineTransform |
getTransform()
Returns a copy of the affine transformation this font is currently
subject to, if any.
|
int |
hashCode()
Returns a hash value for this font.
|
boolean |
hasLayoutAttributes() |
boolean |
hasUniformLineMetrics()
Indicates whether this font's line metrics are uniform.
|
boolean |
isBold()
Tests whether or not this font is bold.
|
boolean |
isItalic()
Tests whether or not this font is italic.
|
boolean |
isPlain()
Tests whether or not this is a plain font.
|
boolean |
isTransformed()
Indicates whether this font is subject to a non-identity affine
transformation.
|
GlyphVector |
layoutGlyphVector(FontRenderContext frc,
char[] chars,
int start,
int limit,
int flags)
Produces a glyph vector representing a full layout fo the specified
text in this font.
|
String |
toString()
Returns a string representation of this font.
|
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
createFont
that the supplied font data
is in TrueType format.
Specification Note: The Sun JavaDoc for J2SE 1.4 does not indicate whether this value also subsumes OpenType. OpenType is essentially the same format as TrueType, but allows to define glyph shapes in the same way as PostScript, using cubic bezier curves.
public static final int TYPE1_FONT
createFont
that the supplied font data
is in Type1 format.public static final int LAYOUT_LEFT_TO_RIGHT
layoutGlyphVector
, indicating that the
orientation of a text run is from left to right.public static final int LAYOUT_RIGHT_TO_LEFT
layoutGlyphVector
, indicating that the
orientation of a text run is from right to left.public static final int LAYOUT_NO_START_CONTEXT
layoutGlyphVector
, indicating that the
text does not contain valid characters before the
start
position. If this flag is set,
layoutGlyphVector
does not examine the text before
start
, even if this would be necessary to select the
correct glyphs (e.g., for Arabic text).public static final int LAYOUT_NO_LIMIT_CONTEXT
layoutGlyphVector
, indicating that the
text does not contain valid characters after the
limit
position. If this flag is set,
layoutGlyphVector
does not examine the text after
limit
, even if this would be necessary to select the
correct glyphs (e.g., for Arabic text).public static final String DIALOG
public static final String DIALOG_INPUT
public static final String MONOSPACED
public static final String SANS_SERIF
public static final String SERIF
protected int size
protected float pointSize
protected int style
public Font(String name, int style, int size)
Font
with the specified
attributes.name
- The name of the font.style
- The font style.size
- The font point size.public Font(Map<? extends AttributedCharacterIterator.Attribute,?> attrs)
public static Font decode(String fontspec)
Font
object from the specified string, which
is in one of the following formats:
The style should be one of BOLD, ITALIC, or BOLDITALIC. The default style if none is specified is PLAIN. The default size if none is specified is 12.
fontspec
- a string specifying the required font (null
permitted, interpreted as 'Dialog-PLAIN-12').public static Font getFont(String propname, Font defval)
Font
object from the passed property name.propname
- The name of the system property.defval
- Value to use if the property is not found.default
if the property
not exist or is malformed.public static Font getFont(String propname)
Font
object from the passed property name.propname
- The name of the system property.null
if the property
not exist or is malformed.public String getName()
getFamily()
,
getFontName()
public int getSize()
public float getSize2D()
public boolean isPlain()
true
if this is a plain font, false
otherwise.public boolean isBold()
true
if this font is bold, false
otherwise.public boolean isItalic()
true
if this font is italic, false
otherwise.public String getFamily()
getName()
,
getFontName()
,
GraphicsEnvironment.getAvailableFontFamilyNames()
public int getStyle()
PLAIN
, BOLD
, or ITALIC
.isPlain()
,
isBold()
,
isItalic()
public boolean canDisplay(char c)
c
- The character to check.public boolean canDisplay(int codePoint)
public int canDisplayUpTo(String s)
s
- The string to check.s
which cannot
be converted to a glyph by this font, or -1
if all
characters can be mapped to glyphs.public int canDisplayUpTo(char[] text, int start, int limit)
text
- Array containing the text to check.start
- Position of first character to check in text
.limit
- Position of last character to check in text
.-1
if all
characters can be mapped to glyphs.IndexOutOfBoundsException
- if the range [start, limit] is
invalid in text
.public int canDisplayUpTo(CharacterIterator i, int start, int limit)
i
- Iterator over the text to check.start
- Position of first character to check in i
.limit
- Position of last character to check in i
.-1
if all
characters can be mapped to glyphs.IndexOutOfBoundsException
- if the range [start, limit] is
invalid in i
.public static Font createFont(int fontFormat, InputStream is) throws FontFormatException, IOException
PLAIN
style,
reading font data from the provided input stream. The resulting font
can have further fonts derived from it using its
deriveFont
method.fontFormat
- Integer code indicating the format the font data is
in.Currently this can only be TRUETYPE_FONT
.is
- InputStream
from which font data will be read. This
stream is not closed after font data is extracted.Font
of the format indicated.IllegalArgumentException
- if fontType
is not
recognized.FontFormatException
- if data in InputStream is not of format
indicated.IOException
- if insufficient data is present on InputStream.public static Font createFont(int fontFormat, File file) throws FontFormatException, IOException
fontFormat
- - Integer code indicating the format the font data is
in.Currently this can only be TRUETYPE_FONT
.file
- - a File
from which font data will be read.Font
of the format indicated.IllegalArgumentException
- if fontType
is not
recognized.NullPointerException
- if file
is null
.FontFormatException
- if data in the file is invalid or cannot be read..SecurityException
- if the caller has no read permission for the file.IOException
- if the file cannot be readlayoutGlyphVector(FontRenderContext, char[], int, int, int)
public GlyphVector createGlyphVector(FontRenderContext ctx, String str)
GlyphVector
with a mapped glyph for each input character. This
sort of mapping is often sufficient for some scripts such as Roman, but
is inappropriate for scripts with special shaping or contextual layout
requirements such as Arabic, Indic, Hebrew or Thai.ctx
- The rendering context used for precise glyph placement.str
- The string to convert to Glyphs.GlyphVector
containing glyphs mapped from str,
through the font's cmap table.layoutGlyphVector(FontRenderContext, char[], int, int, int)
public GlyphVector createGlyphVector(FontRenderContext ctx, CharacterIterator i)
GlyphVector
with a mapped glyph for each input character. This
sort of mapping is often sufficient for some scripts such as Roman, but
is inappropriate for scripts with special shaping or contextual layout
requirements such as Arabic, Indic, Hebrew or Thai.ctx
- The rendering context used for precise glyph placement.i
- Iterator over the text to convert to glyphs.GlyphVector
containing glyphs mapped from str,
through the font's cmap table.layoutGlyphVector(FontRenderContext, char[], int, int, int)
public GlyphVector createGlyphVector(FontRenderContext ctx, char[] chars)
GlyphVector
with a mapped glyph for each input character. This
sort of mapping is often sufficient for some scripts such as Roman, but
is inappropriate for scripts with special shaping or contextual layout
requirements such as Arabic, Indic, Hebrew or Thai.ctx
- The rendering context used for precise glyph placement.chars
- Array of characters to convert to glyphs.GlyphVector
containing glyphs mapped from str,
through the font's cmap table.layoutGlyphVector(FontRenderContext, char[], int, int, int)
public GlyphVector createGlyphVector(FontRenderContext ctx, int[] glyphCodes)
GlyphVector
with a mapped glyph for each input glyph code.ctx
- The rendering context used for precise glyph placement.glyphCodes
- Array of characters to convert to glyphs.GlyphVector
containing glyphs mapped from str,
through the font's cmap table.layoutGlyphVector(FontRenderContext, char[], int, int, int)
public Font deriveFont(int style, float size)
Font
based on the current font, adjusted to a
new size and style.style
- The style of the newly created font.size
- The size of the newly created font.public Font deriveFont(float size)
Font
based on the current font, adjusted to a
new size.size
- The size of the newly created font.public Font deriveFont(int style)
Font
based on the current font, adjusted to a
new style.style
- The style of the newly created font.public Font deriveFont(int style, AffineTransform a)
Font
based on the current font, adjusted to a
new style and subjected to a new affine transformation.style
- The style of the newly created font.a
- The transformation to apply.IllegalArgumentException
- If transformation is
null
.public Font deriveFont(AffineTransform a)
Font
based on the current font, subjected
to a new affine transformation.a
- The transformation to apply.IllegalArgumentException
- If transformation is
null
.public Font deriveFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
Font
based on the current font, adjusted to a
new set of attributes.attributes
- Attributes of the newly created font.public Map<TextAttribute,?> getAttributes()
getAvailableAttributes()
,
AttributedCharacterIterator.Attribute
,
TextAttribute
public AttributedCharacterIterator.Attribute[] getAvailableAttributes()
getAttributes()
,
AttributedCharacterIterator.Attribute
,
TextAttribute
public byte getBaselineFor(char c)
ROMAN_BASELINE
, CENTER_BASELINE
or HANGING_BASELINE
) indicating which baseline
this font will measure baseline offsets for, when presenting glyph
metrics for a given character.
Baseline offsets describe the position of a glyph relative to an
invisible line drawn under, through the center of, or over a line of
rendered text, respectively. Different scripts use different baseline
modes, so clients should not assume all baseline offsets in a glyph
vector are from a common baseline.c
- The character code to select a baseline mode for.LineMetrics.getBaselineOffsets()
public String getFamily(Locale lc)
lc
- The locale in which to describe the name of the font family.getName()
,
getFontName()
,
GraphicsEnvironment.getAvailableFontFamilyNames()
,
Locale
public static Font getFont(Map<? extends AttributedCharacterIterator.Attribute,?> attributes)
attributes
- The attributes required for the new font.TextAttribute
public String getFontName()
getName()
,
getFamily()
public String getFontName(Locale lc)
lc
- The locale in which to describe the name of the font face.getName()
,
getFamily()
public float getItalicAngle()
ITALIC
. The precise meaning is the inverse slope of a
caret line which "best measures" the font's italic posture.TextAttribute.POSTURE
public LineMetrics getLineMetrics(String text, int begin, int limit, FontRenderContext rc)
LineMetrics
object constructed with the specified
text and FontRenderContext
.text
- The string to calculate metrics from.begin
- Index of first character in text
to measure.limit
- Index of last character in text
to measure.rc
- Context for calculating precise glyph placement and hints.LineMetrics
object.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in text
.public LineMetrics getLineMetrics(char[] chars, int begin, int limit, FontRenderContext rc)
LineMetrics
object constructed with the specified
text and FontRenderContext
.chars
- The string to calculate metrics from.begin
- Index of first character in text
to measure.limit
- Index of last character in text
to measure.rc
- Context for calculating precise glyph placement and hints.LineMetrics
object.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in chars
.public LineMetrics getLineMetrics(CharacterIterator ci, int begin, int limit, FontRenderContext rc)
LineMetrics
object constructed with the specified
text and FontRenderContext
.ci
- The string to calculate metrics from.begin
- Index of first character in text
to measure.limit
- Index of last character in text
to measure.rc
- Context for calculating precise glyph placement and hints.LineMetrics
object.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in ci
.public Rectangle2D getMaxCharBounds(FontRenderContext rc)
FontRenderContext
rc
- Context in which to evaluate bounding boxes.public int getMissingGlyphCode()
public int getNumGlyphs()
[ 0, getNumGlyphs() - 1 ]
.public String getPSName()
getName()
,
getFamily()
,
getFontName()
public Rectangle2D getStringBounds(String str, FontRenderContext frc)
FontRenderContext
. This box will include the
glyph origin, ascent, advance, height, and leading, but may not include all
diacritics or accents. To get the complete visual bounding box of all the
glyphs in a run of text, use the TextLayout.getBounds()
method of
TextLayout
.str
- The string to measure.frc
- The context in which to make the precise glyph measurements.createGlyphVector(FontRenderContext, String)
public Rectangle2D getStringBounds(String str, int begin, int limit, FontRenderContext frc)
FontRenderContext
. This box will include the
glyph origin, ascent, advance, height, and leading, but may not include all
diacritics or accents. To get the complete visual bounding box of all the
glyphs in a run of text, use the TextLayout.getBounds()
method of
TextLayout
.str
- The string to measure.begin
- Index of the first character in str
to measure.limit
- Index of the last character in str
to measure.frc
- The context in which to make the precise glyph measurements.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in str
.createGlyphVector(FontRenderContext, String)
public Rectangle2D getStringBounds(CharacterIterator ci, int begin, int limit, FontRenderContext frc)
FontRenderContext
. This box will include the
glyph origin, ascent, advance, height, and leading, but may not include all
diacritics or accents. To get the complete visual bounding box of all the
glyphs in a run of text, use the TextLayout.getBounds()
method of
TextLayout
.ci
- The text to measure.begin
- Index of the first character in ci
to measure.limit
- Index of the last character in ci
to measure.frc
- The context in which to make the precise glyph measurements.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in ci
.createGlyphVector(FontRenderContext, CharacterIterator)
public Rectangle2D getStringBounds(char[] chars, int begin, int limit, FontRenderContext frc)
FontRenderContext
. This box will include the
glyph origin, ascent, advance, height, and leading, but may not include all
diacritics or accents. To get the complete visual bounding box of all the
glyphs in a run of text, use the TextLayout.getBounds()
method of
TextLayout
.chars
- The text to measure.begin
- Index of the first character in ci
to measure.limit
- Index of the last character in ci
to measure.frc
- The context in which to make the precise glyph measurements.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in chars
.createGlyphVector(FontRenderContext, char[])
public AffineTransform getTransform()
public boolean hasUniformLineMetrics()
LineMetrics
,
getLineMetrics(String, FontRenderContext)
public boolean isTransformed()
true
iff the font has a non-identity affine
transformation applied to it.public GlyphVector layoutGlyphVector(FontRenderContext frc, char[] chars, int start, int limit, int flags)
LAYOUT_LEFT_TO_RIGHT
or LAYOUT_RIGHT_TO_LEFT
.
Some types of layout (notably Arabic glyph shaping) may examine context
characters beyond the bounds of the indicated range, in order to select
an appropriate shape. The flags LAYOUT_NO_START_CONTEXT
and
LAYOUT_NO_LIMIT_CONTEXT
can be provided to prevent these extra
context areas from being examined, for instance if they contain invalid
characters.frc
- Context in which to perform the layout.chars
- Text to perform layout on.start
- Index of first character to perform layout on.limit
- Index of last character to perform layout on.flags
- Combination of flags controlling layout.GlyphVector
representing the specified text.IndexOutOfBoundsException
- if the range [begin, limit] is
invalid in chars
.public FontPeer getPeer()
public int hashCode()
hashCode
in class Object
Object.equals(Object)
,
System.identityHashCode(Object)
public boolean equals(Object obj)
null
.
Font
.
equals
in class Object
obj
- the Object to compare totrue
if the specified object is equal to this
object, false
otherwise.Object.hashCode()
public String toString()
toString
in class Object
Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public LineMetrics getLineMetrics(String str, FontRenderContext frc)
str
- the text run to be measured.frc
- the font rendering parameters that are used for the
measurement. The exact placement and size of text slightly
depends on device-specific characteristics, for instance
the device resolution or anti-aliasing. For this reason,
the returned measurement will only be accurate if the
passed FontRenderContext
correctly reflects
the relevant parameters. Hence, frc
should be
obtained from the same Graphics2D
that will
be used for drawing, and any rendering hints should be set
to the desired values before obtaining frc
.Graphics2D.getFontRenderContext()
public boolean hasLayoutAttributes()