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             maSvgStyleAttributes.checkForCssStyle(aClassStr);
57 
58             return &maSvgStyleAttributes;
59         }
60 
61         void SvgEllipseNode::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);
68 
69             // parse own
70             switch(aSVGToken)
71             {
72                 case SVGTokenStyle:
73                 {
74                     maSvgStyleAttributes.readStyle(aContent);
75                     break;
76                 }
77                 case SVGTokenCx:
78                 {
79                     SvgNumber aNum;
80 
81                     if(readSingleNumber(aContent, aNum))
82                     {
83                         setCx(aNum);
84                     }
85                     break;
86                 }
87                 case SVGTokenCy:
88                 {
89                     SvgNumber aNum;
90 
91                     if(readSingleNumber(aContent, aNum))
92                     {
93                         setCy(aNum);
94                     }
95                     break;
96                 }
97                 case SVGTokenRx:
98                 {
99                     SvgNumber aNum;
100 
101                     if(readSingleNumber(aContent, aNum))
102                     {
103                         if(aNum.isPositive())
104                         {
105                             setRx(aNum);
106                         }
107                     }
108                     break;
109                 }
110                 case SVGTokenRy:
111                 {
112                     SvgNumber aNum;
113 
114                     if(readSingleNumber(aContent, aNum))
115                     {
116                         if(aNum.isPositive())
117                         {
118                             setRy(aNum);
119                         }
120                     }
121                     break;
122                 }
123                 case SVGTokenTransform:
124                 {
125                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
126 
127                     if(!aMatrix.isIdentity())
128                     {
129                         setTransform(&aMatrix);
130                     }
131                     break;
132                 }
133             }
134         }
135 
136         void SvgEllipseNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool bReferenced) const
137         {
138             const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
139 
140             if(pStyle && getRx().isSet() && getRy().isSet())
141             {
142                 const double fRx(getRx().solve(*this, xcoordinate));
143                 const double fRy(getRy().solve(*this, ycoordinate));
144 
145                 if(fRx > 0.0 && fRy > 0.0)
146                 {
147                     const basegfx::B2DPolygon aPath(
148                         basegfx::tools::createPolygonFromEllipse(
149                             basegfx::B2DPoint(
150                                 getCx().isSet() ? getCx().solve(*this, xcoordinate) : 0.0,
151                                 getCy().isSet() ? getCy().solve(*this, ycoordinate) : 0.0),
152                             fRx, fRy));
153 
154                     drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
155 
156                     pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget);
157 
158                     if(aNewTarget.hasElements())
159                     {
160                         pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
161                     }
162                 }
163             }
164         }
165     } // end of namespace svgreader
166 } // end of namespace svgio
167 
168 //////////////////////////////////////////////////////////////////////////////
169 // eof
170