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 (
int i=0; i<textDecorations.size(); ++i) {
176 TextDecoration textDecoration = textDecorations.at(i);
179 QRectF &rect = textDecoration.rect;
180 rect.setY(qRound(rect.y() + m_currentLine.ascent() + offset));
181 rect.setHeight(thickness);
184 m_lines.append(textDecoration);
191 if (m_currentLineTree.isEmpty())
200 QVarLengthArray<
int> sortedIndexes;
201 BinaryTreeNode::inOrder(m_currentLineTree, &sortedIndexes);
203 Q_ASSERT(sortedIndexes.size() == m_currentLineTree.size());
205 SelectionState currentSelectionState = Unselected;
209 qreal underlineOffset = 0.0;
210 qreal underlineThickness = 0.0;
212 qreal overlineOffset = 0.0;
213 qreal overlineThickness = 0.0;
215 qreal strikeOutOffset = 0.0;
216 qreal strikeOutThickness = 0.0;
218 QRectF decorationRect = currentRect;
221 QColor lastBackgroundColor;
222 QColor lastDecorationColor;
224 QVarLengthArray<TextDecoration> pendingUnderlines;
225 QVarLengthArray<TextDecoration> pendingOverlines;
226 QVarLengthArray<TextDecoration> pendingStrikeOuts;
227 if (!sortedIndexes.isEmpty()) {
228 QQuickDefaultClipNode *currentClipNode = m_hasSelection ?
new QQuickDefaultClipNode(QRectF()) :
nullptr;
229 bool currentClipNodeUsed =
false;
230 for (
int i=0; i<=sortedIndexes.size(); ++i) {
232 if (i < sortedIndexes.size()) {
233 int sortedIndex = sortedIndexes.at(i);
234 Q_ASSERT(sortedIndex < m_currentLineTree.size());
236 node = m_currentLineTree.data() + sortedIndex;
238 currentSelectionState = node->selectionState;
243 decorationRect.setY(m_position.y() + m_currentLine.y());
244 decorationRect.setHeight(m_currentLine.height());
247 decorationRect.setRight(node->boundingRect.left());
249 TextDecoration textDecoration(currentSelectionState, decorationRect, lastColor);
250 if (lastDecorationColor.isValid() &&
254 textDecoration.color = lastDecorationColor;
257 pendingUnderlines.append(textDecoration);
260 pendingOverlines.append(textDecoration);
263 pendingStrikeOuts.append(textDecoration);
265 if (currentDecorations & Decoration::Background)
266 m_backgrounds.append(std::make_pair(decorationRect, lastBackgroundColor));
272 if (node ==
nullptr || node->selectionState != currentSelectionState) {
273 currentRect.setY(m_position.y() + m_currentLine.y());
274 currentRect.setHeight(m_currentLine.height());
276 if (currentSelectionState == Selected)
277 m_selectionRects.append(currentRect);
279 if (currentClipNode !=
nullptr) {
280 if (!currentClipNodeUsed) {
281 delete currentClipNode;
283 currentClipNode->setIsRectangular(
true);
284 currentClipNode->setRect(currentRect);
285 currentClipNode->update();
289 if (node !=
nullptr && m_hasSelection)
290 currentClipNode =
new QQuickDefaultClipNode(QRectF());
292 currentClipNode =
nullptr;
293 currentClipNodeUsed =
false;
295 if (node !=
nullptr) {
296 currentSelectionState = node->selectionState;
297 currentRect = node->boundingRect;
300 if (currentRect.isNull())
301 currentRect.setSize(QSizeF(1, 1));
304 if (currentRect.isNull())
305 currentRect = node->boundingRect;
307 currentRect = currentRect.united(node->boundingRect);
310 if (node !=
nullptr) {
311 if (node->selectionState == Selected) {
312 node->clipNode = currentClipNode;
313 currentClipNodeUsed =
true;
316 decorationRect = node->boundingRect;
320 if (!pendingUnderlines.isEmpty()
322 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
324 pendingUnderlines.clear();
326 underlineOffset = 0.0;
327 underlineThickness = 0.0;
332 if (!pendingOverlines.isEmpty()) {
333 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
335 pendingOverlines.clear();
337 overlineOffset = 0.0;
338 overlineThickness = 0.0;
343 if (!pendingStrikeOuts.isEmpty()) {
344 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
346 pendingStrikeOuts.clear();
348 strikeOutOffset = 0.0;
349 strikeOutThickness = 0.0;
353 QRawFont rawFont = node->glyphRun.rawFont();
355 if (rawFont.lineThickness() > underlineThickness) {
356 underlineThickness = rawFont.lineThickness();
357 underlineOffset = rawFont.underlinePosition();
362 overlineOffset = -rawFont.ascent();
363 overlineThickness = rawFont.lineThickness();
367 strikeOutThickness = rawFont.lineThickness();
368 strikeOutOffset = rawFont.ascent() / -3.0;
371 currentDecorations = node->decorations;
372 lastColor = node->color;
373 lastBackgroundColor = node->backgroundColor;
374 lastDecorationColor = node->decorationColor;
375 m_processedNodes.append(*node);
379 if (!pendingUnderlines.isEmpty())
380 addTextDecorations(pendingUnderlines, underlineOffset, underlineThickness);
382 if (!pendingOverlines.isEmpty())
383 addTextDecorations(pendingOverlines, overlineOffset, overlineThickness);
385 if (!pendingStrikeOuts.isEmpty())
386 addTextDecorations(pendingStrikeOuts, strikeOutOffset, strikeOutThickness);
389 m_currentLineTree.clear();
390 m_currentLine = QTextLine();
391 m_hasSelection =
false;
395 SelectionState selectionState,
396 QTextFrameFormat::Position layoutPosition)
398 QRectF searchRect = rect;
399 if (layoutPosition == QTextFrameFormat::InFlow) {
400 if (m_currentLineTree.isEmpty()) {
401 qreal y = m_currentLine.ascent() - ascent;
402 if (m_currentTextDirection == Qt::RightToLeft)
403 searchRect.moveTopRight(m_position + m_currentLine.rect().topRight() + QPointF(0, y));
405 searchRect.moveTopLeft(m_position + m_currentLine.position() + QPointF(0, y));
407 const BinaryTreeNode *lastNode = m_currentLineTree.data() + m_currentLineTree.size() - 1;
408 if (lastNode->glyphRun.isRightToLeft()) {
409 QPointF lastPos = lastNode->boundingRect.topLeft();
410 searchRect.moveTopRight(lastPos - QPointF(0, ascent - lastNode->ascent));
412 QPointF lastPos = lastNode->boundingRect.topRight();
413 searchRect.moveTopLeft(lastPos - QPointF(0, ascent - lastNode->ascent));
418 BinaryTreeNode::insert(&m_currentLineTree, searchRect, image, ascent, selectionState);
419 m_hasContents =
true;
423 SelectionState selectionState,
424 QTextDocument *textDocument,
int pos,
425 QTextFrameFormat::Position layoutPosition)
427 QTextObjectInterface *handler = textDocument->documentLayout()->handlerForObject(format.objectType());
428 if (handler !=
nullptr) {
430 QSizeF size = handler->intrinsicSize(textDocument, pos, format);
432 if (format.objectType() == QTextFormat::ImageObject) {
433 QTextImageFormat imageFormat = format.toImageFormat();
434 QTextImageHandler *imageHandler =
static_cast<QTextImageHandler *>(handler);
435 image = imageHandler->image(textDocument, imageFormat);
438 if (image.isNull()) {
439 if (QImageIOHandler::allocateImage((size * m_devicePixelRatio).toSize(),
440 QImage::Format_ARGB32_Premultiplied,
442 image.setDevicePixelRatio(m_devicePixelRatio);
443 image.fill(Qt::transparent);
445 QPainter painter(&image);
446 handler->drawObject(&painter, QRectF({}, size), textDocument, pos, format);
455 QTextLine line = block.layout()->lineForTextPosition(pos - block.position());
456 switch (format.verticalAlignment())
458 case QTextCharFormat::AlignTop:
459 ascent = line.ascent();
461 case QTextCharFormat::AlignMiddle:
463 ascent = (line.ascent() + line.descent()) / 2 - line.descent() + size.height() / 2;
465 case QTextCharFormat::AlignBottom:
466 ascent = size.height() - line.descent();
468 case QTextCharFormat::AlignBaseline:
470 ascent = size.height();
473 addImage(QRectF(position, size), image, ascent, selectionState, layoutPosition);
479 BinaryTreeNode::insert(&m_currentLineTree,
482 Decoration::NoDecoration,
491 int currentSize = m_currentLineTree.size();
492 BinaryTreeNode::insert(&m_currentLineTree,
495 Decoration::NoDecoration,
500 m_hasSelection = m_hasSelection || m_currentLineTree.size() > currentSize;
505 int selectionStart,
int selectionEnd)
507 int currentPosition = start;
508 int remainingLength = end - start;
509 for (
int j=0; j<ranges.size(); ++j) {
510 const QTextLayout::FormatRange &range = ranges.at(j);
511 if (range.start + range.length > currentPosition
512 && range.start < currentPosition + remainingLength) {
514 if (range.start > currentPosition) {
516 QColor()
, QColor()
, QColor()
, selectionStart
, selectionEnd
);
518 int rangeEnd = qMin(range.start + range.length, currentPosition + remainingLength);
520 if (range.format.hasProperty(QTextFormat::ForegroundBrush))
521 rangeColor = range.format.foreground().color();
522 else if (range.format.isAnchor())
523 rangeColor = m_anchorColor;
524 QColor rangeBackgroundColor = range.format.hasProperty(QTextFormat::BackgroundBrush)
525 ? range.format.background().color()
528 QColor rangeDecorationColor = range.format.hasProperty(QTextFormat::TextUnderlineColor)
529 ? range.format.underlineColor()
533 rangeColor
, rangeBackgroundColor
, rangeDecorationColor
,
534 selectionStart
, selectionEnd
);
536 currentPosition = range.start + range.length;
537 remainingLength = end - currentPosition;
539 }
else if (range.start > currentPosition + remainingLength || remainingLength <= 0) {
544 if (remainingLength > 0) {
546 selectionStart
, selectionEnd
);
552 const QColor &color,
const QColor &backgroundColor,
const QColor &decorationColor,
553 int selectionStart,
int selectionEnd)
556 if (color.isValid()) {
557 oldColor = m_textColor;
561 QColor oldBackgroundColor = m_backgroundColor;
562 if (backgroundColor.isValid()) {
563 oldBackgroundColor = m_backgroundColor;
564 m_backgroundColor = backgroundColor;
567 QColor oldDecorationColor = m_decorationColor;
568 if (decorationColor.isValid()) {
569 oldDecorationColor = m_decorationColor;
570 m_decorationColor = decorationColor;
573 bool hasSelection = selectionEnd >= 0
574 && selectionStart <= selectionEnd;
576 QTextLine &line = m_currentLine;
577 int rangeEnd = rangeStart + rangeLength;
578 if (!hasSelection || (selectionStart > rangeEnd || selectionEnd < rangeStart)) {
579 QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, rangeLength);
580 for (
int j=0; j<glyphRuns.size(); ++j) {
581 const QGlyphRun &glyphRun = glyphRuns.at(j);
582 addUnselectedGlyphs(glyphRun);
585 if (rangeStart < selectionStart) {
586 int length = qMin(selectionStart - rangeStart, rangeLength);
587 QList<QGlyphRun> glyphRuns = line.glyphRuns(rangeStart, length);
588 for (
int j=0; j<glyphRuns.size(); ++j) {
589 const QGlyphRun &glyphRun = glyphRuns.at(j);
590 addUnselectedGlyphs(glyphRun);
594 if (rangeEnd > selectionStart) {
595 int start = qMax(selectionStart, rangeStart);
596 int length = qMin(selectionEnd - start + 1, rangeEnd - start);
597 QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
599 for (
int j=0; j<glyphRuns.size(); ++j) {
600 const QGlyphRun &glyphRun = glyphRuns.at(j);
601 addSelectedGlyphs(glyphRun);
605 if (selectionEnd >= rangeStart && selectionEnd < rangeEnd) {
606 int start = selectionEnd + 1;
607 int length = rangeEnd - selectionEnd - 1;
608 QList<QGlyphRun> glyphRuns = line.glyphRuns(start, length);
609 for (
int j=0; j<glyphRuns.size(); ++j) {
610 const QGlyphRun &glyphRun = glyphRuns.at(j);
611 addUnselectedGlyphs(glyphRun);
616 if (decorationColor.isValid())
617 m_decorationColor = oldDecorationColor;
619 if (backgroundColor.isValid())
620 m_backgroundColor = oldBackgroundColor;
622 if (oldColor.isValid())
623 m_textColor = oldColor;
627 QTextFrameFormat::BorderStyle borderStyle,
628 const QBrush &borderBrush)
630 const QColor &color = borderBrush.color();
633 Q_UNUSED(borderStyle);
635 m_backgrounds.append(std::make_pair(QRectF(rect.left(), rect.top(), border, rect.height() + border), color));
636 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.top(), rect.width(), border), color));
637 m_backgrounds.append(std::make_pair(QRectF(rect.right(), rect.top() + border, border, rect.height() - border), color));
638 m_backgrounds.append(std::make_pair(QRectF(rect.left() + border, rect.bottom(), rect.width(), border), color));
643 QTextDocumentLayout *documentLayout = qobject_cast<QTextDocumentLayout *>(document->documentLayout());
644 if (Q_UNLIKELY(!documentLayout))
647 QTextFrameFormat frameFormat = frame->format().toFrameFormat();
648 QTextTable *table = qobject_cast<QTextTable *>(frame);
650 QRectF boundingRect = table ==
nullptr
651 ? documentLayout->frameBoundingRect(frame)
652 : documentLayout->tableBoundingRect(table);
654 QBrush bg = frame->frameFormat().background();
655 if (bg.style() != Qt::NoBrush)
656 m_backgrounds.append(std::make_pair(boundingRect, bg.color()));
658 if (!frameFormat.hasProperty(QTextFormat::FrameBorder))
661 qreal borderWidth = frameFormat.border();
662 if (qFuzzyIsNull(borderWidth))
665 QBrush borderBrush = frameFormat.borderBrush();
666 QTextFrameFormat::BorderStyle borderStyle = frameFormat.borderStyle();
667 if (borderStyle == QTextFrameFormat::BorderStyle_None)
670 const auto collapsed = table->format().borderCollapse();
673 addBorder(boundingRect.adjusted(frameFormat.leftMargin(), frameFormat.topMargin(),
674 -frameFormat.rightMargin() - borderWidth,
675 -frameFormat.bottomMargin() - borderWidth),
676 borderWidth, borderStyle, borderBrush);
678 if (table !=
nullptr) {
679 int rows = table->rows();
680 int columns = table->columns();
682 for (
int row=0; row<rows; ++row) {
683 for (
int column=0; column<columns; ++column) {
684 QTextTableCell cell = table->cellAt(row, column);
686 QRectF cellRect = documentLayout->tableCellBoundingRect(table, cell);
687 addBorder(cellRect.adjusted(-borderWidth, -borderWidth, collapsed ? -borderWidth : 0, collapsed ? -borderWidth : 0), borderWidth,
688 borderStyle, borderBrush);
696 return qHashMulti(seed, key.fontEngine, key.clipNode, key.color, key
.selectionState);
700 QList<BinaryTreeNode *> *imageNodes)
702 QHash<BinaryTreeNodeKey, QList<BinaryTreeNode *> > map;
704 for (
int i = 0; i < m_processedNodes.size(); ++i) {
707 if (node->image.isNull()) {
708 if (node->glyphRun.isEmpty())
713 QList<BinaryTreeNode *> &nodes = map[key];
715 regularNodes->append(node);
719 imageNodes->append(node);
723 for (
int i = 0; i < regularNodes->size(); ++i) {
727 const QList<BinaryTreeNode *> &nodes = map.value(key);
728 Q_ASSERT(nodes.first() == primaryNode);
731 for (
int j = 0; j < nodes.size(); ++j)
732 count += nodes.at(j)->glyphRun.glyphIndexes().size();
734 if (count != primaryNode->glyphRun.glyphIndexes().size()) {
735 QGlyphRun &glyphRun = primaryNode->glyphRun;
736 QVector<quint32> glyphIndexes = glyphRun.glyphIndexes();
737 glyphIndexes.reserve(count);
739 QVector<QPointF> glyphPositions = glyphRun.positions();
740 glyphPositions.reserve(count);
742 QRectF glyphBoundingRect = glyphRun.boundingRect();
744 for (
int j = 1; j < nodes.size(); ++j) {
746 glyphIndexes += otherNode->glyphRun.glyphIndexes();
747 primaryNode->ranges += otherNode->ranges;
748 glyphBoundingRect = glyphBoundingRect.united(otherNode->boundingRect);
750 QVector<QPointF> otherPositions = otherNode->glyphRun.positions();
751 for (
int k = 0; k < otherPositions.size(); ++k)
752 glyphPositions += otherPositions.at(k) + (otherNode->position - primaryNode->position);
755 Q_ASSERT(glyphPositions.size() == count);
756 Q_ASSERT(glyphIndexes.size() == count);
758 glyphRun.setGlyphIndexes(glyphIndexes);
759 glyphRun.setPositions(glyphPositions);
760 glyphRun.setBoundingRect(glyphBoundingRect);
766 QQuickText::TextStyle style,
767 const QColor &styleColor)
769 if (m_currentLine.isValid())
770 processCurrentLine();
772 QList<BinaryTreeNode *> nodes;
773 QList<BinaryTreeNode *> imageNodes;
774 mergeProcessedNodes(&nodes, &imageNodes);
776 for (
int i = 0; i < m_backgrounds.size(); ++i) {
777 const QRectF &rect = m_backgrounds.at(i).first;
778 const QColor &color = m_backgrounds.at(i).second;
779 if (color.alpha() != 0)
780 parentNode->addRectangleNode(rect, color);
784 for (
int i = 0; i < nodes.size(); ++i) {
785 const BinaryTreeNode *node = nodes.at(i);
786 parentNode->addGlyphs(node->position, node->glyphRun, node->color, style, styleColor,
nullptr);
789 for (
int i = 0; i < imageNodes.size(); ++i) {
790 const BinaryTreeNode *node = imageNodes.at(i);
791 if (node->selectionState == Unselected)
792 parentNode->addImage(node->boundingRect, node->image);
796 for (
int i = 0; i < m_selectionRects.size(); ++i) {
797 const QRectF &rect = m_selectionRects.at(i);
798 if (m_selectionColor.alpha() != 0)
799 parentNode->addRectangleNode(rect, m_selectionColor);
803 for (
int i = 0; i < m_lines.size(); ++i) {
804 const TextDecoration &textDecoration = m_lines.at(i);
806 QColor color = textDecoration.selectionState == Selected
807 ? m_selectedTextColor
808 : textDecoration.color;
810 parentNode->addDecorationNode(textDecoration.rect, color);
814 for (
int i = 0; i < nodes.size(); ++i) {
815 const BinaryTreeNode *node = nodes.at(i);
816 QQuickDefaultClipNode *clipNode = node->clipNode;
817 if (clipNode !=
nullptr && clipNode->parent() ==
nullptr)
818 parentNode->appendChildNode(clipNode);
820 if (node->selectionState == Selected) {
821 QColor color = m_selectedTextColor;
822 int previousNodeIndex = i - 1;
823 int nextNodeIndex = i + 1;
824 const BinaryTreeNode *previousNode = previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
825 while (previousNode !=
nullptr && qFuzzyCompare(previousNode->boundingRect.left(), node->boundingRect.left()))
826 previousNode = --previousNodeIndex < 0 ? 0 : nodes.at(previousNodeIndex);
828 const BinaryTreeNode *nextNode = nextNodeIndex == nodes.size() ? 0 : nodes.at(nextNodeIndex);
830 if (previousNode !=
nullptr && previousNode->selectionState == Unselected)
831 parentNode->addGlyphs(previousNode->position, previousNode->glyphRun, color, style, styleColor, clipNode);
833 if (nextNode !=
nullptr && nextNode->selectionState == Unselected)
834 parentNode->addGlyphs(nextNode->position, nextNode->glyphRun, color, style, styleColor, clipNode);
838 bool drawCurrent =
false;
839 if (previousNode !=
nullptr || nextNode !=
nullptr) {
840 for (
int i = 0; i < node->ranges.size(); ++i) {
841 const std::pair<
int,
int> &range = node->ranges.at(i);
843 int rangeLength = range.second - range.first + 1;
844 if (previousNode !=
nullptr) {
845 for (
int j = 0; j < previousNode->ranges.size(); ++j) {
846 const std::pair<
int,
int> &otherRange = previousNode->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 (nextNode !=
nullptr && rangeLength > 0) {
859 for (
int j = 0; j < nextNode->ranges.size(); ++j) {
860 const std::pair<
int,
int> &otherRange = nextNode->ranges.at(j);
862 if (range.first < otherRange.second && range.second > otherRange.first) {
863 int start = qMax(range.first, otherRange.first);
864 int end = qMin(range.second, otherRange.second);
865 rangeLength -= end - start + 1;
866 if (rangeLength == 0)
872 if (rangeLength > 0) {
882 parentNode->addGlyphs(node->position, node->glyphRun, color, style, styleColor, clipNode);
886 for (
int i = 0; i < imageNodes.size(); ++i) {
887 const BinaryTreeNode *node = imageNodes.at(i);
888 if (node->selectionState == Selected) {
889 parentNode->addImage(node->boundingRect, node->image);
890 if (node->selectionState == Selected) {
891 QColor color = m_selectionColor;
893 parentNode->addRectangleNode(node->boundingRect, color);
899void QQuickTextNodeEngine::mergeFormats(QTextLayout *textLayout, QVarLengthArray<QTextLayout::FormatRange> *mergedFormats)
901 Q_ASSERT(mergedFormats !=
nullptr);
902 if (textLayout ==
nullptr)
905 QVector<QTextLayout::FormatRange> additionalFormats = textLayout->formats();
906 for (
int i=0; i<additionalFormats.size(); ++i) {
907 QTextLayout::FormatRange additionalFormat = additionalFormats.at(i);
908 if (additionalFormat.format.hasProperty(QTextFormat::ForegroundBrush)
909 || additionalFormat.format.hasProperty(QTextFormat::BackgroundBrush)
910 || additionalFormat.format.isAnchor()) {
912 if (!mergedFormats->isEmpty()) {
913 QTextLayout::FormatRange *lastFormat = mergedFormats->data() + mergedFormats->size() - 1;
915 if (additionalFormat.start < lastFormat->start + lastFormat->length) {
916 QTextLayout::FormatRange *mergedRange =
nullptr;
918 int length = additionalFormat.length;
919 if (additionalFormat.start > lastFormat->start) {
920 lastFormat->length = additionalFormat.start - lastFormat->start;
921 length -= lastFormat->length;
923 mergedFormats->append(QTextLayout::FormatRange());
924 mergedRange = mergedFormats->data() + mergedFormats->size() - 1;
925 lastFormat = mergedFormats->data() + mergedFormats->size() - 2;
927 mergedRange = lastFormat;
930 mergedRange->format = lastFormat->format;
931 mergedRange->format.merge(additionalFormat.format);
932 mergedRange->start = additionalFormat.start;
934 int end = qMin(additionalFormat.start + additionalFormat.length,
935 lastFormat->start + lastFormat->length);
937 mergedRange->length = end - mergedRange->start;
938 length -= mergedRange->length;
940 additionalFormat.start = end;
941 additionalFormat.length = length;
945 if (additionalFormat.length > 0)
946 mergedFormats->append(additionalFormat);
953
954
955
956
957
958
959
960
962 const QColor &textColor,
const QColor &anchorColor,
int selectionStart,
int selectionEnd,
const QRectF &viewport)
964 Q_ASSERT(textDocument);
966 int preeditLength = block.isValid() ? block.layout()->preeditAreaText().size() : 0;
967 int preeditPosition = block.isValid() ? block.layout()->preeditAreaPosition() : -1;
970 setCurrentTextDirection(block.textDirection());
972 QVarLengthArray<QTextLayout::FormatRange> colorChanges;
973 mergeFormats(block.layout(), &colorChanges);
975 const QTextCharFormat charFormat = block.charFormat();
976 const QRectF blockBoundingRect = textDocument->documentLayout()->blockBoundingRect(block).translated(position);
977 if (viewport.isValid()) {
978 if (!blockBoundingRect.intersects(viewport))
980 qCDebug(lcSgText) <<
"adding block with length" << block.length() <<
':' << blockBoundingRect <<
"in viewport" << viewport;
983 if (charFormat.background().style() != Qt::NoBrush)
984 m_backgrounds.append(std::make_pair(blockBoundingRect, charFormat.background().color()));
986 if (QTextList *textList = block.textList()) {
987 QPointF pos = blockBoundingRect.topLeft();
988 QTextLayout *layout = block.layout();
989 if (layout->lineCount() > 0) {
990 QTextLine firstLine = layout->lineAt(0);
991 Q_ASSERT(firstLine.isValid());
995 QRectF textRect = firstLine.naturalTextRect();
996 pos += textRect.topLeft();
997 if (block.textDirection() == Qt::RightToLeft)
998 pos.rx() += textRect.width();
1000 QFont font(charFormat.font());
1001 QFontMetricsF fontMetrics(font);
1002 QTextListFormat listFormat = textList->format();
1004 QString listItemBullet;
1005 switch (listFormat.style()) {
1006 case QTextListFormat::ListCircle:
1007 listItemBullet = QChar(0x25E6);
1009 case QTextListFormat::ListSquare:
1010 listItemBullet = QChar(0x25AA);
1012 case QTextListFormat::ListDecimal:
1013 case QTextListFormat::ListLowerAlpha:
1014 case QTextListFormat::ListUpperAlpha:
1015 case QTextListFormat::ListLowerRoman:
1016 case QTextListFormat::ListUpperRoman:
1017 listItemBullet = textList->itemText(block);
1020 listItemBullet = QChar(0x2022);
1024 switch (block.blockFormat().marker()) {
1025 case QTextBlockFormat::MarkerType::Checked:
1026 listItemBullet = QChar(0x2612);
1028 case QTextBlockFormat::MarkerType::Unchecked:
1029 listItemBullet = QChar(0x2610);
1031 case QTextBlockFormat::MarkerType::NoMarker:
1035 QSizeF size(fontMetrics.horizontalAdvance(listItemBullet), fontMetrics.height());
1036 qreal xoff = fontMetrics.horizontalAdvance(QLatin1Char(
' '));
1037 if (block.textDirection() == Qt::LeftToRight)
1038 xoff = -xoff - size.width();
1039 setPosition(pos + QPointF(xoff, 0));
1042 layout.setFont(font);
1043 layout.setText(listItemBullet);
1044 layout.beginLayout();
1045 QTextLine line = layout.createLine();
1046 line.setPosition(QPointF(0, 0));
1050 if (charFormat.foreground().style() == Qt::NoBrush)
1055 QList<QGlyphRun> glyphRuns = layout.glyphRuns();
1056 for (
int i=0; i<glyphRuns.size(); ++i)
1057 addUnselectedGlyphs(glyphRuns.at(i));
1061 int textPos = block.position();
1062 QTextBlock::iterator blockIterator = block.begin();
1064 while (!blockIterator.atEnd()) {
1065 QTextFragment fragment = blockIterator.fragment();
1066 QString text = fragment.text();
1070 QTextCharFormat charFormat = fragment.charFormat();
1071 QFont font(charFormat.font());
1072 QFontMetricsF fontMetrics(font);
1074 int fontHeight = fontMetrics.descent() + fontMetrics.ascent();
1075 int valign = charFormat.verticalAlignment();
1076 if (valign == QTextCharFormat::AlignSuperScript)
1077 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() - fontHeight / 2));
1078 else if (valign == QTextCharFormat::AlignSubScript)
1079 setPosition(QPointF(blockBoundingRect.x(), blockBoundingRect.y() + fontHeight / 6));
1081 setPosition(blockBoundingRect.topLeft());
1083 if (text.contains(QChar::ObjectReplacementCharacter) && charFormat.objectType() != QTextFormat::NoObject) {
1084 QTextFrame *frame = qobject_cast<QTextFrame *>(textDocument->objectForFormat(charFormat));
1085 if (!frame || frame->frameFormat().position() == QTextFrameFormat::InFlow) {
1086 int blockRelativePosition = textPos - block.position();
1087 QTextLine line = block.layout()->lineForTextPosition(blockRelativePosition);
1088 if (!currentLine().isValid()
1089 || line.lineNumber() != currentLine().lineNumber()) {
1093 QQuickTextNodeEngine::SelectionState selectionState =
1094 (selectionStart < textPos + text.size()
1095 && selectionEnd >= textPos)
1096 ? QQuickTextNodeEngine::Selected
1097 : QQuickTextNodeEngine::Unselected;
1099 addTextObject(block, QPointF(), charFormat, selectionState, textDocument, textPos);
1101 textPos += text.size();
1103 if (charFormat.foreground().style() != Qt::NoBrush)
1105 else if (charFormat.isAnchor())
1110 int fragmentEnd = textPos + fragment.length();
1112 if (preeditPosition >= 0
1113 && (preeditPosition + block.position()) >= textPos
1114 && (preeditPosition + block.position()) <= fragmentEnd) {
1115 fragmentEnd += preeditLength;
1118 if (charFormat.background().style() != Qt::NoBrush || charFormat.hasProperty(QTextFormat::TextUnderlineColor)) {
1119 QTextLayout::FormatRange additionalFormat;
1120 additionalFormat.start = textPos - block.position();
1121 additionalFormat.length = fragmentEnd - textPos;
1122 additionalFormat.format = charFormat;
1123 colorChanges << additionalFormat;
1126 textPos = addText(block, charFormat, textColor, colorChanges, textPos, fragmentEnd,
1127 selectionStart, selectionEnd);
1134 if (preeditLength >= 0 && textPos <= block.position() + preeditPosition) {
1135 setPosition(blockBoundingRect.topLeft());
1136 textPos = block.position() + preeditPosition;
1137 QTextLine line = block.layout()->lineForTextPosition(preeditPosition);
1138 if (!currentLine().isValid()
1139 || line.lineNumber() != currentLine().lineNumber()) {
1140 setCurrentLine(line);
1142 textPos = addText(block, block.charFormat(), textColor, colorChanges,
1143 textPos, textPos + preeditLength,
1144 selectionStart, selectionEnd);
1149 if (block.blockFormat().hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) {
1150 auto ruleLength = qvariant_cast<QTextLength>(block.blockFormat().property(QTextFormat::BlockTrailingHorizontalRulerWidth));
1151 QRectF ruleRect(0, 0, ruleLength.value(blockBoundingRect.width()), 1);
1152 ruleRect.moveCenter(blockBoundingRect.center());
1153 const QColor ruleColor = block.blockFormat().hasProperty(QTextFormat::BackgroundBrush)
1154 ? qvariant_cast<QBrush>(block.blockFormat().property(QTextFormat::BackgroundBrush)).color()
1156 m_lines.append(TextDecoration(QQuickTextNodeEngine::Unselected, ruleRect, ruleColor));
1160 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)