File indexing completed on 2024-05-12 04:06:13

0001 /*
0002     SPDX-FileCopyrightText: 2023 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: BSD-3-Clause
0005 */
0006 
0007 #include <QCommandLineParser>
0008 #include <QGuiApplication>
0009 #include <QImage>
0010 #include <QPainter>
0011 #include <QString>
0012 #include <QSvgRenderer>
0013 
0014 #include <iostream>
0015 
0016 using namespace Qt::Literals;
0017 
0018 int main(int argc, char **argv)
0019 {
0020     QGuiApplication app(argc, argv);
0021 
0022     QCommandLineParser parser;
0023     parser.addHelpOption();
0024     parser.addPositionalArgument(u"svg_file"_s, u"Input SVG file"_s);
0025     parser.addPositionalArgument(u"element_id"_s, u"SVG Element Id"_s);
0026     parser.addPositionalArgument(u"width"_s, u"Width"_s);
0027     parser.addPositionalArgument(u"height"_s, u"Height"_s);
0028     parser.addPositionalArgument(u"png_file"_s, u"Output PNG file"_s);
0029 
0030     parser.process(app);
0031 
0032     const QStringList args = parser.positionalArguments();
0033 
0034     if (args.size() < 5) {
0035         std::cout << qPrintable(parser.helpText());
0036         return -1;
0037     }
0038 
0039     const int width = args[2].toInt();
0040     const int height = args[3].toInt();
0041     const QString inputPath = args[0];
0042     const QString elementId = args[1];
0043     const QString outputPath = args[4];
0044 
0045     QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
0046     image.fill(Qt::transparent);
0047 
0048     QSvgRenderer renderer(inputPath);
0049     if (!renderer.isValid()) {
0050         return -1;
0051     }
0052 
0053     QPainter p(&image);
0054     if (elementId.isEmpty()) {
0055         renderer.render(&p);
0056     } else {
0057         renderer.render(&p, elementId);
0058     }
0059 
0060     image.save(outputPath, "PNG");
0061 
0062     return 0;
0063 }