Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsvghandler_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5
6#ifndef QSVGHANDLER_P_H
7#define QSVGHANDLER_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include "QtCore/qxmlstream.h"
21#include "QtCore/qstack.h"
22#include "QtCore/qstringview.h"
23#include <QtCore/QLoggingCategory>
24#include "qsvgstyle_p.h"
25#if QT_CONFIG(cssparser)
26#include <QtSvg/private/qsvgcsshandler_p.h>
27#include <QtSvg/private/qsvgcssproperties_p.h>
28#include <QtSvg/private/qsvgcssvalues_p.h>
29#endif
30#include "qsvggraphics_p.h"
31#include "qsvgfont_p.h"
32#include "qtsvgglobal_p.h"
33#include "qtsvgglobal.h"
34#include <QtSvg/private/qsvgpaintserver_p.h>
35#include <QtGui/private/qguisvg_p.h>
36
37#include <memory>
38
40
41class QSvgNode;
42class QSvgDocument;
43class QSvgHandler;
44class QColor;
45
46Q_AUTOTEST_EXPORT QList<qreal> parseNumbersList(QStringView *str);
47Q_AUTOTEST_EXPORT bool resolveColor(QStringView colorStr, QColor &color, QSvgHandler *handler);
48
49class Q_SVG_EXPORT QSvgHandler
50{
51public:
52 QSvgHandler(QIODevice *device, QtSvg::Options options = {},
53 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
54 QSvgHandler(const QByteArray &data, QtSvg::Options options = {},
55 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
56 QSvgHandler(QXmlStreamReader *const data, QtSvg::Options options = {},
57 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
58 ~QSvgHandler();
59
60 QIODevice *device() const;
61 QSvgDocument *document() const;
62 // resets the stored document: afterwards document() == nullptr and ok() == false
63 [[nodiscard]] std::unique_ptr<QSvgDocument> takeDocument();
64
65 inline bool ok() const {
66 return document() != 0 && !xml->hasError();
67 }
68
69 inline QString errorString() const { return xml->errorString(); }
70 inline int lineNumber() const { return xml->lineNumber(); }
71
72 void setDefaultCoordinateSystem(QGuiSvg::LengthType type);
73 QGuiSvg::LengthType defaultCoordinateSystem() const;
74
75 void pushColor(const QColor &color);
76 void pushColorCopy();
77 void popColor();
78 QColor currentColor() const;
79
80 void pushUnresolvedStyle(QSvgStyleProperty *prop);
81
82 void setCurrentSvgFontFamily(QStringView family);
83
84#ifndef QT_NO_CSSPARSER
85 void setInStyle(bool b);
86 bool inStyle() const;
87
88 QSvgCssHandler &cssHandler();
89#endif
90
91 void setAnimPeriod(int start, int end);
92 int animationDuration() const;
93
94 inline QPen defaultPen() const
95 { return m_defaultPen; }
96
97 QtSvg::Options options() const;
98 QtSvg::AnimatorType animatorType() const;
99 bool trustedSourceMode() const;
100
101public:
102 bool startElement(const QStringView localName, const QXmlStreamAttributes &attributes);
103 bool endElement(const QStringView localName);
104 bool characters(const QStringView str);
105 bool processingInstruction(const QStringView target, const QStringView data);
106
107private:
108 void init();
109
110 std::unique_ptr<QSvgDocument> m_doc;
111 QStack<QSvgNode *> m_nodes;
112 // TODO: This is only needed during parsing, so it unnecessarily takes up space after that.
113 // Temporary container for :
114 // - <use> nodes which haven't been resolved yet.
115 // - <filter> nodes to be checked for unsupported filter primitives.
116 QList<QSvgNode *> m_toBeResolved;
117 QList<QSvgStyleProperty *> m_unresolvedStyles;
118
119 enum CurrentNode
120 {
121 Unknown,
122 Graphics,
123 Style,
124 Doc
125 };
126 QStack<CurrentNode> m_skipNodes;
127
128 /*!
129 Follows the depths of elements. The top is current xml:space
130 value that applies for a given element.
131 */
132 QStack<QSvgText::WhitespaceMode> m_whitespaceMode;
133
134 struct SvgFontData
135 {
136 QSvgFontPtr font;
137 QString fontFamily;
138
139 bool isEmpty() const
140 {
141 return !font || fontFamily.isEmpty();
142 }
143
144 void reset()
145 {
146 font = nullptr;
147 fontFamily.clear();
148 }
149 };
150
151 SvgFontData m_currentSvgFontData;
152 QSvgPaintServerSharedPtr m_paintServer;
153
154 QGuiSvg::LengthType m_defaultCoords;
155
156 QStack<QColor> m_colorStack;
157 QStack<int> m_colorTagCount;
158
159 int m_animEnd;
160
161 QXmlStreamReader *const xml;
162#ifndef QT_NO_CSSPARSER
163 bool m_inStyle;
164 QSvgCssHandler m_cssHandler;
165#endif
166 void parse();
167 void resolvePaintServers();
168 void resolveNodes();
169
170 QPen m_defaultPen;
171 /**
172 * Whether we own the variable xml, and hence whether
173 * we need to delete it.
174 */
175 const bool m_ownsReader;
176
177 const QtSvg::Options m_options;
178 const QtSvg::AnimatorType m_animatorType;
179};
180
181Q_AUTOTEST_EXPORT QSvgNode *createAnimateTransformNode(QSvgNode *parent,
182 const QXmlStreamAttributes &attributes,
183 QSvgHandler *handler);
184
185Q_DECLARE_LOGGING_CATEGORY(lcSvgHandler)
186
187QT_END_NAMESPACE
188
189#endif // QSVGHANDLER_P_H
friend class QPainter
Combined button and popup list for selecting options.
@ AssumeTrustedSource
Definition qtsvgglobal.h:20
@ DisableSMILAnimations
Definition qtsvgglobal.h:23
@ DisableCSSAnimations
Definition qtsvgglobal.h:24
@ Tiny12FeaturesOnly
Definition qtsvgglobal.h:19
@ NoOption
Definition qtsvgglobal.h:18
@ DisableAnimations
Definition qtsvgglobal.h:27
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2620
#define qPrintable(string)
Definition qstring.h:1713
static QByteArray qt_inflateSvgzDataFrom(QIODevice *device, bool doCheckContent=true)
static bool isValidMatrix(const QTransform &transform)
static bool hasSvgHeader(const QByteArray &buf)
std::optional< int > calculateSizeValue(bool isPercent, int sizeValue, qreal viewBoxSizeValue)
Q_AUTOTEST_EXPORT bool resolveColor(QStringView colorStr, QColor &color, QSvgHandler *handler)