From 19bb9aa9e51e48043544fd7edf5abfda67955a4b Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Mon, 15 Dec 2014 13:50:11 +0100 Subject: [PATCH] Fix possibly corrupted log clusters when using custom tab stops The calculateTabWidth() can trigger shaping of the item, which can cause the layout data to be reallocated, so we need to update the local pointers to it, like we do when we explicitly invoke the shaper. [ChangeLog][Text] Fixed problems with text layout when using custom tab stops. Task-number: QTBUG-43126 Change-Id: Ifaeeeb4bfb1a55e6638b12b444f53d2679d3d1e6 Reviewed-by: Konstantin Ritt --- src/gui/text/qtextlayout.cpp | 5 +++ .../gui/text/qtextlayout/tst_qtextlayout.cpp | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 733bb57928..52d2ba0d54 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -1776,6 +1776,11 @@ void QTextLine::layout_helper(int maxGlyphs) QFixed x = line.x + line.textWidth + lbh.tmpData.textWidth + lbh.spaceData.textWidth; QFixed tabWidth = eng->calculateTabWidth(item, x); + attributes = eng->attributes(); + if (!attributes) + return; + lbh.logClusters = eng->layoutData->logClustersPtr; + lbh.glyphs = eng->shapedGlyphs(¤t); lbh.spaceData.textWidth += tabWidth; lbh.spaceData.length++; diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp index a9a358711b..4b2970cd17 100644 --- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp +++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp @@ -116,6 +116,7 @@ private slots: void boundingRectForSetLineWidth(); void glyphLessItems(); void justifyTrailingSpaces(); + void layoutWithCustomTabStops(); // QTextLine stuff void setNumColumnsWrapAtWordBoundaryOrAnywhere(); @@ -2050,5 +2051,38 @@ void tst_QTextLayout::nbsp() layout.endLayout(); } +void tst_QTextLayout::layoutWithCustomTabStops() +{ + QScopedPointer textLayout(new QTextLayout); + QList tabStops; + + const int tabWidth = 18; + const int maxTabPos = 2500; + for (int tabPos = tabWidth; tabPos < maxTabPos; tabPos += tabWidth) + tabStops << QTextOption::Tab(tabPos, QTextOption::LeftTab); + + QTextOption textOption; + textOption.setTabs(tabStops); + textLayout->setTextOption(textOption); + + textLayout->setText(QStringLiteral("\ta aa aa aa aa aa aa")); + + textLayout->beginLayout(); + textLayout->createLine(); + textLayout->endLayout(); + + qreal shortWidth = textLayout->maximumWidth(); + + textLayout->setText(QStringLiteral("\ta aa aa aa aa aa aa aa aa aa aa aa aa a")); + + textLayout->beginLayout(); + textLayout->createLine(); + textLayout->endLayout(); + + qreal longWidth = textLayout->maximumWidth(); + + QVERIFY(longWidth > shortWidth); +} + QTEST_MAIN(tst_QTextLayout) #include "tst_qtextlayout.moc"