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); } }
QList<QGlyphRun> QTextFragment::glyphRuns(int pos, int len) const { if (!p || !n) return QList<QGlyphRun>(); int blockNode = p->blockMap().findNode(position()); const QTextBlockData *blockData = p->blockMap().fragment(blockNode); QTextLayout *layout = blockData->layout; int blockPosition = p->blockMap().position(blockNode); if (pos < 0) pos = position() - blockPosition; if (len < 0) len = length(); if (len == 0) return QList<QGlyphRun>(); QList<QGlyphRun> ret; for (int i=0; i<layout->lineCount(); ++i) { QTextLine textLine = layout->lineAt(i); ret += textLine.glyphRuns(pos, len); } return ret; }