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>
28 : fontEngine(QRawFontPrivate::get(node->glyphRun.rawFont())->fontEngine)
29 , clipNode(node->clipNode)
30 , color(node->color.rgba())
31 , selectionState(node->selectionState)
36 SelectionState selState,
38 const Decorations &decs,
40 const QColor &bc,
const QColor &dc,
41 const QPointF &pos, qreal a)
56 ranges.append(std::make_pair(d->textRangeStart, d->textRangeEnd));
61 Decorations decorations,
const QColor &textColor,
62 const QColor &backgroundColor,
const QColor &decorationColor,
const QPointF &position)
64 QRectF searchRect = glyphRun.boundingRect();
65 searchRect.translate(position);
67 if (qFuzzyIsNull(searchRect.width()) || qFuzzyIsNull(searchRect.height()))
75 qreal ascent = glyphRun.rawFont().ascent();
76 insert(binaryTree, BinaryTreeNode(glyphRun,
89 int newIndex = binaryTree->size();
90 binaryTree->append(binaryTreeNode);
96 BinaryTreeNode *node = binaryTree->data() + searchIndex;
97 if (binaryTreeNode.boundingRect.left() < node->boundingRect.left()) {
98 if (node->leftChildIndex < 0) {
99 node->leftChildIndex = newIndex;
102 searchIndex = node->leftChildIndex;
105 if (node->rightChildIndex < 0) {
106 node->rightChildIndex = newIndex;
109 searchIndex = node->rightChildIndex;
116 QVarLengthArray<
int> *sortedIndexes,
int currentIndex)
118 Q_ASSERT(currentIndex < binaryTree.size());
121 if (node->leftChildIndex >= 0)
122 inOrder(binaryTree, sortedIndexes, node->leftChildIndex);
124 sortedIndexes->append(currentIndex);
126 if (node->rightChildIndex >= 0)
127 inOrder(binaryTree, sortedIndexes, node->rightChildIndex);
132 const QTextCharFormat &charFormat,
133 const QColor &textColor,
134 const QVarLengthArray<QTextLayout::FormatRange> &colorChanges,
135 int textPos,
int fragmentEnd,
136 int selectionStart,
int selectionEnd)
138 if (charFormat.foreground().style() != Qt::NoBrush)
143 while (textPos < fragmentEnd) {
144 int blockRelativePosition = textPos - block.position();
145 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
148 if (!currentLine().isValid()
149 || line.lineNumber() != currentLine().lineNumber()) {
153 Q_ASSERT(line.textLength() > 0);
154 int lineEnd = line.textStart() + block.position() + line.textLength();
156 int len = qMin(lineEnd - textPos, fragmentEnd - textPos);
159 int currentStepEnd = textPos + len;
161 addGlyphsForRanges(colorChanges,
162 textPos - block.position(),
163 currentStepEnd - block.position(),
164 selectionStart - block.position(),
165 selectionEnd - block.position());
167 textPos = currentStepEnd;
172void QQuickTextNodeEngine::addTextDecorations(
const QVarLengthArray<TextDecoration> &textDecorations,
173 qreal offset, qreal thickness)
175 for (
auto textDecoration : textDecorations) {
176 QRectF &rect = textDecoration.rect;
177 rect.setY(qRound(rect.y() + m_currentLine.ascent() + offset));
178 rect.setHeight(thickness);
180 m_lines.append(textDecoration);
187 if (m_currentLineTree.isEmpty())
196 QVarLengthArray<
int> sortedIndexes;
197 BinaryTreeNode::inOrder(m_currentLineTree, &sortedIndexes);
199 Q_ASSERT(sortedIndexes.size() == m_currentLineTree.size());
201 SelectionState currentSelectionState = Unselected;
205 qreal underlineOffset = 0.0;
206 qreal underlineThickness = 0.0;
208 qreal overlineOffset = 0.0;
209 qreal overlineThickness = 0.0;
211 qreal strikeOutOffset = 0.0;
212 qreal strikeOutThickness = 0.0;
214 QRectF decorationRect = currentRect;
217 QColor lastBackgroundColor;
218 QColor lastDecorationColor;
220 QVarLengthArray<TextDecoration> pendingUnderlines;
221 QVarLengthArray<TextDecoration> pendingOverlines;
222 QVarLengthArray<TextDecoration> pendingStrikeOuts;
223 if (!sortedIndexes.isEmpty()) {
224 QQuickDefaultClipNode *currentClipNode = m_hasSelection ?
new QQuickDefaultClipNode(QRectF()) :
nullptr;
225 bool currentClipNodeUsed =
false;
226 for (
int i=0; i<=sortedIndexes.size(); ++i) {
228 if (i < sortedIndexes.size()) {
229 int sortedIndex = sortedIndexes.at(i);
230 Q_ASSERT(sortedIndex < m_currentLineTree.size());
232 node = m_currentLineTree.data() + sortedIndex;
234 currentSelectionState = node->selectionState;
239 decorationRect.setY(m_position.y() + m_currentLine.y());
240 decorationRect.setHeight(m_currentLine.height());
243 decorationRect.setRight(node->boundingRect.left());
245 TextDecoration textDecoration(currentSelectionState, decorationRect, lastColor);
246 if (lastDecorationColor.isValid() &&
250 textDecoration.color = lastDecorationColor;
253 pendingUnderlines.append(textDecoration);
256 pendingOverlines.append(textDecoration);
259 pendingStrikeOuts.append(textDecoration);
261 if (currentDecorations & Decoration::Background)
262 m_backgrounds.append(std::make_pair(decorationRect, lastBackgroundColor));
268 if (node ==
nullptr || node->selectionState != currentSelectionState) {
269 currentRect.setY(m_position.y() + m_currentLine.y());
270 currentRect.setHeight(m_currentLine.height());
272 if (currentSelectionState == Selected)
273 m_selectionRects.append(currentRect);
275 if (currentClipNode !=
nullptr) {
276 if (!currentClipNodeUsed) {
277 delete currentClipNode;
279 currentClipNode->setIsRectangular(
true);
280 currentClipNode->setRect(currentRect);
281 currentClipNode->update();
285 if (node !=
nullptr && m_hasSelection)
286 currentClipNode =
new QQuickDefaultClipNode(QRectF());
288 currentClipNode =
nullptr;
289 currentClipNodeUsed =
false;
291 if (node !=
nullptr) {
292 currentSelectionState = node->selectionState;
293 currentRect = node->boundingRect;
296 if (currentRect.isNull())
297 currentRect.setSize(QSizeF(1, 1));
300 if (currentRect.isNull())
301 currentRect = node->boundingRect;
303 currentRect = currentRect.united(node->boundingRect);
306 if (node !=
nullptr) {
307 if (node->selectionState == Selected) {
308 node->clipNode = currentClipNode;
309 currentClipNodeUsed =
true;
312 decorationRect = node->boundingRect;
316 if (!pendingUnderlines.isEmpty()
318 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
320 pendingUnderlines.clear();
322 underlineOffset = 0.0;
323 underlineThickness = 0.0;
328 if (!pendingOverlines.isEmpty()) {
329 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
331 pendingOverlines.clear();
333 overlineOffset = 0.0;
334 overlineThickness = 0.0;
339 if (!pendingStrikeOuts.isEmpty()) {
340 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
342 pendingStrikeOuts.clear();
344 strikeOutOffset = 0.0;
345 strikeOutThickness = 0.0;
349 QRawFont rawFont = node->glyphRun.rawFont();
351 if (rawFont.lineThickness() > underlineThickness) {
352 underlineThickness = rawFont.lineThickness();
353 underlineOffset = rawFont.underlinePosition();
358 overlineOffset = -rawFont.ascent();
359 overlineThickness = rawFont.lineThickness();
363 strikeOutThickness = rawFont.lineThickness();
364 strikeOutOffset = rawFont.ascent() / -3.0;
367 currentDecorations = node->decorations;
368 lastColor = node->color;
369 lastBackgroundColor = node->backgroundColor;
370 lastDecorationColor = node->decorationColor;
371 m_processedNodes.append(*node);
375 if (!pendingUnderlines.isEmpty())
376 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
378 if (!pendingOverlines.isEmpty())
379 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
381 if (!pendingStrikeOuts.isEmpty())
382 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
385 m_currentLineTree.clear();
386 m_currentLine = QTextLine();
387 m_hasSelection =
false;
391 SelectionState selectionState,
392 QTextFrameFormat::Position layoutPosition)
394 QRectF searchRect = rect;
395 if (layoutPosition == QTextFrameFormat::InFlow) {
396 if (m_currentLineTree.isEmpty()) {
397 qreal y = m_currentLine.ascent() - ascent;
398 if (m_currentTextDirection == Qt::RightToLeft)
399 searchRect.moveTopRight(m_position + m_currentLine.rect().topRight() + QPointF(0, y));
401 searchRect.moveTopLeft(m_position + m_currentLine.position() + QPointF(0, y));
403 const BinaryTreeNode *lastNode = m_currentLineTree.data() + m_currentLineTree.size() - 1;
404 if (lastNode->glyphRun.isRightToLeft()) {
405 QPointF lastPos = lastNode->boundingRect.topLeft();
406 searchRect.moveTopRight(lastPos - QPointF(0, ascent - lastNode->ascent));
408 QPointF lastPos = lastNode->boundingRect.topRight();
409 searchRect.moveTopLeft(lastPos - QPointF(0, ascent - lastNode->ascent));
414 BinaryTreeNode::insert(&m_currentLineTree, searchRect, image, ascent, selectionState);
415 m_hasContents =
true;
419 SelectionState selectionState,
420 QTextDocument *textDocument,
int pos,
421 QTextFrameFormat::Position layoutPosition)
423 QTextObjectInterface *handler = textDocument->documentLayout()->handlerForObject(format.objectType());
424 if (handler !=
nullptr) {
426 QSizeF size = handler->intrinsicSize(textDocument, pos, format);
428 if (format.objectType() == QTextFormat::ImageObject) {
429 QTextImageFormat imageFormat = format.toImageFormat();
430 QTextImageHandler *imageHandler =
static_cast<QTextImageHandler *>(handler);
431 image = imageHandler->image(textDocument, imageFormat);
434 if (image.isNull()) {
435 if (QImageIOHandler::allocateImage((size * m_devicePixelRatio).toSize(),
436 QImage::Format_ARGB32_Premultiplied,
438 image.setDevicePixelRatio(m_devicePixelRatio);
439 image.fill(Qt::transparent);
441 QPainter painter(&image);
442 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 (
const QTextLayout::FormatRange &range : ranges) {
506 if (range.start + range.length > currentPosition
507 && range.start < currentPosition + remainingLength) {
509 if (range.start > currentPosition) {
510 addGlyphsInRange(currentPosition, range.start - currentPosition,
511 QColor(), QColor(), QColor(), selectionStart, selectionEnd);
513 int rangeEnd = qMin(range.start + range.length, currentPosition + remainingLength);
515 if (range.format.hasProperty(QTextFormat::ForegroundBrush))
516 rangeColor = range.format.foreground().color();
517 else if (range.format.isAnchor())
518 rangeColor = m_anchorColor;
519 QColor rangeBackgroundColor = range.format.hasProperty(QTextFormat::BackgroundBrush)
520 ? range.format.background().color()
523 QColor rangeDecorationColor = range.format.hasProperty(QTextFormat::TextUnderlineColor)
524 ? range.format.underlineColor()
527 addGlyphsInRange(range.start, rangeEnd - range.start,
528 rangeColor, rangeBackgroundColor, rangeDecorationColor,
529 selectionStart, selectionEnd);
531 currentPosition = range.start + range.length;
532 remainingLength = end - currentPosition;
534 }
else if (range.start > currentPosition + remainingLength || remainingLength <= 0) {
539 if (remainingLength > 0) {
541 selectionStart
, selectionEnd
);
547 const QColor &color,
const QColor &backgroundColor,
const QColor &decorationColor,
548 int selectionStart,
int selectionEnd)
551 if (color.isValid()) {
552 oldColor = m_textColor;
556 QColor oldBackgroundColor = m_backgroundColor;
557 if (backgroundColor.isValid()) {
558 oldBackgroundColor = m_backgroundColor;
559 m_backgroundColor = backgroundColor;
562 QColor oldDecorationColor = m_decorationColor;
563 if (decorationColor.isValid()) {
564 oldDecorationColor = m_decorationColor;
565 m_decorationColor = decorationColor;
568 bool hasSelection = selectionEnd >= 0
569 && selectionStart <= selectionEnd;
571 QTextLine &line = m_currentLine;
572 int rangeEnd = rangeStart + rangeLength;
573 if (!hasSelection || (selectionStart > rangeEnd || selectionEnd < rangeStart)) {
574 const QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, rangeLength);
575 for (
const QGlyphRun &glyphRun : glyphRuns)
576 addUnselectedGlyphs(glyphRun);
578 if (rangeStart < selectionStart) {
579 int length = qMin(selectionStart - rangeStart, rangeLength);
580 const QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, length);
581 for (
const QGlyphRun &glyphRun : glyphRuns)
582 addUnselectedGlyphs(glyphRun);
585 if (rangeEnd > selectionStart) {
586 int start = qMax(selectionStart, rangeStart);
587 int length = qMin(selectionEnd - start + 1, rangeEnd - start);
588 const QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
589 for (
const QGlyphRun &glyphRun : glyphRuns)
590 addSelectedGlyphs(glyphRun);
593 if (selectionEnd >= rangeStart && selectionEnd < rangeEnd) {
594 int start = selectionEnd + 1;
595 int length = rangeEnd - selectionEnd - 1;
596 const QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
597 for (
const QGlyphRun &glyphRun : glyphRuns)
598 addUnselectedGlyphs(glyphRun);
602 if (decorationColor.isValid())
603 m_decorationColor = oldDecorationColor;
605 if (backgroundColor.isValid())
606 m_backgroundColor = oldBackgroundColor;
608 if (oldColor.isValid())
609 m_textColor = oldColor;
613 QTextFrameFormat::BorderStyle borderStyle,
614 const QBrush &borderBrush)
616 const QColor &color = borderBrush.color();
619 Q_UNUSED(borderStyle);
621 m_backgrounds.append(std::make_pair(QRectF(rect.left(), rect.top(), border, rect.height() + border), color));
622 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.top(), rect.width(), border), color));
623 m_backgrounds.append(std::make_pair(QRectF(rect.right(), rect.top() + border, border, rect.height() - border), color));
624 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.bottom(), rect.width(), border), color));
629 QTextDocumentLayout *documentLayout = qobject_cast<QTextDocumentLayout *>(document->documentLayout());
630 if (Q_UNLIKELY(!documentLayout))
633 QTextFrameFormat frameFormat = frame->format().toFrameFormat();
634 QTextTable *table = qobject_cast<QTextTable *>(frame);
636 QRectF boundingRect = table ==
nullptr
637 ? documentLayout->frameBoundingRect(frame)
638 : documentLayout->tableBoundingRect(table);
640 QBrush bg = frame->frameFormat().background();
641 if (bg.style() != Qt::NoBrush)
642 m_backgrounds.append(std::make_pair(boundingRect, bg.color()));
644 if (!frameFormat.hasProperty(QTextFormat::FrameBorder))
647 qreal borderWidth = frameFormat.border();
648 if (qFuzzyIsNull(borderWidth))
651 QBrush borderBrush = frameFormat.borderBrush();
652 QTextFrameFormat::BorderStyle borderStyle = frameFormat.borderStyle();
653 if (borderStyle == QTextFrameFormat::BorderStyle_None)
656 const auto collapsed = table->format().borderCollapse();
659 addBorder(boundingRect.adjusted(frameFormat.leftMargin(), frameFormat.topMargin(),
660 -frameFormat.rightMargin() - borderWidth,
661 -frameFormat.bottomMargin() - borderWidth),
662 borderWidth, borderStyle, borderBrush);
664 if (table !=
nullptr) {
665 int rows = table->rows();
666 int columns = table->columns();
668 for (
int row=0; row<rows; ++row) {
669 for (
int column=0; column<columns; ++column) {
670 QTextTableCell cell = table->cellAt(row, column);
672 QRectF cellRect = documentLayout->tableCellBoundingRect(table, cell);
673 addBorder(cellRect.adjusted(-borderWidth, -borderWidth, collapsed ? -borderWidth : 0, collapsed ? -borderWidth : 0), borderWidth,
674 borderStyle, borderBrush);
682 return qHashMulti(seed, key.fontEngine, key.clipNode, key.color, key
.selectionState);
686 QList<BinaryTreeNode *> *imageNodes)
688 QHash<BinaryTreeNodeKey, QList<BinaryTreeNode *> > map;
690 for (
int i = 0; i < m_processedNodes.size(); ++i) {
693 if (node->image.isNull()) {
694 if (node->glyphRun.isEmpty())
699 QList<BinaryTreeNode *> &nodes = map[key];
701 regularNodes->append(node);
705 imageNodes->append(node);
709 for (
int i = 0; i < regularNodes->size(); ++i) {
713 const QList<BinaryTreeNode *> &nodes = map.value(key);
714 Q_ASSERT(nodes.first() == primaryNode);
717 for (
int j = 0; j < nodes.size(); ++j)
718 count += nodes.at(j)->glyphRun.glyphIndexes().size();
720 if (count != primaryNode->glyphRun.glyphIndexes().size()) {
721 QGlyphRun &glyphRun = primaryNode->glyphRun;
722 QList<quint32> glyphIndexes = glyphRun.glyphIndexes();
723 glyphIndexes.reserve(count);
725 QList<QPointF> glyphPositions = glyphRun.positions();
726 glyphPositions.reserve(count);
728 QRectF glyphBoundingRect = glyphRun.boundingRect();
730 for (
int j = 1; j < nodes.size(); ++j) {
732 glyphIndexes += otherNode->glyphRun.glyphIndexes();
733 primaryNode->ranges += otherNode->ranges;
734 glyphBoundingRect = glyphBoundingRect.united(otherNode->boundingRect);
736 QList<QPointF> otherPositions = otherNode->glyphRun.positions();
737 for (
int k = 0; k < otherPositions.size(); ++k)
738 glyphPositions += otherPositions.at(k) + (otherNode->position - primaryNode->position);
741 Q_ASSERT(glyphPositions.size() == count);
742 Q_ASSERT(glyphIndexes.size() == count);
744 glyphRun.setGlyphIndexes(glyphIndexes);
745 glyphRun.setPositions(glyphPositions);
746 glyphRun.setBoundingRect(glyphBoundingRect);
752 QQuickText::TextStyle style,
753 const QColor &styleColor)
755 if (m_currentLine.isValid())
756 processCurrentLine();
758 QList<BinaryTreeNode *> nodes;
759 QList<BinaryTreeNode *> imageNodes;
760 mergeProcessedNodes(&nodes, &imageNodes);
762 for (
int i = 0; i < m_backgrounds.size(); ++i) {
763 const QRectF &rect = m_backgrounds.at(i).first;
764 const QColor &color = m_backgrounds.at(i).second;
765 if (color.alpha() != 0)
766 parentNode->addRectangleNode(rect, color);
770 for (
int i = 0; i < nodes.size(); ++i) {
771 const BinaryTreeNode *node = nodes.at(i);
772 parentNode->addGlyphs(node->position, node->glyphRun, node->color, style, styleColor,
nullptr);
775 for (
int i = 0; i < imageNodes.size(); ++i) {
776 const BinaryTreeNode *node = imageNodes.at(i);
777 if (node->selectionState == Unselected)
778 parentNode->addImage(node->boundingRect, node->image);
782 for (
int i = 0; i < m_selectionRects.size(); ++i) {
783 const QRectF &rect = m_selectionRects.at(i);
784 if (m_selectionColor.alpha() != 0)
785 parentNode->addRectangleNode(rect, m_selectionColor);
789 for (
int i = 0; i < m_lines.size(); ++i) {
790 const TextDecoration &textDecoration = m_lines.at(i);
792 QColor color = textDecoration.selectionState == Selected
793 ? m_selectedTextColor
794 : textDecoration.color;
796 parentNode->addDecorationNode(textDecoration.rect, color);
800 for (
int i = 0; i < nodes.size(); ++i) {
801 const BinaryTreeNode *node = nodes.at(i);
802 QQuickDefaultClipNode *clipNode = node->clipNode;
803 if (clipNode !=
nullptr && clipNode->parent() ==
nullptr)
804 parentNode->appendChildNode(clipNode);
806 if (node->selectionState == Selected) {
807 QColor color = m_selectedTextColor;
808 int previousNodeIndex = i - 1;
809 int nextNodeIndex = i + 1;
810 const BinaryTreeNode *previousNode = previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
811 while (previousNode !=
nullptr && qFuzzyCompare(previousNode->boundingRect.left(), node->boundingRect.left()))
812 previousNode = --previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
814 const BinaryTreeNode *nextNode = nextNodeIndex == nodes.size() ? 0 : nodes.at(nextNodeIndex);
816 if (previousNode !=
nullptr && previousNode->selectionState == Unselected)
817 parentNode->addGlyphs(previousNode->position, previousNode->glyphRun, color, style, styleColor, clipNode);
819 if (nextNode !=
nullptr && nextNode->selectionState == Unselected)
820 parentNode->addGlyphs(nextNode->position, nextNode->glyphRun, color, style, styleColor, clipNode);
824 bool drawCurrent =
false;
825 if (previousNode !=
nullptr || nextNode !=
nullptr) {
826 for (
int i = 0; i < node->ranges.size(); ++i) {
827 const std::pair<
int,
int> &range = node->ranges.at(i);
829 int rangeLength = range.second - range.first + 1;
830 if (previousNode !=
nullptr) {
831 for (
int j = 0; j < previousNode->ranges.size(); ++j) {
832 const std::pair<
int,
int> &otherRange = previousNode->ranges.at(j);
834 if (range.first < otherRange.second && range.second > otherRange.first) {
835 int start = qMax(range.first, otherRange.first);
836 int end = qMin(range.second, otherRange.second);
837 rangeLength -= end - start + 1;
838 if (rangeLength == 0)
844 if (nextNode !=
nullptr && rangeLength > 0) {
845 for (
int j = 0; j < nextNode->ranges.size(); ++j) {
846 const std::pair<
int,
int> &otherRange = nextNode->ranges.at(j);
848 if (range.first < otherRange.second && range.second > otherRange.first) {
849 int start = qMax(range.first, otherRange.first);
850 int end = qMin(range.second, otherRange.second);
851 rangeLength -= end - start + 1;
852 if (rangeLength == 0)
858 if (rangeLength > 0) {
868 parentNode->addGlyphs(node->position, node->glyphRun, color, style, styleColor, clipNode);
872 for (
int i = 0; i < imageNodes.size(); ++i) {
873 const BinaryTreeNode *node = imageNodes.at(i);
874 if (node->selectionState == Selected) {
875 parentNode->addImage(node->boundingRect, node->image);
876 if (node->selectionState == Selected) {
877 QColor color = m_selectionColor;
879 parentNode->addRectangleNode(node->boundingRect, color);
885void QQuickTextNodeEngine::mergeFormats(QTextLayout *textLayout, QVarLengthArray<QTextLayout::FormatRange> *mergedFormats)
887 Q_ASSERT(mergedFormats !=
nullptr);
888 if (textLayout ==
nullptr)
891 const QList<QTextLayout::FormatRange> additionalFormats = textLayout->formats();
892 for (QTextLayout::FormatRange additionalFormat : additionalFormats) {
893 if (additionalFormat.format.hasProperty(QTextFormat::ForegroundBrush)
894 || additionalFormat.format.hasProperty(QTextFormat::BackgroundBrush)
895 || additionalFormat.format.isAnchor()) {
897 if (!mergedFormats->isEmpty()) {
898 QTextLayout::FormatRange *lastFormat = mergedFormats->data() + mergedFormats->size() - 1;
900 if (additionalFormat.start < lastFormat->start + lastFormat->length) {
901 QTextLayout::FormatRange *mergedRange =
nullptr;
903 int length = additionalFormat.length;
904 if (additionalFormat.start > lastFormat->start) {
905 lastFormat->length = additionalFormat.start - lastFormat->start;
906 length -= lastFormat->length;
908 mergedFormats->append(QTextLayout::FormatRange());
909 mergedRange = mergedFormats->data() + mergedFormats->size() - 1;
910 lastFormat = mergedFormats->data() + mergedFormats->size() - 2;
912 mergedRange = lastFormat;
915 mergedRange->format = lastFormat->format;
916 mergedRange->format.merge(additionalFormat.format);
917 mergedRange->start = additionalFormat.start;
919 int end = qMin(additionalFormat.start + additionalFormat.length,
920 lastFormat->start + lastFormat->length);
922 mergedRange->length = end - mergedRange->start;
923 length -= mergedRange->length;
925 additionalFormat.start = end;
926 additionalFormat.length = length;
930 if (additionalFormat.length > 0)
931 mergedFormats->append(additionalFormat);
937
938
939
940
941
942
943
944
946 const QColor &textColor,
const QColor &anchorColor,
int selectionStart,
int selectionEnd,
const QRectF &viewport)
948 Q_ASSERT(textDocument);
950 int preeditLength = block.isValid() ? block.layout()->preeditAreaText().size() : 0;
951 int preeditPosition = block.isValid() ? block.layout()->preeditAreaPosition() : -1;
954 setCurrentTextDirection(block.textDirection());
956 QVarLengthArray<QTextLayout::FormatRange> colorChanges;
957 mergeFormats(block.layout(), &colorChanges);
959 const QTextCharFormat charFormat = block.charFormat();
960 const QRectF blockBoundingRect = textDocument->documentLayout()->blockBoundingRect(block).translated(position);
961 if (viewport.isValid()) {
962 if (!blockBoundingRect.intersects(viewport))
964 qCDebug(lcSgText) <<
"adding block with length" << block.length() <<
':' << blockBoundingRect <<
"in viewport" << viewport;
967 if (charFormat.background().style() != Qt::NoBrush)
968 m_backgrounds.append(std::make_pair(blockBoundingRect, charFormat.background().color()));
970 if (QTextList *textList = block.textList()) {
971 QPointF pos = blockBoundingRect.topLeft();
972 QTextLayout *layout = block.layout();
973 if (layout->lineCount() > 0) {
974 QTextLine firstLine = layout->lineAt(0);
975 Q_ASSERT(firstLine.isValid());
979 QRectF textRect = firstLine.naturalTextRect();
980 pos += textRect.topLeft();
981 if (block.textDirection() == Qt::RightToLeft)
982 pos.rx() += textRect.width();
984 QFont font(charFormat.font());
985 QFontMetricsF fontMetrics(font);
986 QTextListFormat listFormat = textList->format();
988 QString listItemBullet;
989 switch (listFormat.style()) {
990 case QTextListFormat::ListCircle:
991 listItemBullet = QChar(0x25E6);
993 case QTextListFormat::ListSquare:
994 listItemBullet = QChar(0x25AA);
996 case QTextListFormat::ListDecimal:
997 case QTextListFormat::ListLowerAlpha:
998 case QTextListFormat::ListUpperAlpha:
999 case QTextListFormat::ListLowerRoman:
1000 case QTextListFormat::ListUpperRoman:
1001 listItemBullet = textList->itemText(block);
1004 listItemBullet = QChar(0x2022);
1008 switch (block.blockFormat().marker()) {
1009 case QTextBlockFormat::MarkerType::Checked:
1010 listItemBullet = QChar(0x2612);
1012 case QTextBlockFormat::MarkerType::Unchecked:
1013 listItemBullet = QChar(0x2610);
1015 case QTextBlockFormat::MarkerType::NoMarker:
1019 QSizeF size(fontMetrics.horizontalAdvance(listItemBullet), fontMetrics.height());
1020 qreal xoff = fontMetrics.horizontalAdvance(QLatin1Char(
' '));
1021 if (block.textDirection() == Qt::LeftToRight)
1022 xoff = -xoff - size.width();
1023 setPosition(pos + QPointF(xoff, 0));
1026 layout.setFont(font);
1027 layout.setText(listItemBullet);
1028 layout.beginLayout();
1029 QTextLine line = layout.createLine();
1030 line.setPosition(QPointF(0, 0));
1034 if (charFormat.foreground().style() == Qt::NoBrush)
1039 const QList<QGlyphRun> glyphRuns = layout.glyphRuns();
1040 for (
const auto &e : glyphRuns)
1041 addUnselectedGlyphs(e);
1045 int textPos = block.position();
1046 QTextBlock::iterator blockIterator = block.begin();
1048 while (!blockIterator.atEnd()) {
1049 QTextFragment fragment = blockIterator.fragment();
1050 QString text = fragment.text();
1054 QTextCharFormat charFormat = fragment.charFormat();
1055 QFont font(charFormat.font());
1056 QFontMetricsF fontMetrics(font);
1058 int fontHeight = fontMetrics.descent() + fontMetrics.ascent();
1059 int valign = charFormat.verticalAlignment();
1060 if (valign == QTextCharFormat::AlignSuperScript)
1061 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() - fontHeight / 2));
1062 else if (valign == QTextCharFormat::AlignSubScript)
1063 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() + fontHeight / 6));
1065 setPosition(blockBoundingRect.topLeft());
1067 if (text.contains(QChar::ObjectReplacementCharacter) && charFormat.objectType() != QTextFormat::NoObject) {
1068 QTextFrame *frame = qobject_cast<QTextFrame *>(textDocument->objectForFormat(charFormat));
1069 if (!frame || frame->frameFormat().position() == QTextFrameFormat::InFlow) {
1070 int blockRelativePosition = textPos - block.position();
1071 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
1072 if (!currentLine().isValid()
1073 || line.lineNumber() != currentLine().lineNumber()) {
1077 QQuickTextNodeEngine::SelectionState selectionState =
1078 (selectionStart < textPos + text.size()
1079 && selectionEnd >= textPos)
1080 ? QQuickTextNodeEngine::Selected
1081 : QQuickTextNodeEngine::Unselected;
1083 addTextObject(block, QPointF(), charFormat, selectionState, textDocument, textPos);
1085 textPos += text.size();
1087 if (charFormat.foreground().style() != Qt::NoBrush)
1089 else if (charFormat.isAnchor())
1094 int fragmentEnd = textPos + fragment.length();
1096 if (preeditPosition >= 0
1097 && (preeditPosition + block.position()) >= textPos
1098 && (preeditPosition + block.position()) <= fragmentEnd) {
1099 fragmentEnd += preeditLength;
1102 if (charFormat.background().style() != Qt::NoBrush || charFormat.hasProperty(QTextFormat::TextUnderlineColor)) {
1103 QTextLayout::FormatRange additionalFormat;
1104 additionalFormat.start = textPos - block.position();
1105 additionalFormat.length = fragmentEnd - textPos;
1106 additionalFormat.format = charFormat;
1107 colorChanges << additionalFormat;
1110 textPos = addText(block, charFormat, textColor, colorChanges, textPos, fragmentEnd,
1111 selectionStart, selectionEnd);
1118 if (preeditLength >= 0 && textPos <= block.position() + preeditPosition) {
1119 setPosition(blockBoundingRect.topLeft());
1120 textPos = block.position() + preeditPosition;
1121 QTextLine line = block.layout()->lineForTextPosition(preeditPosition);
1122 if (!currentLine().isValid()
1123 || line.lineNumber() != currentLine().lineNumber()) {
1124 setCurrentLine(line);
1126 textPos = addText(block, block.charFormat(), textColor, colorChanges,
1127 textPos, textPos + preeditLength,
1128 selectionStart, selectionEnd);
1133 if (block.blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) {
1134 auto ruleLength = qvariant_cast<QTextLength>(block.blockFormat().property(QTextFormat::BlockTrailingHorizontalRulerWidth));
1135 QRectF ruleRect(0, 0, ruleLength.value(blockBoundingRect.width()), 1);
1136 ruleRect.moveCenter(blockBoundingRect.center());
1137 const QColor ruleColor = block.blockFormat().hasProperty(QTextFormat::BackgroundBrush)
1138 ? qvariant_cast<QBrush>(block.blockFormat().property(QTextFormat::BackgroundBrush)).color()
1140 m_lines.append(TextDecoration(QQuickTextNodeEngine::Unselected, ruleRect, ruleColor));
1144 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)
Combined button and popup list for selecting options.
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)