1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <sal/config.h>
13 #include <svgrectnode.hxx>
14 #include <svgsvgnode.hxx>
15 #include <svgpathnode.hxx>
16 #include <svggradientnode.hxx>
18 #include <svgvisitor.hxx>
20 namespace svgio::svgreader
22 SvgDrawVisitor::SvgDrawVisitor()
23 : mpDrawRoot(std::make_shared
<gfx::DrawRoot
>())
24 , mpCurrent(mpDrawRoot
)
28 void SvgDrawVisitor::visit(svgio::svgreader::SvgNode
const& rNode
)
30 switch (rNode
.getType())
32 case svgio::svgreader::SVGToken::Svg
:
34 auto const& rSvgNode
= static_cast<svgio::svgreader::SvgSvgNode
const&>(rNode
);
36 basegfx::B2DRange aRange
= rSvgNode
.getCurrentViewPort();
38 static_cast<gfx::DrawRoot
*>(mpCurrent
.get())->maRectangle
= aRange
;
41 case svgio::svgreader::SVGToken::Rect
:
43 auto const& rRectNode
= static_cast<svgio::svgreader::SvgRectNode
const&>(rNode
);
45 double x
= rRectNode
.getX().getNumber();
46 double y
= rRectNode
.getY().getNumber();
47 double w
= rRectNode
.getWidth().getNumber();
48 double h
= rRectNode
.getHeight().getNumber();
51 = std::make_shared
<gfx::DrawRectangle
>(basegfx::B2DRange(x
, y
, x
+ w
, y
+ h
));
52 pRectangle
->mnRx
= rRectNode
.getRx().getNumber();
53 pRectangle
->mnRy
= rRectNode
.getRy().getNumber();
55 pRectangle
->mnStrokeWidth
56 = rRectNode
.getSvgStyleAttributes()->getStrokeWidth().getNumber();
58 pRectangle
->mnOpacity
= rRectNode
.getSvgStyleAttributes()->getOpacity().getNumber();
60 const basegfx::BColor
* pFillColor
= rRectNode
.getSvgStyleAttributes()->getFill();
61 const SvgGradientNode
* pFillGradient
62 = rRectNode
.getSvgStyleAttributes()->getSvgGradientNodeFill();
65 pRectangle
->mpFillColor
= std::make_shared
<basegfx::BColor
>(*pFillColor
);
67 else if (pFillGradient
)
69 drawinglayer::primitive2d::SvgGradientEntryVector aSvgGradientEntryVector
;
70 pFillGradient
->collectGradientEntries(aSvgGradientEntryVector
);
71 if (!aSvgGradientEntryVector
.empty())
73 auto aGradientInfo
= std::make_shared
<gfx::LinearGradientInfo
>();
75 aGradientInfo
->x1
= pFillGradient
->getX1().getNumber();
76 aGradientInfo
->y1
= pFillGradient
->getY1().getNumber();
77 aGradientInfo
->x2
= pFillGradient
->getX2().getNumber();
78 aGradientInfo
->y2
= pFillGradient
->getY2().getNumber();
80 const basegfx::B2DHomMatrix
* pGradientTransform
81 = pFillGradient
->getGradientTransform();
82 if (pGradientTransform
)
84 aGradientInfo
->maMatrix
= *pGradientTransform
;
87 pRectangle
->mpFillGradient
= aGradientInfo
;
89 for (auto const& rEntry
: aSvgGradientEntryVector
)
91 gfx::GradientStop aStop
;
92 aStop
.maColor
= rEntry
.getColor();
93 aStop
.mfOffset
= rEntry
.getOffset();
94 aStop
.mfOpacity
= rEntry
.getOpacity();
95 pRectangle
->mpFillGradient
->maGradientStops
.push_back(aStop
);
100 const basegfx::BColor
* pStrokeColor
= rRectNode
.getSvgStyleAttributes()->getStroke();
102 pRectangle
->mpStrokeColor
= std::make_shared
<basegfx::BColor
>(*pStrokeColor
);
104 mpCurrent
->maChildren
.push_back(pRectangle
);
107 case svgio::svgreader::SVGToken::Path
:
109 auto const& rPathNode
= static_cast<svgio::svgreader::SvgPathNode
const&>(rNode
);
111 auto pPath
= rPathNode
.getPath();
114 auto pDrawPath
= std::make_shared
<gfx::DrawPath
>(*pPath
);
116 pDrawPath
->mnStrokeWidth
117 = rPathNode
.getSvgStyleAttributes()->getStrokeWidth().getNumber();
119 pDrawPath
->mnOpacity
= rPathNode
.getSvgStyleAttributes()->getOpacity().getNumber();
121 const basegfx::BColor
* pFillColor
= rPathNode
.getSvgStyleAttributes()->getFill();
123 pDrawPath
->mpFillColor
= std::make_shared
<basegfx::BColor
>(*pFillColor
);
125 const basegfx::BColor
* pStrokeColor
126 = rPathNode
.getSvgStyleAttributes()->getStroke();
128 pDrawPath
->mpStrokeColor
= std::make_shared
<basegfx::BColor
>(*pStrokeColor
);
130 mpCurrent
->maChildren
.push_back(pDrawPath
);
141 void SvgDrawVisitor::goToChildren(svgio::svgreader::SvgNode
const& rNode
)
143 for (auto& rChild
: rNode
.getChildren())
145 rChild
->accept(*this);
148 } // end of namespace svgio::svgreader
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */