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 QtDeclarative module 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 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 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file. Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativetextlayout_p.h"
43 #include <private/qstatictext_p.h>
44 #include <private/qfontengine_p.h>
45 #include <private/qtextengine_p.h>
46 #include <private/qpainter_p.h>
47 #include <private/qpaintengineex_p.h>
48
49 QT_BEGIN_NAMESPACE
50
51 class QDeclarativeTextLayoutPrivate
52 {
53 public:
QDeclarativeTextLayoutPrivate()54 QDeclarativeTextLayoutPrivate()
55 : cached(false) {}
56
57 QPointF position;
58
59 bool cached;
60 QVector<QStaticTextItem> items;
61 QVector<QFixedPoint> positions;
62 QVector<glyph_t> glyphs;
63 QVector<QChar> chars;
64 };
65
66 namespace {
67 class DrawTextItemRecorder: public QPaintEngine
68 {
69 public:
DrawTextItemRecorder(bool untransformedCoordinates,bool useBackendOptimizations)70 DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations)
71 : m_inertText(0), m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations),
72 m_untransformedCoordinates(untransformedCoordinates), m_currentColor(Qt::black)
73 {
74 }
75
updateState(const QPaintEngineState & newState)76 virtual void updateState(const QPaintEngineState &newState)
77 {
78 if (newState.state() & QPaintEngine::DirtyPen
79 && newState.pen().color() != m_currentColor) {
80 m_dirtyPen = true;
81 m_currentColor = newState.pen().color();
82 }
83 }
84
drawTextItem(const QPointF & position,const QTextItem & textItem)85 virtual void drawTextItem(const QPointF &position, const QTextItem &textItem)
86 {
87 int glyphOffset = m_inertText->glyphs.size(); // Store offset into glyph pool
88 int positionOffset = m_inertText->glyphs.size(); // Offset into position pool
89 int charOffset = m_inertText->chars.size();
90
91 const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
92
93 bool needFreshCurrentItem = true;
94 if (!m_inertText->items.isEmpty()) {
95 QStaticTextItem &last = m_inertText->items[m_inertText->items.count() - 1];
96
97 if (last.fontEngine() == ti.fontEngine && last.font == ti.font() &&
98 (!m_dirtyPen || last.color == state->pen().color())) {
99 needFreshCurrentItem = false;
100
101 last.numChars += ti.num_chars;
102
103 }
104 }
105
106 if (needFreshCurrentItem) {
107 QStaticTextItem currentItem;
108
109 currentItem.setFontEngine(ti.fontEngine);
110 currentItem.font = ti.font();
111 currentItem.charOffset = charOffset;
112 currentItem.numChars = ti.num_chars;
113 currentItem.numGlyphs = 0;
114 currentItem.glyphOffset = glyphOffset;
115 currentItem.positionOffset = positionOffset;
116 currentItem.useBackendOptimizations = m_useBackendOptimizations;
117 if (m_dirtyPen)
118 currentItem.color = m_currentColor;
119
120 m_inertText->items.append(currentItem);
121 }
122
123 QStaticTextItem ¤tItem = m_inertText->items.last();
124
125 QTransform matrix = m_untransformedCoordinates ? QTransform() : state->transform();
126 matrix.translate(position.x(), position.y());
127
128 QVarLengthArray<glyph_t> glyphs;
129 QVarLengthArray<QFixedPoint> positions;
130 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
131
132 int size = glyphs.size();
133 Q_ASSERT(size == positions.size());
134 currentItem.numGlyphs += size;
135
136 m_inertText->glyphs.resize(m_inertText->glyphs.size() + size);
137 m_inertText->positions.resize(m_inertText->glyphs.size());
138 m_inertText->chars.resize(m_inertText->chars.size() + ti.num_chars);
139
140 glyph_t *glyphsDestination = m_inertText->glyphs.data() + glyphOffset;
141 qMemCopy(glyphsDestination, glyphs.constData(), sizeof(glyph_t) * size);
142
143 QFixedPoint *positionsDestination = m_inertText->positions.data() + positionOffset;
144 qMemCopy(positionsDestination, positions.constData(), sizeof(QFixedPoint) * size);
145
146 QChar *charsDestination = m_inertText->chars.data() + charOffset;
147 qMemCopy(charsDestination, ti.chars, sizeof(QChar) * ti.num_chars);
148
149 }
150
drawPolygon(const QPointF *,int,PolygonDrawMode)151 virtual void drawPolygon(const QPointF *, int , PolygonDrawMode )
152 {
153 /* intentionally empty */
154 }
155
begin(QPaintDevice *)156 virtual bool begin(QPaintDevice *) { return true; }
end()157 virtual bool end() { return true; }
drawPixmap(const QRectF &,const QPixmap &,const QRectF &)158 virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) {}
type() const159 virtual Type type() const
160 {
161 return User;
162 }
163
begin(QDeclarativeTextLayoutPrivate * t)164 void begin(QDeclarativeTextLayoutPrivate *t) {
165 m_inertText = t;
166 m_dirtyPen = false;
167 }
168
169 private:
170 QDeclarativeTextLayoutPrivate *m_inertText;
171
172 bool m_dirtyPen;
173 bool m_useBackendOptimizations;
174 bool m_untransformedCoordinates;
175 QColor m_currentColor;
176 };
177
178 class DrawTextItemDevice: public QPaintDevice
179 {
180 public:
DrawTextItemDevice(bool untransformedCoordinates,bool useBackendOptimizations)181 DrawTextItemDevice(bool untransformedCoordinates, bool useBackendOptimizations)
182 {
183 m_paintEngine = new DrawTextItemRecorder(untransformedCoordinates,
184 useBackendOptimizations);
185 }
186
~DrawTextItemDevice()187 ~DrawTextItemDevice()
188 {
189 delete m_paintEngine;
190 }
191
begin(QDeclarativeTextLayoutPrivate * t)192 void begin(QDeclarativeTextLayoutPrivate *t) {
193 m_paintEngine->begin(t);
194 }
195
metric(PaintDeviceMetric m) const196 int metric(PaintDeviceMetric m) const
197 {
198 int val;
199 switch (m) {
200 case PdmWidth:
201 case PdmHeight:
202 case PdmWidthMM:
203 case PdmHeightMM:
204 val = 0;
205 break;
206 case PdmDpiX:
207 case PdmPhysicalDpiX:
208 val = qt_defaultDpiX();
209 break;
210 case PdmDpiY:
211 case PdmPhysicalDpiY:
212 val = qt_defaultDpiY();
213 break;
214 case PdmNumColors:
215 val = 16777216;
216 break;
217 case PdmDepth:
218 val = 24;
219 break;
220 default:
221 val = 0;
222 qWarning("DrawTextItemDevice::metric: Invalid metric command");
223 }
224 return val;
225 }
226
paintEngine() const227 virtual QPaintEngine *paintEngine() const
228 {
229 return m_paintEngine;
230 }
231
232 private:
233 DrawTextItemRecorder *m_paintEngine;
234 };
235
236 struct InertTextPainter {
InertTextPainter__anon3565b4c90111::InertTextPainter237 InertTextPainter()
238 : device(true, true), painter(&device)
239 {
240 painter.setPen(QPen(QColor())); // explicitly invalid color.
241 }
242
243 DrawTextItemDevice device;
244 QPainter painter;
245 };
246 }
247
248 Q_GLOBAL_STATIC(InertTextPainter, inertTextPainter);
249
250 /*!
251 \class QDeclarativeTextLayout
252 \brief The QDeclarativeTextLayout class is a version of QStaticText that works with QTextLayouts.
253 \internal
254
255 This class is basically a copy of the QStaticText code, but it is adapted to source its text from
256 QTextLayout.
257
258 It is also considerably faster to create a QDeclarativeTextLayout than a QStaticText because it uses
259 a single, shared QPainter instance. QStaticText by comparison creates a new QPainter per instance.
260 As a consequence this means that QDeclarativeTextLayout is not re-enterant. Adding a lock around
261 the shared painter solves this, and only introduces a minor performance penalty, but is unnecessary
262 for QDeclarativeTextLayout's current use (QDeclarativeText is already tied to the GUI thread).
263 */
264
QDeclarativeTextLayout()265 QDeclarativeTextLayout::QDeclarativeTextLayout()
266 : d(0)
267 {
268 }
269
QDeclarativeTextLayout(const QString & text)270 QDeclarativeTextLayout::QDeclarativeTextLayout(const QString &text)
271 : QTextLayout(text), d(0)
272 {
273 }
274
~QDeclarativeTextLayout()275 QDeclarativeTextLayout::~QDeclarativeTextLayout()
276 {
277 if (d) delete d;
278 }
279
beginLayout()280 void QDeclarativeTextLayout::beginLayout()
281 {
282 if (d && d->cached) {
283 d->cached = false;
284 d->items.clear();
285 d->positions.clear();
286 d->glyphs.clear();
287 d->chars.clear();
288 d->position = QPointF();
289 }
290 QTextLayout::beginLayout();
291 }
292
clearLayout()293 void QDeclarativeTextLayout::clearLayout()
294 {
295 if (d && d->cached) {
296 d->cached = false;
297 d->items.clear();
298 d->positions.clear();
299 d->glyphs.clear();
300 d->chars.clear();
301 d->position = QPointF();
302 }
303 QTextLayout::clearLayout();
304 }
305
prepare()306 void QDeclarativeTextLayout::prepare()
307 {
308 if (!d || !d->cached) {
309
310 if (!d)
311 d = new QDeclarativeTextLayoutPrivate;
312
313 InertTextPainter *itp = inertTextPainter();
314 itp->device.begin(d);
315 QTextLayout::draw(&itp->painter, QPointF(0, 0));
316
317 glyph_t *glyphPool = d->glyphs.data();
318 QFixedPoint *positionPool = d->positions.data();
319 QChar *charPool = d->chars.data();
320
321 int itemCount = d->items.count();
322 for (int ii = 0; ii < itemCount; ++ii) {
323 QStaticTextItem &item = d->items[ii];
324 item.glyphs = glyphPool + item.glyphOffset;
325 item.glyphPositions = positionPool + item.positionOffset;
326 item.chars = charPool + item.charOffset;
327 }
328
329 d->cached = true;
330 }
331 }
332
333 // Defined in qpainter.cpp
334 extern Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
335 const QFixedPoint *positions, int glyphCount,
336 QFontEngine *fontEngine, const QFont &font,
337 const QTextCharFormat &charFormat);
338
draw(QPainter * painter,const QPointF & p)339 void QDeclarativeTextLayout::draw(QPainter *painter, const QPointF &p)
340 {
341 QPainterPrivate *priv = QPainterPrivate::get(painter);
342
343 bool paintEngineSupportsTransformations = priv->extended &&
344 (priv->extended->type() == QPaintEngine::OpenGL2 ||
345 priv->extended->type() == QPaintEngine::OpenVG ||
346 priv->extended->type() == QPaintEngine::OpenGL);
347
348 if (!paintEngineSupportsTransformations || !priv->state->matrix.isAffine()) {
349 QTextLayout::draw(painter, p);
350 return;
351 }
352
353 prepare();
354
355 int itemCount = d->items.count();
356
357 if (p != d->position) {
358 QFixed fx = QFixed::fromReal(p.x());
359 QFixed fy = QFixed::fromReal(p.y());
360 QFixed oldX = QFixed::fromReal(d->position.x());
361 QFixed oldY = QFixed::fromReal(d->position.y());
362 for (int item = 0; item < itemCount; ++item) {
363 QStaticTextItem &textItem = d->items[item];
364
365 for (int ii = 0; ii < textItem.numGlyphs; ++ii) {
366 textItem.glyphPositions[ii].x += fx - oldX;
367 textItem.glyphPositions[ii].y += fy - oldY;
368 }
369 textItem.userDataNeedsUpdate = true;
370 }
371
372 d->position = p;
373 }
374
375 QPen oldPen = priv->state->pen;
376 QColor currentColor = oldPen.color();
377 QColor defaultColor = currentColor;
378 for (int ii = 0; ii < itemCount; ++ii) {
379 QStaticTextItem &item = d->items[ii];
380 if (item.color.isValid() && currentColor != item.color) {
381 // up-edge of a <font color="">text</font> tag
382 // we set the painter pen to the text item's specified color.
383 painter->setPen(item.color);
384 currentColor = item.color;
385 } else if (!item.color.isValid() && currentColor != defaultColor) {
386 // down-edge of a <font color="">text</font> tag
387 // we reset the painter pen back to the default color.
388 currentColor = defaultColor;
389 painter->setPen(currentColor);
390 }
391 priv->extended->drawStaticTextItem(&item);
392
393 qt_draw_decoration_for_glyphs(painter, item.glyphs, item.glyphPositions,
394 item.numGlyphs, item.fontEngine(), painter->font(),
395 QTextCharFormat());
396 }
397 if (currentColor != oldPen.color())
398 painter->setPen(oldPen);
399 }
400
401 QT_END_NAMESPACE
402
403