diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/imageformats/svg/main.cpp | 5 | ||||
-rw-r--r-- | src/svg/qsvggenerator.cpp | 57 | ||||
-rw-r--r-- | src/svg/qsvghandler.cpp | 23 | ||||
-rw-r--r-- | src/svg/qsvgrenderer.cpp | 71 | ||||
-rw-r--r-- | src/svg/qsvgrenderer.h | 9 | ||||
-rw-r--r-- | src/svg/qsvgstyle.cpp | 8 | ||||
-rw-r--r-- | src/svg/qsvgstyle_p.h | 10 | ||||
-rw-r--r-- | src/svg/qsvgtinydocument.cpp | 12 | ||||
-rw-r--r-- | src/svg/qsvgtinydocument_p.h | 7 | ||||
-rw-r--r-- | src/svg/qsvgwidget.cpp | 4 |
10 files changed, 144 insertions, 62 deletions
diff --git a/src/plugins/imageformats/svg/main.cpp b/src/plugins/imageformats/svg/main.cpp index f9fcfc3..d3fe476 100644 --- a/src/plugins/imageformats/svg/main.cpp +++ b/src/plugins/imageformats/svg/main.cpp @@ -72,10 +72,9 @@ QImageIOPlugin::Capabilities QSvgPlugin::capabilities(QIODevice *device, const Q if (format == "svg") #endif return Capabilities(CanRead); - if (!format.isEmpty()) - return 0; - Capabilities cap; + if (!format.isEmpty()) + return cap; if (device->isReadable() && QSvgIOHandler::canRead(device)) cap |= CanRead; return cap; diff --git a/src/svg/qsvggenerator.cpp b/src/svg/qsvggenerator.cpp index b671c44..59dd7fc 100644 --- a/src/svg/qsvggenerator.cpp +++ b/src/svg/qsvggenerator.cpp @@ -53,6 +53,7 @@ #include "qbuffer.h" #include "qmath.h" #include "qbitmap.h" +#include "qtransform.h" #include "qdebug.h" @@ -117,7 +118,7 @@ public: QBrush brush; QPen pen; - QMatrix matrix; + QTransform matrix; QFont font; QString generateGradientName() { @@ -227,10 +228,10 @@ public: QString rct(QStringLiteral("<rect x=\"%1\" y=\"%2\" width=\"%3\" height=\"%4\" />")); QTextStream str(&d_func()->defs, QIODevice::Append); str << "<mask id=\"" << maskId << "\" x=\"0\" y=\"0\" width=\"8\" height=\"8\" " - << "stroke=\"none\" fill=\"#ffffff\" patternUnits=\"userSpaceOnUse\" >" << endl; + << "stroke=\"none\" fill=\"#ffffff\" patternUnits=\"userSpaceOnUse\" >" << Qt::endl; for (QRect r : reg) - str << rct.arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height()) << endl; - str << QStringLiteral("</mask>") << endl << endl; + str << rct.arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height()) << Qt::endl; + str << QStringLiteral("</mask>") << Qt::endl << Qt::endl; d_func()->savedPatternMasks.append(maskId); } return maskId; @@ -243,9 +244,9 @@ public: QString maskId = savePatternMask(brush.style()); QString geo(QStringLiteral("x=\"0\" y=\"0\" width=\"8\" height=\"8\"")); QTextStream str(&d_func()->defs, QIODevice::Append); - str << QString(QStringLiteral("<pattern id=\"%1\" %2 patternUnits=\"userSpaceOnUse\" >")).arg(patternId, geo) << endl; - str << QString(QStringLiteral("<rect %1 stroke=\"none\" fill=\"%2\" mask=\"url(#%3);\" />")).arg(geo, color, maskId) << endl; - str << QStringLiteral("</pattern>") << endl << endl; + str << QString(QStringLiteral("<pattern id=\"%1\" %2 patternUnits=\"userSpaceOnUse\" >")).arg(patternId, geo) << Qt::endl; + str << QString(QStringLiteral("<rect %1 stroke=\"none\" fill=\"%2\" mask=\"url(#%3);\" />")).arg(geo, color, maskId) << Qt::endl; + str << QStringLiteral("</pattern>") << Qt::endl << Qt::endl; d_func()->savedPatternBrushes.append(patternId); } return patternId; @@ -266,7 +267,7 @@ public: str << QLatin1String("id=\"") << d_func()->generateGradientName() << QLatin1String("\">\n"); saveGradientStops(str, g); - str << QLatin1String("</linearGradient>") <<endl; + str << QLatin1String("</linearGradient>") <<Qt::endl; } void saveRadialGradientBrush(const QGradient *g) { @@ -283,7 +284,7 @@ public: } str << QLatin1String("id=\"") <<d_func()->generateGradientName()<< QLatin1String("\">\n"); saveGradientStops(str, g); - str << QLatin1String("</radialGradient>") << endl; + str << QLatin1String("</radialGradient>") << Qt::endl; } void saveConicalGradientBrush(const QGradient *) { @@ -544,7 +545,7 @@ public: "font-size=\"" << d->attributes.font_size << "\" " "font-weight=\"" << d->attributes.font_weight << "\" " "font-style=\"" << d->attributes.font_style << "\" " - << endl; + << Qt::endl; } }; @@ -892,29 +893,29 @@ bool QSvgPaintEngine::begin(QPaintDevice *) d->stream = new QTextStream(&d->header); // stream out the header... - *d->stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" << endl << "<svg"; + *d->stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" << Qt::endl << "<svg"; if (d->size.isValid()) { qreal wmm = d->size.width() * 25.4 / d->resolution; qreal hmm = d->size.height() * 25.4 / d->resolution; - *d->stream << " width=\"" << wmm << "mm\" height=\"" << hmm << "mm\"" << endl; + *d->stream << " width=\"" << wmm << "mm\" height=\"" << hmm << "mm\"" << Qt::endl; } if (d->viewBox.isValid()) { *d->stream << " viewBox=\"" << d->viewBox.left() << ' ' << d->viewBox.top(); - *d->stream << ' ' << d->viewBox.width() << ' ' << d->viewBox.height() << '\"' << endl; + *d->stream << ' ' << d->viewBox.width() << ' ' << d->viewBox.height() << '\"' << Qt::endl; } *d->stream << " xmlns=\"http://www.w3.org/2000/svg\"" " xmlns:xlink=\"http://www.w3.org/1999/xlink\" " - " version=\"1.2\" baseProfile=\"tiny\">" << endl; + " version=\"1.2\" baseProfile=\"tiny\">" << Qt::endl; if (!d->attributes.document_title.isEmpty()) { - *d->stream << "<title>" << d->attributes.document_title << "</title>" << endl; + *d->stream << "<title>" << d->attributes.document_title << "</title>" << Qt::endl; } if (!d->attributes.document_description.isEmpty()) { - *d->stream << "<desc>" << d->attributes.document_description << "</desc>" << endl; + *d->stream << "<desc>" << d->attributes.document_description << "</desc>" << Qt::endl; } d->stream->setString(&d->defs); @@ -924,7 +925,7 @@ bool QSvgPaintEngine::begin(QPaintDevice *) // Start the initial graphics state... *d->stream << "<g "; generateQtDefaults(); - *d->stream << endl; + *d->stream << Qt::endl; return true; } @@ -945,10 +946,10 @@ bool QSvgPaintEngine::end() *d->stream << d->defs; *d->stream << d->body; if (d->afterFirstUpdate) - *d->stream << "</g>" << endl; // close the updateState + *d->stream << "</g>" << Qt::endl; // close the updateState - *d->stream << "</g>" << endl // close the Qt defaults - << "</svg>" << endl; + *d->stream << "</g>" << Qt::endl // close the Qt defaults + << "</svg>" << Qt::endl; delete d->stream; @@ -1009,13 +1010,13 @@ void QSvgPaintEngine::updateState(const QPaintEngineState &state) } if (flags & QPaintEngine::DirtyTransform) { - d->matrix = state.transform().toAffine(); + d->matrix = state.transform(); *d->stream << "transform=\"matrix(" << d->matrix.m11() << ',' << d->matrix.m12() << ',' << d->matrix.m21() << ',' << d->matrix.m22() << ',' << d->matrix.dx() << ',' << d->matrix.dy() << ")\"" - << endl; + << Qt::endl; } if (flags & QPaintEngine::DirtyFont) { @@ -1027,7 +1028,7 @@ void QSvgPaintEngine::updateState(const QPaintEngineState &state) stream() << "opacity=\""<<state.opacity()<<"\" "; } - *d->stream << '>' << endl; + *d->stream << '>' << Qt::endl; d->afterFirstUpdate = true; } @@ -1046,7 +1047,7 @@ void QSvgPaintEngine::drawEllipse(const QRectF &r) *d->stream << "\" r=\"" << r.width() / qreal(2.0); else *d->stream << "\" rx=\"" << r.width() / qreal(2.0) << "\" ry=\"" << r.height() / qreal(2.0); - *d->stream << "\"/>" << endl; + *d->stream << "\"/>" << Qt::endl; } void QSvgPaintEngine::drawPath(const QPainterPath &p) @@ -1090,7 +1091,7 @@ void QSvgPaintEngine::drawPath(const QPainterPath &p) } } - *d->stream << "\"/>" << endl; + *d->stream << "\"/>" << Qt::endl; } void QSvgPaintEngine::drawPolygon(const QPointF *points, int pointCount, @@ -1112,7 +1113,7 @@ void QSvgPaintEngine::drawPolygon(const QPointF *points, int pointCount, const QPointF &pt = points[i]; stream() << pt.x() << ',' << pt.y() << ' '; } - stream() << "\" />" <<endl; + stream() << "\" />" <<Qt::endl; } else { path.closeSubpath(); drawPath(path); @@ -1130,7 +1131,7 @@ void QSvgPaintEngine::drawRects(const QRectF *rects, int rectCount) *d->stream << " vector-effect=\"non-scaling-stroke\""; *d->stream << " x=\"" << rect.x() << "\" y=\"" << rect.y() << "\" width=\"" << rect.width() << "\" height=\"" << rect.height() - << "\"/>" << endl; + << "\"/>" << Qt::endl; } } @@ -1155,7 +1156,7 @@ void QSvgPaintEngine::drawTextItem(const QPointF &pt, const QTextItem &textItem) *d->stream << " >" << s.toHtmlEscaped() << "</text>" - << endl; + << Qt::endl; } QT_END_NAMESPACE diff --git a/src/svg/qsvghandler.cpp b/src/svg/qsvghandler.cpp index 86ca77e..64a2bdc 100644 --- a/src/svg/qsvghandler.cpp +++ b/src/svg/qsvghandler.cpp @@ -60,6 +60,7 @@ #include "qmath.h" #include "qnumeric.h" #include <qregularexpression.h> +#include "qtransform.h" #include "qvarlengtharray.h" #include "private/qmath_p.h" @@ -1068,12 +1069,12 @@ static void parseBrush(QSvgNode *node, -static QMatrix parseTransformationMatrix(const QStringRef &value) +static QTransform parseTransformationMatrix(const QStringRef &value) { if (value.isEmpty()) - return QMatrix(); + return QTransform(); - QMatrix matrix; + QTransform matrix; const QChar *str = value.constData(); const QChar *end = str + value.length(); @@ -1156,9 +1157,9 @@ static QMatrix parseTransformationMatrix(const QStringRef &value) if(state == Matrix) { if(points.count() != 6) goto error; - matrix = QMatrix(points[0], points[1], - points[2], points[3], - points[4], points[5]) * matrix; + matrix = QTransform(points[0], points[1], + points[2], points[3], + points[4], points[5]) * matrix; } else if (state == Translate) { if (points.count() == 1) matrix.translate(points[0], 0); @@ -1435,7 +1436,7 @@ static void parseTransform(QSvgNode *node, { if (attributes.transform.isEmpty()) return; - QMatrix matrix = parseTransformationMatrix(trimRef(attributes.transform)); + QTransform matrix = parseTransformationMatrix(trimRef(attributes.transform)); if (!matrix.isIdentity()) { node->appendStyleProperty(new QSvgTransformStyle(QTransform(matrix)), attributes.id); @@ -2846,7 +2847,7 @@ static void parseBaseGradient(QSvgNode *node, handler->pushColor(color); } - QMatrix matrix; + QTransform matrix; QGradient *grad = gradProp->qgradient(); if (!link.isEmpty()) { QSvgStyleProperty *prop = node->styleProperty(link); @@ -2861,7 +2862,7 @@ static void parseBaseGradient(QSvgNode *node, gradProp->setGradientStopsSet(inherited->gradientStopsSet()); } - matrix = inherited->qmatrix(); + matrix = inherited->qtransform(); } else { gradProp->setStopLink(link, handler->document()); } @@ -2869,9 +2870,9 @@ static void parseBaseGradient(QSvgNode *node, if (!trans.isEmpty()) { matrix = parseTransformationMatrix(trans); - gradProp->setMatrix(matrix); + gradProp->setTransform(matrix); } else if (!matrix.isIdentity()) { - gradProp->setMatrix(matrix); + gradProp->setTransform(matrix); } if (!spread.isEmpty()) { diff --git a/src/svg/qsvgrenderer.cpp b/src/svg/qsvgrenderer.cpp index da31a31..0097ec2 100644 --- a/src/svg/qsvgrenderer.cpp +++ b/src/svg/qsvgrenderer.cpp @@ -45,6 +45,7 @@ #include "qbytearray.h" #include "qtimer.h" +#include "qtransform.h" #include "qdebug.h" #include "private/qobject_p.h" @@ -256,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 @@ -437,7 +473,7 @@ void QSvgRenderer::setViewBox(const QRectF &viewbox) The transformation matrix of parent elements is not affecting the bounds of the element. - \sa matrixForElement() + \sa transformForElement() */ QRectF QSvgRenderer::boundsOnElement(const QString &id) const { @@ -471,8 +507,15 @@ bool QSvgRenderer::elementExists(const QString &id) const return exists; } +#if QT_DEPRECATED_SINCE(5, 15) +QT_WARNING_PUSH +QT_WARNING_DISABLE_DEPRECATED /*! \since 4.2 + \deprecated + + Use transformForElement() instead. + Returns the transformation matrix for the element with the given \a id. The matrix is a product of @@ -486,11 +529,31 @@ bool QSvgRenderer::elementExists(const QString &id) const */ QMatrix QSvgRenderer::matrixForElement(const QString &id) const { + return transformForElement(id).toAffine(); +} +QT_WARNING_POP +#endif // QT_DEPRECATED_SINCE(5, 15) + +/*! + \since 5.15 + + Returns the transformation matrix for the element + with the given \a id. The matrix is a product of + the transformation of the element's parents. The transformation of + the element itself is not included. + + To find the bounding rectangle of the element in logical coordinates, + you can apply the matrix on the rectangle returned from boundsOnElement(). + + \sa boundsOnElement() +*/ +QTransform QSvgRenderer::transformForElement(const QString &id) const +{ Q_D(const QSvgRenderer); - QMatrix mat; + QTransform trans; if (d->render) - mat = d->render->matrixForElement(id); - return mat; + trans = d->render->transformForElement(id); + return trans; } QT_END_NAMESPACE diff --git a/src/svg/qsvgrenderer.h b/src/svg/qsvgrenderer.h index 4413509..3703b1c 100644 --- a/src/svg/qsvgrenderer.h +++ b/src/svg/qsvgrenderer.h @@ -56,6 +56,7 @@ QT_BEGIN_NAMESPACE class QSvgRendererPrivate; class QPainter; class QByteArray; +class QTransform; class Q_SVG_EXPORT QSvgRenderer : public QObject { @@ -64,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); @@ -80,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); @@ -89,7 +94,11 @@ public: QRectF boundsOnElement(const QString &id) const; bool elementExists(const QString &id) const; +#if QT_DEPRECATED_SINCE(5, 15) + QT_DEPRECATED_X("Use transformForElement()") QMatrix matrixForElement(const QString &id) const; +#endif // QT_DEPRECATED_SINCE(5, 15) + QTransform transformForElement(const QString &id) const; public Q_SLOTS: bool load(const QString &filename); diff --git a/src/svg/qsvgstyle.cpp b/src/svg/qsvgstyle.cpp index c4edd25..f981931 100644 --- a/src/svg/qsvgstyle.cpp +++ b/src/svg/qsvgstyle.cpp @@ -421,16 +421,16 @@ QBrush QSvgGradientStyle::brush(QPainter *, QSvgExtraStates &) QBrush b(*m_gradient); - if (!m_matrix.isIdentity()) - b.setMatrix(m_matrix); + if (!m_transform.isIdentity()) + b.setTransform(m_transform); return b; } -void QSvgGradientStyle::setMatrix(const QMatrix &mat) +void QSvgGradientStyle::setTransform(const QTransform &transform) { - m_matrix = mat; + m_transform = transform; } QSvgTransformStyle::QSvgTransformStyle(const QTransform &trans) diff --git a/src/svg/qsvgstyle_p.h b/src/svg/qsvgstyle_p.h index 9d616d4..5e0b7cc 100644 --- a/src/svg/qsvgstyle_p.h +++ b/src/svg/qsvgstyle_p.h @@ -54,7 +54,7 @@ #include "QtGui/qpainter.h" #include "QtGui/qpen.h" #include "QtGui/qbrush.h" -#include "QtGui/qmatrix.h" +#include "QtGui/qtransform.h" #include "QtGui/qcolor.h" #include "QtGui/qfont.h" #include <qdebug.h> @@ -581,10 +581,10 @@ public: void resolveStops(); void resolveStops_helper(QStringList *visited); - void setMatrix(const QMatrix &matrix); - QMatrix qmatrix() const + void setTransform(const QTransform &transform); + QTransform qtransform() const { - return m_matrix; + return m_transform; } QGradient *qgradient() const @@ -605,7 +605,7 @@ public: QBrush brush(QPainter *, QSvgExtraStates &) override; private: QGradient *m_gradient; - QMatrix m_matrix; + QTransform m_transform; QSvgTinyDocument *m_doc; QString m_link; diff --git a/src/svg/qsvgtinydocument.cpp b/src/svg/qsvgtinydocument.cpp index 0877e41..b4b9526 100644 --- a/src/svg/qsvgtinydocument.cpp +++ b/src/svg/qsvgtinydocument.cpp @@ -48,6 +48,7 @@ #include "qbytearray.h" #include "qqueue.h" #include "qstack.h" +#include "qtransform.h" #include "qdebug.h" #ifndef QT_NO_COMPRESS @@ -335,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; @@ -470,13 +476,13 @@ bool QSvgTinyDocument::elementExists(const QString &id) const return (node!=0); } -QMatrix QSvgTinyDocument::matrixForElement(const QString &id) const +QTransform QSvgTinyDocument::transformForElement(const QString &id) const { QSvgNode *node = scopeNode(id); if (!node) { qCDebug(lcSvgHandler, "Couldn't find node %s. Skipping rendering.", qPrintable(id)); - return QMatrix(); + return QTransform(); } QTransform t; @@ -488,7 +494,7 @@ QMatrix QSvgTinyDocument::matrixForElement(const QString &id) const node = node->parent(); } - return t.toAffine(); + return t; } int QSvgTinyDocument::currentFrame() const diff --git a/src/svg/qsvgtinydocument_p.h b/src/svg/qsvgtinydocument_p.h index 404587d..d4b92e8 100644 --- a/src/svg/qsvgtinydocument_p.h +++ b/src/svg/qsvgtinydocument_p.h @@ -67,6 +67,7 @@ QT_BEGIN_NAMESPACE class QPainter; class QByteArray; class QSvgFont; +class QTransform; class Q_SVG_PRIVATE_EXPORT QSvgTinyDocument : public QSvgStructureNode { @@ -88,6 +89,7 @@ public: bool heightPercent() const; bool preserveAspectRatio() const; + void setPreserveAspectRatio(bool on); QRectF viewBox() const; void setViewBox(const QRectF &rect); @@ -99,7 +101,7 @@ public: void draw(QPainter *p, const QString &id, const QRectF &bounds=QRectF()); - QMatrix matrixForElement(const QString &id) const; + QTransform transformForElement(const QString &id) const; QRectF boundsOnElement(const QString &id) const; bool elementExists(const QString &id) const; @@ -127,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; @@ -184,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/src/svg/qsvgwidget.cpp b/src/svg/qsvgwidget.cpp index 4b86306..acc8cca 100644 --- a/src/svg/qsvgwidget.cpp +++ b/src/svg/qsvgwidget.cpp @@ -90,7 +90,7 @@ public: Constructs a new SVG display widget with the given \a parent. */ QSvgWidget::QSvgWidget(QWidget *parent) - : QWidget(*new QSvgWidgetPrivate, parent, 0) + : QWidget(*new QSvgWidgetPrivate, parent, {}) { d_func()->renderer = new QSvgRenderer(this); QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()), @@ -102,7 +102,7 @@ QSvgWidget::QSvgWidget(QWidget *parent) of the specified \a file. */ QSvgWidget::QSvgWidget(const QString &file, QWidget *parent) - : QWidget(*new QSvgWidgetPrivate, parent, 0) + : QWidget(*new QSvgWidgetPrivate, parent, {}) { d_func()->renderer = new QSvgRenderer(file, this); QObject::connect(d_func()->renderer, SIGNAL(repaintNeeded()), |