Commit 59fcbc5f authored by Boudewijn Rempt's avatar Boudewijn Rempt

Parse symbols in the defs section and place them in a separate vector

parent d465bbfd
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
#include <SvgParser.h> #include <SvgParser.h>
struct KoSvgSymbolCollectionResource::Private { struct KoSvgSymbolCollectionResource::Private {
QVector<KoSvgSymbol> symbols; QVector<KoSvgSymbol*> symbols;
}; };
...@@ -108,10 +108,9 @@ bool KoSvgSymbolCollectionResource::loadFromDevice(QIODevice *dev) ...@@ -108,10 +108,9 @@ bool KoSvgSymbolCollectionResource::loadFromDevice(QIODevice *dev)
SvgParser parser(&manager); SvgParser parser(&manager);
parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values
QSizeF fragmentSize; QSizeF fragmentSize;
QList<KoShape*> shapes = parser.parseSvg(doc.documentElement(), &fragmentSize); parser.parseSvg(doc.documentElement(), &fragmentSize);
d->symbols = parser.takeSymbols();
KoViewConverter converter; qDebug() << "Loaded" << filename() << "got" << d->symbols.size() << "symbols";
KoShapePaintingContext context;
return true; return true;
} }
......
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
#include <resources/KoResource.h> #include <resources/KoResource.h>
class KoShape; #include <KoShape.h>
#include "kritaflake_export.h" #include "kritaflake_export.h"
...@@ -35,6 +36,11 @@ struct KoSvgSymbol { ...@@ -35,6 +36,11 @@ struct KoSvgSymbol {
KoSvgSymbol(const QString &_title) KoSvgSymbol(const QString &_title)
: title(_title) {} : title(_title) {}
~KoSvgSymbol()
{
delete shape;
}
QString id; QString id;
QString title; QString title;
KoShape *shape; KoShape *shape;
......
...@@ -111,9 +111,11 @@ QList<KoShape*> SvgParser::shapes() const ...@@ -111,9 +111,11 @@ QList<KoShape*> SvgParser::shapes() const
return m_shapes; return m_shapes;
} }
QVector<KoSvgSymbol *> SvgParser::symbols() const QVector<KoSvgSymbol *> SvgParser::takeSymbols()
{ {
return m_symbols; QVector<KoSvgSymbol*> symbols = m_symbols;
m_symbols.clear();
return symbols;
} }
// Helper functions // Helper functions
...@@ -416,7 +418,7 @@ QSharedPointer<KoVectorPatternBackground> SvgParser::parsePattern(const KoXmlEle ...@@ -416,7 +418,7 @@ QSharedPointer<KoVectorPatternBackground> SvgParser::parsePattern(const KoXmlEle
* the pattern should be painted in "user" coordinates. Therefore, we should handle * the pattern should be painted in "user" coordinates. Therefore, we should handle
* this offfset separately. * this offfset separately.
* *
* TODO: Please also not that this offset is different from extraShapeOffset(), * TODO: Please also note that this offset is different from extraShapeOffset(),
* because A.inverted() * B != A * B.inverted(). I'm not sure which variant is * because A.inverted() * B != A * B.inverted(). I'm not sure which variant is
* correct (DK) * correct (DK)
*/ */
...@@ -585,6 +587,38 @@ bool SvgParser::parseMarker(const KoXmlElement &e) ...@@ -585,6 +587,38 @@ bool SvgParser::parseMarker(const KoXmlElement &e)
return true; return true;
} }
bool SvgParser::parseSymbol(const KoXmlElement &e)
{
const QString id = e.attribute("id");
qDebug() << "parsing symbol" << id << e.text();
if (id.isEmpty()) return false;
KoSvgSymbol *svgSymbol = new KoSvgSymbol();
// ensure that the clip path is loaded in local coordinates system
m_context.pushGraphicsContext(e, false);
m_context.currentGC()->matrix = QTransform();
m_context.currentGC()->currentBoundingBox = QRectF(0.0, 0.0, 1.0, 1.0);
QString title = e.firstChildElement("title").toElement().text();
KoShape *symbolShape = parseGroup(e);
m_context.popGraphicsContext();
if (!symbolShape) return false;
svgSymbol->shape = symbolShape;
svgSymbol->title = title;
qDebug() << "created symbol" << svgSymbol->id << svgSymbol->title << svgSymbol->shape->boundingRect();
m_symbols << svgSymbol;
return true;
}
bool SvgParser::parseClipPath(const KoXmlElement &e) bool SvgParser::parseClipPath(const KoXmlElement &e)
{ {
SvgClipPathHelper clipPath; SvgClipPathHelper clipPath;
...@@ -1290,7 +1324,7 @@ QList<KoShape*> SvgParser::parseSingleElement(const KoXmlElement &b) ...@@ -1290,7 +1324,7 @@ QList<KoShape*> SvgParser::parseSingleElement(const KoXmlElement &b)
if (b.tagName() == "svg") { if (b.tagName() == "svg") {
shapes += parseSvg(b); shapes += parseSvg(b);
} else if (b.tagName() == "g" || b.tagName() == "a" || b.tagName() == "symbol") { } else if (b.tagName() == "g" || b.tagName() == "a") {
// treat svg link <a> as group so we don't miss its child elements // treat svg link <a> as group so we don't miss its child elements
shapes += parseGroup(b); shapes += parseGroup(b);
} else if (b.tagName() == "switch") { } else if (b.tagName() == "switch") {
...@@ -1317,6 +1351,8 @@ QList<KoShape*> SvgParser::parseSingleElement(const KoXmlElement &b) ...@@ -1317,6 +1351,8 @@ QList<KoShape*> SvgParser::parseSingleElement(const KoXmlElement &b)
parseClipMask(b); parseClipMask(b);
} else if (b.tagName() == "marker") { } else if (b.tagName() == "marker") {
parseMarker(b); parseMarker(b);
} else if (b.tagName() == "symbol") {
parseSymbol(b);
} else if (b.tagName() == "style") { } else if (b.tagName() == "style") {
m_context.addStyleSheet(b); m_context.addStyleSheet(b);
} else if (b.tagName() == "rect" || } else if (b.tagName() == "rect" ||
......
...@@ -64,8 +64,9 @@ public: ...@@ -64,8 +64,9 @@ public:
/// Returns the list of all shapes of the svg document /// Returns the list of all shapes of the svg document
QList<KoShape*> shapes() const; QList<KoShape*> shapes() const;
/// Returns the collection of symbols contained in the svg document. /// Takes the collection of symbols contained in the svg document. The parser will
QVector<KoSvgSymbol*> symbols() const; /// no longer know about the symbols.
QVector<KoSvgSymbol*> takeSymbols();
typedef std::function<QByteArray(const QString&)> FileFetcherFunc; typedef std::function<QByteArray(const QString&)> FileFetcherFunc;
void setFileFetcher(FileFetcherFunc func); void setFileFetcher(FileFetcherFunc func);
...@@ -91,6 +92,7 @@ protected: ...@@ -91,6 +92,7 @@ protected:
bool parseClipPath(const KoXmlElement &); bool parseClipPath(const KoXmlElement &);
bool parseClipMask(const KoXmlElement &e); bool parseClipMask(const KoXmlElement &e);
bool parseMarker(const KoXmlElement &e); bool parseMarker(const KoXmlElement &e);
bool parseSymbol(const KoXmlElement &e);
/// parses a length attribute /// parses a length attribute
qreal parseUnit(const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF()); qreal parseUnit(const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF());
/// parses a length attribute in x-direction /// parses a length attribute in x-direction
...@@ -168,8 +170,7 @@ private: ...@@ -168,8 +170,7 @@ private:
QMap<QString, QExplicitlySharedDataPointer<KoMarker>> m_markers; QMap<QString, QExplicitlySharedDataPointer<KoMarker>> m_markers;
KoDocumentResourceManager *m_documentResourceManager; KoDocumentResourceManager *m_documentResourceManager;
QList<KoShape*> m_shapes; QList<KoShape*> m_shapes;
QList<KoSvgSymbol*> m_symbols; QVector<KoSvgSymbol*> m_symbols;
QList<KoShape*> m_symbolShapes;
QList<KoShape*> m_toplevelShapes; QList<KoShape*> m_toplevelShapes;
}; };
......
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