-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8341670: [Text,TextFlow] Public API for Text Layout Info #1596
base: master
Are you sure you want to change the base?
8341670: [Text,TextFlow] Public API for Text Layout Info #1596
Conversation
👋 Welcome back angorya! A progress list of the required criteria for merging this PR into |
❗ This change is not yet ready to be integrated. |
@andy-goryachev-oracle |
@andy-goryachev-oracle has indicated that a compatibility and specification (CSR) request is needed for this pull request. @andy-goryachev-oracle please create a CSR request for issue JDK-8341670 with the correct fix version. This pull request cannot be integrated until the CSR request is approved. |
Webrevs
|
* | ||
* @since 24 | ||
*/ | ||
public interface LayoutInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why an interface ? It is easier to evolve a class when you want to add more info.
Also it should be sealed unless you can show why it is important that
applications can create instances.
It is being exposed for the benefit of RT but the class doc itself doesn't say when you might want to get one and why.
It might also benefit from a succinct description of what "the text layout" means and encompasses.
Oh and how do you know when it is no longer valid ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good points, thanks! please let me know if the update addressed your concerns.
a couple of notes:
- converted to an abstract class. I feel weird referring to an internal implementation at the API level (
public sealed abstract class LayoutInfo permits com.sun.javafx.text.PrismLayoutInfo
, yikes!) - why we get it: we don't explain why we need HitInfo or caret shapes or range shapes, do we?
/** | ||
* Represents an immutable snapshot of certain aspects of the text layout | ||
* in a {@code Text} or {@code TextFlow} node, | ||
* such as break up of the text into lines and the their bounds. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the their"
* such as break up of the text into lines and the their bounds. | ||
* <p> | ||
* The snapshot is valid until the layout changes due to any change that | ||
* triggers that, such as resizing of the container or modification of properties. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"until the layout changes due to any change that triggers that"
it's not wrong, but writing change twice reads a bit off.
How about
The layout snapshot is no longer valid after actions such as resizing of the container, or modification of certain properties.
For example updating the text or the font would invalidate the layout snapshot, but a change of color would not.
I still don't see how applications can KNOW this has happened.
There's nothing on the (immutable) layout which says its invalidated.
There's no property to monitor.
So people will in effect never be able to do anything but use it and toss it and get a new one every time.
Perhaps you can make it 'cheap' to do this.
Since it is immutable, the Text or TextFlow can cache it.
If it is invalidated, the Text or TextFlow should know and can flush its cached version.
So the SAME immutable instance can be returned over and over again until that happens.
People can then also use "==" or equals() to check this and save themselves validation too.
I notice you don't have equals() so probably that is needed anyway to save people from manual comparison .. also an evolution issue if the class is updated.
A consequence of that is then the text about invalidation can be updated with advice on doing a simple equals() call if they need to know if it changed. And equals will start with "==" so will be really fast to return true ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you!
I am struggling with how to explain that it should neither be cached, nor compared to another snapshot.
Maybe an example would help. Use case is an editable rich text component based on TextFlow
. The user presses HOME
and the caret needs to go to the beginning of the line. We get the LayoutInfo
, determine which line the caret is currently at and determine the start offset of that line to move the caret to.
Another example: in Text
, to determine whether the mouse is clicked inside of the text or beyond the last character (see JDK-8091012 ).
We are not expecting the application to monitor the text layout, so no notifications or observable properties or equals() are needed.
(I've added some words to that effect in Text/TextFlow.getLayoutInfo()
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clients will usually obtain a LayoutInfo in order to respond to some user action or an update of the target node.
LayoutInfo should be considered an ephemeral snapshot, to be used and immediately discarded.
Clients should always obtain a new one when they need to respond again as likely the trigger to respond again means the target node has changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although I don't see the harm in being more efficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hear a suggestion to create a think wrapper/proxy instead of the snapshot, right?
This might, in fact, be a better idea. The API stays the same.
Let me think.
Thanks!
@Jugen do you think this PR addresses the issue https://bugs.openjdk.org/browse/JDK-8091012 ? |
One additional thought/question: There is a need to extract a few more data points from the text layout in addition to the text lines, specifically:
We currently have There is no corresponding Granted, one can attempt to analyze the So the question are:
|
@andy-goryachev-oracle First off thanks for all your work on the richtext control you're working on.
Unfortunately I don't think this helps for the hit problem Tomas was having, see However I was able to use all the methods in the |
Wrt to rangeShape & underlineShape: RichTextFX quite successfully uses the PathElement[] returned to it's advantage and so wouldn't gain from Rectangles.
|
thank you so much @Jugen for checking and the feedback! I see that a lot of what is being done in TextFlowExt should become unnecessary with the new API (or maybe even the whole thing might become unnecessary). Also, while the new API do not address JDK-8091012 directly, that is, there is no field added to Anyway, thank you. I agree with you that we should add T/TF.strikeThroughShape() for consistency sake. |
Thank you you all @prrace @Jugen @kevinrushforth for the feedback! |
/issue add JDK-8341672 |
@andy-goryachev-oracle |
@andy-goryachev-oracle This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
@prrace please take a look |
@andy-goryachev-oracle This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
@andy-goryachev-oracle this pull request can not be integrated into git checkout ag.text.layout.api
git fetch https://git.openjdk.org/jfx.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push |
@andy-goryachev-oracle This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
keep-alive |
@jperedadnr Can you review this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall it looks really good, the new API is quite useful in some scenarios. I've done some successful tests and left some comments and questions about the API.
* @param leading whether the caret is biased on the leading edge of the character | ||
* @return the caret geometry | ||
*/ | ||
public float[] getCaretInf(int offset, boolean leading); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a typo or is there a reason for naming this method getCaretInf
instead of getCaretInfo
?
* | ||
* @param start the start offset | ||
* @param end the end offset | ||
* @param the type of the geometry |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing type
after @param
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eagle eye!
public final class TextUtils { | ||
/** | ||
* Queries the range geometry of the range of text within the text layout as | ||
* an array of {@code PathElement}s, for for one of the three possible types: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo for for
* <li>{@link #TYPE_STRIKETHROUGH} - strike-through shape | ||
* <li>{@link #TYPE_TEXT} - text selection shape | ||
* <li>{@link #TYPE_UNDERLINE} - underline shape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing TextLayout
before each type #TYPE_...
/** Callback to be called for each rectangular shape */ | ||
@FunctionalInterface | ||
public static interface GeometryCallback { | ||
public void addRectangle(float left, float top, float right, float bottom); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that you use float
here because all the calculations in PrismTextLayout::getRange
use floats (from TextRun
).
However, the calculations down the line to generate the Rectangle2D
mix floats and doubles without any casting (TextUtils::getRange
with implicit casts from double to float, PrismLayoutInfo::getGeometry
with float and double sums).
Do you think we could use doubles here instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the use of float in this interface is correct, but the code in TextUtils::getRange is bad, and i should feel bad. good catch!
} | ||
|
||
private TextLine line(int ix) { | ||
return layout.getLines()[ix]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bound check? In any case, this private method is not used.
* | ||
* @since 25 | ||
*/ | ||
public record TextLineInfo(int start, int end, Rectangle2D bounds) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that you are using javafx.geometry.Rectangle2D
for this PR to hold all caret/layout/line information related to bounds, and that seems to work fine so far.
However, given that the TextFlow
/Text
APIs already provide information using javafx.scene.shape.PathElement
, and given that as a result of this PR users will be able to query either old and new APIs, I was wondering if another Shape
subclass might be better for the new API instead of Rectangle2D
: As is now, you can't easily combine Rectangle2D
with PathElement
objects (in fact you need to do a manual conversion in TextUtils::getRange
or TextUtils::toRectangle2D
).
Have you thought about using javafx.scene.shape.Rectangle
instead? That would simplify your internal operations, and would be more easy to use for developers (for instance, drawing new shapes based the LayoutInfo, like in your MonkeyTester LayoutInfoVisualizer
class).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was intentional decision to use the light weight Rectangle2D
class instead of a Shape
(which extends Node
).
* @param includeLineSpacing determines whether the result includes the line spacing | ||
* @return the immutable list of {@code Rectangle2D} objects | ||
*/ | ||
public abstract List<Rectangle2D> selectionShape(int start, int end, boolean includeLineSpacing); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comment below about using javafx.scene.shape.Rectangle
instead of javafx.geometry.Rectangle2D
: otherwise the API is misleading: selectionShape
doesn't return a Shape
subclass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. The word 'shape' might be confusing, although it is still correct in human terms (it is a shape, after all). I think it's ok, but we could use the word "geometry" for these methods selectionGeometry
/ strikeThroughGeometry
/ underlineGeometry
, what do other people think?
As for using javafx.scene.shape.Rectangle
- it is too heavy of an object, with properties and everything, it's not what we ask from this method.
* @param includeLineSpacing determines whether the result includes the line spacing | ||
* @return the immutable list of {@code Rectangle2D} objects | ||
*/ | ||
public abstract List<Rectangle2D> selectionShape(int start, int end, boolean includeLineSpacing); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I take that LayoutInfo::selectionShape
should match the existing API TextFlow::rangeShape
for the same selection coordinates.
I wonder if you have tested this, with different insets. I take that with your current implementation, for Rectangle2D
objects, it makes sense to have the insets of the TextFlow/Text node, but shapes don't include them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've got https://bugs.openjdk.org/browse/JDK-8341438 and a possibility of a regression if we change the existing methods.
I would very much like to get your thoughts on this.
@Override | ||
public Rectangle2D getBounds(boolean includeLineSpacing) { | ||
BaseBounds b = layout.getBounds(); | ||
Insets m = insets(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my comments below about adding the insets to the layout information. Maybe we could also add another boolean includeInsets
for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! I don't think this is needed: the call returns the layout bounds (in the owning node coordinate system).
The insets only change the position of the layout within the owning node, unlike lineSpacing which actually changes the shape by adding or removing the last line spacing.
I think this method is ok as is.
thank you for thoughtful comments, @jperedadnr ! |
@andy-goryachev-oracle This pull request has been inactive for more than 4 weeks and will be automatically closed if another 4 weeks passes without any activity. To avoid this, simply add a new comment to the pull request. Feel free to ask for assistance if you need help with progressing this pull request towards integration! |
please review |
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("PrismCaretInfo{parts=["); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you shouldn't leak implementation details via the toString()
method (the name of the implementing class, as well as parts=
, which is not a term that appears in the API). Maybe just drop PrismCaretInfo{parts=
entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense.
On a related note, we don't seem to have a consistent standard for toString() output (probably no one does!), so maybe it's worth polling the devs about it.
Standardized log output might be useful when one has to go through masses of old/transient logs, or when scraping the logs for analytics. A JSON output might be useful since it offers a cheap structured form, although it suffers from two issues: verbosity and difficulty to encode binary data and object types easily.
Any ideas?
* @param leading whether the caret is biased on the leading edge of the character | ||
* @return the caret geometry | ||
*/ | ||
public float[] getCaretGeometry(int offset, boolean leading); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this method throw any exceptions? If so, please specify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method does not throw any exceptions, so none are specified.
* @param type the type of the geometry | ||
* @param client the callback to invoke for each rectangular shape | ||
*/ | ||
public void getRange(int start, int end, int type, GeometryCallback client); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this method throw any exceptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no.
* | ||
* @since 25 | ||
*/ | ||
public abstract class LayoutInfo { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any exceptions thrown by methods of this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only public abstract TextLineInfo getTextLine(int index, boolean includeLineSpacing);
(added)
@@ -56,6 +56,8 @@ protected CaretInfo() { | |||
* | |||
* @param index the line index | |||
* @return the bounds of the caret segment | |||
* @throws IndexOutOfBoundsException if the index is outside of the range if the index is out of range |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this sentence repeats the same thing twice, maybe check again. You can probably just say if the index is out of range {@code (...)}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
daylight saving time strikes!
thanks for noticing.
Please refer to
https://github.com/andy-goryachev-oracle/Test/blob/main/doc/Text/LayoutInfo.md
The RichTextArea control (JDK-8301121), or any custom control that needs non-trivial navigation within complex or wrapped text needs a public API to get information about text layout.
This change fixes the missing functionality by adding a new public method to the
Text
andTextFlow
classes.:The
LayoutInfo
provides a view into the text layout withinText
/TextFlow
nodes such as:This PR also adds the missing
strikeThroughShape()
method to complement the existingunderlineShape()
andrangeShape()
for consistency sake:WARNING
Presently, information obtained via certain Text/TextField methods is incorrect with non-zero padding and borders, see JDK-8341438.
This PR provides correct answers in the new API, leaving the behavior of the existing methods unchanged (there is a compatibility risk associated with trying to fix JDK-8341438 ).
Testing
The new APIs can be visually tested using the Monkey Tester on this branch:
https://github.com/andy-goryachev-oracle/MonkeyTest/tree/text.layout.api
in the Text and TextFlow pages:

Two very basic headful tests have been added.
See Also
FXMisc/RichTextFX#1246
/reviewers 2
/csr
Progress
Issues
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jfx.git pull/1596/head:pull/1596
$ git checkout pull/1596
Update a local copy of the PR:
$ git checkout pull/1596
$ git pull https://git.openjdk.org/jfx.git pull/1596/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 1596
View PR using the GUI difftool:
$ git pr show -t 1596
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jfx/pull/1596.diff
Using Webrev
Link to Webrev Comment