| 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 QSVGSTRUCTURE_P_H |
| 5 | #define QSVGSTRUCTURE_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 "qsvgnode_p.h" |
| 19 | |
| 20 | #include "QtCore/qlist.h" |
| 21 | #include "QtCore/qhash.h" |
| 22 | |
| 23 | QT_BEGIN_NAMESPACE |
| 24 | |
| 25 | class QSvgTinyDocument; |
| 26 | class QSvgNode; |
| 27 | class QPainter; |
| 28 | class QSvgDefs; |
| 29 | |
| 30 | class Q_SVG_EXPORT QSvgStructureNode : public QSvgNode |
| 31 | { |
| 32 | public: |
| 33 | QSvgStructureNode(QSvgNode *parent); |
| 34 | ~QSvgStructureNode(); |
| 35 | QSvgNode *scopeNode(const QString &id) const; |
| 36 | void addChild(QSvgNode *child, const QString &id); |
| 37 | QRectF (QPainter *p, QSvgExtraStates &states) const override; |
| 38 | QRectF (QPainter *p, QSvgExtraStates &states) const override; |
| 39 | QSvgNode *previousSiblingNode(QSvgNode *n) const; |
| 40 | QList<QSvgNode*> renderers() const { return m_renderers; } |
| 41 | protected: |
| 42 | QList<QSvgNode*> m_renderers; |
| 43 | QHash<QString, QSvgNode*> m_scope; |
| 44 | QList<QSvgStructureNode*> m_linkedScopes; |
| 45 | mutable bool m_recursing = false; |
| 46 | }; |
| 47 | |
| 48 | class Q_SVG_EXPORT QSvgG : public QSvgStructureNode |
| 49 | { |
| 50 | public: |
| 51 | QSvgG(QSvgNode *parent); |
| 52 | void drawCommand(QPainter *, QSvgExtraStates &) override; |
| 53 | bool (QPainter *p, QSvgExtraStates &states) const override; |
| 54 | Type type() const override; |
| 55 | bool requiresGroupRendering() const override; |
| 56 | }; |
| 57 | |
| 58 | class Q_SVG_EXPORT QSvgDefs : public QSvgStructureNode |
| 59 | { |
| 60 | public: |
| 61 | QSvgDefs(QSvgNode *parent); |
| 62 | void drawCommand(QPainter *, QSvgExtraStates &) override {}; |
| 63 | bool (QPainter *p, QSvgExtraStates &states) const override; |
| 64 | Type type() const override; |
| 65 | }; |
| 66 | |
| 67 | class Q_SVG_EXPORT QSvgSymbolLike : public QSvgStructureNode |
| 68 | { |
| 69 | // Marker, Symbol and potentially other elements share a lot of common |
| 70 | // attributes and functionality. By making a common base class we can |
| 71 | // avoid repetition. |
| 72 | public: |
| 73 | enum class Overflow : quint8 { |
| 74 | Visible, |
| 75 | Hidden, |
| 76 | Scroll = Visible, //Will not support scrolling |
| 77 | Auto = Visible |
| 78 | }; |
| 79 | |
| 80 | enum class PreserveAspectRatio : quint8 { |
| 81 | None = 0b000000, |
| 82 | xMin = 0b000001, |
| 83 | xMid = 0b000010, |
| 84 | xMax = 0b000011, |
| 85 | yMin = 0b000100, |
| 86 | yMid = 0b001000, |
| 87 | yMax = 0b001100, |
| 88 | meet = 0b010000, |
| 89 | slice = 0b100000, |
| 90 | xMask = xMin | xMid | xMax, |
| 91 | yMask = yMin | yMid | yMax, |
| 92 | xyMask = xMask | yMask, |
| 93 | meetSliceMask = meet | slice |
| 94 | }; |
| 95 | Q_DECLARE_FLAGS(PreserveAspectRatios, PreserveAspectRatio) |
| 96 | |
| 97 | QSvgSymbolLike(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP, |
| 98 | QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow); |
| 99 | void drawCommand(QPainter *, QSvgExtraStates &) override {}; |
| 100 | QRectF (QPainter *p, QSvgExtraStates &states) const override; |
| 101 | bool requiresGroupRendering() const override; |
| 102 | protected: |
| 103 | void setPainterToRectAndAdjustment(QPainter *p) const; |
| 104 | protected: |
| 105 | QRectF m_rect; |
| 106 | QRectF m_viewBox; |
| 107 | QPointF m_refP; |
| 108 | PreserveAspectRatios m_pAspectRatios; |
| 109 | Overflow m_overflow; |
| 110 | }; |
| 111 | |
| 112 | Q_DECLARE_OPERATORS_FOR_FLAGS(QSvgSymbolLike::PreserveAspectRatios) |
| 113 | |
| 114 | class Q_SVG_EXPORT QSvgSymbol : public QSvgSymbolLike |
| 115 | { |
| 116 | public: |
| 117 | QSvgSymbol(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP, |
| 118 | QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow); |
| 119 | void drawCommand(QPainter *p, QSvgExtraStates &states) override; |
| 120 | Type type() const override; |
| 121 | }; |
| 122 | |
| 123 | class Q_SVG_EXPORT QSvgMarker : public QSvgSymbolLike |
| 124 | { |
| 125 | public: |
| 126 | enum class Orientation : quint8 { |
| 127 | Auto, |
| 128 | AutoStartReverse, |
| 129 | Value |
| 130 | }; |
| 131 | enum class MarkerUnits : quint8 { |
| 132 | StrokeWidth, |
| 133 | UserSpaceOnUse |
| 134 | }; |
| 135 | |
| 136 | QSvgMarker(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP, |
| 137 | QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow, |
| 138 | Orientation orientation, qreal orientationAngle, MarkerUnits markerUnits); |
| 139 | void drawCommand(QPainter *p, QSvgExtraStates &states) override; |
| 140 | static void (QSvgNode *node, QPainter *p, QSvgExtraStates &states); |
| 141 | static QRectF (const QSvgNode *node, QPainter *p, QSvgExtraStates &states); |
| 142 | |
| 143 | Orientation orientation() const { |
| 144 | return m_orientation; |
| 145 | } |
| 146 | qreal orientationAngle() const { |
| 147 | return m_orientationAngle; |
| 148 | } |
| 149 | MarkerUnits markerUnits() const { |
| 150 | return m_markerUnits; |
| 151 | } |
| 152 | Type type() const override; |
| 153 | |
| 154 | private: |
| 155 | static void (const QSvgNode *node, QPainter *p, |
| 156 | QSvgExtraStates &states, QRectF *boundingRect = nullptr); |
| 157 | |
| 158 | Orientation m_orientation; |
| 159 | qreal m_orientationAngle; |
| 160 | MarkerUnits m_markerUnits; |
| 161 | }; |
| 162 | |
| 163 | class Q_SVG_EXPORT QSvgFilterContainer : public QSvgStructureNode |
| 164 | { |
| 165 | public: |
| 166 | |
| 167 | QSvgFilterContainer(QSvgNode *parent, const QSvgRectF &bounds, QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits); |
| 168 | void drawCommand(QPainter *, QSvgExtraStates &) override {}; |
| 169 | bool (QPainter *, QSvgExtraStates &) const override; |
| 170 | Type type() const override; |
| 171 | QImage applyFilter(const QImage &buffer, QPainter *p, const QRectF &bounds) const; |
| 172 | void setSupported(bool supported); |
| 173 | bool supported() const; |
| 174 | QRectF filterRegion(const QRectF &itemBounds) const; |
| 175 | private: |
| 176 | QSvgRectF m_rect; |
| 177 | QtSvg::UnitTypes m_filterUnits; |
| 178 | QtSvg::UnitTypes m_primitiveUnits; |
| 179 | bool m_supported; |
| 180 | }; |
| 181 | |
| 182 | |
| 183 | class Q_SVG_EXPORT QSvgSwitch : public QSvgStructureNode |
| 184 | { |
| 185 | public: |
| 186 | QSvgSwitch(QSvgNode *parent); |
| 187 | void drawCommand(QPainter *p, QSvgExtraStates &states) override; |
| 188 | Type type() const override; |
| 189 | |
| 190 | QSvgNode *childToRender() const; |
| 191 | private: |
| 192 | void init(); |
| 193 | private: |
| 194 | QString m_systemLanguage; |
| 195 | QString m_systemLanguagePrefix; |
| 196 | }; |
| 197 | |
| 198 | class Q_SVG_EXPORT QSvgMask : public QSvgStructureNode |
| 199 | { |
| 200 | public: |
| 201 | QSvgMask(QSvgNode *parent, QSvgRectF bounds, |
| 202 | QtSvg::UnitTypes contentsUnits); |
| 203 | void drawCommand(QPainter *, QSvgExtraStates &) override {}; |
| 204 | bool (QPainter *, QSvgExtraStates &) const override; |
| 205 | Type type() const override; |
| 206 | QImage (QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect) const; |
| 207 | QImage (QPainter *p, QSvgExtraStates &states, const QRectF &localRect, QRectF *globalRect) const; |
| 208 | |
| 209 | QSvgRectF rect() const |
| 210 | { |
| 211 | return m_rect; |
| 212 | } |
| 213 | |
| 214 | QtSvg::UnitTypes contentUnits() const |
| 215 | { |
| 216 | return m_contentUnits; |
| 217 | } |
| 218 | |
| 219 | private: |
| 220 | QSvgRectF m_rect; |
| 221 | QtSvg::UnitTypes m_contentUnits; |
| 222 | }; |
| 223 | |
| 224 | class Q_SVG_EXPORT QSvgPattern : public QSvgStructureNode |
| 225 | { |
| 226 | public: |
| 227 | QSvgPattern(QSvgNode *parent, QSvgRectF bounds, QRectF viewBox, |
| 228 | QtSvg::UnitTypes contentUnits, QTransform transform); |
| 229 | void drawCommand(QPainter *, QSvgExtraStates &) override {}; |
| 230 | bool (QPainter *, QSvgExtraStates &) const override; |
| 231 | QImage (QPainter *p, QSvgExtraStates &states, const QSvgNode *patternElement); |
| 232 | Type type() const override; |
| 233 | const QTransform& appliedTransform() const { return m_appliedTransform; } |
| 234 | |
| 235 | private: |
| 236 | QImage renderPattern(QSize size, qreal contentScaleX, qreal contentScaleY); |
| 237 | void calculateAppliedTransform(QTransform& worldTransform, QRectF peLocalBB, QSize imageSize); |
| 238 | |
| 239 | private: |
| 240 | QTransform m_appliedTransform; |
| 241 | QSvgRectF m_rect; |
| 242 | QRectF m_viewBox; |
| 243 | QtSvg::UnitTypes m_contentUnits; |
| 244 | QTransform m_transform; |
| 245 | }; |
| 246 | |
| 247 | QT_END_NAMESPACE |
| 248 | |
| 249 | #endif // QSVGSTRUCTURE_P_H |
| 250 | |