forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpath.cc
More file actions
475 lines (407 loc) · 13.1 KB
/
path.cc
File metadata and controls
475 lines (407 loc) · 13.1 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "impeller/geometry/path.h"
#include <optional>
#include <variant>
#include "impeller/geometry/path_component.h"
namespace impeller {
Path::Path() {
AddContourComponent({});
};
Path::~Path() = default;
std::tuple<size_t, size_t> Path::Polyline::GetContourPointBounds(
size_t contour_index) const {
if (contour_index >= contours.size()) {
return {points.size(), points.size()};
}
const size_t start_index = contours.at(contour_index).start_index;
const size_t end_index = (contour_index >= contours.size() - 1)
? points.size()
: contours.at(contour_index + 1).start_index;
return std::make_tuple(start_index, end_index);
}
size_t Path::GetComponentCount(std::optional<ComponentType> type) const {
if (type.has_value()) {
switch (type.value()) {
case ComponentType::kLinear:
return linears_.size();
case ComponentType::kQuadratic:
return quads_.size();
case ComponentType::kCubic:
return cubics_.size();
case ComponentType::kContour:
return contours_.size();
}
}
return components_.size();
}
void Path::SetFillType(FillType fill) {
fill_ = fill;
}
FillType Path::GetFillType() const {
return fill_;
}
bool Path::IsConvex() const {
return convexity_ == Convexity::kConvex;
}
void Path::SetConvexity(Convexity value) {
convexity_ = value;
}
void Path::Shift(Point shift) {
size_t currentIndex = 0;
for (const auto& component : components_) {
switch (component.type) {
case ComponentType::kLinear:
linears_[component.index].p1 += shift;
linears_[component.index].p2 += shift;
break;
case ComponentType::kQuadratic:
quads_[component.index].cp += shift;
quads_[component.index].p1 += shift;
quads_[component.index].p2 += shift;
break;
case ComponentType::kCubic:
cubics_[component.index].cp1 += shift;
cubics_[component.index].cp2 += shift;
cubics_[component.index].p1 += shift;
cubics_[component.index].p2 += shift;
break;
case ComponentType::kContour:
contours_[component.index].destination += shift;
break;
}
currentIndex++;
}
}
Path& Path::AddLinearComponent(Point p1, Point p2) {
linears_.emplace_back(p1, p2);
components_.emplace_back(ComponentType::kLinear, linears_.size() - 1);
return *this;
}
Path& Path::AddQuadraticComponent(Point p1, Point cp, Point p2) {
quads_.emplace_back(p1, cp, p2);
components_.emplace_back(ComponentType::kQuadratic, quads_.size() - 1);
return *this;
}
Path& Path::AddCubicComponent(Point p1, Point cp1, Point cp2, Point p2) {
cubics_.emplace_back(p1, cp1, cp2, p2);
components_.emplace_back(ComponentType::kCubic, cubics_.size() - 1);
return *this;
}
Path& Path::AddContourComponent(Point destination, bool is_closed) {
if (components_.size() > 0 &&
components_.back().type == ComponentType::kContour) {
// Never insert contiguous contours.
contours_.back() = ContourComponent(destination, is_closed);
} else {
contours_.emplace_back(ContourComponent(destination, is_closed));
components_.emplace_back(ComponentType::kContour, contours_.size() - 1);
}
return *this;
}
void Path::SetContourClosed(bool is_closed) {
contours_.back().is_closed = is_closed;
}
void Path::EnumerateComponents(
const Applier<LinearPathComponent>& linear_applier,
const Applier<QuadraticPathComponent>& quad_applier,
const Applier<CubicPathComponent>& cubic_applier,
const Applier<ContourComponent>& contour_applier) const {
size_t currentIndex = 0;
for (const auto& component : components_) {
switch (component.type) {
case ComponentType::kLinear:
if (linear_applier) {
linear_applier(currentIndex, linears_[component.index]);
}
break;
case ComponentType::kQuadratic:
if (quad_applier) {
quad_applier(currentIndex, quads_[component.index]);
}
break;
case ComponentType::kCubic:
if (cubic_applier) {
cubic_applier(currentIndex, cubics_[component.index]);
}
break;
case ComponentType::kContour:
if (contour_applier) {
contour_applier(currentIndex, contours_[component.index]);
}
break;
}
currentIndex++;
}
}
bool Path::GetLinearComponentAtIndex(size_t index,
LinearPathComponent& linear) const {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kLinear) {
return false;
}
linear = linears_[components_[index].index];
return true;
}
bool Path::GetQuadraticComponentAtIndex(
size_t index,
QuadraticPathComponent& quadratic) const {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kQuadratic) {
return false;
}
quadratic = quads_[components_[index].index];
return true;
}
bool Path::GetCubicComponentAtIndex(size_t index,
CubicPathComponent& cubic) const {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kCubic) {
return false;
}
cubic = cubics_[components_[index].index];
return true;
}
bool Path::GetContourComponentAtIndex(size_t index,
ContourComponent& move) const {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kContour) {
return false;
}
move = contours_[components_[index].index];
return true;
}
bool Path::UpdateLinearComponentAtIndex(size_t index,
const LinearPathComponent& linear) {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kLinear) {
return false;
}
linears_[components_[index].index] = linear;
return true;
}
bool Path::UpdateQuadraticComponentAtIndex(
size_t index,
const QuadraticPathComponent& quadratic) {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kQuadratic) {
return false;
}
quads_[components_[index].index] = quadratic;
return true;
}
bool Path::UpdateCubicComponentAtIndex(size_t index,
CubicPathComponent& cubic) {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kCubic) {
return false;
}
cubics_[components_[index].index] = cubic;
return true;
}
bool Path::UpdateContourComponentAtIndex(size_t index,
const ContourComponent& move) {
if (index >= components_.size()) {
return false;
}
if (components_[index].type != ComponentType::kContour) {
return false;
}
contours_[components_[index].index] = move;
return true;
}
Path::Polyline::Polyline(std::vector<Point>& point_buffer)
: points(point_buffer) {}
Path::Polyline Path::CreatePolyline(Scalar scale,
std::vector<Point>& point_buffer) const {
Polyline polyline(point_buffer);
auto get_path_component = [this](size_t component_i) -> PathComponentVariant {
if (component_i >= components_.size()) {
return std::monostate{};
}
const auto& component = components_[component_i];
switch (component.type) {
case ComponentType::kLinear:
return &linears_[component.index];
case ComponentType::kQuadratic:
return &quads_[component.index];
case ComponentType::kCubic:
return &cubics_[component.index];
case ComponentType::kContour:
return std::monostate{};
}
};
auto compute_contour_start_direction =
[&get_path_component](size_t current_path_component_index) {
size_t next_component_index = current_path_component_index + 1;
while (!std::holds_alternative<std::monostate>(
get_path_component(next_component_index))) {
auto next_component = get_path_component(next_component_index);
auto maybe_vector =
std::visit(PathComponentStartDirectionVisitor(), next_component);
if (maybe_vector.has_value()) {
return maybe_vector.value();
} else {
next_component_index++;
}
}
return Vector2(0, -1);
};
std::vector<PolylineContour::Component> components;
std::optional<size_t> previous_path_component_index;
auto end_contour = [&polyline, &previous_path_component_index,
&get_path_component, &components]() {
// Whenever a contour has ended, extract the exact end direction from
// the last component.
if (polyline.contours.empty()) {
return;
}
if (!previous_path_component_index.has_value()) {
return;
}
auto& contour = polyline.contours.back();
contour.end_direction = Vector2(0, 1);
contour.components = components;
components.clear();
size_t previous_index = previous_path_component_index.value();
while (!std::holds_alternative<std::monostate>(
get_path_component(previous_index))) {
auto previous_component = get_path_component(previous_index);
auto maybe_vector =
std::visit(PathComponentEndDirectionVisitor(), previous_component);
if (maybe_vector.has_value()) {
contour.end_direction = maybe_vector.value();
break;
} else {
if (previous_index == 0) {
break;
}
previous_index--;
}
}
};
for (size_t component_i = 0; component_i < components_.size();
component_i++) {
const auto& component = components_[component_i];
switch (component.type) {
case ComponentType::kLinear:
components.push_back({
.component_start_index = polyline.points.size() - 1,
.is_curve = false,
});
linears_[component.index].AppendPolylinePoints(polyline.points);
previous_path_component_index = component_i;
break;
case ComponentType::kQuadratic:
components.push_back({
.component_start_index = polyline.points.size() - 1,
.is_curve = true,
});
quads_[component.index].AppendPolylinePoints(scale, polyline.points);
previous_path_component_index = component_i;
break;
case ComponentType::kCubic:
components.push_back({
.component_start_index = polyline.points.size() - 1,
.is_curve = true,
});
cubics_[component.index].AppendPolylinePoints(scale, polyline.points);
previous_path_component_index = component_i;
break;
case ComponentType::kContour:
if (component_i == components_.size() - 1) {
// If the last component is a contour, that means it's an empty
// contour, so skip it.
continue;
}
end_contour();
Vector2 start_direction = compute_contour_start_direction(component_i);
const auto& contour = contours_[component.index];
polyline.contours.push_back({.start_index = polyline.points.size(),
.is_closed = contour.is_closed,
.start_direction = start_direction,
.components = components});
polyline.points.push_back(contour.destination);
break;
}
}
end_contour();
return polyline;
}
std::optional<Rect> Path::GetBoundingBox() const {
return computed_bounds_;
}
void Path::ComputeBounds() {
auto min_max = GetMinMaxCoveragePoints();
if (!min_max.has_value()) {
computed_bounds_ = std::nullopt;
return;
}
auto min = min_max->first;
auto max = min_max->second;
const auto difference = max - min;
computed_bounds_ = Rect::MakeXYWH(min.x, min.y, difference.x, difference.y);
}
std::optional<Rect> Path::GetTransformedBoundingBox(
const Matrix& transform) const {
auto bounds = GetBoundingBox();
if (!bounds.has_value()) {
return std::nullopt;
}
return bounds->TransformBounds(transform);
}
std::optional<std::pair<Point, Point>> Path::GetMinMaxCoveragePoints() const {
if (linears_.empty() && quads_.empty() && cubics_.empty()) {
return std::nullopt;
}
std::optional<Point> min, max;
auto clamp = [&min, &max](const Point& point) {
if (min.has_value()) {
min = min->Min(point);
} else {
min = point;
}
if (max.has_value()) {
max = max->Max(point);
} else {
max = point;
}
};
for (const auto& linear : linears_) {
clamp(linear.p1);
clamp(linear.p2);
}
for (const auto& quad : quads_) {
for (const Point& point : quad.Extrema()) {
clamp(point);
}
}
for (const auto& cubic : cubics_) {
for (const Point& point : cubic.Extrema()) {
clamp(point);
}
}
if (!min.has_value() || !max.has_value()) {
return std::nullopt;
}
return std::make_pair(min.value(), max.value());
}
void Path::SetBounds(Rect rect) {
computed_bounds_ = rect;
}
} // namespace impeller