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>
29 namespace svgio::svgreader
31 SvgClipPathNode::SvgClipPathNode(
32 SvgDocument
& rDocument
,
34 : SvgNode(SVGTokenClipPathNode
, rDocument
, pParent
),
35 maSvgStyleAttributes(*this),
36 maClipPathUnits(userSpaceOnUse
)
40 SvgClipPathNode::~SvgClipPathNode()
44 const SvgStyleAttributes
* SvgClipPathNode::getSvgStyleAttributes() const
46 return &maSvgStyleAttributes
;
49 void SvgClipPathNode::parseAttribute(const OUString
& rTokenName
, SVGToken aSVGToken
, const OUString
& aContent
)
52 SvgNode::parseAttribute(rTokenName
, aSVGToken
, aContent
);
54 // read style attributes
55 maSvgStyleAttributes
.parseStyleAttribute(aSVGToken
, aContent
, false);
62 readLocalCssStyle(aContent
);
65 case SVGTokenTransform
:
67 const basegfx::B2DHomMatrix
aMatrix(readTransform(aContent
, *this));
69 if(!aMatrix
.isIdentity())
71 setTransform(&aMatrix
);
75 case SVGTokenClipPathUnits
:
77 if(!aContent
.isEmpty())
79 if(aContent
.match(commonStrings::aStrUserSpaceOnUse
))
81 setClipPathUnits(userSpaceOnUse
);
83 else if(aContent
.match(commonStrings::aStrObjectBoundingBox
))
85 setClipPathUnits(objectBoundingBox
);
97 void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer
& rTarget
, bool bReferenced
) const
99 drawinglayer::primitive2d::Primitive2DContainer aNewTarget
;
101 // decompose children
102 SvgNode::decomposeSvgNode(aNewTarget
, bReferenced
);
104 if(aNewTarget
.empty())
109 // create embedding group element with transformation
110 const drawinglayer::primitive2d::Primitive2DReference
xRef(
111 new drawinglayer::primitive2d::TransformPrimitive2D(
115 rTarget
.push_back(xRef
);
119 // append to current target
120 rTarget
.append(aNewTarget
);
124 void SvgClipPathNode::apply(
125 drawinglayer::primitive2d::Primitive2DContainer
& rContent
,
126 const basegfx::B2DHomMatrix
* pTransform
) const
128 if(rContent
.empty() || Display_none
== getDisplay())
131 const drawinglayer::geometry::ViewInformation2D aViewInformation2D
;
132 drawinglayer::primitive2d::Primitive2DContainer aClipTarget
;
133 basegfx::B2DPolyPolygon aClipPolyPolygon
;
135 // get clipPath definition as primitives
136 decomposeSvgNode(aClipTarget
, true);
138 if(!aClipTarget
.empty())
140 // extract filled polygons as base for a mask PolyPolygon
141 drawinglayer::processor2d::ContourExtractor2D
aExtractor(aViewInformation2D
, true);
143 aExtractor
.process(aClipTarget
);
145 const basegfx::B2DPolyPolygonVector
& rResult(aExtractor
.getExtractedContour());
146 const sal_uInt32
nSize(rResult
.size());
150 // merge to single clipPolyPolygon
151 aClipPolyPolygon
= basegfx::utils::mergeToSinglePolyPolygon(rResult
);
155 aClipPolyPolygon
= rResult
[0];
159 if(aClipPolyPolygon
.count())
161 if(objectBoundingBox
== getClipPathUnits())
163 // clip is object-relative, transform using content transformation
164 const basegfx::B2DRange
aContentRange(rContent
.getB2DRange(aViewInformation2D
));
166 aClipPolyPolygon
.transform(
167 basegfx::utils::createScaleTranslateB2DHomMatrix(
168 aContentRange
.getRange(),
169 aContentRange
.getMinimum()));
171 else // userSpaceOnUse
176 aClipPolyPolygon
.transform(*pTransform
);
180 // #i124313# try to avoid creating an embedding to a MaskPrimitive2D if
181 // possible; MaskPrimitive2D processing is potentially expensive
182 bool bCreateEmbedding(true);
183 bool bAddContent(true);
185 if(basegfx::utils::isRectangle(aClipPolyPolygon
))
187 // ClipRegion is a rectangle, thus it is not expensive to tell
188 // if the content is completely inside or outside of it; get ranges
189 const basegfx::B2DRange
aClipRange(aClipPolyPolygon
.getB2DRange());
190 const basegfx::B2DRange
aContentRange(
191 rContent
.getB2DRange(
192 aViewInformation2D
));
194 if(aClipRange
.isInside(aContentRange
))
196 // completely contained, no need to clip at all, so no need for embedding
197 bCreateEmbedding
= false;
199 else if(aClipRange
.overlaps(aContentRange
))
201 // overlap; embedding needed. ClipRegion can be minimized by using
202 // the intersection of the ClipRange and the ContentRange. Minimizing
203 // the ClipRegion potentially enhances further processing since
204 // usually clip operations are expensive.
205 basegfx::B2DRange
aCommonRange(aContentRange
);
207 aCommonRange
.intersect(aClipRange
);
208 aClipPolyPolygon
= basegfx::B2DPolyPolygon(basegfx::utils::createPolygonFromRect(aCommonRange
));
212 // not inside and no overlap -> completely outside
213 // no need for embedding, no need for content at all
214 bCreateEmbedding
= false;
220 // ClipRegion is not a simple rectangle, it would be possible but expensive to
221 // tell if the content needs clipping or not. It is also dependent of
222 // the content's decomposition. To do this, a processor would be needed that
223 // is capable if processing the given sequence of primitives and decide
224 // if all is inside or all is outside. Such a ClipProcessor could be written,
225 // but for now just create the embedding
230 // redefine target. Use MaskPrimitive2D with created clip
231 // geometry. Using the automatically set mbIsClipPathContent at
232 // SvgStyleAttributes the clip definition is without fill, stroke,
233 // and strokeWidth and forced to black
234 const drawinglayer::primitive2d::Primitive2DReference
xEmbedTransparence(
235 new drawinglayer::primitive2d::MaskPrimitive2D(
239 rContent
= drawinglayer::primitive2d::Primitive2DContainer
{ xEmbedTransparence
};
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: */