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