1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svgclippathnode.hxx>
21 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
22 #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
23 #include <basegfx/matrix/b2dhommatrixtools.hxx>
24 #include <drawinglayer/geometry/viewinformation2d.hxx>
25 #include <drawinglayer/processor2d/contourextractor2d.hxx>
26 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 #include <o3tl/string_view.hxx>
30 namespace svgio::svgreader
32 SvgClipPathNode::SvgClipPathNode(
33 SvgDocument
& rDocument
,
35 : SvgNode(SVGToken::ClipPathNode
, rDocument
, pParent
),
36 maSvgStyleAttributes(*this),
37 maClipPathUnits(SvgUnits::userSpaceOnUse
)
41 SvgClipPathNode::~SvgClipPathNode()
45 const SvgStyleAttributes
* SvgClipPathNode::getSvgStyleAttributes() const
47 return &maSvgStyleAttributes
;
50 void SvgClipPathNode::parseAttribute(SVGToken aSVGToken
, const OUString
& aContent
)
53 SvgNode::parseAttribute(aSVGToken
, aContent
);
55 // read style attributes
56 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
);
63 readLocalCssStyle(aContent
);
66 case SVGToken::Transform
:
68 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
70 if(!aMatrix
.isIdentity())
72 setTransform(aMatrix
);
76 case SVGToken::ClipPathUnits
:
78 if(!aContent
.isEmpty())
80 if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent
), commonStrings::aStrUserSpaceOnUse
))
82 setClipPathUnits(SvgUnits::userSpaceOnUse
);
84 else if(o3tl::equalsIgnoreAsciiCase(o3tl::trim(aContent
), commonStrings::aStrObjectBoundingBox
))
86 setClipPathUnits(SvgUnits::objectBoundingBox
);
98 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool bReferenced
) const
100 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
102 // decompose children
103 SvgNode::decomposeSvgNode(aNewTarget
, bReferenced
);
105 if(aNewTarget
.empty())
110 // create embedding group element with transformation
111 const drawinglayer::primitive2d::Primitive2DReference
xRef(
112 new drawinglayer::primitive2d::TransformPrimitive2D(
114 std::move(aNewTarget
)));
116 rTarget
.push_back(xRef
);
120 // append to current target
121 rTarget
.append(std::move(aNewTarget
));
125 void SvgClipPathNode::apply(
126 drawinglayer::primitive2d::Primitive2DContainer
& rContent
,
127 const std::optional
<basegfx::B2DHomMatrix
>& pTransform
) const
129 if (rContent
.empty() || Display::None
== getDisplay())
132 const drawinglayer::geometry::ViewInformation2D aViewInformation2D
;
133 drawinglayer::primitive2d::Primitive2DContainer aClipTarget
;
134 basegfx::B2DPolyPolygon aClipPolyPolygon
;
136 // get clipPath definition as primitives
137 decomposeSvgNode(aClipTarget
, true);
139 if(!aClipTarget
.empty())
141 // extract filled polygons as base for a mask PolyPolygon
142 drawinglayer::processor2d::ContourExtractor2D
aExtractor(aViewInformation2D
, true);
144 aExtractor
.process(aClipTarget
);
146 const basegfx::B2DPolyPolygonVector
& rResult(aExtractor
.getExtractedContour());
147 const sal_uInt32
nSize(rResult
.size());
151 // merge to single clipPolyPolygon
152 aClipPolyPolygon
= basegfx::utils::mergeToSinglePolyPolygon(rResult
);
156 aClipPolyPolygon
= rResult
[0];
160 if(aClipPolyPolygon
.count())
162 if (SvgUnits::objectBoundingBox
== getClipPathUnits())
164 // clip is object-relative, transform using content transformation
165 const basegfx::B2DRange
aContentRange(rContent
.getB2DRange(aViewInformation2D
));
167 aClipPolyPolygon
.transform(
168 basegfx::utils::createScaleTranslateB2DHomMatrix(
169 aContentRange
.getRange(),
170 aContentRange
.getMinimum()));
172 else // userSpaceOnUse
177 aClipPolyPolygon
.transform(*pTransform
);
181 // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if
182 // possible; MaskPrimitive2D processing is potentially expensive
183 bool bCreateEmbedding(true);
184 bool bAddContent(true);
186 if(basegfx::utils::isRectangle(aClipPolyPolygon
))
188 // ClipRegion is a rectangle, thus it is not expensive to tell
189 // if the content is completely inside or outside of it; get ranges
190 const basegfx::B2DRange
aClipRange(aClipPolyPolygon
.getB2DRange());
191 const basegfx::B2DRange
aContentRange(
192 rContent
.getB2DRange(
193 aViewInformation2D
));
195 if(aClipRange
.isInside(aContentRange
))
197 // completely contained, no need to clip at all, so no need for embedding
198 bCreateEmbedding
= false;
200 else if(aClipRange
.overlaps(aContentRange
))
202 // overlap; embedding needed. ClipRegion can be minimized by using
203 // the intersection of the ClipRange and the ContentRange. Minimizing
204 // the ClipRegion potentially enhances further processing since
205 // usually clip operations are expensive.
206 basegfx::B2DRange
aCommonRange(aContentRange
);
208 aCommonRange
.intersect(aClipRange
);
209 aClipPolyPolygon
= basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aCommonRange
));
213 // not inside and no overlap -> completely outside
214 // no need for embedding, no need for content at all
215 bCreateEmbedding
= false;
221 // ClipRegion is not a simple rectangle, it would be possible but expensive to
222 // tell if the content needs clipping or not. It is also dependent of
223 // the content's decomposition. To do this, a processor would be needed that
224 // is capable if processing the given sequence of primitives and decide
225 // if all is inside or all is outside. Such a ClipProcessor could be written,
226 // but for now just create the embedding
231 // redefine target. Use MaskPrimitive2D with created clip
232 // geometry. Using the automatically set mbIsClipPathContent at
233 // SvgStyleAttributes the clip definition is without fill, stroke,
234 // and strokeWidth and forced to black
235 rContent
= drawinglayer::primitive2d::Primitive2DContainer
{
236 new drawinglayer::primitive2d::MaskPrimitive2D(
237 std::move(aClipPolyPolygon
),
251 // An empty clipping path will completely clip away the element that had
252 // the clip-path property applied. (Svg spec)
257 } // end of namespace svgio::svgreader
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */