summaryrefslogtreecommitdiff
path: root/src/svg
diff options
context:
space:
mode:
Diffstat (limited to 'src/svg')
-rw-r--r--src/svg/qsvgrenderer.cpp35
-rw-r--r--src/svg/qsvgrenderer.h4
-rw-r--r--src/svg/qsvgtinydocument.cpp20
-rw-r--r--src/svg/qsvgtinydocument_p.h4
4 files changed, 55 insertions, 8 deletions
diff --git a/src/svg/qsvgrenderer.cpp b/src/svg/qsvgrenderer.cpp
index 40ae6ab..0097ec2 100644
--- a/src/svg/qsvgrenderer.cpp
+++ b/src/svg/qsvgrenderer.cpp
@@ -257,6 +257,41 @@ void QSvgRenderer::setFramesPerSecond(int num)
}
/*!
+ \property QSvgRenderer::aspectRatioMode
+
+ \brief how rendering adheres to the SVG view box aspect ratio
+
+ The accepted modes are:
+ \list
+ \li Qt::IgnoreAspectRatio (the default): the aspect ratio is ignored and the
+ rendering is stretched to the target bounds.
+ \li Qt::KeepAspectRatio: rendering is centered and scaled as large as possible
+ within the target bounds while preserving aspect ratio.
+ \endlist
+
+ \since 5.15
+*/
+
+Qt::AspectRatioMode QSvgRenderer::aspectRatioMode() const
+{
+ Q_D(const QSvgRenderer);
+ if (d->render && d->render->preserveAspectRatio())
+ return Qt::KeepAspectRatio;
+ return Qt::IgnoreAspectRatio;
+}
+
+void QSvgRenderer::setAspectRatioMode(Qt::AspectRatioMode mode)
+{
+ Q_D(QSvgRenderer);
+ if (d->render) {
+ if (mode == Qt::KeepAspectRatio)
+ d->render->setPreserveAspectRatio(true);
+ else if (mode == Qt::IgnoreAspectRatio)
+ d->render->setPreserveAspectRatio(false);
+ }
+}
+
+/*!
\property QSvgRenderer::currentFrame
\brief the current frame of the document's animation, or 0 if the document is not animated
\internal
diff --git a/src/svg/qsvgrenderer.h b/src/svg/qsvgrenderer.h
index c7da2fb..3703b1c 100644
--- a/src/svg/qsvgrenderer.h
+++ b/src/svg/qsvgrenderer.h
@@ -65,6 +65,7 @@ class Q_SVG_EXPORT QSvgRenderer : public QObject
Q_PROPERTY(QRectF viewBox READ viewBoxF WRITE setViewBox)
Q_PROPERTY(int framesPerSecond READ framesPerSecond WRITE setFramesPerSecond)
Q_PROPERTY(int currentFrame READ currentFrame WRITE setCurrentFrame)
+ Q_PROPERTY(Qt::AspectRatioMode aspectRatioMode READ aspectRatioMode WRITE setAspectRatioMode)
public:
QSvgRenderer(QObject *parent = nullptr);
QSvgRenderer(const QString &filename, QObject *parent = nullptr);
@@ -81,6 +82,9 @@ public:
void setViewBox(const QRect &viewbox);
void setViewBox(const QRectF &viewbox);
+ Qt::AspectRatioMode aspectRatioMode() const;
+ void setAspectRatioMode(Qt::AspectRatioMode mode);
+
bool animated() const;
int framesPerSecond() const;
void setFramesPerSecond(int num);
diff --git a/src/svg/qsvgtinydocument.cpp b/src/svg/qsvgtinydocument.cpp
index eb6b6b0..b4b9526 100644
--- a/src/svg/qsvgtinydocument.cpp
+++ b/src/svg/qsvgtinydocument.cpp
@@ -336,6 +336,11 @@ void QSvgTinyDocument::setHeight(int len, bool percent)
m_heightPercent = percent;
}
+void QSvgTinyDocument::setPreserveAspectRatio(bool on)
+{
+ m_preserveAspectRatio = on;
+}
+
void QSvgTinyDocument::setViewBox(const QRectF &rect)
{
m_viewBox = rect;
@@ -421,10 +426,10 @@ void QSvgTinyDocument::mapSourceToTarget(QPainter *p, const QRectF &targetRect,
source = viewBox();
if (source != target && !source.isNull()) {
- if (m_implicitViewBox || !sourceRect.isNull() || !targetRect.isNull()) {
- // Code path used when no view box is set, or when an explicit source size is given which
- // overrides it (which is the case when we're rendering only a specific element by id),
- // or when user has given explicit target bounds that overrides viebox aspect ratio
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
+ if (m_implicitViewBox || !preserveAspectRatio()) {
+ // Code path used when no view box is set, or IgnoreAspectRatio requested
+#endif
QTransform transform;
transform.scale(target.width() / source.width(),
target.height() / source.height());
@@ -433,10 +438,10 @@ void QSvgTinyDocument::mapSourceToTarget(QPainter *p, const QRectF &targetRect,
target.y() - c2.y());
p->scale(target.width() / source.width(),
target.height() / source.height());
+#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
} else {
- // Code path used when a view box is specified and we're not rendering a specific element by id
- // but the entire document. This attempts to emulate the default values of the <preserveAspectRatio>
- // tag that's implicitly defined when <viewbox> is used.
+ // Code path used when KeepAspectRatio is requested. This attempts to emulate the default values
+ // of the <preserveAspectRatio tag that's implicitly defined when <viewbox> is used.
// Scale the view box into the view port (target) by preserve the aspect ratio.
QSizeF viewBoxSize = source.size();
@@ -452,6 +457,7 @@ void QSvgTinyDocument::mapSourceToTarget(QPainter *p, const QRectF &targetRect,
// Apply the view box translation if specified.
p->translate(-source.x(), -source.y());
}
+#endif
}
}
diff --git a/src/svg/qsvgtinydocument_p.h b/src/svg/qsvgtinydocument_p.h
index d0c5cae..d4b92e8 100644
--- a/src/svg/qsvgtinydocument_p.h
+++ b/src/svg/qsvgtinydocument_p.h
@@ -89,6 +89,7 @@ public:
bool heightPercent() const;
bool preserveAspectRatio() const;
+ void setPreserveAspectRatio(bool on);
QRectF viewBox() const;
void setViewBox(const QRectF &rect);
@@ -128,6 +129,7 @@ private:
mutable bool m_implicitViewBox = true;
mutable QRectF m_viewBox;
+ bool m_preserveAspectRatio = false;
QHash<QString, QSvgRefCounter<QSvgFont> > m_fonts;
QHash<QString, QSvgNode *> m_namedNodes;
@@ -185,7 +187,7 @@ inline QRectF QSvgTinyDocument::viewBox() const
inline bool QSvgTinyDocument::preserveAspectRatio() const
{
- return false;
+ return m_preserveAspectRatio;
}
inline int QSvgTinyDocument::currentElapsed() const