blob: 8ad5395f1ce3386d3dd0aea30b5460894fa0a87c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#include <sal/config.h>
#include <svgrectnode.hxx>
#include <svgsvgnode.hxx>
#include <svgpathnode.hxx>
#include <svggradientnode.hxx>
#include <svgvisitor.hxx>
namespace svgio::svgreader
{
SvgDrawVisitor::SvgDrawVisitor()
: mpDrawRoot(std::make_shared<gfx::DrawRoot>())
, mpCurrent(mpDrawRoot)
{
}
void SvgDrawVisitor::visit(svgio::svgreader::SvgNode const& rNode)
{
switch (rNode.getType())
{
case svgio::svgreader::SVGToken::Svg:
{
auto const& rSvgNode = static_cast<svgio::svgreader::SvgSvgNode const&>(rNode);
basegfx::B2DRange aRange = rSvgNode.getCurrentViewPort();
static_cast<gfx::DrawRoot*>(mpCurrent.get())->maRectangle = aRange;
}
break;
case svgio::svgreader::SVGToken::Rect:
{
auto const& rRectNode = static_cast<svgio::svgreader::SvgRectNode const&>(rNode);
double x = rRectNode.getX().getNumber();
double y = rRectNode.getY().getNumber();
double w = rRectNode.getWidth().getNumber();
double h = rRectNode.getHeight().getNumber();
auto pRectangle
= std::make_shared<gfx::DrawRectangle>(basegfx::B2DRange(x, y, x + w, y + h));
pRectangle->mnRx = rRectNode.getRx().getNumber();
pRectangle->mnRy = rRectNode.getRy().getNumber();
pRectangle->mnStrokeWidth
= rRectNode.getSvgStyleAttributes()->getStrokeWidth().getNumber();
pRectangle->mnOpacity = rRectNode.getSvgStyleAttributes()->getOpacity().getNumber();
const basegfx::BColor* pFillColor = rRectNode.getSvgStyleAttributes()->getFill();
const SvgGradientNode* pFillGradient
= rRectNode.getSvgStyleAttributes()->getSvgGradientNodeFill();
if (pFillColor)
{
pRectangle->mpFillColor = std::make_shared<basegfx::BColor>(*pFillColor);
}
else if (pFillGradient)
{
drawinglayer::primitive2d::SvgGradientEntryVector aSvgGradientEntryVector;
pFillGradient->collectGradientEntries(aSvgGradientEntryVector);
if (!aSvgGradientEntryVector.empty())
{
auto aGradientInfo = std::make_shared<gfx::LinearGradientInfo>();
aGradientInfo->x1 = pFillGradient->getX1().getNumber();
aGradientInfo->y1 = pFillGradient->getY1().getNumber();
aGradientInfo->x2 = pFillGradient->getX2().getNumber();
aGradientInfo->y2 = pFillGradient->getY2().getNumber();
std::optional<basegfx::B2DHomMatrix> pGradientTransform
= pFillGradient->getGradientTransform();
if (pGradientTransform)
{
aGradientInfo->maMatrix = *pGradientTransform;
}
pRectangle->mpFillGradient = aGradientInfo;
for (auto const& rEntry : aSvgGradientEntryVector)
{
gfx::GradientStop aStop;
aStop.maColor = rEntry.getColor();
aStop.mfOffset = rEntry.getOffset();
aStop.mfOpacity = rEntry.getOpacity();
pRectangle->mpFillGradient->maGradientStops.push_back(aStop);
}
}
}
const basegfx::BColor* pStrokeColor = rRectNode.getSvgStyleAttributes()->getStroke();
if (pStrokeColor)
pRectangle->mpStrokeColor = std::make_shared<basegfx::BColor>(*pStrokeColor);
mpCurrent->maChildren.push_back(pRectangle);
}
break;
case svgio::svgreader::SVGToken::Path:
{
auto const& rPathNode = static_cast<svgio::svgreader::SvgPathNode const&>(rNode);
auto pPath = rPathNode.getPath();
if (pPath)
{
auto pDrawPath = std::make_shared<gfx::DrawPath>(*pPath);
pDrawPath->mnStrokeWidth
= rPathNode.getSvgStyleAttributes()->getStrokeWidth().getNumber();
pDrawPath->mnOpacity = rPathNode.getSvgStyleAttributes()->getOpacity().getNumber();
const basegfx::BColor* pFillColor = rPathNode.getSvgStyleAttributes()->getFill();
if (pFillColor)
pDrawPath->mpFillColor = std::make_shared<basegfx::BColor>(*pFillColor);
const basegfx::BColor* pStrokeColor
= rPathNode.getSvgStyleAttributes()->getStroke();
if (pStrokeColor)
pDrawPath->mpStrokeColor = std::make_shared<basegfx::BColor>(*pStrokeColor);
mpCurrent->maChildren.push_back(pDrawPath);
}
}
break;
default:
break;
}
goToChildren(rNode);
}
void SvgDrawVisitor::goToChildren(svgio::svgreader::SvgNode const& rNode)
{
for (auto& rChild : rNode.getChildren())
{
rChild->accept(*this);
}
}
} // end of namespace svgio::svgreader
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|