Skip to content

Commit 6f8cec7

Browse files
authored
Fix selection shape (#1067)
Fixed right hand padding not being honored. Also fixed incorrect shape when line spacing is zero and inter-paragraph padding is specified.
1 parent 25ebc97 commit 6f8cec7

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -354,32 +354,38 @@ private PathElement[] getRangeShapeSafely(int start, int end) {
354354
shape = getRangeShape(start, end);
355355
} else {
356356
// Selection includes a newline character.
357+
double width = getWidth() - getInsets().getRight() - getInsets().getLeft();
357358
if (paragraph.length() == 0) {
358359
// empty paragraph
359-
shape = createRectangle(0, 0, getWidth(), getHeight());
360+
shape = createRectangle(0, 0, width, getHeight());
360361
} else if (start == paragraph.length()) {
361362
// selecting only the newline char
362363

363364
// calculate the bounds of the last character
364365
shape = getRangeShape(start - 1, start);
365366
LineTo lineToTopRight = (LineTo) shape[shape.length - 4];
366-
shape = createRectangle(lineToTopRight.getX(), lineToTopRight.getY(), getWidth(), getHeight());
367+
shape = createRectangle(lineToTopRight.getX(), lineToTopRight.getY(), width, getHeight());
367368
} else {
368369
shape = getRangeShape(start, paragraph.length());
369370
// Since this might be a wrapped multi-line paragraph,
370371
// there may be multiple groups of (1 MoveTo, 4 LineTo objects) for each line:
371372
// MoveTo(topLeft), LineTo(topRight), LineTo(bottomRight), LineTo(bottomLeft)
372373

373-
// We only need to adjust the top right and bottom right corners to extend to the
374-
// width/height of the line, simulating a full line selection.
374+
// Adjust the top right, and bottom left & right corners to extend to the
375+
// correct width and height of the line, simulating a full line selection.
375376
int length = shape.length;
376377
if ( length > 3 ) // Prevent IndexOutOfBoundsException accessing shape[] issue #689
377378
{
378379
int bottomRightIndex = length - 3;
379380
int topRightIndex = bottomRightIndex - 1;
381+
int bottomLeftIndex = bottomRightIndex + 1;
382+
380383
LineTo lineToTopRight = (LineTo) shape[topRightIndex];
381-
shape[topRightIndex] = new LineTo(getWidth(), lineToTopRight.getY());
382-
shape[bottomRightIndex] = new LineTo(getWidth(), getHeight());
384+
LineTo lineToBottomLeft = (LineTo) shape[bottomLeftIndex];
385+
386+
shape[topRightIndex] = new LineTo(width, lineToTopRight.getY());
387+
shape[bottomLeftIndex] = new LineTo(lineToBottomLeft.getX(), getHeight());
388+
shape[bottomRightIndex] = new LineTo(width, getHeight());
383389
}
384390
}
385391
}
@@ -402,12 +408,13 @@ private PathElement[] getRangeShapeSafely(int start, int end) {
402408

403409
if (getLineCount() > 1) {
404410
// adjust right corners of wrapped lines
411+
double width = getWidth() - getInsets().getRight() - getInsets().getLeft();
405412
boolean wrappedAtEndPos = (end > 0 && getLineOfCharacter(end) > getLineOfCharacter(end - 1));
406413
int adjustLength = shape.length - (wrappedAtEndPos ? 0 : 5);
407414
for (int i = 0; i < adjustLength; i++) {
408415
if (shape[i] instanceof MoveTo) {
409-
((LineTo)shape[i + 1]).setX(getWidth());
410-
((LineTo)shape[i + 2]).setX(getWidth());
416+
((LineTo)shape[i + 1]).setX(width);
417+
((LineTo)shape[i + 2]).setX(width);
411418
}
412419
}
413420
}

0 commit comments

Comments
 (0)