Coverage Report

Created: 2026-07-15 07:31

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
26.0k
    : KIO::ThumbnailCreator(parent, args)
19
26.0k
{
20
26.0k
}
21
22
KIO::ThumbnailResult SvgCreator::create(const KIO::ThumbnailRequest &request)
23
26.0k
{
24
26.0k
    QSvgRenderer r(request.url().toLocalFile());
25
26.0k
    if (!r.isValid())
26
18.8k
        return KIO::ThumbnailResult::fail();
27
28
    // render using the correct ratio
29
7.25k
    const QSize defaultSize = r.defaultSize();
30
7.25k
    double ratio = 1.0;
31
7.25k
    if (defaultSize.width() > 0 && defaultSize.height() > 0) {
32
5.46k
        if (defaultSize.height() < request.targetSize().height() && defaultSize.width() < request.targetSize().width()) {
33
            // scale to output size if size is smaller than output
34
3.00k
            ratio = 1.0 * defaultSize.height() / defaultSize.width();
35
3.00k
        } else {
36
2.45k
            ratio = static_cast<double>(defaultSize.height()) / static_cast<double>(defaultSize.width());
37
2.45k
        }
38
5.46k
    }
39
40
7.25k
    int width = request.targetSize().width() * request.devicePixelRatio();
41
7.25k
    int height = request.targetSize().height() * request.devicePixelRatio();
42
43
7.25k
    if (defaultSize.height() > defaultSize.width()) {
44
1.34k
        const double tmpHeight = width * ratio;
45
1.34k
        if (tmpHeight <= std::numeric_limits<int>::max()) {
46
1.24k
            height = qRound(tmpHeight);
47
1.24k
        }
48
5.90k
    } else {
49
5.90k
        const double tmpWidth = height / ratio;
50
5.90k
        if (tmpWidth <= std::numeric_limits<int>::max()) {
51
5.72k
            width = qRound(tmpWidth);
52
5.72k
        }
53
5.90k
    }
54
55
7.25k
    QImage i(width, height, QImage::Format_ARGB32_Premultiplied);
56
7.25k
    i.fill(0);
57
7.25k
    QPainter p(&i);
58
7.25k
    r.render(&p, QRectF(QPointF(0, 0), QSizeF(width, height)));
59
60
7.25k
    return KIO::ThumbnailResult::pass(i);
61
26.0k
}
62
63
#include "moc_svgcreator.cpp"
64
#include "svgcreator.moc"