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