Fix rudimentary Emscripten Qt6 copy -> paste support
[LibreOffice.git] / svgio / source / svgreader / svgrectnode.cxx
blob3829f21a678ba0482da107ad57d26ffcf7a8b678
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 <svgrectnode.hxx>
21 #include <basegfx/polygon/b2dpolygon.hxx>
22 #include <basegfx/polygon/b2dpolygontools.hxx>
23 #include <basegfx/polygon/b2dpolypolygon.hxx>
25 namespace svgio::svgreader
27 SvgRectNode::SvgRectNode(
28 SvgDocument& rDocument,
29 SvgNode* pParent)
30 : SvgNode(SVGToken::Rect, rDocument, pParent),
31 maSvgStyleAttributes(*this),
32 maX(0),
33 maY(0),
34 maWidth(0),
35 maHeight(0)
39 SvgRectNode::~SvgRectNode()
43 const SvgStyleAttributes* SvgRectNode::getSvgStyleAttributes() const
45 return checkForCssStyle(maSvgStyleAttributes);
48 void SvgRectNode::parseAttribute(SVGToken aSVGToken, const OUString& aContent)
50 // call parent
51 SvgNode::parseAttribute(aSVGToken, aContent);
53 // read style attributes
54 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
56 // parse own
57 switch(aSVGToken)
59 case SVGToken::Style:
61 readLocalCssStyle(aContent);
62 break;
64 case SVGToken::X:
66 SvgNumber aNum;
68 if(readSingleNumber(aContent, aNum))
70 maX = aNum;
72 break;
74 case SVGToken::Y:
76 SvgNumber aNum;
78 if(readSingleNumber(aContent, aNum))
80 maY = aNum;
82 break;
84 case SVGToken::Width:
86 SvgNumber aNum;
88 if(readSingleNumber(aContent, aNum))
90 if(aNum.isPositive())
92 maWidth = aNum;
95 break;
97 case SVGToken::Height:
99 SvgNumber aNum;
101 if(readSingleNumber(aContent, aNum))
103 if(aNum.isPositive())
105 maHeight = aNum;
108 break;
110 case SVGToken::Rx:
112 SvgNumber aNum;
114 if(readSingleNumber(aContent, aNum))
116 if(aNum.isPositive())
118 maRx = aNum;
121 break;
123 case SVGToken::Ry:
125 SvgNumber aNum;
127 if(readSingleNumber(aContent, aNum))
129 if(aNum.isPositive())
131 maRy = aNum;
134 break;
136 case SVGToken::Transform:
138 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
140 if(!aMatrix.isIdentity())
142 setTransform(aMatrix);
144 break;
146 default:
148 break;
153 void SvgRectNode::decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer& rTarget, bool /*bReferenced*/) const
155 // get size range and create path
156 const SvgStyleAttributes* pStyle = getSvgStyleAttributes();
158 if(!(pStyle && getWidth().isSet() && getHeight().isSet()))
159 return;
161 const double fWidth(getWidth().solve(*this, NumberType::xcoordinate));
162 const double fHeight(getHeight().solve(*this, NumberType::ycoordinate));
164 if(fWidth <= 0.0 || fHeight <= 0.0)
165 return;
167 const double fX(getX().isSet() ? getX().solve(*this, NumberType::xcoordinate) : 0.0);
168 const double fY(getY().isSet() ? getY().solve(*this, NumberType::ycoordinate) : 0.0);
169 const basegfx::B2DRange aRange(fX, fY, fX + fWidth, fY + fHeight);
170 basegfx::B2DPolygon aPath;
172 if(getRx().isSet() || getRy().isSet())
174 double frX(getRx().isSet() ? getRx().solve(*this, NumberType::xcoordinate) : 0.0);
175 double frY(getRy().isSet() ? getRy().solve(*this, NumberType::ycoordinate) : 0.0);
177 if(!getRy().isSet() && 0.0 == frY && frX > 0.0)
179 frY = frX;
181 else if(!getRx().isSet() && 0.0 == frX && frY > 0.0)
183 frX = frY;
186 frX /= fWidth;
187 frY /= fHeight;
189 frX = std::min(0.5, frX);
190 frY = std::min(0.5, frY);
192 aPath = basegfx::utils::createPolygonFromRect(aRange, frX * 2.0, frY * 2.0);
194 else
196 aPath = basegfx::utils::createPolygonFromRect(aRange);
199 drawinglayer::primitive2d::Primitive2DContainer aNewTarget;
201 pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget, nullptr);
203 if(!aNewTarget.empty())
205 pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform());
208 } // end of namespace svgio::svgreader
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */