forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath_builder.cc
456 lines (384 loc) · 15 KB
/
path_builder.cc
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
// 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 "path_builder.h"
#include <cmath>
namespace impeller {
PathBuilder::PathBuilder() = default;
PathBuilder::~PathBuilder() = default;
Path PathBuilder::CopyPath(FillType fill) const {
auto path = prototype_;
path.SetFillType(fill);
return path;
}
Path PathBuilder::TakePath(FillType fill) {
auto path = prototype_;
path.SetFillType(fill);
path.SetConvexity(convexity_);
return path;
}
PathBuilder& PathBuilder::MoveTo(Point point, bool relative) {
current_ = relative ? current_ + point : point;
subpath_start_ = current_;
prototype_.AddContourComponent(current_);
return *this;
}
PathBuilder& PathBuilder::Close() {
LineTo(subpath_start_);
prototype_.SetContourClosed(true);
prototype_.AddContourComponent(current_);
return *this;
}
PathBuilder& PathBuilder::LineTo(Point point, bool relative) {
point = relative ? current_ + point : point;
prototype_.AddLinearComponent(current_, point);
current_ = point;
return *this;
}
PathBuilder& PathBuilder::HorizontalLineTo(Scalar x, bool relative) {
Point endpoint =
relative ? Point{current_.x + x, current_.y} : Point{x, current_.y};
prototype_.AddLinearComponent(current_, endpoint);
current_ = endpoint;
return *this;
}
PathBuilder& PathBuilder::VerticalLineTo(Scalar y, bool relative) {
Point endpoint =
relative ? Point{current_.x, current_.y + y} : Point{current_.x, y};
prototype_.AddLinearComponent(current_, endpoint);
current_ = endpoint;
return *this;
}
PathBuilder& PathBuilder::QuadraticCurveTo(Point controlPoint,
Point point,
bool relative) {
point = relative ? current_ + point : point;
controlPoint = relative ? current_ + controlPoint : controlPoint;
prototype_.AddQuadraticComponent(current_, controlPoint, point);
current_ = point;
return *this;
}
Point PathBuilder::ReflectedQuadraticControlPoint1() const {
/*
* If there is no previous command or if the previous command was not a
* quadratic, assume the control point is coincident with the current point.
*/
if (prototype_.GetComponentCount() == 0) {
return current_;
}
QuadraticPathComponent quad;
if (!prototype_.GetQuadraticComponentAtIndex(
prototype_.GetComponentCount() - 1, quad)) {
return current_;
}
/*
* The control point is assumed to be the reflection of the control point on
* the previous command relative to the current point.
*/
return (current_ * 2.0) - quad.cp;
}
PathBuilder& PathBuilder::SmoothQuadraticCurveTo(Point point, bool relative) {
point = relative ? current_ + point : point;
/*
* The reflected control point is absolute and we made the endpoint absolute
* too. So there the last argument is always false (i.e, not relative).
*/
QuadraticCurveTo(point, ReflectedQuadraticControlPoint1(), false);
return *this;
}
PathBuilder& PathBuilder::SetConvexity(Convexity value) {
convexity_ = value;
return *this;
}
PathBuilder& PathBuilder::CubicCurveTo(Point controlPoint1,
Point controlPoint2,
Point point,
bool relative) {
controlPoint1 = relative ? current_ + controlPoint1 : controlPoint1;
controlPoint2 = relative ? current_ + controlPoint2 : controlPoint2;
point = relative ? current_ + point : point;
prototype_.AddCubicComponent(current_, controlPoint1, controlPoint2, point);
current_ = point;
return *this;
}
Point PathBuilder::ReflectedCubicControlPoint1() const {
/*
* If there is no previous command or if the previous command was not a
* cubic, assume the first control point is coincident with the current
* point.
*/
if (prototype_.GetComponentCount() == 0) {
return current_;
}
CubicPathComponent cubic;
if (!prototype_.GetCubicComponentAtIndex(prototype_.GetComponentCount() - 1,
cubic)) {
return current_;
}
/*
* The first control point is assumed to be the reflection of the second
* control point on the previous command relative to the current point.
*/
return (current_ * 2.0) - cubic.cp2;
}
PathBuilder& PathBuilder::SmoothCubicCurveTo(Point controlPoint2,
Point point,
bool relative) {
auto controlPoint1 = ReflectedCubicControlPoint1();
controlPoint2 = relative ? current_ + controlPoint2 : controlPoint2;
auto endpoint = relative ? current_ + point : point;
CubicCurveTo(endpoint, // endpoint
controlPoint1, // control point 1
controlPoint2, // control point 2
false // relative since all points are already absolute
);
return *this;
}
PathBuilder& PathBuilder::AddQuadraticCurve(Point p1, Point cp, Point p2) {
MoveTo(p1);
prototype_.AddQuadraticComponent(p1, cp, p2);
return *this;
}
PathBuilder& PathBuilder::AddCubicCurve(Point p1,
Point cp1,
Point cp2,
Point p2) {
MoveTo(p1);
prototype_.AddCubicComponent(p1, cp1, cp2, p2);
return *this;
}
PathBuilder& PathBuilder::AddRect(Rect rect) {
current_ = rect.origin;
auto tl = rect.origin;
auto bl = rect.origin + Point{0.0, rect.size.height};
auto br = rect.origin + Point{rect.size.width, rect.size.height};
auto tr = rect.origin + Point{rect.size.width, 0.0};
MoveTo(tl);
prototype_.AddLinearComponent(tl, tr)
.AddLinearComponent(tr, br)
.AddLinearComponent(br, bl);
Close();
return *this;
}
PathBuilder& PathBuilder::AddCircle(const Point& c, Scalar r) {
return AddOval(Rect{c.x - r, c.y - r, 2.0f * r, 2.0f * r});
}
PathBuilder& PathBuilder::AddRoundedRect(Rect rect, Scalar radius) {
return radius <= 0.0 ? AddRect(rect)
: AddRoundedRect(rect, {radius, radius, radius, radius});
}
PathBuilder& PathBuilder::AddRoundedRect(Rect rect, RoundingRadii radii) {
if (radii.AreAllZero()) {
return AddRect(rect);
}
current_ = rect.origin + Point{radii.top_left.x, 0.0};
MoveTo({rect.origin.x + radii.top_left.x, rect.origin.y});
//----------------------------------------------------------------------------
// Top line.
//
prototype_.AddLinearComponent(
{rect.origin.x + radii.top_left.x, rect.origin.y},
{rect.origin.x + rect.size.width - radii.top_right.x, rect.origin.y});
//----------------------------------------------------------------------------
// Top right arc.
//
AddRoundedRectTopRight(rect, radii);
//----------------------------------------------------------------------------
// Right line.
//
prototype_.AddLinearComponent(
{rect.origin.x + rect.size.width, rect.origin.y + radii.top_right.y},
{rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height - radii.bottom_right.y});
//----------------------------------------------------------------------------
// Bottom right arc.
//
AddRoundedRectBottomRight(rect, radii);
//----------------------------------------------------------------------------
// Bottom line.
//
prototype_.AddLinearComponent(
{rect.origin.x + rect.size.width - radii.bottom_right.x,
rect.origin.y + rect.size.height},
{rect.origin.x + radii.bottom_left.x, rect.origin.y + rect.size.height});
//----------------------------------------------------------------------------
// Bottom left arc.
//
AddRoundedRectBottomLeft(rect, radii);
//----------------------------------------------------------------------------
// Left line.
//
prototype_.AddLinearComponent(
{rect.origin.x, rect.origin.y + rect.size.height - radii.bottom_left.y},
{rect.origin.x, rect.origin.y + radii.top_left.y});
//----------------------------------------------------------------------------
// Top left arc.
//
AddRoundedRectTopLeft(rect, radii);
Close();
return *this;
}
PathBuilder& PathBuilder::AddRoundedRectTopLeft(Rect rect,
RoundingRadii radii) {
const auto magic_top_left = radii.top_left * kArcApproximationMagic;
prototype_.AddCubicComponent(
{rect.origin.x, rect.origin.y + radii.top_left.y},
{rect.origin.x, rect.origin.y + radii.top_left.y - magic_top_left.y},
{rect.origin.x + radii.top_left.x - magic_top_left.x, rect.origin.y},
{rect.origin.x + radii.top_left.x, rect.origin.y});
return *this;
}
PathBuilder& PathBuilder::AddRoundedRectTopRight(Rect rect,
RoundingRadii radii) {
const auto magic_top_right = radii.top_right * kArcApproximationMagic;
prototype_.AddCubicComponent(
{rect.origin.x + rect.size.width - radii.top_right.x, rect.origin.y},
{rect.origin.x + rect.size.width - radii.top_right.x + magic_top_right.x,
rect.origin.y},
{rect.origin.x + rect.size.width,
rect.origin.y + radii.top_right.y - magic_top_right.y},
{rect.origin.x + rect.size.width, rect.origin.y + radii.top_right.y});
return *this;
}
PathBuilder& PathBuilder::AddRoundedRectBottomRight(Rect rect,
RoundingRadii radii) {
const auto magic_bottom_right = radii.bottom_right * kArcApproximationMagic;
prototype_.AddCubicComponent(
{rect.origin.x + rect.size.width,
rect.origin.y + rect.size.height - radii.bottom_right.y},
{rect.origin.x + rect.size.width, rect.origin.y + rect.size.height -
radii.bottom_right.y +
magic_bottom_right.y},
{rect.origin.x + rect.size.width - radii.bottom_right.x +
magic_bottom_right.x,
rect.origin.y + rect.size.height},
{rect.origin.x + rect.size.width - radii.bottom_right.x,
rect.origin.y + rect.size.height});
return *this;
}
PathBuilder& PathBuilder::AddRoundedRectBottomLeft(Rect rect,
RoundingRadii radii) {
const auto magic_bottom_left = radii.bottom_left * kArcApproximationMagic;
prototype_.AddCubicComponent(
{rect.origin.x + radii.bottom_left.x, rect.origin.y + rect.size.height},
{rect.origin.x + radii.bottom_left.x - magic_bottom_left.x,
rect.origin.y + rect.size.height},
{rect.origin.x, rect.origin.y + rect.size.height - radii.bottom_left.y +
magic_bottom_left.y},
{rect.origin.x, rect.origin.y + rect.size.height - radii.bottom_left.y});
return *this;
}
PathBuilder& PathBuilder::AddArc(const Rect& oval_bounds,
Radians start,
Radians sweep,
bool use_center) {
if (sweep.radians < 0) {
start.radians += sweep.radians;
sweep.radians *= -1;
}
sweep.radians = std::min(k2Pi, sweep.radians);
start.radians = std::fmod(start.radians, k2Pi);
const Point radius = {oval_bounds.size.width * 0.5f,
oval_bounds.size.height * 0.5f};
const Point center = {oval_bounds.origin.x + radius.x,
oval_bounds.origin.y + radius.y};
Vector2 p1_unit(std::cos(start.radians), std::sin(start.radians));
if (use_center) {
MoveTo(center);
LineTo(center + p1_unit * radius);
} else {
MoveTo(center + p1_unit * radius);
}
while (sweep.radians > 0) {
Vector2 p2_unit;
Scalar quadrant_angle;
if (sweep.radians < kPiOver2) {
quadrant_angle = sweep.radians;
p2_unit = Vector2(std::cos(start.radians + quadrant_angle),
std::sin(start.radians + quadrant_angle));
} else {
quadrant_angle = kPiOver2;
p2_unit = Vector2(-p1_unit.y, p1_unit.x);
}
Vector2 arc_cp_lengths =
(quadrant_angle / kPiOver2) * kArcApproximationMagic * radius;
Point p1 = center + p1_unit * radius;
Point p2 = center + p2_unit * radius;
Point cp1 = p1 + Vector2(-p1_unit.y, p1_unit.x) * arc_cp_lengths;
Point cp2 = p2 + Vector2(p2_unit.y, -p2_unit.x) * arc_cp_lengths;
prototype_.AddCubicComponent(p1, cp1, cp2, p2);
current_ = p2;
start.radians += quadrant_angle;
sweep.radians -= quadrant_angle;
p1_unit = p2_unit;
}
if (use_center) {
Close();
}
return *this;
}
PathBuilder& PathBuilder::AddOval(const Rect& container) {
const Point r = {container.size.width * 0.5f, container.size.height * 0.5f};
const Point c = {container.origin.x + r.x, container.origin.y + r.y};
const Point m = {kArcApproximationMagic * r.x, kArcApproximationMagic * r.y};
MoveTo({c.x, c.y - r.y});
//----------------------------------------------------------------------------
// Top right arc.
//
prototype_.AddCubicComponent({c.x, c.y - r.y}, // p1
{c.x + m.x, c.y - r.y}, // cp1
{c.x + r.x, c.y - m.y}, // cp2
{c.x + r.x, c.y} // p2
);
//----------------------------------------------------------------------------
// Bottom right arc.
//
prototype_.AddCubicComponent({c.x + r.x, c.y}, // p1
{c.x + r.x, c.y + m.y}, // cp1
{c.x + m.x, c.y + r.y}, // cp2
{c.x, c.y + r.y} // p2
);
//----------------------------------------------------------------------------
// Bottom left arc.
//
prototype_.AddCubicComponent({c.x, c.y + r.y}, // p1
{c.x - m.x, c.y + r.y}, // cp1
{c.x - r.x, c.y + m.y}, // cp2
{c.x - r.x, c.y} // p2
);
//----------------------------------------------------------------------------
// Top left arc.
//
prototype_.AddCubicComponent({c.x - r.x, c.y}, // p1
{c.x - r.x, c.y - m.y}, // cp1
{c.x - m.x, c.y - r.y}, // cp2
{c.x, c.y - r.y} // p2
);
Close();
return *this;
}
PathBuilder& PathBuilder::AddLine(const Point& p1, const Point& p2) {
MoveTo(p1);
prototype_.AddLinearComponent(p1, p2);
return *this;
}
const Path& PathBuilder::GetCurrentPath() const {
return prototype_;
}
PathBuilder& PathBuilder::AddPath(const Path& path) {
auto linear = [&](size_t index, const LinearPathComponent& l) {
prototype_.AddLinearComponent(l.p1, l.p2);
};
auto quadratic = [&](size_t index, const QuadraticPathComponent& q) {
prototype_.AddQuadraticComponent(q.p1, q.cp, q.p2);
};
auto cubic = [&](size_t index, const CubicPathComponent& c) {
prototype_.AddCubicComponent(c.p1, c.cp1, c.cp2, c.p2);
};
auto move = [&](size_t index, const ContourComponent& m) {
prototype_.AddContourComponent(m.destination);
};
path.EnumerateComponents(linear, quadratic, cubic, move);
return *this;
}
} // namespace impeller