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