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/svgellipsenode.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace svgio
32 {
33     namespace svgreader
34     {
35         SvgEllipseNode::SvgEllipseNode(
36             SvgDocument& rDocument,
37             SvgNode* pParent)
38         :   SvgNode(SVGTokenEllipse, rDocument, pParent),
39             maSvgStyleAttributes(*this),
40             maCx(0),
41             maCy(0),
42             maRx(0),
43             maRy(0),
44             mpaTransform(0)
45         {
46         }
47 
48         SvgEllipseNode::~SvgEllipseNode()
49         {
50             if(mpaTransform) delete mpaTransform;
51         }
52 
53         const SvgStyleAttributes* SvgEllipseNode::getSvgStyleAttributes() const
54         {
55             static rtl::OUString aClassStr(rtl::OUString::createFromAscii("ellipse"));
56 
57             return checkForCssStyle(aClassStr, maSvgStyleAttributes);
58         }
59 
60         void SvgEllipseNode::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 SVGTokenCx:
77                 {
78                     SvgNumber aNum;
79 
80                     if(readSingleNumber(aContent, aNum))
81                     {
82                         setCx(aNum);
83                     }
84                     break;
85                 }
86                 case SVGTokenCy:
87                 {
88                     SvgNumber aNum;
89 
90                     if(readSingleNumber(aContent, aNum))
91                     {
92                         setCy(aNum);
93                     }
94                     break;
95                 }
96                 case SVGTokenRx:
97                 {
98                     SvgNumber aNum;
99 
100                     if(readSingleNumber(aContent, aNum))
101                     {
102                         if(aNum.isPositive())
103                         {
104                             setRx(aNum);
105                         }
106                     }
107                     break;
108                 }
109                 case SVGTokenRy:
110                 {
111                     SvgNumber aNum;
112 
113                     if(readSingleNumber(aContent, aNum))
114                     {
115                         if(aNum.isPositive())
116                         {
117                             setRy(aNum);
118                         }
119                     }
120                     break;
121                 }
122                 case SVGTokenTransform:
123                 {
124                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
125 
126                     if(!aMatrix.isIdentity())
127                     {
128                         setTransform(&aMatrix);
129                     }
130                     break;
131                 }
132                 default:
133                 {
134                     break;
135                 }
136             }
137         }
138 
139         void SvgEllipseNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced*/) const
140         {
141             const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
142 
143             if(pStyle && getRx().isSet() && getRy().isSet())
144             {
145                 const double fRx(getRx().solve(*this, xcoordinate));
146                 const double fRy(getRy().solve(*this, ycoordinate));
147 
148                 if(fRx > 0.0 && fRy > 0.0)
149                 {
150                     const basegfx::B2DPolygon aPath(
151                         basegfx::tools::createPolygonFromEllipse(
152                             basegfx::B2DPoint(
153                                 getCx().isSet() ? getCx().solve(*this, xcoordinate) : 0.0,
154                                 getCy().isSet() ? getCy().solve(*this, ycoordinate) : 0.0),
155                             fRx, fRy));
156 
157                     drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
158 
159                     pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget);
160 
161                     if(aNewTarget.hasElements())
162                     {
163                         pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
164                     }
165                 }
166             }
167         }
168     } // end of namespace svgreader
169 } // end of namespace svgio
170 
171 //////////////////////////////////////////////////////////////////////////////
172 // eof
173