summaryrefslogtreecommitdiffstats
path: root/src/pdf
diff options
context:
space:
mode:
authorShawn Rutledge <shawn.rutledge@qt.io>2020-04-30 22:38:43 +0200
committerShawn Rutledge <shawn.rutledge@qt.io>2020-04-30 23:02:15 +0200
commit4fc8a1074435b5e44e1c24d9f6ca5cc0a07f1243 (patch)
treeb25e334fdae6764b797118c8b45fc0f45c198afb /src/pdf
parent4a08034ca3b274e0309530bed4630521b8ad91c9 (diff)
QPdfDocument::getSelection(): fix off-by-one character selection
When mouse-dragging to select text, the selection always contained one more character than the geometry (set of polygons) was showing. Conversely, it was not actually possible to select a single character: you'd always get at least two, even though only one looked like it was selected. Applied a similar fix as the one in hitTest() (in 8d13facbd88d821cc89b21f43708cc1a81ac79f3): check the bounding box of the last character, and if the given end position (where the mouse is being dragged to) is closer to the right edge than to the left, increment the ending character index, to point to the character after (which is probably an invisible newline or other terminator). This fix might not be so great if the text doesn't run left-to-right, but it works for Western text in normal orientation at least. Fixes: QTBUG-83685 Change-Id: I099424cd334fa38986445c134c82580a0307c2fe Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/pdf')
-rw-r--r--src/pdf/qpdfdocument.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/pdf/qpdfdocument.cpp b/src/pdf/qpdfdocument.cpp
index 7dd54a8e3..e4ec363ce 100644
--- a/src/pdf/qpdfdocument.cpp
+++ b/src/pdf/qpdfdocument.cpp
@@ -792,7 +792,14 @@ QPdfSelection QPdfDocument::getSelection(int page, QPointF start, QPointF end)
if (startIndex >= 0 && endIndex != startIndex) {
if (startIndex > endIndex)
qSwap(startIndex, endIndex);
- int count = endIndex - startIndex + 1;
+
+ // If the given end position is past the end of the line, i.e. if the right edge of the last character's
+ // bounding box is closer to it than the left edge is, then extend the char range by one
+ QRectF endCharBox = d->getCharBox(textPage, pageHeight, endIndex);
+ if (qAbs(endCharBox.right() - end.x()) < qAbs(endCharBox.x() - end.x()))
+ ++endIndex;
+
+ int count = endIndex - startIndex;
QString text = d->getText(textPage, startIndex, count);
QVector<QPolygonF> bounds;
QRectF hull;