Skip to content

Commit c20f6d0

Browse files
8304831: TextFlow.hitTest.insertionIndex incorrect with surrogate pairs
Reviewed-by: angorya, prr
1 parent 72be85e commit c20f6d0

File tree

3 files changed

+408
-1
lines changed

3 files changed

+408
-1
lines changed

modules/javafx.graphics/src/main/java/com/sun/javafx/text/PrismTextLayout.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,14 @@ public PathElement[] getCaretShape(int offset, boolean isLeading,
422422
@Override
423423
public Hit getHitInfo(float x, float y) {
424424
int charIndex = -1;
425+
int insertionIndex = -1;
425426
boolean leading = false;
426427

427428
ensureLayout();
428429
int lineIndex = getLineIndex(y);
429430
if (lineIndex >= getLineCount()) {
430431
charIndex = getCharCount();
432+
insertionIndex = charIndex + 1;
431433
} else {
432434
if (isMirrored()) {
433435
x = getMirroringWidth() - x;
@@ -450,13 +452,30 @@ public Hit getHitInfo(float x, float y) {
450452
int[] trailing = new int[1];
451453
charIndex = run.getStart() + run.getOffsetAtX(x, trailing);
452454
leading = (trailing[0] == 0);
455+
456+
insertionIndex = charIndex;
457+
if (getText() != null && insertionIndex < getText().length) {
458+
if (!leading) {
459+
BreakIterator charIterator = BreakIterator.getCharacterInstance();
460+
charIterator.setText(new String(getText()));
461+
int next = charIterator.following(insertionIndex);
462+
if (next == BreakIterator.DONE) {
463+
insertionIndex += 1;
464+
} else {
465+
insertionIndex = next;
466+
}
467+
}
468+
} else if (!leading) {
469+
insertionIndex += 1;
470+
}
453471
} else {
454472
//empty line, set to line break leading
455473
charIndex = line.getStart();
456474
leading = true;
475+
insertionIndex = charIndex;
457476
}
458477
}
459-
return new Hit(charIndex, -1, leading);
478+
return new Hit(charIndex, insertionIndex, leading);
460479
}
461480

462481
@Override

0 commit comments

Comments
 (0)