1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svgio.hxx"
24 
25 #include <svgio/svgreader/svgclippathnode.hxx>
26 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
27 #include <drawinglayer/primitive2d/transparenceprimitive2d.hxx>
28 #include <basegfx/matrix/b2dhommatrixtools.hxx>
29 #include <drawinglayer/geometry/viewinformation2d.hxx>
30 
31 //////////////////////////////////////////////////////////////////////////////
32 
33 namespace svgio
34 {
35     namespace svgreader
36     {
37         SvgClipPathNode::SvgClipPathNode(
38             SvgDocument& rDocument,
39             SvgNode* pParent)
40         :   SvgNode(SVGTokenClipPathNode, rDocument, pParent),
41             maSvgStyleAttributes(*this),
42             mpaTransform(0),
43             maClipPathUnits(userSpaceOnUse)
44         {
45         }
46 
47         SvgClipPathNode::~SvgClipPathNode()
48         {
49             if(mpaTransform) delete mpaTransform;
50         }
51 
52         const SvgStyleAttributes* SvgClipPathNode::getSvgStyleAttributes() const
53         {
54             return &maSvgStyleAttributes;
55         }
56 
57         void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
58         {
59             // call parent
60             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
61 
62             // read style attributes
63             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
64 
65             // parse own
66             switch(aSVGToken)
67             {
68                 case SVGTokenStyle:
69                 {
70                     maSvgStyleAttributes.readStyle(aContent);
71                     break;
72                 }
73                 case SVGTokenTransform:
74                 {
75                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
76 
77                     if(!aMatrix.isIdentity())
78                     {
79                         setTransform(&aMatrix);
80                     }
81                     break;
82                 }
83                 case SVGTokenClipPathUnits:
84                 {
85                     if(aContent.getLength())
86                     {
87                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
88                         {
89                             setClipPathUnits(userSpaceOnUse);
90                         }
91                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
92                         {
93                             setClipPathUnits(objectBoundingBox);
94                         }
95                     }
96                     break;
97                 }
98                 default:
99                 {
100                     break;
101                 }
102             }
103         }
104 
105         void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
106         {
107             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
108 
109             // decompose childs
110             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
111 
112             if(aNewTarget.hasElements())
113             {
114                 if(getTransform())
115                 {
116                     // create embedding group element with transformation
117                     const drawinglayer::primitive2d::Primitive2DReference xRef(
118                         new drawinglayer::primitive2d::TransformPrimitive2D(
119                             *getTransform(),
120                             aNewTarget));
121 
122                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
123                 }
124                 else
125                 {
126                     // append to current target
127                     drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
128                 }
129             }
130         }
131 
132         void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
133         {
134             if(rContent.hasElements())
135             {
136                 drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
137 
138                 // get clipPath definition as primitives
139                 decomposeSvgNode(aClipTarget, true);
140 
141                 if(aClipTarget.hasElements())
142                 {
143                     if(objectBoundingBox == getClipPathUnits())
144                     {
145                         // clip is object-relative, embed in content transformation
146                         const basegfx::B2DRange aContentRange(
147                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
148                                 rContent,
149                                 drawinglayer::geometry::ViewInformation2D()));
150 
151                         const drawinglayer::primitive2d::Primitive2DReference xTransform(
152                             new drawinglayer::primitive2d::TransformPrimitive2D(
153                                 basegfx::tools::createScaleTranslateB2DHomMatrix(
154                                     aContentRange.getRange(),
155                                     aContentRange.getMinimum()),
156                                 aClipTarget));
157 
158                         aClipTarget = drawinglayer::primitive2d::Primitive2DSequence(&xTransform, 1);
159                     }
160 
161                     // redefine target. Use TransparencePrimitive2D with created clip
162                     // geometry. Using the automatically set mbIsClipPathContent at
163                     // SvgStyleAttributes the clip definition is without fill, stroke,
164                     // and strokeWidth and forced to black, thus being 100% opaque
165                     const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
166                         new drawinglayer::primitive2d::TransparencePrimitive2D(
167                             rContent,
168                             aClipTarget));
169 
170                     rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
171                 }
172                 else
173                 {
174                     // An empty clipping path will completely clip away the element that had
175                     // the �clip-path� property applied. (Svg spec)
176                     rContent.realloc(0);
177                 }
178             }
179         }
180 
181     } // end of namespace svgreader
182 } // end of namespace svgio
183 
184 //////////////////////////////////////////////////////////////////////////////
185 // eof
186