| 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/qhash.h" |
| 20 | #include "QtCore/qstack.h" |
| 21 | #include <QtCore/QLoggingCategory> |
| 22 | #include "qsvgstyle_p.h" |
| 23 | #if QT_CONFIG(cssparser) |
| 24 | #include "private/qcssparser_p.h" |
| 25 | #endif |
| 26 | #include "qsvggraphics_p.h" |
| 27 | #include "qtsvgglobal_p.h" |
| 28 | |
| 29 | QT_BEGIN_NAMESPACE |
| 30 | |
| 31 | class QSvgNode; |
| 32 | class QSvgTinyDocument; |
| 33 | class QSvgHandler; |
| 34 | class QColor; |
| 35 | class QSvgStyleSelector; |
| 36 | |
| 37 | #ifndef QT_NO_CSSPARSER |
| 38 | |
| 39 | struct QSvgCssAttribute |
| 40 | { |
| 41 | QString name; |
| 42 | QString value; |
| 43 | }; |
| 44 | |
| 45 | #endif |
| 46 | |
| 47 | class Q_SVG_EXPORT QSvgHandler |
| 48 | { |
| 49 | public: |
| 50 | enum LengthType { |
| 51 | LT_PERCENT, |
| 52 | LT_PX, |
| 53 | LT_PC, |
| 54 | LT_PT, |
| 55 | LT_MM, |
| 56 | LT_CM, |
| 57 | LT_IN, |
| 58 | LT_OTHER |
| 59 | }; |
| 60 | |
| 61 | public: |
| 62 | QSvgHandler(QIODevice *device, QtSvg::Options options = {}); |
| 63 | QSvgHandler(const QByteArray &data, QtSvg::Options options = {}); |
| 64 | QSvgHandler(QXmlStreamReader *const data, QtSvg::Options options = {}); |
| 65 | ~QSvgHandler(); |
| 66 | |
| 67 | QIODevice *device() const; |
| 68 | QSvgTinyDocument *document() const; |
| 69 | |
| 70 | inline bool ok() const { |
| 71 | return document() != 0 && !xml->hasError(); |
| 72 | } |
| 73 | |
| 74 | inline QString errorString() const { return xml->errorString(); } |
| 75 | inline int lineNumber() const { return xml->lineNumber(); } |
| 76 | |
| 77 | void setDefaultCoordinateSystem(LengthType type); |
| 78 | LengthType defaultCoordinateSystem() const; |
| 79 | |
| 80 | void pushColor(const QColor &color); |
| 81 | void pushColorCopy(); |
| 82 | void popColor(); |
| 83 | QColor currentColor() const; |
| 84 | |
| 85 | #ifndef QT_NO_CSSPARSER |
| 86 | void setInStyle(bool b); |
| 87 | bool inStyle() const; |
| 88 | |
| 89 | QSvgStyleSelector *selector() const; |
| 90 | #endif |
| 91 | |
| 92 | void setAnimPeriod(int start, int end); |
| 93 | int animationDuration() const; |
| 94 | |
| 95 | #ifndef QT_NO_CSSPARSER |
| 96 | void parseCSStoXMLAttrs(const QString &css, QList<QSvgCssAttribute> *attributes); |
| 97 | #endif |
| 98 | |
| 99 | inline QPen defaultPen() const |
| 100 | { return m_defaultPen; } |
| 101 | |
| 102 | QtSvg::Options options() const; |
| 103 | bool trustedSourceMode() const; |
| 104 | |
| 105 | public: |
| 106 | bool startElement(const QString &localName, const QXmlStreamAttributes &attributes); |
| 107 | bool endElement(QStringView localName); |
| 108 | bool characters(QStringView str); |
| 109 | bool processingInstruction(const QString &target, const QString &data); |
| 110 | |
| 111 | private: |
| 112 | void init(); |
| 113 | |
| 114 | QSvgTinyDocument *m_doc; |
| 115 | QStack<QSvgNode *> m_nodes; |
| 116 | // TODO: This is only needed during parsing, so it unnecessarily takes up space after that. |
| 117 | // Temporary container for : |
| 118 | // - <use> nodes which haven't been resolved yet. |
| 119 | // - <filter> nodes to be checked for unsupported filter primitives. |
| 120 | QList<QSvgNode *> m_toBeResolved; |
| 121 | |
| 122 | enum CurrentNode |
| 123 | { |
| 124 | Unknown, |
| 125 | Graphics, |
| 126 | Style, |
| 127 | Doc |
| 128 | }; |
| 129 | QStack<CurrentNode> m_skipNodes; |
| 130 | |
| 131 | /*! |
| 132 | Follows the depths of elements. The top is current xml:space |
| 133 | value that applies for a given element. |
| 134 | */ |
| 135 | QStack<QSvgText::WhitespaceMode> m_whitespaceMode; |
| 136 | |
| 137 | QSvgRefCounter<QSvgStyleProperty> m_style; |
| 138 | |
| 139 | LengthType m_defaultCoords; |
| 140 | |
| 141 | QStack<QColor> m_colorStack; |
| 142 | QStack<int> m_colorTagCount; |
| 143 | |
| 144 | int m_animEnd; |
| 145 | |
| 146 | QXmlStreamReader *const xml; |
| 147 | #ifndef QT_NO_CSSPARSER |
| 148 | bool m_inStyle; |
| 149 | QSvgStyleSelector *m_selector; |
| 150 | QCss::Parser m_cssParser; |
| 151 | #endif |
| 152 | void parse(); |
| 153 | void resolvePaintServers(QSvgNode *node, int nestedDepth = 0); |
| 154 | void resolveNodes(); |
| 155 | |
| 156 | QPen m_defaultPen; |
| 157 | /** |
| 158 | * Whether we own the variable xml, and hence whether |
| 159 | * we need to delete it. |
| 160 | */ |
| 161 | const bool m_ownsReader; |
| 162 | |
| 163 | const QtSvg::Options m_options; |
| 164 | }; |
| 165 | |
| 166 | Q_DECLARE_LOGGING_CATEGORY(lcSvgHandler) |
| 167 | |
| 168 | QT_END_NAMESPACE |
| 169 | |
| 170 | #endif // QSVGHANDLER_P_H |
| 171 | |