|
Java Platform 1.2 Beta 4 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.font.TextLayout
It provides the following capabilities:
TextLayout can be rendered by calling Graphics2D.drawString passing an instance of TextLayout and a position as the arguments.
TextLayout can be constructed either directly or through use of a LineBreakMeasurer. When constructed directly, the source text represents a single paragraph. LineBreakMeasurer provides support for line breaking to support wrapped text, see its documentation for more information.
TextLayout construction logically proceeds as follows:
All graphical information returned from TextLayout's methods is relative to the origin of the TextLayout, which is the intersection of the TextLayout's baseline with its left edge. Also, coordinates passed into TextLayout's methods are assumed to be relative to the TextLayout's origin. Clients will usually need to translate between TextLayout's coordinate system and the coordinate system in another object (such as a Graphics).
TextLayouts are constructed from styled text, but they do not retain a reference to their source text. Thus, changes in the text previously used to generate a TextLayout do not affect the TextLayout.
Three methods on TextLayout (getNextRightHit
,
getNextLeftHit
, and hitTestChar
) return instances
of TextHitInfo
. The offsets contained in these TextHitInfo's
are relative to the start of the TextLayout, not to the text used to
create the TextLayout. Similarly, TextLayout methods which accept
TextHitInfo instances as parameters expect the TextHitInfo's offsets to be
relative to the TextLayout, not to any underlying text storage model.
Examples:
Constructing and drawing a TextLayout and its bounding rectangle:
Graphics2D g = ...; Point2D loc = ...; Font font = Font.getFont("Helvetica-bold-italic"); TextLayout layout = new TextLayout("This is a string", font); g.drawString(layout, loc.getX(), loc.getY()); Rectangle2D bounds = layout.getBounds(); bounds.setRect(bounds.getX()+loc.getX(), bounds.getY()+loc.getY(), bounds.getWidth(), bounds.getHeight()) g.draw(bounds);
Hit-testing a TextLayout (determining which character is at a particular graphical location):
Point2D click = ...; TextHitInfo hit = layout.hitTestChar( (float) (click.getX() - loc.getX()), (float) (click.getY() - loc.getY()));
Responding to a right-arrow key press:
Drawing a selection range corresponding to a substring in the source text.
The selected area may not be visually contiguous:
Drawing a visually contiguous selection range. The selection range may
correspond to more than one substring in the source text. The ranges of
the corresponding source text substrings can be obtained with
All the text is styled using the provided attributes.
The string must specify a single paragraph of text, as an
entire paragraph is required for the bidirectional
algorithm.
The iterator must specify a single paragraph of text, as an
entire paragraph is required for the bidirectional
algorithm.
If this layout has already been justified, an exception is thrown.
If this layout's justification ratio is zero, a layout identical to this
one is returned.
Some code may rely on immutablity of layouts. Subclassers should not
call this directly, but instead should call getJustifiedLayout, which
will call this method on a clone of this layout, preserving
the original.
The array is indexed by one of the values defined in Font (roman,
centered, hanging). The values are relative to this layout's baseline,
so that getBaselineOffsets[getBaseline()] == 0. Offsets
are added to the position of the layout's baseline to get the position
for the new baseline.
The leading is computed from the leading, descent, and baseline
of all glyphsets in the layout. The algorithm is roughly as follows:
This method is meant for informational use. To display carets, it
is better to use
If the selection includes the leftmost (topmost) position, the selection
is extended to the left (top) of the bounds. If the selection includes
the rightmost (bottommost) position, the selection is extended to the
right (bottom) of the bounds. The height (width on vertical lines) of
the selection is always extended to bounds.
Although the selection is always contiguous, the logically selected
text can be discontiguous on lines with mixed-direction text. The
logical ranges of text selected can be retrieved using
getLogicalRangesForVisualSelection. For example, consider the text
'ABCdef' where capital letters indicate right-to-left text, rendered
on a right-to-left line, with a visual selection from 0L (the leading
edge of 'A') to 3T (the trailing edge of 'd'). The text appears as
follows, with bold underlined areas representing the selection:
If the selection range includes the first logical character, the
selection is extended to the portion of bounds before the start of the
layout. If the range includes the last logical character, the
selection is extended to the portion of bounds after the end of
the layout. The height (width on vertical lines) of the selection is
always extended to bounds.
The selection can be discontiguous on lines with mixed-direction text.
Only those characters in the logical range between start and limit will
appear selected. For example consider the text 'ABCdef' where capital
letters indicate right-to-left text, rendered on a right-to-left line,
with a logical selection from 0 to 4 ('ABCd'). The text appears as
follows, with bold standing in for the selection, and underlining for
the extension:
int insertionIndex = ...;
TextHitInfo next = layout.getNextRightHit(insertionIndex);
if (next != null) {
// translate graphics to origin of layout on screen
g.translate(loc.getX(), loc.getY());
Shape[] carets = layout.getCaretShapes(next.getInsertionIndex());
g.draw(carets[0]);
if (carets[1] != null) {
g.draw(carets[1]);
}
}
// selStart, selLimit should be relative to the layout,
// not to the source text
int selStart = ..., selLimit = ...;
Color selectionColor = ...;
Shape selection = layout.getLogicalHighlightShape(selStart, selLimit);
// selection may consist of disjoint areas
// graphics is assumed to be tranlated to origin of layout
g.setColor(selectionColor);
g.fill(selection);
getLogicalRangesForVisualSelection()
:
TextHitInfo selStart = ..., selLimit = ...;
Shape selection = layout.getVisualHighlightSelection(selStart, selLimit);
g.setColor(selectionColor);
g.fill(selection);
int[] ranges = getLogicalRangesForVisualSelection(selStart, selLimit);
// ranges[0], ranges[1] is the first selection range,
// ranges[2], ranges[3] is the second selection range, etc.
LineBreakMeasurer
,
TextAttribute
,
TextHitInfo
Inner Class Summary
static
TextLayout.CaretPolicy
Define a policy for determining the strong caret location.
Field Summary
static TextLayout.CaretPolicy
DEFAULT_CARET_POLICY
This CaretPolicy is used when a policy is not specified by the
client.
Constructor Summary
TextLayout(AttributedCharacterIterator text,
FontRenderContext frc)
Construct a layout from an iterator over styled text.
TextLayout(String string,
Font font,
FontRenderContext frc)
Construct a layout from a string and a font.
TextLayout(String string,
Map attributes,
FontRenderContext frc)
Construct a layout from a string and an attribute set.
Method Summary
protected Object
clone()
Create a copy of this layout.
void
draw(Graphics2D g2,
float x,
float y)
Render the layout at the provided location in the graphics.
boolean
equals(Object obj)
Return true if the object is a TextLayout and this equals the object.
boolean
equals(TextLayout rhs)
Return true if the two layouts are equal.
float
getAdvance()
Return the advance of the layout.
float
getAscent()
Return the ascent of the layout.
byte
getBaseline()
Return the baseline for this layout.
float[]
getBaselineOffsets()
Return the offsets array for the baselines used for this layout.
Shape
getBlackBoxBounds(int firstEndpoint,
int secondEndpoint)
Return the black box bounds of the characters in the given range.
Rectangle2D
getBounds()
Return the bounds of the layout.
float[]
getCaretInfo(TextHitInfo hit,
Rectangle2D bounds)
Return information about the caret corresponding to hit.
float[]
getCaretInfo(TextHitInfo hit)
A convenience overload using the natural bounds of this layout.
Shape
getCaretShape(TextHitInfo hit,
Rectangle2D bounds)
Return a shape representing the caret at hit inside the given bounds.
Shape
getCaretShape(TextHitInfo hit)
Return a shape representing the caret at hit.
Shape[]
getCaretShapes(int offset,
Rectangle2D bounds,
TextLayout.CaretPolicy policy)
Return two paths corresponding to the strong and weak caret.
Shape[]
getCaretShapes(int offset,
Rectangle2D bounds)
A convenience overload that uses the default caret policy.
Shape[]
getCaretShapes(int offset)
A convenience overload that uses the natural bounds of the layout as
the bounds, and the default caret policy.
int
getCharacterCount()
Return the number of characters represented by this layout.
byte
getCharacterLevel(int index)
Return the level of the character at index.
float
getDescent()
Return the descent of the layout.
TextLayout
getJustifiedLayout(float justificationWidth)
Create a copy of this layout justified to the given width.
float
getLeading()
Return the leading of the layout.
Shape
getLogicalHighlightShape(int firstEndpoint,
int secondEndpoint,
Rectangle2D bounds)
Return a path enclosing the logical selection in the given range,
extended to bounds.
Shape
getLogicalHighlightShape(int firstEndpoint,
int secondEndpoint)
A convenience overload which uses the natural bounds of the layout.
int[]
getLogicalRangesForVisualSelection(TextHitInfo firstEndpoint,
TextHitInfo secondEndpoint)
Return the logical ranges of text corresponding to a visual selection.
TextHitInfo
getNextLeftHit(int offset,
TextLayout.CaretPolicy policy)
Return the hit for the next caret to the left (top); if no
such hit, return null.
TextHitInfo
getNextLeftHit(int offset)
Return the hit for the next caret to the left (top); if no
such hit, return null.
TextHitInfo
getNextLeftHit(TextHitInfo hit)
Return the hit for the next caret to the left (top); if no such
hit, return null.
TextHitInfo
getNextRightHit(int offset,
TextLayout.CaretPolicy policy)
Return the hit for the next caret to the right (bottom); if no
such hit, return null.
TextHitInfo
getNextRightHit(int offset)
Return the hit for the next caret to the right (bottom); if no
such hit, return null.
TextHitInfo
getNextRightHit(TextHitInfo hit)
Return the hit for the next caret to the right (bottom); if no
such hit, return null.
Shape
getOutline(AffineTransform tx,
float x,
float y)
float
getVisibleAdvance()
Return the advance of the layout, minus trailing whitespace.
Shape
getVisualHighlightShape(TextHitInfo firstEndpoint,
TextHitInfo secondEndpoint,
Rectangle2D bounds)
Return a path enclosing the visual selection in the given range,
extended to bounds.
Shape
getVisualHighlightShape(TextHitInfo firstEndpoint,
TextHitInfo secondEndpoint)
A convenience overload which uses the natural bounds of the layout.
TextHitInfo
getVisualOtherHit(TextHitInfo hit)
Return the hit on the opposite side of this hit's caret.
protected void
handleJustify(float justificationWidth)
Justify this layout.
int
hashCode()
Return the hash code of this layout.
TextHitInfo
hitTestChar(float x,
float y,
Rectangle2D bounds)
Return a TextHitInfo corresponding to the point.
TextHitInfo
hitTestChar(float x,
float y)
A convenience overload which uses the natural bounds of the layout.
boolean
isLeftToRight()
Return true if the layout is left-to-right.
boolean
isVertical()
Return true if the layout is vertical.
String
toString()
Return debugging information for the layout.
Methods inherited from class java.lang.Object
finalize
, getClass
, notify
, notifyAll
, wait
, wait
, wait
Field Detail
DEFAULT_CARET_POLICY
public static final TextLayout.CaretPolicy DEFAULT_CARET_POLICY
Constructor Detail
TextLayout
public TextLayout(String string,
Font font,
FontRenderContext frc)
str
- the text to display.
font
- a font used to style the text.
TextLayout
public TextLayout(String string,
Map attributes,
FontRenderContext frc)
str
- the text to display.
attributes
- the attributes used to style the text.
TextLayout
public TextLayout(AttributedCharacterIterator text,
FontRenderContext frc)
text
- the styled text to display.
Method Detail
clone
protected Object clone()
getJustifiedLayout
public TextLayout getJustifiedLayout(float justificationWidth)
justificationWidth
- the width to use when justifying the line.
For best results, it should not be too different from the current
advance of the line.
handleJustify
protected void handleJustify(float justificationWidth)
justificationWidth
- the width to use when justifying the line.
For best results, it should not be too different from the current
advance of the line.getJustifiedLayout(float)
getBaseline
public byte getBaseline()
getBaselineOffsets()
,
Font
getBaselineOffsets
public float[] getBaselineOffsets()
getBaseline()
,
Font
getAdvance
public float getAdvance()
getVisibleAdvance
public float getVisibleAdvance()
getAdvance()
getAscent
public float getAscent()
getDescent
public float getDescent()
getLeading
public float getLeading()
maxD = 0;
maxDL = 0;
for (GlyphSet g in all glyphsets) {
maxD = max(maxD, g.getDescent() + offsets[g.getBaseline()]);
maxDL = max(maxDL, g.getDescent() + g.getLeading() +
offsets[g.getBaseline()]);
}
return maxDL - maxD;
getBounds
public Rectangle2D getBounds()
isLeftToRight
public boolean isLeftToRight()
isVertical
public boolean isVertical()
getCharacterCount
public int getCharacterCount()
getCaretInfo
public float[] getCaretInfo(TextHitInfo hit,
Rectangle2D bounds)
getCaretShapes
.
hit
- a hit on a character in this layout
bounds
- the bounds to which the caret info is constructedgetCaretShapes(int, java.awt.geom.Rectangle2D, java.awt.font.TextLayout.CaretPolicy)
getCaretInfo
public float[] getCaretInfo(TextHitInfo hit)
getNextRightHit
public TextHitInfo getNextRightHit(TextHitInfo hit)
hit
- a hit on a character in this layout.
getNextRightHit
public TextHitInfo getNextRightHit(int offset,
TextLayout.CaretPolicy policy)
offset
- An insertion offset in this layout. Cannot be
less than 0 or greater than the layout's character count.
policy
- The policy used to select the strong caret.
getNextRightHit
public TextHitInfo getNextRightHit(int offset)
offset
- An insertion offset in this layout. Cannot be
less than 0 or greater than the layout's character count.
getNextLeftHit
public TextHitInfo getNextLeftHit(TextHitInfo hit)
hit
- a hit on a character in this layout.
getNextLeftHit
public TextHitInfo getNextLeftHit(int offset,
TextLayout.CaretPolicy policy)
offset
- An insertion offset in this layout. Cannot be
less than 0 or greater than the layout's character count.
policy
- The policy used to select the strong caret.
getNextLeftHit
public TextHitInfo getNextLeftHit(int offset)
offset
- An insertion offset in this layout. Cannot be
less than 0 or greater than the layout's character count.
getVisualOtherHit
public TextHitInfo getVisualOtherHit(TextHitInfo hit)
getCaretShape
public Shape getCaretShape(TextHitInfo hit,
Rectangle2D bounds)
hit
- the hit at which to generate the caret.
bounds
- the bounds of the layout to use in generating the caret.
getCaretShape
public Shape getCaretShape(TextHitInfo hit)
hit
- the hit at which to generate the caret.
getCharacterLevel
public byte getCharacterLevel(int index)
getCaretShapes
public Shape[] getCaretShapes(int offset,
Rectangle2D bounds,
TextLayout.CaretPolicy policy)
offset
- an offset in the layout
bounds
- the bounds to which to extend the carets
getCaretShapes
public Shape[] getCaretShapes(int offset,
Rectangle2D bounds)
getCaretShapes
public Shape[] getCaretShapes(int offset)
getLogicalRangesForVisualSelection
public int[] getLogicalRangesForVisualSelection(TextHitInfo firstEndpoint,
TextHitInfo secondEndpoint)
firstEndpoint
- an endpoint of the visual range.
secondEndpoint
- the other enpoint of the visual range. Can be less than
firstEndpoint
.getVisualHighlightShape(java.awt.font.TextHitInfo, java.awt.font.TextHitInfo, java.awt.geom.Rectangle2D)
getVisualHighlightShape
public Shape getVisualHighlightShape(TextHitInfo firstEndpoint,
TextHitInfo secondEndpoint,
Rectangle2D bounds)
defCBA
The logical selection ranges are 0-3, 4-6 (ABC, ef) because the
visually contiguous text is logically discontiguous. Also note that
since the rightmost position on the layout (to the right of 'A') is
selected, the selection is extended to the right of the bounds.
firstEndpoint
- one end of the visual selection
secondEndpoint
- the other end of the visual selection
bounds
- the bounding rectangle to which to extend the selectiongetLogicalRangesForVisualSelection(java.awt.font.TextHitInfo, java.awt.font.TextHitInfo)
,
getLogicalHighlightShape(int, int, java.awt.geom.Rectangle2D)
getVisualHighlightShape
public Shape getVisualHighlightShape(TextHitInfo firstEndpoint,
TextHitInfo secondEndpoint)
getLogicalHighlightShape
public Shape getLogicalHighlightShape(int firstEndpoint,
int secondEndpoint,
Rectangle2D bounds)
defCBA
The selection is discontiguous because the selected characters are
visually discontiguous. Also note that since the range includes the
first logical character (A), the selection is extended to the portion
of the bounds before the start of the layout, which in this case
(a right-to-left line) is the right portion of the bounds.
firstEndpoint
- an endpoint in the range of characters to select
secondEndpoint
- the other endpoint of the range of characters
to select. Can be less than firstEndpoint
. The range
includes the character at min(firstEndpoint, secondEndpoint), but
excludes max(firstEndpoint, secondEndpoint).
bounds
- the bounding rectangle to which to extend the selectiongetVisualHighlightShape(java.awt.font.TextHitInfo, java.awt.font.TextHitInfo, java.awt.geom.Rectangle2D)
getLogicalHighlightShape
public Shape getLogicalHighlightShape(int firstEndpoint,
int secondEndpoint)
getBlackBoxBounds
public Shape getBlackBoxBounds(int firstEndpoint,
int secondEndpoint)
firstEndpoint
- one end of the character range
secondEndpoint
- the other end of the character range. Can be
less than firstEndpoint
.
hitTestChar
public TextHitInfo hitTestChar(float x,
float y,
Rectangle2D bounds)
x
- the x offset from the origin of the layout
y
- the y offset from the origin of the layout
hitTestChar
public TextHitInfo hitTestChar(float x,
float y)
hashCode
public int hashCode()
equals
public boolean equals(Object obj)
equals
public boolean equals(TextLayout rhs)
layout
- the layout to which to compare this layout.
toString
public String toString()
draw
public void draw(Graphics2D g2,
float x,
float y)
g2
- the graphics into which to render the layout
x
- the x position for the origin of the layout
y
- the y position for the origin of the layoutgetBounds()
getOutline
public Shape getOutline(AffineTransform tx,
float x,
float y)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
Java Platform 1.2
Beta 4
PREV CLASS
NEXT CLASS
FRAMES NO FRAMES
SUMMARY: INNER | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
Submit a bug or feature
Submit comments/suggestions about new javadoc look
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.
Copyright 1993-1998 Sun Microsystems, Inc. 901 San Antonio Road,
Palo Alto, California, 94303, U.S.A. All Rights Reserved.
This documentation was generated with a post-Beta4 version of Javadoc.