OpenCoverage

qtextimagehandler.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/text/qtextimagehandler.cpp
Source codeSwitch to Preprocessed file
LineSourceCount
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()) {
url->isValid()Description
TRUEnever evaluated
FALSEnever evaluated
0
64 if (url->scheme() == QLatin1Literal("qrc")) {
url->scheme() ...Literal("qrc")Description
TRUEnever evaluated
FALSEnever evaluated
0
65 fileName = fileName.right(fileName.length() - 3);-
66 }
never executed: end of block
0
67 else if (url->scheme() == QLatin1Literal("file")) {
url->scheme() ...iteral("file")Description
TRUEnever evaluated
FALSEnever evaluated
0
68 fileName = url->toLocalFile();-
69 }
never executed: end of block
0
70 }
never executed: end of block
0
71-
72 if (targetDevicePixelRatio <= 1.0)
targetDevicePixelRatio <= 1.0Description
TRUEnever evaluated
FALSEnever evaluated
0
73 return fileName;
never executed: return fileName;
0
74-
75 // try to find a Nx version-
76 return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio);
never executed: return qt_findAtNxFile(fileName, targetDevicePixelRatio, sourceDevicePixelRatio);
0
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
name.startsWit...1String(":/"))Description
TRUEnever evaluated
FALSEnever evaluated
0
86 name.prepend(QLatin1String("qrc"));
never executed: name.prepend(QLatin1String("qrc"));
0
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.type() == QVariant::Pixmap || data.type() == QVariant::Image) {
data.type() ==...ariant::PixmapDescription
TRUEnever evaluated
FALSEnever evaluated
data.type() == QVariant::ImageDescription
TRUEnever evaluated
FALSEnever evaluated
0
92 pm = qvariant_cast<QPixmap>(data);-
93 } else if (data.type() == QVariant::ByteArray) {
never executed: end of block
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
0
94 pm.loadFromData(data.toByteArray());-
95 }
never executed: end of block
0
96-
97 if (pm.isNull()) {
pm.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
98#if 0-
99 QString context;-
100 // ### Qt5-
101 QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());-
102 if (browser)-
103 context = browser->source().toString();-
104#endif-
105 // try direct loading-
106 QImage img;-
107 if (name.isEmpty() || !img.load(name))
name.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
!img.load(name)Description
TRUEnever evaluated
FALSEnever evaluated
0
108 return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
never executed: return QPixmap(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
0
109-
110 pm = QPixmap::fromImage(img);-
111 doc->addResource(QTextDocument::ImageResource, url, pm);-
112 }
never executed: end of block
0
113-
114 if (name.contains(QLatin1String("@2x")))
name.contains(...String("@2x"))Description
TRUEnever evaluated
FALSEnever evaluated
0
115 pm.setDevicePixelRatio(sourcePixelRatio);
never executed: pm.setDevicePixelRatio(sourcePixelRatio);
0
116-
117 return pm;
never executed: return pm;
0
118}-
119-
120static QSize getPixmapSize(QTextDocument *doc, const QTextImageFormat &format)-
121{-
122 QPixmap pm;-
123-
124 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);-
125 const int width = qRound(format.width());-
126 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);-
127 const int height = qRound(format.height());-
128-
129 QSize size(width, height);-
130 if (!hasWidth || !hasHeight) {
!hasWidthDescription
TRUEnever evaluated
FALSEnever evaluated
!hasHeightDescription
TRUEnever evaluated
FALSEnever evaluated
0
131 pm = getPixmap(doc, format);-
132 const int pmWidth = pm.width() / pm.devicePixelRatio();-
133 const int pmHeight = pm.height() / pm.devicePixelRatio();-
134-
135 if (!hasWidth) {
!hasWidthDescription
TRUEnever evaluated
FALSEnever evaluated
0
136 if (!hasHeight)
!hasHeightDescription
TRUEnever evaluated
FALSEnever evaluated
0
137 size.setWidth(pmWidth);
never executed: size.setWidth(pmWidth);
0
138 else-
139 size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight)));
never executed: size.setWidth(qRound(height * (pmWidth / (qreal) pmHeight)));
0
140 }-
141 if (!hasHeight) {
!hasHeightDescription
TRUEnever evaluated
FALSEnever evaluated
0
142 if (!hasWidth)
!hasWidthDescription
TRUEnever evaluated
FALSEnever evaluated
0
143 size.setHeight(pmHeight);
never executed: size.setHeight(pmHeight);
0
144 else-
145 size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth)));
never executed: size.setHeight(qRound(width * (pmHeight / (qreal) pmWidth)));
0
146 }-
147 }
never executed: end of block
0
148-
149 qreal scale = 1.0;-
150 QPaintDevice *pdev = doc->documentLayout()->paintDevice();-
151 if (pdev) {
pdevDescription
TRUEnever evaluated
FALSEnever evaluated
0
152 if (pm.isNull())
pm.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
153 pm = getPixmap(doc, format);
never executed: pm = getPixmap(doc, format);
0
154 if (!pm.isNull())
!pm.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
155 scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
never executed: scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
0
156 }
never executed: end of block
0
157 size *= scale;-
158-
159 return size;
never executed: return size;
0
160}-
161-
162static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio = 1.0)-
163{-
164 QImage image;-
165-
166 QString name = format.name();-
167 if (name.startsWith(QLatin1String(":/"))) // auto-detect resources
name.startsWit...1String(":/"))Description
TRUEnever evaluated
FALSEnever evaluated
0
168 name.prepend(QLatin1String("qrc"));
never executed: name.prepend(QLatin1String("qrc"));
0
169 QUrl url = QUrl(name);-
170 qreal sourcePixelRatio = 1.0;-
171 name = resolveFileName(name, &url, devicePixelRatio, &sourcePixelRatio);-
172 const QVariant data = doc->resource(QTextDocument::ImageResource, url);-
173 if (data.type() == QVariant::Image) {
data.type() == QVariant::ImageDescription
TRUEnever evaluated
FALSEnever evaluated
0
174 image = qvariant_cast<QImage>(data);-
175 } else if (data.type() == QVariant::ByteArray) {
never executed: end of block
data.type() ==...ant::ByteArrayDescription
TRUEnever evaluated
FALSEnever evaluated
0
176 image.loadFromData(data.toByteArray());-
177 }
never executed: end of block
0
178-
179 if (image.isNull()) {
image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
180#if 0-
181 QString context;-
182 // ### Qt5-
183 QTextBrowser *browser = qobject_cast<QTextBrowser *>(doc->parent());-
184 if (browser)-
185 context = browser->source().toString();-
186#endif-
187 // try direct loading-
188-
189 if (name.isEmpty() || !image.load(name))
name.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
!image.load(name)Description
TRUEnever evaluated
FALSEnever evaluated
0
190 return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
never executed: return QImage(QLatin1String(":/qt-project.org/styles/commonstyle/images/file-16.png"));
0
191-
192 doc->addResource(QTextDocument::ImageResource, url, image);-
193 }
never executed: end of block
0
194-
195 if (sourcePixelRatio != 1.0)
sourcePixelRatio != 1.0Description
TRUEnever evaluated
FALSEnever evaluated
0
196 image.setDevicePixelRatio(sourcePixelRatio);
never executed: image.setDevicePixelRatio(sourcePixelRatio);
0
197-
198 return image;
never executed: return image;
0
199}-
200-
201static QSize getImageSize(QTextDocument *doc, const QTextImageFormat &format)-
202{-
203 QImage image;-
204-
205 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);-
206 const int width = qRound(format.width());-
207 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);-
208 const int height = qRound(format.height());-
209-
210 QSize size(width, height);-
211 if (!hasWidth || !hasHeight) {
!hasWidthDescription
TRUEnever evaluated
FALSEnever evaluated
!hasHeightDescription
TRUEnever evaluated
FALSEnever evaluated
0
212 image = getImage(doc, format);-
213 if (!hasWidth)
!hasWidthDescription
TRUEnever evaluated
FALSEnever evaluated
0
214 size.setWidth(image.width() / image.devicePixelRatio());
never executed: size.setWidth(image.width() / image.devicePixelRatio());
0
215 if (!hasHeight)
!hasHeightDescription
TRUEnever evaluated
FALSEnever evaluated
0
216 size.setHeight(image.height() / image.devicePixelRatio());
never executed: size.setHeight(image.height() / image.devicePixelRatio());
0
217 }
never executed: end of block
0
218-
219 qreal scale = 1.0;-
220 QPaintDevice *pdev = doc->documentLayout()->paintDevice();-
221 if (pdev) {
pdevDescription
TRUEnever evaluated
FALSEnever evaluated
0
222 if (image.isNull())
image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
223 image = getImage(doc, format);
never executed: image = getImage(doc, format);
0
224 if (!image.isNull())
!image.isNull()Description
TRUEnever evaluated
FALSEnever evaluated
0
225 scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
never executed: scale = qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
0
226 }
never executed: end of block
0
227 size *= scale;-
228-
229 return size;
never executed: return size;
0
230}-
231-
232QTextImageHandler::QTextImageHandler(QObject *parent)-
233 : QObject(parent)-
234{-
235}
never executed: end of block
0
236-
237QSizeF QTextImageHandler::intrinsicSize(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())
QCoreApplicati...urrentThread()Description
TRUEnever evaluated
FALSEnever evaluated
0
243 return getImageSize(doc, imageFormat);
never executed: return getImageSize(doc, imageFormat);
0
244 return getPixmapSize(doc, imageFormat);
never executed: return getPixmapSize(doc, imageFormat);
0
245}-
246-
247QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat)-
248{-
249 Q_ASSERT(doc != 0);-
250-
251 return getImage(doc, imageFormat);
never executed: return getImage(doc, imageFormat);
0
252}-
253-
254void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)-
255{-
256 Q_UNUSED(posInDocument)-
257 const QTextImageFormat imageFormat = format.toImageFormat();-
258-
259 if (QCoreApplication::instance()->thread() != QThread::currentThread()) {
QCoreApplicati...urrentThread()Description
TRUEnever evaluated
FALSEnever evaluated
0
260 const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatioF());-
261 p->drawImage(rect, image, image.rect());-
262 } else {
never executed: end of block
0
263 const QPixmap pixmap = getPixmap(doc, imageFormat, p->device()->devicePixelRatioF());-
264 p->drawPixmap(rect, pixmap, pixmap.rect());-
265 }
never executed: end of block
0
266}-
267-
268QT_END_NAMESPACE-
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9