| 1 | // Copyright (C) 2023 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 "qsgtextnode.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | /*! |
| 9 | \class QSGTextNode |
| 10 | |
| 11 | \brief The QSGTextNode class is a class for drawing text layouts and text documents in |
| 12 | the Qt Quick scene graph. |
| 13 | \inmodule QtQuick |
| 14 | \since 6.7 |
| 15 | |
| 16 | QSGTextNode can be useful for creating custom Qt Quick items that require text. It is used |
| 17 | in Qt Quick by the Text, TextEdit and TextInput elements. |
| 18 | |
| 19 | You can create QSGTextNode objects using QQuickWindow::createTextNode(). The addTextLayout() |
| 20 | and addTextDocument() functions provide ways to add text to the QSGTextNode. The text must |
| 21 | already be laid out. |
| 22 | |
| 23 | \note Properties must be set before \l addTextLayout() or \l addTextDocument() are called in |
| 24 | order to have an effect. |
| 25 | |
| 26 | \note The destruction of QSGTextNode has to be managed with care. In particular, since it |
| 27 | references graphics resources, it must be deleted when the Qt Quick scene graph is invalidated. |
| 28 | If the node is part of the graph and has the \c OwnedByParent flag set (which is the default), |
| 29 | this will happen automatically. However, if the \c OwnedByParent flag is cleared and the node is |
| 30 | disposed of manually, care must be taken to do this when the scene graph is invalidated. This |
| 31 | can be done by connecting to the \l{QQuickWindow::sceneGraphInvalidated()} signal, or by |
| 32 | implementing a slot in the QQuickItem subclass which is named \c{invalidateSceneGraph()}. |
| 33 | See also the documentation of QQuickItem for more details. |
| 34 | */ |
| 35 | |
| 36 | /*! |
| 37 | \enum QSGTextNode::TextStyle |
| 38 | |
| 39 | This enum type describes styles that can be applied to text rendering. |
| 40 | |
| 41 | \value Normal The text is drawn without any style applied. |
| 42 | \value Outline The text is drawn with an outline. |
| 43 | \value Raised The text is drawn raised. |
| 44 | \value Sunken The text is drawn sunken. |
| 45 | |
| 46 | \sa setTextStyle(), setStyleColor() |
| 47 | */ |
| 48 | |
| 49 | /*! |
| 50 | \enum QSGTextNode::RenderType |
| 51 | |
| 52 | This enum type describes type of glyph node used for rendering the text. |
| 53 | |
| 54 | \value QtRendering Text is rendered using a scalable distance field for each glyph. |
| 55 | \value NativeRendering Text is rendered using a platform-specific technique. |
| 56 | \value CurveRendering Text is rendered using a curve rasterizer running directly on the |
| 57 | graphics hardware. |
| 58 | |
| 59 | Select \c NativeRendering if you prefer text to look native on the target platform and do |
| 60 | not require advanced features such as transformation of the text. Using such features in |
| 61 | combination with the NativeRendering render type will lend poor and sometimes pixelated |
| 62 | results. |
| 63 | |
| 64 | Both \c Text.QtRendering and \c Text.CurveRendering are hardware-accelerated techniques. |
| 65 | \c QtRendering is the faster of the two, but uses more memory and will exhibit rendering |
| 66 | artifacts at large sizes. \c CurveRendering should be considered as an alternative in cases |
| 67 | where \c QtRendering does not give good visual results or where reducing graphics memory |
| 68 | consumption is a priority. |
| 69 | |
| 70 | \sa setRenderType(), setRenderTypeQuality() |
| 71 | */ |
| 72 | |
| 73 | /*! |
| 74 | \fn void QSGTextNode::setColor(QColor color) |
| 75 | |
| 76 | Sets the main color to use when rendering the text to \a color. |
| 77 | |
| 78 | The default is black: \c QColor(0, 0, 0). |
| 79 | */ |
| 80 | |
| 81 | /*! |
| 82 | \fn QColor QSGTextNode::color() const |
| 83 | |
| 84 | Returns the main color used when rendering the text. |
| 85 | */ |
| 86 | |
| 87 | /*! |
| 88 | \fn void QSGTextNode::setStyleColor(QColor styleColor) |
| 89 | |
| 90 | Sets the style color to use when rendering the text to \a styleColor. |
| 91 | |
| 92 | The default is black: \c QColor(0, 0, 0). |
| 93 | |
| 94 | \sa setTextStyle() |
| 95 | */ |
| 96 | |
| 97 | /*! |
| 98 | \fn QColor QSGTextNode::styleColor() const |
| 99 | |
| 100 | Returns the style color used when rendering the text. |
| 101 | |
| 102 | \sa textStyle() |
| 103 | */ |
| 104 | |
| 105 | /*! |
| 106 | \fn void QSGTextNode::setTextStyle(QSGTextNode::TextStyle textStyle) |
| 107 | |
| 108 | Sets the style of the rendered text to \a textStyle. The default is \c Normal. |
| 109 | |
| 110 | \sa setStyleColor() |
| 111 | */ |
| 112 | |
| 113 | /*! |
| 114 | \fn QSGTextNode::TextStyle QSGTextNode::textStyle() |
| 115 | |
| 116 | Returns the style of the rendered text. |
| 117 | |
| 118 | \sa styleColor() |
| 119 | */ |
| 120 | |
| 121 | /*! |
| 122 | \fn void QSGTextNode::setLinkColor(QColor linkColor) |
| 123 | |
| 124 | Sets the color of or hyperlinks to \a linkColor in the text. |
| 125 | |
| 126 | The default is blue: \c QColor(0, 0, 255). |
| 127 | */ |
| 128 | |
| 129 | /*! |
| 130 | \fn QColor QSGTextNode::linkColor() const |
| 131 | |
| 132 | Returns the color of hyperlinks in the text. |
| 133 | */ |
| 134 | |
| 135 | /*! |
| 136 | \fn void QSGTextNode::setSelectionColor(QColor color) |
| 137 | |
| 138 | Sets the color of the selection background to \a color when any part of the text is |
| 139 | marked as selected. |
| 140 | |
| 141 | The default is dark blue: \c QColor(0, 0, 128). |
| 142 | */ |
| 143 | |
| 144 | /*! |
| 145 | \fn QColor QSGTextNode::selectionColor() const |
| 146 | |
| 147 | Returns the color of the selection background when any part of the text is marked as selected. |
| 148 | */ |
| 149 | |
| 150 | /*! |
| 151 | \fn QColor QSGTextNode::selectionTextColor() const |
| 152 | |
| 153 | Returns the color of the selection text when any part of the text is marked as selected. |
| 154 | */ |
| 155 | |
| 156 | /*! |
| 157 | \fn void QSGTextNode::setSelectionTextColor(QColor selectionTextColor) |
| 158 | |
| 159 | Sets the color of the selection text to \a selectionTextColor when any part of the text is |
| 160 | marked as selected. |
| 161 | |
| 162 | The default is white: \c QColor(255, 255, 255). |
| 163 | */ |
| 164 | |
| 165 | |
| 166 | /*! |
| 167 | \fn void QSGTextNode::setRenderType(RenderType renderType) |
| 168 | |
| 169 | Sets the type of glyph node in use to \a renderType. |
| 170 | |
| 171 | The default is \l QtRendering. |
| 172 | */ |
| 173 | |
| 174 | /*! |
| 175 | \fn QSGTextNode::RenderType QSGTextNode::renderType() const |
| 176 | |
| 177 | Returns the type of glyph node used for rendering the text. |
| 178 | */ |
| 179 | |
| 180 | /*! |
| 181 | \fn void QSGTextNode::setRenderTypeQuality(int renderTypeQuality) |
| 182 | |
| 183 | If the \l renderType() in use supports it, set the quality to use when rendering the text. |
| 184 | When supported, this can be used to trade visual fidelity for execution speed or memory. |
| 185 | |
| 186 | When the \a renderTypeQuality is < 0, the default quality is used. |
| 187 | |
| 188 | The \a renderTypeQuality can be any integer, although limitations imposed by the underlying |
| 189 | graphics hardware may be encountered if extreme values are set. The Qt Quick Text element |
| 190 | operates with the following predefined values: |
| 191 | |
| 192 | \value DefaultRenderTypeQuality -1 (default) |
| 193 | \value LowRenderTypeQuality 26 |
| 194 | \value NormalRenderTypeQuality 52 |
| 195 | \value HighRenderTypeQuality 104 |
| 196 | \value VeryHighRenderTypeQuality 208 |
| 197 | |
| 198 | This value is currently only respected by the QtRendering render type. Setting it changes the |
| 199 | resolution of the distance fields used to represent the glyphs. Setting it above normal will |
| 200 | cause memory consumption to increase, but reduces filtering artifacts on very large text. |
| 201 | |
| 202 | The default is -1. |
| 203 | */ |
| 204 | |
| 205 | /*! |
| 206 | \fn int QSGTextNode::renderTypeQuality() const |
| 207 | |
| 208 | Returns the render type quality of the node. See \l setRenderTypeQuality() for details. |
| 209 | */ |
| 210 | |
| 211 | /*! |
| 212 | \fn void QSGTextNode::setFiltering(QSGTexture::Filtering filtering) |
| 213 | |
| 214 | Sets the sampling mode used when scaling images that are part of the displayed text to |
| 215 | \a filtering. For smoothly scaled images, use \l{QSGTexture::Linear} here. |
| 216 | |
| 217 | The default is \l{QSGTexture::Nearest}. |
| 218 | |
| 219 | \sa filtering() |
| 220 | */ |
| 221 | |
| 222 | /*! |
| 223 | \fn QSGTexture::Filtering QSGTextNode::filtering() const |
| 224 | |
| 225 | Returns the sampling mode used when scaling images that are part of the displayed text. |
| 226 | |
| 227 | \sa setFiltering() |
| 228 | */ |
| 229 | |
| 230 | /*! |
| 231 | \fn void QSGTextNode::setViewport(const QRectF &viewport) |
| 232 | |
| 233 | Sets the bounding rect of the viewport where the text is displayed to \a viewport. Providing |
| 234 | this information makes it possible for the QSGTextNode to optimize which parts of the text |
| 235 | layout or document are included in the scene graph. |
| 236 | |
| 237 | The default is a default-constructed QRectF. For this viewport, all contents will be included |
| 238 | in the graph. |
| 239 | */ |
| 240 | |
| 241 | /*! |
| 242 | \fn QRectF QSGTextNode::viewport() const |
| 243 | |
| 244 | Returns the current viewport set for this QSGTextNode. |
| 245 | */ |
| 246 | |
| 247 | /*! |
| 248 | \fn QSGTextNode::addTextLayout(QPointF position, QTextLayout *layout, int selectionStart = -1, int selectionCount = -1, int lineStart = 0, int lineCount = -1) |
| 249 | |
| 250 | Adds the contents of \a layout to the text node at \a position. If \a selectionStart is >= 0, |
| 251 | then this marks the first character in a selected area of \a selectionCount number of |
| 252 | characters. The selection is represented as a background fill with the \l selectionColor() and |
| 253 | the selected text is rendered in the \l selectionTextColor(). |
| 254 | |
| 255 | For convenience, \a lineStart and \a lineCount can be used to select the range of \l QTextLine |
| 256 | objects to include from the layout. This can be useful, for instance, when creating elided |
| 257 | layouts. If \a lineCount is < 0, then the the node will include the lines from \a lineStart to |
| 258 | the end of the layout. |
| 259 | |
| 260 | This function forwards its arguments to the virtual function doAddTextLayout(). |
| 261 | |
| 262 | \sa clear(), doAddTextLayout() |
| 263 | */ |
| 264 | |
| 265 | /*! |
| 266 | \fn QSGTextNode::addTextDocument(QPointF position, QTextDocument *document, int selectionStart = -1, int selectionCount = -1) |
| 267 | |
| 268 | Adds the contents of \a document to the text node at \a position. If \a selectionStart is >= 0, |
| 269 | then this marks the first character in a selected area of \a selectionCount number of |
| 270 | characters. The selection is represented as a background fill with the \l selectionColor() and |
| 271 | the selected text is rendered in the \l selectionTextColor(). |
| 272 | |
| 273 | This function forwards its arguments to the virtual function doAddTextDocument(). |
| 274 | |
| 275 | \sa clear(), doAddTextDocument() |
| 276 | */ |
| 277 | |
| 278 | /*! |
| 279 | \fn QSGTextNode::doAddTextLayout(QPointF position, QTextLayout *layout, int selectionStart, int selectionCount, int lineStart, int lineCount) |
| 280 | |
| 281 | Virtual function called by addTextLayout(), which converts the contents of \a layout to scene |
| 282 | graph nodes and adds them to the current node at \a position. |
| 283 | |
| 284 | If \a selectionStart is >= 0, then this marks the first character in a selected area of |
| 285 | \a selectionCount number of characters. The selection is represented as a background fill with |
| 286 | the \l selectionColor() and the selected text is rendered in the \l selectionTextColor(). |
| 287 | |
| 288 | For convenience, \a lineStart and \a lineCount can be used to select the range of \l QTextLine |
| 289 | objects to include from the layout. This can be useful, for instance, when creating elided |
| 290 | layouts. If \a lineCount is < 0, then the the node will include the lines from \a lineStart to |
| 291 | the end of the layout. |
| 292 | |
| 293 | \sa clear(), addTextLayout() |
| 294 | */ |
| 295 | |
| 296 | /*! |
| 297 | \fn QSGTextNode::doAddTextDocument(QPointF position, QTextDocument *document, int selectionStart, int selectionCount) |
| 298 | |
| 299 | Virtual function called by addTextDocument(), which converts the contents of \a document to |
| 300 | scene graph nodes and adds them to the current node at \a position. |
| 301 | |
| 302 | If \a selectionStart is >= 0, then this marks the first character in a selected area of |
| 303 | \a selectionCount number of characters. The selection is represented as a background fill with |
| 304 | the \l selectionColor() and the selected text is rendered in the \l selectionTextColor(). |
| 305 | |
| 306 | \sa clear(), addTextDocument() |
| 307 | */ |
| 308 | |
| 309 | /*! |
| 310 | \fn QSGTextNode::clear() |
| 311 | |
| 312 | Clears the contents of the node, deleting nodes and other data that represents the layouts |
| 313 | and documents that have been added to it. |
| 314 | |
| 315 | \sa addTextLayout(), addTextDocument() |
| 316 | */ |
| 317 | |
| 318 | QSGTextNode::~QSGTextNode() = default; |
| 319 | |
| 320 | QT_END_NAMESPACE |
| 321 | |