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