update credits
[LibreOffice.git] / svgio / source / svgreader / svgrectnode.cxx
blob5d518a46c2459543e655ff6d580f10cd1f78b901
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <svgio/svgreader/svgrectnode.hxx>
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
24 //////////////////////////////////////////////////////////////////////////////
26 namespace svgio
28 namespace svgreader
30 SvgRectNode::SvgRectNode(
31 SvgDocument& rDocument,
32 SvgNode* pParent)
33 : SvgNode(SVGTokenRect, rDocument, pParent),
34 maSvgStyleAttributes(*this),
35 maX(0),
36 maY(0),
37 maWidth(0),
38 maHeight(0),
39 maRx(0),
40 maRy(0),
41 mpaTransform(0)
45 SvgRectNode::~SvgRectNode()
47 if(mpaTransform) delete mpaTransform;
50 const SvgStyleAttributes* SvgRectNode::getSvgStyleAttributes() const
52 static rtl::OUString aClassStr(rtl::OUString::createFromAscii("rect"));
53 return checkForCssStyle(aClassStr, maSvgStyleAttributes);
56 void SvgRectNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
58 // call parent
59 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
61 // read style attributes
62 maSvgStyleAttributes.parseStyleAttribute(rTokenName, aSVGToken, aContent);
64 // parse own
65 switch(aSVGToken)
67 case SVGTokenStyle:
69 maSvgStyleAttributes.readStyle(aContent);
70 break;
72 case SVGTokenX:
74 SvgNumber aNum;
76 if(readSingleNumber(aContent, aNum))
78 setX(aNum);
80 break;
82 case SVGTokenY:
84 SvgNumber aNum;
86 if(readSingleNumber(aContent, aNum))
88 setY(aNum);
90 break;
92 case SVGTokenWidth:
94 SvgNumber aNum;
96 if(readSingleNumber(aContent, aNum))
98 if(aNum.isPositive())
100 setWidth(aNum);
103 break;
105 case SVGTokenHeight:
107 SvgNumber aNum;
109 if(readSingleNumber(aContent, aNum))
111 if(aNum.isPositive())
113 setHeight(aNum);
116 break;
118 case SVGTokenRx:
120 SvgNumber aNum;
122 if(readSingleNumber(aContent, aNum))
124 if(aNum.isPositive())
126 setRx(aNum);
129 break;
131 case SVGTokenRy:
133 SvgNumber aNum;
135 if(readSingleNumber(aContent, aNum))
137 if(aNum.isPositive())
139 setRy(aNum);
142 break;
144 case SVGTokenTransform:
146 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
148 if(!aMatrix.isIdentity())
150 setTransform(&aMatrix);
152 break;
154 default:
156 break;
161 void SvgRectNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DSequence& rTarget, bool /*bReferenced*/) const
163 // get size range and create path
164 const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
166 if(pStyle && getWidth().isSet() && getHeight().isSet())
168 const double fWidth(getWidth().solve(*this, xcoordinate));
169 const double fHeight(getHeight().solve(*this, ycoordinate));
171 if(fWidth > 0.0 && fHeight > 0.0)
173 const double fX(getX().isSet() ? getX().solve(*this, xcoordinate) : 0.0);
174 const double fY(getY().isSet() ? getY().solve(*this, ycoordinate) : 0.0);
175 const basegfx::B2DRange aRange(fX, fY, fX + fWidth, fY + fHeight);
176 basegfx::B2DPolygon aPath;
178 if(getRx().isSet() || getRy().isSet())
180 double frX(getRx().isSet() ? getRx().solve(*this, xcoordinate) : 0.0);
181 double frY(getRy().isSet() ? getRy().solve(*this, ycoordinate) : 0.0);
183 frX = std::max(0.0, frX);
184 frY = std::max(0.0, frY);
186 if(0.0 == frY && frX > 0.0)
188 frY = frX;
190 else if(0.0 == frX && frY > 0.0)
192 frX = frY;
195 frX /= fWidth;
196 frY /= fHeight;
198 frX = std::min(0.5, frX);
199 frY = std::min(0.5, frY);
201 aPath = basegfx::tools::createPolygonFromRect(aRange, frX * 2.0, frY * 2.0);
203 else
205 aPath = basegfx::tools::createPolygonFromRect(aRange);
208 drawinglayer::primitive2d::Primitive2DSequence aNewTarget;
210 pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget);
212 if(aNewTarget.hasElements())
214 pStyle->add_postProcess(rTarget, aNewTarget, getTransform());
219 } // end of namespace svgreader
220 } // end of namespace svgio
222 //////////////////////////////////////////////////////////////////////////////
223 // eof
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */