1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui 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 https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40
41#include "qtextimagehandler_p.h"
42
43#include <qguiapplication.h>
44#include <qtextformat.h>
45#include <qpainter.h>
46#include <qdebug.h>
47#include <qfile.h>
48#include <private/qtextengine_p.h>
49#include <qpalette.h>
50#include <qthread.h>
51
52QT_BEGIN_NAMESPACE
53
54extern QString qt_findAtNxFile(const QString &baseFileName, qreal targetDevicePixelRatio,
55 qreal *sourceDevicePixelRatio);
56static QString resolveFileName(QString fileName, QUrl *url, qreal targetDevicePixelRatio,
57 qreal *sourceDevicePixelRatio)
58{
59 // We might use the fileName for loading if url loading fails
60 // try to make sure it is a valid file path.
61 // Also, QFile{Info}::exists works only on filepaths (not urls)
62
63 if (url->isValid()) {
64 if (url->scheme() == QLatin1String("qrc")) {
65 fileName = fileName.right(fileName.length() - 3);
66 }
67 else if (url->scheme() == QLatin1String("file")) {
68 fileName = url->toLocalFile();
69 }
70 }
71
72 if (targetDevicePixelRatio <= 1.0)
73 return fileName;
74
75 // try to find a Nx version
76 return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio);
77}
78
79
80static QPixmap getPixmap(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0)
81{
82 QPixmap pm;
83
84 QString name = format.name();
85 if (name.startsWith(QLatin1String(":/"))) // auto-detect resources and convert them to url
86 name.prepend(QLatin1String("qrc"));
87 QUrl url = QUrl(name);
88 qreal sourcePixelRatio = 1.0;
89 name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio);
90 const QVariant data = doc->resource(QTextDocument::ImageResource, url);
91 if (data.userType() == QMetaType::QPixmap || data.userType() == QMetaType::QImage) {
92 pm = qvariant_cast<QPixmap>(data);
93 } else if (data.userType() == QMetaType::QByteArray) {
94 pm.loadFromData(data.toByteArray());
95 }
96
97 if (pm.isNull()) {
98 QImage img;
99 if (name.isEmpty() || !img.load(name))
100 return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
101
102 pm = QPixmap::fromImage(img);
103 doc->addResource(QTextDocument::ImageResource, url, pm);
104 }
105
106 if (name.contains(QLatin1String("@2x")))
107 pm.setDevicePixelRatio(sourcePixelRatio);
108
109 return pm;
110}
111
112static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)
113{
114 QPixmap pm;
115
116 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
117 const int width = qRound(format.width());
118 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
119 const int height = qRound(format.height());
120
121 QSize size(width, height);
122 if (!hasWidth || !hasHeight) {
123 pm = getPixmap(doc, format);
124 const int pmWidth = pm.width() / pm.devicePixelRatio();
125 const int pmHeight = pm.height() / pm.devicePixelRatio();
126
127 if (!hasWidth) {
128 if (!hasHeight)
129 size.setWidth(pmWidth);
130 else
131 size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight)));
132 }
133 if (!hasHeight) {
134 if (!hasWidth)
135 size.setHeight(pmHeight);
136 else
137 size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth)));
138 }
139 }
140
141 qreal scale = 1.0;
142 QPaintDevice *pdev = doc->documentLayout()->paintDevice();
143 if (pdev) {
144 if (pm.isNull())
145 pm = getPixmap(doc, format);
146 if (!pm.isNull())
147 scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
148 }
149 size *= scale;
150
151 return size;
152}
153
154static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0)
155{
156 QImage image;
157
158 QString name = format.name();
159 if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
160 name.prepend(QLatin1String("qrc"));
161 QUrl url = QUrl(name);
162 qreal sourcePixelRatio = 1.0;
163 name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio);
164 const QVariant data = doc->resource(QTextDocument::ImageResource, url);
165 if (data.userType() == QMetaType::QImage) {
166 image = qvariant_cast<QImage>(data);
167 } else if (data.userType() == QMetaType::QByteArray) {
168 image.loadFromData(data.toByteArray());
169 }
170
171 if (image.isNull()) {
172 if (name.isEmpty() || !image.load(name))
173 return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
174
175 doc->addResource(QTextDocument::ImageResource, url, image);
176 }
177
178 if (sourcePixelRatio != 1.0)
179 image.setDevicePixelRatio(sourcePixelRatio);
180
181 return image;
182}
183
184static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)
185{
186 QImage image;
187
188 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
189 const int width = qRound(format.width());
190 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
191 const int height = qRound(format.height());
192
193 QSize size(width, height);
194 if (!hasWidth || !hasHeight) {
195 image = getImage(doc, format);
196 if (!hasWidth)
197 size.setWidth(image.width() / image.devicePixelRatio());
198 if (!hasHeight)
199 size.setHeight(image.height() / image.devicePixelRatio());
200 }
201
202 qreal scale = 1.0;
203 QPaintDevice *pdev = doc->documentLayout()->paintDevice();
204 if (pdev) {
205 if (image.isNull())
206 image = getImage(doc, format);
207 if (!image.isNull())
208 scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
209 }
210 size *= scale;
211
212 return size;
213}
214
215QTextImageHandler::QTextImageHandler(QObject *parent)
216 : QObject(parent)
217{
218}
219
220QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
221{
222 Q_UNUSED(posInDocument);
223 const QTextImageFormat imageFormat = format.toImageFormat();
224
225 if (QCoreApplication::instance()->thread() != QThread::currentThread())
226 return getImageSize(doc, imageFormat);
227 return getPixmapSize(doc, imageFormat);
228}
229
230QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat)
231{
232 Q_ASSERT(doc != nullptr);
233
234 return getImage(doc, imageFormat);
235}
236
237void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
238{
239 Q_UNUSED(posInDocument);
240 const QTextImageFormat imageFormat = format.toImageFormat();
241
242 if (QCoreApplication::instance()->thread() != QThread::currentThread()) {
243 const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatio());
244 p->drawImage(rect, image, image.rect());
245 } else {
246 const QPixmap pixmap = getPixmap(doc, imageFormat, p->device()->devicePixelRatio());
247 p->drawPixmap(rect, pixmap, pixmap.rect());
248 }
249}
250
251QT_END_NAMESPACE
252