Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,14 @@ public PathElement[] getCaretShape(int offset, boolean isLeading,
@Override
public Hit getHitInfo(float x, float y) {
int charIndex = -1;
int insertionIndex = -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

[question]

Currently, there are a few scenarios when a negative insertionIndex is passed down to HitInfo. This will trigger a similar (and probably incorrect) computation of the insertion index in HitInfo, see for example JDK-8302511.

My question is - should we instead resolve the insertion index always?

Copy link
Member Author

Choose a reason for hiding this comment

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

After current fix, only scenario where insertionIndex not getting initialized will be when lineIndex >= getLineCount() is true. So even if insertionIndex -1, text will be null and HitInfo.getInsertionIndex() will not have any computation to perform. So we can resolve the insertion index always I think. Please let me know your thoughts on this.

boolean leading = false;

ensureLayout();
int lineIndex = getLineIndex(y);
if (lineIndex >= getLineCount()) {
charIndex = getCharCount();
insertionIndex = charIndex + 1;
} else {
if (isMirrored()) {
x = getMirroringWidth() - x;
Expand All @@ -450,13 +452,30 @@ public Hit getHitInfo(float x, float y) {
int[] trailing = new int[1];
charIndex = run.getStart() + run.getOffsetAtX(x, trailing);
leading = (trailing[0] == 0);

insertionIndex = charIndex;
if (getText() != null && insertionIndex < getText().length) {
if (!leading) {
BreakIterator charIterator = BreakIterator.getCharacterInstance();
charIterator.setText(new String(getText()));
int next = charIterator.following(insertionIndex);
if (next == BreakIterator.DONE) {
insertionIndex += 1;
} else {
insertionIndex = next;
}
}
} else if (!leading) {
insertionIndex += 1;
}
} else {
//empty line, set to line break leading
charIndex = line.getStart();
leading = true;
insertionIndex = charIndex;
}
}
return new Hit(charIndex, -1, leading);
return new Hit(charIndex, insertionIndex, leading);
}

@Override
Expand Down
Loading