Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Parse and write SVG title and desc elements.

Wolthera van Hövell tot Westerflier14540878051b
diff --git a/libs/flake/svg/SvgParser.cpp b/libs/flake/svg/SvgParser.cpp
index 29bd8d8501..e856e61284 100644
--- a/libs/flake/svg/SvgParser.cpp
+++ b/libs/flake/svg/SvgParser.cpp
@@ -844,6 +844,26 @@ bool SvgParser::parseSymbol(const QDomElement &e)
return true;
}
+void SvgParser::parseMetadataApplyToShape(const QDomElement &e, KoShape *shape)
+{
+ const QString titleTag = "title";
+ const QString descriptionTag = "desc";
+ QDomElement title = e.firstChildElement(titleTag);
+ if (!title.isNull()) {
+ QDomText text = getTheOnlyTextChild(title);
+ if (!text.data().isEmpty()) {
+ shape->setAdditionalAttribute(titleTag, text.data());
+ }
+ }
+ QDomElement description = e.firstChildElement(descriptionTag);
+ if (!description.isNull()) {
+ QDomText text = getTheOnlyTextChild(description);
+ if (!text.data().isEmpty()) {
+ shape->setAdditionalAttribute(descriptionTag, text.data());
+ }
+ }
+}
+
bool SvgParser::parseClipPath(const QDomElement &e)
{
SvgClipPathHelper clipPath;
@@ -1729,6 +1749,8 @@ KoShape* SvgParser::parseGroup(const QDomElement &b, const QDomElement &override
applyCurrentStyle(group, extraOffset); // apply style to this group after size is set
+ parseMetadataApplyToShape(b, group);
+
if (createContext) {
m_context.popGraphicsContext();
}
@@ -1736,7 +1758,7 @@ KoShape* SvgParser::parseGroup(const QDomElement &b, const QDomElement &override
return group;
}
-QDomText getTheOnlyTextChild(const QDomElement &e)
+QDomText SvgParser::getTheOnlyTextChild(const QDomElement &e)
{
QDomNode firstChild = e.firstChild();
return !firstChild.isNull() && firstChild == e.lastChild() && firstChild.isText() ?
@@ -1777,6 +1799,7 @@ void SvgParser::parseTextChildren(const QDomElement &e, KoSvgTextLoader &textLoa
textLoader.enterNodeSubtree();
for (QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) {
QDomElement b = n.toElement();
+ if (b.tagName() == "title" || b.tagName() == "desc") continue; /// TODO: we should skip dublin core metadata too...
textLoader.nextNode();
if (b.isNull()) {
textLoader.loadSvgText(n.toText(), m_context);
@@ -1844,6 +1867,8 @@ KoShape *SvgParser::parseTextElement(const QDomElement &e, KoSvgTextShape *merge
}
}
+ parseMetadataApplyToShape(e, rootTextShape);
+
if (!mergeIntoShape) {
rootTextShape->setZIndex(m_context.nextZIndex());
}
@@ -1902,7 +1927,7 @@ KoShape *SvgParser::parseTextElement(const QDomElement &e, KoSvgTextShape *merge
return rootTextShape;
}
-QList<KoShape*> SvgParser::parseContainer(const QDomElement &e, bool parseTextNodes)
+QList<KoShape*> SvgParser::parseContainer(const QDomElement &e)
{
QList<KoShape*> shapes;
@@ -2123,6 +2148,7 @@ KoShape * SvgParser::createObjectDirect(const QDomElement &b)
// handle id
applyId(b.attribute("id"), obj);
obj->setZIndex(m_context.nextZIndex());
+ parseMetadataApplyToShape(b, obj);
}
m_context.popGraphicsContext();
@@ -2149,6 +2175,7 @@ KoShape * SvgParser::createObject(const QDomElement &b, const SvgStyles &style)
// handle id
applyId(b.attribute("id"), obj);
obj->setZIndex(m_context.nextZIndex());
+ parseMetadataApplyToShape(b, obj);
}
m_context.popGraphicsContext();
diff --git a/libs/flake/svg/SvgParser.h b/libs/flake/svg/SvgParser.h
index 32034e234a..b4ffcef7cd 100644
--- a/libs/flake/svg/SvgParser.h
+++ b/libs/flake/svg/SvgParser.h
@@ -34,6 +34,7 @@ class KoMarker;
class KoPathShape;
class KoSvgTextShape;
class KoSvgTextLoader;
+class QDomText;
class KRITAFLAKE_EXPORT SvgParser
{
@@ -90,7 +91,7 @@ protected:
void parseTextChildren(const QDomElement &e, KoSvgTextLoader &textLoader);
/// Parses a container element, returning a list of child shapes
- QList<KoShape*> parseContainer(const QDomElement &, bool parseTextNodes = false);
+ QList<KoShape*> parseContainer(const QDomElement &);
/// XXX
QList<KoShape*> parseSingleElement(const QDomElement &b, DeferredUseStore* deferredUseStore = 0);
@@ -123,6 +124,9 @@ protected:
bool parseSymbol(const QDomElement &e);
+ /// This parses the SVG native title and desc elements and adds them into additional attributes.
+ void parseMetadataApplyToShape(const QDomElement &e, KoShape *shape);
+
/// parses a length attribute
qreal parseUnit(const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF());
@@ -209,6 +213,8 @@ protected:
/// NOTE: after applying the function currentBoundingBox can become null!
void applyViewBoxTransform(const QDomElement &element);
+ QDomText getTheOnlyTextChild(const QDomElement &e);
+
private:
SvgLoadingContext m_context;
QMap<QString, SvgGradientHelper> m_gradients;
diff --git a/libs/flake/svg/SvgShape.h b/libs/flake/svg/SvgShape.h
index 3159db829f..19115dab8c 100644
--- a/libs/flake/svg/SvgShape.h
+++ b/libs/flake/svg/SvgShape.h
@@ -24,6 +24,8 @@ public:
/// Loads data from specified svg element
virtual bool loadSvg(const QDomElement &element, SvgLoadingContext &context);
+
+ void saveMetadata(SvgSavingContext &context);
};
#endif // SVGSHAPE_H
diff --git a/libs/flake/svg/SvgStyleWriter.cpp b/libs/flake/svg/SvgStyleWriter.cpp
index 67b0f42ab8..5fe673e956 100644
--- a/libs/flake/svg/SvgStyleWriter.cpp
+++ b/libs/flake/svg/SvgStyleWriter.cpp
@@ -234,6 +234,22 @@ QString SvgStyleWriter::embedShape(const KoShape *shape, SvgSavingContext &conte
return uid;
}
+void SvgStyleWriter::saveMetadata(const KoShape *shape, SvgSavingContext &context)
+{
+ const QString title = shape->additionalAttribute("title");
+ if (!title.trimmed().isEmpty()) {
+ context.shapeWriter().startElement("title");
+ context.shapeWriter().addTextNode(title);
+ context.shapeWriter().endElement();
+ }
+ const QString desc = shape->additionalAttribute("desc");
+ if (!desc.trimmed().isEmpty()) {
+ context.shapeWriter().startElement("desc");
+ context.shapeWriter().addTextNode(desc);
+ context.shapeWriter().endElement();
+ }
+}
+
void SvgStyleWriter::saveSvgClipping(KoShape *shape, SvgSavingContext &context)
{
KoClipPath *clipPath = shape->clipPath();
diff --git a/libs/flake/svg/SvgStyleWriter.h b/libs/flake/svg/SvgStyleWriter.h
index 68de6c1f65..96b124e990 100644
--- a/libs/flake/svg/SvgStyleWriter.h
+++ b/libs/flake/svg/SvgStyleWriter.h
@@ -49,6 +49,9 @@ public:
// embed the given shape, returns an id to refer to.
static QString embedShape(const KoShape *shape, SvgSavingContext &context);
+ // Save title and desc elements.
+ static void saveMetadata(const KoShape *shape, SvgSavingContext &context);
+
protected:
/// Saves effects of specified shape
static void saveSvgEffects(KoShape *shape, SvgSavingContext &context);
diff --git a/libs/flake/svg/SvgWriter.cpp b/libs/flake/svg/SvgWriter.cpp
index 40d9cb3235..1556a94977 100644
--- a/libs/flake/svg/SvgWriter.cpp
+++ b/libs/flake/svg/SvgWriter.cpp
@@ -190,6 +190,9 @@ void SvgWriter::saveGroup(KoShapeGroup * group, SvgSavingContext &context)
SvgUtil::writeTransformAttributeLazy("transform", group->transformation(), context.shapeWriter());
+
+ SvgStyleWriter::saveMetadata(group, context);
+
SvgStyleWriter::saveSvgStyle(group, context);
QList<KoShape*> sortedShapes = group->shapes();
@@ -227,6 +230,7 @@ void SvgWriter::savePath(KoPathShape *path, SvgSavingContext &context)
context.shapeWriter().addAttribute("id", context.getID(path));
SvgUtil::writeTransformAttributeLazy("transform", path->transformation(), context.shapeWriter());
+ SvgStyleWriter::saveMetadata(path, context);
SvgStyleWriter::saveSvgStyle(path, context);
diff --git a/libs/flake/tests/TestSvgParser.cpp b/libs/flake/tests/TestSvgParser.cpp
index d6c3833bc6..463ad4e224 100644
--- a/libs/flake/tests/TestSvgParser.cpp
+++ b/libs/flake/tests/TestSvgParser.cpp
@@ -4226,4 +4226,33 @@ void TestSvgParser::testSodipodiChordShape()
}
+
+
+void TestSvgParser::testDescAndTitleParsing()
+{
+ const QString title = "Test Rectangle";
+ const QString description = "A description for the test rect";
+ const QString dataBefore =
+ "<svg width=\"30px\" height=\"30px\""
+ " xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\""
+ " xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""
+ ">"
+ "<path"
+ " fill=\"red\" stroke=\"black\""
+ " id=\"testRect\""
+ " d=\"M5,15 C5,5 25,5 25,15 L15,25\">";
+ const QString dataAfter = "</path></svg>";
+
+ const QString data = dataBefore + "<title>"+title+"</title>" + "<desc>"+description+"</desc>" + dataAfter;
+
+ SvgTester t (data);
+ t.parser().setResolution(QRectF(0, 0, 380, 380) /* px */, 72 /* ppi */);
+ t.run();
+ KoShape *shape = t.findShape("testRect");
+
+ QVERIFY(shape);
+ QVERIFY(shape->additionalAttribute("title") == title);
+ QVERIFY(shape->additionalAttribute("desc") == description);
+}
+
KISTEST_MAIN(TestSvgParser)
diff --git a/libs/flake/tests/TestSvgParser.h b/libs/flake/tests/TestSvgParser.h
index 4411974f61..76cfa2d8fd 100644
--- a/libs/flake/tests/TestSvgParser.h
+++ b/libs/flake/tests/TestSvgParser.h
@@ -178,6 +178,8 @@ private Q_SLOTS:
void testRenderPaintOrderProperty_data();
void testRenderPaintOrderProperty();
+
+ void testDescAndTitleParsing();
private:
};
diff --git a/libs/flake/text/KoSvgTextShape.cpp b/libs/flake/text/KoSvgTextShape.cpp
index fd53593340..31c881e568 100644
--- a/libs/flake/text/KoSvgTextShape.cpp
+++ b/libs/flake/text/KoSvgTextShape.cpp
@@ -1178,6 +1178,7 @@ bool KoSvgTextShape::saveSvg(SvgSavingContext &context)
}
if (it == d->textData.compositionBegin()) {
context.shapeWriter().startElement("text", false);
+ SvgStyleWriter::saveMetadata(this, context);
if (!context.strippedTextMode()) {
context.shapeWriter().addAttribute("id", context.getID(this));
diff --git a/plugins/flake/imageshape/ImageShape.cpp b/plugins/flake/imageshape/ImageShape.cpp
index b26660c319..c863a4c32f 100644
--- a/plugins/flake/imageshape/ImageShape.cpp
+++ b/plugins/flake/imageshape/ImageShape.cpp
@@ -13,6 +13,7 @@
#include <SvgLoadingContext.h>
#include <SvgSavingContext.h>
#include <SvgUtil.h>
+#include <SvgStyleWriter.h>
#include <QBuffer>
#include <KisMimeDatabase.h>
#include <KoXmlWriter.h>
@@ -96,6 +97,7 @@ bool ImageShape::saveSvg(SvgSavingContext &context)
const QString mimeType = KisMimeDatabase::mimeTypeForSuffix("*.png");
context.shapeWriter().addAttribute("xlink:href", "data:"+ mimeType + ";base64," + buffer.data().toBase64());
}
+ SvgStyleWriter::saveMetadata(this, context);
context.shapeWriter().endElement(); // image
diff --git a/plugins/flake/pathshapes/ellipse/EllipseShape.cpp b/plugins/flake/pathshapes/ellipse/EllipseShape.cpp
index 1c1dbd231d..13635e1ddb 100644
--- a/plugins/flake/pathshapes/ellipse/EllipseShape.cpp
+++ b/plugins/flake/pathshapes/ellipse/EllipseShape.cpp
@@ -328,6 +328,7 @@ bool EllipseShape::saveSvg(SvgSavingContext &context)
context.shapeWriter().startElement(isCircle ? "circle" : "ellipse");
context.shapeWriter().addAttribute("id", context.getID(this));
SvgUtil::writeTransformAttributeLazy("transform", transformation(), context.shapeWriter());
+ SvgStyleWriter::saveMetadata(this, context);
if (isCircle) {
context.shapeWriter().addAttribute("r", 0.5 * size.width());
diff --git a/plugins/flake/pathshapes/rectangle/RectangleShape.cpp b/plugins/flake/pathshapes/rectangle/RectangleShape.cpp
index e06d59d455..bd5f784d87 100644
--- a/plugins/flake/pathshapes/rectangle/RectangleShape.cpp
+++ b/plugins/flake/pathshapes/rectangle/RectangleShape.cpp
@@ -273,6 +273,7 @@ bool RectangleShape::saveSvg(SvgSavingContext &context)
SvgUtil::writeTransformAttributeLazy("transform", transformation(), context.shapeWriter());
SvgStyleWriter::saveSvgStyle(this, context);
+ SvgStyleWriter::saveMetadata(this, context);
const QSizeF size = this->size();
context.shapeWriter().addAttribute("width", size.width());