1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "qtextimagehandler_p.h"
45 #include <qcoreapplication.h>
46 #include <qtextformat.h>
49 #include <private/qtextengine_p.h>
55 static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format)
59 QString name = format.name();
60 if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
61 name.prepend(QLatin1String("qrc"));
62 QUrl url = QUrl(name);
63 const QVariant data = doc->resource(QTextDocument::ImageResource, url);
64 if (data.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
65 pm = qvariant_cast<QPixmap>(data);
66 } else if (data.type() == QVariant::ByteArray) {
67 pm.loadFromData(data.toByteArray());
74 QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
76 context = browser->source().toString();
79 if (img.isNull()) { // try direct loading
80 name = format.name(); // remove qrc:/ prefix again
81 if (name.isEmpty() || !img.load(name))
82 return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
84 pm = QPixmap::fromImage(img);
85 doc->addResource(QTextDocument::ImageResource, url, pm);
91 static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)
95 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
96 const int width = qRound(format.width());
97 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
98 const int height = qRound(format.height());
100 QSize size(width, height);
101 if (!hasWidth || !hasHeight) {
102 pm = getPixmap(doc, format);
105 size.setWidth(pm.width());
107 size.setWidth(qRound(height * (pm.width() / (qreal) pm.height())));
111 size.setHeight(pm.height());
113 size.setHeight(qRound(width * (pm.height() / (qreal) pm.width())));
118 QPaintDevice *pdev = doc->documentLayout()->paintDevice();
121 pm = getPixmap(doc, format);
123 scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
130 static QImage getImage(QTextDocument *doc, const QTextImageFormat &format)
134 QString name = format.name();
135 if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
136 name.prepend(QLatin1String("qrc"));
137 QUrl url = QUrl(name);
138 const QVariant data = doc->resource(QTextDocument::ImageResource, url);
139 if (data.type() == QVariant::Image) {
140 image = qvariant_cast<QImage>(data);
141 } else if (data.type() == QVariant::ByteArray) {
142 image.loadFromData(data.toByteArray());
145 if (image.isNull()) {
150 QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());
152 context = browser->source().toString();
154 if (image.isNull()) { // try direct loading
155 name = format.name(); // remove qrc:/ prefix again
156 if (name.isEmpty() || !image.load(name))
157 return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
159 doc->addResource(QTextDocument::ImageResource, url, image);
165 static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)
169 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
170 const int width = qRound(format.width());
171 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
172 const int height = qRound(format.height());
174 QSize size(width, height);
175 if (!hasWidth || !hasHeight) {
176 image = getImage(doc, format);
178 size.setWidth(image.width());
180 size.setHeight(image.height());
184 QPaintDevice *pdev = doc->documentLayout()->paintDevice();
187 image = getImage(doc, format);
189 scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
196 QTextImageHandler::QTextImageHandler(QObject *parent)
201 QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
203 Q_UNUSED(posInDocument)
204 const QTextImageFormat imageFormat = format.toImageFormat();
206 if (QCoreApplication::instance()->thread() != QThread::currentThread())
207 return getImageSize(doc, imageFormat);
208 return getPixmapSize(doc, imageFormat);
211 QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat)
215 return getImage(doc, imageFormat);
218 void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
220 Q_UNUSED(posInDocument)
221 const QTextImageFormat imageFormat = format.toImageFormat();
223 if (QCoreApplication::instance()->thread() != QThread::currentThread()) {
224 const QImage image = getImage(doc, imageFormat);
225 p->drawImage(rect, image, image.rect());
227 const QPixmap pixmap = getPixmap(doc, imageFormat);
228 p->drawPixmap(rect, pixmap, pixmap.rect());