LibreOffice Module svgio (master) 1
svgrectnode.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <svgrectnode.hxx>
24
25namespace svgio::svgreader
26{
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)
36 {
37 }
38
40 {
41 }
42
44 {
46 }
47
48 void SvgRectNode::parseAttribute(const OUString& rTokenName, SVGToken aSVGToken, const OUString& aContent)
49 {
50 // call parent
51 SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
52
53 // read style attributes
54 maSvgStyleAttributes.parseStyleAttribute(aSVGToken, aContent);
55
56 // parse own
57 switch(aSVGToken)
58 {
59 case SVGToken::Style:
60 {
61 readLocalCssStyle(aContent);
62 break;
63 }
64 case SVGToken::X:
65 {
66 SvgNumber aNum;
67
68 if(readSingleNumber(aContent, aNum))
69 {
70 maX = aNum;
71 }
72 break;
73 }
74 case SVGToken::Y:
75 {
76 SvgNumber aNum;
77
78 if(readSingleNumber(aContent, aNum))
79 {
80 maY = aNum;
81 }
82 break;
83 }
84 case SVGToken::Width:
85 {
86 SvgNumber aNum;
87
88 if(readSingleNumber(aContent, aNum))
89 {
90 if(aNum.isPositive())
91 {
92 maWidth = aNum;
93 }
94 }
95 break;
96 }
98 {
99 SvgNumber aNum;
100
101 if(readSingleNumber(aContent, aNum))
102 {
103 if(aNum.isPositive())
104 {
105 maHeight = aNum;
106 }
107 }
108 break;
109 }
110 case SVGToken::Rx:
111 {
112 SvgNumber aNum;
113
114 if(readSingleNumber(aContent, aNum))
115 {
116 if(aNum.isPositive())
117 {
118 maRx = aNum;
119 }
120 }
121 break;
122 }
123 case SVGToken::Ry:
124 {
125 SvgNumber aNum;
126
127 if(readSingleNumber(aContent, aNum))
128 {
129 if(aNum.isPositive())
130 {
131 maRy = aNum;
132 }
133 }
134 break;
135 }
137 {
138 const basegfx::B2DHomMatrix aMatrix(readTransform(aContent, *this));
139
140 if(!aMatrix.isIdentity())
141 {
142 setTransform(aMatrix);
143 }
144 break;
145 }
146 default:
147 {
148 break;
149 }
150 }
151 }
152
154 {
155 // get size range and create path
157
158 if(!(pStyle && getWidth().isSet() && getHeight().isSet()))
159 return;
160
161 const double fWidth(getWidth().solve(*this, NumberType::xcoordinate));
162 const double fHeight(getHeight().solve(*this, NumberType::ycoordinate));
163
164 if(fWidth <= 0.0 || fHeight <= 0.0)
165 return;
166
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);
171
172 if(getRx().isSet() || getRy().isSet())
173 {
174 double frX(getRx().isSet() ? getRx().solve(*this, NumberType::xcoordinate) : 0.0);
175 double frY(getRy().isSet() ? getRy().solve(*this, NumberType::ycoordinate) : 0.0);
176
177 if(!getRy().isSet() && 0.0 == frY && frX > 0.0)
178 {
179 frY = frX;
180 }
181 else if(!getRx().isSet() && 0.0 == frX && frY > 0.0)
182 {
183 frX = frY;
184 }
185
186 frX /= fWidth;
187 frY /= fHeight;
188
189 frX = std::min(0.5, frX);
190 frY = std::min(0.5, frY);
191
192 aPath = basegfx::utils::createPolygonFromRect(aRange, frX * 2.0, frY * 2.0);
193 }
194 else
195 {
197 }
198
200
201 pStyle->add_path(basegfx::B2DPolyPolygon(aPath), aNewTarget, nullptr);
202
203 if(!aNewTarget.empty())
204 {
205 pStyle->add_postProcess(rTarget, std::move(aNewTarget), getTransform());
206 }
207 }
208} // end of namespace svgio::svgreader
209
210/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool isIdentity() const
const SvgStyleAttributes * checkForCssStyle(const SvgStyleAttributes &rOriginal) const
helper to evtl. link to css style
Definition: svgnode.cxx:335
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent)
Definition: svgnode.cxx:534
void readLocalCssStyle(std::u16string_view aContent)
scan helper to read and interpret a local CssStyle to mpLocalCssStyle
Definition: svgnode.cxx:412
void setTransform(const std::optional< basegfx::B2DHomMatrix > &pMatrix)
Definition: svgrectnode.hxx:73
const std::optional< basegfx::B2DHomMatrix > & getTransform() const
transform content, set if found in current context
Definition: svgrectnode.hxx:72
SvgStyleAttributes maSvgStyleAttributes
use styles
Definition: svgrectnode.hxx:32
const SvgNumber & getX() const
x content, set if found in current context
Definition: svgrectnode.hxx:54
SvgNumber maX
variable scan values, dependent of given XAttributeList
Definition: svgrectnode.hxx:35
const SvgNumber & getRx() const
Rx content, set if found in current context.
Definition: svgrectnode.hxx:66
const SvgNumber & getHeight() const
height content, set if found in current context
Definition: svgrectnode.hxx:63
const SvgNumber & getWidth() const
width content, set if found in current context
Definition: svgrectnode.hxx:60
const SvgNumber & getRy() const
Ry content, set if found in current context.
Definition: svgrectnode.hxx:69
SvgRectNode(SvgDocument &rDocument, SvgNode *pParent)
Definition: svgrectnode.cxx:27
virtual const SvgStyleAttributes * getSvgStyleAttributes() const override
Definition: svgrectnode.cxx:43
virtual void decomposeSvgNode(drawinglayer::primitive2d::Primitive2DContainer &rTarget, bool bReferenced) const override
virtual void parseAttribute(const OUString &rTokenName, SVGToken aSVGToken, const OUString &aContent) override
Definition: svgrectnode.cxx:48
virtual ~SvgRectNode() override
Definition: svgrectnode.cxx:39
const SvgNumber & getY() const
y content, set if found in current context
Definition: svgrectnode.hxx:57
void add_postProcess(drawinglayer::primitive2d::Primitive2DContainer &rTarget, drawinglayer::primitive2d::Primitive2DContainer &&rSource, const std::optional< basegfx::B2DHomMatrix > &pTransform) const
void add_path(const basegfx::B2DPolyPolygon &rPath, drawinglayer::primitive2d::Primitive2DContainer &rTarget, const basegfx::utils::PointIndexSet *pHelpPointIndices) const
void parseStyleAttribute(SVGToken aSVGToken, const OUString &rContent)
local attribute scanner
FilterGroup & rTarget
bool solve(Matrix &matrix, int rows, int cols, Vector &result, BaseType minPivot)
B2DPolygon createPolygonFromRect(const B2DRectangle &rRect, double fRadiusX, double fRadiusY)
bool readSingleNumber(std::u16string_view rCandidate, SvgNumber &aNum)
Definition: svgtools.cxx:1076
basegfx::B2DHomMatrix readTransform(std::u16string_view rCandidate, InfoProvider const &rInfoProvider)
Definition: svgtools.cxx:871