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>
32ddde725dSArmin Le Grand 
33ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
34ddde725dSArmin Le Grand 
35ddde725dSArmin Le Grand namespace svgio
36ddde725dSArmin Le Grand {
37ddde725dSArmin Le Grand     namespace svgreader
38ddde725dSArmin Le Grand     {
39ddde725dSArmin Le Grand         SvgClipPathNode::SvgClipPathNode(
40ddde725dSArmin Le Grand             SvgDocument& rDocument,
41ddde725dSArmin Le Grand             SvgNode* pParent)
42ddde725dSArmin Le Grand         :   SvgNode(SVGTokenClipPathNode, rDocument, pParent),
43ddde725dSArmin Le Grand             maSvgStyleAttributes(*this),
44ddde725dSArmin Le Grand             mpaTransform(0),
45ddde725dSArmin Le Grand             maClipPathUnits(userSpaceOnUse)
46ddde725dSArmin Le Grand         {
47ddde725dSArmin Le Grand         }
48ddde725dSArmin Le Grand 
49ddde725dSArmin Le Grand         SvgClipPathNode::~SvgClipPathNode()
50ddde725dSArmin Le Grand         {
51ddde725dSArmin Le Grand             if(mpaTransform) delete mpaTransform;
52ddde725dSArmin Le Grand         }
53ddde725dSArmin Le Grand 
54ddde725dSArmin Le Grand         const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
55ddde725dSArmin Le Grand         {
56ddde725dSArmin Le Grand             return &maSvgStyleAttributes;
57ddde725dSArmin Le Grand         }
58ddde725dSArmin Le Grand 
59ddde725dSArmin Le Grand         void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
60ddde725dSArmin Le Grand         {
61ddde725dSArmin Le Grand             // call parent
62ddde725dSArmin Le Grand             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
63ddde725dSArmin Le Grand 
64ddde725dSArmin Le Grand             // read style attributes
65ddde725dSArmin Le Grand             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
66ddde725dSArmin Le Grand 
67ddde725dSArmin Le Grand             // parse own
68ddde725dSArmin Le Grand             switch(aSVGToken)
69ddde725dSArmin Le Grand             {
70ddde725dSArmin Le Grand                 case SVGTokenStyle:
71ddde725dSArmin Le Grand                 {
72ddde725dSArmin Le Grand                     maSvgStyleAttributes.readStyle(aContent);
73ddde725dSArmin Le Grand                     break;
74ddde725dSArmin Le Grand                 }
75ddde725dSArmin Le Grand                 case SVGTokenTransform:
76ddde725dSArmin Le Grand                 {
77ddde725dSArmin Le Grand                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
78ddde725dSArmin Le Grand 
79ddde725dSArmin Le Grand                     if(!aMatrix.isIdentity())
80ddde725dSArmin Le Grand                     {
81ddde725dSArmin Le Grand                         setTransform(&aMatrix);
82ddde725dSArmin Le Grand                     }
83ddde725dSArmin Le Grand                     break;
84ddde725dSArmin Le Grand                 }
85ddde725dSArmin Le Grand                 case SVGTokenClipPathUnits:
86ddde725dSArmin Le Grand                 {
87ddde725dSArmin Le Grand                     if(aContent.getLength())
88ddde725dSArmin Le Grand                     {
89ddde725dSArmin Le Grand                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
90ddde725dSArmin Le Grand                         {
91ddde725dSArmin Le Grand                             setClipPathUnits(userSpaceOnUse);
92ddde725dSArmin Le Grand                         }
93ddde725dSArmin Le Grand                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
94ddde725dSArmin Le Grand                         {
95ddde725dSArmin Le Grand                             setClipPathUnits(objectBoundingBox);
96ddde725dSArmin Le Grand                         }
97ddde725dSArmin Le Grand                     }
98ddde725dSArmin Le Grand                     break;
99ddde725dSArmin Le Grand                 }
100e2bf1e9dSArmin Le Grand                 default:
101e2bf1e9dSArmin Le Grand                 {
102e2bf1e9dSArmin Le Grand                     break;
103e2bf1e9dSArmin Le Grand                 }
104ddde725dSArmin Le Grand             }
105ddde725dSArmin Le Grand         }
106ddde725dSArmin Le Grand 
107ddde725dSArmin Le Grand         void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
108ddde725dSArmin Le Grand         {
109ddde725dSArmin Le Grand             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
110ddde725dSArmin Le Grand 
111ddde725dSArmin Le Grand             // decompose childs
112ddde725dSArmin Le Grand             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
113ddde725dSArmin Le Grand 
114ddde725dSArmin Le Grand             if(aNewTarget.hasElements())
115ddde725dSArmin Le Grand             {
116ddde725dSArmin Le Grand                 if(getTransform())
117ddde725dSArmin Le Grand                 {
118ddde725dSArmin Le Grand                     // create embedding group element with transformation
119ddde725dSArmin Le Grand                     const drawinglayer::primitive2d::Primitive2DReference xRef(
120ddde725dSArmin Le Grand                         new drawinglayer::primitive2d::TransformPrimitive2D(
121ddde725dSArmin Le Grand                             *getTransform(),
122ddde725dSArmin Le Grand                             aNewTarget));
123ddde725dSArmin Le Grand 
124ddde725dSArmin Le Grand                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
125ddde725dSArmin Le Grand                 }
126ddde725dSArmin Le Grand                 else
127ddde725dSArmin Le Grand                 {
128ddde725dSArmin Le Grand                     // append to current target
129ddde725dSArmin Le Grand                     drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
130ddde725dSArmin Le Grand                 }
131ddde725dSArmin Le Grand             }
132ddde725dSArmin Le Grand         }
133ddde725dSArmin Le Grand 
134ddde725dSArmin Le Grand         void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
135ddde725dSArmin Le Grand         {
136*a275c134SArmin Le Grand             if(rContent.hasElements() && Display_none != getDisplay())
137ddde725dSArmin Le Grand             {
138db3fbf26SArmin Le Grand                 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
139ddde725dSArmin Le Grand                 drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
140db3fbf26SArmin Le Grand                 basegfx::B2DPolyPolygon aClipPolyPolygon;
141ddde725dSArmin Le Grand 
142ddde725dSArmin Le Grand                 // get clipPath definition as primitives
143ddde725dSArmin Le Grand                 decomposeSvgNode(aClipTarget, true);
144ddde725dSArmin Le Grand 
145ddde725dSArmin Le Grand                 if(aClipTarget.hasElements())
146db3fbf26SArmin Le Grand                 {
147db3fbf26SArmin Le Grand                     // extract filled plygons as base for a mask PolyPolygon
148db3fbf26SArmin Le Grand                     drawinglayer::processor2d::ContourExtractor2D aExtractor(aViewInformation2D, true);
149db3fbf26SArmin Le Grand 
150db3fbf26SArmin Le Grand                     aExtractor.process(aClipTarget);
151db3fbf26SArmin Le Grand 
152db3fbf26SArmin Le Grand                     const basegfx::B2DPolyPolygonVector& rResult(aExtractor.getExtractedContour());
153db3fbf26SArmin Le Grand                     const sal_uInt32 nSize(rResult.size());
154db3fbf26SArmin Le Grand 
155db3fbf26SArmin Le Grand                     if(nSize > 1)
156db3fbf26SArmin Le Grand                     {
157db3fbf26SArmin Le Grand                         // merge to single clipPolyPolygon
158db3fbf26SArmin Le Grand                         aClipPolyPolygon = basegfx::tools::mergeToSinglePolyPolygon(rResult);
159db3fbf26SArmin Le Grand                     }
160db3fbf26SArmin Le Grand                     else
161db3fbf26SArmin Le Grand                     {
162db3fbf26SArmin Le Grand                         aClipPolyPolygon = rResult[0];
163db3fbf26SArmin Le Grand                     }
164db3fbf26SArmin Le Grand                 }
165db3fbf26SArmin Le Grand 
166db3fbf26SArmin Le Grand                 if(aClipPolyPolygon.count())
167ddde725dSArmin Le Grand                 {
168ddde725dSArmin Le Grand                     if(objectBoundingBox == getClipPathUnits())
169ddde725dSArmin Le Grand                     {
170db3fbf26SArmin Le Grand                         // clip is object-relative, transform using content transformation
171ddde725dSArmin Le Grand                         const basegfx::B2DRange aContentRange(
172ddde725dSArmin Le Grand                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
173ddde725dSArmin Le Grand                                 rContent,
174db3fbf26SArmin Le Grand                                 aViewInformation2D));
175ddde725dSArmin Le Grand 
176db3fbf26SArmin Le Grand                         aClipPolyPolygon.transform(
177db3fbf26SArmin Le Grand                             basegfx::tools::createScaleTranslateB2DHomMatrix(
178db3fbf26SArmin Le Grand                                 aContentRange.getRange(),
179db3fbf26SArmin Le Grand                                 aContentRange.getMinimum()));
180ddde725dSArmin Le Grand                     }
181ddde725dSArmin Le Grand 
182db3fbf26SArmin Le Grand                     // redefine target. Use MaskPrimitive2D with created clip
183ddde725dSArmin Le Grand                     // geometry. Using the automatically set mbIsClipPathContent at
184ddde725dSArmin Le Grand                     // SvgStyleAttributes the clip definition is without fill, stroke,
185db3fbf26SArmin Le Grand                     // and strokeWidth and forced to black
186ddde725dSArmin Le Grand                     const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
187db3fbf26SArmin Le Grand                         new drawinglayer::primitive2d::MaskPrimitive2D(
188db3fbf26SArmin Le Grand                             aClipPolyPolygon,
189db3fbf26SArmin Le Grand                             rContent));
190ddde725dSArmin Le Grand 
191ddde725dSArmin Le Grand                     rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
192ddde725dSArmin Le Grand                 }
193ddde725dSArmin Le Grand                 else
194ddde725dSArmin Le Grand                 {
195ddde725dSArmin Le Grand                     // An empty clipping path will completely clip away the element that had
196ddde725dSArmin Le Grand                     // the �clip-path� property applied. (Svg spec)
197ddde725dSArmin Le Grand                     rContent.realloc(0);
198ddde725dSArmin Le Grand                 }
199ddde725dSArmin Le Grand             }
200ddde725dSArmin Le Grand         }
201ddde725dSArmin Le Grand 
202ddde725dSArmin Le Grand     } // end of namespace svgreader
203ddde725dSArmin Le Grand } // end of namespace svgio
204ddde725dSArmin Le Grand 
205ddde725dSArmin Le Grand //////////////////////////////////////////////////////////////////////////////
206ddde725dSArmin Le Grand // eof
207