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>
33 SvgClipPathNode::SvgClipPathNode(
34 SvgDocument
& rDocument
,
36 : SvgNode(SVGTokenClipPathNode
, rDocument
, pParent
),
37 maSvgStyleAttributes(*this),
38 maClipPathUnits(userSpaceOnUse
)
42 SvgClipPathNode::~SvgClipPathNode()
46 const SvgStyleAttributes
* SvgClipPathNode::getSvgStyleAttributes() const
48 return &maSvgStyleAttributes
;
51 void SvgClipPathNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
54 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
56 // read style attributes
57 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
, false);
64 readLocalCssStyle(aContent
);
67 case SVGTokenTransform
:
69 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
71 if(!aMatrix
.isIdentity())
73 setTransform(&aMatrix
);
77 case SVGTokenClipPathUnits
:
79 if(!aContent
.isEmpty())
81 if(aContent
.match(commonStrings::aStrUserSpaceOnUse
))
83 setClipPathUnits(userSpaceOnUse
);
85 else if(aContent
.match(commonStrings::aStrObjectBoundingBox
))
87 setClipPathUnits(objectBoundingBox
);
99 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool bReferenced
) const
101 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
103 // decompose children
104 SvgNode::decomposeSvgNode(aNewTarget
, bReferenced
);
106 if(!aNewTarget
.empty())
110 // create embedding group element with transformation
111 const drawinglayer::primitive2d::Primitive2DReference
xRef(
112 new drawinglayer::primitive2d::TransformPrimitive2D(
116 rTarget
.push_back(xRef
);
120 // append to current target
121 rTarget
.append(aNewTarget
);
126 void SvgClipPathNode::apply(
127 drawinglayer::primitive2d::Primitive2DContainer
& rContent
,
128 const basegfx::B2DHomMatrix
* pTransform
) const
130 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(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 const drawinglayer::primitive2d::Primitive2DReference
xEmbedTransparence(
236 new drawinglayer::primitive2d::MaskPrimitive2D(
240 rContent
= drawinglayer::primitive2d::Primitive2DContainer
{ xEmbedTransparence
};
252 // An empty clipping path will completely clip away the element that had
253 // the clip-path property applied. (Svg spec)
259 } // end of namespace svgreader
260 } // end of namespace svgio
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */