summaryrefslogtreecommitdiffstats
path: root/src/svg/qsvgstructure_p.h
blob: 9a3c2a01fcea65956f1ff2d604e544f20c6e813a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QSVGSTRUCTURE_P_H
#define QSVGSTRUCTURE_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qsvgnode_p.h"

#include "QtCore/qlist.h"
#include "QtCore/qhash.h"

QT_BEGIN_NAMESPACE

class QSvgTinyDocument;
class QSvgNode;
class QPainter;
class QSvgDefs;

class Q_SVG_EXPORT QSvgStructureNode : public QSvgNode
{
public:
    QSvgStructureNode(QSvgNode *parent);
    ~QSvgStructureNode();
    QSvgNode *scopeNode(const QString &id) const;
    void addChild(QSvgNode *child, const QString &id);
    QRectF bounds(QPainter *p, QSvgExtraStates &states) const override;
    QSvgNode *previousSiblingNode(QSvgNode *n) const;
    QList<QSvgNode*> renderers() const { return m_renderers; }
protected:
    QList<QSvgNode*>          m_renderers;
    QHash<QString, QSvgNode*> m_scope;
    QList<QSvgStructureNode*> m_linkedScopes;
    mutable bool              m_recursing = false;
};

class Q_SVG_EXPORT QSvgG : public QSvgStructureNode
{
public:
    QSvgG(QSvgNode *parent);
    void drawCommand(QPainter *, QSvgExtraStates &) override;
    bool shouldDrawNode(QPainter *p, QSvgExtraStates &states) const override;
    Type type() const override;
};

class Q_SVG_EXPORT QSvgDefs : public QSvgStructureNode
{
public:
    QSvgDefs(QSvgNode *parent);
    void drawCommand(QPainter *, QSvgExtraStates &) override {};
    bool shouldDrawNode(QPainter *p, QSvgExtraStates &states) const override;
    Type type() const override;
};

class Q_SVG_EXPORT QSvgSymbolLike : public QSvgStructureNode
{
    // Marker, Symbol and potentially other elements share a lot of common
    // attributes and functionality. By making a common base class we can
    // avoid repetition.
public:
    enum class Overflow : quint8 {
        Visible,
        Hidden,
        Scroll = Visible, //Will not support scrolling
        Auto = Visible
    };

    enum class PreserveAspectRatio : quint8 {
        None =  0b000000,
        xMin =  0b000001,
        xMid =  0b000010,
        xMax =  0b000011,
        yMin =  0b000100,
        yMid =  0b001000,
        yMax =  0b001100,
        meet =  0b010000,
        slice = 0b100000,
        xMask = xMin | xMid | xMax,
        yMask = yMin | yMid | yMax,
        xyMask = xMask | yMask,
        meetSliceMask = meet | slice
    };
    Q_DECLARE_FLAGS(PreserveAspectRatios, PreserveAspectRatio)

    QSvgSymbolLike(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
                   QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow);
    void drawCommand(QPainter *, QSvgExtraStates &) override {};
protected:
    void setPainterToRectAndAdjustment(QPainter *p) const;
protected:
    QRectF m_rect;
    QRectF m_viewBox;
    QPointF m_refP;
    PreserveAspectRatios m_pAspectRatios;
    Overflow m_overflow;
};

Q_DECLARE_OPERATORS_FOR_FLAGS(QSvgSymbolLike::PreserveAspectRatios)

class Q_SVG_EXPORT QSvgSymbol : public QSvgSymbolLike
{
public:
    QSvgSymbol(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
               QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow);
    void drawCommand(QPainter *p, QSvgExtraStates &states) override;
    Type type() const override;
};

class Q_SVG_EXPORT QSvgMarker : public QSvgSymbolLike
{
public:
    enum class Orientation : quint8 {
        Auto,
        AutoStartReverse,
        Value
    };
    enum class MarkerUnits : quint8 {
        StrokeWidth,
        UserSpaceOnUse
    };

    QSvgMarker(QSvgNode *parent, QRectF bounds, QRectF viewBox, QPointF refP,
               QSvgSymbolLike::PreserveAspectRatios pAspectRatios, QSvgSymbolLike::Overflow overflow,
               Orientation orientation, qreal orientationAngle, MarkerUnits markerUnits);
    void drawCommand(QPainter *p, QSvgExtraStates &states) override;
    static void drawMarkersForNode(QSvgNode *node, QPainter *p, QSvgExtraStates &states);
    Orientation orientation() const {
        return m_orientation;
    }
    qreal orientationAngle() const {
        return m_orientationAngle;
    }
    MarkerUnits markerUnits() const {
        return m_markerUnits;
    }
    Type type() const override;
private:
    struct PositionMarkerPair {
        qreal x;
        qreal y;
        qreal angle;
        QString markerId;
        bool isStartNode = false;
    };
    Orientation m_orientation;
    qreal m_orientationAngle;
    MarkerUnits m_markerUnits;
};

class Q_SVG_EXPORT QSvgFilterContainer : public QSvgStructureNode
{
public:

    QSvgFilterContainer(QSvgNode *parent, const QSvgRectF &bounds, QtSvg::UnitTypes filterUnits, QtSvg::UnitTypes primitiveUnits);
    void drawCommand(QPainter *, QSvgExtraStates &) override {};
    Type type() const override;
    QImage applyFilter(QSvgNode *referenceNode, const QImage &buffer, QPainter *p, QRectF bounds) const;
private:
    QSvgRectF m_rect;
    QtSvg::UnitTypes m_filterUnits;
    QtSvg::UnitTypes m_primitiveUnits;
};


class Q_SVG_EXPORT QSvgSwitch : public QSvgStructureNode
{
public:
    QSvgSwitch(QSvgNode *parent);
    void drawCommand(QPainter *p, QSvgExtraStates &states) override;
    Type type() const override;
private:
    void init();
private:
    QString m_systemLanguage;
    QString m_systemLanguagePrefix;
};

class Q_SVG_EXPORT QSvgMask : public QSvgStructureNode
{
public:
    QSvgMask(QSvgNode *parent, QSvgRectF bounds,
             QtSvg::UnitTypes contentsUnits);
    void drawCommand(QPainter *, QSvgExtraStates &) override {};
    Type type() const override;
    QImage createMask(QPainter *p, QSvgExtraStates &states, QSvgNode *targetNode, QRectF *globalRect) const;
    QImage createMask(QPainter *p, QSvgExtraStates &states, const QRectF &localRect, QRectF *globalRect) const;

    QSvgRectF rect() const
    {
        return m_rect;
    }

    QtSvg::UnitTypes contentUnits() const
    {
        return m_contentUnits;
    }

private:
    QSvgRectF m_rect;
    QtSvg::UnitTypes m_contentUnits;
};

class Q_SVG_EXPORT QSvgPattern : public QSvgStructureNode
{
public:
    QSvgPattern(QSvgNode *parent, QSvgRectF bounds, QRectF viewBox,
                QtSvg::UnitTypes contentUnits, QTransform transform);
    void drawCommand(QPainter *, QSvgExtraStates &) override {};
    QImage patternImage(QPainter *p, QSvgExtraStates &states, const QSvgNode *patternElement);
    Type type() const override;
    const QTransform& appliedTransform() const { return m_appliedTransform; }

private:
    QImage renderPattern(QSize size, qreal contentScaleX, qreal contentScaleY);
    void calculateAppliedTransform(QTransform& worldTransform, QRectF peLocalBB, QSize imageSize);

private:
    QTransform m_appliedTransform;
    QSvgRectF m_rect;
    QRectF m_viewBox;
    QtSvg::UnitTypes m_contentUnits;
    QTransform m_transform;
};

QT_END_NAMESPACE

#endif // QSVGSTRUCTURE_P_H