-
Notifications
You must be signed in to change notification settings - Fork 516
8341670: [Text,TextFlow] Public API for Text Layout Info #1596
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
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 |
* @return an array of {@code PathElement} which can be used to create a {@code Shape} | ||
* @since 25 | ||
*/ | ||
public final PathElement[] strikeThroughShape(int start, int end) { |
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.
Perhaps this method should be renamed to getStrikeThroughShape()
to be consistent with the proposed evolution of the buggy methods in https://bugs.openjdk.org/browse/JDK-8341438
(same in Text
)
I'll update the CSR once the javadoc settles. |
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 changes to the API look good. I left a few wording suggestions. Once those are resolved, update the CSR.
I see that this PR lists JDK-8341671 and JDK-8341672 as additional issues solved. They are included as part of the text LayoutInfo, so it would be better to close them as duplicates and remove them from this PR (using /issue remove
).
* <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. |
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.
Oh, so it isn't a view but a copy. I think what you changed it to is closer. The "object which provides a snapshot" is a little misleading, though. It is this method that provides the snapshot. The returned TextLayout object is the snapshot. Maybe something like this?
* Returns a copy of the of the text layout geometry for this node. This copy is a snapshot
* of the text layout at the time the method is called.
* 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. | ||
* | ||
* @return the layout information |
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.
maybe "a copy of the ..."?
@@ -698,4 +714,28 @@ public TextLayout getTextLayout(TextFlow f) { | |||
} | |||
}); | |||
} | |||
|
|||
/** | |||
* Returns the object which provides a snapshot of the text layout geometry for this node. |
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.
Same comment as for Text::getLayoutInfo
import com.sun.javafx.text.PrismLayoutInfo; | ||
|
||
/** | ||
* Provides a snapshot of the text layout geometry in a {@code Text} or a {@code TextFlow} node, |
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 might either remove the word "Provides" or else replace it with "Holds".
/issue remove JDK-8341671 |
@andy-goryachev-oracle |
/issue remove JDK-8341672 |
@andy-goryachev-oracle |
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.
Updated API looks good. I left a comment on the CSR.
I left a comment on one other naming / semantic issue that I just noticed.
* @return an array of {@code PathElement} which can be used to create a {@code Shape} | ||
* @since 25 | ||
*/ | ||
public final PathElement[] getStrikeThroughShape(int start, int end) { |
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 just noticed that this is named getStrikeThroughShape
rather than strikeThroughShape
. The latter would match the similar rangeShape
, caretShape
, and underlineShape
methods. We had discussed that you might propose to fix JDK-8341438 by deprecating rangeShape
and underlineShape
and adding new getRangeShape
and getUnderlineShape
methods (although that might be problematic given the caretShape
property, which necessarily already has a no-arg getCaretShape()
method).
Did you name this method getStrikeThroughShape
with the intention that it would have the fixed semantics of applying the insets from its inception? If so, then I recommend documenting that fact in this method.
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.
Yes, you are right - and I plan to address this in https://bugs.openjdk.org/browse/JDK-8341438 (as it will be a separate change).
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 discussed this with @kevinrushforth - I am going to remove the getStrikeThroughShape()
method from Text/TextFlow in this PR, and add it under either a separate ticket, or as a part of https://bugs.openjdk.org/browse/JDK-8341438
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.
Created https://bugs.openjdk.org/browse/JDK-8357594 for Text::strikeThroughShape
and TextFlow::getStrikeThroughShape
.
public List<TextLineInfo> getTextLines(boolean includeLineSpacing) { | ||
TextLine[] lines = layout.getLines(); | ||
Insets m = insets(); | ||
double dx = m.getLeft(); // TODO rtl? |
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 comment looks like it's a question to the reader. Does the code not work for RTL layouts?
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.
JavaFX does not support RTL fully - there is a whole series of issues waiting to be fixed.
See https://bugs.openjdk.org/browse/JDK-8343557
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 API looks good given the removal of the Text/TextFlow getStrikeTrhoughShape methods. I added one question about whether you want to consider adding a getter to CaretInfo to return the list of rectangles in a single call, but I'll leave that up to you.
I pointed out what I think are a couple doc typos in TextLine info.
I think this is close to being ready. I'll review the implementation and do some testing.
* for left-aligned text. | ||
* <li> | ||
* {@code minY} - the ascent of the line (negative). | ||
* The ascent of the line is the max ascent of all fonts in the line. |
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.
Shouldn't that be "glyphs" not "fonts"?
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 definition is copied from com.sun.javafx.scene.text.TextLine::getBounds
L58.
I'll let Phil confirm that, but since it's a max ascent, it might be a function of the font rather than the particular glyphs.
* <li> | ||
* {@code height} - the height of the line. | ||
* The height of the line is sum of the max ascent, max descent, and | ||
* max line gap of all the fonts in the line. |
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.
Shouldn't that be "glyphs" not "fonts"?
* The ascent of the line is the max ascent of all fonts in the line. | ||
* <li> | ||
* {@code width} - the width of the line. | ||
* The width for the line is sum of all the run widths in the line, it is not |
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.
minor: "for the line" --> "of the line"
* | ||
* @return the number of segments representing the caret | ||
*/ | ||
public abstract int getSegmentCount(); |
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.
Do you think it's worth adding a List<Rectangle2D>getSegments()
method in addition to (or instead of) getSegmentCount
and getSegmentAt
?
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.
Probably unnecessary, because the caret has one segment most of the time. When bidi is involved, the caret might have 2 segments. It will never have 3 or more segments.
@prrace Can you also review the API changes? |
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:WARNINGS
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 ).
Also, the RTL support is out of scope for this PR, due to multiple pre-existing conditions, see https://bugs.openjdk.org/browse/JDK-8343557
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
Reviewers
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