-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvg.h
More file actions
193 lines (158 loc) · 4.51 KB
/
Copy pathsvg.h
File metadata and controls
193 lines (158 loc) · 4.51 KB
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
#pragma once
#include <cstdint>
#include <variant>
#include <string>
#include <iostream>
#include <vector>
#include <memory>
#include <optional>
namespace Svg {
struct Point {
double x{0};
double y{0};
};
struct Rgb {
std::uint8_t red;
std::uint8_t green;
std::uint8_t blue;
};
struct Rgba {
std::uint8_t red;
std::uint8_t green;
std::uint8_t blue;
double alpha;
};
using Color = std::variant<std::monostate, Rgb, Rgba, std::string>;
const Color NoneColor{};
void RenderColor(std::ostream& out, std::monostate);
void RenderColor(std::ostream& out, const Rgb& rgb);
void RenderColor(std::ostream& out, const Rgba& rgb);
void RenderColor(std::ostream& out, const std::string& name);
void RenderColor(std::ostream& out, const Color& color);
class Shape {
public:
virtual void Render(std::ostream& out) const = 0;
virtual ~Shape() = default;
};
template <typename Owner>
class ShapeProperties {
public:
Owner& SetFillColor(const Color& fill);
Owner& SetStrokeColor(const Color& stroke);
Owner& SetStrokeWidth(double stroke_width);
Owner& SetStrokeLineCap(const std::string& stroke_linecap);
Owner& SetStrokeLineJoin(const std::string& stroke_linejoin);
Owner& SetOuterMargin(double outer_margin);
Owner& AsOwner();
void RenderProperties(std::ostream& out) const;
private:
Color fill_{NoneColor};
Color stroke_{NoneColor};
double stroke_width_{1.0};
std::optional<std::string> stroke_linecap_;
std::optional<std::string> stroke_linejoin_;
};
template <typename Owner>
Owner& ShapeProperties<Owner>::SetFillColor(const Color& fill) {
fill_ = fill;
return AsOwner();
}
template <typename Owner>
Owner& ShapeProperties<Owner>::SetStrokeColor(const Color& stroke) {
stroke_ = stroke;
return AsOwner();
}
template <typename Owner>
Owner& ShapeProperties<Owner>::SetStrokeWidth(double stroke_width) {
stroke_width_ = stroke_width;
return AsOwner();
}
template <typename Owner>
Owner& ShapeProperties<Owner>::SetStrokeLineCap(const std::string& stroke_linecap) {
stroke_linecap_ = stroke_linecap;
return AsOwner();
}
template <typename Owner>
Owner& ShapeProperties<Owner>::SetStrokeLineJoin(const std::string& stroke_linejoin) {
stroke_linejoin_ = stroke_linejoin;
return AsOwner();
}
template <typename Owner>
Owner& ShapeProperties<Owner>::AsOwner() {
return static_cast<Owner&>(*this);
}
template <typename Owner>
void ShapeProperties<Owner>::RenderProperties(std::ostream& out) const {
out << "fill" << "=" << "\"";
RenderColor(out, fill_);
out << "\"";
out << " " << "stroke" << "=" << "\"";
RenderColor(out, stroke_);
out << "\"";
out << " " << "stroke-width" << "=" << "\"" << stroke_width_ << "\"";
if (stroke_linecap_) {
out << " " << "stroke-linecap" << "=" << "\"" << stroke_linecap_.value() << "\"";
}
if (stroke_linejoin_) {
out << " " << "stroke-linejoin" << "=" << "\"" << stroke_linejoin_.value() << "\"";
}
}
class Rectangle : public Shape, public ShapeProperties<Rectangle> {
public:
Rectangle& SetBasePoint(Point p);
Rectangle& SetWidth(double w);
Rectangle& SetHeight(double h);
void Render(std::ostream& out) const;
private:
Point base_point_{0.0, 0.0};
double w_{1.0};
double h_{1.0};
};
class Circle : public Shape, public ShapeProperties<Circle> {
public:
Circle& SetCenter(Point center);
Circle& SetRadius(double r);
void Render(std::ostream& out) const;
private:
Point center_{0.0, 0.0};
double r_{1.0};
};
class Polyline : public Shape, public ShapeProperties<Polyline> {
public:
Polyline& AddPoint(Point point);
void Render(std::ostream& out) const;
private:
std::vector<Point> points_;
};
class Text : public Shape, public ShapeProperties<Text> {
public:
Text& SetPoint(Point coordinates);
Text& SetOffset(Point offset);
Text& SetFontSize(uint32_t font_size);
Text& SetFontFamily(const std::string& font_family);
Text& SetFontWeight(const std::string& font_weight);
Text& SetData(const std::string& data);
void Render(std::ostream& out) const;
private:
Point coordinates_;
Point offset_;
uint32_t font_size_;
std::optional<std::string> font_family_;
std::optional<std::string> font_weight_;
std::string data_;
};
class Document {
public:
explicit Document() {}
template <typename ShapeType>
void Add(ShapeType shape);
void Render(std::ostream& out) const;
std::string ToString() const;
private:
std::vector<std::unique_ptr<Shape>> shapes_;
};
template <typename ShapeType>
void Document::Add(ShapeType shape) {
shapes_.push_back(std::make_unique<ShapeType>(std::move(shape)));
}
} // namespace Svg