-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.cpp
235 lines (187 loc) · 5.43 KB
/
svg.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "svg.h"
namespace svg {
using namespace std::literals;
void Object::Render(const RenderContext& context) const {
context.RenderIndent();
// Делегируем вывод тега своим подклассам
RenderObject(context);
context.out << std::endl;
}
// ---------- Circle ------------------
Circle& Circle::SetCenter(Point center) {
center_ = center;
return *this;
}
Circle& Circle::SetRadius(double radius) {
radius_ = radius;
return *this;
}
void Circle::RenderObject(const RenderContext& context) const {
auto& out = context.out;
out << "<circle cx=\""sv << center_.x << "\" cy=\""sv << center_.y << "\" "sv;
out << "r=\""sv << radius_ << "\" "sv;
RenderAttrs(out);
out << "/>"sv;
}
// ---------- Polyline ------------------
Polyline& Polyline::AddPoint(Point point) {
points_.push_back(std::move(point));
return *this;
}
void Polyline::RenderObject(const RenderContext& context) const {
auto& out = context.out;
out << "<polyline points=\""sv;
for (size_t i = 0; i < points_.size(); ++i) {
if (i != 0) {
out << " "sv;
}
const Point& point = points_.at(i);
out << point.x << ","sv << point.y;
}
out << "\""sv;
RenderAttrs(out);
out << "/>"sv;
}
// ---------- Text ------------------
Text& Text::SetPosition(Point pos) {
pos_ = pos;
return *this;
}
Text& Text::SetOffset(Point offset) {
offset_ = offset;
return *this;
}
Text& Text::SetFontSize(uint32_t size) {
font_size_ = size;
return *this;
}
Text& Text::SetFontFamily(const std::string& font_family) {
font_family_ = font_family;
return *this;
}
Text& Text::SetFontWeight(const std::string& font_weight) {
font_weight_ = font_weight;
return *this;
}
Text& Text::SetData(const std::string& data) {
data_ = data;
return *this;
}
void Text::PrintText(const RenderContext& context) const {
auto& out = context.out;
for (char c : data_) {
switch (c) {
case '\"':
out << """;
break;
case '\'':
out << "'";
break;
case '<':
out << "<";
break;
case '>':
out << ">";
break;
case '&':
out << "&";
break;
default:
out << c;
}
}
}
void Text::RenderObject(const RenderContext& context) const {
auto& out = context.out;
out << "<text";
RenderAttrs(out);
out << " x=\"" << pos_.x << "\"";
out << " y=\"" << pos_.y << "\"";
out << " dx=\"" << offset_.x << "\"";
out << " dy=\"" << offset_.y << "\"";
out << " font-size=\"" << font_size_ << "\"";
if (font_family_ != "") {
out << " font-family=\"" << font_family_ << "\"";
}
if (font_weight_ != "") {
out << " font-weight=\"" << font_weight_ << "\"";
}
out << ">";
Text::PrintText(context);
out << "</text>";
}
// ---------- Document ------------------
void Document::AddPtr(std::unique_ptr<Object>&& obj) {
items_.push_back(std::move(obj));
}
void Document::Render(std::ostream& out) const {
RenderContext render_context(out);
out << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"sv << std::endl;
out << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">"sv << std::endl;
for (const auto& ptr : items_) {
out << " ";
ptr.get()->Render(render_context);
}
out << "</svg>"sv;
}
void Document::Clear() {
items_.clear();
}
} // namespace svg
namespace shapes {
// ---------- Triangle ------------------
Triangle::Triangle(svg::Point p1, svg::Point p2, svg::Point p3)
: p1_(p1)
, p2_(p2)
, p3_(p3) {
}
// Реализует метод Draw интерфейса svg::Drawable
void Triangle::Draw(svg::ObjectContainer& container) const {
container.Add(svg::Polyline().AddPoint(p1_).AddPoint(p2_).AddPoint(p3_).AddPoint(p1_));
}
// ---------- Star ------------------
Star::Star(svg::Point center, double outer_rad, double inner_rad, int num_rays) {
polyline_ = CreateStar(center, outer_rad, inner_rad, num_rays);
}
void Star::Draw(svg::ObjectContainer& container) const {
container.Add(polyline_);
}
svg::Polyline Star::CreateStar(svg::Point center, double outer_rad, double inner_rad, int num_rays) {
using namespace svg;
Polyline polyline;
for (int i = 0; i <= num_rays; ++i) {
double angle = 2 * M_PI * (i % num_rays) / num_rays;
polyline.AddPoint({ center.x + outer_rad * sin(angle), center.y - outer_rad * cos(angle) });
if (i == num_rays) {
break;
}
angle += M_PI / num_rays;
polyline.AddPoint({ center.x + inner_rad * sin(angle), center.y - inner_rad * cos(angle) });
}
polyline.SetFillColor("red").SetStrokeColor("black");
return polyline;
}
// ---------- Snowman ------------------
Snowman::Snowman(svg::Point head_center, double head_radius) {
head_center_ = head_center;
head_radius_ = head_radius;
}
void Snowman::Draw(svg::ObjectContainer& container) const {
using namespace svg;
Circle circle1;
circle1.SetCenter(head_center_);
circle1.SetRadius(head_radius_);
circle1.SetFillColor("rgb(240,240,240)").SetStrokeColor("black");
Circle circle2;
circle2.SetCenter({ head_center_.x, head_center_.y + head_radius_ * 2 });
circle2.SetRadius(head_radius_ * 1.5);
circle2.SetFillColor("rgb(240,240,240)").SetStrokeColor("black");
Circle circle3;
circle3.SetCenter({ head_center_.x, head_center_.y + head_radius_ * 5 });
circle3.SetRadius(head_radius_ * 2);
circle3.SetFillColor("rgb(240,240,240)").SetStrokeColor("black");
container.Add(circle3);
container.Add(circle2);
container.Add(circle1);
}
} //namespace shapes