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             static rtl::OUString aClassStr(rtl::OUString::createFromAscii("clip-path"));
55             maSvgStyleAttributes.checkForCssStyle(aClassStr);
56 
57             return &maSvgStyleAttributes;
58         }
59 
60         void SvgClipPathNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
61         {
62             // call parent
63             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
64 
65             // read style attributes
66             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
67 
68             // parse own
69             switch(aSVGToken)
70             {
71                 case SVGTokenStyle:
72                 {
73                     maSvgStyleAttributes.readStyle(aContent);
74                     break;
75                 }
76                 case SVGTokenTransform:
77                 {
78                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
79 
80                     if(!aMatrix.isIdentity())
81                     {
82                         setTransform(&aMatrix);
83                     }
84                     break;
85                 }
86                 case SVGTokenClipPathUnits:
87                 {
88                     if(aContent.getLength())
89                     {
90                         if(aContent.match(commonStrings::aStrUserSpaceOnUse, 0))
91                         {
92                             setClipPathUnits(userSpaceOnUse);
93                         }
94                         else if(aContent.match(commonStrings::aStrObjectBoundingBox, 0))
95                         {
96                             setClipPathUnits(objectBoundingBox);
97                         }
98                     }
99                     break;
100                 }
101                 default:
102                 {
103                     break;
104                 }
105             }
106         }
107 
108         void SvgClipPathNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
109         {
110             drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
111 
112             // decompose childs
113             SvgNode::decomposeSvgNode(aNewTarget, bReferenced);
114 
115             if(aNewTarget.hasElements())
116             {
117                 if(getTransform())
118                 {
119                     // create embedding group element with transformation
120                     const drawinglayer::primitive2d::Primitive2DReference xRef(
121                         new drawinglayer::primitive2d::TransformPrimitive2D(
122                             *getTransform(),
123                             aNewTarget));
124 
125                     drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
126                 }
127                 else
128                 {
129                     // append to current target
130                     drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
131                 }
132             }
133         }
134 
135         void SvgClipPathNode::apply(drawinglayer::primitive2d::Primitive2DSequence& rContent) const
136         {
137             if(rContent.hasElements())
138             {
139                 drawinglayer::primitive2d::Primitive2DSequence aClipTarget;
140 
141                 // get clipPath definition as primitives
142                 decomposeSvgNode(aClipTarget, true);
143 
144                 if(aClipTarget.hasElements())
145                 {
146                     if(objectBoundingBox == getClipPathUnits())
147                     {
148                         // clip is object-relative, embed in content transformation
149                         const basegfx::B2DRange aContentRange(
150                             drawinglayer::primitive2d::getB2DRangeFromPrimitive2DSequence(
151                                 rContent,
152                                 drawinglayer::geometry::ViewInformation2D()));
153 
154                         const drawinglayer::primitive2d::Primitive2DReference xTransform(
155                             new drawinglayer::primitive2d::TransformPrimitive2D(
156                                 basegfx::tools::createScaleTranslateB2DHomMatrix(
157                                     aContentRange.getRange(),
158                                     aContentRange.getMinimum()),
159                                 aClipTarget));
160 
161                         aClipTarget = drawinglayer::primitive2d::Primitive2DSequence(&xTransform, 1);
162                     }
163 
164                     // redefine target. Use TransparencePrimitive2D with created clip
165                     // geometry. Using the automatically set mbIsClipPathContent at
166                     // SvgStyleAttributes the clip definition is without fill, stroke,
167                     // and strokeWidth and forced to black, thus being 100% opaque
168                     const drawinglayer::primitive2d::Primitive2DReference xEmbedTransparence(
169                         new drawinglayer::primitive2d::TransparencePrimitive2D(
170                             rContent,
171                             aClipTarget));
172 
173                     rContent = drawinglayer::primitive2d::Primitive2DSequence(&xEmbedTransparence, 1);
174                 }
175                 else
176                 {
177                     // An empty clipping path will completely clip away the element that had
178                     // the �clip-path� property applied. (Svg spec)
179                     rContent.realloc(0);
180                 }
181             }
182         }
183 
184     } // end of namespace svgreader
185 } // end of namespace svgio
186 
187 //////////////////////////////////////////////////////////////////////////////
188 // eof
189