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