From f8560717dd56514269cfb081c7f4b94e231e10d7 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Tue, 28 Jun 2011 17:07:13 +0200 Subject: Fix text color in some cases of QML and QStaticText This reverts 518c2a58ed6fdfd7449cb4476aa8ea0d32ad16e3 which caused a regression. When writing systems are mixed and an underline is set on the font, QPainter will set a pen with the current color and a new width on itself before drawing the decoration. This would cause the recorder in QStaticText to mark the pen as dirty, saving the current pen color in all subsequent text items. The effect was e.g. that in QML the cached color would override the current one, making it impossible to change the color on the text without forcing a relayout somehow. The right fix is to only mark the pen as dirty when its color actually changes. Task-number: QTBUG-20159 Reviewed-by: Jiang Jiang --- .../graphicsitems/qdeclarativetextlayout.cpp | 15 ++++---- .../graphicsitems/qdeclarativetextlayout_p.h | 2 +- src/gui/text/qstatictext.cpp | 10 ++++-- tests/auto/qstatictext/tst_qstatictext.cpp | 40 ++++++++++++++++++++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativetextlayout.cpp b/src/declarative/graphicsitems/qdeclarativetextlayout.cpp index cf2ef26..9aef504 100644 --- a/src/declarative/graphicsitems/qdeclarativetextlayout.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextlayout.cpp @@ -72,14 +72,17 @@ class DrawTextItemRecorder: public QPaintEngine public: DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations) : m_inertText(0), m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations), - m_untransformedCoordinates(untransformedCoordinates) + m_untransformedCoordinates(untransformedCoordinates), m_currentColor(Qt::black) { } virtual void updateState(const QPaintEngineState &newState) { - if (newState.state() & QPaintEngine::DirtyPen) + if (newState.state() & QPaintEngine::DirtyPen + && newState.pen().color() != m_currentColor) { m_dirtyPen = true; + m_currentColor = newState.pen().color(); + } } virtual void drawTextItem(const QPointF &position, const QTextItem &textItem) @@ -115,7 +118,7 @@ class DrawTextItemRecorder: public QPaintEngine currentItem.positionOffset = positionOffset; currentItem.useBackendOptimizations = m_useBackendOptimizations; if (m_dirtyPen) - currentItem.color = state->pen().color(); + currentItem.color = m_currentColor; m_inertText->items.append(currentItem); } @@ -172,6 +175,7 @@ class DrawTextItemRecorder: public QPaintEngine bool m_dirtyPen; bool m_useBackendOptimizations; bool m_untransformedCoordinates; + QColor m_currentColor; }; class DrawTextItemDevice: public QPaintDevice @@ -299,7 +303,7 @@ void QDeclarativeTextLayout::clearLayout() QTextLayout::clearLayout(); } -void QDeclarativeTextLayout::prepare(QPainter *painter) +void QDeclarativeTextLayout::prepare() { if (!d || !d->cached) { @@ -308,7 +312,6 @@ void QDeclarativeTextLayout::prepare(QPainter *painter) InertTextPainter *itp = inertTextPainter(); itp->device.begin(d); - itp->painter.setPen(painter->pen()); QTextLayout::draw(&itp->painter, QPointF(0, 0)); glyph_t *glyphPool = d->glyphs.data(); @@ -347,7 +350,7 @@ void QDeclarativeTextLayout::draw(QPainter *painter, const QPointF &p) return; } - prepare(painter); + prepare(); int itemCount = d->items.count(); diff --git a/src/declarative/graphicsitems/qdeclarativetextlayout_p.h b/src/declarative/graphicsitems/qdeclarativetextlayout_p.h index 85d333e..d83ce7e 100644 --- a/src/declarative/graphicsitems/qdeclarativetextlayout_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextlayout_p.h @@ -61,7 +61,7 @@ public: void beginLayout(); void clearLayout(); - void prepare(QPainter *); + void prepare(); void draw(QPainter *, const QPointF & = QPointF()); private: diff --git a/src/gui/text/qstatictext.cpp b/src/gui/text/qstatictext.cpp index 414de51..73a871b 100644 --- a/src/gui/text/qstatictext.cpp +++ b/src/gui/text/qstatictext.cpp @@ -430,14 +430,17 @@ namespace { public: DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations) : m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations), - m_untransformedCoordinates(untransformedCoordinates) + m_untransformedCoordinates(untransformedCoordinates), m_currentColor(Qt::black) { } virtual void updateState(const QPaintEngineState &newState) { - if (newState.state() & QPaintEngine::DirtyPen) + if (newState.state() & QPaintEngine::DirtyPen + && newState.pen().color() != m_currentColor) { m_dirtyPen = true; + m_currentColor = newState.pen().color(); + } } virtual void drawTextItem(const QPointF &position, const QTextItem &textItem) @@ -453,7 +456,7 @@ namespace { currentItem.positionOffset = m_glyphs.size(); // Offset into position pool currentItem.useBackendOptimizations = m_useBackendOptimizations; if (m_dirtyPen) - currentItem.color = state->pen().color(); + currentItem.color = m_currentColor; QTransform matrix = m_untransformedCoordinates ? QTransform() : state->transform(); matrix.translate(position.x(), position.y()); @@ -524,6 +527,7 @@ namespace { bool m_dirtyPen; bool m_useBackendOptimizations; bool m_untransformedCoordinates; + QColor m_currentColor; }; class DrawTextItemDevice: public QPaintDevice diff --git a/tests/auto/qstatictext/tst_qstatictext.cpp b/tests/auto/qstatictext/tst_qstatictext.cpp index b6f06d3..66a2dd4 100644 --- a/tests/auto/qstatictext/tst_qstatictext.cpp +++ b/tests/auto/qstatictext/tst_qstatictext.cpp @@ -93,6 +93,9 @@ private slots: void drawUnderlinedText(); void unprintableCharacter_qtbug12614(); + + void underlinedColor_qtbug20159(); + void textDocumentColor(); }; void tst_QStaticText::init() @@ -764,5 +767,42 @@ void tst_QStaticText::unprintableCharacter_qtbug12614() QVERIFY(staticText.size().isValid()); // Force layout. Should not crash. } +void tst_QStaticText::underlinedColor_qtbug20159() +{ + QString multiScriptText; + multiScriptText += QChar(0x0410); // Cyrillic 'A' + multiScriptText += QLatin1Char('A'); + + QStaticText staticText(multiScriptText); + + QFont font; + font.setUnderline(true); + + staticText.prepare(QTransform(), font); + + QStaticTextPrivate *d = QStaticTextPrivate::get(&staticText); + QCOMPARE(d->itemCount, 2); + + // The pen should not be marked as dirty when drawing the underline + QVERIFY(!d->items[0].color.isValid()); + QVERIFY(!d->items[1].color.isValid()); +} + +void tst_QStaticText::textDocumentColor() +{ + QStaticText staticText("AB"); + staticText.setTextFormat(Qt::RichText); + staticText.prepare(); + + QStaticTextPrivate *d = QStaticTextPrivate::get(&staticText); + QCOMPARE(d->itemCount, 2); + + // The pen should not be marked as dirty when drawing the underline + QVERIFY(!d->items[0].color.isValid()); + QVERIFY(d->items[1].color.isValid()); + + QCOMPARE(d->items[1].color, QColor(Qt::red)); +} + QTEST_MAIN(tst_QStaticText) #include "tst_qstatictext.moc" -- cgit v0.12