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                 default:
141                 {
142                     break;
143                 }
144             }
145         }
146 
147         void SvgUseNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced*/) const
148         {
149             // try to access link to content
150             const SvgNode* mpXLink = getDocument().findSvgNodeById(maXLink);
151 
152             if(mpXLink)
153             {
154                 // decompose childs
155                 drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
156 
157                 // todo: in case mpXLink is a SVGTokenSvg or SVGTokenSymbol the
158                 // SVG docs want the getWidth() and getHeight() from this node
159                 // to be valid for the subtree.
160                 const_cast< SvgNode* >(mpXLink)->setAlternativeParent(this);
161                 mpXLink->decomposeSvgNode(aNewTarget, true);
162                 const_cast< SvgNode* >(mpXLink)->setAlternativeParent(0);
163 
164                 if(aNewTarget.hasElements())
165                 {
166                     basegfx::B2DHomMatrix aTransform;
167 
168                     if(getX().isSet() || getY().isSet())
169                     {
170                         aTransform.translate(
171                             getX().solve(*this, xcoordinate),
172                             getY().solve(*this, ycoordinate));
173                     }
174 
175                     if(getTransform())
176                     {
177                         aTransform = *getTransform() * aTransform;
178                     }
179 
180                     if(!aTransform.isIdentity())
181                     {
182                         const drawinglayer::primitive2d::Primitive2DReference xRef(
183                             new drawinglayer::primitive2d::TransformPrimitive2D(
184                                 aTransform,
185                                 aNewTarget));
186 
187                         drawinglayer::primitive2d::appendPrimitive2DReferenceToPrimitive2DSequence(rTarget, xRef);
188                     }
189                     else
190                     {
191                         drawinglayer::primitive2d::appendPrimitive2DSequenceToPrimitive2DSequence(rTarget, aNewTarget);
192                     }
193                 }
194             }
195         }
196 
197     } // end of namespace svgreader
198 } // end of namespace svgio
199 
200 //////////////////////////////////////////////////////////////////////////////
201 // eof
202