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 @@
#include <SvgParser.h>
struct KoSvgSymbolCollectionResource::Private {
QVector<KoSvgSymbol> symbols;
QVector<KoSvgSymbol*> symbols;
};
......@@ -108,10 +108,9 @@ bool KoSvgSymbolCollectionResource::loadFromDevice(QIODevice *dev)
SvgParser parser(&manager);
parser.setResolution(QRectF(0,0,100,100), 72); // initialize with default values
QSizeF fragmentSize;
QList<KoShape*> shapes = parser.parseSvg(doc.documentElement(), &fragmentSize);
KoViewConverter converter;
KoShapePaintingContext context;
parser.parseSvg(doc.documentElement(), &fragmentSize);
d->symbols = parser.takeSymbols();
qDebug() << "Loaded" << filename() << "got" << d->symbols.size() << "symbols";
return true;
}
......
......@@ -26,7 +26,8 @@
#include <resources/KoResource.h>
class KoShape;
#include <KoShape.h>
#include "kritaflake_export.h"
......@@ -35,6 +36,11 @@ struct KoSvgSymbol {
KoSvgSymbol(const QString &_title)
: title(_title) {}
~KoSvgSymbol()
{
delete shape;
}
QString id;
QString title;
KoShape *shape;
......
......@@ -111,9 +111,11 @@ QList<KoShape*> SvgParser::shapes() const
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
......@@ -416,7 +418,7 @@ QSharedPointer<KoVectorPatternBackground> SvgParser::parsePattern(const KoXmlEle
* the pattern should be painted in "user" coordinates. Therefore, we should handle
* 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
* correct (DK)
*/
......@@ -585,6 +587,38 @@ bool SvgParser::parseMarker(const KoXmlElement &e)
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)
{
SvgClipPathHelper clipPath;
......@@ -1290,7 +1324,7 @@ QList<KoShape*> SvgParser::parseSingleElement(const KoXmlElement &b)
if (b.tagName() == "svg") {
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
shapes += parseGroup(b);
} else if (b.tagName() == "switch") {
......@@ -1317,6 +1351,8 @@ QList<KoShape*> SvgParser::parseSingleElement(const KoXmlElement &b)
parseClipMask(b);
} else if (b.tagName() == "marker") {
parseMarker(b);
} else if (b.tagName() == "symbol") {
parseSymbol(b);
} else if (b.tagName() == "style") {
m_context.addStyleSheet(b);
} else if (b.tagName() == "rect" ||
......
......@@ -64,8 +64,9 @@ public:
/// Returns the list of all shapes of the svg document
QList<KoShape*> shapes() const;
/// Returns the collection of symbols contained in the svg document.
QVector<KoSvgSymbol*> symbols() const;
/// Takes the collection of symbols contained in the svg document. The parser will
/// no longer know about the symbols.
QVector<KoSvgSymbol*> takeSymbols();
typedef std::function<QByteArray(const QString&)> FileFetcherFunc;
void setFileFetcher(FileFetcherFunc func);
......@@ -91,6 +92,7 @@ protected:
bool parseClipPath(const KoXmlElement &);
bool parseClipMask(const KoXmlElement &e);
bool parseMarker(const KoXmlElement &e);
bool parseSymbol(const KoXmlElement &e);
/// parses a length attribute
qreal parseUnit(const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF());
/// parses a length attribute in x-direction
......@@ -168,8 +170,7 @@ private:
QMap<QString, QExplicitlySharedDataPointer<KoMarker>> m_markers;
KoDocumentResourceManager *m_documentResourceManager;
QList<KoShape*> m_shapes;
QList<KoSvgSymbol*> m_symbols;
QList<KoShape*> m_symbolShapes;
QVector<KoSvgSymbol*> m_symbols;
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