From 77ad7934057b056bdb0c15d15a01ef83190dafaa Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 7 Nov 2022 14:41:45 +0100 Subject: QSvgGenerator: add support for clip paths SVG 1.1 allows to specify clipping paths. Before they were silently discarded, but now we can support them. The SVG generator code is very simple at its core -- at *any* state change of the painter, a new tag is emitted with the new state (brush, pen, transform, ...). Clipping is slightly more complicated because: 1) it needs its own element (), which needs to be referenced by a shape/group by using a clip-path attribute (specifying a url). 2) in QPainter clipping happens in the logical coordinates when the clip was set. Then the coordinates may get transformed again, but the drawn shapes still have to honor the original clipping. In SVG, if one specifies both the clip-path and the transform attributes on a shape, the transformation also affects the clip-path (!). This is the 'clipPathUnits' attribute [1], that however doesn't match QPainter semantics. As a workaround: a) store clip paths already transformed (using the transform existing when the clip path got set) b) when clipping is active, emit an untransformed group, clip that group, then open another inner group with the current painter transformation. This ensures that the clip path is unaffected by any further modification of the painter's transform. Add a manual test. [1] https://www.w3.org/TR/SVG11/masking.html#EstablishingANewClippingPath Change-Id: I78161091925dc09c86e35ed042e31cece2618b9d Reviewed-by: Albert Astals Cid Reviewed-by: Eirik Aavitsland --- src/svg/qsvggenerator.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'src') diff --git a/src/svg/qsvggenerator.cpp b/src/svg/qsvggenerator.cpp index 7514275..7ab882d 100644 --- a/src/svg/qsvggenerator.cpp +++ b/src/svg/qsvggenerator.cpp @@ -20,6 +20,8 @@ #include "qdebug.h" +#include + QT_BEGIN_NAMESPACE static void translate_color(const QColor &color, QString *color_string, @@ -109,6 +111,21 @@ public: QString dashPattern, dashOffset; QString fill, fillOpacity; } attributes; + + QString generateClipPathName() { + ++numClipPaths; + currentClipPathName = QStringLiteral("clipPath%1").arg(numClipPaths); + return currentClipPathName; + } + + std::optional clipPath; + bool clipEnabled = false; + bool isClippingEffective() const { + return clipEnabled && clipPath.has_value(); + } + QString currentClipPathName; + int numClipPaths = 0; + bool hasEmittedClipGroup = false; }; static inline QPaintEngine::PaintEngineFeatures svgEngineFeatures() @@ -137,6 +154,7 @@ public: bool end() override; void updateState(const QPaintEngineState &state) override; + void updateClipState(const QPaintEngineState &state); void popGroup(); void drawEllipse(const QRectF &r) override; @@ -937,6 +955,8 @@ bool QSvgPaintEngine::end() *d->stream << d->header; *d->stream << d->defs; *d->stream << d->body; + if (d->hasEmittedClipGroup) + *d->stream << ""; if (d->afterFirstUpdate) *d->stream << "" << Qt::endl; // close the updateState @@ -992,9 +1012,20 @@ void QSvgPaintEngine::updateState(const QPaintEngineState &state) // always stream full gstate, which is not required, but... // close old state and start a new one... + if (d->hasEmittedClipGroup) + *d->stream << "\n"; if (d->afterFirstUpdate) *d->stream << "\n\n"; + updateClipState(state); + + if (d->isClippingEffective()) { + *d->stream << QStringLiteral("").arg(d->currentClipPathName); + d->hasEmittedClipGroup = true; + } else { + d->hasEmittedClipGroup = false; + } + *d->stream << "afterFirstUpdate = true; } +void QSvgPaintEngine::updateClipState(const QPaintEngineState &state) +{ + Q_D(QSvgPaintEngine); + switch (d->svgVersion) { + case QSvgGenerator::SvgVersion::SvgTiny12: + // no clip handling in Tiny 1.2 + return; + case QSvgGenerator::SvgVersion::Svg11: + break; + } + + const QPaintEngine::DirtyFlags flags = state.state(); + + const bool clippingChanged = flags.testAnyFlags(DirtyClipPath | DirtyClipRegion); + if (clippingChanged) { + switch (state.clipOperation()) { + case Qt::NoClip: + d->clipEnabled = false; + d->clipPath.reset(); + break; + case Qt::ReplaceClip: + case Qt::IntersectClip: + d->clipPath = painter()->transform().map(painter()->clipPath()); + break; + } + } + + if (flags & DirtyClipEnabled) + d->clipEnabled = state.isClipEnabled(); + + if (d->isClippingEffective() && clippingChanged) { + d->stream->setString(&d->defs); + *d->stream << QLatin1String("\n").arg(d->generateClipPathName()); + drawPath(*d->clipPath); + *d->stream << "\n"; + d->stream->setString(&d->body); + } +} + void QSvgPaintEngine::drawEllipse(const QRectF &r) { Q_D(QSvgPaintEngine); -- cgit v1.2.1