void setUnicodeText(const QString &text, const QFont &font, const QColor &color) { deleteContent(); QRawFont raw_font = QRawFont::fromFont(font, QFontDatabase::Latin); qreal line_width = raw_font.averageCharWidth() * text.size(); QSGRenderContext *sgr = QQuickItemPrivate::get(m_owner)->sceneGraphRenderContext(); QTextLayout layout(text,font); layout.beginLayout(); QTextLine line = layout.createLine(); line.setLineWidth(line_width); //Q_ASSERT(!layout.createLine().isValid()); layout.endLayout(); QList<QGlyphRun> glyphRuns = line.glyphRuns(); qreal xpos = 0; for (int i = 0; i < glyphRuns.size(); i++) { QSGGlyphNode *node = sgr->sceneGraphContext()->createGlyphNode(sgr, false); node->setOwnerElement(m_owner); node->geometry()->setIndexDataPattern(QSGGeometry::StaticPattern); node->geometry()->setVertexDataPattern(QSGGeometry::StaticPattern); node->setGlyphs(QPointF(xpos, raw_font.ascent()), glyphRuns.at(i)); node->setStyle(QQuickText::Normal); node->setColor(color); xpos += raw_font.averageCharWidth() * glyphRuns.at(i).positions().size(); node->update(); appendChildNode(node); } }
void QQuickTextNode::setColor(const QColor &color) { if (m_usePixmapCache) { setUpdateFlag(UpdateNodes); } else { for (QSGNode *childNode = firstChild(); childNode; childNode = childNode->nextSibling()) { if (childNode->subType() == GlyphNodeSubType) { QSGGlyphNode *glyphNode = static_cast<QSGGlyphNode *>(childNode); if (glyphNode->color() == m_color) glyphNode->setColor(color); } else if (childNode->subType() == SolidRectNodeSubType) { QSGSimpleRectNode *solidRectNode = static_cast<QSGSimpleRectNode *>(childNode); if (solidRectNode->color() == m_color) solidRectNode->setColor(color); } } } m_color = color; }
QSGGlyphNode *QQuickTextNode::addGlyphs(const QPointF &position, const QGlyphRun &glyphs, const QColor &color, QQuickText::TextStyle style, const QColor &styleColor, QSGNode *parentNode) { QSGRenderContext *sg = QQuickItemPrivate::get(m_ownerElement)->sceneGraphRenderContext(); QRawFont font = glyphs.rawFont(); bool preferNativeGlyphNode = m_useNativeRenderer; if (!preferNativeGlyphNode) { QRawFontPrivate *fontPriv = QRawFontPrivate::get(font); if (fontPriv->fontEngine->hasUnreliableGlyphOutline()) { preferNativeGlyphNode = true; } else { QFontEngine *fe = QRawFontPrivate::get(font)->fontEngine; preferNativeGlyphNode = !fe->isSmoothlyScalable; } } QSGGlyphNode *node = sg->sceneGraphContext()->createGlyphNode(sg, preferNativeGlyphNode); node->setOwnerElement(m_ownerElement); node->setGlyphs(position + QPointF(0, glyphs.rawFont().ascent()), glyphs); node->setStyle(style); node->setStyleColor(styleColor); node->setColor(color); node->update(); /* We flag the geometry as static, but we never call markVertexDataDirty or markIndexDataDirty on them. This is because all text nodes are discarded when a change occurs. If we start appending/removing from existing geometry, then we also need to start marking the geometry as dirty. */ node->geometry()->setIndexDataPattern(QSGGeometry::StaticPattern); node->geometry()->setVertexDataPattern(QSGGeometry::StaticPattern); if (parentNode == 0) parentNode = this; parentNode->appendChildNode(node); return node; }
void setLatinText(const QString &text, const QFont &font, const QColor &color) { QRawFont raw_font = QRawFont::fromFont(font, QFontDatabase::Latin); if (raw_font != m_raw_font) { m_raw_font = raw_font; m_positions.clear(); } if (m_positions.size() < text.size()) { qreal x_pos = 0; qreal max_char_width = raw_font.averageCharWidth(); qreal ascent = raw_font.ascent(); if (m_positions.size()) x_pos = m_positions.last().x() + max_char_width; int to_add = text.size() - m_positions.size(); for (int i = 0; i < to_add; i++) { m_positions << QPointF(x_pos, ascent); x_pos += max_char_width; } } deleteContent(); QSGRenderContext *sgr = QQuickItemPrivate::get(m_owner)->sceneGraphRenderContext(); QSGGlyphNode *node = sgr->sceneGraphContext()->createGlyphNode(sgr, false); node->setOwnerElement(m_owner); node->geometry()->setIndexDataPattern(QSGGeometry::StaticPattern); node->geometry()->setVertexDataPattern(QSGGeometry::StaticPattern); node->setStyle(QQuickText::Normal); node->setColor(color); QGlyphRun glyphrun; glyphrun.setRawFont(raw_font); glyphrun.setGlyphIndexes(raw_font.glyphIndexesForString(text)); glyphrun.setPositions(m_positions); node->setGlyphs(QPointF(0, raw_font.ascent()), glyphrun); node->update(); appendChildNode(node); }
QSGGlyphNode *QSGTextNode::addGlyphs(const QPointF &position, const QGlyphRun &glyphs, const QColor &color, QSGText::TextStyle style, const QColor &styleColor, QSGGlyphNode *prevNode) { QSGGlyphNode *node = prevNode; if (!node) node = m_context->createGlyphNode(); node->setGlyphs(position, glyphs); if (node != prevNode) { node->setStyle(style); node->setStyleColor(styleColor); node->setColor(color); } node->update(); if (node != prevNode) appendChildNode(node); return node; }