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
|
#include "cru/platform/graphics/cairo/CairoGeometry.h"
#include "cru/platform/graphics/Geometry.h"
#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
#include <cairo/cairo.h>
namespace cru::platform::graphics::cairo {
CairoGeometry::CairoGeometry(CairoGraphicsFactory* factory,
cairo_path_t* cairo_path, const Matrix& transform,
bool auto_destroy)
: CairoResource(factory),
cairo_path_(cairo_path),
transform_(transform),
auto_destroy_(auto_destroy) {
Expects(cairo_path);
}
CairoGeometry::~CairoGeometry() {
if (auto_destroy_) {
cairo_path_destroy(cairo_path_);
cairo_path_ = nullptr;
}
}
bool CairoGeometry::StrokeContains(float width, const Point& point) {
auto cairo = GetCairoGraphicsFactory()->GetDefaultCairo();
cairo_save(cairo);
auto matrix = Convert(transform_);
cairo_transform(cairo, &matrix);
cairo_new_path(cairo);
cairo_append_path(cairo, cairo_path_);
cairo_set_line_width(cairo, width);
auto result = cairo_in_stroke(cairo, point.x, point.y);
cairo_restore(cairo);
return result;
}
bool CairoGeometry::FillContains(const Point& point) {
auto cairo = GetCairoGraphicsFactory()->GetDefaultCairo();
cairo_save(cairo);
auto matrix = Convert(transform_);
cairo_transform(cairo, &matrix);
cairo_new_path(cairo);
cairo_append_path(cairo, cairo_path_);
auto result = cairo_in_fill(cairo, point.x, point.y);
cairo_restore(cairo);
return result;
}
Rect CairoGeometry::GetBounds() {
auto cairo = GetCairoGraphicsFactory()->GetDefaultCairo();
cairo_save(cairo);
auto matrix = Convert(transform_);
cairo_transform(cairo, &matrix);
cairo_new_path(cairo);
cairo_append_path(cairo, cairo_path_);
double l, t, r, b;
cairo_path_extents(cairo, &l, &t, &r, &b);
cairo_restore(cairo);
return Rect::FromVertices(l, t, r, b);
}
std::unique_ptr<IGeometry> CairoGeometry::Transform(const Matrix& matrix) {
auto cairo = GetCairoGraphicsFactory()->GetDefaultCairo();
cairo_save(cairo);
cairo_new_path(cairo);
cairo_append_path(cairo, cairo_path_);
auto path = cairo_copy_path(cairo);
cairo_restore(cairo);
return std::unique_ptr<IGeometry>(new CairoGeometry(
GetCairoGraphicsFactory(), path, transform_ * matrix, true));
}
std::unique_ptr<IGeometry> CairoGeometry::CreateStrokeGeometry(float width) {
throw Exception(u"Not implemented");
}
CairoGeometryBuilder::CairoGeometryBuilder(CairoGraphicsFactory* factory)
: CairoResource(factory) {
surface_ = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, nullptr);
cairo_ = cairo_create(surface_);
cairo_new_path(cairo_);
cairo_move_to(cairo_, 0.0, 0.0);
}
CairoGeometryBuilder::~CairoGeometryBuilder() {
cairo_destroy(cairo_);
cairo_surface_destroy(surface_);
}
Point CairoGeometryBuilder::GetCurrentPosition() {
double x, y;
cairo_get_current_point(cairo_, &x, &y);
return Point(x, y);
}
void CairoGeometryBuilder::MoveTo(const Point& point) {
cairo_move_to(cairo_, point.x, point.y);
}
void CairoGeometryBuilder::LineTo(const Point& point) {
cairo_line_to(cairo_, point.x, point.y);
}
void CairoGeometryBuilder::CubicBezierTo(const Point& start_control_point,
const Point& end_control_point,
const Point& end_point) {
cairo_curve_to(cairo_, start_control_point.x, start_control_point.y,
end_control_point.x, end_control_point.y, end_point.x,
end_point.y);
}
void CairoGeometryBuilder::QuadraticBezierTo(const Point& control_point,
const Point& end_point) {
CubicBezierTo(control_point, control_point, end_point);
}
void CairoGeometryBuilder::CloseFigure(bool close) {
if (close) cairo_close_path(cairo_);
}
std::unique_ptr<IGeometry> CairoGeometryBuilder::Build() {
cairo_path_t* path = cairo_copy_path(cairo_);
return std::unique_ptr<IGeometry>(new CairoGeometry(
GetCairoGraphicsFactory(), path, Matrix::Identity(), true));
}
} // namespace cru::platform::graphics::cairo
|