TextLayout is an immutable graphical representation of styled
character data.
It provides the following capabilities:
A TextLayout object can be rendered using
its draw method.
TextLayout can be constructed either directly or through
the use of a LineBreakMeasurer. When constructed directly, the
source text represents a single paragraph. LineBreakMeasurer
allows styled text to be broken into lines that fit within a particular
width. See the LineBreakMeasurer documentation for more
information.
TextLayout construction logically proceeds as follows:
TextAttribute#FONT is present, otherwise by computing
a default font using the attributes that have been defined
All graphical information returned from a TextLayout
object's methods is relative to the origin of the
TextLayout, which is the intersection of the
TextLayout object's baseline with its left edge. Also,
coordinates passed into a TextLayout object's methods
are assumed to be relative to the TextLayout object's
origin. Clients usually need to translate between a
TextLayout object's coordinate system and the coordinate
system in another object (such as a
Graphics object).
TextLayout objects 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 a TextLayout object
(getNextRightHit, getNextLeftHit, and
hitTestChar) return instances of TextHitInfo.
The offsets contained in these TextHitInfo objects
are relative to the start of the TextLayout, not
to the text used to create the TextLayout. Similarly,
TextLayout methods that accept TextHitInfo
instances as parameters expect the TextHitInfo object'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");
FontRenderContext frc = g.getFontRenderContext();
TextLayout layout = new TextLayout("This is a string", font, frc);
layout.draw(g, (float)loc.getX(), (float)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:
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]);
}
}
Drawing a selection range corresponding to a substring in the source text. The selected area may not be visually contiguous:
// 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 translated to origin of layout g.setColor(selectionColor); g.fill(selection);
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
getLogicalRangesForVisualSelection():
TextHitInfo selStart = ..., selLimit = ...; Shape selection = layout.getVisualHighlightShape(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.
Note
Font rotations can cause text baselines to be rotated, and multiple runs with different rotations can cause the baseline to bend or zig-zag. In order to account for this (rare) possibility, some APIs are specified to return metrics and take parameters 'in baseline-relative coordinates' (e.g. ascent, advance), and others are in 'in standard coordinates' (e.g. getBounds). Values in baseline-relative coordinates map the 'x' coordinate to the distance 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). Values in standard coordinates are measured along the x and y axes, with 0,0 at the origin of the TextLayout. Documentation for each relevant API indicates what values are in what coordinate system. In general, measurement-related APIs are in baseline-relative coordinates, while display-related APIs are in standard coordinates.
LineBreakMeasurer, TextAttribute, TextHitInfo, LayoutPath
| Modifier and Type | Class and Description |
|---|---|
| public static class | TextLayout.
Defines a policy for determining the strong caret location. |
| Modifier and Type | Field and Description |
|---|---|
| private static final float | |
| private byte | |
| private float[] | |
| private Rectangle2D | |
| private boolean | |
| private boolean | |
| private int | |
| public static final TextLayout. | DEFAULT_CARET_POLICY
This |
| private static float | |
| private static float | |
| private boolean | |
| private float | |
| private TextLine. | |
| private Rectangle2D | |
| private TextLine | |
| private float |
| Access | Constructor and Description |
|---|---|
| public | TextLayout(String
the text to display string, Font a font, FontRenderContext Font used to style the textcontains information about a graphics device which is needed
to measure the text correctly.
Text measurements can vary slightly depending on the
device resolution, and attributes such as antialiasing. This
parameter does not specify a translation between the
frc)TextLayout and user space.Constructs a |
| public | TextLayout(String
the text to display string, Map<? extends AttributedCharacterIterator.the attributes used to style the text attributes, FontRenderContext contains information about a graphics device which is needed
to measure the text correctly.
Text measurements can vary slightly depending on the
device resolution, and attributes such as antialiasing. This
parameter does not specify a translation between the
frc)TextLayout and user space.Constructs a |
| public | TextLayout(AttributedCharacterIterator
the styled text to display text, FontRenderContext contains information about a graphics device which is needed
to measure the text correctly.
Text measurements can vary slightly depending on the
device resolution, and attributes such as antialiasing. This
parameter does not specify a translation between the
frc)TextLayout and user space.Constructs a |
| pack-priv | TextLayout(TextLine
the line measurement attributes to apply to the
the resulting textLine, byte TextLayoutthe baseline of the text baseline, float[] the baseline offsets for this
baselineOffsets, float TextLayout. This should already be normalized to
baseline0 if the TextLayout
cannot be justified; 1 otherwise.Creates a |
| Modifier and Type | Method and Description |
|---|---|
| private GeneralPath | |
| private void | |
| private GeneralPath | |
| private boolean | |
| private TextHitInfo | Returns: a hit on this layout whose strong caret is at the requested index.a caret index. caret)Given a caret index, return a hit whose caret is at the index. |
| private float | caretToPointDistance(float[] caretInfo, float x, float y)
Returns the distance from the point (x, y) to the caret along
the line direction defined in |
| private void | |
| protected Object | |
| public void | draw(Graphics2D
the g2, float Graphics2D context into which to render
the layoutthe X coordinate of the origin of this x, float TextLayoutthe Y coordinate of the origin of this y)TextLayoutRenders this |
| private void | |
| public boolean | Returns: true if the specified TextLayout
equals this TextLayout.the rhs)TextLayout to compare to this
TextLayoutReturns |
| private void | fastInit(char[] chars, Font font, Map<? extends AttributedCharacterIterator.
|
| public float | |
| public float | |
| public byte | |
| pack-priv static byte | |
| public float[] | Returns: the offsets array containing the baselines used for thisTextLayout.Returns the offsets array for the baselines used for this
|
| public Shape | Returns: aShape enclosing the black box bounds. This is
in standard coordinates.one end of the character range firstEndpoint, int the other end of the character range. Can be
less than secondEndpoint)firstEndpoint.Returns the black box bounds of the characters in the specified range. |
| public Rectangle2D | |
| private float[] | |
| public float[] | Returns: a two-element array containing the position and slope of the caret. The returned caret info is in baseline-relative coordinates.a hit on a character in this hit, Rectangle2D TextLayoutthe bounds to which the caret info is constructed.
The bounds is in baseline-relative coordinates. bounds)Returns information about the caret corresponding to |
| public float[] | Returns: the information about a caret corresponding to a hit. The returned caret info is in baseline-relative coordinates.a hit on a character in this hit)TextLayoutReturns information about the caret corresponding to |
| private float[] | |
| private double[] | |
| private double[] | getCaretPath(int caret, Rectangle2D bounds, boolean clipToBounds)
Return an array of four floats corresponding the endpoints of the caret x0, y0, x1, y1. |
| public Shape | Returns: aShape representing the caret. The returned
shape is in standard coordinates.the hit at which to generate the caret hit, Rectangle2D the bounds of the bounds)TextLayout to use
in generating the caret. The bounds is in baseline-relative
coordinates.Returns a |
| public Shape | Returns: aShape representing the caret. The returned
shape is in standard coordinates.the hit at which to generate the caret hit)Returns a |
| public Shape[] | Returns: an array of two paths. Element zero is the strong caret. If there are two carets, element one is the weak caret, otherwise it isnull. The returned shapes
are in standard coordinates.an offset in this offset, Rectangle2D TextLayoutthe bounds to which to extend the carets. The
bounds is in baseline-relative coordinates. bounds, TextLayout.the specified policyCaretPolicyReturns two paths corresponding to the strong and weak caret. |
| public Shape[] | Returns: two paths corresponding to the strong and weak caret as defined by theDEFAULT_CARET_POLICY. These are
in standard coordinates.an offset in this offset, Rectangle2D TextLayoutthe bounds to which to extend the carets. This is
in baseline-relative coordinates. bounds)Returns two paths corresponding to the strong and weak caret. |
| public Shape[] | Returns: two paths corresponding to the strong and weak caret as defined by theDEFAULT_CARET_POLICY. These are
in standard coordinates.an offset in this offset)TextLayoutReturns two paths corresponding to the strong and weak caret. |
| public int | Returns: the number of characters in thisTextLayout.Returns the number of characters represented by this
|
| public byte | Returns: the level of the character at the specified index.the index of the character from which to get the level index)Returns the level of the character at |
| public float | |
| public TextLayout | Returns: aTextLayout justified to the specified width.the width to use when justifying the line.
For best results, it should not be too different from the current
advance of the line. justificationWidth)Creates a copy of this |
| public LayoutPath | Returns: the layout pathReturn the LayoutPath, or null if the layout path is the default path (x maps to advance, y maps to offset). |
| public float | |
| public Shape | Returns: an area enclosing the selection. This is in standard coordinates.an endpoint in the range of characters to select firstEndpoint, int the other endpoint of the range of characters
to select. Can be less than secondEndpoint, Rectangle2D firstEndpoint. The range
includes the character at min(firstEndpoint, secondEndpoint), but
excludes max(firstEndpoint, secondEndpoint).the bounding rectangle to which to extend the selection.
This is in baseline-relative coordinates. bounds)Returns a |
| public Shape | Returns: aShape enclosing the selection. This is in
standard coordinates.an endpoint in the range of characters to select firstEndpoint, int the other endpoint of the range of characters
to select. Can be less than secondEndpoint)firstEndpoint. The range
includes the character at min(firstEndpoint, secondEndpoint), but
excludes max(firstEndpoint, secondEndpoint).Returns a |
| public int[] | Returns: an array of integers representing start/limit pairs for the selected ranges.an endpoint of the visual range firstEndpoint, TextHitInfo the other endpoint of the visual range.
This endpoint can be less than secondEndpoint)firstEndpoint.Returns the logical ranges of text corresponding to a visual selection. |
| private Rectangle2D | |
| public TextHitInfo | Returns: a hit whose caret appears at the next position to the left (top) of the caret of the provided hit, ornull.a hit on a character in this hit)TextLayout.Returns the hit for the next caret to the left (top); if no such
hit, returns |
| public TextHitInfo | Returns: a hit whose caret appears at the next position to the left (top) of the caret of the provided hit, ornull.an insertion offset in this offset, TextLayout.TextLayout.
Cannot be less than 0 or greater than this TextLayout
object's character count.the policy used to select the strong caret policyReturns the hit for the next caret to the left (top); if no
such hit, returns |
| public TextHitInfo | Returns: a hit whose caret appears at the next position to the left (top) of the caret of the provided hit, ornull.an insertion offset in this offset)TextLayout.
Cannot be less than 0 or greater than this TextLayout
object's character count.Returns the hit for the next caret to the left (top); if no
such hit, returns |
| public TextHitInfo | Returns: a hit whose caret appears at the next position to the right (bottom) of the caret of the provided hit ornull.a hit on a character in this layout hit)Returns the hit for the next caret to the right (bottom); if there
is no such hit, returns |
| public TextHitInfo | Returns: a hit whose caret appears at the next position to the right (bottom) of the caret of the provided hit, ornull.an insertion offset in this offset, TextLayout.TextLayout.
Cannot be less than 0 or greater than this TextLayout
object's character count.the policy used to select the strong caret policyReturns the hit for the next caret to the right (bottom); if no
such hit, returns |
| public TextHitInfo | Returns: a hit whose caret appears at the next position to the right (bottom) of the caret of the provided hit, ornull.an insertion offset in this offset)TextLayout.
Cannot be less than 0 or greater than the TextLayout
object's character count.Returns the hit for the next caret to the right (bottom); if no
such hit, returns |
| public Shape | Returns: aShape that is the outline of this
TextLayout. This is in standard coordinates.an optional tx)AffineTransform to apply to the
outline of this TextLayout.Returns a |
| public Rectangle | Returns: aRectangle bounding the pixels that would be affected.the frc, float FontRenderContext of the Graphics.the x-coordinate at which to render this x, float TextLayout.the y-coordinate at which to render this y)TextLayout.Returns the pixel bounds of this |
| private TextHitInfo | |
| pack-priv TextLine | |
| public float | Returns: the advance of thisTextLayout without the
trailing whitespace.Returns the advance of this |
| public Shape | Returns: aShape enclosing the selection. This is in
standard coordinates.one end of the visual selection firstEndpoint, TextHitInfo the other end of the visual selection secondEndpoint, Rectangle2D the bounding rectangle to which to extend the selection.
This is in baseline-relative coordinates. bounds)Returns a path enclosing the visual selection in the specified range,
extended to |
| public Shape | Returns: aShape enclosing the selection. This is
in standard coordinates.one end of the visual selection firstEndpoint, TextHitInfo the other end of the visual selection secondEndpoint)Returns a |
| public TextHitInfo | Returns: a hit that is on the opposite side of the specified hit's caret.the specified hit hit)Returns the hit on the opposite side of the specified hit's caret. |
| protected void | handleJustify(float
the width to use when justifying the line.
For best results, it should not be too different from the current
advance of the line. justificationWidth)Justify this layout. |
| public TextHitInfo | Returns: a hit describing the character and edge (leading or trailing) under the specified point.the x offset from the origin of this
x, float TextLayout. This is in standard coordinates.the y offset from the origin of this
y, Rectangle2D TextLayout. This is in standard coordinates.the bounds of the bounds)TextLayout. This
is in baseline-relative coordinates.Returns a |
| public TextHitInfo | Returns: a hit describing the character and edge (leading or trailing) under the specified point.the x offset from the origin of this
x, float TextLayout. This is in standard coordinates.the y offset from the origin of this
y)TextLayout. This is in standard coordinates.Returns a |
| private int | Returns: a caret index corresponding to the specified hit.a hit on a character in this hit)TextLayoutReturns a caret index corresponding to |
| public void | hitToPoint(TextHitInfo
the hit to check. This must be a valid hit on
the TextLayout. hit, Point2D the returned point. The point is in standard
coordinates. point)Convert a hit to a point in standard coordinates. |
| public boolean | Returns: true if the base direction of this
TextLayout is left-to-right; false
otherwise.Returns |
| public boolean | Returns: true if this TextLayout is vertical;
false otherwise.Returns |
| private GeneralPath | |
| private void | paragraphInit(byte aBaseline, CoreMetrics lm, Map<? extends AttributedCharacterIterator.
Initialize the paragraph-specific data. |
| private static GeneralPath | |
| private GeneralPath | |
| private static int | sameBaselineUpTo(Font font, char[] text, int start, int limit)
Return the index of the first character with a different baseline from the character at start, or limit if all characters between start and limit have the same baseline. |
| private static Font | singleFont(char[] text, int start, int limit, Map<? extends AttributedCharacterIterator.
|
| private void | |
| public String | Returns: thetextLine of this TextLayout
as a String.Overrides java. Returns debugging information for this |