From c42a1f33b93981b5458458f3376031184014a640 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Tue, 1 Feb 2022 05:48:58 +0200 Subject: [PATCH] pics: reimplement ksvgtopng utility Signed-off-by: Ivailo Monev --- pics/CMakeLists.txt | 2 +- pics/ksvgtopng.cpp | 97 +++++++++++++++++++++++++---------------------------- 2 files changed, 47 insertions(+), 52 deletions(-) diff --git a/pics/CMakeLists.txt b/pics/CMakeLists.txt index 30afe897..bda57014 100644 --- a/pics/CMakeLists.txt +++ b/pics/CMakeLists.txt @@ -2,5 +2,5 @@ add_subdirectory( emoticons ) set(ksvgtopng_SRCS ksvgtopng.cpp ) add_executable(ksvgtopng ${ksvgtopng_SRCS}) -target_link_libraries(ksvgtopng ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTSVG_LIBRARY}) +target_link_libraries(ksvgtopng ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY}) install(TARGETS ksvgtopng ${INSTALL_TARGETS_DEFAULT_ARGS} ) diff --git a/pics/ksvgtopng.cpp b/pics/ksvgtopng.cpp index d50ba84b..3204e1ba 100644 --- a/pics/ksvgtopng.cpp +++ b/pics/ksvgtopng.cpp @@ -1,57 +1,52 @@ +/* This file is part of the KDE Project + Copyright (C) 2022 Ivailo Monev + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2, as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + #include #include -#include - -#include -#include -#include - -using std::cout; -using std::endl; +#include int main(int argc, char **argv) { - // Initialize Qt application, otherwise for some svg files it can segfault with: - // ASSERT failure in QFontDatabase: "A QApplication object needs to be - // constructed before FontConfig is used." - QApplication app(argc, argv); - - if(argc < 5) - { - cout << "Usage : ksvgtopng width height svgfilename outputfilename" << endl; - cout << "Please use full path name for svgfilename" << endl; - return -1; - } - - int width = atoi(argv[1]); - int height = atoi(argv[2]); - - QImage img(width, height, QImage::Format_ARGB32_Premultiplied); - img.fill(0); - - QSvgRenderer renderer(QString::fromLocal8Bit(argv[3])); - if(renderer.isValid()) - { - QPainter p(&img); - renderer.render(&p); -/* - // Apply icon sharpening - double factor = 0; - - if(width == 16) - factor = 30; - else if(width == 32) - factor = 20; - else if(width == 48) - factor = 10; - else if(width == 64) - factor = 5; - - *img = KImageEffect::sharpen(*img, factor); // use QImageBlitz::sharpen() -*/ - } - - img.save(argv[4], "PNG"); - - return 0; + // Initialize Qt application, otherwise for some svg files it can segfault with: + // ASSERT failure in QFontDatabase: "A QApplication object needs to be + // constructed before FontConfig is used." + QApplication app(argc, argv); + + if(argc < 5) { + qDebug() << "Usage : ksvgtopng width height svgfilename outputfilename"; + qDebug() << "Please use full path name for svgfilename"; + return 1; + } + + QImage image(argv[3], "SVG"); + if (image.isNull()) { + qWarning() << "Could not load" << argv[3]; + return 2; + } + + const int width = atoi(argv[1]); + const int height = atoi(argv[2]); + image = image.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + + if (image.save(argv[4], "PNG") == false) { + qWarning() << "Could not save" << argv[4]; + return 4; + } + + return 0; } -- 2.11.0