Coverage Report

Created: 2026-06-30 07:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qtbase/src/gui/text/qtextimagehandler.cpp
Line
Count
Source
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
5
#include "qtextimagehandler_p.h"
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
22
QT_BEGIN_NAMESPACE
23
24
using namespace Qt::StringLiterals;
25
26
static inline QUrl findAtNxFileOrResource(const QString &baseFileName,
27
                                          qreal targetDevicePixelRatio,
28
                                          qreal *sourceDevicePixelRatio,
29
                                          QString *name)
30
0
{
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
0
    QString localFile;
34
0
    const QUrl url(baseFileName);
35
0
    bool hasFileScheme = false;
36
0
    bool isResource = false;
37
0
    if (url.isLocalFile()) {
38
0
        localFile = url.toLocalFile();
39
0
        hasFileScheme = true;
40
0
    } else if (baseFileName.startsWith("qrc:/"_L1)) {
41
        // QFile::exists() can only handle ":/file.txt"
42
0
        localFile = baseFileName.sliced(3);
43
0
        isResource = true;
44
0
    } else {
45
0
        localFile = baseFileName;
46
0
        isResource = baseFileName.startsWith(":/"_L1);
47
0
    }
48
0
    *name = qt_findAtNxFile(localFile, targetDevicePixelRatio, sourceDevicePixelRatio);
49
50
0
    if (hasFileScheme)
51
0
        return QUrl::fromLocalFile(*name);
52
0
    if (isResource)
53
0
        return QUrl("qrc"_L1 + *name);
54
0
    return QUrl(*name);
55
0
}
56
57
static QImage getImage(QTextDocument *doc, const QTextImageFormat &format,
58
                       const qreal devicePixelRatio = 1.0, const QSizeF &size = {})
59
0
{
60
0
    qreal sourcePixelRatio = 1.0;
61
0
    QString name;
62
0
    const QUrl url = findAtNxFileOrResource(format.name(), devicePixelRatio, &sourcePixelRatio, &name);
63
0
    const QVariant data = doc->resource(QTextDocument::ImageResource, url);
64
65
    // directly provided as QPixmap/QImage or already cached
66
0
    if (data.userType() == QMetaType::QPixmap || data.userType() == QMetaType::QImage)
67
0
        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
0
    const bool canUsePixmapCache = (QThread::isMainThread()
74
0
                                    || QGuiApplicationPrivate::platformIntegration()->hasCapability(
75
0
                                            QPlatformIntegration::ThreadedPixmaps));
76
0
    const auto buildCacheKey = [](const QString &name, const QSizeF &size, qreal dpr) -> QString {
77
0
        return QLatin1String("qt_textimagehandler_") % name
78
0
                % HexString<int>(size.width())
79
0
                % HexString<int>(size.height())
80
0
                % HexString<qint16>(qRound(dpr * 1000));
81
0
    };
82
0
    const QString cacheKey = canUsePixmapCache
83
0
            ? buildCacheKey(name, size * devicePixelRatio, sourcePixelRatio)
84
0
            : QString();
85
86
0
    if (canUsePixmapCache) {
87
0
        QPixmap pm;
88
0
        if (QPixmapCache::find(cacheKey, &pm))
89
0
            return pm.toImage();
90
0
    }
91
92
0
    const auto readImage = [&](auto &&content) {
93
0
        QImageReader imgReader(content);
94
0
        if (imgReader.canRead()) {
95
0
            const bool supportsScaledSize = imgReader.supportsOption(QImageIOHandler::ScaledSize);
96
0
            if (size.isValid() && supportsScaledSize)
97
0
                imgReader.setScaledSize((size * devicePixelRatio).toSize());
98
0
            QImage result = imgReader.read();
99
0
            result.setDevicePixelRatio(sourcePixelRatio);
100
0
            if (!supportsScaledSize)
101
0
                doc->addResource(QTextDocument::ImageResource, url, result);
102
0
            else if (canUsePixmapCache)
103
0
                QPixmapCache::insert(cacheKey, QPixmap::fromImage(result));
104
0
            return result;
105
0
        }
106
0
        return QImage();
107
0
    };
Unexecuted instantiation: qtextimagehandler.cpp:auto getImage(QTextDocument*, QTextImageFormat const&, double, QSizeF const&)::$_0::operator()<QBuffer*>(QBuffer*&&) const
Unexecuted instantiation: qtextimagehandler.cpp:auto getImage(QTextDocument*, QTextImageFormat const&, double, QSizeF const&)::$_0::operator()<QString&>(QString&) const
108
109
0
    QImage result;
110
0
    if (data.metaType() == QMetaType::fromType<QByteArray>()) {
111
0
        QByteArray ba(data.toByteArray());
112
0
        QBuffer buf(&ba);
113
0
        result = readImage(&buf);
114
0
    }
115
0
    if (result.isNull())
116
0
        result = readImage(name);
117
118
0
    if (result.isNull())
119
0
        result = QImage(":/qt-project.org/styles/commonstyle/images/file-16.png"_L1);
120
0
    return result;
121
0
}
122
123
static QSize getSize(QTextDocument *doc, const QTextImageFormat &format)
124
0
{
125
0
    const bool hasWidth = format.hasProperty(QTextFormat::ImageWidth);
126
0
    int width = qRound(format.width());
127
0
    const bool hasHeight = format.hasProperty(QTextFormat::ImageHeight);
128
0
    const int height = qRound(format.height());
129
130
0
    const bool hasMaxWidth = format.hasProperty(QTextFormat::ImageMaxWidth);
131
0
    const auto maxWidth = format.maximumWidth();
132
133
0
    int effectiveMaxWidth = std::numeric_limits<int>::max();
134
0
    if (hasMaxWidth) {
135
0
        if (maxWidth.type() == QTextLength::PercentageLength)
136
0
            effectiveMaxWidth = (doc->pageSize().width() - 2 * doc->documentMargin()) * maxWidth.value(100) / 100;
137
0
        else
138
0
            effectiveMaxWidth = maxWidth.rawValue();
139
140
0
        width = qMin(effectiveMaxWidth, width);
141
0
    }
142
143
0
    QImage source;
144
0
    QSize size(width, height);
145
0
    if (!hasWidth || !hasHeight) {
146
0
        source = getImage(doc, format);
147
0
        QSizeF sourceSize = source.deviceIndependentSize();
148
149
0
        if (sourceSize.width() > effectiveMaxWidth) {
150
            // image is bigger than effectiveMaxWidth, scale it down
151
0
            sourceSize.setHeight(effectiveMaxWidth * (sourceSize.height() / qreal(sourceSize.width())));
152
0
            sourceSize.setWidth(effectiveMaxWidth);
153
0
        }
154
155
0
        if (!hasWidth) {
156
0
            if (!hasHeight)
157
0
                size.setWidth(sourceSize.width());
158
0
            else
159
0
                size.setWidth(qMin(effectiveMaxWidth, qRound(height * (sourceSize.width() / qreal(sourceSize.height())))));
160
0
        }
161
0
        if (!hasHeight) {
162
0
            if (!hasWidth)
163
0
                size.setHeight(sourceSize.height());
164
0
            else
165
0
                size.setHeight(qRound(width * (sourceSize.height() / qreal(sourceSize.width()))));
166
0
        }
167
0
    }
168
169
0
    const QPaintDevice *pdev = doc->documentLayout()->paintDevice();
170
0
    if (pdev)
171
0
        size *= qreal(pdev->logicalDpiY()) / qreal(qt_defaultDpi());
172
173
0
    return size;
174
0
}
175
176
QTextImageHandler::QTextImageHandler(QObject *parent)
177
0
    : QObject(parent)
178
0
{
179
0
}
180
181
QSizeF QTextImageHandler::intrinsicSize(QTextDocument *doc, int posInDocument, const QTextFormat &format)
182
0
{
183
0
    Q_UNUSED(posInDocument);
184
0
    const QTextImageFormat imageFormat = format.toImageFormat();
185
186
0
    return getSize(doc, imageFormat);
187
0
}
188
189
QImage QTextImageHandler::image(QTextDocument *doc, const QTextImageFormat &imageFormat)
190
0
{
191
0
    Q_ASSERT(doc != nullptr);
192
193
0
    return getImage(doc, imageFormat);
194
0
}
195
196
void QTextImageHandler::drawObject(QPainter *p, const QRectF &rect, QTextDocument *doc, int posInDocument, const QTextFormat &format)
197
0
{
198
0
    Q_UNUSED(posInDocument);
199
0
        const QTextImageFormat imageFormat = format.toImageFormat();
200
201
0
    const QImage image = getImage(doc, imageFormat, p->device()->devicePixelRatio(), rect.size());
202
0
    p->drawImage(rect, image, image.rect());
203
0
}
204
205
QT_END_NAMESPACE
206
207
#include "moc_qtextimagehandler_p.cpp"