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 "SvgFilterHelper.h"
21#include "SvgClipPathHelper.h"
22#include "SvgLoadingContext.h"
23#include "SvgStyleParser.h"
24#include "KoClipMask.h"
26#include <KoID.h>
27
28class KoShape;
29class KoShapeGroup;
33class KoMarker;
34class KoPathShape;
35class KoSvgTextShape;
36class KoSvgTextLoader;
37class QDomText;
38
39class KRITAFLAKE_EXPORT SvgParser
40{
41 struct DeferredUseStore;
42
43public:
44 explicit SvgParser(KoDocumentResourceManager *documentResourceManager);
45 virtual ~SvgParser();
46
47 static QDomDocument createDocumentFromSvg(QIODevice *device, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
48 static QDomDocument createDocumentFromSvg(const QByteArray &data, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
49 static QDomDocument createDocumentFromSvg(const QString &data, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
50 static QDomDocument createDocumentFromSvg(QXmlStreamReader reader, QString *errorMsg = 0, int *errorLine = 0, int *errorColumn = 0);
51
53 QList<KoShape*> parseSvg(const QDomElement &e, QSizeF * fragmentSize = 0);
54
56 void setXmlBaseDir(const QString &baseDir);
57
58 void setResolution(const QRectF boundsInPixels, qreal pixelsPerInch);
59 void setDefaultKraTextVersion(int version);
60
61 // Set whether to always consider shapes without fill or stroke explicitly set as inherited.
62 // By default, this is off, and the parser retrieves the fill from the graphicsContext.
63 void setFillStrokeInheritByDefault(const bool enable);
64
65 // Whether to try and resolve the text properties for toplevel shapes.
66 // By default this is on, as there might be doc-wide css that needs to
67 // be resolved. However, for the style properties resource we turn this
68 // off.
69 void setResolveTextPropertiesForTopLevel(const bool enable);
70
72 QList<KoShape*> shapes() const;
73
76 QVector<KoSvgSymbol*> takeSymbols();
77
78 QString documentTitle() const;
79 QString documentDescription() const;
80
81
82 typedef std::function<QByteArray(const QString&)> FileFetcherFunc;
83 void setFileFetcher(FileFetcherFunc func);
84
86
87 void parseDefsElement(const QDomElement &e);
88 KoShape* parseTextElement(const QDomElement &e, KoSvgTextShape *mergeIntoShape = 0);
89
90 QStringList warnings() const;
91
92protected:
93
95 KoShape* parseGroup(const QDomElement &e, const QDomElement &overrideChildrenFrom = QDomElement(), bool createContext = true);
96
98 KoShape* getTextPath(const QDomElement &e);
99
101 void parseTextChildren(const QDomElement &e, KoSvgTextLoader &textLoader);
102
104 QList<KoShape*> parseContainer(const QDomElement &);
105
107 QList<KoShape*> parseSingleElement(const QDomElement &b, DeferredUseStore* deferredUseStore = 0);
108
110 KoShape* parseUse(const QDomElement &, DeferredUseStore* deferredUseStore);
111
112 KoShape* resolveUse(const QDomElement &e, const QString& key);
113
115 SvgGradientHelper *parseGradient(const QDomElement &);
116
118 SvgGradientHelper* parseMeshGradient(const QDomElement&);
119
121 QList<QPair<QString, QColor>> parseMeshPatch(const QDomNode& meshpatch);
122
124 QSharedPointer<KoVectorPatternBackground> parsePattern(const QDomElement &e, const KoShape *__shape);
125
127 bool parseFilter(const QDomElement &, const QDomElement &referencedBy = QDomElement());
128
130 bool parseClipPath(const QDomElement &);
131 bool parseClipMask(const QDomElement &e);
132
133 bool parseMarker(const QDomElement &e);
134
135 bool parseSymbol(const QDomElement &e);
136
138 void parseMetadataApplyToShape(const QDomElement &e, KoShape *shape);
139
141 qreal parseUnit(const QString &, bool horiz = false, bool vert = false, const QRectF &bbox = QRectF());
142
144 qreal parseUnitX(const QString &unit);
145
147 qreal parseUnitY(const QString &unit);
148
150 qreal parseUnitXY(const QString &unit);
151
153 qreal parseAngular(const QString &unit);
154
155 KoShape *createObjectDirect(const QDomElement &b);
156
158 KoShape * createObject(const QDomElement &, const SvgStyles &style = SvgStyles());
159
161 KoShape * createPath(const QDomElement &);
162
164 SvgGradientHelper* findGradient(const QString &id);
165
167 QSharedPointer<KoVectorPatternBackground> findPattern(const QString &id, const KoShape *shape);
168
170 SvgFilterHelper* findFilter(const QString &id, const QString &href = QString());
171
173 SvgClipPathHelper* findClipPath(const QString &id);
174
176 void addToGroup(QList<KoShape*> shapes, KoShapeContainer *group);
177
179 KoShape * createShape(const QString &shapeID);
180
182 KoShape * createShapeFromElement(const QDomElement &element, SvgLoadingContext &context);
183
185 KoShape * createShapeFromCSS(const QDomElement e, const QString value, SvgLoadingContext &context);
186
188 QList<KoShape*> createListOfShapesFromCSS(const QDomElement e, const QString value, SvgLoadingContext &context);
189
192
193 void uploadStyleToContext(const QDomElement &e);
194 void applyCurrentStyle(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates);
195 void applyCurrentBasicStyle(KoShape *shape);
196
198 void applyStyle(KoShape *, const QDomElement &, const QPointF &shapeToOriginalUserCoordinates);
199
201 void applyStyle(KoShape *, const SvgStyles &, const QPointF &shapeToOriginalUserCoordinates);
202
204 void applyFillStyle(KoShape * shape);
205
207 void applyStrokeStyle(KoShape * shape);
208
210 void applyFilter(KoShape * shape);
211
213 void applyClipping(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates);
214 void applyMaskClipping(KoShape *shape, const QPointF &shapeToOriginalUserCoordinates);
215 void applyMarkers(KoPathShape *shape);
216
217 void applyPaintOrder(KoShape *shape);
218
220 void applyId(const QString &id, KoShape *shape);
221
224 void applyViewBoxTransform(const QDomElement &element);
225
226 QDomText getTheOnlyTextChild(const QDomElement &e);
227
228private:
230 QMap<QString, SvgGradientHelper> m_gradients;
231 QMap<QString, SvgFilterHelper> m_filters;
232 QMap<QString, SvgClipPathHelper> m_clipPaths;
233 QMap<QString, QSharedPointer<KoClipMask>> m_clipMasks;
234 QMap<QString, QExplicitlySharedDataPointer<KoMarker>> m_markers;
237 QMap<QString, KoSvgSymbol*> m_symbols;
239 bool m_isInsideTextSubtree = false;
243 QMap<KoShape *, QTransform> m_shapeParentTransform;
244 bool m_inheritStrokeFillByDefault = false;
245 bool m_resolveTextPropertiesForTopLevel = true;
246};
247
248#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:241
QMap< QString, SvgClipPathHelper > m_clipPaths
Definition SvgParser.h:232
QString m_documentTitle
Definition SvgParser.h:240
QList< KoShape * > m_defsShapes
Definition SvgParser.h:238
QMap< QString, KoSvgSymbol * > m_symbols
Definition SvgParser.h:237
QMap< QString, QExplicitlySharedDataPointer< KoMarker > > m_markers
Definition SvgParser.h:234
QMap< QString, SvgFilterHelper > m_filters
Definition SvgParser.h:231
KoDocumentResourceManager * m_documentResourceManager
Definition SvgParser.h:235
std::function< QByteArray(const QString &) FileFetcherFunc)
Definition SvgParser.h:82
QMap< KoShape *, QTransform > m_shapeParentTransform
Definition SvgParser.h:243
QList< KoShape * > m_shapes
Definition SvgParser.h:236
SvgLoadingContext m_context
Definition SvgParser.h:229
QVector< KoID > m_warnings
Definition SvgParser.h:242
QMap< QString, QSharedPointer< KoClipMask > > m_clipMasks
Definition SvgParser.h:233
void buildDocument(QList< KoShape * > shapes)
Builds the document from the given shapes list.
QMap< QString, SvgGradientHelper > m_gradients
Definition SvgParser.h:230