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/svgrectnode.hxx>
26 #include <basegfx/polygon/b2dpolygon.hxx>
27 #include <basegfx/polygon/b2dpolygontools.hxx>
28 
29 //////////////////////////////////////////////////////////////////////////////
30 
31 namespace svgio
32 {
33     namespace svgreader
34     {
SvgRectNode(SvgDocument & rDocument,SvgNode * pParent)35         SvgRectNode::SvgRectNode(
36             SvgDocument& rDocument,
37             SvgNode* pParent)
38         :   SvgNode(SVGTokenRect, rDocument, pParent),
39             maSvgStyleAttributes(*this),
40             maX(0),
41             maY(0),
42             maWidth(0),
43             maHeight(0),
44             maRx(0),
45             maRy(0),
46             mpaTransform(0)
47         {
48         }
49 
~SvgRectNode()50         SvgRectNode::~SvgRectNode()
51         {
52             if(mpaTransform) delete mpaTransform;
53         }
54 
getSvgStyleAttributes() const55         const SvgStyleAttributes* SvgRectNode::getSvgStyleAttributes() const
56         {
57             static rtl::OUString aClassStr(rtl::OUString::createFromAscii("rect"));
58 
59             return checkForCssStyle(aClassStr, maSvgStyleAttributes);
60         }
61 
parseAttribute(const rtl::OUString & rTokenName,SVGToken aSVGToken,const rtl::OUString & aContent)62         void SvgRectNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
63         {
64             // call parent
65             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
66 
67             // read style attributes
68             maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent, false);
69 
70             // parse own
71             switch(aSVGToken)
72             {
73                 case SVGTokenStyle:
74                 {
75                     readLocalCssStyle(aContent);
76                     break;
77                 }
78                 case SVGTokenX:
79                 {
80                     SvgNumber aNum;
81 
82                     if(readSingleNumber(aContent, aNum))
83                     {
84                         setX(aNum);
85                     }
86                     break;
87                 }
88                 case SVGTokenY:
89                 {
90                     SvgNumber aNum;
91 
92                     if(readSingleNumber(aContent, aNum))
93                     {
94                         setY(aNum);
95                     }
96                     break;
97                 }
98                 case SVGTokenWidth:
99                 {
100                     SvgNumber aNum;
101 
102                     if(readSingleNumber(aContent, aNum))
103                     {
104                         if(aNum.isPositive())
105                         {
106                             setWidth(aNum);
107                         }
108                     }
109                     break;
110                 }
111                 case SVGTokenHeight:
112                 {
113                     SvgNumber aNum;
114 
115                     if(readSingleNumber(aContent, aNum))
116                     {
117                         if(aNum.isPositive())
118                         {
119                             setHeight(aNum);
120                         }
121                     }
122                     break;
123                 }
124                 case SVGTokenRx:
125                 {
126                     SvgNumber aNum;
127 
128                     if(readSingleNumber(aContent, aNum))
129                     {
130                         if(aNum.isPositive())
131                         {
132                             setRx(aNum);
133                         }
134                     }
135                     break;
136                 }
137                 case SVGTokenRy:
138                 {
139                     SvgNumber aNum;
140 
141                     if(readSingleNumber(aContent, aNum))
142                     {
143                         if(aNum.isPositive())
144                         {
145                             setRy(aNum);
146                         }
147                     }
148                     break;
149                 }
150                 case SVGTokenTransform:
151                 {
152                     const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
153 
154                     if(!aMatrix.isIdentity())
155                     {
156                         setTransform(&aMatrix);
157                     }
158                     break;
159                 }
160                 default:
161                 {
162                     break;
163                 }
164             }
165         }
166 
decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence & rTarget,bool) const167         void SvgRectNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced*/) const
168         {
169             // get size range and create path
170             const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
171 
172             if(pStyle && getWidth().isSet() && getHeight().isSet())
173             {
174                 const double fWidth(getWidth().solve(*this, xcoordinate));
175                 const double fHeight(getHeight().solve(*this, ycoordinate));
176 
177                 if(fWidth > 0.0 && fHeight > 0.0)
178                 {
179                     const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0);
180                     const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0);
181                     const basegfx::B2DRange aRange(fX, fY, fX + fWidth, fY + fHeight);
182                     basegfx::B2DPolygon aPath;
183 
184                     if(getRx().isSet() || getRy().isSet())
185                     {
186                         double frX(getRx().isSet() ? getRx().solve(*this, xcoordinate) : 0.0);
187                         double frY(getRy().isSet() ? getRy().solve(*this, ycoordinate) : 0.0);
188 
189                         frX = std::max(0.0, frX);
190                         frY = std::max(0.0, frY);
191 
192                         if(0.0 == frY && frX > 0.0)
193                         {
194                             frY = frX;
195                         }
196                         else if(0.0 == frX && frY > 0.0)
197                         {
198                             frX = frY;
199                         }
200 
201                         frX /= fWidth;
202                         frY /= fHeight;
203 
204                         frX = std::min(0.5, frX);
205                         frY = std::min(0.5, frY);
206 
207                         aPath = basegfx::tools::createPolygonFromRect(aRange, frX * 2.0, frY * 2.0);
208                     }
209                     else
210                     {
211                         aPath = basegfx::tools::createPolygonFromRect(aRange);
212                     }
213 
214                     drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
215 
216                     pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget, 0);
217 
218                     if(aNewTarget.hasElements())
219                     {
220                         pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
221                     }
222                 }
223             }
224         }
225     } // end of namespace svgreader
226 } // end of namespace svgio
227 
228 //////////////////////////////////////////////////////////////////////////////
229 // eof
230