Krita Source Code Documentation
Loading...
Searching...
No Matches
SvgParser.h
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2002-2003, 2005 Rob Buis <buis@kde.org>
3 * SPDX-FileCopyrightText: 2005-2006 Tim Beaulen <tbscope@gmail.com>
4 * SPDX-FileCopyrightText: 2005, 2007-2009 Jan Hambrecht <jaham@gmx.net>
5 *
6 * SPDX-License-Identifier: LGPL-2.0-or-later
7 */
8
9#ifndef SVGPARSER_H
10#define SVGPARSER_H
11
12#include <QMap>
13#include <QSizeF>
14#include <QRectF>
15#include <QSharedPointer>
16#include <QExplicitlySharedDataPointer>
17
18#include "kritaflake_export.h"
19#include "SvgGradientHelper.h"
20#include "SvgClipPathHelper.h"
21#include "SvgLoadingContext.h"
22#include "SvgStyleParser.h"
23#include "KoClipMask.h"
25#include <KoID.h>
26
27class KoShape;
28class KoShapeGroup;
32class KoMarker;
33class KoPathShape;
34class KoSvgTextShape;
35class KoSvgTextLoader;
36class QDomText;
37
38class KRITAFLAKE_EXPORT SvgParser
39{
40 struct DeferredUseStore;
41
42public:
43 explicit SvgParser(KoDocumentResourceManager *documentResourceManager);
44 virtual ~SvgParser();
45
46 static QDomDocument createDocumentFromSvg(QIODevice *device, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
47 static QDomDocument createDocumentFromSvg(const QByteArray &data, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
48 static QDomDocument createDocumentFromSvg(const QString &data, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
49 static QDomDocument createDocumentFromSvg(QXmlStreamReader reader, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
50
52 QList<KoShape*> parseSvg(const QDomElement &e, QSizeF * fragmentSize = 0);
53
55 void setXmlBaseDir(const QString &baseDir);
56
57 void setResolution(const QRectF boundsInPixels, qreal pixelsPerInch);
58 void setDefaultKraTextVersion(int version);
59
60 // Set whether to always consider shapes without fill or stroke explicitly set as inherited.
61 // By default, this is off, and the parser retrieves the fill from the graphicsContext.
62 void setFillStrokeInheritByDefault(const bool enable);
63
64 // Whether to try and resolve the text properties for toplevel shapes.
65 // By default this is on, as there might be doc-wide css that needs to
66 // be resolved. However, for the style properties resource we turn this
67 // off.
68 void setResolveTextPropertiesForTopLevel(const bool enable);
69
71 QList<KoShape*> shapes() const;
72
75 QVector<KoSvgSymbol*> takeSymbols();
76
77 QString documentTitle() const;
78 QString documentDescription() const;
79
80
81 typedef std::function<QByteArray(const QString&)> FileFetcherFunc;
82 void setFileFetcher(FileFetcherFunc func);
83
85
86 void parseDefsElement(const QDomElement &e);
87 KoShape* parseTextElement(const QDomElement &e, KoSvgTextShape *mergeIntoShape = 0);
88
89 QStringList warnings() const;
90
91protected:
92
94 KoShape* parseGroup(const QDomElement &e, const QDomElement &overrideChildrenFrom = QDomElement(), bool createContext = true);
95
97 KoShape* getTextPath(const QDomElement &e, bool hideShapesFromDefs = true);
98
100 void parseTextChildren(const QDomElement &e, KoSvgTextLoader &textLoader, bool hideShapesFromDefs = true);
101
103 QList<KoShape*> parseContainer(const QDomElement &);
104
106 QList<KoShape*> parseSingleElement(const QDomElement &b, DeferredUseStore* deferredUseStore = 0);
107
109 KoShape* parseUse(const QDomElement &, DeferredUseStore* deferredUseStore);
110
111 KoShape* resolveUse(const QDomElement &e, const QString& key);
112
114 SvgGradientHelper *parseGradient(const QDomElement &);
115
117 SvgGradientHelper* parseMeshGradient(const QDomElement&);
118
120 QList<QPair<QString, QColor>> parseMeshPatch(const QDomNode& meshpatch);
121
123 QSharedPointer<KoVectorPatternBackground> parsePattern(const QDomElement &e, const KoShape *__shape);
124
126 bool parseFilter(const QDomElement &, const QDomElement &referencedBy = QDomElement());
127
129 bool parseClipPath(const QDomElement &);
130 bool parseClipMask(const QDomElement &e);
131
132 bool parseMarker(const QDomElement &e);
133
134 bool parseSymbol(const QDomElement &e);
135
137 void parseMetadataApplyToShape(const QDomElement &e, KoShape *shape);
138
140 qreal parseUnit(const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF());
141
143 qreal parseUnitX(const QString &unit);
144
146 qreal parseUnitY(const QString &unit);
147
149 qreal parseUnitXY(const QString &unit);
150
152 qreal parseAngular(const QString &unit);
153
154 KoShape *createObjectDirect(const QDomElement &b);
155
157 KoShape * createObject(const QDomElement &, const SvgStyles &style = SvgStyles());
158
160 KoShape * createPath(const QDomElement &);
161
163 SvgGradientHelper* findGradient(const QString &id);
164
166 QSharedPointer<KoVectorPatternBackground> findPattern(const QString &id, const KoShape *shape);
167
169 SvgClipPathHelper* findClipPath(const QString &id);
170
172 void addToGroup(QList<KoShape*> shapes, KoShapeContainer *group);
173
175 KoShape * createShape(const QString &shapeID);
176
178 KoShape * createShapeFromElement(const QDomElement &element, SvgLoadingContext &context);
179
181 KoShape * createShapeFromCSS(const QDomElement e, const QString value, SvgLoadingContext &context, bool hideShapesFromDefs = true);
182
184 QList<KoShape*> createListOfShapesFromCSS(const QDomElement e, const QString value, SvgLoadingContext &context, bool hideShapesFromDefs = true);
185
188
189 void uploadStyleToContext(const QDomElement &e);
190 void applyCurrentStyle(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates);
191 void applyCurrentBasicStyle(KoShape *shape);
192
194 void applyStyle(KoShape *, const QDomElement &, const QPointF &shapeToOriginalUserCoordinates);
195
197 void applyStyle(KoShape *, const SvgStyles &, const QPointF &shapeToOriginalUserCoordinates);
198
200 void applyFillStyle(KoShape * shape);
201
203 void applyStrokeStyle(KoShape * shape);
204
206 void applyClipping(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates);
207 void applyMaskClipping(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates);
208 void applyMarkers(KoPathShape *shape);
209
210 void applyPaintOrder(KoShape *shape);
211
213 void applyId(const QString &id, KoShape *shape);
214
217 void applyViewBoxTransform(const QDomElement &element);
218
219 QDomText getTheOnlyTextChild(const QDomElement &e);
220
222 bool shapeInDefs(const KoShape *shape);
223
224private:
226 QMap<QString, SvgGradientHelper> m_gradients;
227 QMap<QString, SvgClipPathHelper> m_clipPaths;
228 QMap<QString, QSharedPointer<KoClipMask>> m_clipMasks;
229 QMap<QString, QExplicitlySharedDataPointer<KoMarker>> m_markers;
232 QMap<QString, KoSvgSymbol*> m_symbols;
234 bool m_isInsideTextSubtree = false;
238 QMap<KoShape *, QTransform> m_shapeParentTransform;
239 bool m_inheritStrokeFillByDefault = false;
240 bool m_resolveTextPropertiesForTopLevel = true;
241};
242
243#endif
float value(const T *src, size_t ch)
QMap< QString, QString > SvgStyles
The position of a path point within a path shape.
Definition KoPathShape.h:63
Contains data used for loading svg.
QString m_documentDescription
Definition SvgParser.h:236
QMap< QString, SvgClipPathHelper > m_clipPaths
Definition SvgParser.h:227
QString m_documentTitle
Definition SvgParser.h:235
QList< KoShape * > m_defsShapes
Definition SvgParser.h:233
QMap< QString, KoSvgSymbol * > m_symbols
Definition SvgParser.h:232
QMap< QString, QExplicitlySharedDataPointer< KoMarker > > m_markers
Definition SvgParser.h:229
KoDocumentResourceManager * m_documentResourceManager
Definition SvgParser.h:230
std::function< QByteArray(const QString &) FileFetcherFunc)
Definition SvgParser.h:81
QMap< KoShape *, QTransform > m_shapeParentTransform
Definition SvgParser.h:238
QList< KoShape * > m_shapes
Definition SvgParser.h:231
SvgLoadingContext m_context
Definition SvgParser.h:225
QVector< KoID > m_warnings
Definition SvgParser.h:237
QMap< QString, QSharedPointer< KoClipMask > > m_clipMasks
Definition SvgParser.h:228
void buildDocument(QList< KoShape * > shapes)
Builds the document from the given shapes list.
bool parseFilter(const QDomElement &, const QDomElement &referencedBy=QDomElement())
Parses a filter element.
QMap< QString, SvgGradientHelper > m_gradients
Definition SvgParser.h:226