Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsvgdocument_p.h
Go to the documentation of this file.
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// Qt-Security score:significant reason:default
4
5
6#ifndef QSVGDOCUMENT_P_H
7#define QSVGDOCUMENT_P_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
21#include "qtsvgglobal.h"
22#include "qtsvgglobal_p.h"
23
24#include "QtCore/qrect.h"
25#include "QtCore/qhash.h"
26#include "QtCore/qxmlstream.h"
27#include "QtCore/qscopedvaluerollback.h"
28#include "QtCore/qsharedpointer.h"
29#include "qsvgstyle_p.h"
30#include "qsvgfont_p.h"
31#include "private/qsvganimator_p.h"
32#include <QtSvg/private/qsvgpaintserver_p.h>
33
35
36class QPainter;
37class QByteArray;
38class QSvgFont;
39class QTransform;
40
41class Q_SVG_EXPORT QSvgDocument : public QSvgStructureNode
42{
43public:
44 static std::unique_ptr<QSvgDocument> load(const QString &file, QtSvg::Options options = {},
45 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
46 static std::unique_ptr<QSvgDocument> load(const QByteArray &contents, QtSvg::Options options = {},
47 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
48 static std::unique_ptr<QSvgDocument> load(QXmlStreamReader *contents, QtSvg::Options options = {},
49 QtSvg::AnimatorType type = QtSvg::AnimatorType::Automatic);
50 static bool isLikelySvg(QIODevice *device, bool *isCompressed = nullptr);
51public:
52 QSvgDocument(QtSvg::Options options, QtSvg::AnimatorType type);
53 ~QSvgDocument() override;
54
55 Type type() const override;
56
57 inline QSize size() const;
58 void setWidth(int len, bool percent);
59 void setHeight(int len, bool percent);
60 inline int width() const;
61 inline int height() const;
62 inline bool widthPercent() const;
63 inline bool heightPercent() const;
64
65 inline bool preserveAspectRatio() const;
66 void setPreserveAspectRatio(bool on);
67
68 inline QRectF viewBox() const;
69 void setViewBox(const QRectF &rect);
70 bool isCalculatingImplicitViewBox() { return m_calculatingImplicitViewBox; }
71
72 QtSvg::Options options() const;
73
74 void drawCommand(QPainter *, QSvgExtraStates &) override;
75
76 void draw(QPainter *p);
77 void draw(QPainter *p, const QRectF &bounds);
78 void draw(QPainter *p, const QString &id,
79 const QRectF &bounds=QRectF());
80
81 QTransform transformForElement(const QString &id) const;
82 QRectF boundsOnElement(const QString &id) const;
83 bool elementExists(const QString &id) const;
84
85 void addSvgFont(QSvgFont *);
86 QSvgFont *svgFont(const QString &family) const;
87 void addNamedNode(const QString &id, QSvgNode *node);
88 QSvgNode *namedNode(const QString &id) const;
89 void addPaintServer(QSvgPaintServerSharedPtr paintServer, const QString &id);
90 QSvgPaintServerSharedPtr paintServer(QStringView id) const;
91
92 void restartAnimation();
93 inline qint64 currentElapsed() const;
94 bool animated() const;
95 void setAnimated(bool a);
96 inline int animationDuration() const;
97 int currentFrame() const;
98 void setCurrentFrame(int);
99 void setFramesPerSecond(int num);
100
101 QSharedPointer<QSvgAbstractAnimator> animator() const;
102
103private:
104 void mapSourceToTarget(QPainter *p, const QRectF &targetRect, const QRectF &sourceRect = QRectF());
105private:
106 QSize m_size;
107 bool m_widthPercent;
108 bool m_heightPercent;
109
110 mutable bool m_calculatingImplicitViewBox = false;
111 mutable bool m_implicitViewBox = true;
112 mutable QRectF m_viewBox;
113 bool m_preserveAspectRatio = false;
114
115 QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts;
116 QHash<QString, QSvgNode *> m_namedNodes;
117 std::unordered_map<QString, QSvgPaintServerSharedPtr> m_paintServers;
118
119 bool m_animated;
120 int m_fps;
121
122 QSvgExtraStates m_states;
123
124 const QtSvg::Options m_options;
125 QSharedPointer<QSvgAbstractAnimator> m_animator;
126};
127
128Q_SVG_EXPORT QDebug operator<<(QDebug debug, const QSvgDocument &doc);
129
130inline std::optional<int> calculateSizeValue(bool isPercent, int sizeValue, qreal viewBoxSizeValue)
131{
132 if (!isPercent)
133 return sizeValue;
134
135 const double valueAsDouble = 0.01 * sizeValue * viewBoxSizeValue;
136 if (valueAsDouble < (std::numeric_limits<int>::min)() || valueAsDouble > (std::numeric_limits<int>::max)())
137 return {};
138 return qRound(valueAsDouble);
139}
140
141inline QSize QSvgDocument::size() const
142{
143 if (m_size.isEmpty())
144 return viewBox().size().toSize();
145 if (m_widthPercent || m_heightPercent) {
146 const std::optional<int> width = calculateSizeValue(m_widthPercent, m_size.width(), viewBox().size().width());
147 const std::optional<int> height = calculateSizeValue(m_heightPercent, m_size.height(), viewBox().size().height());
148 if (!width || !height)
149 return {};
150
151 return QSize(*width, *height);
152 }
153 return m_size;
154}
155
156inline int QSvgDocument::width() const
157{
158 return size().width();
159}
160
161inline int QSvgDocument::height() const
162{
163 return size().height();
164}
165
166inline bool QSvgDocument::widthPercent() const
167{
168 return m_widthPercent;
169}
170
171inline bool QSvgDocument::heightPercent() const
172{
173 return m_heightPercent;
174}
175
176inline QRectF QSvgDocument::viewBox() const
177{
178 if (m_viewBox.isNull()) {
179 QScopedValueRollback<bool> guard(m_calculatingImplicitViewBox, true);
180 m_viewBox = bounds();
181 m_implicitViewBox = true;
182 }
183
184 return m_viewBox;
185}
186
187inline bool QSvgDocument::preserveAspectRatio() const
188{
189 return m_preserveAspectRatio;
190}
191
192inline qint64 QSvgDocument::currentElapsed() const
193{
194 return m_animator->currentElapsed();
195}
196
197inline int QSvgDocument::animationDuration() const
198{
199 return m_animator->animationDuration();
200}
201
202QT_END_NAMESPACE
203
204#endif // QSVGDOCUMENT_P_H
friend class QPainter
Combined button and popup list for selecting options.
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
#define qPrintable(string)
Definition qstring.h:1683
static QByteArray qt_inflateSvgzDataFrom(QIODevice *device, bool doCheckContent=true)
static bool isValidMatrix(const QTransform &transform)
static bool hasSvgHeader(const QByteArray &buf)
std::optional< int > calculateSizeValue(bool isPercent, int sizeValue, qreal viewBoxSizeValue)