7#include <QtCore/qpoint.h>
8#include <QtGui/qabstracttextdocumentlayout.h>
9#include <QtGui/qrawfont.h>
10#include <QtGui/qtextdocument.h>
11#include <QtGui/qtextlayout.h>
12#include <QtGui/qtextobject.h>
13#include <QtGui/qtexttable.h>
14#include <QtGui/qtextlist.h>
16#include <private/qquicktext_p.h>
17#include <private/qtextdocumentlayout_p.h>
18#include <private/qtextimagehandler_p.h>
19#include <private/qrawfont_p.h>
20#include <private/qglyphrun_p.h>
21#include <private/qquickitem_p.h>
22#include <private/qsgdistancefieldglyphnode_p.h>
27 : fontEngine(QRawFontPrivate::get(node->glyphRun.rawFont())->fontEngine)
28 , clipNode(node->clipNode)
29 , color(node->color.rgba())
30 , selectionState(node->selectionState)
35 SelectionState selState,
37 const Decorations &decs,
39 const QColor &bc,
const QColor &dc,
40 const QPointF &pos, qreal a)
55 ranges.append(std::make_pair(d->textRangeStart, d->textRangeEnd));
60 Decorations decorations,
const QColor &textColor,
61 const QColor &backgroundColor,
const QColor &decorationColor,
const QPointF &position)
63 QRectF searchRect = glyphRun.boundingRect();
64 searchRect.translate(position);
66 if (qFuzzyIsNull(searchRect.width()) || qFuzzyIsNull(searchRect.height()))
74 qreal ascent = glyphRun.rawFont().ascent();
75 insert(binaryTree, BinaryTreeNode(glyphRun,
88 int newIndex = binaryTree->size();
89 binaryTree->append(binaryTreeNode);
95 BinaryTreeNode *node = binaryTree->data() + searchIndex;
96 if (binaryTreeNode.boundingRect.left() < node->boundingRect.left()) {
97 if (node->leftChildIndex < 0) {
98 node->leftChildIndex = newIndex;
101 searchIndex = node->leftChildIndex;
104 if (node->rightChildIndex < 0) {
105 node->rightChildIndex = newIndex;
108 searchIndex = node->rightChildIndex;
115 QVarLengthArray<
int> *sortedIndexes,
int currentIndex)
117 Q_ASSERT(currentIndex < binaryTree.size());
120 if (node->leftChildIndex >= 0)
121 inOrder(binaryTree, sortedIndexes, node->leftChildIndex);
123 sortedIndexes->append(currentIndex);
125 if (node->rightChildIndex >= 0)
126 inOrder(binaryTree, sortedIndexes, node->rightChildIndex);
131 const QTextCharFormat &charFormat,
132 const QColor &textColor,
133 const QVarLengthArray<QTextLayout::FormatRange> &colorChanges,
134 int textPos,
int fragmentEnd,
135 int selectionStart,
int selectionEnd)
137 if (charFormat.foreground().style() != Qt::NoBrush)
142 while (textPos < fragmentEnd) {
143 int blockRelativePosition = textPos - block.position();
144 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
147 if (!currentLine().isValid()
148 || line.lineNumber() != currentLine().lineNumber()) {
152 Q_ASSERT(line.textLength() > 0);
153 int lineEnd = line.textStart() + block.position() + line.textLength();
155 int len = qMin(lineEnd - textPos, fragmentEnd - textPos);
158 int currentStepEnd = textPos + len;
160 addGlyphsForRanges(colorChanges,
161 textPos - block.position(),
162 currentStepEnd - block.position(),
163 selectionStart - block.position(),
164 selectionEnd - block.position());
166 textPos = currentStepEnd;
171void QQuickTextNodeEngine::addTextDecorations(
const QVarLengthArray<TextDecoration> &textDecorations,
172 qreal offset, qreal thickness)
174 for (
int i=0; i<textDecorations.size(); ++i) {
175 TextDecoration textDecoration = textDecorations.at(i);
178 QRectF &rect = textDecoration.rect;
179 rect.setY(qRound(rect.y() + m_currentLine.ascent() + offset));
180 rect.setHeight(thickness);
183 m_lines.append(textDecoration);
190 if (m_currentLineTree.isEmpty())
199 QVarLengthArray<
int> sortedIndexes;
200 BinaryTreeNode::inOrder(m_currentLineTree, &sortedIndexes);
202 Q_ASSERT(sortedIndexes.size() == m_currentLineTree.size());
204 SelectionState currentSelectionState = Unselected;
208 qreal underlineOffset = 0.0;
209 qreal underlineThickness = 0.0;
211 qreal overlineOffset = 0.0;
212 qreal overlineThickness = 0.0;
214 qreal strikeOutOffset = 0.0;
215 qreal strikeOutThickness = 0.0;
217 QRectF decorationRect = currentRect;
220 QColor lastBackgroundColor;
221 QColor lastDecorationColor;
223 QVarLengthArray<TextDecoration> pendingUnderlines;
224 QVarLengthArray<TextDecoration> pendingOverlines;
225 QVarLengthArray<TextDecoration> pendingStrikeOuts;
226 if (!sortedIndexes.isEmpty()) {
227 QQuickDefaultClipNode *currentClipNode = m_hasSelection ?
new QQuickDefaultClipNode(QRectF()) :
nullptr;
228 bool currentClipNodeUsed =
false;
229 for (
int i=0; i<=sortedIndexes.size(); ++i) {
231 if (i < sortedIndexes.size()) {
232 int sortedIndex = sortedIndexes.at(i);
233 Q_ASSERT(sortedIndex < m_currentLineTree.size());
235 node = m_currentLineTree.data() + sortedIndex;
237 currentSelectionState = node->selectionState;
242 decorationRect.setY(m_position.y() + m_currentLine.y());
243 decorationRect.setHeight(m_currentLine.height());
246 decorationRect.setRight(node->boundingRect.left());
248 TextDecoration textDecoration(currentSelectionState, decorationRect, lastColor);
249 if (lastDecorationColor.isValid() &&
253 textDecoration.color = lastDecorationColor;
256 pendingUnderlines.append(textDecoration);
259 pendingOverlines.append(textDecoration);
262 pendingStrikeOuts.append(textDecoration);
264 if (currentDecorations & Decoration::Background)
265 m_backgrounds.append(std::make_pair(decorationRect, lastBackgroundColor));
271 if (node ==
nullptr || node->selectionState != currentSelectionState) {
272 currentRect.setY(m_position.y() + m_currentLine.y());
273 currentRect.setHeight(m_currentLine.height());
275 if (currentSelectionState == Selected)
276 m_selectionRects.append(currentRect);
278 if (currentClipNode !=
nullptr) {
279 if (!currentClipNodeUsed) {
280 delete currentClipNode;
282 currentClipNode->setIsRectangular(
true);
283 currentClipNode->setRect(currentRect);
284 currentClipNode->update();
288 if (node !=
nullptr && m_hasSelection)
289 currentClipNode =
new QQuickDefaultClipNode(QRectF());
291 currentClipNode =
nullptr;
292 currentClipNodeUsed =
false;
294 if (node !=
nullptr) {
295 currentSelectionState = node->selectionState;
296 currentRect = node->boundingRect;
299 if (currentRect.isNull())
300 currentRect.setSize(QSizeF(1, 1));
303 if (currentRect.isNull())
304 currentRect = node->boundingRect;
306 currentRect = currentRect.united(node->boundingRect);
309 if (node !=
nullptr) {
310 if (node->selectionState == Selected) {
311 node->clipNode = currentClipNode;
312 currentClipNodeUsed =
true;
315 decorationRect = node->boundingRect;
319 if (!pendingUnderlines.isEmpty()
321 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
323 pendingUnderlines.clear();
325 underlineOffset = 0.0;
326 underlineThickness = 0.0;
331 if (!pendingOverlines.isEmpty()) {
332 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
334 pendingOverlines.clear();
336 overlineOffset = 0.0;
337 overlineThickness = 0.0;
342 if (!pendingStrikeOuts.isEmpty()) {
343 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
345 pendingStrikeOuts.clear();
347 strikeOutOffset = 0.0;
348 strikeOutThickness = 0.0;
352 QRawFont rawFont = node->glyphRun.rawFont();
354 if (rawFont.lineThickness() > underlineThickness) {
355 underlineThickness = rawFont.lineThickness();
356 underlineOffset = rawFont.underlinePosition();
361 overlineOffset = -rawFont.ascent();
362 overlineThickness = rawFont.lineThickness();
366 strikeOutThickness = rawFont.lineThickness();
367 strikeOutOffset = rawFont.ascent() / -3.0;
370 currentDecorations = node->decorations;
371 lastColor = node->color;
372 lastBackgroundColor = node->backgroundColor;
373 lastDecorationColor = node->decorationColor;
374 m_processedNodes.append(*node);
378 if (!pendingUnderlines.isEmpty())
379 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
381 if (!pendingOverlines.isEmpty())
382 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
384 if (!pendingStrikeOuts.isEmpty())
385 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
388 m_currentLineTree.clear();
389 m_currentLine = QTextLine();
390 m_hasSelection =
false;
394 SelectionState selectionState,
395 QTextFrameFormat::Position layoutPosition)
397 QRectF searchRect = rect;
398 if (layoutPosition == QTextFrameFormat::InFlow) {
399 if (m_currentLineTree.isEmpty()) {
400 qreal y = m_currentLine.ascent() - ascent;
401 if (m_currentTextDirection == Qt::RightToLeft)
402 searchRect.moveTopRight(m_position + m_currentLine.rect().topRight() + QPointF(0, y));
404 searchRect.moveTopLeft(m_position + m_currentLine.position() + QPointF(0, y));
406 const BinaryTreeNode *lastNode = m_currentLineTree.data() + m_currentLineTree.size() - 1;
407 if (lastNode->glyphRun.isRightToLeft()) {
408 QPointF lastPos = lastNode->boundingRect.topLeft();
409 searchRect.moveTopRight(lastPos - QPointF(0, ascent - lastNode->ascent));
411 QPointF lastPos = lastNode->boundingRect.topRight();
412 searchRect.moveTopLeft(lastPos - QPointF(0, ascent - lastNode->ascent));
417 BinaryTreeNode::insert(&m_currentLineTree, searchRect, image, ascent, selectionState);
418 m_hasContents =
true;
422 SelectionState selectionState,
423 QTextDocument *textDocument,
int pos,
424 QTextFrameFormat::Position layoutPosition)
426 QTextObjectInterface *handler = textDocument->documentLayout()->handlerForObject(format.objectType());
427 if (handler !=
nullptr) {
429 QSizeF size = handler->intrinsicSize(textDocument, pos, format);
431 if (format.objectType() == QTextFormat::ImageObject) {
432 QTextImageFormat imageFormat = format.toImageFormat();
433 QTextImageHandler *imageHandler =
static_cast<QTextImageHandler *>(handler);
434 image = imageHandler->image(textDocument, imageFormat);
437 if (image.isNull()) {
438 image = QImage((size * m_devicePixelRatio).toSize(), QImage::Format_ARGB32_Premultiplied);
439 image.setDevicePixelRatio(m_devicePixelRatio);
440 image.fill(Qt::transparent);
442 QPainter painter(&image);
443 handler->drawObject(&painter, QRectF({}, size), textDocument, pos, format);
451 QTextLine line = block.layout()->lineForTextPosition(pos - block.position());
452 switch (format.verticalAlignment())
454 case QTextCharFormat::AlignTop:
455 ascent = line.ascent();
457 case QTextCharFormat::AlignMiddle:
459 ascent = (line.ascent() + line.descent()) / 2 - line.descent() + size.height() / 2;
461 case QTextCharFormat::AlignBottom:
462 ascent = size.height() - line.descent();
464 case QTextCharFormat::AlignBaseline:
466 ascent = size.height();
469 addImage(QRectF(position, size), image, ascent, selectionState, layoutPosition);
475 BinaryTreeNode::insert(&m_currentLineTree,
478 Decoration::NoDecoration,
487 int currentSize = m_currentLineTree.size();
488 BinaryTreeNode::insert(&m_currentLineTree,
491 Decoration::NoDecoration,
496 m_hasSelection = m_hasSelection || m_currentLineTree.size() > currentSize;
501 int selectionStart,
int selectionEnd)
503 int currentPosition = start;
504 int remainingLength = end - start;
505 for (
int j=0; j<ranges.size(); ++j) {
506 const QTextLayout::FormatRange &range = ranges.at(j);
507 if (range.start + range.length > currentPosition
508 && range.start < currentPosition + remainingLength) {
510 if (range.start > currentPosition) {
512 QColor()
, QColor()
, QColor()
, selectionStart
, selectionEnd
);
514 int rangeEnd = qMin(range.start + range.length, currentPosition + remainingLength);
516 if (range.format.hasProperty(QTextFormat::ForegroundBrush))
517 rangeColor = range.format.foreground().color();
518 else if (range.format.isAnchor())
519 rangeColor = m_anchorColor;
520 QColor rangeBackgroundColor = range.format.hasProperty(QTextFormat::BackgroundBrush)
521 ? range.format.background().color()
524 QColor rangeDecorationColor = range.format.hasProperty(QTextFormat::TextUnderlineColor)
525 ? range.format.underlineColor()
529 rangeColor
, rangeBackgroundColor
, rangeDecorationColor
,
530 selectionStart
, selectionEnd
);
532 currentPosition = range.start + range.length;
533 remainingLength = end - currentPosition;
535 }
else if (range.start > currentPosition + remainingLength || remainingLength <= 0) {
540 if (remainingLength > 0) {
542 selectionStart
, selectionEnd
);
548 const QColor &color,
const QColor &backgroundColor,
const QColor &decorationColor,
549 int selectionStart,
int selectionEnd)
552 if (color.isValid()) {
553 oldColor = m_textColor;
557 QColor oldBackgroundColor = m_backgroundColor;
558 if (backgroundColor.isValid()) {
559 oldBackgroundColor = m_backgroundColor;
560 m_backgroundColor = backgroundColor;
563 QColor oldDecorationColor = m_decorationColor;
564 if (decorationColor.isValid()) {
565 oldDecorationColor = m_decorationColor;
566 m_decorationColor = decorationColor;
569 bool hasSelection = selectionEnd >= 0
570 && selectionStart <= selectionEnd;
572 QTextLine &line = m_currentLine;
573 int rangeEnd = rangeStart + rangeLength;
574 if (!hasSelection || (selectionStart > rangeEnd || selectionEnd < rangeStart)) {
575 QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, rangeLength);
576 for (
int j=0; j<glyphRuns.size(); ++j) {
577 const QGlyphRun &glyphRun = glyphRuns.at(j);
578 addUnselectedGlyphs(glyphRun);
581 if (rangeStart < selectionStart) {
582 int length = qMin(selectionStart - rangeStart, rangeLength);
583 QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, length);
584 for (
int j=0; j<glyphRuns.size(); ++j) {
585 const QGlyphRun &glyphRun = glyphRuns.at(j);
586 addUnselectedGlyphs(glyphRun);
590 if (rangeEnd > selectionStart) {
591 int start = qMax(selectionStart, rangeStart);
592 int length = qMin(selectionEnd - start + 1, rangeEnd - start);
593 QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
595 for (
int j=0; j<glyphRuns.size(); ++j) {
596 const QGlyphRun &glyphRun = glyphRuns.at(j);
597 addSelectedGlyphs(glyphRun);
601 if (selectionEnd >= rangeStart && selectionEnd < rangeEnd) {
602 int start = selectionEnd + 1;
603 int length = rangeEnd - selectionEnd - 1;
604 QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
605 for (
int j=0; j<glyphRuns.size(); ++j) {
606 const QGlyphRun &glyphRun = glyphRuns.at(j);
607 addUnselectedGlyphs(glyphRun);
612 if (decorationColor.isValid())
613 m_decorationColor = oldDecorationColor;
615 if (backgroundColor.isValid())
616 m_backgroundColor = oldBackgroundColor;
618 if (oldColor.isValid())
619 m_textColor = oldColor;
623 QTextFrameFormat::BorderStyle borderStyle,
624 const QBrush &borderBrush)
626 const QColor &color = borderBrush.color();
629 Q_UNUSED(borderStyle);
631 m_backgrounds.append(std::make_pair(QRectF(rect.left(), rect.top(), border, rect.height() + border), color));
632 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.top(), rect.width(), border), color));
633 m_backgrounds.append(std::make_pair(QRectF(rect.right(), rect.top() + border, border, rect.height() - border), color));
634 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.bottom(), rect.width(), border), color));
639 QTextDocumentLayout *documentLayout = qobject_cast<QTextDocumentLayout *>(document->documentLayout());
640 if (Q_UNLIKELY(!documentLayout))
643 QTextFrameFormat frameFormat = frame->format().toFrameFormat();
644 QTextTable *table = qobject_cast<QTextTable *>(frame);
646 QRectF boundingRect = table ==
nullptr
647 ? documentLayout->frameBoundingRect(frame)
648 : documentLayout->tableBoundingRect(table);
650 QBrush bg = frame->frameFormat().background();
651 if (bg.style() != Qt::NoBrush)
652 m_backgrounds.append(std::make_pair(boundingRect, bg.color()));
654 if (!frameFormat.hasProperty(QTextFormat::FrameBorder))
657 qreal borderWidth = frameFormat.border();
658 if (qFuzzyIsNull(borderWidth))
661 QBrush borderBrush = frameFormat.borderBrush();
662 QTextFrameFormat::BorderStyle borderStyle = frameFormat.borderStyle();
663 if (borderStyle == QTextFrameFormat::BorderStyle_None)
666 const auto collapsed = table->format().borderCollapse();
669 addBorder(boundingRect.adjusted(frameFormat.leftMargin(), frameFormat.topMargin(),
670 -frameFormat.rightMargin() - borderWidth,
671 -frameFormat.bottomMargin() - borderWidth),
672 borderWidth, borderStyle, borderBrush);
674 if (table !=
nullptr) {
675 int rows = table->rows();
676 int columns = table->columns();
678 for (
int row=0; row<rows; ++row) {
679 for (
int column=0; column<columns; ++column) {
680 QTextTableCell cell = table->cellAt(row, column);
682 QRectF cellRect = documentLayout->tableCellBoundingRect(table, cell);
683 addBorder(cellRect.adjusted(-borderWidth, -borderWidth, collapsed ? -borderWidth : 0, collapsed ? -borderWidth : 0), borderWidth,
684 borderStyle, borderBrush);
692 return qHashMulti(seed, key.fontEngine, key.clipNode, key.color, key
.selectionState);
696 QList<BinaryTreeNode *> *imageNodes)
698 QHash<BinaryTreeNodeKey, QList<BinaryTreeNode *> > map;
700 for (
int i = 0; i < m_processedNodes.size(); ++i) {
703 if (node->image.isNull()) {
704 if (node->glyphRun.isEmpty())
709 QList<BinaryTreeNode *> &nodes = map[key];
711 regularNodes->append(node);
715 imageNodes->append(node);
719 for (
int i = 0; i < regularNodes->size(); ++i) {
723 const QList<BinaryTreeNode *> &nodes = map.value(key);
724 Q_ASSERT(nodes.first() == primaryNode);
727 for (
int j = 0; j < nodes.size(); ++j)
728 count += nodes.at(j)->glyphRun.glyphIndexes().size();
730 if (count != primaryNode->glyphRun.glyphIndexes().size()) {
731 QGlyphRun &glyphRun = primaryNode->glyphRun;
732 QVector<quint32> glyphIndexes = glyphRun.glyphIndexes();
733 glyphIndexes.reserve(count);
735 QVector<QPointF> glyphPositions = glyphRun.positions();
736 glyphPositions.reserve(count);
738 QRectF glyphBoundingRect = glyphRun.boundingRect();
740 for (
int j = 1; j < nodes.size(); ++j) {
742 glyphIndexes += otherNode->glyphRun.glyphIndexes();
743 primaryNode->ranges += otherNode->ranges;
744 glyphBoundingRect = glyphBoundingRect.united(otherNode->boundingRect);
746 QVector<QPointF> otherPositions = otherNode->glyphRun.positions();
747 for (
int k = 0; k < otherPositions.size(); ++k)
748 glyphPositions += otherPositions.at(k) + (otherNode->position - primaryNode->position);
751 Q_ASSERT(glyphPositions.size() == count);
752 Q_ASSERT(glyphIndexes.size() == count);
754 glyphRun.setGlyphIndexes(glyphIndexes);
755 glyphRun.setPositions(glyphPositions);
756 glyphRun.setBoundingRect(glyphBoundingRect);
762 QQuickText::TextStyle style,
763 const QColor &styleColor)
765 if (m_currentLine.isValid())
766 processCurrentLine();
768 QList<BinaryTreeNode *> nodes;
769 QList<BinaryTreeNode *> imageNodes;
770 mergeProcessedNodes(&nodes, &imageNodes);
772 for (
int i = 0; i < m_backgrounds.size(); ++i) {
773 const QRectF &rect = m_backgrounds.at(i).first;
774 const QColor &color = m_backgrounds.at(i).second;
775 if (color.alpha() != 0)
776 parentNode->addRectangleNode(rect, color);
780 for (
int i = 0; i < nodes.size(); ++i) {
781 const BinaryTreeNode *node = nodes.at(i);
782 parentNode->addGlyphs(node->position, node->glyphRun, node->color, style, styleColor,
nullptr);
785 for (
int i = 0; i < imageNodes.size(); ++i) {
786 const BinaryTreeNode *node = imageNodes.at(i);
787 if (node->selectionState == Unselected)
788 parentNode->addImage(node->boundingRect, node->image);
792 for (
int i = 0; i < m_selectionRects.size(); ++i) {
793 const QRectF &rect = m_selectionRects.at(i);
794 if (m_selectionColor.alpha() != 0)
795 parentNode->addRectangleNode(rect, m_selectionColor);
799 for (
int i = 0; i < m_lines.size(); ++i) {
800 const TextDecoration &textDecoration = m_lines.at(i);
802 QColor color = textDecoration.selectionState == Selected
803 ? m_selectedTextColor
804 : textDecoration.color;
806 parentNode->addDecorationNode(textDecoration.rect, color);
810 for (
int i = 0; i < nodes.size(); ++i) {
811 const BinaryTreeNode *node = nodes.at(i);
812 QQuickDefaultClipNode *clipNode = node->clipNode;
813 if (clipNode !=
nullptr && clipNode->parent() ==
nullptr)
814 parentNode->appendChildNode(clipNode);
816 if (node->selectionState == Selected) {
817 QColor color = m_selectedTextColor;
818 int previousNodeIndex = i - 1;
819 int nextNodeIndex = i + 1;
820 const BinaryTreeNode *previousNode = previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
821 while (previousNode !=
nullptr && qFuzzyCompare(previousNode->boundingRect.left(), node->boundingRect.left()))
822 previousNode = --previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
824 const BinaryTreeNode *nextNode = nextNodeIndex == nodes.size() ? 0 : nodes.at(nextNodeIndex);
826 if (previousNode !=
nullptr && previousNode->selectionState == Unselected)
827 parentNode->addGlyphs(previousNode->position, previousNode->glyphRun, color, style, styleColor, clipNode);
829 if (nextNode !=
nullptr && nextNode->selectionState == Unselected)
830 parentNode->addGlyphs(nextNode->position, nextNode->glyphRun, color, style, styleColor, clipNode);
834 bool drawCurrent =
false;
835 if (previousNode !=
nullptr || nextNode !=
nullptr) {
836 for (
int i = 0; i < node->ranges.size(); ++i) {
837 const std::pair<
int,
int> &range = node->ranges.at(i);
839 int rangeLength = range.second - range.first + 1;
840 if (previousNode !=
nullptr) {
841 for (
int j = 0; j < previousNode->ranges.size(); ++j) {
842 const std::pair<
int,
int> &otherRange = previousNode->ranges.at(j);
844 if (range.first < otherRange.second && range.second > otherRange.first) {
845 int start = qMax(range.first, otherRange.first);
846 int end = qMin(range.second, otherRange.second);
847 rangeLength -= end - start + 1;
848 if (rangeLength == 0)
854 if (nextNode !=
nullptr && rangeLength > 0) {
855 for (
int j = 0; j < nextNode->ranges.size(); ++j) {
856 const std::pair<
int,
int> &otherRange = nextNode->ranges.at(j);
858 if (range.first < otherRange.second && range.second > otherRange.first) {
859 int start = qMax(range.first, otherRange.first);
860 int end = qMin(range.second, otherRange.second);
861 rangeLength -= end - start + 1;
862 if (rangeLength == 0)
868 if (rangeLength > 0) {
878 parentNode->addGlyphs(node->position, node->glyphRun, color, style, styleColor, clipNode);
882 for (
int i = 0; i < imageNodes.size(); ++i) {
883 const BinaryTreeNode *node = imageNodes.at(i);
884 if (node->selectionState == Selected) {
885 parentNode->addImage(node->boundingRect, node->image);
886 if (node->selectionState == Selected) {
887 QColor color = m_selectionColor;
889 parentNode->addRectangleNode(node->boundingRect, color);
895void QQuickTextNodeEngine::mergeFormats(QTextLayout *textLayout, QVarLengthArray<QTextLayout::FormatRange> *mergedFormats)
897 Q_ASSERT(mergedFormats !=
nullptr);
898 if (textLayout ==
nullptr)
901 QVector<QTextLayout::FormatRange> additionalFormats = textLayout->formats();
902 for (
int i=0; i<additionalFormats.size(); ++i) {
903 QTextLayout::FormatRange additionalFormat = additionalFormats.at(i);
904 if (additionalFormat.format.hasProperty(QTextFormat::ForegroundBrush)
905 || additionalFormat.format.hasProperty(QTextFormat::BackgroundBrush)
906 || additionalFormat.format.isAnchor()) {
908 if (!mergedFormats->isEmpty()) {
909 QTextLayout::FormatRange *lastFormat = mergedFormats->data() + mergedFormats->size() - 1;
911 if (additionalFormat.start < lastFormat->start + lastFormat->length) {
912 QTextLayout::FormatRange *mergedRange =
nullptr;
914 int length = additionalFormat.length;
915 if (additionalFormat.start > lastFormat->start) {
916 lastFormat->length = additionalFormat.start - lastFormat->start;
917 length -= lastFormat->length;
919 mergedFormats->append(QTextLayout::FormatRange());
920 mergedRange = mergedFormats->data() + mergedFormats->size() - 1;
921 lastFormat = mergedFormats->data() + mergedFormats->size() - 2;
923 mergedRange = lastFormat;
926 mergedRange->format = lastFormat->format;
927 mergedRange->format.merge(additionalFormat.format);
928 mergedRange->start = additionalFormat.start;
930 int end = qMin(additionalFormat.start + additionalFormat.length,
931 lastFormat->start + lastFormat->length);
933 mergedRange->length = end - mergedRange->start;
934 length -= mergedRange->length;
936 additionalFormat.start = end;
937 additionalFormat.length = length;
941 if (additionalFormat.length > 0)
942 mergedFormats->append(additionalFormat);
949
950
951
952
953
954
955
956
958 const QColor &textColor,
const QColor &anchorColor,
int selectionStart,
int selectionEnd,
const QRectF &viewport)
960 Q_ASSERT(textDocument);
962 int preeditLength = block.isValid() ? block.layout()->preeditAreaText().size() : 0;
963 int preeditPosition = block.isValid() ? block.layout()->preeditAreaPosition() : -1;
966 setCurrentTextDirection(block.textDirection());
968 QVarLengthArray<QTextLayout::FormatRange> colorChanges;
969 mergeFormats(block.layout(), &colorChanges);
971 const QTextCharFormat charFormat = block.charFormat();
972 const QRectF blockBoundingRect = textDocument->documentLayout()->blockBoundingRect(block).translated(position);
973 if (viewport.isValid()) {
974 if (!blockBoundingRect.intersects(viewport))
976 qCDebug(lcSgText) <<
"adding block with length" << block.length() <<
':' << blockBoundingRect <<
"in viewport" << viewport;
979 if (charFormat.background().style() != Qt::NoBrush)
980 m_backgrounds.append(std::make_pair(blockBoundingRect, charFormat.background().color()));
982 if (QTextList *textList = block.textList()) {
983 QPointF pos = blockBoundingRect.topLeft();
984 QTextLayout *layout = block.layout();
985 if (layout->lineCount() > 0) {
986 QTextLine firstLine = layout->lineAt(0);
987 Q_ASSERT(firstLine.isValid());
991 QRectF textRect = firstLine.naturalTextRect();
992 pos += textRect.topLeft();
993 if (block.textDirection() == Qt::RightToLeft)
994 pos.rx() += textRect.width();
996 QFont font(charFormat.font());
997 QFontMetricsF fontMetrics(font);
998 QTextListFormat listFormat = textList->format();
1000 QString listItemBullet;
1001 switch (listFormat.style()) {
1002 case QTextListFormat::ListCircle:
1003 listItemBullet = QChar(0x25E6);
1005 case QTextListFormat::ListSquare:
1006 listItemBullet = QChar(0x25AA);
1008 case QTextListFormat::ListDecimal:
1009 case QTextListFormat::ListLowerAlpha:
1010 case QTextListFormat::ListUpperAlpha:
1011 case QTextListFormat::ListLowerRoman:
1012 case QTextListFormat::ListUpperRoman:
1013 listItemBullet = textList->itemText(block);
1016 listItemBullet = QChar(0x2022);
1020 switch (block.blockFormat().marker()) {
1021 case QTextBlockFormat::MarkerType::Checked:
1022 listItemBullet = QChar(0x2612);
1024 case QTextBlockFormat::MarkerType::Unchecked:
1025 listItemBullet = QChar(0x2610);
1027 case QTextBlockFormat::MarkerType::NoMarker:
1031 QSizeF size(fontMetrics.horizontalAdvance(listItemBullet), fontMetrics.height());
1032 qreal xoff = fontMetrics.horizontalAdvance(QLatin1Char(
' '));
1033 if (block.textDirection() == Qt::LeftToRight)
1034 xoff = -xoff - size.width();
1035 setPosition(pos + QPointF(xoff, 0));
1038 layout.setFont(font);
1039 layout.setText(listItemBullet);
1040 layout.beginLayout();
1041 QTextLine line = layout.createLine();
1042 line.setPosition(QPointF(0, 0));
1046 if (charFormat.foreground().style() == Qt::NoBrush)
1051 QList<QGlyphRun> glyphRuns = layout.glyphRuns();
1052 for (
int i=0; i<glyphRuns.size(); ++i)
1053 addUnselectedGlyphs(glyphRuns.at(i));
1057 int textPos = block.position();
1058 QTextBlock::iterator blockIterator = block.begin();
1060 while (!blockIterator.atEnd()) {
1061 QTextFragment fragment = blockIterator.fragment();
1062 QString text = fragment.text();
1066 QTextCharFormat charFormat = fragment.charFormat();
1067 QFont font(charFormat.font());
1068 QFontMetricsF fontMetrics(font);
1070 int fontHeight = fontMetrics.descent() + fontMetrics.ascent();
1071 int valign = charFormat.verticalAlignment();
1072 if (valign == QTextCharFormat::AlignSuperScript)
1073 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() - fontHeight / 2));
1074 else if (valign == QTextCharFormat::AlignSubScript)
1075 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() + fontHeight / 6));
1077 setPosition(blockBoundingRect.topLeft());
1079 if (text.contains(QChar::ObjectReplacementCharacter) && charFormat.objectType() != QTextFormat::NoObject) {
1080 QTextFrame *frame = qobject_cast<QTextFrame *>(textDocument->objectForFormat(charFormat));
1081 if (!frame || frame->frameFormat().position() == QTextFrameFormat::InFlow) {
1082 int blockRelativePosition = textPos - block.position();
1083 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
1084 if (!currentLine().isValid()
1085 || line.lineNumber() != currentLine().lineNumber()) {
1089 QQuickTextNodeEngine::SelectionState selectionState =
1090 (selectionStart < textPos + text.size()
1091 && selectionEnd >= textPos)
1092 ? QQuickTextNodeEngine::Selected
1093 : QQuickTextNodeEngine::Unselected;
1095 addTextObject(block, QPointF(), charFormat, selectionState, textDocument, textPos);
1097 textPos += text.size();
1099 if (charFormat.foreground().style() != Qt::NoBrush)
1101 else if (charFormat.isAnchor())
1106 int fragmentEnd = textPos + fragment.length();
1108 if (preeditPosition >= 0
1109 && (preeditPosition + block.position()) >= textPos
1110 && (preeditPosition + block.position()) <= fragmentEnd) {
1111 fragmentEnd += preeditLength;
1114 if (charFormat.background().style() != Qt::NoBrush || charFormat.hasProperty(QTextFormat::TextUnderlineColor)) {
1115 QTextLayout::FormatRange additionalFormat;
1116 additionalFormat.start = textPos - block.position();
1117 additionalFormat.length = fragmentEnd - textPos;
1118 additionalFormat.format = charFormat;
1119 colorChanges << additionalFormat;
1122 textPos = addText(block, charFormat, textColor, colorChanges, textPos, fragmentEnd,
1123 selectionStart, selectionEnd);
1130 if (preeditLength >= 0 && textPos <= block.position() + preeditPosition) {
1131 setPosition(blockBoundingRect.topLeft());
1132 textPos = block.position() + preeditPosition;
1133 QTextLine line = block.layout()->lineForTextPosition(preeditPosition);
1134 if (!currentLine().isValid()
1135 || line.lineNumber() != currentLine().lineNumber()) {
1136 setCurrentLine(line);
1138 textPos = addText(block, block.charFormat(), textColor, colorChanges,
1139 textPos, textPos + preeditLength,
1140 selectionStart, selectionEnd);
1145 if (block.blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) {
1146 auto ruleLength = qvariant_cast<QTextLength>(block.blockFormat().property(QTextFormat::BlockTrailingHorizontalRulerWidth));
1147 QRectF ruleRect(0, 0, ruleLength.value(blockBoundingRect.width()), 1);
1148 ruleRect.moveCenter(blockBoundingRect.center());
1149 const QColor ruleColor = block.blockFormat().hasProperty(QTextFormat::BackgroundBrush)
1150 ? qvariant_cast<QBrush>(block.blockFormat().property(QTextFormat::BackgroundBrush)).color()
1152 m_lines.append(TextDecoration(QQuickTextNodeEngine::Unselected, ruleRect, ruleColor));
1156 m_hasContents =
true;
void addSelectedGlyphs(const QGlyphRun &glyphRun)
void addUnselectedGlyphs(const QGlyphRun &glyphRun)
void addTextObject(const QTextBlock &block, const QPointF &position, const QTextCharFormat &format, SelectionState selectionState, QTextDocument *textDocument, int pos, QTextFrameFormat::Position layoutPosition=QTextFrameFormat::InFlow)
void mergeProcessedNodes(QList< BinaryTreeNode * > *regularNodes, QList< BinaryTreeNode * > *imageNodes)
void addTextBlock(QTextDocument *, const QTextBlock &, const QPointF &position, const QColor &textColor, const QColor &anchorColor, int selectionStart, int selectionEnd, const QRectF &viewport=QRectF())
void addGlyphsInRange(int rangeStart, int rangeEnd, const QColor &color, const QColor &backgroundColor, const QColor &underlineColor, int selectionStart, int selectionEnd)
void setTextColor(const QColor &textColor)
void setCurrentLine(const QTextLine ¤tLine)
int addText(const QTextBlock &block, const QTextCharFormat &charFormat, const QColor &textColor, const QVarLengthArray< QTextLayout::FormatRange > &colorChanges, int textPos, int fragmentEnd, int selectionStart, int selectionEnd)
void addImage(const QRectF &rect, const QImage &image, qreal ascent, SelectionState selectionState, QTextFrameFormat::Position layoutPosition)
void addBorder(const QRectF &rect, qreal border, QTextFrameFormat::BorderStyle borderStyle, const QBrush &borderBrush)
void addFrameDecorations(QTextDocument *document, QTextFrame *frame)
void addGlyphsForRanges(const QVarLengthArray< QTextLayout::FormatRange > &ranges, int start, int end, int selectionStart, int selectionEnd)
size_t qHash(const QQuickTextNodeEngine::BinaryTreeNodeKey &key, size_t seed=0)
BinaryTreeNodeKey(BinaryTreeNode *node)
BinaryTreeNode(const QGlyphRun &g, SelectionState selState, const QRectF &brect, const Decorations &decs, const QColor &c, const QColor &bc, const QColor &dc, const QPointF &pos, qreal a)