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