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>
15#include <QtGui/qimageiohandler.h>
17#include <private/qquicktext_p.h>
18#include <private/qtextdocumentlayout_p.h>
19#include <private/qtextimagehandler_p.h>
20#include <private/qrawfont_p.h>
21#include <private/qglyphrun_p.h>
22#include <private/qquickitem_p.h>
23#include <private/qsgdistancefieldglyphnode_p.h>
25#include <QtGui/private/qguiapplication_p.h>
26#include <qpa/qplatformtheme.h>
34 if (style == QTextCharFormat::SpellCheckUnderline) {
35 QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme();
37 style = QTextCharFormat::UnderlineStyle(
38 theme->themeHint(QPlatformTheme::SpellCheckUnderlineStyle).toInt());
40 if (style == QTextCharFormat::SpellCheckUnderline)
41 style = QTextCharFormat::WaveUnderline;
45 case QTextCharFormat::NoUnderline:
46 case QTextCharFormat::SingleUnderline:
47 case QTextCharFormat::DashUnderline:
48 case QTextCharFormat::DotLine:
49 case QTextCharFormat::DashDotLine:
50 case QTextCharFormat::DashDotDotLine:
51 case QTextCharFormat::WaveUnderline:
54 qWarning(
"QQuickTextNodeEngine: unknown underline style %d, falling back to SingleUnderline",
56 return QTextCharFormat::SingleUnderline;
69 SelectionState selState,
71 const Decorations &decs,
72 QTextCharFormat::UnderlineStyle us,
74 const QColor &bc,
const QColor &dc,
75 const QPointF &pos, qreal a)
91 ranges.append(std::make_pair(d->textRangeStart, d->textRangeEnd));
95 Decorations decorations, QTextCharFormat::UnderlineStyle underlineStyle,
const QColor &textColor,
96 const QColor &backgroundColor,
const QColor &decorationColor,
const QPointF &position)
98 QRectF searchRect = glyphRun.boundingRect();
99 searchRect.translate(position);
101 if (qFuzzyIsNull(searchRect.width()) || qFuzzyIsNull(searchRect.height()))
104 if (glyphRun.underline() || underlineStyle != QTextCharFormat::NoUnderline)
110 qreal ascent = glyphRun.rawFont().ascent();
111 insert(binaryTree, BinaryTreeNode(glyphRun,
125 int newIndex = binaryTree->size();
126 binaryTree->append(binaryTreeNode);
132 BinaryTreeNode *node = binaryTree->data() + searchIndex;
133 if (binaryTreeNode.boundingRect.left() < node->boundingRect.left()) {
134 if (node->leftChildIndex < 0) {
135 node->leftChildIndex = newIndex;
138 searchIndex = node->leftChildIndex;
141 if (node->rightChildIndex < 0) {
142 node->rightChildIndex = newIndex;
145 searchIndex = node->rightChildIndex;
152 QVarLengthArray<
int> *sortedIndexes,
int currentIndex)
154 Q_ASSERT(currentIndex < binaryTree.size());
157 if (node->leftChildIndex >= 0)
158 inOrder(binaryTree, sortedIndexes, node->leftChildIndex);
160 sortedIndexes->append(currentIndex);
162 if (node->rightChildIndex >= 0)
163 inOrder(binaryTree, sortedIndexes, node->rightChildIndex);
168 const QTextCharFormat &charFormat,
169 const QColor &textColor,
170 const QVarLengthArray<QTextLayout::FormatRange> &colorChanges,
171 int textPos,
int fragmentEnd,
172 int selectionStart,
int selectionEnd)
174 if (charFormat.foreground().style() != Qt::NoBrush)
179 while (textPos < fragmentEnd) {
180 int blockRelativePosition = textPos - block.position();
181 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
184 if (!currentLine().isValid()
185 || line.lineNumber() != currentLine().lineNumber()) {
189 Q_ASSERT(line.textLength() > 0);
190 int lineEnd = line.textStart() + block.position() + line.textLength();
192 int len = qMin(lineEnd - textPos, fragmentEnd - textPos);
195 int currentStepEnd = textPos + len;
197 addGlyphsForRanges(colorChanges,
198 textPos - block.position(),
199 currentStepEnd - block.position(),
200 selectionStart - block.position(),
201 selectionEnd - block.position());
203 textPos = currentStepEnd;
209 qreal offset, qreal thickness)
211 for (
auto textDecoration : textDecorations) {
212 QRectF &rect = textDecoration.rect;
213 rect.setY(qRound(rect.y() + m_currentLine.ascent() + offset));
214 rect.setHeight(thickness);
216 m_lines.append(textDecoration);
223 if (m_currentLineTree.isEmpty())
232 QVarLengthArray<
int> sortedIndexes;
233 BinaryTreeNode::inOrder(m_currentLineTree, &sortedIndexes);
235 Q_ASSERT(sortedIndexes.size() == m_currentLineTree.size());
237 SelectionState currentSelectionState = Unselected;
241 qreal underlineOffset = 0.0;
242 qreal underlineThickness = 0.0;
244 qreal overlineOffset = 0.0;
245 qreal overlineThickness = 0.0;
247 qreal strikeOutOffset = 0.0;
248 qreal strikeOutThickness = 0.0;
250 QRectF decorationRect = currentRect;
253 QColor lastBackgroundColor;
254 QColor lastDecorationColor;
255 QTextCharFormat::UnderlineStyle lastUnderlineStyle = QTextCharFormat::NoUnderline;
257 QVarLengthArray<TextDecoration, 16> pendingUnderlines;
258 QVarLengthArray<TextDecoration, 16> pendingOverlines;
259 QVarLengthArray<TextDecoration, 16> pendingStrikeOuts;
260 if (!sortedIndexes.isEmpty()) {
261 QQuickDefaultClipNode *currentClipNode = m_hasSelection ?
new QQuickDefaultClipNode(QRectF()) :
nullptr;
262 bool currentClipNodeUsed =
false;
263 for (
int i=0; i<=sortedIndexes.size(); ++i) {
265 if (i < sortedIndexes.size()) {
266 int sortedIndex = sortedIndexes.at(i);
267 Q_ASSERT(sortedIndex < m_currentLineTree.size());
269 node = m_currentLineTree.data() + sortedIndex;
271 currentSelectionState = node->selectionState;
276 decorationRect.setY(m_position.y() + m_currentLine.y());
277 decorationRect.setHeight(m_currentLine.height());
283 if (node !=
nullptr) {
284 const bool continuesDecoration = node->decorations & currentDecorations;
285 if (continuesDecoration || node->boundingRect.left() < decorationRect.right())
286 decorationRect.setRight(node->boundingRect.left());
289 TextDecoration textDecoration(currentSelectionState, decorationRect, lastColor);
290 if (lastDecorationColor.isValid() &&
294 textDecoration.color = lastDecorationColor;
297 textDecoration.underlineStyle = lastUnderlineStyle;
299 if (!pendingUnderlines.isEmpty()) {
300 TextDecoration &last = pendingUnderlines.last();
302 constexpr qreal mergeThreshold = 0.5;
303 if (last.underlineStyle == lastUnderlineStyle
304 && last.color == textDecoration.color
305 && last.selectionState == textDecoration.selectionState
306 && qAbs(last.rect.right() - textDecoration.rect.left()) < mergeThreshold) {
308 last.rect.setRight(textDecoration.rect.right());
310 pendingUnderlines.append(textDecoration);
313 pendingUnderlines.append(textDecoration);
318 pendingOverlines.append(textDecoration);
321 pendingStrikeOuts.append(textDecoration);
323 if (currentDecorations & Decoration::Background)
324 m_backgrounds.append(std::make_pair(decorationRect, lastBackgroundColor));
330 if (node ==
nullptr || node->selectionState != currentSelectionState) {
331 currentRect.setY(m_position.y() + m_currentLine.y());
332 currentRect.setHeight(m_currentLine.height());
334 if (currentSelectionState == Selected)
335 m_selectionRects.append(currentRect);
337 if (currentClipNode !=
nullptr) {
338 if (!currentClipNodeUsed) {
339 delete currentClipNode;
341 currentClipNode->setIsRectangular(
true);
342 currentClipNode->setRect(currentRect);
343 currentClipNode->update();
347 if (node !=
nullptr && m_hasSelection)
348 currentClipNode =
new QQuickDefaultClipNode(QRectF());
350 currentClipNode =
nullptr;
351 currentClipNodeUsed =
false;
353 if (node !=
nullptr) {
354 currentSelectionState = node->selectionState;
355 currentRect = node->boundingRect;
358 if (currentRect.isNull())
359 currentRect.setSize(QSizeF(1, 1));
362 if (currentRect.isNull())
363 currentRect = node->boundingRect;
365 currentRect = currentRect.united(node->boundingRect);
368 if (node !=
nullptr) {
369 if (node->selectionState == Selected) {
370 node->clipNode = currentClipNode;
371 currentClipNodeUsed =
true;
374 decorationRect = node->boundingRect;
378 if (!pendingUnderlines.isEmpty()
380 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
382 pendingUnderlines.clear();
384 underlineOffset = 0.0;
385 underlineThickness = 0.0;
390 if (!pendingOverlines.isEmpty()) {
391 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
393 pendingOverlines.clear();
395 overlineOffset = 0.0;
396 overlineThickness = 0.0;
401 if (!pendingStrikeOuts.isEmpty()) {
402 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
404 pendingStrikeOuts.clear();
406 strikeOutOffset = 0.0;
407 strikeOutThickness = 0.0;
411 QRawFont rawFont = node->glyphRun.rawFont();
413 if (rawFont.lineThickness() > underlineThickness) {
414 underlineThickness = rawFont.lineThickness();
415 underlineOffset = rawFont.underlinePosition();
420 overlineOffset = -rawFont.ascent();
421 overlineThickness = rawFont.lineThickness();
425 strikeOutThickness = rawFont.lineThickness();
426 strikeOutOffset = rawFont.ascent() / -3.0;
429 currentDecorations = node->decorations;
430 lastColor = node->color;
431 lastBackgroundColor = node->backgroundColor;
432 lastDecorationColor = node->decorationColor;
433 lastUnderlineStyle = node->underlineStyle;
434 m_processedNodes.append(*node);
438 if (!pendingUnderlines.isEmpty())
439 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
441 if (!pendingOverlines.isEmpty())
442 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
444 if (!pendingStrikeOuts.isEmpty())
445 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
448 m_currentLineTree.clear();
449 m_currentLine = QTextLine();
450 m_hasSelection =
false;
454 SelectionState selectionState,
455 QTextFrameFormat::Position layoutPosition)
457 QRectF searchRect = rect;
458 if (layoutPosition == QTextFrameFormat::InFlow) {
459 if (m_currentLineTree.isEmpty()) {
460 qreal y = m_currentLine.ascent() - ascent;
461 if (m_currentTextDirection == Qt::RightToLeft)
462 searchRect.moveTopRight(m_position + m_currentLine.rect().topRight() + QPointF(0, y));
464 searchRect.moveTopLeft(m_position + m_currentLine.position() + QPointF(0, y));
466 const BinaryTreeNode *lastNode = m_currentLineTree.data() + m_currentLineTree.size() - 1;
467 if (lastNode->glyphRun.isRightToLeft()) {
468 QPointF lastPos = lastNode->boundingRect.topLeft();
469 searchRect.moveTopRight(lastPos - QPointF(0, ascent - lastNode->ascent));
471 QPointF lastPos = lastNode->boundingRect.topRight();
472 searchRect.moveTopLeft(lastPos - QPointF(0, ascent - lastNode->ascent));
477 BinaryTreeNode::insert(&m_currentLineTree, searchRect, image, ascent, selectionState);
478 m_hasContents =
true;
482 SelectionState selectionState,
483 QTextDocument *textDocument,
int pos,
484 QTextFrameFormat::Position layoutPosition)
486 QTextObjectInterface *handler = textDocument->documentLayout()->handlerForObject(format.objectType());
487 if (handler !=
nullptr) {
489 QSizeF size = handler->intrinsicSize(textDocument, pos, format);
491 if (format.objectType() == QTextFormat::ImageObject) {
492 QTextImageFormat imageFormat = format.toImageFormat();
493 QTextImageHandler *imageHandler =
static_cast<QTextImageHandler *>(handler);
494 image = imageHandler->image(textDocument, imageFormat);
497 if (image.isNull()) {
498 if (QImageIOHandler::allocateImage((size * m_devicePixelRatio).toSize(),
499 QImage::Format_ARGB32_Premultiplied,
501 image.setDevicePixelRatio(m_devicePixelRatio);
502 image.fill(Qt::transparent);
504 QPainter painter(&image);
505 handler->drawObject(&painter, QRectF({}, size), textDocument, pos, format);
514 QTextLine line = block.layout()->lineForTextPosition(pos - block.position());
515 switch (format.verticalAlignment())
517 case QTextCharFormat::AlignTop:
518 ascent = line.ascent();
520 case QTextCharFormat::AlignMiddle:
522 ascent = (line.ascent() + line.descent()) / 2 - line.descent() + size.height() / 2;
524 case QTextCharFormat::AlignBottom:
525 ascent = size.height() - line.descent();
527 case QTextCharFormat::AlignBaseline: {
528 QFontMetricsF m(format.font());
529 ascent = size.height() - m.descent();
533 ascent = size.height();
536 addImage(QRectF(position, size), image, ascent, selectionState, layoutPosition);
542 BinaryTreeNode::insert(&m_currentLineTree,
545 Decoration::NoDecoration,
555 int currentSize = m_currentLineTree.size();
556 BinaryTreeNode::insert(&m_currentLineTree,
559 Decoration::NoDecoration,
565 m_hasSelection = m_hasSelection || m_currentLineTree.size() > currentSize;
570 int selectionStart,
int selectionEnd)
572 int currentPosition = start;
573 int remainingLength = end - start;
574 for (
const QTextLayout::FormatRange &range : ranges) {
575 if (range.start + range.length > currentPosition
576 && range.start < currentPosition + remainingLength) {
578 if (range.start > currentPosition) {
579 addGlyphsInRange(currentPosition, range.start - currentPosition,
580 QTextCharFormat::NoUnderline,
581 QColor(), QColor(), QColor(), selectionStart, selectionEnd);
583 int rangeEnd = qMin(range.start + range.length, currentPosition + remainingLength);
585 if (range.format.hasProperty(QTextFormat::ForegroundBrush))
586 rangeColor = range.format.foreground().color();
587 else if (range.format.isAnchor())
588 rangeColor = m_anchorColor;
589 QColor rangeBackgroundColor = range.format.hasProperty(QTextFormat::BackgroundBrush)
590 ? range.format.background().color()
593 QColor rangeDecorationColor = range.format.hasProperty(QTextFormat::TextUnderlineColor)
594 ? range.format.underlineColor()
597 QTextCharFormat::UnderlineStyle rangeUnderlineStyle = QTextCharFormat::NoUnderline;
598 if (range.format.hasProperty(QTextFormat::TextUnderlineStyle))
599 rangeUnderlineStyle = range.format.underlineStyle();
601 addGlyphsInRange(range.start, rangeEnd - range.start, rangeUnderlineStyle,
602 rangeColor, rangeBackgroundColor,
603 rangeDecorationColor, selectionStart, selectionEnd);
605 currentPosition = range.start + range.length;
606 remainingLength = end - currentPosition;
608 }
else if (range.start > currentPosition + remainingLength || remainingLength <= 0) {
613 if (remainingLength > 0) {
614 addGlyphsInRange(currentPosition, remainingLength, QTextCharFormat::NoUnderline,
615 QColor(), QColor(), QColor(),
616 selectionStart, selectionEnd);
622 QTextCharFormat::UnderlineStyle underlineStyle,
623 const QColor &color,
const QColor &backgroundColor,
const QColor &decorationColor,
624 int selectionStart,
int selectionEnd)
627 if (color.isValid()) {
628 oldColor = m_textColor;
632 QColor oldBackgroundColor = m_backgroundColor;
633 if (backgroundColor.isValid()) {
634 oldBackgroundColor = m_backgroundColor;
635 m_backgroundColor = backgroundColor;
638 QColor oldDecorationColor = m_decorationColor;
639 if (decorationColor.isValid()) {
640 oldDecorationColor = m_decorationColor;
641 m_decorationColor = decorationColor;
644 QTextCharFormat::UnderlineStyle oldUnderlineStyle = m_underlineStyle;
645 m_underlineStyle = underlineStyle;
647 bool hasSelection = selectionEnd >= 0
648 && selectionStart <= selectionEnd;
650 QTextLine &line = m_currentLine;
651 int rangeEnd = rangeStart + rangeLength;
652 if (!hasSelection || (selectionStart > rangeEnd || selectionEnd < rangeStart)) {
653 const QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, rangeLength);
654 for (
const QGlyphRun &glyphRun : glyphRuns)
655 addUnselectedGlyphs(glyphRun);
657 if (rangeStart < selectionStart) {
658 int length = qMin(selectionStart - rangeStart, rangeLength);
659 const QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, length);
660 for (
const QGlyphRun &glyphRun : glyphRuns)
661 addUnselectedGlyphs(glyphRun);
664 if (rangeEnd > selectionStart) {
665 int start = qMax(selectionStart, rangeStart);
666 int length = qMin(selectionEnd - start + 1, rangeEnd - start);
667 const QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
668 for (
const QGlyphRun &glyphRun : glyphRuns)
669 addSelectedGlyphs(glyphRun);
672 if (selectionEnd >= rangeStart && selectionEnd < rangeEnd) {
673 int start = selectionEnd + 1;
674 int length = rangeEnd - selectionEnd - 1;
675 const QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
676 for (
const QGlyphRun &glyphRun : glyphRuns)
677 addUnselectedGlyphs(glyphRun);
681 if (decorationColor.isValid())
682 m_decorationColor = oldDecorationColor;
684 if (backgroundColor.isValid())
685 m_backgroundColor = oldBackgroundColor;
687 if (oldColor.isValid())
688 m_textColor = oldColor;
690 m_underlineStyle = oldUnderlineStyle;
694 QTextFrameFormat::BorderStyle borderStyle,
695 const QBrush &borderBrush)
697 const QColor &color = borderBrush.color();
700 Q_UNUSED(borderStyle);
702 m_backgrounds.append(std::make_pair(QRectF(rect.left(), rect.top(), border, rect.height() + border), color));
703 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.top(), rect.width(), border), color));
704 m_backgrounds.append(std::make_pair(QRectF(rect.right(), rect.top() + border, border, rect.height() - border), color));
705 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.bottom(), rect.width(), border), color));
710 QTextDocumentLayout *documentLayout = qobject_cast<QTextDocumentLayout *>(document->documentLayout());
711 if (Q_UNLIKELY(!documentLayout))
714 QTextFrameFormat frameFormat = frame->format().toFrameFormat();
715 QTextTable *table = qobject_cast<QTextTable *>(frame);
717 QRectF boundingRect = table ==
nullptr
718 ? documentLayout->frameBoundingRect(frame)
719 : documentLayout->tableBoundingRect(table);
721 QBrush bg = frame->frameFormat().background();
722 if (bg.style() != Qt::NoBrush)
723 m_backgrounds.append(std::make_pair(boundingRect, bg.color()));
725 if (!frameFormat.hasProperty(QTextFormat::FrameBorder))
728 qreal borderWidth = frameFormat.border();
729 if (qFuzzyIsNull(borderWidth))
732 QBrush borderBrush = frameFormat.borderBrush();
733 QTextFrameFormat::BorderStyle borderStyle = frameFormat.borderStyle();
734 if (borderStyle == QTextFrameFormat::BorderStyle_None)
737 const auto collapsed = table->format().borderCollapse();
740 addBorder(boundingRect.adjusted(frameFormat.leftMargin(), frameFormat.topMargin(),
741 -frameFormat.rightMargin() - borderWidth,
742 -frameFormat.bottomMargin() - borderWidth),
743 borderWidth, borderStyle, borderBrush);
745 if (table !=
nullptr) {
746 int rows = table->rows();
747 int columns = table->columns();
749 for (
int row=0; row<rows; ++row) {
750 for (
int column=0; column<columns; ++column) {
751 QTextTableCell cell = table->cellAt(row, column);
753 QRectF cellRect = documentLayout->tableCellBoundingRect(table, cell);
754 addBorder(cellRect.adjusted(-borderWidth, -borderWidth, collapsed ? -borderWidth : 0, collapsed ? -borderWidth : 0), borderWidth,
755 borderStyle, borderBrush);
763 return qHashMulti(seed, key.fontEngine, key.clipNode, key.color, key
.selectionState);
767 QVarLengthArray<BinaryTreeNode *, 8> *imageNodes)
769 QHash<BinaryTreeNodeKey, QVarLengthArray<BinaryTreeNode *, 2> > map;
771 for (
int i = 0; i < m_processedNodes.size(); ++i) {
774 if (node->image.isNull()) {
775 if (node->glyphRun.isEmpty())
780 QVarLengthArray<BinaryTreeNode *, 2> &nodes = map[key];
782 regularNodes->append(node);
786 imageNodes->append(node);
790 for (
int i = 0; i < regularNodes->size(); ++i) {
794 const auto it = map.constFind(key);
795 Q_ASSERT(it != map.constEnd());
796 const QVarLengthArray<BinaryTreeNode *, 2> &nodes = it.value();
797 Q_ASSERT(nodes.first() == primaryNode);
800 for (
int j = 0; j < nodes.size(); ++j)
801 count += nodes.at(j)->glyphRun.glyphIndexes().size();
803 if (count != primaryNode->glyphRun.glyphIndexes().size()) {
804 QGlyphRun &glyphRun = primaryNode->glyphRun;
805 QList<quint32> glyphIndexes = glyphRun.glyphIndexes();
806 glyphIndexes.reserve(count);
808 QList<QPointF> glyphPositions = glyphRun.positions();
809 glyphPositions.reserve(count);
811 QRectF glyphBoundingRect = glyphRun.boundingRect();
813 for (
int j = 1; j < nodes.size(); ++j) {
815 glyphIndexes += otherNode->glyphRun.glyphIndexes();
816 primaryNode->ranges += otherNode->ranges;
817 glyphBoundingRect = glyphBoundingRect.united(otherNode->boundingRect);
819 QList<QPointF> otherPositions = otherNode->glyphRun.positions();
820 for (
int k = 0; k < otherPositions.size(); ++k)
821 glyphPositions += otherPositions.at(k) + (otherNode->position - primaryNode->position);
824 Q_ASSERT(glyphPositions.size() == count);
825 Q_ASSERT(glyphIndexes.size() == count);
827 glyphRun.setGlyphIndexes(glyphIndexes);
828 glyphRun.setPositions(glyphPositions);
829 glyphRun.setBoundingRect(glyphBoundingRect);
835 QSGInternalTextNode::RecycleBin *recycleBin,
836 QQuickText::TextStyle style,
837 const QColor &styleColor)
839 if (m_currentLine.isValid())
840 processCurrentLine();
842 QVarLengthArray<BinaryTreeNode *, 8> nodes;
843 QVarLengthArray<BinaryTreeNode *, 8> imageNodes;
844 mergeProcessedNodes(&nodes, &imageNodes);
846 for (
int i = 0; i < m_backgrounds.size(); ++i) {
847 const QRectF &rect = m_backgrounds.at(i).first;
848 const QColor &color = m_backgrounds.at(i).second;
849 if (color.alpha() != 0)
850 parentNode->addRectangleNode(rect, color, recycleBin);
854 for (
int i = 0; i < nodes.size(); ++i) {
855 const BinaryTreeNode *node = nodes.at(i);
856 parentNode->addGlyphs(node->position, node->glyphRun, node->color, recycleBin, style, styleColor,
nullptr);
859 for (
int i = 0; i < imageNodes.size(); ++i) {
860 const BinaryTreeNode *node = imageNodes.at(i);
861 if (node->selectionState == Unselected)
862 parentNode->addImage(node->boundingRect, node->image, recycleBin);
865 for (
int i = 0; i < m_selectionRects.size(); ++i) {
866 const QRectF &rect = m_selectionRects.at(i);
867 if (m_selectionColor.alpha() != 0)
868 parentNode->addRectangleNode(rect, m_selectionColor, recycleBin);
872 for (
int i = 0; i < m_lines.size(); ++i) {
873 const TextDecoration &textDecoration = m_lines.at(i);
875 QColor color = textDecoration.selectionState == Selected
876 ? m_selectedTextColor
877 : textDecoration.color;
879 parentNode->addDecorationNode(textDecoration.rect,
881 resolveUnderlineStyle(textDecoration.underlineStyle),
886 for (
int i = 0; i < nodes.size(); ++i) {
887 const BinaryTreeNode *node = nodes.at(i);
888 QQuickDefaultClipNode *clipNode = node->clipNode;
889 if (clipNode !=
nullptr && clipNode->parent() ==
nullptr)
890 parentNode->appendChildNode(clipNode);
892 if (node->selectionState == Selected) {
893 QColor color = m_selectedTextColor;
894 int previousNodeIndex = i - 1;
895 int nextNodeIndex = i + 1;
896 const BinaryTreeNode *previousNode = previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
897 while (previousNode !=
nullptr && qFuzzyCompare(previousNode->boundingRect.left(), node->boundingRect.left()))
898 previousNode = --previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
900 const BinaryTreeNode *nextNode = nextNodeIndex == nodes.size() ? 0 : nodes.at(nextNodeIndex);
902 if (previousNode !=
nullptr && previousNode->selectionState == Unselected)
903 parentNode->addGlyphs(previousNode->position, previousNode->glyphRun, color, recycleBin, style, styleColor, clipNode);
905 if (nextNode !=
nullptr && nextNode->selectionState == Unselected)
906 parentNode->addGlyphs(nextNode->position, nextNode->glyphRun, color, recycleBin, style, styleColor, clipNode);
910 bool drawCurrent =
false;
911 if (previousNode !=
nullptr || nextNode !=
nullptr) {
917 auto subtractOverlap = [](
int rangeLength,
const std::pair<
int,
int> &range,
918 const BinaryTreeNode *otherNode,
const QPointF &nodePos) {
919 if (otherNode->position != nodePos)
921 for (
int j = 0; j < otherNode->ranges.size(); ++j) {
922 const std::pair<
int,
int> &otherRange = otherNode->ranges.at(j);
923 if (range.first < otherRange.second && range.second > otherRange.first) {
924 int start = qMax(range.first, otherRange.first);
925 int end = qMin(range.second, otherRange.second);
926 rangeLength -= end - start + 1;
927 if (rangeLength == 0)
934 for (
int i = 0; i < node->ranges.size(); ++i) {
935 const std::pair<
int,
int> &range = node->ranges.at(i);
937 int rangeLength = range.second - range.first + 1;
938 if (previousNode !=
nullptr)
939 rangeLength = subtractOverlap(rangeLength, range, previousNode, node->position);
940 if (nextNode !=
nullptr && rangeLength > 0)
941 rangeLength = subtractOverlap(rangeLength, range, nextNode, node->position);
943 if (rangeLength > 0) {
953 parentNode->addGlyphs(node->position, node->glyphRun, color, recycleBin, style, styleColor, clipNode);
957 for (
int i = 0; i < imageNodes.size(); ++i) {
958 const BinaryTreeNode *node = imageNodes.at(i);
959 if (node->selectionState == Selected) {
960 parentNode->addImage(node->boundingRect, node->image, recycleBin);
961 if (node->selectionState == Selected) {
962 QColor color = m_selectionColor;
964 parentNode->addRectangleNode(node->boundingRect, color, recycleBin);
970void QQuickTextNodeEngine::mergeFormats(QTextLayout *textLayout, QVarLengthArray<QTextLayout::FormatRange> *mergedFormats)
972 Q_ASSERT(mergedFormats !=
nullptr);
973 if (textLayout ==
nullptr)
976 const QList<QTextLayout::FormatRange> additionalFormats = textLayout->formats();
977 for (QTextLayout::FormatRange additionalFormat : additionalFormats) {
978 if (additionalFormat.format.hasProperty(QTextFormat::ForegroundBrush)
979 || additionalFormat.format.hasProperty(QTextFormat::BackgroundBrush)
980 || additionalFormat.format.hasProperty(QTextFormat::TextUnderlineStyle)
981 || additionalFormat.format.hasProperty(QTextFormat::TextUnderlineColor)
982 || additionalFormat.format.isAnchor()) {
984 if (!mergedFormats->isEmpty()) {
985 QTextLayout::FormatRange *lastFormat = mergedFormats->data() + mergedFormats->size() - 1;
987 if (additionalFormat.start < lastFormat->start + lastFormat->length) {
988 QTextLayout::FormatRange *mergedRange =
nullptr;
990 int length = additionalFormat.length;
991 if (additionalFormat.start > lastFormat->start) {
992 lastFormat->length = additionalFormat.start - lastFormat->start;
993 length -= lastFormat->length;
995 mergedFormats->append(QTextLayout::FormatRange());
996 mergedRange = mergedFormats->data() + mergedFormats->size() - 1;
997 lastFormat = mergedFormats->data() + mergedFormats->size() - 2;
999 mergedRange = lastFormat;
1002 mergedRange->format = lastFormat->format;
1003 mergedRange->format.merge(additionalFormat.format);
1004 mergedRange->start = additionalFormat.start;
1006 int end = qMin(additionalFormat.start + additionalFormat.length,
1007 lastFormat->start + lastFormat->length);
1009 mergedRange->length = end - mergedRange->start;
1010 length -= mergedRange->length;
1012 additionalFormat.start = end;
1013 additionalFormat.length = length;
1017 if (additionalFormat.length > 0)
1018 mergedFormats->append(additionalFormat);
1024
1025
1026
1027
1028
1029
1030
1031
1033 const QColor &textColor,
const QColor &anchorColor,
int selectionStart,
int selectionEnd,
const QRectF &viewport)
1035 Q_ASSERT(textDocument);
1037 int preeditLength = block.isValid() ? block.layout()->preeditAreaText().size() : 0;
1038 int preeditPosition = block.isValid() ? block.layout()->preeditAreaPosition() : -1;
1041 setCurrentTextDirection(block.textDirection());
1043 QVarLengthArray<QTextLayout::FormatRange> colorChanges;
1044 mergeFormats(block.layout(), &colorChanges);
1046 const QTextCharFormat charFormat = block.charFormat();
1047 const QRectF blockBoundingRect = textDocument->documentLayout()->blockBoundingRect(block).translated(position);
1048 if (viewport.isValid()) {
1049 if (!blockBoundingRect.intersects(viewport))
1051 qCDebug(lcSgText) <<
"adding block with length" << block.length() <<
':' << blockBoundingRect <<
"in viewport" << viewport;
1054 if (charFormat.background().style() != Qt::NoBrush)
1055 m_backgrounds.append(std::make_pair(blockBoundingRect, charFormat.background().color()));
1057 if (QTextList *textList = block.textList()) {
1058 QPointF pos = blockBoundingRect.topLeft();
1059 QTextLayout *layout = block.layout();
1060 if (layout->lineCount() > 0) {
1061 QTextLine firstLine = layout->lineAt(0);
1062 Q_ASSERT(firstLine.isValid());
1066 QRectF textRect = firstLine.naturalTextRect();
1067 pos += textRect.topLeft();
1068 if (block.textDirection() == Qt::RightToLeft)
1069 pos.rx() += textRect.width();
1071 QFont font(charFormat.font());
1072 QFontMetricsF fontMetrics(font);
1073 QTextListFormat listFormat = textList->format();
1075 QString listItemBullet;
1076 switch (listFormat.style()) {
1077 case QTextListFormat::ListCircle:
1078 listItemBullet = QChar(0x25E6);
1080 case QTextListFormat::ListSquare:
1081 listItemBullet = QChar(0x25AA);
1083 case QTextListFormat::ListDecimal:
1084 case QTextListFormat::ListLowerAlpha:
1085 case QTextListFormat::ListUpperAlpha:
1086 case QTextListFormat::ListLowerRoman:
1087 case QTextListFormat::ListUpperRoman:
1088 listItemBullet = textList->itemText(block);
1091 listItemBullet = QChar(0x2022);
1095 switch (block.blockFormat().marker()) {
1096 case QTextBlockFormat::MarkerType::Checked:
1097 listItemBullet = QChar(0x2612);
1099 case QTextBlockFormat::MarkerType::Unchecked:
1100 listItemBullet = QChar(0x2610);
1102 case QTextBlockFormat::MarkerType::NoMarker:
1106 QSizeF size(fontMetrics.horizontalAdvance(listItemBullet), fontMetrics.height());
1107 qreal xoff = fontMetrics.horizontalAdvance(QLatin1Char(
' '));
1108 if (block.textDirection() == Qt::LeftToRight)
1109 xoff = -xoff - size.width();
1112 layout.setFont(font);
1113 layout.setText(listItemBullet);
1114 layout.beginLayout();
1115 QTextLine line = layout.createLine();
1116 line.setPosition(QPointF(0, 0));
1121 const qreal baselineOffset = firstLine.ascent() - line.ascent();
1122 setPosition(pos + QPointF(xoff, baselineOffset));
1125 if (charFormat.foreground().style() == Qt::NoBrush)
1130 const QList<QGlyphRun> glyphRuns = layout.glyphRuns();
1131 for (
const auto &e : glyphRuns)
1132 addUnselectedGlyphs(e);
1136 int textPos = block.position();
1137 QTextBlock::iterator blockIterator = block.begin();
1139 while (!blockIterator.atEnd()) {
1140 QTextFragment fragment = blockIterator.fragment();
1141 QString text = fragment.text();
1145 QTextCharFormat charFormat = fragment.charFormat();
1146 QFont font(charFormat.font());
1147 QFontMetricsF fontMetrics(font);
1149 int fontHeight = fontMetrics.descent() + fontMetrics.ascent();
1150 int valign = charFormat.verticalAlignment();
1151 if (valign == QTextCharFormat::AlignSuperScript)
1152 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() - fontHeight / 2));
1153 else if (valign == QTextCharFormat::AlignSubScript)
1154 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() + fontHeight / 6));
1156 setPosition(blockBoundingRect.topLeft());
1158 if (text.contains(QChar::ObjectReplacementCharacter) && charFormat.objectType() != QTextFormat::NoObject) {
1159 QTextFrame *frame = qobject_cast<QTextFrame *>(textDocument->objectForFormat(charFormat));
1160 if (!frame || frame->frameFormat().position() == QTextFrameFormat::InFlow) {
1161 int blockRelativePosition = textPos - block.position();
1162 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
1163 if (!currentLine().isValid()
1164 || line.lineNumber() != currentLine().lineNumber()) {
1168 QQuickTextNodeEngine::SelectionState selectionState =
1169 (selectionStart < textPos + text.size()
1170 && selectionEnd >= textPos)
1171 ? QQuickTextNodeEngine::Selected
1172 : QQuickTextNodeEngine::Unselected;
1174 addTextObject(block, QPointF(), charFormat, selectionState, textDocument, textPos);
1176 textPos += text.size();
1178 if (charFormat.foreground().style() != Qt::NoBrush)
1180 else if (charFormat.isAnchor())
1185 int fragmentEnd = textPos + fragment.length();
1187 if (preeditPosition >= 0
1188 && (preeditPosition + block.position()) >= textPos
1189 && (preeditPosition + block.position()) <= fragmentEnd) {
1190 fragmentEnd += preeditLength;
1193 if (charFormat.background().style() != Qt::NoBrush
1194 || charFormat.hasProperty(QTextFormat::TextUnderlineColor)
1195 || charFormat.hasProperty(QTextFormat::TextUnderlineStyle)) {
1196 QTextLayout::FormatRange additionalFormat;
1197 additionalFormat.start = textPos - block.position();
1198 additionalFormat.length = fragmentEnd - textPos;
1199 additionalFormat.format = charFormat;
1200 colorChanges << additionalFormat;
1203 textPos = addText(block, charFormat, textColor, colorChanges, textPos, fragmentEnd,
1204 selectionStart, selectionEnd);
1211 if (preeditLength >= 0 && textPos <= block.position() + preeditPosition) {
1212 setPosition(blockBoundingRect.topLeft());
1213 textPos = block.position() + preeditPosition;
1214 QTextLine line = block.layout()->lineForTextPosition(preeditPosition);
1215 if (!currentLine().isValid()
1216 || line.lineNumber() != currentLine().lineNumber()) {
1217 setCurrentLine(line);
1219 textPos = addText(block, block.charFormat(), textColor, colorChanges,
1220 textPos, textPos + preeditLength,
1221 selectionStart, selectionEnd);
1226 if (block.blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) {
1227 auto ruleLength = qvariant_cast<QTextLength>(block.blockFormat().property(QTextFormat::BlockTrailingHorizontalRulerWidth));
1228 QRectF ruleRect(0, 0, ruleLength.value(blockBoundingRect.width()), 1);
1229 ruleRect.moveCenter(blockBoundingRect.center());
1230 const QColor ruleColor = block.blockFormat().hasProperty(QTextFormat::BackgroundBrush)
1231 ? qvariant_cast<QBrush>(block.blockFormat().property(QTextFormat::BackgroundBrush)).color()
1233 m_lines.append(TextDecoration(QQuickTextNodeEngine::Unselected, ruleRect, ruleColor));
1237 m_hasContents =
true;
void addSelectedGlyphs(const QGlyphRun &glyphRun)
void addUnselectedGlyphs(const QGlyphRun &glyphRun)
void mergeProcessedNodes(QVarLengthArray< BinaryTreeNode *, 8 > *regularNodes, QVarLengthArray< BinaryTreeNode *, 8 > *imageNodes)
void addTextObject(const QTextBlock &block, const QPointF &position, const QTextCharFormat &format, SelectionState selectionState, QTextDocument *textDocument, int pos, QTextFrameFormat::Position layoutPosition=QTextFrameFormat::InFlow)
void addTextBlock(QTextDocument *, const QTextBlock &, const QPointF &position, const QColor &textColor, const QColor &anchorColor, int selectionStart, int selectionEnd, const QRectF &viewport=QRectF())
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 addGlyphsInRange(int rangeStart, int rangeEnd, QTextCharFormat::UnderlineStyle underlineStyle, const QColor &color, const QColor &backgroundColor, const QColor &underlineColor, int selectionStart, int selectionEnd)
void addFrameDecorations(QTextDocument *document, QTextFrame *frame)
void addGlyphsForRanges(const QVarLengthArray< QTextLayout::FormatRange > &ranges, int start, int end, int selectionStart, int selectionEnd)
Combined button and popup list for selecting options.
size_t qHash(const QQuickTextNodeEngine::BinaryTreeNodeKey &key, size_t seed=0)
static QT_BEGIN_NAMESPACE QTextCharFormat::UnderlineStyle resolveUnderlineStyle(QTextCharFormat::UnderlineStyle style)
BinaryTreeNodeKey(BinaryTreeNode *node)
BinaryTreeNode(const QGlyphRun &g, SelectionState selState, const QRectF &brect, const Decorations &decs, QTextCharFormat::UnderlineStyle us, const QColor &c, const QColor &bc, const QColor &dc, const QPointF &pos, qreal a)