Skip to content

Commit

Permalink
Don't round Text dimensions.
Browse files Browse the repository at this point in the history
Painting issues in QtQuick1 meant drawing had to be aligned to pixel
boundaries, since this is no longer a problem we should use qreal
everywhere.

Change-Id: I58e88e10270fa603170f1cedf471bfb53bd89b73
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Feb 29, 2012
1 parent 7fa6fcd commit 4e0e0e5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
37 changes: 19 additions & 18 deletions src/quick/items/qquicktext.cpp
Expand Up @@ -386,7 +386,7 @@ void QQuickTextPrivate::updateSize()
return;
}

QFontMetrics fm(font);
QFontMetricsF fm(font);
if (text.isEmpty()) {
qreal fontHeight = fm.height();
q->setImplicitSize(0, fontHeight);
Expand All @@ -399,16 +399,16 @@ void QQuickTextPrivate::updateSize()

qreal naturalWidth = 0;

int dy = q->height();
QSize size(0, 0);
QSize previousSize = layedOutTextRect.size();
qreal dy = q->height();
QSizeF size(0, 0);
QSizeF previousSize = layedOutTextRect.size();
#if defined(Q_OS_MAC)
layoutThread = QThread::currentThread();
#endif

//setup instance of QTextLayout for all cases other than richtext
if (!richText) {
QRect textRect = setupTextLayout(&naturalWidth);
QRectF textRect = setupTextLayout(&naturalWidth);
layedOutTextRect = textRect;
size = textRect.size();
dy -= size.height();
Expand Down Expand Up @@ -436,12 +436,12 @@ void QQuickTextPrivate::updateSize()
doc->setTextWidth(q->width());
else
doc->setTextWidth(doc->idealWidth()); // ### Text does not align if width is not set (QTextDoc bug)
dy -= (int)doc->size().height();
QSize dsize = doc->size().toSize();
layedOutTextRect = QRect(QPoint(0,0), dsize);
size = QSize(int(doc->idealWidth()),dsize.height());
dy -= doc->size().height();
QSizeF dsize = doc->size();
layedOutTextRect = QRectF(QPointF(0,0), dsize);
size = QSizeF(doc->idealWidth(),dsize.height());
}
int yoff = 0;
qreal yoff = 0;

if (q->heightValid()) {
if (vAlign == QQuickText::AlignBottom)
Expand Down Expand Up @@ -601,13 +601,13 @@ void QQuickTextPrivate::setupCustomLineGeometry(QTextLine &line, qreal &height,
#endif
}

QString QQuickTextPrivate::elidedText(int lineWidth, const QTextLine &line, QTextLine *nextLine) const
QString QQuickTextPrivate::elidedText(qreal lineWidth, const QTextLine &line, QTextLine *nextLine) const
{
if (nextLine) {
nextLine->setLineWidth(INT_MAX);
return layout.engine()->elidedText(
Qt::TextElideMode(elideMode),
lineWidth,
QFixed::fromReal(lineWidth),
0,
line.textStart(),
line.textLength() + nextLine->textLength());
Expand All @@ -630,7 +630,7 @@ QString QQuickTextPrivate::elidedText(int lineWidth, const QTextLine &line, QTex
already absolutely positioned horizontally).
*/

QRect QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth)
QRectF QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth)
{
Q_Q(QQuickText);
layout.setCacheEnabled(true);
Expand Down Expand Up @@ -673,8 +673,9 @@ QRect QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth)
return QRect(0, 0, 0, height);
}

const int lineWidth = q->widthValid() ? q->width() : INT_MAX;
const qreal lineWidth = q->widthValid() ? q->width() : FLT_MAX;
const qreal maxHeight = q->heightValid() ? q->height() : FLT_MAX;

const bool customLayout = isLineLaidOutConnected();
const bool wasTruncated = truncated;

Expand Down Expand Up @@ -783,7 +784,7 @@ QRect QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth)
height = preLayoutHeight;
elideText = layout.engine()->elidedText(
Qt::TextElideMode(elideMode),
lineWidth,
QFixed::fromReal(lineWidth),
0,
line.textStart(),
line.textLength());
Expand Down Expand Up @@ -941,7 +942,7 @@ QRect QQuickTextPrivate::setupTextLayout(qreal *const naturalWidth)
if (truncated != wasTruncated)
emit q->truncatedChanged();

return QRect(qRound(br.x()), qRound(br.y()), qCeil(br.width()), qCeil(br.height()));
return br;
}

void QQuickTextPrivate::setLineGeometry(QTextLine &line, qreal lineWidth, qreal &height)
Expand Down Expand Up @@ -1897,7 +1898,7 @@ QRectF QQuickText::boundingRect() const
{
Q_D(const QQuickText);

QRect rect = d->layedOutTextRect;
QRectF rect = d->layedOutTextRect;
if (d->style != Normal)
rect.adjust(-1, 0, 1, 2);

Expand All @@ -1915,7 +1916,7 @@ QRectF QQuickText::boundingRect() const
break;
}

return QRectF(rect);
return rect;
}

/*! \internal */
Expand Down
6 changes: 3 additions & 3 deletions src/quick/items/qquicktext_p_p.h
Expand Up @@ -82,9 +82,9 @@ class Q_AUTOTEST_EXPORT QQuickTextPrivate : public QQuickImplicitSizeItemPrivate
QTextDocument *textDocument();
bool isLineLaidOutConnected();
void setLineGeometry(QTextLine &line, qreal lineWidth, qreal &height);
QString elidedText(int lineWidth, const QTextLine &line, QTextLine *nextLine = 0) const;
QString elidedText(qreal lineWidth, const QTextLine &line, QTextLine *nextLine = 0) const;

QRect layedOutTextRect;
QRectF layedOutTextRect;

qreal lineHeight;

Expand Down Expand Up @@ -156,7 +156,7 @@ class Q_AUTOTEST_EXPORT QQuickTextPrivate : public QQuickImplicitSizeItemPrivate

void ensureDoc();

QRect setupTextLayout(qreal *const naturalWidth);
QRectF setupTextLayout(qreal *const naturalWidth);
void setupCustomLineGeometry(QTextLine &line, qreal &height, int lineOffset = 0);
bool isLinkActivatedConnected();
QString anchorAt(const QPointF &pos);
Expand Down
2 changes: 1 addition & 1 deletion tests/auto/qtquick2/qquicktext/data/multilengthStrings.qml
Expand Up @@ -7,7 +7,7 @@ Item {
Text {
id: myText
objectName: "myText"
width: 100
width: 60
font.pixelSize: 15
font.family: "Helvetica"
}
Expand Down
Expand Up @@ -7,7 +7,7 @@ Item {
Text {
id: myText
objectName: "myText"
width: 100
width: 60
height: 36
font.pixelSize: 15
font.family: "Helvetica"
Expand Down
8 changes: 2 additions & 6 deletions tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp
Expand Up @@ -295,11 +295,10 @@ void tst_qquicktext::width()

layout.endLayout();

metricWidth = qCeil(layout.boundingRect().width());
metricWidth = layout.boundingRect().width();
} else {
QFontMetricsF fm(f);
qreal metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width();
metricWidth = qCeil(metricWidth);
metricWidth = fm.size(Qt::TextExpandTabs && Qt::TextShowMnemonic, standard.at(i)).width();
}

QString componentStr = "import QtQuick 2.0\nText { text: \"" + standard.at(i) + "\" }";
Expand Down Expand Up @@ -2422,9 +2421,6 @@ void tst_qquicktext::multilengthStrings()

QCOMPARE(myText->contentWidth(), mediumWidth);
QCOMPARE(myText->contentHeight(), mediumHeight);
#ifdef Q_OS_MAC
QEXPECT_FAIL("Wrap", "QTBUG-24310", Continue);
#endif
QCOMPARE(myText->truncated(), true);

myText->setSize(QSizeF(shortWidth, shortHeight));
Expand Down

0 comments on commit 4e0e0e5

Please sign in to comment.