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 QSVGTINYDOCUMENT_P_H
5#define QSVGTINYDOCUMENT_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 "qsvgstructure_p.h"
19#include "qtsvgglobal_p.h"
20
21#include "QtCore/qrect.h"
22#include "QtCore/qlist.h"
23#include "QtCore/qhash.h"
24#include "QtCore/qdatetime.h"
25#include "QtCore/qxmlstream.h"
26#include "QtCore/qsharedpointer.h"
27#include "qsvgstyle_p.h"
28#include "qsvgfont_p.h"
29#include "private/qsvganimator_p.h"
30
31QT_BEGIN_NAMESPACE
32
33class QPainter;
34class QByteArray;
35class QSvgFont;
36class QTransform;
37
38class Q_SVG_EXPORT QSvgTinyDocument : public QSvgStructureNode
39{
40public:
41 static QSvgTinyDocument *load(const QString &file, QtSvg::Options options = {},
42 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
43 static QSvgTinyDocument *load(const QByteArray &contents, QtSvg::Options options = {},
44 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
45 static QSvgTinyDocument *load(QXmlStreamReader *contents, QtSvg::Options options = {},
46 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
47 static bool isLikelySvg(QIODevice *device, bool *isCompressed = nullptr);
48public:
49 QSvgTinyDocument(QtSvg::Options options, QtSvg::AnimatorType type);
50 ~QSvgTinyDocument();
51 Type type() const override;
52
53 inline QSize size() const;
54 void setWidth(int len, bool percent);
55 void setHeight(int len, bool percent);
56 inline int width() const;
57 inline int height() const;
58 inline bool widthPercent() const;
59 inline bool heightPercent() const;
60
61 inline bool preserveAspectRatio() const;
62 void setPreserveAspectRatio(bool on);
63
64 inline QRectF viewBox() const;
65 void setViewBox(const QRectF &rect);
66
67 QtSvg::Options options() const;
68
69 void drawCommand(QPainter *, QSvgExtraStates &) override;
70
71 void draw(QPainter *p);
72 void draw(QPainter *p, const QRectF &bounds);
73 void draw(QPainter *p, const QString &id,
74 const QRectF &bounds=QRectF());
75
76 QTransform transformForElement(const QString &id) const;
77 QRectF boundsOnElement(const QString &id) const;
78 bool elementExists(const QString &id) const;
79
80 void addSvgFont(QSvgFont *);
81 QSvgFont *svgFont(const QString &family) const;
82 void addNamedNode(const QString &id, QSvgNode *node);
83 QSvgNode *namedNode(const QString &id) const;
84 void addNamedStyle(const QString &id, QSvgPaintStyleProperty *style);
85 QSvgPaintStyleProperty *namedStyle(const QString &id) const;
86
87 void restartAnimation();
88 inline qint64 currentElapsed() const;
89 bool animated() const;
90 void setAnimated(bool a);
91 inline int animationDuration() const;
92 int currentFrame() const;
93 void setCurrentFrame(int);
94 void setFramesPerSecond(int num);
95
96 QSharedPointer<QSvgAbstractAnimator> animator() const;
97
98private:
99 void mapSourceToTarget(QPainter *p, const QRectF &targetRect, const QRectF &sourceRect = QRectF());
100private:
101 QSize m_size;
102 bool m_widthPercent;
103 bool m_heightPercent;
104
105 mutable bool m_implicitViewBox = true;
106 mutable QRectF m_viewBox;
107 bool m_preserveAspectRatio = false;
108
109 QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts;
110 QHash<QString, QSvgNode *> m_namedNodes;
111 QHash<QString, QSvgRefCounter<QSvgPaintStyleProperty> > m_namedStyles;
112
113 bool m_animated;
114 int m_fps;
115
116 QSvgExtraStates m_states;
117
118 const QtSvg::Options m_options;
119 QSharedPointer<QSvgAbstractAnimator> m_animator;
120};
121
122Q_SVG_EXPORT QDebug operator<<(QDebug debug, const QSvgTinyDocument &doc);
123
124inline QSize QSvgTinyDocument::size() const
125{
126 if (m_size.isEmpty())
127 return viewBox().size().toSize();
128 if (m_widthPercent || m_heightPercent) {
129 const int width = m_widthPercent ? qRound(d: 0.01 * m_size.width() * viewBox().size().width()) : m_size.width();
130 const int height = m_heightPercent ? qRound(d: 0.01 * m_size.height() * viewBox().size().height()) : m_size.height();
131 return QSize(width, height);
132 }
133 return m_size;
134}
135
136inline int QSvgTinyDocument::width() const
137{
138 return size().width();
139}
140
141inline int QSvgTinyDocument::height() const
142{
143 return size().height();
144}
145
146inline bool QSvgTinyDocument::widthPercent() const
147{
148 return m_widthPercent;
149}
150
151inline bool QSvgTinyDocument::heightPercent() const
152{
153 return m_heightPercent;
154}
155
156inline QRectF QSvgTinyDocument::viewBox() const
157{
158 if (m_viewBox.isNull()) {
159 m_viewBox = bounds();
160 m_implicitViewBox = true;
161 }
162
163 return m_viewBox;
164}
165
166inline bool QSvgTinyDocument::preserveAspectRatio() const
167{
168 return m_preserveAspectRatio;
169}
170
171inline qint64 QSvgTinyDocument::currentElapsed() const
172{
173 return m_animator->currentElapsed();
174}
175
176inline int QSvgTinyDocument::animationDuration() const
177{
178 return m_animator->animationDuration();
179}
180
181QT_END_NAMESPACE
182
183#endif // QSVGTINYDOCUMENT_P_H
184

source code of qtsvg/src/svg/qsvgtinydocument_p.h