From d07982b104de5dc2b54bef09c071500ce22cf539 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Wed, 9 May 2012 16:14:34 +1000 Subject: Fix cursorToX for right to left text with trailing whitespace. QTextLine::cursorToX returned the line width for cursor positions outside the width of a wrapped right to left line because the leading space width was always calculated as 0. Returning a non-zero width for the leading space does cause problems for other uses of QTextEngine::alignLine() though as the textAdvance already doesn't include the leading/trailing space so subtracting it there double accounts for it. Task-number: QTBUG-24801 Change-Id: I56cbb139814c32813bebb49de8c045b29154a958 Reviewed-by: Jiang Jiang --- src/gui/text/qtextengine.cpp | 14 +++----------- src/gui/text/qtextlayout.cpp | 10 ++++++---- 2 files changed, 9 insertions(+), 15 deletions(-) (limited to 'src/gui/text') diff --git a/src/gui/text/qtextengine.cpp b/src/gui/text/qtextengine.cpp index 5dd0cb8c6c..1bbadfb182 100644 --- a/src/gui/text/qtextengine.cpp +++ b/src/gui/text/qtextengine.cpp @@ -2698,13 +2698,7 @@ QFixed QTextEngine::leadingSpaceWidth(const QScriptLine &line) || !isRightToLeft()) return QFixed(); - int pos = line.length; - const HB_CharAttributes *attributes = this->attributes(); - if (!attributes) - return QFixed(); - while (pos > 0 && attributes[line.from + pos - 1].whiteSpace) - --pos; - return width(line.from + pos, line.length - pos); + return width(line.from + line.length, line.trailingSpaces); } QFixed QTextEngine::alignLine(const QScriptLine &line) @@ -2714,14 +2708,12 @@ QFixed QTextEngine::alignLine(const QScriptLine &line) // if width is QFIXED_MAX that means we used setNumColumns() and that implicitly makes this line left aligned. if (!line.justified && line.width != QFIXED_MAX) { int align = option.alignment(); - if (align & Qt::AlignLeft) - x -= leadingSpaceWidth(line); if (align & Qt::AlignJustify && isRightToLeft()) align = Qt::AlignRight; if (align & Qt::AlignRight) - x = line.width - (line.textAdvance + leadingSpaceWidth(line)); + x = line.width - (line.textAdvance); else if (align & Qt::AlignHCenter) - x = (line.width - line.textAdvance)/2 - leadingSpaceWidth(line); + x = (line.width - line.textAdvance)/2; } return x; } diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 0ab964778a..43d212f394 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -2584,13 +2584,14 @@ qreal QTextLine::cursorToX(int *cursorPos, Edge edge) const bool lastLine = index >= eng->lines.size() - 1; QFixed x = line.x; - x += eng->alignLine(line); + x += eng->alignLine(line) - eng->leadingSpaceWidth(line); if (!index && !eng->layoutData->items.size()) { *cursorPos = 0; return x.toReal(); } + int lineEnd = line.from + line.length + line.trailingSpaces; int pos = *cursorPos; int itm; const HB_CharAttributes *attributes = eng->attributes(); @@ -2598,9 +2599,9 @@ qreal QTextLine::cursorToX(int *cursorPos, Edge edge) const *cursorPos = 0; return x.toReal(); } - while (pos < line.from + line.length && !attributes[pos].charStop) + while (pos < lineEnd && !attributes[pos].charStop) pos++; - if (pos == line.from + (int)line.length) { + if (pos == lineEnd) { // end of line ensure we have the last item on the line itm = eng->findItem(pos-1); } @@ -2633,7 +2634,6 @@ qreal QTextLine::cursorToX(int *cursorPos, Edge edge) const bool reverse = eng->layoutData->items[itm].analysis.bidiLevel % 2; - int lineEnd = line.from + line.length; // add the items left of the cursor @@ -2705,6 +2705,8 @@ qreal QTextLine::cursorToX(int *cursorPos, Edge edge) const if (eng->option.wrapMode() != QTextOption::NoWrap && x > line.width) x = line.width; + if (eng->option.wrapMode() != QTextOption::NoWrap && x < 0) + x = 0; *cursorPos = pos + si->position; return x.toReal(); -- cgit v1.2.3