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

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