Hitting an empty Text node, i.e. a Text whose test is an empty string, returns insertion index 1, which is out of text bounds. The only valid position within an empty string is 0. The test case is below.
I realize that impl_* methods are not public API, but I'm using them in RichTextFX [1] anyway because there's no public API for this functionality. If you are planning/willing to expose more public API on Text/TextFlow, I will be happy to summarize what private API is currently used in RichTextFX.
The workaround is to check for empty text and treat it specially (i.e. don't call impl_hitTestChar on an empty text at all).
import javafx.geometry.Point2D;
import javafx.scene.text.Text;
import com.sun.javafx.scene.text.HitInfo;
public class HitEmptyText {
public static void main(String[] args) {
Text text = new Text("");
HitInfo hit = text.impl_hitTestChar(new Point2D(1, 1));
int insertionIndex = hit.getInsertionIndex();
System.out.println(hit);
System.out.println("insertion index: " + insertionIndex); // prints 1
assert insertionIndex == 0; // because there's no other valid position
}
}
[1] https://github.com/TomasMikula/RichTextFX