Commit 9a027090 authored by Dmitry Kazakov's avatar Dmitry Kazakov

Implement loading SVG files as a Document into Krita

parent 89386c44
......@@ -510,7 +510,11 @@ bool KisShapeLayer::saveLayer(KoStore * store) const
return true;
}
void KisShapeLayer::loadSvg(QIODevice *device, const QString &baseXmlDir)
QList<KoShape *> KisShapeLayer::createShapesFromSvg(QIODevice *device, const QString &baseXmlDir,
const QRectF &rectInPixels,
qreal resolutionPPI,
KoDocumentResourceManager *resourceManager,
QSizeF *fragmentSize)
{
QXmlStreamReader reader(device);
reader.setNamespaceProcessing(false);
......@@ -529,6 +533,14 @@ void KisShapeLayer::loadSvg(QIODevice *device, const QString &baseXmlDir)
, errorLine , errorColumn , errorMsg);
}
SvgParser parser(resourceManager);
parser.setXmlBaseDir(baseXmlDir);
parser.setResolution(rectInPixels /* px */, resolutionPPI /* ppi */);
return parser.parseSvg(doc.documentElement(), fragmentSize);
}
bool KisShapeLayer::loadSvg(QIODevice *device, const QString &baseXmlDir)
{
QSizeF fragmentSize; // unused!
KisImageSP image = this->image();
......@@ -536,15 +548,17 @@ void KisShapeLayer::loadSvg(QIODevice *device, const QString &baseXmlDir)
KIS_SAFE_ASSERT_RECOVER_NOOP(qFuzzyCompare(image->xRes(), image->yRes()));
const qreal resolutionPPI = 72.0 * image->xRes();
SvgParser parser(m_d->controller->resourceManager());
parser.setXmlBaseDir(baseXmlDir);
parser.setResolution(image->bounds() /* px */, resolutionPPI /* ppi */);
QList<KoShape*> shapes =
parser.parseSvg(doc.documentElement(), &fragmentSize);
createShapesFromSvg(device, baseXmlDir,
image->bounds(), resolutionPPI,
m_d->controller->resourceManager(),
&fragmentSize);
Q_FOREACH (KoShape *shape, shapes) {
addShape(shape);
}
return true;
}
bool KisShapeLayer::loadLayer(KoStore* store)
......
......@@ -34,6 +34,7 @@ class KoShapeManager;
class KoStore;
class KoViewConverter;
class KoShapeBasedDocumentBase;
class KoDocumentResourceManager;
const QString KIS_SHAPE_LAYER_ID = "KisShapeLayer";
/**
......@@ -106,9 +107,14 @@ public:
bool saveLayer(KoStore * store) const;
void loadSvg(QIODevice *device, const QString &baseXmlDir);
bool loadSvg(QIODevice *device, const QString &baseXmlDir);
bool loadLayer(KoStore* store);
static QList<KoShape*> createShapesFromSvg(QIODevice *device, const QString &baseXmlDir,
const QRectF &rectInPixels, qreal resolutionPPI,
KoDocumentResourceManager *resourceManager,
QSizeF *fragmentSize);
KUndo2Command* crop(const QRect & rect);
KUndo2Command* transform(const QTransform &transform);
......
......@@ -29,6 +29,8 @@ if(LIBRAW_FOUND)
add_subdirectory(raw)
endif()
add_subdirectory(svg)
add_subdirectory(bmp)
add_subdirectory(ora)
add_subdirectory(ppm)
......
......@@ -133,7 +133,7 @@ KisImportExportFilter::ConversionStatus KisODGImport::convert(const QByteArray&
KoShapeLoadingContext shapeContext(context, doc->shapeController()->resourceManager());
const KoColorSpace* cs = KoColorSpaceRegistry::instance()->rgb8();
KisImageWSP image = new KisImage(doc->createUndoStore(), width, height, cs, "built image");
KisImageSP image = new KisImage(doc->createUndoStore(), width, height, cs, "built image");
doc->setCurrentImage(image);
KoXmlElement layerElement;
......
add_subdirectory(tests)
set(kritasvgimport_SOURCES
kis_svg_import.cc
)
add_library(kritasvgimport MODULE ${kritasvgimport_SOURCES})
add_definitions(${SVG_DEFINITIONS} ${KDE4_ENABLE_EXCEPTIONS})
target_link_libraries(kritasvgimport kritaui )
install(TARGETS kritasvgimport DESTINATION ${KRITA_PLUGIN_INSTALL_DIR})
install( PROGRAMS krita_svg.desktop DESTINATION ${XDG_APPS_INSTALL_DIR})
/*
* Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "kis_svg_import.h"
#include <kpluginfactory.h>
#include <QFileInfo>
#include <KisFilterChain.h>
#include <KisImportExportManager.h>
#include <KisDocument.h>
#include <kis_image.h>
#include <KisViewManager.h>
K_PLUGIN_FACTORY_WITH_JSON(SVGImportFactory, "krita_svg_import.json", registerPlugin<KisSVGImport>();)
KisSVGImport::KisSVGImport(QObject *parent, const QVariantList &) : KisImportExportFilter(parent)
{
}
KisSVGImport::~KisSVGImport()
{
}
#include <SvgParser.h>
#include <KoColorSpaceRegistry.h>
#include "kis_shape_layer.h"
#include <KoShapeBasedDocumentBase.h>
KisImportExportFilter::ConversionStatus KisSVGImport::convert(const QByteArray&, const QByteArray& to, KisPropertiesConfigurationSP configuration)
{
dbgFile << "Importing using SVGImport!";
if (to != "application/x-krita")
return KisImportExportFilter::BadMimeType;
KisDocument * doc = outputDocument();
if (!doc) {
return KisImportExportFilter::NoDocumentCreated;
}
QString filename = inputFile();
ENTER_FUNCTION() << ppVar(filename);
doc -> prepareForImport();
if (filename.isEmpty() || !QFileInfo(filename).exists()) {
return KisImportExportFilter::FileNotFound;
}
const QString baseXmlDir = QFileInfo(filename).canonicalPath();
QFile file(filename);
file.open(QIODevice::ReadOnly);
const qreal resolutionPPI = 100;
const qreal resolution = resolutionPPI / 72.0;
QSizeF fragmentSize;
QList<KoShape*> shapes =
KisShapeLayer::createShapesFromSvg(&file, baseXmlDir,
QRectF(0,0,1200,800), resolutionPPI,
doc->shapeController()->resourceManager(),
&fragmentSize);
ENTER_FUNCTION() << ppVar(shapes.size());
QRectF rawImageRect(QPointF(), fragmentSize);
QRect imageRect(rawImageRect.toAlignedRect());
const KoColorSpace* cs = KoColorSpaceRegistry::instance()->rgb8();
KisImageSP image = new KisImage(doc->createUndoStore(), imageRect.width(), imageRect.height(), cs, "svg image");
image->setResolution(resolution, resolution);
doc->setCurrentImage(image);
KisShapeLayerSP shapeLayer =
new KisShapeLayer(doc->shapeController(), image,
i18n("Vector Layer"),
OPACITY_OPAQUE_U8);
// if (!shapes.isEmpty()) {
// dbgKrita << "Could not load vector layer!";
// return KisImportExportFilter::CreationError;
// }
Q_FOREACH (KoShape *shape, shapes) {
shapeLayer->addShape(shape);
}
image->addNode(shapeLayer);
return KisImportExportFilter::OK;
}
#include <kis_svg_import.moc>
/*
* Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef _KIS_SVG_IMPORT_H_
#define _KIS_SVG_IMPORT_H_
#include <QVariant>
#include <KisImportExportFilter.h>
class KisSVGImport : public KisImportExportFilter
{
Q_OBJECT
public:
KisSVGImport(QObject *parent, const QVariantList &);
virtual ~KisSVGImport();
public:
virtual KisImportExportFilter::ConversionStatus convert(const QByteArray& from, const QByteArray& to, KisPropertiesConfigurationSP configuration = 0);
};
#endif
[Desktop Entry]
Name=Krita
Name[af]=Krita
Name[bg]=Krita
Name[br]=Krita
Name[bs]=Krita
Name[ca]=Krita
Name[ca@valencia]=Krita
Name[cs]=Krita
Name[cy]=Krita
Name[da]=Krita
Name[de]=Krita
Name[el]=Krita
Name[en_GB]=Krita
Name[eo]=Krita
Name[es]=Krita
Name[et]=Krita
Name[eu]=Krita
Name[fi]=Krita
Name[fr]=Krita
Name[fy]=Krita
Name[ga]=Krita
Name[gl]=Krita
Name[he]=Krita
Name[hi]=केरिता
Name[hne]=केरिता
Name[hr]=Krita
Name[hu]=Krita
Name[ia]=Krita
Name[is]=Krita
Name[it]=Krita
Name[kk]=Krita
Name[ko]=Krita
Name[lt]=Krita
Name[lv]=Krita
Name[mr]=क्रिटा
Name[ms]=Krita
Name[nb]=Krita
Name[nds]=Krita
Name[ne]=क्रिता
Name[nl]=Krita
Name[pl]=Krita
Name[pt]=Krita
Name[pt_BR]=Krita
Name[ro]=Krita
Name[ru]=Krita
Name[se]=Krita
Name[sk]=Krita
Name[sl]=Krita
Name[sv]=Krita
Name[ta]=கிரிட்டா
Name[tg]=Krita
Name[tr]=Krita
Name[ug]=Krita
Name[uk]=Krita
Name[uz]=Krita
Name[uz@cyrillic]=Krita
Name[wa]=Krita
Name[xh]=Krita
Name[x-test]=xxKritaxx
Name[zh_CN]=Krita
Name[zh_TW]=繪圖_Krita
Exec=krita %u
MimeType=image/svg;
Type=Application
Icon=calligrakrita
Categories=Qt;KDE;Office;Graphics;
StartupNotify=true
NoDisplay=true
{
"Id": "Krita SVG Import Filter",
"NoDisplay": "true",
"Type": "Service",
"X-KDE-Export": "application/x-krita",
"X-KDE-Import": "image/svg+xml",
"X-KDE-Library": "kritasvgimport",
"X-KDE-ServiceTypes": [
"Krita/FileFilter"
],
"X-KDE-Weight": "1",
"X-KDE-Extensions" : "svg"
}
set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} )
include_directories( ${CMAKE_SOURCE_DIR}/sdk/tests )
macro_add_unittest_definitions()
set(kis_svg_test_SRCS kis_svg_test.cpp )
kde4_add_broken_unit_test(kis_svg_test TESTNAME krita-plugins-formats-svg_test ${kis_svg_test_SRCS})
target_link_libraries(kis_svg_test kritaui Qt5::Test)
This source diff could not be displayed because it is too large. You can view the blob instead.
/*
* Copyright (C) 2007 Cyrille Berger <cberger@cberger.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "kis_svg_test.h"
#include <QTest>
#include <QCoreApplication>
#include <QTest>
#include "filestest.h"
#ifndef FILES_DATA_DIR
#error "FILES_DATA_DIR not set. A directory with the data used for testing the importing of files in krita"
#endif
void KisSvgTest::testFiles()
{
TestUtil::testFiles(QString(FILES_DATA_DIR) + "/sources", QStringList());
}
QTEST_MAIN(KisSvgTest)
/*
* Copyright (C) 2007 Cyrille Berger <cberger@cberger.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef _KIS_SVG_TEST_H_
#define _KIS_SVG_TEST_H_
#include <QtTest>
class KisSvgTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testFiles();
};
#endif
......@@ -100,7 +100,8 @@ void testFiles(const QString& _dirname, const QStringList& exclusions, const QSt
if (!TestUtil::compareQImages(pt, resultImage, sourceImage, fuzzy)) {
failuresCompare << sourceFileInfo.fileName() + ": " + QString("Pixel (%1,%2) has different values").arg(pt.x()).arg(pt.y()).toLatin1();
resultImage.save(sourceFileInfo.fileName() + ".png");
sourceImage.save(sourceFileInfo.fileName() + ".png");
resultImage.save(resultFileInfo.fileName() + ".expected.png");
continue;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment