Coverage Report

Created: 2026-04-29 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kio-extras/thumbnail/svgcreator.cpp
Line
Count
Source
1
/*  This file is part of the KDE libraries
2
    SPDX-FileCopyrightText: 2006 Pascal Létourneau <pascal.letourneau@kdemail.net>
3
4
    SPDX-License-Identifier: LGPL-2.0-or-later
5
*/
6
7
#include "svgcreator.h"
8
9
#include <QImage>
10
#include <QPainter>
11
#include <QSvgRenderer>
12
13
#include <KPluginFactory>
14
15
0
K_PLUGIN_CLASS_WITH_JSON(SvgCreator, "svgthumbnail.json")
Unexecuted instantiation: svgthumbnail_factory::tr(char const*, char const*, int)
Unexecuted instantiation: svgthumbnail_factory::~svgthumbnail_factory()
16
0
17
0
SvgCreator::SvgCreator(QObject *parent, const QVariantList &args)
18
9.67k
    : KIO::ThumbnailCreator(parent, args)
19
9.67k
{
20
9.67k
}
21
22
KIO::ThumbnailResult SvgCreator::create(const KIO::ThumbnailRequest &request)
23
9.67k
{
24
9.67k
    QSvgRenderer r(request.url().toLocalFile());
25
9.67k
    if (!r.isValid())
26
7.67k
        return KIO::ThumbnailResult::fail();
27
28
    // render using the correct ratio
29
1.99k
    const QSize defaultSize = r.defaultSize();
30
1.99k
    double ratio = 1.0;
31
1.99k
    if (defaultSize.width() > 0 && defaultSize.height() > 0) {
32
1.55k
        if (defaultSize.height() < request.targetSize().height() && defaultSize.width() < request.targetSize().width()) {
33
            // scale to output size if size is smaller than output
34
642
            ratio = 1.0 * defaultSize.height() / defaultSize.width();
35
911
        } else {
36
911
            ratio = static_cast<double>(defaultSize.height()) / static_cast<double>(defaultSize.width());
37
911
        }
38
1.55k
    }
39
40
1.99k
    int width = request.targetSize().width() * request.devicePixelRatio();
41
1.99k
    int height = request.targetSize().height() * request.devicePixelRatio();
42
43
1.99k
    if (defaultSize.height() > defaultSize.width()) {
44
450
        const double tmpHeight = width / ratio;
45
450
        if (tmpHeight <= std::numeric_limits<int>::max()) {
46
450
            height = qRound(tmpHeight);
47
450
        }
48
1.54k
    } else {
49
1.54k
        const double tmpWidth = height / ratio;
50
1.54k
        if (tmpWidth <= std::numeric_limits<int>::max()) {
51
1.51k
            width = qRound(tmpWidth);
52
1.51k
        }
53
1.54k
    }
54
55
1.99k
    QImage i(width, height, QImage::Format_ARGB32_Premultiplied);
56
1.99k
    i.fill(0);
57
1.99k
    QPainter p(&i);
58
1.99k
    r.render(&p, QRectF(QPointF(0, 0), QSizeF(width, height)));
59
60
1.99k
    return KIO::ThumbnailResult::pass(i);
61
9.67k
}
62
63
#include "moc_svgcreator.cpp"
64
#include "svgcreator.moc"