1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
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>
51 class QDeclarativeTextLayoutPrivate
54 QDeclarativeTextLayoutPrivate()
60 QVector<QStaticTextItem> items;
61 QVector<QFixedPoint> positions;
62 QVector<glyph_t> glyphs;
67 class DrawTextItemRecorder: public QPaintEngine
70 DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations)
71 : m_inertText(0), m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations),
72 m_untransformedCoordinates(untransformedCoordinates), m_currentColor(Qt::black)
76 virtual void updateState(const QPaintEngineState &newState)
78 if (newState.state() & QPaintEngine::DirtyPen
79 && newState.pen().color() != m_currentColor) {
81 m_currentColor = newState.pen().color();
85 virtual void drawTextItem(const QPointF &position, const QTextItem &textItem)
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();
91 const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
93 bool needFreshCurrentItem = true;
94 if (!m_inertText->items.isEmpty()) {
95 QStaticTextItem &last = m_inertText->items[m_inertText->items.count() - 1];
97 if (last.fontEngine() == ti.fontEngine && last.font == ti.font() &&
98 (!m_dirtyPen || last.color == state->pen().color())) {
99 needFreshCurrentItem = false;
101 last.numChars += ti.num_chars;
106 if (needFreshCurrentItem) {
107 QStaticTextItem currentItem;
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;
118 currentItem.color = m_currentColor;
120 m_inertText->items.append(currentItem);
123 QStaticTextItem ¤tItem = m_inertText->items.last();
125 QTransform matrix = m_untransformedCoordinates ? QTransform() : state->transform();
126 matrix.translate(position.x(), position.y());
128 QVarLengthArray<glyph_t> glyphs;
129 QVarLengthArray<QFixedPoint> positions;
130 ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
132 int size = glyphs.size();
133 Q_ASSERT(size == positions.size());
134 currentItem.numGlyphs += size;
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);
140 glyph_t *glyphsDestination = m_inertText->glyphs.data() + glyphOffset;
141 qMemCopy(glyphsDestination, glyphs.constData(), sizeof(glyph_t) * size);
143 QFixedPoint *positionsDestination = m_inertText->positions.data() + positionOffset;
144 qMemCopy(positionsDestination, positions.constData(), sizeof(QFixedPoint) * size);
146 QChar *charsDestination = m_inertText->chars.data() + charOffset;
147 qMemCopy(charsDestination, ti.chars, sizeof(QChar) * ti.num_chars);
151 virtual void drawPolygon(const QPointF *, int , PolygonDrawMode )
153 /* intentionally empty */
156 virtual bool begin(QPaintDevice *) { return true; }
157 virtual bool end() { return true; }
158 virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) {}
159 virtual Type type() const
164 void begin(QDeclarativeTextLayoutPrivate *t) {
170 QDeclarativeTextLayoutPrivate *m_inertText;
173 bool m_useBackendOptimizations;
174 bool m_untransformedCoordinates;
175 QColor m_currentColor;
178 class DrawTextItemDevice: public QPaintDevice
181 DrawTextItemDevice(bool untransformedCoordinates, bool useBackendOptimizations)
183 m_paintEngine = new DrawTextItemRecorder(untransformedCoordinates,
184 useBackendOptimizations);
187 ~DrawTextItemDevice()
189 delete m_paintEngine;
192 void begin(QDeclarativeTextLayoutPrivate *t) {
193 m_paintEngine->begin(t);
196 int metric(PaintDeviceMetric m) const
207 case PdmPhysicalDpiX:
208 val = qt_defaultDpiX();
211 case PdmPhysicalDpiY:
212 val = qt_defaultDpiY();
222 qWarning("DrawTextItemDevice::metric: Invalid metric command");
227 virtual QPaintEngine *paintEngine() const
229 return m_paintEngine;
233 DrawTextItemRecorder *m_paintEngine;
236 struct InertTextPainter {
238 : device(true, true), painter(&device) {}
240 DrawTextItemDevice device;
245 Q_GLOBAL_STATIC(InertTextPainter, inertTextPainter);
248 \class QDeclarativeTextLayout
249 \brief The QDeclarativeTextLayout class is a version of QStaticText that works with QTextLayouts.
252 This class is basically a copy of the QStaticText code, but it is adapted to source its text from
255 It is also considerably faster to create a QDeclarativeTextLayout than a QStaticText because it uses
256 a single, shared QPainter instance. QStaticText by comparison creates a new QPainter per instance.
257 As a consequence this means that QDeclarativeTextLayout is not re-enterant. Adding a lock around
258 the shared painter solves this, and only introduces a minor performance penalty, but is unnecessary
259 for QDeclarativeTextLayout's current use (QDeclarativeText is already tied to the GUI thread).
262 QDeclarativeTextLayout::QDeclarativeTextLayout()
267 QDeclarativeTextLayout::QDeclarativeTextLayout(const QString &text)
268 : QTextLayout(text), d(0)
272 QDeclarativeTextLayout::~QDeclarativeTextLayout()
277 void QDeclarativeTextLayout::beginLayout()
279 if (d && d->cached) {
282 d->positions.clear();
285 d->position = QPointF();
287 QTextLayout::beginLayout();
290 void QDeclarativeTextLayout::clearLayout()
292 if (d && d->cached) {
295 d->positions.clear();
298 d->position = QPointF();
300 QTextLayout::clearLayout();
303 void QDeclarativeTextLayout::prepare()
305 if (!d || !d->cached) {
308 d = new QDeclarativeTextLayoutPrivate;
310 InertTextPainter *itp = inertTextPainter();
311 itp->device.begin(d);
312 QTextLayout::draw(&itp->painter, QPointF(0, 0));
314 glyph_t *glyphPool = d->glyphs.data();
315 QFixedPoint *positionPool = d->positions.data();
316 QChar *charPool = d->chars.data();
318 int itemCount = d->items.count();
319 for (int ii = 0; ii < itemCount; ++ii) {
320 QStaticTextItem &item = d->items[ii];
321 item.glyphs = glyphPool + item.glyphOffset;
322 item.glyphPositions = positionPool + item.positionOffset;
323 item.chars = charPool + item.charOffset;
330 // Defined in qpainter.cpp
331 extern Q_GUI_EXPORT void qt_draw_decoration_for_glyphs(QPainter *painter, const glyph_t *glyphArray,
332 const QFixedPoint *positions, int glyphCount,
333 QFontEngine *fontEngine, const QFont &font,
334 const QTextCharFormat &charFormat);
336 void QDeclarativeTextLayout::draw(QPainter *painter, const QPointF &p)
338 QPainterPrivate *priv = QPainterPrivate::get(painter);
340 bool paintEngineSupportsTransformations = priv->extended &&
341 (priv->extended->type() == QPaintEngine::OpenGL2 ||
342 priv->extended->type() == QPaintEngine::OpenVG ||
343 priv->extended->type() == QPaintEngine::OpenGL);
345 if (!paintEngineSupportsTransformations || !priv->state->matrix.isAffine()) {
346 QTextLayout::draw(painter, p);
352 int itemCount = d->items.count();
354 if (p != d->position) {
355 QFixed fx = QFixed::fromReal(p.x());
356 QFixed fy = QFixed::fromReal(p.y());
357 QFixed oldX = QFixed::fromReal(d->position.x());
358 QFixed oldY = QFixed::fromReal(d->position.y());
359 for (int item = 0; item < itemCount; ++item) {
360 QStaticTextItem &textItem = d->items[item];
362 for (int ii = 0; ii < textItem.numGlyphs; ++ii) {
363 textItem.glyphPositions[ii].x += fx - oldX;
364 textItem.glyphPositions[ii].y += fy - oldY;
366 textItem.userDataNeedsUpdate = true;
372 QPen oldPen = priv->state->pen;
373 QColor currentColor = oldPen.color();
374 for (int ii = 0; ii < itemCount; ++ii) {
375 QStaticTextItem &item = d->items[ii];
376 if (item.color.isValid() && currentColor != item.color) {
377 painter->setPen(item.color);
378 currentColor = item.color;
380 priv->extended->drawStaticTextItem(&item);
382 qt_draw_decoration_for_glyphs(painter, item.glyphs, item.glyphPositions,
383 item.numGlyphs, item.fontEngine(), painter->font(),
386 if (currentColor != oldPen.color())
387 painter->setPen(oldPen);