| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt SVG module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QSVGHANDLER_P_H |
| 41 | #define QSVGHANDLER_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include "QtCore/qxmlstream.h" |
| 55 | #include "QtCore/qhash.h" |
| 56 | #include "QtCore/qstack.h" |
| 57 | #include <QtCore/QLoggingCategory> |
| 58 | #include "qsvgstyle_p.h" |
| 59 | #include "private/qcssparser_p.h" |
| 60 | #include "qsvggraphics_p.h" |
| 61 | #include "qtsvgglobal_p.h" |
| 62 | |
| 63 | QT_BEGIN_NAMESPACE |
| 64 | |
| 65 | class QSvgNode; |
| 66 | class QSvgTinyDocument; |
| 67 | class QSvgHandler; |
| 68 | class QColor; |
| 69 | class QSvgStyleSelector; |
| 70 | class QXmlStreamReader; |
| 71 | |
| 72 | #ifndef QT_NO_CSSPARSER |
| 73 | |
| 74 | struct QSvgCssAttribute |
| 75 | { |
| 76 | QXmlStreamStringRef name; |
| 77 | QXmlStreamStringRef value; |
| 78 | }; |
| 79 | |
| 80 | #endif |
| 81 | |
| 82 | class Q_SVG_PRIVATE_EXPORT QSvgHandler |
| 83 | { |
| 84 | public: |
| 85 | enum LengthType { |
| 86 | LT_PERCENT, |
| 87 | LT_PX, |
| 88 | LT_PC, |
| 89 | LT_PT, |
| 90 | LT_MM, |
| 91 | LT_CM, |
| 92 | LT_IN, |
| 93 | LT_OTHER |
| 94 | }; |
| 95 | |
| 96 | public: |
| 97 | QSvgHandler(QIODevice *device); |
| 98 | QSvgHandler(const QByteArray &data); |
| 99 | QSvgHandler(QXmlStreamReader *const data); |
| 100 | ~QSvgHandler(); |
| 101 | |
| 102 | QIODevice *device() const; |
| 103 | QSvgTinyDocument *document() const; |
| 104 | |
| 105 | inline bool ok() const { |
| 106 | return document() != 0 && !xml->hasError(); |
| 107 | } |
| 108 | |
| 109 | inline QString errorString() const { return xml->errorString(); } |
| 110 | inline int lineNumber() const { return xml->lineNumber(); } |
| 111 | |
| 112 | void setDefaultCoordinateSystem(LengthType type); |
| 113 | LengthType defaultCoordinateSystem() const; |
| 114 | |
| 115 | void pushColor(const QColor &color); |
| 116 | void pushColorCopy(); |
| 117 | void popColor(); |
| 118 | QColor currentColor() const; |
| 119 | |
| 120 | #ifndef QT_NO_CSSPARSER |
| 121 | void setInStyle(bool b); |
| 122 | bool inStyle() const; |
| 123 | |
| 124 | QSvgStyleSelector *selector() const; |
| 125 | #endif |
| 126 | |
| 127 | void setAnimPeriod(int start, int end); |
| 128 | int animationDuration() const; |
| 129 | |
| 130 | #ifndef QT_NO_CSSPARSER |
| 131 | void parseCSStoXMLAttrs(const QString &css, QVector<QSvgCssAttribute> *attributes); |
| 132 | #endif |
| 133 | |
| 134 | inline QPen defaultPen() const |
| 135 | { return m_defaultPen; } |
| 136 | |
| 137 | public: |
| 138 | bool startElement(const QString &localName, const QXmlStreamAttributes &attributes); |
| 139 | bool endElement(const QStringRef &localName); |
| 140 | bool characters(const QStringRef &str); |
| 141 | bool processingInstruction(const QString &target, const QString &data); |
| 142 | |
| 143 | private: |
| 144 | void init(); |
| 145 | |
| 146 | QSvgTinyDocument *m_doc; |
| 147 | QStack<QSvgNode*> m_nodes; |
| 148 | |
| 149 | QList<QSvgNode*> m_resolveNodes; |
| 150 | |
| 151 | enum CurrentNode |
| 152 | { |
| 153 | Unknown, |
| 154 | Graphics, |
| 155 | Style |
| 156 | }; |
| 157 | QStack<CurrentNode> m_skipNodes; |
| 158 | |
| 159 | /*! |
| 160 | Follows the depths of elements. The top is current xml:space |
| 161 | value that applies for a given element. |
| 162 | */ |
| 163 | QStack<QSvgText::WhitespaceMode> m_whitespaceMode; |
| 164 | |
| 165 | QSvgRefCounter<QSvgStyleProperty> m_style; |
| 166 | |
| 167 | LengthType m_defaultCoords; |
| 168 | |
| 169 | QStack<QColor> m_colorStack; |
| 170 | QStack<int> m_colorTagCount; |
| 171 | |
| 172 | int m_animEnd; |
| 173 | |
| 174 | QXmlStreamReader *const xml; |
| 175 | #ifndef QT_NO_CSSPARSER |
| 176 | bool m_inStyle; |
| 177 | QSvgStyleSelector *m_selector; |
| 178 | QCss::Parser m_cssParser; |
| 179 | #endif |
| 180 | void parse(); |
| 181 | void resolveGradients(QSvgNode *node, int nestedDepth = 0); |
| 182 | void resolveNodes(); |
| 183 | |
| 184 | QPen m_defaultPen; |
| 185 | /** |
| 186 | * Whether we own the variable xml, and hence whether |
| 187 | * we need to delete it. |
| 188 | */ |
| 189 | const bool m_ownsReader; |
| 190 | }; |
| 191 | |
| 192 | Q_DECLARE_LOGGING_CATEGORY(lcSvgHandler) |
| 193 | |
| 194 | QT_END_NAMESPACE |
| 195 | |
| 196 | #endif // QSVGHANDLER_P_H |
| 197 | |