From 279562172d2e998e910d82599255cb04b54df823 Mon Sep 17 00:00:00 2001 From: Konstantin Ritt Date: Tue, 15 May 2012 20:02:53 +0300 Subject: enable the text layout's cache where it is seems to be missed e.g. in QStaticText, the data is used just to get the line's y-position and re-calculates just after the loop to determine the bounding rect and to draw the text; in QWidgetLineControl, the data re-calculated over and over while the result is seems to remain the same; probably the caching is needed here too Change-Id: I0f7eb291532f63eccb9c5f749daebb73ff90632f Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../doc/snippets/code/src_gui_text_qtextlayout.cpp | 1 + src/gui/text/qstatictext.cpp | 1 + src/widgets/widgets/qwidgetlinecontrol.cpp | 1 + tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp | 8 ++++ tests/auto/gui/text/qrawfont/tst_qrawfont.cpp | 3 ++ .../auto/gui/text/qtextlayout/tst_qtextlayout.cpp | 43 ++++++++++++++++++++-- .../qtextscriptengine/tst_qtextscriptengine.cpp | 4 ++ tests/auto/other/qcomplextext/tst_qcomplextext.cpp | 4 ++ 8 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp b/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp index c8e1d2e010..cb82f3f9a6 100644 --- a/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp +++ b/src/gui/doc/snippets/code/src_gui_text_qtextlayout.cpp @@ -41,6 +41,7 @@ //! [0] int leading = fontMetrics.leading(); qreal height = 0; +textLayout.setCacheEnabled(true); textLayout.beginLayout(); while (1) { QTextLine line = textLayout.createLine(); diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp index 5bee2a9915..faee6d2b76 100644 --- a/src/gui/text/qstatictext.cpp +++ b/src/gui/text/qstatictext.cpp @@ -610,6 +610,7 @@ void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) textLayout.setText(text); textLayout.setFont(font); textLayout.setTextOption(textOption); + textLayout.setCacheEnabled(true); qreal leading = QFontMetricsF(font).leading(); qreal height = -leading; diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp index 6347593efe..6920354e73 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -351,6 +351,7 @@ void QWidgetLineControl::_q_deleteSelected() */ void QWidgetLineControl::init(const QString &txt) { + m_textLayout.setCacheEnabled(true); m_text = txt; updateDisplayText(); m_cursor = m_text.length(); diff --git a/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp b/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp index 8ef030ae60..3cb5760648 100644 --- a/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp +++ b/tests/auto/gui/text/qglyphrun/tst_qglyphrun.cpp @@ -244,6 +244,7 @@ void tst_QGlyphRun::textLayoutGlyphIndexes() QTextLayout layout(s); layout.setFont(m_testFont); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -273,6 +274,7 @@ void tst_QGlyphRun::drawExistingGlyphs() QTextLayout layout(s); layout.setFont(m_testFont); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -411,6 +413,7 @@ void tst_QGlyphRun::drawMultiScriptText1() text += QChar(0x03D0); // Greek, beta QTextLayout textLayout(text); + textLayout.setCacheEnabled(true); textLayout.beginLayout(); textLayout.createLine(); textLayout.endLayout(); @@ -451,6 +454,7 @@ void tst_QGlyphRun::drawMultiScriptText2() text += QChar(0x03D0); // Greek, beta QTextLayout textLayout(text); + textLayout.setCacheEnabled(true); textLayout.beginLayout(); textLayout.createLine(); textLayout.endLayout(); @@ -515,6 +519,7 @@ void tst_QGlyphRun::drawStruckOutText() QTextLayout layout(s); layout.setFont(font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -556,6 +561,7 @@ void tst_QGlyphRun::drawOverlinedText() QTextLayout layout(s); layout.setFont(font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -597,6 +603,7 @@ void tst_QGlyphRun::drawUnderlinedText() QTextLayout layout(s); layout.setFont(font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -640,6 +647,7 @@ void tst_QGlyphRun::drawRightToLeft() QTextLayout layout(s); layout.setFont(font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); diff --git a/tests/auto/gui/text/qrawfont/tst_qrawfont.cpp b/tests/auto/gui/text/qrawfont/tst_qrawfont.cpp index a3fe7b5368..007bf63a12 100644 --- a/tests/auto/gui/text/qrawfont/tst_qrawfont.cpp +++ b/tests/auto/gui/text/qrawfont/tst_qrawfont.cpp @@ -325,6 +325,7 @@ void tst_QRawFont::textLayout() QTextLayout layout(QLatin1String("Foobar")); layout.setFont(font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -822,6 +823,7 @@ void tst_QRawFont::unsupportedWritingSystem() QTextLayout layout; layout.setFont(font); layout.setText(arabicText); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -862,6 +864,7 @@ void tst_QRawFont::rawFontSetPixelSize() font.setPixelSize(12); layout.setFont(font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp index d83f1e9a36..e5238865fb 100644 --- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp +++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp @@ -415,6 +415,8 @@ void tst_QTextLayout::forcedBreaks() QCOMPARE(qRound(line.naturalTextWidth()), testFont.pixelSize()); QCOMPARE((int) line.height(), testFont.pixelSize()); QCOMPARE(line.xToCursor(0), line.textStart()); + + layout.endLayout(); } void tst_QTextLayout::breakAny() @@ -425,6 +427,7 @@ void tst_QTextLayout::breakAny() QString text = "ABCD"; QTextLayout layout(text, testFont); + layout.setCacheEnabled(true); QTextLine line; QTextOption opt; @@ -506,6 +509,7 @@ void tst_QTextLayout::cursorToXForInlineObjects() void tst_QTextLayout::cursorToXForSetColumns() { QTextLayout lay("abc", testFont); + lay.setCacheEnabled(true); QTextOption o = lay.textOption(); o.setWrapMode(QTextOption::WrapAnywhere); @@ -614,6 +618,7 @@ void tst_QTextLayout::xToCursorAtEndOfLine() const qreal firstLineWidth = QString("FirstLine").length() * testFont.pixelSize(); QTextLayout layout(text, testFont); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); @@ -635,6 +640,7 @@ void tst_QTextLayout::boundingRectTopLeft() text.replace('\n', QChar::LineSeparator); QTextLayout layout(text, testFont); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine firstLine = layout.createLine(); @@ -711,6 +717,7 @@ void tst_QTextLayout::setNumColumnsWrapAtWordBoundaryOrAnywhere() { QString txt("This is a small test text"); QTextLayout layout(txt, testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); layout.setTextOption(option); @@ -734,6 +741,7 @@ void tst_QTextLayout::setNumColumnsWordWrap() { QString txt("This is a small test text"); QTextLayout layout(txt, testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setWrapMode(QTextOption::WordWrap); layout.setTextOption(option); @@ -757,6 +765,7 @@ void tst_QTextLayout::smallTextLengthNoWrap() { QString txt("This is a small test text"); QTextLayout layout(txt, testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setWrapMode(QTextOption::NoWrap); layout.setTextOption(option); @@ -780,6 +789,7 @@ void tst_QTextLayout::smallTextLengthWordWrap() { QString txt("This is a small test text"); QTextLayout layout(txt, testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setWrapMode(QTextOption::WordWrap); layout.setTextOption(option); @@ -804,6 +814,7 @@ void tst_QTextLayout::smallTextLengthWrapAtWordBoundaryOrAnywhere() { QString txt("This is a small test text"); QTextLayout layout(txt, testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere); layout.setTextOption(option); @@ -827,6 +838,7 @@ void tst_QTextLayout::smallTextLengthWrapAtWordBoundaryOrAnywhere() void tst_QTextLayout::testDefaultTabs() { QTextLayout layout("Foo\tBar\ta slightly longer text\tend.", testFont); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); line.setLineWidth(1000); @@ -868,6 +880,7 @@ void tst_QTextLayout::testDefaultTabs() void tst_QTextLayout::testTabs() { QTextLayout layout("Foo\tBar.", testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setTabStop(150); layout.setTextOption(option); @@ -884,6 +897,7 @@ void tst_QTextLayout::testTabs() void tst_QTextLayout::testMultilineTab() { QTextLayout layout("Lorem ipsum dolor sit\tBar.", testFont); + layout.setCacheEnabled(true); // test if this works on the second line. layout.beginLayout(); QTextLine line = layout.createLine(); @@ -898,6 +912,7 @@ void tst_QTextLayout::testMultilineTab() void tst_QTextLayout::testMultiTab() { QTextLayout layout("Foo\t\t\tBar.", testFont); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); line.setLineWidth(1000.); @@ -909,6 +924,7 @@ void tst_QTextLayout::testMultiTab() void tst_QTextLayout::testTabsInAlignedParag() { QTextLayout layout("Foo\tsome more words", testFont); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); // right option.setAlignment(Qt::AlignRight); @@ -966,6 +982,7 @@ void tst_QTextLayout::testRightTab() c) tab is after last tab (both auto and defined) and thus moves text to start of next line. d) tab takes space so text until enter fits to tab pos. */ + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); QList tabs; @@ -1004,6 +1021,7 @@ void tst_QTextLayout::testRightTab() void tst_QTextLayout::testCenteredTab() { QTextLayout layout("Foo\tBar", testFont); + layout.setCacheEnabled(true); // test if centering the tab works. We expect the center of 'Bar.' to be at the tab point. QTextOption option = layout.textOption(); QList tabs; @@ -1024,6 +1042,7 @@ void tst_QTextLayout::testCenteredTab() void tst_QTextLayout::testDelimiterTab() { QTextLayout layout("Foo\tBar. Barrabas", testFont); + layout.setCacheEnabled(true); // try the different delimiter characters to see if the alignment works there. QTextOption option = layout.textOption(); QList tabs; @@ -1044,6 +1063,7 @@ void tst_QTextLayout::testDelimiterTab() void tst_QTextLayout::testLineBreakingAllSpaces() { QTextLayout layout(" 123", testFont); // thats 20 spaces + layout.setCacheEnabled(true); const qreal firstLineWidth = 17 * TESTFONT_SIZE; layout.beginLayout(); QTextLine line1 = layout.createLine(); @@ -1073,6 +1093,7 @@ void tst_QTextLayout::tabsForRtl() c) right tab on RTL is a left tab; so its at width - 240 d) center tab is still a centered tab. */ + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); QList tabs; @@ -1135,6 +1156,7 @@ void tst_QTextLayout::testTabDPIScale() MyPaintDevice pd; QTextLayout layout("text1\ttext2\ttext3\tend", testFont, &pd); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); QList tabs; @@ -1171,6 +1193,7 @@ void tst_QTextLayout::testTabDPIScale() void tst_QTextLayout::tabHeight() { QTextLayout layout("\t", testFont); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1184,6 +1207,7 @@ void tst_QTextLayout::capitalization_allUpperCase() QFont font(testFont); font.setCapitalization(QFont::AllUppercase); QTextLayout layout("Test", font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1199,6 +1223,7 @@ void tst_QTextLayout::capitalization_allLowerCase() QFont font(testFont); font.setCapitalization(QFont::AllLowercase); QTextLayout layout("Test", font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1214,6 +1239,7 @@ void tst_QTextLayout::capitalization_smallCaps() QFont font(testFont); font.setCapitalization(QFont::SmallCaps); QTextLayout layout("Test", font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1230,6 +1256,7 @@ void tst_QTextLayout::capitalization_capitalize() QFont font(testFont); font.setCapitalization(QFont::Capitalize); QTextLayout layout("hello\tworld", font); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1250,6 +1277,7 @@ void tst_QTextLayout::longText() { QTextLayout layout(longText, testFont); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1261,6 +1289,7 @@ void tst_QTextLayout::longText() QFont f(testFont); f.setCapitalization(QFont::Capitalization(cap)); QTextLayout layout(longText, f); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1270,6 +1299,7 @@ void tst_QTextLayout::longText() { QTextLayout layout(longText, testFont); + layout.setCacheEnabled(true); layout.setFlags(Qt::TextForceLeftToRight); layout.beginLayout(); QTextLine line = layout.createLine(); @@ -1280,6 +1310,7 @@ void tst_QTextLayout::longText() { QTextLayout layout(longText, testFont); + layout.setCacheEnabled(true); layout.setFlags(Qt::TextForceRightToLeft); layout.beginLayout(); QTextLine line = layout.createLine(); @@ -1334,7 +1365,7 @@ void tst_QTextLayout::columnWrapWithTabs() void tst_QTextLayout::boundingRectForUnsetLineWidth() { QTextLayout layout("FOOBAR"); - + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1345,7 +1376,7 @@ void tst_QTextLayout::boundingRectForUnsetLineWidth() void tst_QTextLayout::boundingRectForSetLineWidth() { QTextLayout layout("FOOBAR"); - + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); line.setLineWidth(QFIXED_MAX - 1); @@ -1388,6 +1419,7 @@ void tst_QTextLayout::glyphLessItems() void tst_QTextLayout::textWidthVsWIdth() { QTextLayout layout; + layout.setCacheEnabled(true); QTextOption opt; opt.setWrapMode(QTextOption::WrapAnywhere); layout.setTextOption(opt); @@ -1417,6 +1449,7 @@ void tst_QTextLayout::textWithSurrogates_qtbug15679() { QString str = QString::fromUtf8("🀀a🀀"); QTextLayout layout(str); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1436,6 +1469,7 @@ void tst_QTextLayout::textWidthWithStackedTextEngine() { QString text = QString::fromUtf8("คลิก ถัดไป เพื่อดำเนินการต่อ"); QTextLayout layout(text); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1465,6 +1499,7 @@ void tst_QTextLayout::cursorInLigatureWithMultipleLines() QSKIP("This test can only be run on Mac"); #endif QTextLayout layout("first line finish", QFont("Times", 20)); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); line.setLineWidth(70); @@ -1481,6 +1516,7 @@ void tst_QTextLayout::xToCursorForLigatures() QSKIP("This test can only be run on Mac"); #endif QTextLayout layout("fi", QFont("Times", 20)); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); @@ -1489,7 +1525,7 @@ void tst_QTextLayout::xToCursorForLigatures() // U+0061 U+0308 QTextLayout layout2(QString::fromUtf8("\x61\xCC\x88"), QFont("Times", 20)); - + layout2.setCacheEnabled(true); layout2.beginLayout(); line = layout2.createLine(); layout2.endLayout(); @@ -1505,6 +1541,7 @@ void tst_QTextLayout::cursorInNonStopChars() QSKIP("This test can not be run on Mac"); #endif QTextLayout layout(QString::fromUtf8("\xE0\xA4\xA4\xE0\xA5\x8D\xE0\xA4\xA8")); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); layout.endLayout(); diff --git a/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp b/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp index 5a53f09e77..f2ecd5dacb 100644 --- a/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp +++ b/tests/auto/gui/text/qtextscriptengine/tst_qtextscriptengine.cpp @@ -1231,6 +1231,7 @@ void tst_QTextScriptEngine::mirroredChars() HB_Glyph rightParenthesis; { QTextLayout layout(s); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1267,6 +1268,7 @@ void tst_QTextScriptEngine::thaiIsolatedSaraAm() s.append(QChar(0x0e33)); QTextLayout layout(s, QFont("Waree")); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1289,6 +1291,7 @@ void tst_QTextScriptEngine::thaiWithZWJ() #endif QString s(QString::fromUtf8("ร‍ร‌.ร.“ร…ร”ร\xA0ร本ร") + QChar(0x0363)/*superscript 'a', for testing Inherited class*/); QTextLayout layout(s); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); @@ -1341,6 +1344,7 @@ void tst_QTextScriptEngine::thaiMultipleVowels() for (int i = 0; i < 10; i++) s += s; //Repeat the string to make it more likely to crash if we have a buffer overflow QTextLayout layout(s); + layout.setCacheEnabled(true); layout.beginLayout(); layout.createLine(); layout.endLayout(); diff --git a/tests/auto/other/qcomplextext/tst_qcomplextext.cpp b/tests/auto/other/qcomplextext/tst_qcomplextext.cpp index 6b418d7c46..764c75f137 100644 --- a/tests/auto/other/qcomplextext/tst_qcomplextext.cpp +++ b/tests/auto/other/qcomplextext/tst_qcomplextext.cpp @@ -168,6 +168,7 @@ void tst_QComplexText::bidiCursor_qtbug2795() QTextLayout l1(str); l1.beginLayout(); + l1.setCacheEnabled(true); QTextLine line1 = l1.createLine(); l1.endLayout(); @@ -175,6 +176,7 @@ void tst_QComplexText::bidiCursor_qtbug2795() str.append("1"); QTextLayout l2(str); + l2.setCacheEnabled(true); l2.beginLayout(); QTextLine line2 = l2.createLine(); l2.endLayout(); @@ -206,6 +208,7 @@ void tst_QComplexText::bidiCursorMovement() QFETCH(int, basicDir); QTextLayout layout(logical); + layout.setCacheEnabled(true); QTextOption option = layout.textOption(); option.setTextDirection(basicDir == QChar::DirL ? Qt::LeftToRight : Qt::RightToLeft); @@ -273,6 +276,7 @@ void tst_QComplexText::bidiCursor_PDF() { QString str = QString::fromUtf8("\342\200\252hello\342\200\254"); QTextLayout layout(str); + layout.setCacheEnabled(true); layout.beginLayout(); QTextLine line = layout.createLine(); -- cgit v1.2.3