| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 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: | 
|---|
| 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 | Q_GUI_EXPORT extern int qt_defaultDpiX(); | 
|---|
| 67 | Q_GUI_EXPORT extern int qt_defaultDpiY(); | 
|---|
| 68 |  | 
|---|
| 69 | namespace { | 
|---|
| 70 | class DrawTextItemRecorder: public QPaintEngine | 
|---|
| 71 | { | 
|---|
| 72 | public: | 
|---|
| 73 | DrawTextItemRecorder(bool untransformedCoordinates, bool useBackendOptimizations) | 
|---|
| 74 | : m_inertText(0), m_dirtyPen(false), m_useBackendOptimizations(useBackendOptimizations), | 
|---|
| 75 | m_untransformedCoordinates(untransformedCoordinates) | 
|---|
| 76 | { | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | virtual void updateState(const QPaintEngineState &newState) | 
|---|
| 80 | { | 
|---|
| 81 | if (newState.state() & QPaintEngine::DirtyPen) | 
|---|
| 82 | m_dirtyPen = true; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 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 | last.numGlyphs += ti.glyphs.numGlyphs; | 
|---|
| 103 |  | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | if (needFreshCurrentItem) { | 
|---|
| 108 | QStaticTextItem currentItem; | 
|---|
| 109 |  | 
|---|
| 110 | currentItem.setFontEngine(ti.fontEngine); | 
|---|
| 111 | currentItem.font = ti.font(); | 
|---|
| 112 | currentItem.charOffset = charOffset; | 
|---|
| 113 | currentItem.numChars = ti.num_chars; | 
|---|
| 114 | currentItem.numGlyphs = ti.glyphs.numGlyphs; | 
|---|
| 115 | currentItem.glyphOffset = glyphOffset; | 
|---|
| 116 | currentItem.positionOffset = positionOffset; | 
|---|
| 117 | currentItem.useBackendOptimizations = m_useBackendOptimizations; | 
|---|
| 118 | if (m_dirtyPen) | 
|---|
| 119 | currentItem.color = state->pen().color(); | 
|---|
| 120 |  | 
|---|
| 121 | m_inertText->items.append(currentItem); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | QTransform matrix = m_untransformedCoordinates ? QTransform() : state->transform(); | 
|---|
| 125 | matrix.translate(position.x(), position.y()); | 
|---|
| 126 |  | 
|---|
| 127 | QVarLengthArray<glyph_t> glyphs; | 
|---|
| 128 | QVarLengthArray<QFixedPoint> positions; | 
|---|
| 129 | ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions); | 
|---|
| 130 |  | 
|---|
| 131 | int size = glyphs.size(); | 
|---|
| 132 | Q_ASSERT(size == ti.glyphs.numGlyphs); | 
|---|
| 133 | Q_ASSERT(size == positions.size()); | 
|---|
| 134 |  | 
|---|
| 135 | m_inertText->glyphs.resize(m_inertText->glyphs.size() + size); | 
|---|
| 136 | m_inertText->positions.resize(m_inertText->glyphs.size()); | 
|---|
| 137 | m_inertText->chars.resize(m_inertText->chars.size() + ti.num_chars); | 
|---|
| 138 |  | 
|---|
| 139 | glyph_t *glyphsDestination = m_inertText->glyphs.data() + glyphOffset; | 
|---|
| 140 | qMemCopy(glyphsDestination, glyphs.constData(), sizeof(glyph_t) * ti.glyphs.numGlyphs); | 
|---|
| 141 |  | 
|---|
| 142 | QFixedPoint *positionsDestination = m_inertText->positions.data() + positionOffset; | 
|---|
| 143 | qMemCopy(positionsDestination, positions.constData(), sizeof(QFixedPoint) * ti.glyphs.numGlyphs); | 
|---|
| 144 |  | 
|---|
| 145 | QChar *charsDestination = m_inertText->chars.data() + charOffset; | 
|---|
| 146 | qMemCopy(charsDestination, ti.chars, sizeof(QChar) * ti.num_chars); | 
|---|
| 147 |  | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | virtual void drawPolygon(const QPointF *, int , PolygonDrawMode ) | 
|---|
| 151 | { | 
|---|
| 152 | /* intentionally empty */ | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | virtual bool begin(QPaintDevice *)  { return true; } | 
|---|
| 156 | virtual bool end() { return true; } | 
|---|
| 157 | virtual void drawPixmap(const QRectF &, const QPixmap &, const QRectF &) {} | 
|---|
| 158 | virtual Type type() const | 
|---|
| 159 | { | 
|---|
| 160 | return User; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void begin(QDeclarativeTextLayoutPrivate *t) { | 
|---|
| 164 | m_inertText = t; | 
|---|
| 165 | m_dirtyPen = false; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 | private: | 
|---|
| 169 | QDeclarativeTextLayoutPrivate *m_inertText; | 
|---|
| 170 |  | 
|---|
| 171 | bool m_dirtyPen; | 
|---|
| 172 | bool m_useBackendOptimizations; | 
|---|
| 173 | bool m_untransformedCoordinates; | 
|---|
| 174 | }; | 
|---|
| 175 |  | 
|---|
| 176 | class DrawTextItemDevice: public QPaintDevice | 
|---|
| 177 | { | 
|---|
| 178 | public: | 
|---|
| 179 | DrawTextItemDevice(bool untransformedCoordinates, bool useBackendOptimizations) | 
|---|
| 180 | { | 
|---|
| 181 | m_paintEngine = new DrawTextItemRecorder(untransformedCoordinates, | 
|---|
| 182 | useBackendOptimizations); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | ~DrawTextItemDevice() | 
|---|
| 186 | { | 
|---|
| 187 | delete m_paintEngine; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | void begin(QDeclarativeTextLayoutPrivate *t) { | 
|---|
| 191 | m_paintEngine->begin(t); | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | int metric(PaintDeviceMetric m) const | 
|---|
| 195 | { | 
|---|
| 196 | int val; | 
|---|
| 197 | switch (m) { | 
|---|
| 198 | case PdmWidth: | 
|---|
| 199 | case PdmHeight: | 
|---|
| 200 | case PdmWidthMM: | 
|---|
| 201 | case PdmHeightMM: | 
|---|
| 202 | val = 0; | 
|---|
| 203 | break; | 
|---|
| 204 | case PdmDpiX: | 
|---|
| 205 | case PdmPhysicalDpiX: | 
|---|
| 206 | val = qt_defaultDpiX(); | 
|---|
| 207 | break; | 
|---|
| 208 | case PdmDpiY: | 
|---|
| 209 | case PdmPhysicalDpiY: | 
|---|
| 210 | val = qt_defaultDpiY(); | 
|---|
| 211 | break; | 
|---|
| 212 | case PdmNumColors: | 
|---|
| 213 | val = 16777216; | 
|---|
| 214 | break; | 
|---|
| 215 | case PdmDepth: | 
|---|
| 216 | val = 24; | 
|---|
| 217 | break; | 
|---|
| 218 | default: | 
|---|
| 219 | val = 0; | 
|---|
| 220 | qWarning("DrawTextItemDevice::metric: Invalid metric command"); | 
|---|
| 221 | } | 
|---|
| 222 | return val; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | virtual QPaintEngine *paintEngine() const | 
|---|
| 226 | { | 
|---|
| 227 | return m_paintEngine; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | private: | 
|---|
| 231 | DrawTextItemRecorder *m_paintEngine; | 
|---|
| 232 | }; | 
|---|
| 233 |  | 
|---|
| 234 | struct InertTextPainter { | 
|---|
| 235 | InertTextPainter() | 
|---|
| 236 | : device(true, true), painter(&device) {} | 
|---|
| 237 |  | 
|---|
| 238 | DrawTextItemDevice device; | 
|---|
| 239 | QPainter painter; | 
|---|
| 240 | }; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | Q_GLOBAL_STATIC(InertTextPainter, inertTextPainter); | 
|---|
| 244 |  | 
|---|
| 245 | /*! | 
|---|
| 246 | \class QDeclarativeTextLayout | 
|---|
| 247 | \brief The QDeclarativeTextLayout class is a version of QStaticText that works with QTextLayouts. | 
|---|
| 248 | \internal | 
|---|
| 249 |  | 
|---|
| 250 | This class is basically a copy of the QStaticText code, but it is adapted to source its text from | 
|---|
| 251 | QTextLayout. | 
|---|
| 252 |  | 
|---|
| 253 | It is also considerably faster to create a QDeclarativeTextLayout than a QStaticText because it uses | 
|---|
| 254 | a single, shared QPainter instance.  QStaticText by comparison creates a new QPainter per instance. | 
|---|
| 255 | As a consequence this means that QDeclarativeTextLayout is not re-enterant.  Adding a lock around | 
|---|
| 256 | the shared painter solves this, and only introduces a minor performance penalty, but is unnecessary | 
|---|
| 257 | for QDeclarativeTextLayout's current use (QDeclarativeText is already tied to the GUI thread). | 
|---|
| 258 | */ | 
|---|
| 259 |  | 
|---|
| 260 | QDeclarativeTextLayout::QDeclarativeTextLayout() | 
|---|
| 261 | : d(0) | 
|---|
| 262 | { | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 | QDeclarativeTextLayout::QDeclarativeTextLayout(const QString &text) | 
|---|
| 266 | : QTextLayout(text), d(0) | 
|---|
| 267 | { | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | QDeclarativeTextLayout::~QDeclarativeTextLayout() | 
|---|
| 271 | { | 
|---|
| 272 | if (d) delete d; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | void QDeclarativeTextLayout::beginLayout() | 
|---|
| 276 | { | 
|---|
| 277 | if (d && d->cached) { | 
|---|
| 278 | d->cached = false; | 
|---|
| 279 | d->items.clear(); | 
|---|
| 280 | d->positions.clear(); | 
|---|
| 281 | d->glyphs.clear(); | 
|---|
| 282 | d->chars.clear(); | 
|---|
| 283 | d->position = QPointF(); | 
|---|
| 284 | } | 
|---|
| 285 | QTextLayout::beginLayout(); | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | void QDeclarativeTextLayout::clearLayout() | 
|---|
| 289 | { | 
|---|
| 290 | if (d && d->cached) { | 
|---|
| 291 | d->cached = false; | 
|---|
| 292 | d->items.clear(); | 
|---|
| 293 | d->positions.clear(); | 
|---|
| 294 | d->glyphs.clear(); | 
|---|
| 295 | d->chars.clear(); | 
|---|
| 296 | d->position = QPointF(); | 
|---|
| 297 | } | 
|---|
| 298 | QTextLayout::clearLayout(); | 
|---|
| 299 | } | 
|---|
| 300 |  | 
|---|
| 301 | void QDeclarativeTextLayout::prepare() | 
|---|
| 302 | { | 
|---|
| 303 | if (!d || !d->cached) { | 
|---|
| 304 |  | 
|---|
| 305 | if (!d) | 
|---|
| 306 | d = new QDeclarativeTextLayoutPrivate; | 
|---|
| 307 |  | 
|---|
| 308 | InertTextPainter *itp = inertTextPainter(); | 
|---|
| 309 | itp->device.begin(d); | 
|---|
| 310 | QTextLayout::draw(&itp->painter, QPointF(0, 0)); | 
|---|
| 311 |  | 
|---|
| 312 | glyph_t *glyphPool = d->glyphs.data(); | 
|---|
| 313 | QFixedPoint *positionPool = d->positions.data(); | 
|---|
| 314 | QChar *charPool = d->chars.data(); | 
|---|
| 315 |  | 
|---|
| 316 | int itemCount = d->items.count(); | 
|---|
| 317 | for (int ii = 0; ii < itemCount; ++ii) { | 
|---|
| 318 | QStaticTextItem &item = d->items[ii]; | 
|---|
| 319 | item.glyphs = glyphPool + item.glyphOffset; | 
|---|
| 320 | item.glyphPositions = positionPool + item.positionOffset; | 
|---|
| 321 | item.chars = charPool + item.charOffset; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 | d->cached = true; | 
|---|
| 325 | } | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|
| 328 | void QDeclarativeTextLayout::draw(QPainter *painter, const QPointF &p) | 
|---|
| 329 | { | 
|---|
| 330 | QPainterPrivate *priv = QPainterPrivate::get(painter); | 
|---|
| 331 |  | 
|---|
| 332 | bool paintEngineSupportsTransformations = priv->extended && | 
|---|
| 333 | (priv->extended->type() == QPaintEngine::OpenGL2 || | 
|---|
| 334 | priv->extended->type() == QPaintEngine::OpenVG || | 
|---|
| 335 | priv->extended->type() == QPaintEngine::OpenGL); | 
|---|
| 336 |  | 
|---|
| 337 | if (!paintEngineSupportsTransformations || !priv->state->matrix.isAffine()) { | 
|---|
| 338 | QTextLayout::draw(painter, p); | 
|---|
| 339 | return; | 
|---|
| 340 | } | 
|---|
| 341 |  | 
|---|
| 342 | prepare(); | 
|---|
| 343 |  | 
|---|
| 344 | int itemCount = d->items.count(); | 
|---|
| 345 |  | 
|---|
| 346 | if (p != d->position) { | 
|---|
| 347 | QFixed fx = QFixed::fromReal(p.x()); | 
|---|
| 348 | QFixed fy = QFixed::fromReal(p.y()); | 
|---|
| 349 | QFixed oldX = QFixed::fromReal(d->position.x()); | 
|---|
| 350 | QFixed oldY = QFixed::fromReal(d->position.y()); | 
|---|
| 351 | for (int item = 0; item < itemCount; ++item) { | 
|---|
| 352 | QStaticTextItem &textItem = d->items[item]; | 
|---|
| 353 |  | 
|---|
| 354 | for (int ii = 0; ii < textItem.numGlyphs; ++ii) { | 
|---|
| 355 | textItem.glyphPositions[ii].x += fx - oldX; | 
|---|
| 356 | textItem.glyphPositions[ii].y += fy - oldY; | 
|---|
| 357 | } | 
|---|
| 358 | textItem.userDataNeedsUpdate = true; | 
|---|
| 359 | } | 
|---|
| 360 |  | 
|---|
| 361 | d->position = p; | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | QPen oldPen = priv->state->pen; | 
|---|
| 365 | QColor currentColor = oldPen.color(); | 
|---|
| 366 | for (int ii = 0; ii < itemCount; ++ii) { | 
|---|
| 367 | QStaticTextItem &item = d->items[ii]; | 
|---|
| 368 | if (item.color.isValid() && currentColor != item.color) { | 
|---|
| 369 | painter->setPen(item.color); | 
|---|
| 370 | currentColor = item.color; | 
|---|
| 371 | } | 
|---|
| 372 | priv->extended->drawStaticTextItem(&item); | 
|---|
| 373 | } | 
|---|
| 374 | if (currentColor != oldPen.color()) | 
|---|
| 375 | painter->setPen(oldPen); | 
|---|
| 376 | } | 
|---|
| 377 |  | 
|---|
| 378 | QT_END_NAMESPACE | 
|---|
| 379 |  | 
|---|