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 QSVGTINYDOCUMENT_P_H |
41 | #define QSVGTINYDOCUMENT_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 "qsvgstructure_p.h" |
55 | #include "qtsvgglobal_p.h" |
56 | |
57 | #include "QtCore/qrect.h" |
58 | #include "QtCore/qlist.h" |
59 | #include "QtCore/qhash.h" |
60 | #include "QtCore/qdatetime.h" |
61 | #include "QtCore/qxmlstream.h" |
62 | #include "qsvgstyle_p.h" |
63 | #include "qsvgfont_p.h" |
64 | |
65 | QT_BEGIN_NAMESPACE |
66 | |
67 | class QPainter; |
68 | class QByteArray; |
69 | class QSvgFont; |
70 | class QTransform; |
71 | |
72 | class Q_SVG_PRIVATE_EXPORT QSvgTinyDocument : public QSvgStructureNode |
73 | { |
74 | public: |
75 | static QSvgTinyDocument * load(const QString &file); |
76 | static QSvgTinyDocument * load(const QByteArray &contents); |
77 | static QSvgTinyDocument * load(QXmlStreamReader *contents); |
78 | public: |
79 | QSvgTinyDocument(); |
80 | ~QSvgTinyDocument(); |
81 | Type type() const override; |
82 | |
83 | QSize size() const; |
84 | void setWidth(int len, bool percent); |
85 | void setHeight(int len, bool percent); |
86 | int width() const; |
87 | int height() const; |
88 | bool widthPercent() const; |
89 | bool heightPercent() const; |
90 | |
91 | bool preserveAspectRatio() const; |
92 | void setPreserveAspectRatio(bool on); |
93 | |
94 | QRectF viewBox() const; |
95 | void setViewBox(const QRectF &rect); |
96 | |
97 | void (QPainter *p, QSvgExtraStates &) override; //from the QSvgNode |
98 | |
99 | void draw(QPainter *p); |
100 | void draw(QPainter *p, const QRectF &bounds); |
101 | void draw(QPainter *p, const QString &id, |
102 | const QRectF &bounds=QRectF()); |
103 | |
104 | QTransform transformForElement(const QString &id) const; |
105 | QRectF boundsOnElement(const QString &id) const; |
106 | bool elementExists(const QString &id) const; |
107 | |
108 | void addSvgFont(QSvgFont *); |
109 | QSvgFont *svgFont(const QString &family) const; |
110 | void addNamedNode(const QString &id, QSvgNode *node); |
111 | QSvgNode *namedNode(const QString &id) const; |
112 | void addNamedStyle(const QString &id, QSvgFillStyleProperty *style); |
113 | QSvgFillStyleProperty *namedStyle(const QString &id) const; |
114 | |
115 | void restartAnimation(); |
116 | int currentElapsed() const; |
117 | bool animated() const; |
118 | void setAnimated(bool a); |
119 | int animationDuration() const; |
120 | int currentFrame() const; |
121 | void setCurrentFrame(int); |
122 | void setFramesPerSecond(int num); |
123 | private: |
124 | void mapSourceToTarget(QPainter *p, const QRectF &targetRect, const QRectF &sourceRect = QRectF()); |
125 | private: |
126 | QSize m_size; |
127 | bool m_widthPercent; |
128 | bool m_heightPercent; |
129 | |
130 | mutable bool m_implicitViewBox = true; |
131 | mutable QRectF m_viewBox; |
132 | bool m_preserveAspectRatio = false; |
133 | |
134 | QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts; |
135 | QHash<QString, QSvgNode *> m_namedNodes; |
136 | QHash<QString, QSvgRefCounter<QSvgFillStyleProperty> > m_namedStyles; |
137 | |
138 | qint64 m_time; |
139 | bool m_animated; |
140 | int m_animationDuration; |
141 | int m_fps; |
142 | |
143 | QSvgExtraStates m_states; |
144 | }; |
145 | |
146 | inline QSize QSvgTinyDocument::size() const |
147 | { |
148 | if (m_size.isEmpty()) |
149 | return viewBox().size().toSize(); |
150 | if (m_widthPercent || m_heightPercent) { |
151 | const int width = m_widthPercent ? qRound(d: 0.01 * m_size.width() * viewBox().size().width()) : m_size.width(); |
152 | const int height = m_heightPercent ? qRound(d: 0.01 * m_size.height() * viewBox().size().height()) : m_size.height(); |
153 | return QSize(width, height); |
154 | } |
155 | return m_size; |
156 | } |
157 | |
158 | inline int QSvgTinyDocument::width() const |
159 | { |
160 | return size().width(); |
161 | } |
162 | |
163 | inline int QSvgTinyDocument::height() const |
164 | { |
165 | return size().height(); |
166 | } |
167 | |
168 | inline bool QSvgTinyDocument::widthPercent() const |
169 | { |
170 | return m_widthPercent; |
171 | } |
172 | |
173 | inline bool QSvgTinyDocument::heightPercent() const |
174 | { |
175 | return m_heightPercent; |
176 | } |
177 | |
178 | inline QRectF QSvgTinyDocument::viewBox() const |
179 | { |
180 | if (m_viewBox.isNull()) { |
181 | m_viewBox = transformedBounds(); |
182 | m_implicitViewBox = true; |
183 | } |
184 | |
185 | return m_viewBox; |
186 | } |
187 | |
188 | inline bool QSvgTinyDocument::preserveAspectRatio() const |
189 | { |
190 | return m_preserveAspectRatio; |
191 | } |
192 | |
193 | inline int QSvgTinyDocument::currentElapsed() const |
194 | { |
195 | return QDateTime::currentMSecsSinceEpoch() - m_time; |
196 | } |
197 | |
198 | inline int QSvgTinyDocument::animationDuration() const |
199 | { |
200 | return m_animationDuration; |
201 | } |
202 | |
203 | QT_END_NAMESPACE |
204 | |
205 | #endif // QSVGTINYDOCUMENT_P_H |
206 | |