Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtextimagehandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
6
7#include <qbuffer.h>
8#include <qguiapplication.h>
9#include <qtextformat.h>
10#include <qpainter.h>
11#include <qdebug.h>
12#include <qicon.h>
13#include <qimagereader.h>
14#include <qpixmapcache.h>
15#include <qthread.h>
16#include <qpa/qplatformintegration.h>
17#include <private/qguiapplication_p.h>
18#include <private/qhexstring_p.h>
19#include <private/qtextengine_p.h>
20#include <limits>
21
23
24using namespace Qt::StringLiterals;
25
26static inline QUrl findAtNxFileOrResource(const QString &baseFileName,
27 qreal targetDevicePixelRatio,
28 qreal *sourceDevicePixelRatio,
29 QString *name)
30{
31 // qt_findAtNxFile expects a file name that can be tested with QFile::exists.
32 // so if the format.name() is a file:/ or qrc:/ URL, then we need to strip away the schema.
33 QString localFile;
34 const QUrl url(baseFileName);
35 bool hasFileScheme = false;
36 bool isResource = false;
37 if (url.isLocalFile()) {
38 localFile = url.toLocalFile();
39 hasFileScheme = true;
40 } else if (baseFileName.startsWith("qrc:/"_L1)) {
41 // QFile::exists() can only handle ":/file.txt"
42 localFile = baseFileName.sliced(3);
43 isResource = true;
44 } else {
45 localFile = baseFileName;
46 isResource = baseFileName.startsWith(":/"_L1);
47 }
48 *name = qt_findAtNxFile(localFile, targetDevicePixelRatio, sourceDevicePixelRatio);
49
50 if (hasFileScheme)
51 return QUrl::fromLocalFile(*name);
52 if (isResource)
53 return QUrl("qrc"_L1 + *name);
54 return QUrl(*name);
55}
56
57static QImage getImage(QTextDocument *doc, const QTextImageFormat &format,
58 const qreal devicePixelRatio = 1.0, const QSizeF &size = {})
59{
60 qreal sourcePixelRatio = 1.0;
61 QString name;
62 const QUrl url = findAtNxFileOrResource(format.name(), devicePixelRatio, &sourcePixelRatio, &name);
63 const QVariant data = doc->resource(QTextDocument::ImageResource, url);
64
65 // directly provided as QPixmap/QImage or already cached
66 if (data.userType() == QMetaType::QPixmap || data.userType() == QMetaType::QImage)
67 return data.value<QImage>();
68
69 // some image formats are scalable (e.g. svg) so we should not store a single
70 // QImage as QTextDocument::ImageResource to avoid scaling artefacts later on.
71 // But otoh we don't want to load them on every usage. Therefore cache the result
72 // in QPixmapCache
73 const bool canUsePixmapCache = (QThread::isMainThread()
74 || QGuiApplicationPrivate::platformIntegration()->hasCapability(
75 QPlatformIntegration::ThreadedPixmaps));
76 const auto buildCacheKey = [](const QSizeF &size, qreal dpr) {
77 return QLatin1String("qt_textimagehandler_") % HexString<int>(size.width())
78 % HexString<int>(size.height())
79 % HexString<qint16>(qRound(dpr * 1000));
80 };
81 const QString cacheKey = canUsePixmapCache
82 ? buildCacheKey(size * devicePixelRatio, sourcePixelRatio)
83 : QString();
84
85 if (canUsePixmapCache) {
86 QPixmap pm;
87 if (QPixmapCache::find(cacheKey, &pm))
88 return pm.toImage();
89 }
90
91 const auto readImage = [&](auto &&content) {
92 QImageReader imgReader(content);
93 if (imgReader.canRead()) {
94 const bool supportsScaledSize = imgReader.supportsOption(QImageIOHandler::ScaledSize);
95 if (size.isValid() && supportsScaledSize)
96 imgReader.setScaledSize((size * devicePixelRatio).toSize());
97 QImage result = imgReader.read();
98 result.setDevicePixelRatio(sourcePixelRatio);
99 if (!supportsScaledSize)
100 doc->addResource(QTextDocument::ImageResource, url, result);
101 else if (canUsePixmapCache)
102 QPixmapCache::insert(cacheKey, QPixmap::fromImage(result));
103 return result;
104 }
105 return QImage();
106 };
107
108 QImage result;
109 if (data.metaType() == QMetaType::fromType<QByteArray>()) {
110 QByteArray ba(data.toByteArray());
111 QBuffer buf(&ba);
112 result = readImage(&buf);
113 }
114 if (result.isNull())
115 result = readImage(name);
116
117 if (result.isNull())
118 result = QImage(":/qt-project.org/styles/commonstyle/images/file-16.png"_L1);
119 return result;
120}
121
122static QSize getSize(QTextDocument *doc, const QTextImageFormat &format)
123{
124 const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
125 int width = qRound(format.width());
126 const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
127 const int height = qRound(format.height());
128
129 const bool hasMaxWidth = format.hasProperty(QTextFormat::ImageMaxWidth);
130 const auto maxWidth = format.maximumWidth();
131
132 int effectiveMaxWidth = std::numeric_limits<int>::max();
133 if (hasMaxWidth) {
134 if (maxWidth.type() == QTextLength::PercentageLength)
135 effectiveMaxWidth = (doc->pageSize().width() - 2 * doc->documentMargin()) * maxWidth.value(100) / 100;
136 else
137 effectiveMaxWidth = maxWidth.rawValue();
138
139 width = qMin(effectiveMaxWidth, width);
140 }
141
142 QImage source;
143 QSize size(width, height);
144 if (!hasWidth || !hasHeight) {
145 source = getImage(doc, format);
146 QSizeF sourceSize = source.deviceIndependentSize();
147
148 if (sourceSize.width() > effectiveMaxWidth) {
149 // image is bigger than effectiveMaxWidth, scale it down
150 sourceSize.setHeight(effectiveMaxWidth * (sourceSize.height() / qreal(sourceSize.width())));
151 sourceSize.setWidth(effectiveMaxWidth);
152 }
153
154 if (!hasWidth) {
155 if (!hasHeight)
156 size.setWidth(sourceSize.width());
157 else
158 size.setWidth(qMin(effectiveMaxWidth, qRound(height * (sourceSize.width() / qreal(sourceSize.height())))));
159 }
160 if (!hasHeight) {
161 if (!hasWidth)
162 size.setHeight(sourceSize.height());
163 else
164 size.setHeight(qRound(width * (sourceSize.height() / qreal(sourceSize.width()))));
165 }
166 }
167
168 const QPaintDevice *pdev = doc->documentLayout()->paintDevice();
169 if (pdev)
170 size *= qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
171
172 return size;
173}
174
175QTextImageHandler::QTextImageHandler(QObject *parent)
176 : QObject(parent)
177{
178}
179
180QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
181{
182 Q_UNUSED(posInDocument);
183 const QTextImageFormat imageFormat = format.toImageFormat();
184
185 return getSize(doc, imageFormat);
186}
187
188QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat)
189{
190 Q_ASSERT(doc != nullptr);
191
192 return getImage(doc, imageFormat);
193}
194
195void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
196{
197 Q_UNUSED(posInDocument);
198 const QTextImageFormat imageFormat = format.toImageFormat();
199
200 const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatio(), rect.size());
201 p->drawImage(rect, image, image.rect());
202}
203
204QT_END_NAMESPACE
205
206#include "moc_qtextimagehandler_p.cpp"
\inmodule QtCore \reentrant
Definition qbuffer.h:17
Combined button and popup list for selecting options.
static QUrl findAtNxFileOrResource(const QString &baseFileName, qreal targetDevicePixelRatio, qreal *sourceDevicePixelRatio, QString *name)
static QSize getSize(QTextDocument *doc, const QTextImageFormat &format)
static QImage getImage(QTextDocument *doc, const QTextImageFormat &format, const qreal devicePixelRatio=1.0, const QSizeF &size={})