| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qstatictext.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the test suite of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 24 | ** | - | ||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 35 | ** | - | ||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 37 | ** | - | ||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | #include "qstatictext.h" | - | ||||||||||||
| 41 | #include "qstatictext_p.h" | - | ||||||||||||
| 42 | #include <private/qtextengine_p.h> | - | ||||||||||||
| 43 | #include <private/qfontengine_p.h> | - | ||||||||||||
| 44 | #include <qabstracttextdocumentlayout.h> | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 47 | - | |||||||||||||
| 48 | QStaticTextUserData::~QStaticTextUserData() | - | ||||||||||||
| 49 | { | - | ||||||||||||
| 50 | } | - | ||||||||||||
| 51 | - | |||||||||||||
| 52 | /*! | - | ||||||||||||
| 53 | \class QStaticText | - | ||||||||||||
| 54 | \brief The QStaticText class enables optimized drawing of text when the text and its layout | - | ||||||||||||
| 55 | is updated rarely. | - | ||||||||||||
| 56 | \since 4.7 | - | ||||||||||||
| 57 | \inmodule QtGui | - | ||||||||||||
| 58 | - | |||||||||||||
| 59 | \ingroup multimedia | - | ||||||||||||
| 60 | \ingroup text | - | ||||||||||||
| 61 | \ingroup shared | - | ||||||||||||
| 62 | - | |||||||||||||
| 63 | QStaticText provides a way to cache layout data for a block of text so that it can be drawn | - | ||||||||||||
| 64 | more efficiently than by using QPainter::drawText() in which the layout information is | - | ||||||||||||
| 65 | recalculated with every call. | - | ||||||||||||
| 66 | - | |||||||||||||
| 67 | The class primarily provides an optimization for cases where the text, its font and the | - | ||||||||||||
| 68 | transformations on the painter are static over several paint events. If the text or its layout | - | ||||||||||||
| 69 | is changed for every iteration, QPainter::drawText() is the more efficient alternative, since | - | ||||||||||||
| 70 | the static text's layout would have to be recalculated to take the new state into consideration. | - | ||||||||||||
| 71 | - | |||||||||||||
| 72 | Translating the painter will not cause the layout of the text to be recalculated, but will cause | - | ||||||||||||
| 73 | a very small performance impact on drawStaticText(). Altering any other parts of the painter's | - | ||||||||||||
| 74 | transformation or the painter's font will cause the layout of the static text to be | - | ||||||||||||
| 75 | recalculated. This should be avoided as often as possible to maximize the performance | - | ||||||||||||
| 76 | benefit of using QStaticText. | - | ||||||||||||
| 77 | - | |||||||||||||
| 78 | In addition, only affine transformations are supported by drawStaticText(). Calling | - | ||||||||||||
| 79 | drawStaticText() on a projected painter will perform slightly worse than using the regular | - | ||||||||||||
| 80 | drawText() call, so this should be avoided. | - | ||||||||||||
| 81 | - | |||||||||||||
| 82 | \code | - | ||||||||||||
| 83 | class MyWidget: public QWidget | - | ||||||||||||
| 84 | { | - | ||||||||||||
| 85 | public: | - | ||||||||||||
| 86 | MyWidget(QWidget *parent = 0) : QWidget(parent), m_staticText("This is static text") | - | ||||||||||||
| 87 | - | |||||||||||||
| 88 | protected: | - | ||||||||||||
| 89 | void paintEvent(QPaintEvent *) | - | ||||||||||||
| 90 | { | - | ||||||||||||
| 91 | QPainter painter(this); | - | ||||||||||||
| 92 | painter.drawStaticText(0, 0, m_staticText); | - | ||||||||||||
| 93 | } | - | ||||||||||||
| 94 | - | |||||||||||||
| 95 | private: | - | ||||||||||||
| 96 | QStaticText m_staticText; | - | ||||||||||||
| 97 | }; | - | ||||||||||||
| 98 | \endcode | - | ||||||||||||
| 99 | - | |||||||||||||
| 100 | The QStaticText class can be used to mimic the behavior of QPainter::drawText() to a specific | - | ||||||||||||
| 101 | point with no boundaries, and also when QPainter::drawText() is called with a bounding | - | ||||||||||||
| 102 | rectangle. | - | ||||||||||||
| 103 | - | |||||||||||||
| 104 | If a bounding rectangle is not required, create a QStaticText object without setting a preferred | - | ||||||||||||
| 105 | text width. The text will then occupy a single line. | - | ||||||||||||
| 106 | - | |||||||||||||
| 107 | If you set a text width on the QStaticText object, this will bound the text. The text will | - | ||||||||||||
| 108 | be formatted so that no line exceeds the given width. The text width set for QStaticText will | - | ||||||||||||
| 109 | not automatically be used for clipping. To achieve clipping in addition to line breaks, use | - | ||||||||||||
| 110 | QPainter::setClipRect(). The position of the text is decided by the argument passed to | - | ||||||||||||
| 111 | QPainter::drawStaticText() and can change from call to call with a minimal impact on | - | ||||||||||||
| 112 | performance. | - | ||||||||||||
| 113 | - | |||||||||||||
| 114 | For extra convenience, it is possible to apply formatting to the text using the HTML subset | - | ||||||||||||
| 115 | supported by QTextDocument. QStaticText will attempt to guess the format of the input text using | - | ||||||||||||
| 116 | Qt::mightBeRichText(), and interpret it as rich text if this function returns \c true. To force | - | ||||||||||||
| 117 | QStaticText to display its contents as either plain text or rich text, use the function | - | ||||||||||||
| 118 | QStaticText::setTextFormat() and pass in, respectively, Qt::PlainText and Qt::RichText. | - | ||||||||||||
| 119 | - | |||||||||||||
| 120 | QStaticText can only represent text, so only HTML tags which alter the layout or appearance of | - | ||||||||||||
| 121 | the text will be respected. Adding an image to the input HTML, for instance, will cause the | - | ||||||||||||
| 122 | image to be included as part of the layout, affecting the positions of the text glyphs, but it | - | ||||||||||||
| 123 | will not be displayed. The result will be an empty area the size of the image in the output. | - | ||||||||||||
| 124 | Similarly, using tables will cause the text to be laid out in table format, but the borders | - | ||||||||||||
| 125 | will not be drawn. | - | ||||||||||||
| 126 | - | |||||||||||||
| 127 | If it's the first time the static text is drawn, or if the static text, or the painter's font | - | ||||||||||||
| 128 | has been altered since the last time it was drawn, the text's layout has to be | - | ||||||||||||
| 129 | recalculated. On some paint engines, changing the matrix of the painter will also cause the | - | ||||||||||||
| 130 | layout to be recalculated. In particular, this will happen for any engine except for the | - | ||||||||||||
| 131 | OpenGL2 paint engine. Recalculating the layout will impose an overhead on the | - | ||||||||||||
| 132 | QPainter::drawStaticText() call where it occurs. To avoid this overhead in the paint event, you | - | ||||||||||||
| 133 | can call prepare() ahead of time to ensure that the layout is calculated. | - | ||||||||||||
| 134 | - | |||||||||||||
| 135 | \sa QPainter::drawText(), QPainter::drawStaticText(), QTextLayout, QTextDocument | - | ||||||||||||
| 136 | */ | - | ||||||||||||
| 137 | - | |||||||||||||
| 138 | /*! | - | ||||||||||||
| 139 | \enum QStaticText::PerformanceHint | - | ||||||||||||
| 140 | - | |||||||||||||
| 141 | This enum the different performance hints that can be set on the QStaticText. These hints | - | ||||||||||||
| 142 | can be used to indicate that the QStaticText should use additional caches, if possible, | - | ||||||||||||
| 143 | to improve performance at the expense of memory. In particular, setting the performance hint | - | ||||||||||||
| 144 | AggressiveCaching on the QStaticText will improve performance when using the OpenGL graphics | - | ||||||||||||
| 145 | system or when drawing to a QOpenGLWidget. | - | ||||||||||||
| 146 | - | |||||||||||||
| 147 | \value ModerateCaching Do basic caching for high performance at a low memory cost. | - | ||||||||||||
| 148 | \value AggressiveCaching Use additional caching when available. This may improve performance | - | ||||||||||||
| 149 | at a higher memory cost. | - | ||||||||||||
| 150 | */ | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | /*! | - | ||||||||||||
| 153 | Constructs an empty QStaticText | - | ||||||||||||
| 154 | */ | - | ||||||||||||
| 155 | QStaticText::QStaticText() | - | ||||||||||||
| 156 | : data(new QStaticTextPrivate) | - | ||||||||||||
| 157 | { | - | ||||||||||||
| 158 | } never executed: end of block | 0 | ||||||||||||
| 159 | - | |||||||||||||
| 160 | /*! | - | ||||||||||||
| 161 | Constructs a QStaticText object with the given \a text. | - | ||||||||||||
| 162 | */ | - | ||||||||||||
| 163 | QStaticText::QStaticText(const QString &text) | - | ||||||||||||
| 164 | : data(new QStaticTextPrivate) | - | ||||||||||||
| 165 | { | - | ||||||||||||
| 166 | data->text = text; | - | ||||||||||||
| 167 | data->invalidate(); | - | ||||||||||||
| 168 | } never executed: end of block | 0 | ||||||||||||
| 169 | - | |||||||||||||
| 170 | /*! | - | ||||||||||||
| 171 | Constructs a QStaticText object which is a copy of \a other. | - | ||||||||||||
| 172 | */ | - | ||||||||||||
| 173 | QStaticText::QStaticText(const QStaticText &other) | - | ||||||||||||
| 174 | { | - | ||||||||||||
| 175 | data = other.data; | - | ||||||||||||
| 176 | } never executed: end of block | 0 | ||||||||||||
| 177 | - | |||||||||||||
| 178 | /*! | - | ||||||||||||
| 179 | Destroys the QStaticText. | - | ||||||||||||
| 180 | */ | - | ||||||||||||
| 181 | QStaticText::~QStaticText() | - | ||||||||||||
| 182 | { | - | ||||||||||||
| 183 | Q_ASSERT(!data || data->ref.load() >= 1); | - | ||||||||||||
| 184 | } never executed: end of block | 0 | ||||||||||||
| 185 | - | |||||||||||||
| 186 | /*! | - | ||||||||||||
| 187 | \internal | - | ||||||||||||
| 188 | */ | - | ||||||||||||
| 189 | void QStaticText::detach() | - | ||||||||||||
| 190 | { | - | ||||||||||||
| 191 | if (data->ref.load() != 1)
| 0 | ||||||||||||
| 192 | data.detach(); never executed: data.detach(); | 0 | ||||||||||||
| 193 | } never executed: end of block | 0 | ||||||||||||
| 194 | - | |||||||||||||
| 195 | /*! | - | ||||||||||||
| 196 | Prepares the QStaticText object for being painted with the given \a matrix and the given \a font | - | ||||||||||||
| 197 | to avoid overhead when the actual drawStaticText() call is made. | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | When drawStaticText() is called, the layout of the QStaticText will be recalculated if any part | - | ||||||||||||
| 200 | of the QStaticText object has changed since the last time it was drawn. It will also be | - | ||||||||||||
| 201 | recalculated if the painter's font is not the same as when the QStaticText was last drawn, or, | - | ||||||||||||
| 202 | on any other paint engine than the OpenGL2 engine, if the painter's matrix has been altered | - | ||||||||||||
| 203 | since the static text was last drawn. | - | ||||||||||||
| 204 | - | |||||||||||||
| 205 | To avoid the overhead of creating the layout the first time you draw the QStaticText after | - | ||||||||||||
| 206 | making changes, you can use the prepare() function and pass in the \a matrix and \a font you | - | ||||||||||||
| 207 | expect to use when drawing the text. | - | ||||||||||||
| 208 | - | |||||||||||||
| 209 | \sa QPainter::setFont(), QPainter::setMatrix() | - | ||||||||||||
| 210 | */ | - | ||||||||||||
| 211 | void QStaticText::prepare(const QTransform &matrix, const QFont &font) | - | ||||||||||||
| 212 | { | - | ||||||||||||
| 213 | data->matrix = matrix; | - | ||||||||||||
| 214 | data->font = font; | - | ||||||||||||
| 215 | data->init(); | - | ||||||||||||
| 216 | } never executed: end of block | 0 | ||||||||||||
| 217 | - | |||||||||||||
| 218 | - | |||||||||||||
| 219 | /*! | - | ||||||||||||
| 220 | Assigns \a other to this QStaticText. | - | ||||||||||||
| 221 | */ | - | ||||||||||||
| 222 | QStaticText &QStaticText::operator=(const QStaticText &other) | - | ||||||||||||
| 223 | { | - | ||||||||||||
| 224 | data = other.data; | - | ||||||||||||
| 225 | return *this; never executed: return *this; | 0 | ||||||||||||
| 226 | } | - | ||||||||||||
| 227 | - | |||||||||||||
| 228 | /*! | - | ||||||||||||
| 229 | \fn void QStaticText::swap(QStaticText &other) | - | ||||||||||||
| 230 | \since 5.0 | - | ||||||||||||
| 231 | - | |||||||||||||
| 232 | Swaps this static text instance with \a other. This function is | - | ||||||||||||
| 233 | very fast and never fails. | - | ||||||||||||
| 234 | */ | - | ||||||||||||
| 235 | - | |||||||||||||
| 236 | /*! | - | ||||||||||||
| 237 | Compares \a other to this QStaticText. Returns \c true if the texts, fonts and text widths | - | ||||||||||||
| 238 | are equal. | - | ||||||||||||
| 239 | */ | - | ||||||||||||
| 240 | bool QStaticText::operator==(const QStaticText &other) const | - | ||||||||||||
| 241 | { | - | ||||||||||||
| 242 | return (data == other.data never executed: return (data == other.data || (data->text == other.data->text && data->font == other.data->font && data->textWidth == other.data->textWidth)); | 0 | ||||||||||||
| 243 | || (data->text == other.data->text never executed: return (data == other.data || (data->text == other.data->text && data->font == other.data->font && data->textWidth == other.data->textWidth)); | 0 | ||||||||||||
| 244 | && data->font == other.data->font never executed: return (data == other.data || (data->text == other.data->text && data->font == other.data->font && data->textWidth == other.data->textWidth)); | 0 | ||||||||||||
| 245 | && data->textWidth == other.data->textWidth)); never executed: return (data == other.data || (data->text == other.data->text && data->font == other.data->font && data->textWidth == other.data->textWidth)); | 0 | ||||||||||||
| 246 | } | - | ||||||||||||
| 247 | - | |||||||||||||
| 248 | /*! | - | ||||||||||||
| 249 | Compares \a other to this QStaticText. Returns \c true if the texts, fonts or maximum sizes | - | ||||||||||||
| 250 | are different. | - | ||||||||||||
| 251 | */ | - | ||||||||||||
| 252 | bool QStaticText::operator!=(const QStaticText &other) const | - | ||||||||||||
| 253 | { | - | ||||||||||||
| 254 | return !(*this == other); never executed: return !(*this == other); | 0 | ||||||||||||
| 255 | } | - | ||||||||||||
| 256 | - | |||||||||||||
| 257 | /*! | - | ||||||||||||
| 258 | Sets the text of the QStaticText to \a text. | - | ||||||||||||
| 259 | - | |||||||||||||
| 260 | \note This function will cause the layout of the text to require recalculation. | - | ||||||||||||
| 261 | - | |||||||||||||
| 262 | \sa text() | - | ||||||||||||
| 263 | */ | - | ||||||||||||
| 264 | void QStaticText::setText(const QString &text) | - | ||||||||||||
| 265 | { | - | ||||||||||||
| 266 | detach(); | - | ||||||||||||
| 267 | data->text = text; | - | ||||||||||||
| 268 | data->invalidate(); | - | ||||||||||||
| 269 | } never executed: end of block | 0 | ||||||||||||
| 270 | - | |||||||||||||
| 271 | /*! | - | ||||||||||||
| 272 | Sets the text format of the QStaticText to \a textFormat. If \a textFormat is set to | - | ||||||||||||
| 273 | Qt::AutoText (the default), the format of the text will try to be determined using the | - | ||||||||||||
| 274 | function Qt::mightBeRichText(). If the text format is Qt::PlainText, then the text will be | - | ||||||||||||
| 275 | displayed as is, whereas it will be interpreted as HTML if the format is Qt::RichText. HTML tags | - | ||||||||||||
| 276 | that alter the font of the text, its color, or its layout are supported by QStaticText. | - | ||||||||||||
| 277 | - | |||||||||||||
| 278 | \note This function will cause the layout of the text to require recalculation. | - | ||||||||||||
| 279 | - | |||||||||||||
| 280 | \sa textFormat(), setText(), text() | - | ||||||||||||
| 281 | */ | - | ||||||||||||
| 282 | void QStaticText::setTextFormat(Qt::TextFormat textFormat) | - | ||||||||||||
| 283 | { | - | ||||||||||||
| 284 | detach(); | - | ||||||||||||
| 285 | data->textFormat = textFormat; | - | ||||||||||||
| 286 | data->invalidate(); | - | ||||||||||||
| 287 | } never executed: end of block | 0 | ||||||||||||
| 288 | - | |||||||||||||
| 289 | /*! | - | ||||||||||||
| 290 | Returns the text format of the QStaticText. | - | ||||||||||||
| 291 | - | |||||||||||||
| 292 | \sa setTextFormat(), setText(), text() | - | ||||||||||||
| 293 | */ | - | ||||||||||||
| 294 | Qt::TextFormat QStaticText::textFormat() const | - | ||||||||||||
| 295 | { | - | ||||||||||||
| 296 | return Qt::TextFormat(data->textFormat); never executed: return Qt::TextFormat(data->textFormat); | 0 | ||||||||||||
| 297 | } | - | ||||||||||||
| 298 | - | |||||||||||||
| 299 | /*! | - | ||||||||||||
| 300 | Returns the text of the QStaticText. | - | ||||||||||||
| 301 | - | |||||||||||||
| 302 | \sa setText() | - | ||||||||||||
| 303 | */ | - | ||||||||||||
| 304 | QString QStaticText::text() const | - | ||||||||||||
| 305 | { | - | ||||||||||||
| 306 | return data->text; never executed: return data->text; | 0 | ||||||||||||
| 307 | } | - | ||||||||||||
| 308 | - | |||||||||||||
| 309 | /*! | - | ||||||||||||
| 310 | Sets the performance hint of the QStaticText according to the \a | - | ||||||||||||
| 311 | performanceHint provided. The \a performanceHint is used to | - | ||||||||||||
| 312 | customize how much caching is done internally to improve | - | ||||||||||||
| 313 | performance. | - | ||||||||||||
| 314 | - | |||||||||||||
| 315 | The default is QStaticText::ModerateCaching. | - | ||||||||||||
| 316 | - | |||||||||||||
| 317 | \note This function will cause the layout of the text to require recalculation. | - | ||||||||||||
| 318 | - | |||||||||||||
| 319 | \sa performanceHint() | - | ||||||||||||
| 320 | */ | - | ||||||||||||
| 321 | void QStaticText::setPerformanceHint(PerformanceHint performanceHint) | - | ||||||||||||
| 322 | { | - | ||||||||||||
| 323 | if ((performanceHint == ModerateCaching && !data->useBackendOptimizations)
| 0 | ||||||||||||
| 324 | || (performanceHint == AggressiveCaching && data->useBackendOptimizations)) {
| 0 | ||||||||||||
| 325 | return; never executed: return; | 0 | ||||||||||||
| 326 | } | - | ||||||||||||
| 327 | detach(); | - | ||||||||||||
| 328 | data->useBackendOptimizations = (performanceHint == AggressiveCaching); | - | ||||||||||||
| 329 | data->invalidate(); | - | ||||||||||||
| 330 | } never executed: end of block | 0 | ||||||||||||
| 331 | - | |||||||||||||
| 332 | /*! | - | ||||||||||||
| 333 | Returns which performance hint is set for the QStaticText. | - | ||||||||||||
| 334 | - | |||||||||||||
| 335 | \sa setPerformanceHint() | - | ||||||||||||
| 336 | */ | - | ||||||||||||
| 337 | QStaticText::PerformanceHint QStaticText::performanceHint() const | - | ||||||||||||
| 338 | { | - | ||||||||||||
| 339 | return data->useBackendOptimizations ? AggressiveCaching : ModerateCaching; never executed: return data->useBackendOptimizations ? AggressiveCaching : ModerateCaching; | 0 | ||||||||||||
| 340 | } | - | ||||||||||||
| 341 | - | |||||||||||||
| 342 | /*! | - | ||||||||||||
| 343 | Sets the text option structure that controls the layout process to the given \a textOption. | - | ||||||||||||
| 344 | - | |||||||||||||
| 345 | \sa textOption() | - | ||||||||||||
| 346 | */ | - | ||||||||||||
| 347 | void QStaticText::setTextOption(const QTextOption &textOption) | - | ||||||||||||
| 348 | { | - | ||||||||||||
| 349 | detach(); | - | ||||||||||||
| 350 | data->textOption = textOption; | - | ||||||||||||
| 351 | data->invalidate(); | - | ||||||||||||
| 352 | } never executed: end of block | 0 | ||||||||||||
| 353 | - | |||||||||||||
| 354 | /*! | - | ||||||||||||
| 355 | Returns the current text option used to control the layout process. | - | ||||||||||||
| 356 | */ | - | ||||||||||||
| 357 | QTextOption QStaticText::textOption() const | - | ||||||||||||
| 358 | { | - | ||||||||||||
| 359 | return data->textOption; never executed: return data->textOption; | 0 | ||||||||||||
| 360 | } | - | ||||||||||||
| 361 | - | |||||||||||||
| 362 | /*! | - | ||||||||||||
| 363 | Sets the preferred width for this QStaticText. If the text is wider than the specified width, | - | ||||||||||||
| 364 | it will be broken into multiple lines and grow vertically. If the text cannot be split into | - | ||||||||||||
| 365 | multiple lines, it will be larger than the specified \a textWidth. | - | ||||||||||||
| 366 | - | |||||||||||||
| 367 | Setting the preferred text width to a negative number will cause the text to be unbounded. | - | ||||||||||||
| 368 | - | |||||||||||||
| 369 | Use size() to get the actual size of the text. | - | ||||||||||||
| 370 | - | |||||||||||||
| 371 | \note This function will cause the layout of the text to require recalculation. | - | ||||||||||||
| 372 | - | |||||||||||||
| 373 | \sa textWidth(), size() | - | ||||||||||||
| 374 | */ | - | ||||||||||||
| 375 | void QStaticText::setTextWidth(qreal textWidth) | - | ||||||||||||
| 376 | { | - | ||||||||||||
| 377 | detach(); | - | ||||||||||||
| 378 | data->textWidth = textWidth; | - | ||||||||||||
| 379 | data->invalidate(); | - | ||||||||||||
| 380 | } never executed: end of block | 0 | ||||||||||||
| 381 | - | |||||||||||||
| 382 | /*! | - | ||||||||||||
| 383 | Returns the preferred width for this QStaticText. | - | ||||||||||||
| 384 | - | |||||||||||||
| 385 | \sa setTextWidth() | - | ||||||||||||
| 386 | */ | - | ||||||||||||
| 387 | qreal QStaticText::textWidth() const | - | ||||||||||||
| 388 | { | - | ||||||||||||
| 389 | return data->textWidth; never executed: return data->textWidth; | 0 | ||||||||||||
| 390 | } | - | ||||||||||||
| 391 | - | |||||||||||||
| 392 | /*! | - | ||||||||||||
| 393 | Returns the size of the bounding rect for this QStaticText. | - | ||||||||||||
| 394 | - | |||||||||||||
| 395 | \sa textWidth() | - | ||||||||||||
| 396 | */ | - | ||||||||||||
| 397 | QSizeF QStaticText::size() const | - | ||||||||||||
| 398 | { | - | ||||||||||||
| 399 | if (data->needsRelayout)
| 0 | ||||||||||||
| 400 | data->init(); never executed: data->init(); | 0 | ||||||||||||
| 401 | return data->actualSize; never executed: return data->actualSize; | 0 | ||||||||||||
| 402 | } | - | ||||||||||||
| 403 | - | |||||||||||||
| 404 | QStaticTextPrivate::QStaticTextPrivate() | - | ||||||||||||
| 405 | : textWidth(-1.0), items(0), itemCount(0), glyphPool(0), positionPool(0), | - | ||||||||||||
| 406 | needsRelayout(true), useBackendOptimizations(false), textFormat(Qt::AutoText), | - | ||||||||||||
| 407 | untransformedCoordinates(false) | - | ||||||||||||
| 408 | { | - | ||||||||||||
| 409 | } never executed: end of block | 0 | ||||||||||||
| 410 | - | |||||||||||||
| 411 | QStaticTextPrivate::QStaticTextPrivate(const QStaticTextPrivate &other) | - | ||||||||||||
| 412 | : text(other.text), font(other.font), textWidth(other.textWidth), matrix(other.matrix), | - | ||||||||||||
| 413 | items(0), itemCount(0), glyphPool(0), positionPool(0), textOption(other.textOption), | - | ||||||||||||
| 414 | needsRelayout(true), useBackendOptimizations(other.useBackendOptimizations), | - | ||||||||||||
| 415 | textFormat(other.textFormat), untransformedCoordinates(other.untransformedCoordinates) | - | ||||||||||||
| 416 | { | - | ||||||||||||
| 417 | } never executed: end of block | 0 | ||||||||||||
| 418 | - | |||||||||||||
| 419 | QStaticTextPrivate::~QStaticTextPrivate() | - | ||||||||||||
| 420 | { | - | ||||||||||||
| 421 | delete[] items; | - | ||||||||||||
| 422 | delete[] glyphPool; | - | ||||||||||||
| 423 | delete[] positionPool; | - | ||||||||||||
| 424 | } never executed: end of block | 0 | ||||||||||||
| 425 | - | |||||||||||||
| 426 | QStaticTextPrivate *QStaticTextPrivate::get(const QStaticText *q) | - | ||||||||||||
| 427 | { | - | ||||||||||||
| 428 | return q->data.data(); never executed: return q->data.data(); | 0 | ||||||||||||
| 429 | } | - | ||||||||||||
| 430 | - | |||||||||||||
| 431 | namespace { | - | ||||||||||||
| 432 | - | |||||||||||||
| 433 | class DrawTextItemRecorder: public QPaintEngine | - | ||||||||||||
| 434 | { | - | ||||||||||||
| 435 | public: | - | ||||||||||||
| 436 | DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations) | - | ||||||||||||
| 437 | : m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations), | - | ||||||||||||
| 438 | m_untransformedCoordinates(untransformedCoordinates), m_currentColor(Qt::black) | - | ||||||||||||
| 439 | { | - | ||||||||||||
| 440 | } never executed: end of block | 0 | ||||||||||||
| 441 | - | |||||||||||||
| 442 | virtual void updateState(const QPaintEngineState &newState) Q_DECL_OVERRIDE | - | ||||||||||||
| 443 | { | - | ||||||||||||
| 444 | if (newState.state() & QPaintEngine::DirtyPen
| 0 | ||||||||||||
| 445 | && newState.pen().color() != m_currentColor) {
| 0 | ||||||||||||
| 446 | m_dirtyPen = true; | - | ||||||||||||
| 447 | m_currentColor = newState.pen().color(); | - | ||||||||||||
| 448 | } never executed: end of block | 0 | ||||||||||||
| 449 | } never executed: end of block | 0 | ||||||||||||
| 450 | - | |||||||||||||
| 451 | virtual void drawTextItem(const QPointF &position, const QTextItem &textItem) Q_DECL_OVERRIDE | - | ||||||||||||
| 452 | { | - | ||||||||||||
| 453 | const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem); | - | ||||||||||||
| 454 | - | |||||||||||||
| 455 | QStaticTextItem currentItem; | - | ||||||||||||
| 456 | currentItem.setFontEngine(ti.fontEngine); | - | ||||||||||||
| 457 | currentItem.font = ti.font(); | - | ||||||||||||
| 458 | currentItem.glyphOffset = m_glyphs.size(); // Store offset into glyph pool | - | ||||||||||||
| 459 | currentItem.positionOffset = m_glyphs.size(); // Offset into position pool | - | ||||||||||||
| 460 | currentItem.useBackendOptimizations = m_useBackendOptimizations; | - | ||||||||||||
| 461 | if (m_dirtyPen)
| 0 | ||||||||||||
| 462 | currentItem.color = m_currentColor; never executed: currentItem.color = m_currentColor; | 0 | ||||||||||||
| 463 | - | |||||||||||||
| 464 | QTransform matrix = m_untransformedCoordinates ? QTransform() : state->transform();
| 0 | ||||||||||||
| 465 | matrix.translate(position.x(), position.y()); | - | ||||||||||||
| 466 | - | |||||||||||||
| 467 | QVarLengthArray<glyph_t> glyphs; | - | ||||||||||||
| 468 | QVarLengthArray<QFixedPoint> positions; | - | ||||||||||||
| 469 | ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions); | - | ||||||||||||
| 470 | - | |||||||||||||
| 471 | int size = glyphs.size(); | - | ||||||||||||
| 472 | Q_ASSERT(size == positions.size()); | - | ||||||||||||
| 473 | currentItem.numGlyphs = size; | - | ||||||||||||
| 474 | - | |||||||||||||
| 475 | m_glyphs.resize(m_glyphs.size() + size); | - | ||||||||||||
| 476 | m_positions.resize(m_glyphs.size()); | - | ||||||||||||
| 477 | - | |||||||||||||
| 478 | glyph_t *glyphsDestination = m_glyphs.data() + currentItem.glyphOffset; | - | ||||||||||||
| 479 | memcpy(glyphsDestination, glyphs.constData(), sizeof(glyph_t) * currentItem.numGlyphs); | - | ||||||||||||
| 480 | - | |||||||||||||
| 481 | QFixedPoint *positionsDestination = m_positions.data() + currentItem.positionOffset; | - | ||||||||||||
| 482 | memcpy(positionsDestination, positions.constData(), sizeof(QFixedPoint) * currentItem.numGlyphs); | - | ||||||||||||
| 483 | - | |||||||||||||
| 484 | m_items.append(currentItem); | - | ||||||||||||
| 485 | } never executed: end of block | 0 | ||||||||||||
| 486 | - | |||||||||||||
| 487 | virtual void drawPolygon(const QPointF *, int , PolygonDrawMode ) Q_DECL_OVERRIDE | - | ||||||||||||
| 488 | { | - | ||||||||||||
| 489 | /* intentionally empty */ | - | ||||||||||||
| 490 | } | - | ||||||||||||
| 491 | - | |||||||||||||
| 492 | virtual bool begin(QPaintDevice *) Q_DECL_OVERRIDE { return true; } never executed: return true; | 0 | ||||||||||||
| 493 | virtual bool end() Q_DECL_OVERRIDE { return true; } never executed: return true; | 0 | ||||||||||||
| 494 | virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) Q_DECL_OVERRIDE {} | - | ||||||||||||
| 495 | virtual Type type() const Q_DECL_OVERRIDE | - | ||||||||||||
| 496 | { | - | ||||||||||||
| 497 | return User; never executed: return User; | 0 | ||||||||||||
| 498 | } | - | ||||||||||||
| 499 | - | |||||||||||||
| 500 | QVector<QStaticTextItem> items() const | - | ||||||||||||
| 501 | { | - | ||||||||||||
| 502 | return m_items; never executed: return m_items; | 0 | ||||||||||||
| 503 | } | - | ||||||||||||
| 504 | - | |||||||||||||
| 505 | QVector<QFixedPoint> positions() const | - | ||||||||||||
| 506 | { | - | ||||||||||||
| 507 | return m_positions; never executed: return m_positions; | 0 | ||||||||||||
| 508 | } | - | ||||||||||||
| 509 | - | |||||||||||||
| 510 | QVector<glyph_t> glyphs() const | - | ||||||||||||
| 511 | { | - | ||||||||||||
| 512 | return m_glyphs; never executed: return m_glyphs; | 0 | ||||||||||||
| 513 | } | - | ||||||||||||
| 514 | - | |||||||||||||
| 515 | private: | - | ||||||||||||
| 516 | QVector<QStaticTextItem> m_items; | - | ||||||||||||
| 517 | QVector<QFixedPoint> m_positions; | - | ||||||||||||
| 518 | QVector<glyph_t> m_glyphs; | - | ||||||||||||
| 519 | - | |||||||||||||
| 520 | bool m_dirtyPen; | - | ||||||||||||
| 521 | bool m_useBackendOptimizations; | - | ||||||||||||
| 522 | bool m_untransformedCoordinates; | - | ||||||||||||
| 523 | QColor m_currentColor; | - | ||||||||||||
| 524 | }; | - | ||||||||||||
| 525 | - | |||||||||||||
| 526 | class DrawTextItemDevice: public QPaintDevice | - | ||||||||||||
| 527 | { | - | ||||||||||||
| 528 | public: | - | ||||||||||||
| 529 | DrawTextItemDevice(bool untransformedCoordinates, bool useBackendOptimizations) | - | ||||||||||||
| 530 | { | - | ||||||||||||
| 531 | m_paintEngine = new DrawTextItemRecorder(untransformedCoordinates, | - | ||||||||||||
| 532 | useBackendOptimizations); | - | ||||||||||||
| 533 | } never executed: end of block | 0 | ||||||||||||
| 534 | - | |||||||||||||
| 535 | ~DrawTextItemDevice() | - | ||||||||||||
| 536 | { | - | ||||||||||||
| 537 | delete m_paintEngine; | - | ||||||||||||
| 538 | } never executed: end of block | 0 | ||||||||||||
| 539 | - | |||||||||||||
| 540 | int metric(PaintDeviceMetric m) const Q_DECL_OVERRIDE | - | ||||||||||||
| 541 | { | - | ||||||||||||
| 542 | int val; | - | ||||||||||||
| 543 | switch (m) { | - | ||||||||||||
| 544 | case PdmWidth: never executed: case PdmWidth: | 0 | ||||||||||||
| 545 | case PdmHeight: never executed: case PdmHeight: | 0 | ||||||||||||
| 546 | case PdmWidthMM: never executed: case PdmWidthMM: | 0 | ||||||||||||
| 547 | case PdmHeightMM: never executed: case PdmHeightMM: | 0 | ||||||||||||
| 548 | val = 0; | - | ||||||||||||
| 549 | break; never executed: break; | 0 | ||||||||||||
| 550 | case PdmDpiX: never executed: case PdmDpiX: | 0 | ||||||||||||
| 551 | case PdmPhysicalDpiX: never executed: case PdmPhysicalDpiX: | 0 | ||||||||||||
| 552 | val = qt_defaultDpiX(); | - | ||||||||||||
| 553 | break; never executed: break; | 0 | ||||||||||||
| 554 | case PdmDpiY: never executed: case PdmDpiY: | 0 | ||||||||||||
| 555 | case PdmPhysicalDpiY: never executed: case PdmPhysicalDpiY: | 0 | ||||||||||||
| 556 | val = qt_defaultDpiY(); | - | ||||||||||||
| 557 | break; never executed: break; | 0 | ||||||||||||
| 558 | case PdmNumColors: never executed: case PdmNumColors: | 0 | ||||||||||||
| 559 | val = 16777216; | - | ||||||||||||
| 560 | break; never executed: break; | 0 | ||||||||||||
| 561 | case PdmDepth: never executed: case PdmDepth: | 0 | ||||||||||||
| 562 | val = 24; | - | ||||||||||||
| 563 | break; never executed: break; | 0 | ||||||||||||
| 564 | case PdmDevicePixelRatio: never executed: case PdmDevicePixelRatio: | 0 | ||||||||||||
| 565 | val = 1; | - | ||||||||||||
| 566 | break; never executed: break; | 0 | ||||||||||||
| 567 | case PdmDevicePixelRatioScaled: never executed: case PdmDevicePixelRatioScaled: | 0 | ||||||||||||
| 568 | val = devicePixelRatioFScale(); | - | ||||||||||||
| 569 | break; never executed: break; | 0 | ||||||||||||
| 570 | default: never executed: default: | 0 | ||||||||||||
| 571 | val = 0; | - | ||||||||||||
| 572 | qWarning("DrawTextItemDevice::metric: Invalid metric command"); | - | ||||||||||||
| 573 | } never executed: end of block | 0 | ||||||||||||
| 574 | return val; never executed: return val; | 0 | ||||||||||||
| 575 | } | - | ||||||||||||
| 576 | - | |||||||||||||
| 577 | virtual QPaintEngine *paintEngine() const Q_DECL_OVERRIDE | - | ||||||||||||
| 578 | { | - | ||||||||||||
| 579 | return m_paintEngine; never executed: return m_paintEngine; | 0 | ||||||||||||
| 580 | } | - | ||||||||||||
| 581 | - | |||||||||||||
| 582 | QVector<glyph_t> glyphs() const | - | ||||||||||||
| 583 | { | - | ||||||||||||
| 584 | return m_paintEngine->glyphs(); never executed: return m_paintEngine->glyphs(); | 0 | ||||||||||||
| 585 | } | - | ||||||||||||
| 586 | - | |||||||||||||
| 587 | QVector<QFixedPoint> positions() const | - | ||||||||||||
| 588 | { | - | ||||||||||||
| 589 | return m_paintEngine->positions(); never executed: return m_paintEngine->positions(); | 0 | ||||||||||||
| 590 | } | - | ||||||||||||
| 591 | - | |||||||||||||
| 592 | QVector<QStaticTextItem> items() const | - | ||||||||||||
| 593 | { | - | ||||||||||||
| 594 | return m_paintEngine->items(); never executed: return m_paintEngine->items(); | 0 | ||||||||||||
| 595 | } | - | ||||||||||||
| 596 | - | |||||||||||||
| 597 | private: | - | ||||||||||||
| 598 | DrawTextItemRecorder *m_paintEngine; | - | ||||||||||||
| 599 | }; | - | ||||||||||||
| 600 | } | - | ||||||||||||
| 601 | - | |||||||||||||
| 602 | void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p) | - | ||||||||||||
| 603 | { | - | ||||||||||||
| 604 | bool preferRichText = textFormat == Qt::RichText
| 0 | ||||||||||||
| 605 | || (textFormat == Qt::AutoText && Qt::mightBeRichText(text));
| 0 | ||||||||||||
| 606 | - | |||||||||||||
| 607 | if (!preferRichText) {
| 0 | ||||||||||||
| 608 | QTextLayout textLayout; | - | ||||||||||||
| 609 | textLayout.setText(text); | - | ||||||||||||
| 610 | textLayout.setFont(font); | - | ||||||||||||
| 611 | textLayout.setTextOption(textOption); | - | ||||||||||||
| 612 | textLayout.setCacheEnabled(true); | - | ||||||||||||
| 613 | - | |||||||||||||
| 614 | qreal leading = QFontMetricsF(font).leading(); | - | ||||||||||||
| 615 | qreal height = -leading; | - | ||||||||||||
| 616 | - | |||||||||||||
| 617 | textLayout.beginLayout(); | - | ||||||||||||
| 618 | while (1) { | - | ||||||||||||
| 619 | QTextLine line = textLayout.createLine(); | - | ||||||||||||
| 620 | if (!line.isValid())
| 0 | ||||||||||||
| 621 | break; never executed: break; | 0 | ||||||||||||
| 622 | - | |||||||||||||
| 623 | if (textWidth >= 0.0)
| 0 | ||||||||||||
| 624 | line.setLineWidth(textWidth); never executed: line.setLineWidth(textWidth); | 0 | ||||||||||||
| 625 | height += leading; | - | ||||||||||||
| 626 | line.setPosition(QPointF(0.0, height)); | - | ||||||||||||
| 627 | height += line.height(); | - | ||||||||||||
| 628 | } never executed: end of block | 0 | ||||||||||||
| 629 | textLayout.endLayout(); | - | ||||||||||||
| 630 | - | |||||||||||||
| 631 | actualSize = textLayout.boundingRect().size(); | - | ||||||||||||
| 632 | textLayout.draw(p, topLeftPosition); | - | ||||||||||||
| 633 | } else { never executed: end of block | 0 | ||||||||||||
| 634 | QTextDocument document; | - | ||||||||||||
| 635 | #ifndef QT_NO_CSSPARSER | - | ||||||||||||
| 636 | QColor color = p->pen().color(); | - | ||||||||||||
| 637 | document.setDefaultStyleSheet(QString::fromLatin1("body { color: #%1%2%3 }") | - | ||||||||||||
| 638 | .arg(QString::number(color.red(), 16), 2, QLatin1Char('0')) | - | ||||||||||||
| 639 | .arg(QString::number(color.green(), 16), 2, QLatin1Char('0')) | - | ||||||||||||
| 640 | .arg(QString::number(color.blue(), 16), 2, QLatin1Char('0'))); | - | ||||||||||||
| 641 | #endif | - | ||||||||||||
| 642 | document.setDefaultFont(font); | - | ||||||||||||
| 643 | document.setDocumentMargin(0.0); | - | ||||||||||||
| 644 | #ifndef QT_NO_TEXTHTMLPARSER | - | ||||||||||||
| 645 | document.setHtml(text); | - | ||||||||||||
| 646 | #else | - | ||||||||||||
| 647 | document.setPlainText(text); | - | ||||||||||||
| 648 | #endif | - | ||||||||||||
| 649 | if (textWidth >= 0.0)
| 0 | ||||||||||||
| 650 | document.setTextWidth(textWidth); never executed: document.setTextWidth(textWidth); | 0 | ||||||||||||
| 651 | else | - | ||||||||||||
| 652 | document.adjustSize(); never executed: document.adjustSize(); | 0 | ||||||||||||
| 653 | document.setDefaultTextOption(textOption); | - | ||||||||||||
| 654 | - | |||||||||||||
| 655 | p->save(); | - | ||||||||||||
| 656 | p->translate(topLeftPosition); | - | ||||||||||||
| 657 | QAbstractTextDocumentLayout::PaintContext ctx; | - | ||||||||||||
| 658 | ctx.palette.setColor(QPalette::Text, p->pen().color()); | - | ||||||||||||
| 659 | document.documentLayout()->draw(p, ctx); | - | ||||||||||||
| 660 | p->restore(); | - | ||||||||||||
| 661 | - | |||||||||||||
| 662 | if (textWidth >= 0.0)
| 0 | ||||||||||||
| 663 | document.adjustSize(); // Find optimal size never executed: document.adjustSize(); | 0 | ||||||||||||
| 664 | - | |||||||||||||
| 665 | actualSize = document.size(); | - | ||||||||||||
| 666 | } never executed: end of block | 0 | ||||||||||||
| 667 | } | - | ||||||||||||
| 668 | - | |||||||||||||
| 669 | void QStaticTextPrivate::init() | - | ||||||||||||
| 670 | { | - | ||||||||||||
| 671 | delete[] items; | - | ||||||||||||
| 672 | delete[] glyphPool; | - | ||||||||||||
| 673 | delete[] positionPool; | - | ||||||||||||
| 674 | - | |||||||||||||
| 675 | position = QPointF(0, 0); | - | ||||||||||||
| 676 | - | |||||||||||||
| 677 | DrawTextItemDevice device(untransformedCoordinates, useBackendOptimizations); | - | ||||||||||||
| 678 | { | - | ||||||||||||
| 679 | QPainter painter(&device); | - | ||||||||||||
| 680 | painter.setFont(font); | - | ||||||||||||
| 681 | painter.setTransform(matrix); | - | ||||||||||||
| 682 | - | |||||||||||||
| 683 | paintText(QPointF(0, 0), &painter); | - | ||||||||||||
| 684 | } | - | ||||||||||||
| 685 | - | |||||||||||||
| 686 | QVector<QStaticTextItem> deviceItems = device.items(); | - | ||||||||||||
| 687 | QVector<QFixedPoint> positions = device.positions(); | - | ||||||||||||
| 688 | QVector<glyph_t> glyphs = device.glyphs(); | - | ||||||||||||
| 689 | - | |||||||||||||
| 690 | itemCount = deviceItems.size(); | - | ||||||||||||
| 691 | items = new QStaticTextItem[itemCount]; | - | ||||||||||||
| 692 | - | |||||||||||||
| 693 | glyphPool = new glyph_t[glyphs.size()]; | - | ||||||||||||
| 694 | memcpy(glyphPool, glyphs.constData(), glyphs.size() * sizeof(glyph_t)); | - | ||||||||||||
| 695 | - | |||||||||||||
| 696 | positionPool = new QFixedPoint[positions.size()]; | - | ||||||||||||
| 697 | memcpy(positionPool, positions.constData(), positions.size() * sizeof(QFixedPoint)); | - | ||||||||||||
| 698 | - | |||||||||||||
| 699 | for (int i=0; i<itemCount; ++i) {
| 0 | ||||||||||||
| 700 | items[i] = deviceItems.at(i); | - | ||||||||||||
| 701 | - | |||||||||||||
| 702 | items[i].glyphs = glyphPool + items[i].glyphOffset; | - | ||||||||||||
| 703 | items[i].glyphPositions = positionPool + items[i].positionOffset; | - | ||||||||||||
| 704 | } never executed: end of block | 0 | ||||||||||||
| 705 | - | |||||||||||||
| 706 | needsRelayout = false; | - | ||||||||||||
| 707 | } never executed: end of block | 0 | ||||||||||||
| 708 | - | |||||||||||||
| 709 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |