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 <svgio/svgreader/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),
39 maClipPathUnits(userSpaceOnUse
)
43 SvgClipPathNode::~SvgClipPathNode()
45 if(mpaTransform
) delete mpaTransform
;
48 const SvgStyleAttributes
* SvgClipPathNode::getSvgStyleAttributes() const
50 return &maSvgStyleAttributes
;
53 void SvgClipPathNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
56 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
58 // read style attributes
59 maSvgStyleAttributes
.parseStyleAttribute(rTokenName
, aSVGToken
, aContent
, false);
66 readLocalCssStyle(aContent
);
69 case SVGTokenTransform
:
71 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
73 if(!aMatrix
.isIdentity())
75 setTransform(&aMatrix
);
79 case SVGTokenClipPathUnits
:
81 if(!aContent
.isEmpty())
83 if(aContent
.match(commonStrings::aStrUserSpaceOnUse
, 0))
85 setClipPathUnits(userSpaceOnUse
);
87 else if(aContent
.match(commonStrings::aStrObjectBoundingBox
, 0))
89 setClipPathUnits(objectBoundingBox
);
101 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence
& rTarget
, bool bReferenced
) const
103 drawinglayer::primitive2d::Primitive2DSequence aNewTarget
;
105 // decompose children
106 SvgNode::decomposeSvgNode(aNewTarget
, bReferenced
);
108 if(aNewTarget
.hasElements())
112 // create embedding group element with transformation
113 const drawinglayer::primitive2d::Primitive2DReference
xRef(
114 new drawinglayer::primitive2d::TransformPrimitive2D(
118 drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget
, xRef
);
122 // append to current target
123 drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget
, aNewTarget
);
128 void SvgClipPathNode::apply(
129 drawinglayer::primitive2d::Primitive2DSequence
& rContent
,
130 const basegfx::B2DHomMatrix
* pTransform
) const
132 if(rContent
.hasElements() && Display_none
!= getDisplay())
134 const drawinglayer::geometry::ViewInformation2D aViewInformation2D
;
135 drawinglayer::primitive2d::Primitive2DSequence aClipTarget
;
136 basegfx::B2DPolyPolygon aClipPolyPolygon
;
138 // get clipPath definition as primitives
139 decomposeSvgNode(aClipTarget
, true);
141 if(aClipTarget
.hasElements())
143 // extract filled polygons as base for a mask PolyPolygon
144 drawinglayer::processor2d::ContourExtractor2D
aExtractor(aViewInformation2D
, true);
146 aExtractor
.process(aClipTarget
);
148 const basegfx::B2DPolyPolygonVector
& rResult(aExtractor
.getExtractedContour());
149 const sal_uInt32
nSize(rResult
.size());
153 // merge to single clipPolyPolygon
154 aClipPolyPolygon
= basegfx::tools::mergeToSinglePolyPolygon(rResult
);
158 aClipPolyPolygon
= rResult
[0];
162 if(aClipPolyPolygon
.count())
164 if(objectBoundingBox
== getClipPathUnits())
166 // clip is object-relative, transform using content transformation
167 const basegfx::B2DRange
aContentRange(
168 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
170 aViewInformation2D
));
172 aClipPolyPolygon
.transform(
173 basegfx::tools::createScaleTranslateB2DHomMatrix(
174 aContentRange
.getRange(),
175 aContentRange
.getMinimum()));
177 else // userSpaceOnUse
182 aClipPolyPolygon
.transform(*pTransform
);
186 // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if
187 // possible; MaskPrimitive2D processing is potentially expensive
188 bool bCreateEmbedding(true);
189 bool bAddContent(true);
191 if(basegfx::tools::isRectangle(aClipPolyPolygon
))
193 // ClipRegion is a rectangle, thus it is not expensive to tell
194 // if the content is completely inside or outside of it; get ranges
195 const basegfx::B2DRange
aClipRange(aClipPolyPolygon
.getB2DRange());
196 const basegfx::B2DRange
aContentRange(
197 drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
199 aViewInformation2D
));
201 if(aClipRange
.isInside(aContentRange
))
203 // completely contained, no need to clip at all, so no need for embedding
204 bCreateEmbedding
= false;
206 else if(aClipRange
.overlaps(aContentRange
))
208 // overlap; embedding needed. ClipRegion can be minimized by using
209 // the intersection of the ClipRange and the ContentRange. Minimizing
210 // the ClipRegion potentially enhances further processing since
211 // usually clip operations are expensive.
212 basegfx::B2DRange
aCommonRange(aContentRange
);
214 aCommonRange
.intersect(aClipRange
);
215 aClipPolyPolygon
= basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aCommonRange
));
219 // not inside and no overlap -> completely outside
220 // no need for embedding, no need for content at all
221 bCreateEmbedding
= false;
227 // ClipRegion is not a simple rectangle, it would be possible but expensive to
228 // tell if the content needs clipping or not. It is also dependent of
229 // the content's decomposition. To do this, a processor would be needed that
230 // is capable if processing the given sequence of primitives and decide
231 // if all is inside or all is outside. Such a ClipProcessor could be written,
232 // but for now just create the embedding
237 // redefine target. Use MaskPrimitive2D with created clip
238 // geometry. Using the automatically set mbIsClipPathContent at
239 // SvgStyleAttributes the clip definition is without fill, stroke,
240 // and strokeWidth and forced to black
241 const drawinglayer::primitive2d::Primitive2DReference
xEmbedTransparence(
242 new drawinglayer::primitive2d::MaskPrimitive2D(
246 rContent
= drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence
, 1);
258 // An empty clipping path will completely clip away the element that had
259 // the clip-path property applied. (Svg spec)
265 } // end of namespace svgreader
266 } // end of namespace svgio
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */