Summary
Adds public LayoutInfo getLayoutInfo()
method to Text and TextFlow classes for the purpose of getting additional information about the layout of text.
Problem
Applications dealing with text-based Nodes, such as those providing rich text editors, need more information about the layout of text than currently provided via public API.
While partial information about the text layout can be obtained by reverse-engineering undocumented PathElement[]
returned by the existing caretShape()
and rangeShape()
methods in the Text and TextFlow classes,
other information, such as strike-through geometry, cannot be obtained via public API at all.
Reverse engineering is currently successful because developers realize that the PathElement[]
array contains
a sequence of MoveTo
and LineTo
elements, and nothing else. This may or may not be true in the future,
and in general the structure of this array, while suitable for using with the Path
class, is not documented.
The application and library developers typically resolve to creating custom nodes that extend TextFlow
,
or use the hacks like drawing mid-height lines for the strike-through decoration.
Solution
Please refer to https://github.com/andy-goryachev-oracle/Test/blob/main/doc/Text/LayoutInfo.md
Specification
javafx.scene.text.Text
/**
* Returns the object which provides a view into the text layout for this node, which allows for querying
* the details of the layout.
* <p>
* While there is no general guarantee that successive invocations of this method return the same instance,
* it is safe to either cache this object or call this method each time, since the information obtained from
* this lightweight object remains valid until the next layout cycle.
* <p>
* The information obtained after the next layout cycle might be different as a result
* of actions such as resizing of the container, or modification of certain properties.
* For example updating the text or the font might change the layout, but a change of color would not.
*
* @return the layout information
* @since 24
*/
public final LayoutInfo getLayoutInfo()
javafx.scene.text.TextFlow
/**
* Returns the object which provides a view into the text layout for this node, which allows for querying
* the details of the layout.
* <p>
* While there is no general guarantee that successive invocations of this method return the same instance,
* it is safe to either cache this object or call this method each time, since the information obtained from
* this lightweight object remains valid until the next layout cycle.
* <p>
* The information obtained after the next layout cycle might be different as a result
* of actions such as resizing of the container, or modification of certain properties.
* For example updating the text or the font might change the layout, but a change of color would not.
*
* @return the layout information
* @since 24
*/
public final LayoutInfo getLayoutInfo()
javafx.scene.text.LayoutInfo
This class provides a view into the text layout used by the Text and TextFlow nodes, with the purpose of querying the details of the layout such as break up of the text into lines, as well as geometry of other shapes derived from the layout (selection, underline, etc.).
The information obtained via this object may change after the next layout cycle, which may come as a result of actions such as resizing of the container, or modification of certain properties. For example updating the text or the font might change the layout, but a change of color would not.
The LayoutInfo class provides the following methods:
public Rectangle2D getBounds(boolean includeLineSpacing)
- returns the logical bounds of the layoutpublic int getTextLineCount()
- returns the number of text lines in the layoutpublic List<TextLineInfo> getTextLines(boolean includeLineSpacing)
- returns information about text lines in the layoutpublic TextLineInfo getTextLine(int index, boolean includeLineSpacing)
- returns the information about the text line at the given indexpublic List<Rectangle2D> selectionShape(int start, int end, boolean includeLineSpacing)
- returns the geometry of the text selection for the given start and end offsetspublic List<Rectangle2D> strikeThroughShape(int start, int end)
- returns the geometry of the strike-through shape for the given start and end offsetspublic List<Rectangle2D> underlineShape(int start, int end)
- returns the geometry of the underline shape for the given start and end offsetspublic CaretInfo caretInfo(int charIndex, boolean leading)
- returns the caret geometry for the given character index and the character bias
javafx.scene.text.TextLineInfo
Provides the information about a text line in a text layout:
start
the start offset for the lineend
the end offset for the line (index of the last character + 1)bounds
the bounds of the text line, in local coordinates
javafx.scene.text.CaretInfo
Provides the information associated with the caret:
public int getPartCount()
- returns the number of parts representing the caretpublic Rectangle2D getPartAt(int index)
- returns the geometry of the part at the specified index
- csr of
-
JDK-8341670 [Text,TextFlow] Public API for Text Layout Info
- In Progress