From 35acf29a38d4b631dd27ce767466533223e8b2c4 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Fri, 5 Jul 2019 13:08:21 +0200 Subject: Support text color for color fonts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In order to support pen color for color fonts, we have to bake the color into the cache (since the cache contains actual color data and not alpha values). This is equivalent of 78caba7ae637bf4b33631c3425eb92ec3946c99e in Qt Base. [ChangeLog][Text] Added support for text color when using color fonts. Task-number: QTBUG-74761 Change-Id: I5910636c240bd4c0ec3f0b13db4e2f78d4b062ff Reviewed-by: Tor Arne Vestbø --- src/quick/scenegraph/qsgdefaultglyphnode.cpp | 2 +- src/quick/scenegraph/qsgdefaultglyphnode_p.cpp | 30 +++++++++++++++++++++----- src/quick/scenegraph/qsgdefaultglyphnode_p_p.h | 7 +++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/quick/scenegraph/qsgdefaultglyphnode.cpp b/src/quick/scenegraph/qsgdefaultglyphnode.cpp index 0d42102f36..cae0eda3f4 100644 --- a/src/quick/scenegraph/qsgdefaultglyphnode.cpp +++ b/src/quick/scenegraph/qsgdefaultglyphnode.cpp @@ -75,7 +75,7 @@ void QSGDefaultGlyphNode::update() QMargins margins(0, 0, 0, 0); if (m_style == QQuickText::Normal) { - m_material = new QSGTextMaskMaterial(font); + m_material = new QSGTextMaskMaterial(QVector4D(m_color.redF(), m_color.greenF(), m_color.blueF(), m_color.alphaF()), font); } else if (m_style == QQuickText::Outline) { QSGOutlinedTextMaterial *material = new QSGOutlinedTextMaterial(font); material->setStyleColor(m_styleColor); diff --git a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp index ce706d76f7..b9a22dd44b 100644 --- a/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp +++ b/src/quick/scenegraph/qsgdefaultglyphnode_p.cpp @@ -399,10 +399,11 @@ public: } }; -QSGTextMaskMaterial::QSGTextMaskMaterial(const QRawFont &font, QFontEngine::GlyphFormat glyphFormat) +QSGTextMaskMaterial::QSGTextMaskMaterial(const QVector4D &color, const QRawFont &font, QFontEngine::GlyphFormat glyphFormat) : m_texture(nullptr) , m_glyphCache(nullptr) , m_font(font) + , m_color(color) { init(glyphFormat); } @@ -412,12 +413,30 @@ QSGTextMaskMaterial::~QSGTextMaskMaterial() delete m_texture; } +void QSGTextMaskMaterial::setColor(const QVector4D &color) +{ + if (m_color == color) + return; + + m_color = color; + + // If it is an RGB cache, then the pen color is actually part of the cache key + // so it has to be updated + if (m_glyphCache != nullptr && m_glyphCache->glyphFormat() == QFontEngine::Format_ARGB) + updateCache(QFontEngine::Format_ARGB); +} + void QSGTextMaskMaterial::init(QFontEngine::GlyphFormat glyphFormat) { Q_ASSERT(m_font.isValid()); setFlag(Blending, true); + updateCache(glyphFormat); +} + +void QSGTextMaskMaterial::updateCache(QFontEngine::GlyphFormat glyphFormat) +{ QOpenGLContext *ctx = const_cast(QOpenGLContext::currentContext()); Q_ASSERT(ctx != nullptr); @@ -437,20 +456,21 @@ void QSGTextMaskMaterial::init(QFontEngine::GlyphFormat glyphFormat) qreal devicePixelRatio = qsg_device_pixel_ratio(ctx); - QTransform glyphCacheTransform = QTransform::fromScale(devicePixelRatio, devicePixelRatio); if (!fontEngine->supportsTransformation(glyphCacheTransform)) glyphCacheTransform = QTransform(); - m_glyphCache = fontEngine->glyphCache(ctx, glyphFormat, glyphCacheTransform); + QColor color = glyphFormat == QFontEngine::Format_ARGB ? QColor::fromRgbF(m_color.x(), m_color.y(), m_color.z(), m_color.w()) : QColor(); + m_glyphCache = fontEngine->glyphCache(ctx, glyphFormat, glyphCacheTransform, color); if (!m_glyphCache || int(m_glyphCache->glyphFormat()) != glyphFormat) { - m_glyphCache = new QOpenGLTextureGlyphCache(glyphFormat, glyphCacheTransform); + m_glyphCache = new QOpenGLTextureGlyphCache(glyphFormat, glyphCacheTransform, color); fontEngine->setGlyphCache(ctx, m_glyphCache.data()); auto sg = QSGDefaultRenderContext::from(ctx); Q_ASSERT(sg); sg->registerFontengineForCleanup(fontEngine); } } + } void QSGTextMaskMaterial::populate(const QPointF &p, @@ -629,7 +649,7 @@ int QSGTextMaskMaterial::cacheTextureHeight() const QSGStyledTextMaterial::QSGStyledTextMaterial(const QRawFont &font) - : QSGTextMaskMaterial(font, QFontEngine::Format_A8) + : QSGTextMaskMaterial(QVector4D(), font, QFontEngine::Format_A8) { } diff --git a/src/quick/scenegraph/qsgdefaultglyphnode_p_p.h b/src/quick/scenegraph/qsgdefaultglyphnode_p_p.h index b0a2788dd8..56084dea96 100644 --- a/src/quick/scenegraph/qsgdefaultglyphnode_p_p.h +++ b/src/quick/scenegraph/qsgdefaultglyphnode_p_p.h @@ -68,15 +68,15 @@ class Geometry; class QSGTextMaskMaterial: public QSGMaterial { public: - QSGTextMaskMaterial(const QRawFont &font, QFontEngine::GlyphFormat glyphFormat = QFontEngine::Format_None); + QSGTextMaskMaterial(const QVector4D &color, const QRawFont &font, QFontEngine::GlyphFormat glyphFormat = QFontEngine::Format_None); virtual ~QSGTextMaskMaterial(); QSGMaterialType *type() const override; QSGMaterialShader *createShader() const override; int compare(const QSGMaterial *other) const override; - void setColor(const QColor &c) { m_color = QVector4D(c.redF(), c.greenF(), c.blueF(), c.alphaF()); } - void setColor(const QVector4D &color) { m_color = color; } + void setColor(const QColor &c) { setColor(QVector4D(c.redF(), c.greenF(), c.blueF(), c.alphaF())); } + void setColor(const QVector4D &color); const QVector4D &color() const { return m_color; } QSGTexture *texture() const { return m_texture; } @@ -94,6 +94,7 @@ public: private: void init(QFontEngine::GlyphFormat glyphFormat); + void updateCache(QFontEngine::GlyphFormat glyphFormat); QSGPlainTexture *m_texture; QExplicitlySharedDataPointer m_glyphCache; -- cgit v1.2.3