summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-11-07 14:41:45 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-11-29 10:10:47 +0100
commit77ad7934057b056bdb0c15d15a01ef83190dafaa (patch)
treed7b6e5c609c3eaf52445a8c0e08e47bc9483a867 /src
parent5019037adf38f7b7fd299949af3f8f46c07e574d (diff)
downloadqtsvg-77ad7934057b056bdb0c15d15a01ef83190dafaa.tar.gz
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 <g> tag is emitted with the new state (brush, pen, transform, ...). Clipping is slightly more complicated because: 1) it needs its own element (<clipPath>), 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 <aacid@kde.org> Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/svg/qsvggenerator.cpp70
1 files changed, 70 insertions, 0 deletions
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 <optional>
+
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<QPainterPath> 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 << "</g>";
if (d->afterFirstUpdate)
*d->stream << "</g>" << 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 << "</g>\n";
if (d->afterFirstUpdate)
*d->stream << "</g>\n\n";
+ updateClipState(state);
+
+ if (d->isClippingEffective()) {
+ *d->stream << QStringLiteral("<g clip-path=\"url(#%1)\">").arg(d->currentClipPathName);
+ d->hasEmittedClipGroup = true;
+ } else {
+ d->hasEmittedClipGroup = false;
+ }
+
*d->stream << "<g ";
qbrushToSvg(state.brush());
@@ -1018,6 +1049,45 @@ void QSvgPaintEngine::updateState(const QPaintEngineState &state)
d->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("<clipPath id=\"%1\">\n").arg(d->generateClipPathName());
+ drawPath(*d->clipPath);
+ *d->stream << "</clipPath>\n";
+ d->stream->setString(&d->body);
+ }
+}
+
void QSvgPaintEngine::drawEllipse(const QRectF &r)
{
Q_D(QSvgPaintEngine);