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/svgusenode.hxx>
26 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
27 #include <svgio/svgreader/svgdocument.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace svgio
32 {
33     namespace svgreader
34     {
35         SvgUseNode::SvgUseNode(
36             SvgDocument& rDocument,
37             SvgNode* pParent)
38         :   SvgNode(SVGTokenG, rDocument, pParent),
39             maSvgStyleAttributes(*this),
40             mpaTransform(0),
41             maX(),
42             maY(),
43             maWidth(),
44             maHeight(),
45             maXLink()
46         {
47         }
48 
49         SvgUseNode::~SvgUseNode()
50         {
51             if(mpaTransform) delete mpaTransform;
52         }
53 
54         const SvgStyleAttributes* SvgUseNode::getSvgStyleAttributes() const
55         {
56             return &maSvgStyleAttributes;
57         }
58 
59         void SvgUseNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
60         {
61             // call parent
62             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
63 
64             // read style attributes
65             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
66 
67             // parse own
68             switch(aSVGToken)
69             {
70                 case SVGTokenStyle:
71                 {
72                     maSvgStyleAttributes.readStyle(aContent);
73                     break;
74                 }
75                 case SVGTokenTransform:
76                 {
77                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
78 
79                     if(!aMatrix.isIdentity())
80                     {
81                         setTransform(&aMatrix);
82                     }
83                     break;
84                 }
85                 case SVGTokenX:
86                 {
87                     SvgNumber aNum;
88 
89                     if(readSingleNumber(aContent, aNum))
90                     {
91                         setX(aNum);
92                     }
93                     break;
94                 }
95                 case SVGTokenY:
96                 {
97                     SvgNumber aNum;
98 
99                     if(readSingleNumber(aContent, aNum))
100                     {
101                         setY(aNum);
102                     }
103                     break;
104                 }
105                 case SVGTokenWidth:
106                 {
107                     SvgNumber aNum;
108 
109                     if(readSingleNumber(aContent, aNum))
110                     {
111                         if(aNum.isPositive())
112                         {
113                             setWidth(aNum);
114                         }
115                     }
116                     break;
117                 }
118                 case SVGTokenHeight:
119                 {
120                     SvgNumber aNum;
121 
122                     if(readSingleNumber(aContent, aNum))
123                     {
124                         if(aNum.isPositive())
125                         {
126                             setHeight(aNum);
127                         }
128                     }
129                 }
130                 case SVGTokenXlinkHref:
131                 {
132                     const sal_Int32 nLen(aContent.getLength());
133 
134                     if(nLen && sal_Unicode('#') == aContent[0])
135                     {
136                         maXLink = aContent.copy(1);
137                     }
138                     break;
139                 }
140             }
141         }
142 
143         void SvgUseNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
144         {
145             // try to access link to content
146             const SvgNode* mpXLink = getDocument().findSvgNodeById(maXLink);
147 
148             if(mpXLink)
149             {
150                 // decompose childs
151                 drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
152 
153                 // todo: in case mpXLink is a SVGTokenSvg or SVGTokenSymbol the
154                 // SVG docs want the getWidth() and getHeight() from this node
155                 // to be valid for the subtree.
156                 const_cast< SvgNode* >(mpXLink)->setAlternativeParent(this);
157                 mpXLink->decomposeSvgNode(aNewTarget, true);
158                 const_cast< SvgNode* >(mpXLink)->setAlternativeParent(0);
159 
160                 if(aNewTarget.hasElements())
161                 {
162                     basegfx::B2DHomMatrix aTransform;
163 
164                     if(getX().isSet() || getY().isSet())
165                     {
166                         aTransform.translate(
167                             getX().solve(*this, xcoordinate),
168                             getY().solve(*this, ycoordinate));
169                     }
170 
171                     if(getTransform())
172                     {
173                         aTransform = *getTransform() * aTransform;
174                     }
175 
176                     if(!aTransform.isIdentity())
177                     {
178                         const drawinglayer::primitive2d::Primitive2DReference xRef(
179                             new drawinglayer::primitive2d::TransformPrimitive2D(
180                                 aTransform,
181                                 aNewTarget));
182 
183                         drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
184                     }
185                     else
186                     {
187                         drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
188                     }
189                 }
190             }
191         }
192 
193     } // end of namespace svgreader
194 } // end of namespace svgio
195 
196 //////////////////////////////////////////////////////////////////////////////
197 // eof
198