xref: /trunk/main/svgio/source/svgreader/svgclippathnode.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1ddde725dSArmin Le Grand /**************************************************************
2ddde725dSArmin Le Grand  *
3ddde725dSArmin Le Grand  * Licensed to the Apache Software Foundation (ASF) under one
4ddde725dSArmin Le Grand  * or more contributor license agreements.  See the NOTICE file
5ddde725dSArmin Le Grand  * distributed with this work for additional information
6ddde725dSArmin Le Grand  * regarding copyright ownership.  The ASF licenses this file
7ddde725dSArmin Le Grand  * to you under the Apache License, Version 2.0 (the
8ddde725dSArmin Le Grand  * "License"); you may not use this file except in compliance
9ddde725dSArmin Le Grand  * with the License.  You may obtain a copy of the License at
10ddde725dSArmin Le Grand  *
11ddde725dSArmin Le Grand  *   http://www.apache.org/licenses/LICENSE-2.0
12ddde725dSArmin Le Grand  *
13ddde725dSArmin Le Grand  * Unless required by applicable law or agreed to in writing,
14ddde725dSArmin Le Grand  * software distributed under the License is distributed on an
15ddde725dSArmin Le Grand  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ddde725dSArmin Le Grand  * KIND, either express or implied.  See the License for the
17ddde725dSArmin Le Grand  * specific language governing permissions and limitations
18ddde725dSArmin Le Grand  * under the License.
19ddde725dSArmin Le Grand  *
20ddde725dSArmin Le Grand  *************************************************************/
21ddde725dSArmin Le Grand 
22ddde725dSArmin Le Grand // MARKER(update_precomp.py): autogen include statement, do not remove
23ddde725dSArmin Le Grand #include "precompiled_svgio.hxx"
24ddde725dSArmin Le Grand 
25ddde725dSArmin Le Grand #include <svgio/svgreader/svgclippathnode.hxx>
26ddde725dSArmin Le Grand #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
27db3fbf26SArmin Le Grand #include <drawinglayer/primitive2d/maskprimitive2d.hxx>
28ddde725dSArmin Le Grand #include <basegfx/matrix/b2dhommatrixtools.hxx>
29ddde725dSArmin Le Grand #include <drawinglayer/geometry/viewinformation2d.hxx>
30db3fbf26SArmin Le Grand #include <drawinglayer/processor2d/contourextractor2d.hxx>
31db3fbf26SArmin Le Grand #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
32f73c0288SArmin Le Grand #include <basegfx/polygon/b2dpolygontools.hxx>
33ddde725dSArmin Le Grand 
34ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
35ddde725dSArmin Le Grand 
36ddde725dSArmin Le Grand namespace svgio
37ddde725dSArmin Le Grand {
38ddde725dSArmin Le Grand     namespace svgreader
39ddde725dSArmin Le Grand     {
SvgClipPathNode(SvgDocument & rDocument,SvgNode * pParent)40ddde725dSArmin Le Grand         SvgClipPathNode::SvgClipPathNode(
41ddde725dSArmin Le Grand             SvgDocument& rDocument,
42ddde725dSArmin Le Grand             SvgNode* pParent)
43ddde725dSArmin Le Grand         :   SvgNode(SVGTokenClipPathNode, rDocument, pParent),
44ddde725dSArmin Le Grand             maSvgStyleAttributes(*this),
45ddde725dSArmin Le Grand             mpaTransform(0),
46ddde725dSArmin Le Grand             maClipPathUnits(userSpaceOnUse)
47ddde725dSArmin Le Grand         {
48ddde725dSArmin Le Grand         }
49ddde725dSArmin Le Grand 
~SvgClipPathNode()50ddde725dSArmin Le Grand         SvgClipPathNode::~SvgClipPathNode()
51ddde725dSArmin Le Grand         {
52ddde725dSArmin Le Grand             if(mpaTransform) delete mpaTransform;
53ddde725dSArmin Le Grand         }
54ddde725dSArmin Le Grand 
getSvgStyleAttributes() const55ddde725dSArmin Le Grand         const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
56ddde725dSArmin Le Grand         {
57ddde725dSArmin Le Grand             return &maSvgStyleAttributes;
58ddde725dSArmin Le Grand         }
59ddde725dSArmin Le Grand 
parseAttribute(const rtl::OUString & rTokenName,SVGToken aSVGToken,const rtl::OUString & aContent)60ddde725dSArmin Le Grand         void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
61ddde725dSArmin Le Grand         {
62ddde725dSArmin Le Grand             // call parent
63ddde725dSArmin Le Grand             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
64ddde725dSArmin Le Grand 
65ddde725dSArmin Le Grand             // read style attributes
66*52cb04b8SArmin Le Grand             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent, false);
67ddde725dSArmin Le Grand 
68ddde725dSArmin Le Grand             // parse own
69ddde725dSArmin Le Grand             switch(aSVGToken)
70ddde725dSArmin Le Grand             {
71ddde725dSArmin Le Grand                 case SVGTokenStyle:
72ddde725dSArmin Le Grand                 {
739d01bcdeSArmin Le Grand                     readLocalCssStyle(aContent);
74ddde725dSArmin Le Grand                     break;
75ddde725dSArmin Le Grand                 }
76ddde725dSArmin Le Grand                 case SVGTokenTransform:
77ddde725dSArmin Le Grand                 {
78ddde725dSArmin Le Grand                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
79ddde725dSArmin Le Grand 
80ddde725dSArmin Le Grand                     if(!aMatrix.isIdentity())
81ddde725dSArmin Le Grand                     {
82ddde725dSArmin Le Grand                         setTransform(&aMatrix);
83ddde725dSArmin Le Grand                     }
84ddde725dSArmin Le Grand                     break;
85ddde725dSArmin Le Grand                 }
86ddde725dSArmin Le Grand                 case SVGTokenClipPathUnits:
87ddde725dSArmin Le Grand                 {
88ddde725dSArmin Le Grand                     if(aContent.getLength())
89ddde725dSArmin Le Grand                     {
90ddde725dSArmin Le Grand                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
91ddde725dSArmin Le Grand                         {
92ddde725dSArmin Le Grand                             setClipPathUnits(userSpaceOnUse);
93ddde725dSArmin Le Grand                         }
94ddde725dSArmin Le Grand                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
95ddde725dSArmin Le Grand                         {
96ddde725dSArmin Le Grand                             setClipPathUnits(objectBoundingBox);
97ddde725dSArmin Le Grand                         }
98ddde725dSArmin Le Grand                     }
99ddde725dSArmin Le Grand                     break;
100ddde725dSArmin Le Grand                 }
101e2bf1e9dSArmin Le Grand                 default:
102e2bf1e9dSArmin Le Grand                 {
103e2bf1e9dSArmin Le Grand                     break;
104e2bf1e9dSArmin Le Grand                 }
105ddde725dSArmin Le Grand             }
106ddde725dSArmin Le Grand         }
107ddde725dSArmin Le Grand 
decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence & rTarget,bool bReferenced) const108ddde725dSArmin Le Grand         void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
109ddde725dSArmin Le Grand         {
110ddde725dSArmin Le Grand             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
111ddde725dSArmin Le Grand 
112ddde725dSArmin Le Grand             // decompose childs
113ddde725dSArmin Le Grand             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
114ddde725dSArmin Le Grand 
115ddde725dSArmin Le Grand             if(aNewTarget.hasElements())
116ddde725dSArmin Le Grand             {
117ddde725dSArmin Le Grand                 if(getTransform())
118ddde725dSArmin Le Grand                 {
119ddde725dSArmin Le Grand                     // create embedding group element with transformation
120ddde725dSArmin Le Grand                     const drawinglayer::primitive2d::Primitive2DReference xRef(
121ddde725dSArmin Le Grand                         new drawinglayer::primitive2d::TransformPrimitive2D(
122ddde725dSArmin Le Grand                             *getTransform(),
123ddde725dSArmin Le Grand                             aNewTarget));
124ddde725dSArmin Le Grand 
125ddde725dSArmin Le Grand                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
126ddde725dSArmin Le Grand                 }
127ddde725dSArmin Le Grand                 else
128ddde725dSArmin Le Grand                 {
129ddde725dSArmin Le Grand                     // append to current target
130ddde725dSArmin Le Grand                     drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
131ddde725dSArmin Le Grand                 }
132ddde725dSArmin Le Grand             }
133ddde725dSArmin Le Grand         }
134ddde725dSArmin Le Grand 
apply(drawinglayer::primitive2d::Primitive2DSequence & rContent,const basegfx::B2DHomMatrix * pTransform) const135e90e3a55SArmin Le Grand         void SvgClipPathNode::apply(
136e90e3a55SArmin Le Grand             drawinglayer::primitive2d::Primitive2DSequence& rContent,
137e90e3a55SArmin Le Grand             const basegfx::B2DHomMatrix* pTransform) const
138ddde725dSArmin Le Grand         {
139a275c134SArmin Le Grand             if(rContent.hasElements() && Display_none != getDisplay())
140ddde725dSArmin Le Grand             {
141db3fbf26SArmin Le Grand                 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
142ddde725dSArmin Le Grand                 drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
143db3fbf26SArmin Le Grand                 basegfx::B2DPolyPolygon aClipPolyPolygon;
144ddde725dSArmin Le Grand 
145ddde725dSArmin Le Grand                 // get clipPath definition as primitives
146ddde725dSArmin Le Grand                 decomposeSvgNode(aClipTarget, true);
147ddde725dSArmin Le Grand 
148ddde725dSArmin Le Grand                 if(aClipTarget.hasElements())
149ddde725dSArmin Le Grand                 {
150db3fbf26SArmin Le Grand                     // extract filled plygons as base for a mask PolyPolygon
151db3fbf26SArmin Le Grand                     drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
152db3fbf26SArmin Le Grand 
153db3fbf26SArmin Le Grand                     aExtractor.process(aClipTarget);
154db3fbf26SArmin Le Grand 
155db3fbf26SArmin Le Grand                     const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
156db3fbf26SArmin Le Grand                     const sal_uInt32 nSize(rResult.size());
157db3fbf26SArmin Le Grand 
158db3fbf26SArmin Le Grand                     if(nSize > 1)
159db3fbf26SArmin Le Grand                     {
160db3fbf26SArmin Le Grand                         // merge to single clipPolyPolygon
161db3fbf26SArmin Le Grand                         aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult);
162db3fbf26SArmin Le Grand                     }
163db3fbf26SArmin Le Grand                     else
164db3fbf26SArmin Le Grand                     {
165db3fbf26SArmin Le Grand                         aClipPolyPolygon = rResult[0];
166db3fbf26SArmin Le Grand                     }
167db3fbf26SArmin Le Grand                 }
168db3fbf26SArmin Le Grand 
169db3fbf26SArmin Le Grand                 if(aClipPolyPolygon.count())
170db3fbf26SArmin Le Grand                 {
171ddde725dSArmin Le Grand                     if(objectBoundingBox == getClipPathUnits())
172ddde725dSArmin Le Grand                     {
173db3fbf26SArmin Le Grand                         // clip is object-relative, transform using content transformation
174ddde725dSArmin Le Grand                         const basegfx::B2DRange aContentRange(
175ddde725dSArmin Le Grand                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
176ddde725dSArmin Le Grand                                 rContent,
177db3fbf26SArmin Le Grand                                 aViewInformation2D));
178ddde725dSArmin Le Grand 
179db3fbf26SArmin Le Grand                         aClipPolyPolygon.transform(
180ddde725dSArmin Le Grand                             basegfx::tools::createScaleTranslateB2DHomMatrix(
181ddde725dSArmin Le Grand                                 aContentRange.getRange(),
182db3fbf26SArmin Le Grand                                 aContentRange.getMinimum()));
183ddde725dSArmin Le Grand                     }
184e90e3a55SArmin Le Grand                     else // userSpaceOnUse
185e90e3a55SArmin Le Grand                     {
186e90e3a55SArmin Le Grand                         // #i124852#
187e90e3a55SArmin Le Grand                         if(pTransform)
188e90e3a55SArmin Le Grand                         {
189e90e3a55SArmin Le Grand                             aClipPolyPolygon.transform(*pTransform);
190e90e3a55SArmin Le Grand                         }
191e90e3a55SArmin Le Grand                     }
192ddde725dSArmin Le Grand 
193f73c0288SArmin Le Grand                     // #124313# try to avoid creating an embedding to a MaskPrimitive2D if
194f73c0288SArmin Le Grand                     // possible; MaskPrimitive2D processing is potentially expensive
195f73c0288SArmin Le Grand                     bool bCreateEmbedding(true);
196f73c0288SArmin Le Grand                     bool bAddContent(true);
197f73c0288SArmin Le Grand 
198f73c0288SArmin Le Grand                     if(basegfx::tools::isRectangle(aClipPolyPolygon))
199f73c0288SArmin Le Grand                     {
200f73c0288SArmin Le Grand                         // ClipRegion is a rectangle, thus it is not expensive to tell
201f73c0288SArmin Le Grand                         // if the content is completely inside or outside of it; get ranges
202f73c0288SArmin Le Grand                         const basegfx::B2DRange aClipRange(aClipPolyPolygon.getB2DRange());
203f73c0288SArmin Le Grand                         const basegfx::B2DRange aContentRange(
204f73c0288SArmin Le Grand                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
205f73c0288SArmin Le Grand                                 rContent,
206f73c0288SArmin Le Grand                                 aViewInformation2D));
207f73c0288SArmin Le Grand 
208f73c0288SArmin Le Grand                         if(aClipRange.isInside(aContentRange))
209f73c0288SArmin Le Grand                         {
210f73c0288SArmin Le Grand                             // completely contained, no need to clip at all, so no need for embedding
211f73c0288SArmin Le Grand                             bCreateEmbedding = false;
212f73c0288SArmin Le Grand                         }
213f73c0288SArmin Le Grand                         else if(aClipRange.overlaps(aContentRange))
214f73c0288SArmin Le Grand                         {
215f73c0288SArmin Le Grand                             // overlap; embedding needed. ClipRegion can be minimized by using
216f73c0288SArmin Le Grand                             // the intersection of the ClipRange and the ContentRange. Minimizing
217f73c0288SArmin Le Grand                             // the ClipRegion potentially enhances further processing since
218f73c0288SArmin Le Grand                             // usually clip operations are expensive.
219f73c0288SArmin Le Grand                             basegfx::B2DRange aCommonRange(aContentRange);
220f73c0288SArmin Le Grand 
221f73c0288SArmin Le Grand                             aCommonRange.intersect(aClipRange);
222f73c0288SArmin Le Grand                             aClipPolyPolygon = basegfx::B2DPolyPolygon(basegfx::tools::createPolygonFromRect(aCommonRange));
223f73c0288SArmin Le Grand                         }
224f73c0288SArmin Le Grand                         else
225f73c0288SArmin Le Grand                         {
226f73c0288SArmin Le Grand                             // not inside and no overlap -> completely outside
227f73c0288SArmin Le Grand                             // no need for embedding, no need for content at all
228f73c0288SArmin Le Grand                             bCreateEmbedding = false;
229f73c0288SArmin Le Grand                             bAddContent = false;
230f73c0288SArmin Le Grand                         }
231f73c0288SArmin Le Grand                     }
232f73c0288SArmin Le Grand                     else
233f73c0288SArmin Le Grand                     {
234f73c0288SArmin Le Grand                         // ClipRegion is not a simple rectangle, it would be possible but expensive to
235f73c0288SArmin Le Grand                         // tell if the content needs clipping or not. It is also dependent of
236f73c0288SArmin Le Grand                         // the content's decomposition. To do this, a processor would be needed that
237f73c0288SArmin Le Grand                         // is capable if processing the given sequence of primitives and decide
238f73c0288SArmin Le Grand                         // if all is inside or all is outside. Such a ClipProcessor could be written,
239f73c0288SArmin Le Grand                         // but for now just create the embedding
240f73c0288SArmin Le Grand                     }
241f73c0288SArmin Le Grand 
242f73c0288SArmin Le Grand                     if(bCreateEmbedding)
243f73c0288SArmin Le Grand                     {
244db3fbf26SArmin Le Grand                         // redefine target. Use MaskPrimitive2D with created clip
245ddde725dSArmin Le Grand                         // geometry. Using the automatically set mbIsClipPathContent at
246ddde725dSArmin Le Grand                         // SvgStyleAttributes the clip definition is without fill, stroke,
247db3fbf26SArmin Le Grand                         // and strokeWidth and forced to black
248ddde725dSArmin Le Grand                         const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
249db3fbf26SArmin Le Grand                             new drawinglayer::primitive2d::MaskPrimitive2D(
250db3fbf26SArmin Le Grand                                 aClipPolyPolygon,
251db3fbf26SArmin Le Grand                                 rContent));
252ddde725dSArmin Le Grand 
253ddde725dSArmin Le Grand                         rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
254ddde725dSArmin Le Grand                     }
255ddde725dSArmin Le Grand                     else
256ddde725dSArmin Le Grand                     {
257f73c0288SArmin Le Grand                         if(!bAddContent)
258f73c0288SArmin Le Grand                         {
259f73c0288SArmin Le Grand                             rContent.realloc(0);
260f73c0288SArmin Le Grand                         }
261f73c0288SArmin Le Grand                     }
262f73c0288SArmin Le Grand                 }
263f73c0288SArmin Le Grand                 else
264f73c0288SArmin Le Grand                 {
265ddde725dSArmin Le Grand                     // An empty clipping path will completely clip away the element that had
266ddde725dSArmin Le Grand                     // the �clip-path� property applied. (Svg spec)
267ddde725dSArmin Le Grand                     rContent.realloc(0);
268ddde725dSArmin Le Grand                 }
269ddde725dSArmin Le Grand             }
270ddde725dSArmin Le Grand         }
271ddde725dSArmin Le Grand 
272ddde725dSArmin Le Grand     } // end of namespace svgreader
273ddde725dSArmin Le Grand } // end of namespace svgio
274ddde725dSArmin Le Grand 
275ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
276ddde725dSArmin Le Grand // eof
277