File indexing completed on 2025-02-02 04:21:51

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "kis_svg_import.h"
0008 
0009 #include <kpluginfactory.h>
0010 #include <QFileInfo>
0011 #include "kis_config.h"
0012 
0013 #include <QInputDialog>
0014 #include <KisDocument.h>
0015 #include <kis_image.h>
0016 
0017 #include <SvgParser.h>
0018 #include <KoColorSpaceRegistry.h>
0019 #include "kis_shape_layer.h"
0020 #include <KoShapeControllerBase.h>
0021 
0022 K_PLUGIN_FACTORY_WITH_JSON(SVGImportFactory, "krita_svg_import.json", registerPlugin<KisSVGImport>();)
0023 
0024 KisSVGImport::KisSVGImport(QObject *parent, const QVariantList &) : KisImportExportFilter(parent)
0025 {
0026 }
0027 
0028 KisSVGImport::~KisSVGImport()
0029 {
0030 }
0031 
0032 KisImportExportErrorCode KisSVGImport::convert(KisDocument *document, QIODevice *io,  KisPropertiesConfigurationSP configuration)
0033 {
0034     Q_UNUSED(configuration);
0035 
0036     KisDocument * doc = document;
0037 
0038     const QString baseXmlDir = QFileInfo(filename()).canonicalPath();
0039 
0040     KisConfig cfg(false);
0041 
0042     qreal resolutionPPI = cfg.preferredVectorImportResolutionPPI(true);
0043 
0044     if (!batchMode()) {
0045         bool okay = false;
0046         const QString name = QFileInfo(filename()).fileName();
0047         resolutionPPI = QInputDialog::getInt(0,
0048                                              i18n("Import SVG"),
0049                                              i18n("Enter preferred resolution (PPI) for \"%1\"", name),
0050                                              cfg.preferredVectorImportResolutionPPI(),
0051                                              0, 100000, 1, &okay);
0052 
0053         if (!okay) {
0054             return ImportExportCodes::Cancelled;
0055         }
0056 
0057         cfg.setPreferredVectorImportResolutionPPI(resolutionPPI);
0058     }
0059 
0060     const qreal resolution = resolutionPPI;
0061 
0062     QStringList warnings;
0063     QStringList errors;
0064 
0065     QSizeF fragmentSize;
0066     QList<KoShape*> shapes =
0067             KisShapeLayer::createShapesFromSvg(io, baseXmlDir,
0068                                                QRectF(0,0,1200,800), resolutionPPI,
0069                                                doc->shapeController()->resourceManager(),
0070                                                false,
0071                                                &fragmentSize,
0072                                                &warnings, &errors);
0073 
0074     if (!warnings.isEmpty()) {
0075         doc->setWarningMessage(warnings.join('\n'));
0076     }
0077     if (!errors.isEmpty()) {
0078         doc->setErrorMessage(errors.join('\n'));
0079         return ImportExportCodes::FileFormatIncorrect;
0080     }
0081 
0082 
0083     QRectF rawImageRect(QPointF(), fragmentSize);
0084     QRect imageRect(rawImageRect.toAlignedRect());
0085 
0086     const KoColorSpace* cs = KoColorSpaceRegistry::instance()->rgb8();
0087     KisImageSP image = new KisImage(doc->createUndoStore(), imageRect.width(), imageRect.height(), cs, "svg image");
0088     image->setResolution(resolution / 72.0, resolution / 72.0);
0089     doc->setCurrentImage(image);
0090 
0091     KisShapeLayerSP shapeLayer =
0092             new KisShapeLayer(doc->shapeController(), image,
0093                               i18n("Vector Layer"),
0094                               OPACITY_OPAQUE_U8);
0095 
0096     Q_FOREACH (KoShape *shape, shapes) {
0097         shapeLayer->addShape(shape);
0098     }
0099 
0100     image->addNode(shapeLayer);
0101     return ImportExportCodes::OK;
0102 }
0103 
0104 #include <kis_svg_import.moc>