Skip to content

8341670: [Text,TextFlow] Public API for Text Layout Info#1596

Closed
andy-goryachev-oracle wants to merge 65 commits intoopenjdk:masterfrom
andy-goryachev-oracle:ag.text.layout.api
Closed

8341670: [Text,TextFlow] Public API for Text Layout Info#1596
andy-goryachev-oracle wants to merge 65 commits intoopenjdk:masterfrom
andy-goryachev-oracle:ag.text.layout.api

Conversation

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor

@andy-goryachev-oracle andy-goryachev-oracle commented Oct 8, 2024

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 and TextFlow classes.:

    /**
     * Obtains the snapshot of the current text layout information.
     * @return the layout information
     * @since 25
     */
    public final LayoutInfo getLayoutInfo()

The LayoutInfo provides a view into the text layout within Text/TextFlow nodes such as:

  • caret information
  • text lines: offsets and bounds
  • overall layout bounds
  • text selection geometry
  • strike-through geometry
  • underline geometry

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:
Screenshot 2024-11-04 at 11 38 21

Two very basic headful tests have been added.

See Also

FXMisc/RichTextFX#1246

/reviewers 2
/csr


Progress

  • Change must not contain extraneous whitespace
  • Change requires CSR request JDK-8344648 to be approved
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issues

  • JDK-8341670: [Text,TextFlow] Public API for Text Layout Info (Enhancement - P4)
  • JDK-8344648: [Text,TextFlow] Public API for Text Layout Info (CSR)

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

@bridgekeeper
Copy link
Copy Markdown

bridgekeeper Bot commented Oct 8, 2024

👋 Welcome back angorya! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Oct 8, 2024

@andy-goryachev-oracle This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8341670: [Text,TextFlow] Public API for Text Layout Info

Reviewed-by: kcr, mstrauss

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 5 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Oct 8, 2024

@andy-goryachev-oracle
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

@openjdk openjdk Bot added the csr Need approved CSR to integrate pull request label Oct 8, 2024
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Oct 8, 2024

@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.

@andy-goryachev-oracle andy-goryachev-oracle marked this pull request as ready for review October 8, 2024 18:16
@openjdk openjdk Bot added the rfr Ready for review label Oct 8, 2024
@mlbridge
Copy link
Copy Markdown

mlbridge Bot commented Oct 8, 2024

*
* @since 24
*/
public interface LayoutInfo {
Copy link
Copy Markdown
Collaborator

@prrace prrace Oct 9, 2024

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 ?

Copy link
Copy Markdown
Contributor Author

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.
Copy link
Copy Markdown
Collaborator

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.
Copy link
Copy Markdown
Collaborator

@prrace prrace Oct 9, 2024

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 ..

Copy link
Copy Markdown
Contributor Author

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() )

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Contributor Author

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!

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

@Jugen do you think this PR addresses the issue https://bugs.openjdk.org/browse/JDK-8091012 ?

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

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:

  • bounds of the (selection) range, as rectangles
  • strikethrough range shape (as PathElement[] and/or rectangles)
  • underline range shape (as PathElement[] and/or rectangles)

We currently have Text/TextFlow.underlineShape(int,int), .rangeShape(int,int) which return PathElement[] in an undocumented way.

There is no corresponding .strikeThroughShape() method, or a way to retrieve the geometric bounds as lines/rectangles.

Granted, one can attempt to analyze the PathElement[] and convert it to geometry, but it seems we should avoid asking the clients to do that.

So the question are:

  • should we add these data points to LayoutInfo
  • if so, in what form? i.e. as individual methods or as an array of Line/Rectangle objects,
  • should we add the .strikeThroughShape() to Text/TextFlow, or simply adding it here should be sufficient?

@Jugen
Copy link
Copy Markdown

Jugen commented Oct 11, 2024

@andy-goryachev-oracle First off thanks for all your work on the richtext control you're working on.

@Jugen do you think this PR addresses the issue https://bugs.openjdk.org/browse/JDK-8091012 ?

Unfortunately I don't think this helps for the hit problem Tomas was having, see hit(x,y) in RichTextFX's TextFlowExt for the relevant code.

However I was able to use all the methods in the LayoutInfo interface of this PR in RichTextFX to replace the custom layout info code, which is great.

@Jugen
Copy link
Copy Markdown

Jugen commented Oct 11, 2024

Wrt to rangeShape & underlineShape: RichTextFX quite successfully uses the PathElement[] returned to it's advantage and so wouldn't gain from Rectangles.

.strikeThroughShape() should be added to Text/TextFlow for consistency sake.

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

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 HitInfo, it looks like the information provided by the LayoutInfo should be sufficient to implement what you guys need as a utility, since you will have the geometry of text lines in the layout, right?

Anyway, thank you. I agree with you that we should add T/TF.strikeThroughShape() for consistency sake.

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

Thank you you all @prrace @Jugen @kevinrushforth for the feedback!
I am going to take this PR back to draft to make the suggested changes.

@andy-goryachev-oracle andy-goryachev-oracle marked this pull request as draft October 11, 2024 17:15
@openjdk openjdk Bot removed the rfr Ready for review label Oct 11, 2024
@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

/issue add JDK-8341672

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Oct 11, 2024

@andy-goryachev-oracle
Adding additional issue to issue list: 8341672: [Text/TextFlow] getRangeInfo.

@andy-goryachev-oracle andy-goryachev-oracle marked this pull request as ready for review October 11, 2024 22:29
@openjdk openjdk Bot added the rfr Ready for review label Oct 11, 2024
public List<TextLineInfo> getTextLines(boolean includeLineSpacing) {
TextLine[] lines = layout.getLines();
Insets m = insets();
double dx = m.getLeft(); // TODO rtl?
Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
Member

@kevinrushforth kevinrushforth left a 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.
Copy link
Copy Markdown
Member

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"?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I asked Phil offline and he confirmed that this should be "font" not "glyph", which makes sense, since what you want to know is how tall the line should be to accommodate any glyph that you might display with that font.

* <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.
Copy link
Copy Markdown
Member

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"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. It's correct as is.

* 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
Copy link
Copy Markdown
Member

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"

@kevinrushforth
Copy link
Copy Markdown
Member

@prrace Can you also review the API changes?

@kevinrushforth
Copy link
Copy Markdown
Member

I ran the tests included with this fix on my macOS 14 system and see 3 failures:

$ gradle --continue --info -PTEST_ONLY=true -PFULL_TEST=true -PUSE_ROBOT=true :systemTests:test --tests TextFlow_TextLayout_Test --tests Text_TextLayout_Test

Text_TextLayout_Test > testSelection() FAILED
    org.opentest4j.AssertionFailedError: expected: <0.0> but was: <-23.203125>
        at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:86)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:81)
        at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1014)
        at app//test.robot.javafx.scene.Text_TextLayout_Test.testSelection(Text_TextLayout_Test.java:205)

Text_TextLayout_Test > testTextLines() FAILED
    org.opentest4j.AssertionFailedError: expected: <0.0> but was: <-23.203125>
        at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:86)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:81)
        at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1014)
        at app//test.robot.javafx.scene.Text_TextLayout_Test.testTextLines(Text_TextLayout_Test.java:155)

Text_TextLayout_Test > testCaretInfo() FAILED
    org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
        at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at app//org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
        at app//org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
        at app//org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:31)
        at app//org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:183)
        at app//test.robot.javafx.scene.Text_TextLayout_Test.testCaretInfo(Text_TextLayout_Test.java:87)

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

andy-goryachev-oracle commented Jun 10, 2025

good catch! the failures not specific to macOS 14.
modified the headful tests to use top text origin (all the other choices are handled by headless tests).

launched an internal headful run: job 378

edit: apart from one unrelated failure (QPathTest timeout on macOS) all headful tests pass.

Copy link
Copy Markdown
Member

@kevinrushforth kevinrushforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks good. The tests look good, although I noted a few "assertEquals" in the test with expected and actual parameters reserved (meaning the error message would be confusing if they ever failed).

CaretInfo ci = la.caretInfoAt(0, true);

// caret is one line
assertEquals(ci.getSegmentCount(), 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected and actual are reversed, here and a few others below (most are OK).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the other test as well... good catch!


assertEquals(la.getTextLineCount(), 3);
List<TextLineInfo> ls = la.getTextLines(false);
assertNotNull(ls);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: maybe check the following, too?

        assertEquals(3, ls.size());

CaretInfo ci = la.caretInfoAt(0, true);

// caret is one line
assertEquals(ci.getSegmentCount(), 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected and actual are reversed, here and a few others below (most are OK).

Copy link
Copy Markdown
Member

@kevinrushforth kevinrushforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@openjdk openjdk Bot added ready Ready to be integrated and removed csr Need approved CSR to integrate pull request labels Jun 17, 2025
@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

/integrate

@openjdk
Copy link
Copy Markdown

openjdk Bot commented Jun 17, 2025

Going to push as commit 1ea980e.
Since your change was applied there have been 5 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Jun 17, 2025
@openjdk openjdk Bot closed this Jun 17, 2025
@openjdk openjdk Bot removed ready Ready to be integrated rfr Ready for review labels Jun 17, 2025
@openjdk
Copy link
Copy Markdown

openjdk Bot commented Jun 17, 2025

@andy-goryachev-oracle Pushed as commit 1ea980e.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@andy-goryachev-oracle
Copy link
Copy Markdown
Contributor Author

Thank you all for reviewing this PR!

@andy-goryachev-oracle andy-goryachev-oracle deleted the ag.text.layout.api branch June 17, 2025 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

7 participants