From 84bc219fb1e9f586c1f7ac2c10172c4da2605f96 Mon Sep 17 00:00:00 2001 From: Eirik Aavitsland Date: Mon, 27 Jan 2020 14:22:19 +0100 Subject: Add API to enable opt-in aspect ratio feature in QSvgRenderer [ChangeLog][QSvgRenderer] Introduced aspect ratio mode property, so that rendering may be set to preserve the view box aspect ratio. Task-number: QTBUG-81259 Change-Id: I9802788cdaf2c30974c1ffc34ad5b67cdc74ed57 Reviewed-by: Simon Hausmann --- src/svg/qsvgrenderer.cpp | 35 ++++++++++++++++++++++++++++ src/svg/qsvgrenderer.h | 4 ++++ src/svg/qsvgtinydocument.cpp | 5 ++++ src/svg/qsvgtinydocument_p.h | 4 +++- tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp | 3 +-- 5 files changed, 48 insertions(+), 3 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 @@ -256,6 +256,41 @@ void QSvgRenderer::setFramesPerSecond(int num) d->fps = 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 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 8f6aac1..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; 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 > m_fonts; QHash 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 diff --git a/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp b/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp index 14cb473..686c854 100644 --- a/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp +++ b/tests/auto/qsvgrenderer/tst_qsvgrenderer.cpp @@ -270,18 +270,17 @@ void tst_QSvgRenderer::testMapViewBoxToTarget() QCOMPARE(picture.boundingRect(), QRect(125, 125, 250, 250)); } -#if 0 // Requires keep-aspectratio feature { // Viewport and viewBox specified -> scale 500x500 square to 1000x750 while preserving aspect ratio gives 750x750 data = ""; QPicture picture; QPainter painter(&picture); QSvgRenderer rend(data); + rend.setAspectRatioMode(Qt::KeepAspectRatio); rend.render(&painter); painter.end(); QCOMPARE(picture.boundingRect(), QRect(500, 375, 750, 750)); } -#endif } void tst_QSvgRenderer::testRenderElement() -- cgit v1.2.1