From: Eskil Abrahamsen Blomfeldt Date: Wed, 31 Aug 2011 10:47:20 +0000 (+0200) Subject: Support font color with Text.StyledText in SceneGraph X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=565a7827bb2a7c7af23eade8f3c8809c72f81251;p=konrad%2Fqtdeclarative.git Support font color with Text.StyledText in SceneGraph When Text.StyledText is set as format, QML will use a QTextLayout back-end with additional formats to render styled text, supporting a few HTML tags. To support color changes using this format, we need to process the additional formats in the text node. The code that fetches glyph runs is now separated into its own function and is called once per color-changing format if any such exist. Task-number: QTBUG-21199 Change-Id: Ib9b0cbec8d23de2dfafae9f641a2e3f7819c6a7a Reviewed-on: http://codereview.qt.nokia.com/3982 Reviewed-by: Qt Sanity Bot Reviewed-by: Jiang Jiang --- diff --git a/src/declarative/items/qsgtextnode.cpp b/src/declarative/items/qsgtextnode.cpp index 1ae457b..a8a29ba 100644 --- a/src/declarative/items/qsgtextnode.cpp +++ b/src/declarative/items/qsgtextnode.cpp @@ -281,6 +281,8 @@ namespace { void addSelectedGlyphs(const QGlyphRun &glyphRun); void addUnselectedGlyphs(const QGlyphRun &glyphRun); + void addGlyphsInRange(int rangeStart, int rangeEnd, const QColor &color, + int selectionStart, int selectionEnd); void addToSceneGraph(QSGTextNode *parent, QSGText::TextStyle style = QSGText::Normal, @@ -568,6 +570,45 @@ namespace { m_hasSelection = m_hasSelection || m_currentLineTree.size() > currentSize; } + void SelectionEngine::addGlyphsInRange(int rangeStart, int rangeLength, + const QColor &color, + int selectionStart, int selectionEnd) + { + QColor oldColor = m_textColor; + m_textColor = color; + + QTextLine &line = m_currentLine; + int rangeEnd = rangeStart + rangeLength; + if (selectionStart > rangeEnd || selectionEnd < rangeStart) { + QList glyphRuns = line.glyphRuns(rangeStart, rangeLength); + for (int j=0; j glyphRuns = line.glyphRuns(rangeStart, + qMin(selectionStart - rangeStart, + rangeLength)); + + for (int j=0; j= selectionStart && selectionStart >= rangeStart) { + QList glyphRuns = line.glyphRuns(selectionStart, selectionEnd - selectionStart + 1); + + for (int j=0; j= rangeStart && selectionEnd < rangeEnd) { + QList glyphRuns = line.glyphRuns(selectionEnd + 1, rangeEnd - selectionEnd); + for (int j=0; j additionalFormats = textLayout->additionalFormats(); + QVarLengthArray colorChanges; + for (int i=0; ilineCount(); ++i) { QTextLine line = textLayout->lineAt(i); engine.setCurrentLine(line); - int lineEnd = line.textStart() + line.textLength(); - if (selectionStart > lineEnd || selectionEnd < line.textStart()) { - QList glyphRuns = line.glyphRuns(); - for (int j=0; j glyphRuns = line.glyphRuns(line.textStart(), - qMin(selectionStart - line.textStart(), - line.textLength())); + int currentPosition = line.textStart(); + int remainingLength = line.textLength(); + for (int j=0; j= currentPosition + && range.start < currentPosition + remainingLength) { - for (int j=0; j currentPosition) { + engine.addGlyphsInRange(currentPosition, range.start - currentPosition, + color, selectionStart, selectionEnd); + } - if (lineEnd >= selectionStart && selectionStart >= line.textStart()) { - QList glyphRuns = line.glyphRuns(selectionStart, selectionEnd - selectionStart + 1); + int rangeEnd = qMin(range.start + range.length, currentPosition + remainingLength); + QColor rangeColor = range.format.foreground().color(); - for (int j=0; j= line.textStart() && selectionEnd < lineEnd) { - QList glyphRuns = line.glyphRuns(selectionEnd + 1, lineEnd - selectionEnd); - for (int j=0; j currentPosition + remainingLength || remainingLength <= 0) { + break; } } + + if (remainingLength > 0) { + engine.addGlyphsInRange(currentPosition, remainingLength, color, + selectionStart, selectionEnd); + } } engine.addToSceneGraph(this, style, styleColor);