JDK-8136350 : [TextFlow] Hit test API for TextFlow
  • Type: Sub-task
  • Component: javafx
  • Sub-Component: graphics
  • Affected Version: 8
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-09-11
  • Updated: 2016-03-14
  • Resolved: 2016-03-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
This issue can be thought of as a subset of JDK-8092278 that can hopefully be implemented much sooner. The scope of this issue is solely hit test API (i.e. given the (x, y) coordinates, give me the position in text), *not* selection API a la DOM Range.

It was possible to work around the lack of a hit test API using reflection and private (com.sun.*) API, but this workaround will stop working in JDK 9.

A hit test must be able to determine the character that was hit, if any, and the insertion index (for caret positioning).

I propose adding the following method to TextFlow:

    public HitInfo hitTest(double x, double y);

where HitInfo has the following interface

    public class HitInfo {
        public OptionalInt getCharacterIndex();
        public int getInsertionIndex();
    }

getCharacterIndex() returning OptionalInt allows to distinguish whether a character was hit at all (JDK-8091012).

getInsertionIndex() should take character clusters (JDK-8092327 point 2.) into account.

I implemented the hit test method in RichTextFX based on Felipe's suggestion (https://bugs.openjdk.java.net/browse/JDK-8091012?focusedCommentId=13795297&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13795297). It does not handle clusters, but I am happy to contribute the code.
Comments
One other thing to consider is to determine the character closest to the hit position if no character was hit. Consider hitting beyond the end of a wrapped line. We need to be able to determine: - the insertion index (covered by getInsertionIndex() above); - that no character was hit (covered by getCharacterIndex() above); - on which line to place the caret (insertionIndex alone is ambiguous in this case: put the caret at the end of the first line or at the beginning of the second one?). To support this, we might want to add int getNearestCharacterIndex(); or InsertionBias getInsertionBias(); where InsertionBias is something like enum { Before, After }.
22-09-2015

Glad to see this targeted for 9. It also makes sense to coordinate with JDK-8136971.
22-09-2015