@@ -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