1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QSVGHANDLER_P_H
5#define QSVGHANDLER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "QtCore/qxmlstream.h"
19#include "QtCore/qstack.h"
20#include <QtCore/QLoggingCategory>
21#include "qsvgstyle_p.h"
22#if QT_CONFIG(cssparser)
23#include <QtSvg/private/qsvgcsshandler_p.h>
24#include <QtSvg/private/qsvgcssproperties_p.h>
25#endif
26#include "qsvggraphics_p.h"
27#include "qtsvgglobal_p.h"
28#include "qsvgutils_p.h"
29
30QT_BEGIN_NAMESPACE
31
32class QSvgNode;
33class QSvgTinyDocument;
34class QSvgHandler;
35class QColor;
36
37class Q_SVG_EXPORT QSvgHandler
38{
39public:
40 QSvgHandler(QIODevice *device, QtSvg::Options options = {},
41 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
42 QSvgHandler(const QByteArray &data, QtSvg::Options options = {},
43 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
44 QSvgHandler(QXmlStreamReader *const data, QtSvg::Options options = {},
45 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
46 ~QSvgHandler();
47
48 QIODevice *device() const;
49 QSvgTinyDocument *document() const;
50
51 inline bool ok() const {
52 return document() != 0 && !xml->hasError();
53 }
54
55 inline QString errorString() const { return xml->errorString(); }
56 inline int lineNumber() const { return xml->lineNumber(); }
57
58 void setDefaultCoordinateSystem(QSvgUtils::LengthType type);
59 QSvgUtils::LengthType defaultCoordinateSystem() const;
60
61 void pushColor(const QColor &color);
62 void pushColorCopy();
63 void popColor();
64 QColor currentColor() const;
65
66#ifndef QT_NO_CSSPARSER
67 void setInStyle(bool b);
68 bool inStyle() const;
69
70 QSvgCssHandler &cssHandler();
71#endif
72
73 void setAnimPeriod(int start, int end);
74 int animationDuration() const;
75
76 inline QPen defaultPen() const
77 { return m_defaultPen; }
78
79 QtSvg::Options options() const;
80 QtSvg::AnimatorType animatorType() const;
81 bool trustedSourceMode() const;
82
83public:
84 bool startElement(const QStringView localName, const QXmlStreamAttributes &attributes);
85 bool endElement(const QStringView localName);
86 bool characters(const QStringView str);
87 bool processingInstruction(const QStringView target, const QStringView data);
88
89private:
90 void init();
91
92 QSvgTinyDocument *m_doc;
93 QStack<QSvgNode *> m_nodes;
94 // TODO: This is only needed during parsing, so it unnecessarily takes up space after that.
95 // Temporary container for :
96 // - <use> nodes which haven't been resolved yet.
97 // - <filter> nodes to be checked for unsupported filter primitives.
98 QList<QSvgNode *> m_toBeResolved;
99
100 enum CurrentNode
101 {
102 Unknown,
103 Graphics,
104 Style,
105 Doc
106 };
107 QStack<CurrentNode> m_skipNodes;
108
109 /*!
110 Follows the depths of elements. The top is current xml:space
111 value that applies for a given element.
112 */
113 QStack<QSvgText::WhitespaceMode> m_whitespaceMode;
114
115 QSvgRefCounter<QSvgStyleProperty> m_style;
116
117 QSvgUtils::LengthType m_defaultCoords;
118
119 QStack<QColor> m_colorStack;
120 QStack<int> m_colorTagCount;
121
122 int m_animEnd;
123
124 QXmlStreamReader *const xml;
125#ifndef QT_NO_CSSPARSER
126 bool m_inStyle;
127 QSvgCssHandler m_cssHandler;
128#endif
129 void parse();
130 void resolvePaintServers(QSvgNode *node, int nestedDepth = 0);
131 void resolveNodes();
132
133 QPen m_defaultPen;
134 /**
135 * Whether we own the variable xml, and hence whether
136 * we need to delete it.
137 */
138 const bool m_ownsReader;
139
140 const QtSvg::Options m_options;
141 const QtSvg::AnimatorType m_animatorType;
142};
143
144Q_DECLARE_LOGGING_CATEGORY(lcSvgHandler)
145
146QT_END_NAMESPACE
147
148#endif // QSVGHANDLER_P_H
149

source code of qtsvg/src/svg/qsvghandler_p.h