aboutsummaryrefslogtreecommitdiff
path: root/src/platform/graphics/cairo/CairoPainter.cpp
blob: 00c8187ce5c4645a7c095b7977fb67946e8fe7c6 (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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "cru/platform/graphics/cairo/CairoPainter.h"
#include "cru/common/Exception.h"
#include "cru/platform/Check.h"
#include "cru/platform/Exception.h"
#include "cru/platform/graphics/cairo/Base.h"
#include "cru/platform/graphics/cairo/CairoBrush.h"
#include "cru/platform/graphics/cairo/CairoGeometry.h"
#include "cru/platform/graphics/cairo/CairoImage.h"
#include "cru/platform/graphics/cairo/CairoResource.h"
#include "cru/platform/graphics/cairo/PangoTextLayout.h"

#include <pango/pangocairo.h>

namespace cru::platform::graphics::cairo {
CairoPainter::CairoPainter(CairoGraphicsFactory* factory, cairo_t* cairo,
                           bool auto_release, cairo_surface_t* cairo_surface)
    : CairoResource(factory),
      cairo_(cairo),
      auto_release_(auto_release),
      cairo_surface_(cairo_surface) {}

CairoPainter::~CairoPainter() {
  if (auto_release_) {
    cairo_destroy(cairo_);
  }
}

Matrix CairoPainter::GetTransform() {
  CheckValidation();
  cairo_matrix_t matrix;
  cairo_get_matrix(cairo_, &matrix);
  return Convert(matrix);
}

void CairoPainter::SetTransform(const Matrix& matrix) {
  CheckValidation();
  auto m = Convert(matrix);
  cairo_set_matrix(cairo_, &m);
}

void CairoPainter::ConcatTransform(const Matrix& matrix) {
  CheckValidation();
  auto m = Convert(matrix);
  cairo_transform(cairo_, &m);
}

void CairoPainter::Clear(const Color& color) {
  CheckValidation();
  cairo_set_source_rgba(cairo_, color.GetFloatRed(), color.GetFloatGreen(),
                        color.GetFloatBlue(), color.GetFloatAlpha());
  cairo_paint(cairo_);
}

void CairoPainter::DrawLine(const Point& start, const Point& end, IBrush* brush,
                            float width) {
  CheckValidation();
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
  auto cairo_pattern = cairo_brush->GetCairoPattern();
  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  cairo_set_line_width(cairo_, width);
  cairo_new_path(cairo_);
  cairo_move_to(cairo_, start.x, start.y);
  cairo_line_to(cairo_, end.x, end.y);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush,
                                   float width) {
  CheckValidation();
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
  auto cairo_pattern = cairo_brush->GetCairoPattern();
  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  cairo_set_line_width(cairo_, width);
  cairo_new_path(cairo_);
  cairo_rectangle(cairo_, rectangle.left, rectangle.top, rectangle.width,
                  rectangle.height);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::FillRectangle(const Rect& rectangle, IBrush* brush) {
  CheckValidation();
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
  auto cairo_pattern = cairo_brush->GetCairoPattern();
  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  cairo_new_path(cairo_);
  cairo_rectangle(cairo_, rectangle.left, rectangle.top, rectangle.width,
                  rectangle.height);
  cairo_fill(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::StrokeEllipse(const Rect& outline_rect, IBrush* brush,
                                 float width) {
  CheckValidation();
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
  auto cairo_pattern = cairo_brush->GetCairoPattern();
  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  cairo_set_line_width(cairo_, width);
  {
    auto center = outline_rect.GetCenter();
    cairo_matrix_t save_matrix;
    cairo_get_matrix(cairo_, &save_matrix);
    cairo_translate(cairo_, center.x, center.y);
    cairo_scale(cairo_, 1, outline_rect.height / outline_rect.width);
    cairo_translate(cairo_, -center.x, -center.y);
    cairo_new_path(cairo_);
    cairo_arc(cairo_, center.x, center.y, outline_rect.width / 2.0, 0,
              2 * M_PI);
    cairo_set_matrix(cairo_, &save_matrix);
  }
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::FillEllipse(const Rect& outline_rect, IBrush* brush) {
  CheckValidation();
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
  auto cairo_pattern = cairo_brush->GetCairoPattern();
  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  {
    auto center = outline_rect.GetCenter();
    cairo_matrix_t save_matrix;
    cairo_get_matrix(cairo_, &save_matrix);
    cairo_translate(cairo_, center.x, center.y);
    cairo_scale(cairo_, 1, outline_rect.height / outline_rect.width);
    cairo_translate(cairo_, -center.x, -center.y);
    cairo_new_path(cairo_);
    cairo_arc(cairo_, center.x, center.y, outline_rect.width / 2.0, 0,
              2 * M_PI);
    cairo_set_matrix(cairo_, &save_matrix);
  }
  cairo_fill(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush,
                                  float width) {
  CheckValidation();
  auto cairo_geometry = CheckPlatform<CairoGeometry>(geometry, GetPlatformId());
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());

  auto cairo_path = cairo_geometry->GetCairoPath();
  auto cairo_pattern = cairo_brush->GetCairoPattern();

  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  cairo_set_line_width(cairo_, width);
  cairo_new_path(cairo_);
  cairo_append_path(cairo_, cairo_path);
  cairo_stroke(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::FillGeometry(IGeometry* geometry, IBrush* brush) {
  CheckValidation();
  auto cairo_geometry = CheckPlatform<CairoGeometry>(geometry, GetPlatformId());
  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());

  auto cairo_path = cairo_geometry->GetCairoPath();
  auto cairo_pattern = cairo_brush->GetCairoPattern();

  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  cairo_new_path(cairo_);
  cairo_append_path(cairo_, cairo_path);
  cairo_fill(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::DrawText(const Point& offset, ITextLayout* text_layout,
                            IBrush* brush) {
  CheckValidation();

  auto pango_text_layout =
      CheckPlatform<PangoTextLayout>(text_layout, GetPlatformId());

  auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
  auto cairo_pattern = cairo_brush->GetCairoPattern();

  cairo_save(cairo_);
  cairo_set_source(cairo_, cairo_pattern);
  pango_cairo_show_layout(cairo_, pango_text_layout->GetPangoLayout());
  cairo_restore(cairo_);
}

void CairoPainter::DrawImage(const Point& offset, IImage* image) {
  CheckValidation();
  auto cairo_image = CheckPlatform<CairoImage>(image, GetPlatformId());
  cairo_save(cairo_);
  cairo_set_source_surface(cairo_, cairo_image->GetCairoSurface(), 0, 0);
  cairo_new_path(cairo_);
  cairo_rectangle(cairo_, offset.x, offset.y, image->GetWidth(),
                  image->GetHeight());
  cairo_fill(cairo_);
  cairo_restore(cairo_);
}

void CairoPainter::PushLayer(const Rect& bounds) {
  CheckValidation();
  layer_stack_.push_back(bounds);
  cairo_reset_clip(cairo_);
  cairo_new_path(cairo_);
  cairo_rectangle(cairo_, bounds.left, bounds.top, bounds.width, bounds.height);
  cairo_clip(cairo_);
}

void CairoPainter::PopLayer() {
  CheckValidation();
  layer_stack_.pop_back();
  cairo_reset_clip(cairo_);
  if (!layer_stack_.empty()) {
    auto clip = layer_stack_.back();
    cairo_new_path(cairo_);
    cairo_rectangle(cairo_, clip.left, clip.top, clip.width, clip.height);
    cairo_clip(cairo_);
  }
}

void CairoPainter::PushState() {
  CheckValidation();
  cairo_save(cairo_);
}

void CairoPainter::PopState() {
  CheckValidation();
  cairo_restore(cairo_);
}

void CairoPainter::EndDraw() {
  if (cairo_surface_ != nullptr) {
    cairo_surface_flush(cairo_surface_);
  }
  valid_ = false;
}

void CairoPainter::CheckValidation() {
  if (!valid_) {
    throw ReuseException(u"Painter already ended drawing.");
  }
}
}  // namespace cru::platform::graphics::cairo