1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qsginternaltextnode_p.h" |
5 | |
6 | #include "qquicktextnodeengine_p.h" |
7 | |
8 | #include <private/qsgadaptationlayer_p.h> |
9 | #include <private/qsgdistancefieldglyphnode_p.h> |
10 | #include <private/qquickclipnode_p.h> |
11 | #include <private/qquickitem_p.h> |
12 | #include <private/qquicktextdocument_p.h> |
13 | |
14 | #include <QtCore/qpoint.h> |
15 | #include <qtextdocument.h> |
16 | #include <qtextlayout.h> |
17 | #include <qabstracttextdocumentlayout.h> |
18 | #include <private/qquickstyledtext_p.h> |
19 | #include <private/qquicktext_p_p.h> |
20 | #include <private/qfont_p.h> |
21 | #include <private/qfontengine_p.h> |
22 | |
23 | #include <private/qtextdocumentlayout_p.h> |
24 | #include <qhash.h> |
25 | |
26 | QT_BEGIN_NAMESPACE |
27 | |
28 | Q_DECLARE_LOGGING_CATEGORY(lcVP) |
29 | |
30 | /*! |
31 | Creates an empty QSGInternalTextNode |
32 | */ |
33 | QSGInternalTextNode::QSGInternalTextNode(QSGRenderContext *renderContext) |
34 | : m_renderContext(renderContext) |
35 | { |
36 | #ifdef QSG_RUNTIME_DESCRIPTION |
37 | qsgnode_set_description(node: this, description: QLatin1String("text" )); |
38 | #endif |
39 | |
40 | static_assert(int(QSGTextNode::Normal) == int(QQuickText::Normal)); |
41 | static_assert(int(QSGTextNode::Outline) == int(QQuickText::Outline)); |
42 | static_assert(int(QSGTextNode::Raised) == int(QQuickText::Raised)); |
43 | static_assert(int(QSGTextNode::Sunken) == int(QQuickText::Sunken)); |
44 | |
45 | static_assert(int(QSGTextNode::QtRendering) == int(QQuickText::QtRendering)); |
46 | static_assert(int(QSGTextNode::NativeRendering) == int(QQuickText::NativeRendering)); |
47 | static_assert(int(QSGTextNode::CurveRendering) == int(QQuickText::CurveRendering)); |
48 | } |
49 | |
50 | QSGInternalTextNode::~QSGInternalTextNode() |
51 | { |
52 | qDeleteAll(c: m_textures); |
53 | } |
54 | |
55 | QSGGlyphNode *QSGInternalTextNode::addGlyphs(const QPointF &position, const QGlyphRun &glyphs, const QColor &color, |
56 | QQuickText::TextStyle style, const QColor &styleColor, |
57 | QSGNode *parentNode) |
58 | { |
59 | QRawFont font = glyphs.rawFont(); |
60 | |
61 | QSGTextNode::RenderType preferredRenderType = m_renderType; |
62 | if (m_renderType != NativeRendering) { |
63 | if (const QFontEngine *fe = QRawFontPrivate::get(font)->fontEngine) |
64 | if (fe->hasUnreliableGlyphOutline() || !fe->isSmoothlyScalable) |
65 | preferredRenderType = QSGTextNode::NativeRendering; |
66 | } |
67 | |
68 | if (preferredRenderType == NativeRendering) |
69 | m_containsUnscalableGlyphs = true; |
70 | |
71 | QSGGlyphNode *node = m_renderContext->sceneGraphContext()->createGlyphNode(rc: m_renderContext, |
72 | renderType: preferredRenderType, |
73 | renderTypeQuality: m_renderTypeQuality); |
74 | node->setGlyphs(position: position + QPointF(0, glyphs.rawFont().ascent()), glyphs); |
75 | node->setStyle(style); |
76 | node->setStyleColor(styleColor); |
77 | node->setColor(color); |
78 | node->update(); |
79 | |
80 | /* We flag the geometry as static, but we never call markVertexDataDirty |
81 | or markIndexDataDirty on them. This is because all text nodes are |
82 | discarded when a change occurs. If we start appending/removing from |
83 | existing geometry, then we also need to start marking the geometry as |
84 | dirty. |
85 | */ |
86 | node->geometry()->setIndexDataPattern(QSGGeometry::StaticPattern); |
87 | node->geometry()->setVertexDataPattern(QSGGeometry::StaticPattern); |
88 | |
89 | if (parentNode == nullptr) |
90 | parentNode = this; |
91 | parentNode->appendChildNode(node); |
92 | |
93 | if (style == QQuickText::Outline && color.alpha() > 0 && styleColor != color) { |
94 | QSGGlyphNode *fillNode = m_renderContext->sceneGraphContext()->createGlyphNode(rc: m_renderContext, |
95 | renderType: preferredRenderType, |
96 | renderTypeQuality: m_renderTypeQuality); |
97 | fillNode->setGlyphs(position: position + QPointF(0, glyphs.rawFont().ascent()), glyphs); |
98 | fillNode->setStyle(QQuickText::Normal); |
99 | fillNode->setPreferredAntialiasingMode(QSGGlyphNode::GrayAntialiasing); |
100 | fillNode->setColor(color); |
101 | fillNode->update(); |
102 | |
103 | fillNode->geometry()->setIndexDataPattern(QSGGeometry::StaticPattern); |
104 | fillNode->geometry()->setVertexDataPattern(QSGGeometry::StaticPattern); |
105 | |
106 | parentNode->appendChildNode(node: fillNode); |
107 | fillNode->setRenderOrder(node->renderOrder() + 1); |
108 | } |
109 | |
110 | return node; |
111 | } |
112 | |
113 | void QSGInternalTextNode::setCursor(const QRectF &rect, const QColor &color) |
114 | { |
115 | if (m_cursorNode != nullptr) |
116 | delete m_cursorNode; |
117 | |
118 | m_cursorNode = m_renderContext->sceneGraphContext()->createInternalRectangleNode(rect, c: color); |
119 | appendChildNode(node: m_cursorNode); |
120 | } |
121 | |
122 | void QSGInternalTextNode::clearCursor() |
123 | { |
124 | if (m_cursorNode) |
125 | removeChildNode(node: m_cursorNode); |
126 | delete m_cursorNode; |
127 | m_cursorNode = nullptr; |
128 | } |
129 | |
130 | void QSGInternalTextNode::addDecorationNode(const QRectF &rect, const QColor &color) |
131 | { |
132 | addRectangleNode(rect, color); |
133 | } |
134 | |
135 | void QSGInternalTextNode::addRectangleNode(const QRectF &rect, const QColor &color) |
136 | { |
137 | appendChildNode(node: m_renderContext->sceneGraphContext()->createInternalRectangleNode(rect, c: color)); |
138 | } |
139 | |
140 | void QSGInternalTextNode::addImage(const QRectF &rect, const QImage &image) |
141 | { |
142 | QSGInternalImageNode *node = m_renderContext->sceneGraphContext()->createInternalImageNode(renderContext: m_renderContext); |
143 | QSGTexture *texture = m_renderContext->createTexture(image); |
144 | texture->setFiltering(m_filtering); |
145 | m_textures.append(t: texture); |
146 | node->setTargetRect(rect); |
147 | node->setInnerTargetRect(rect); |
148 | node->setTexture(texture); |
149 | node->setFiltering(m_filtering); |
150 | appendChildNode(node); |
151 | node->update(); |
152 | } |
153 | |
154 | void QSGInternalTextNode::doAddTextDocument(QPointF position, QTextDocument *textDocument, |
155 | int selectionStart, int selectionEnd) |
156 | { |
157 | QQuickTextNodeEngine engine; |
158 | engine.setTextColor(m_color); |
159 | engine.setSelectedTextColor(m_selectionTextColor); |
160 | engine.setSelectionColor(m_selectionColor); |
161 | engine.setAnchorColor(m_linkColor); |
162 | engine.setPosition(position); |
163 | |
164 | QList<QTextFrame *> frames; |
165 | frames.append(t: textDocument->rootFrame()); |
166 | while (!frames.isEmpty()) { |
167 | QTextFrame *textFrame = frames.takeFirst(); |
168 | frames.append(other: textFrame->childFrames()); |
169 | |
170 | engine.addFrameDecorations(document: textDocument, frame: textFrame); |
171 | |
172 | if (textFrame->firstPosition() > textFrame->lastPosition() |
173 | && textFrame->frameFormat().position() != QTextFrameFormat::InFlow) { |
174 | const int pos = textFrame->firstPosition() - 1; |
175 | auto *a = static_cast<QtPrivate::ProtectedLayoutAccessor *>(textDocument->documentLayout()); |
176 | QTextCharFormat format = a->formatAccessor(pos); |
177 | QRectF rect = a->frameBoundingRect(frame: textFrame); |
178 | |
179 | QTextBlock block = textFrame->firstCursorPosition().block(); |
180 | engine.setCurrentLine(block.layout()->lineForTextPosition(pos: pos - block.position())); |
181 | engine.addTextObject(block, position: rect.topLeft(), format, selectionState: QQuickTextNodeEngine::Unselected, textDocument, |
182 | pos, layoutPosition: textFrame->frameFormat().position()); |
183 | } else { |
184 | QTextFrame::iterator it = textFrame->begin(); |
185 | |
186 | while (!it.atEnd()) { |
187 | Q_ASSERT(!engine.currentLine().isValid()); |
188 | |
189 | QTextBlock block = it.currentBlock(); |
190 | engine.addTextBlock(textDocument, block, position, textColor: m_color, anchorColor: m_linkColor, selectionStart, selectionEnd, |
191 | viewport: (textDocument->characterCount() > QQuickTextPrivate::largeTextSizeThreshold ? |
192 | m_viewport : QRectF())); |
193 | ++it; |
194 | } |
195 | } |
196 | } |
197 | |
198 | engine.addToSceneGraph(parent: this, style: QQuickText::TextStyle(m_textStyle), styleColor: m_styleColor); |
199 | } |
200 | |
201 | void QSGInternalTextNode::doAddTextLayout(QPointF position, QTextLayout *textLayout, |
202 | int selectionStart, int selectionEnd, |
203 | int lineStart, int lineCount) |
204 | { |
205 | QQuickTextNodeEngine engine; |
206 | engine.setTextColor(m_color); |
207 | engine.setSelectedTextColor(m_selectionTextColor); |
208 | engine.setSelectionColor(m_selectionColor); |
209 | engine.setAnchorColor(m_linkColor); |
210 | engine.setPosition(position); |
211 | |
212 | #if QT_CONFIG(im) |
213 | int preeditLength = textLayout->preeditAreaText().size(); |
214 | int preeditPosition = textLayout->preeditAreaPosition(); |
215 | #endif |
216 | |
217 | QVarLengthArray<QTextLayout::FormatRange> colorChanges; |
218 | engine.mergeFormats(textLayout, mergedFormats: &colorChanges); |
219 | |
220 | lineCount = lineCount >= 0 |
221 | ? qMin(a: lineStart + lineCount, b: textLayout->lineCount()) |
222 | : textLayout->lineCount(); |
223 | |
224 | bool inViewport = false; |
225 | for (int i=lineStart; i<lineCount; ++i) { |
226 | QTextLine line = textLayout->lineAt(i); |
227 | |
228 | int start = line.textStart(); |
229 | int length = line.textLength(); |
230 | int end = start + length; |
231 | |
232 | #if QT_CONFIG(im) |
233 | if (preeditPosition >= 0 |
234 | && preeditPosition >= start |
235 | && preeditPosition < end) { |
236 | end += preeditLength; |
237 | } |
238 | #endif |
239 | // If there's a lot of text, insert only the range of lines that can possibly be visible within the viewport. |
240 | if (m_viewport.isNull() || (line.y() + line.height() > m_viewport.top() && line.y() < m_viewport.bottom())) { |
241 | if (!inViewport && !m_viewport.isNull()) { |
242 | m_firstLineInViewport = i; |
243 | qCDebug(lcVP) << "first line in viewport" << i << "@" << line.y(); |
244 | } |
245 | inViewport = true; |
246 | engine.setCurrentLine(line); |
247 | engine.addGlyphsForRanges(ranges: colorChanges, start, end, selectionStart, selectionEnd); |
248 | } else if (inViewport) { |
249 | Q_ASSERT(!m_viewport.isNull()); |
250 | m_firstLinePastViewport = i; |
251 | qCDebug(lcVP) << "first omitted line past bottom of viewport" << i << "@" << line.y(); |
252 | break; // went past the bottom of the viewport, so we're done |
253 | } |
254 | } |
255 | |
256 | engine.addToSceneGraph(parent: this, style: QQuickText::TextStyle(m_textStyle), styleColor: m_styleColor); |
257 | } |
258 | |
259 | void QSGInternalTextNode::clear() |
260 | { |
261 | while (firstChild() != nullptr) |
262 | delete firstChild(); |
263 | m_cursorNode = nullptr; |
264 | qDeleteAll(c: m_textures); |
265 | m_textures.clear(); |
266 | } |
267 | |
268 | QT_END_NAMESPACE |
269 | |