blob: 05ea2da88fdbfe6aef6135fae555eda2e3d93867 [file] [log] [blame]
Doris Liu30bcf692015-11-04 14:56:24 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "VectorDrawablePath.h"
18
19#include "PathParser.h"
20
21#include <math.h>
22#include <utils/Log.h>
23
24namespace android {
25namespace uirenderer {
26
27class PathResolver {
28public:
29 float currentX = 0;
30 float currentY = 0;
31 float ctrlPointX = 0;
32 float ctrlPointY = 0;
33 float currentSegmentStartX = 0;
34 float currentSegmentStartY = 0;
35 void addCommand(SkPath* outPath, char previousCmd,
36 char cmd, const std::vector<float>* points, size_t start, size_t end);
37};
38
39VectorDrawablePath::VectorDrawablePath(const char* pathStr, size_t strLength) {
Doris Liu1e67f082015-11-12 15:57:45 -080040 PathParser::ParseResult result;
41 PathParser::getPathDataFromString(&mData, &result, pathStr, strLength);
42 if (!result.failureOccurred) {
43 verbsToPath(&mSkPath, &mData);
44 }
Doris Liu30bcf692015-11-04 14:56:24 -080045}
46
47VectorDrawablePath::VectorDrawablePath(const PathData& data) {
48 mData = data;
49 // Now we need to construct a path
50 verbsToPath(&mSkPath, &data);
51}
52
53VectorDrawablePath::VectorDrawablePath(const VectorDrawablePath& path) {
54 mData = path.mData;
55 verbsToPath(&mSkPath, &mData);
56}
57
58bool VectorDrawablePath::canMorph(const PathData& morphTo) {
59 if (mData.verbs.size() != morphTo.verbs.size()) {
60 return false;
61 }
62
63 for (unsigned int i = 0; i < mData.verbs.size(); i++) {
64 if (mData.verbs[i] != morphTo.verbs[i]
65 || mData.verbSizes[i] != morphTo.verbSizes[i]) {
66 return false;
67 }
68 }
69 return true;
70}
71
72bool VectorDrawablePath::canMorph(const VectorDrawablePath& path) {
73 return canMorph(path.mData);
74}
75 /**
76 * Convert an array of PathVerb to Path.
77 */
78void VectorDrawablePath::verbsToPath(SkPath* outPath, const PathData* data) {
79 PathResolver resolver;
80 char previousCommand = 'm';
81 size_t start = 0;
82 outPath->reset();
83 for (unsigned int i = 0; i < data->verbs.size(); i++) {
84 size_t verbSize = data->verbSizes[i];
85 resolver.addCommand(outPath, previousCommand, data->verbs[i], &data->points, start,
Doris Liu1e67f082015-11-12 15:57:45 -080086 start + verbSize);
Doris Liu30bcf692015-11-04 14:56:24 -080087 previousCommand = data->verbs[i];
88 start += verbSize;
89 }
90}
91
92/**
93 * The current PathVerb will be interpolated between the
94 * <code>nodeFrom</code> and <code>nodeTo</code> according to the
95 * <code>fraction</code>.
96 *
97 * @param nodeFrom The start value as a PathVerb.
98 * @param nodeTo The end value as a PathVerb
99 * @param fraction The fraction to interpolate.
100 */
101void VectorDrawablePath::interpolatePaths(PathData* outData,
102 const PathData* from, const PathData* to, float fraction) {
103 outData->points.resize(from->points.size());
104 outData->verbSizes = from->verbSizes;
105 outData->verbs = from->verbs;
106
107 for (size_t i = 0; i < from->points.size(); i++) {
108 outData->points[i] = from->points[i] * (1 - fraction) + to->points[i] * fraction;
109 }
110}
111
112/**
113 * Converts an arc to cubic Bezier segments and records them in p.
114 *
115 * @param p The target for the cubic Bezier segments
116 * @param cx The x coordinate center of the ellipse
117 * @param cy The y coordinate center of the ellipse
118 * @param a The radius of the ellipse in the horizontal direction
119 * @param b The radius of the ellipse in the vertical direction
120 * @param e1x E(eta1) x coordinate of the starting point of the arc
121 * @param e1y E(eta2) y coordinate of the starting point of the arc
122 * @param theta The angle that the ellipse bounding rectangle makes with horizontal plane
123 * @param start The start angle of the arc on the ellipse
124 * @param sweep The angle (positive or negative) of the sweep of the arc on the ellipse
125 */
126static void arcToBezier(SkPath* p,
127 double cx,
128 double cy,
129 double a,
130 double b,
131 double e1x,
132 double e1y,
133 double theta,
134 double start,
135 double sweep) {
136 // Taken from equations at: http://spaceroots.org/documents/ellipse/node8.html
137 // and http://www.spaceroots.org/documents/ellipse/node22.html
138
139 // Maximum of 45 degrees per cubic Bezier segment
140 int numSegments = ceil(fabs(sweep * 4 / M_PI));
141
142 double eta1 = start;
143 double cosTheta = cos(theta);
144 double sinTheta = sin(theta);
145 double cosEta1 = cos(eta1);
146 double sinEta1 = sin(eta1);
147 double ep1x = (-a * cosTheta * sinEta1) - (b * sinTheta * cosEta1);
148 double ep1y = (-a * sinTheta * sinEta1) + (b * cosTheta * cosEta1);
149
150 double anglePerSegment = sweep / numSegments;
151 for (int i = 0; i < numSegments; i++) {
152 double eta2 = eta1 + anglePerSegment;
153 double sinEta2 = sin(eta2);
154 double cosEta2 = cos(eta2);
155 double e2x = cx + (a * cosTheta * cosEta2) - (b * sinTheta * sinEta2);
156 double e2y = cy + (a * sinTheta * cosEta2) + (b * cosTheta * sinEta2);
157 double ep2x = -a * cosTheta * sinEta2 - b * sinTheta * cosEta2;
158 double ep2y = -a * sinTheta * sinEta2 + b * cosTheta * cosEta2;
159 double tanDiff2 = tan((eta2 - eta1) / 2);
160 double alpha =
161 sin(eta2 - eta1) * (sqrt(4 + (3 * tanDiff2 * tanDiff2)) - 1) / 3;
162 double q1x = e1x + alpha * ep1x;
163 double q1y = e1y + alpha * ep1y;
164 double q2x = e2x - alpha * ep2x;
165 double q2y = e2y - alpha * ep2y;
166
167 p->cubicTo((float) q1x,
168 (float) q1y,
169 (float) q2x,
170 (float) q2y,
171 (float) e2x,
172 (float) e2y);
173 eta1 = eta2;
174 e1x = e2x;
175 e1y = e2y;
176 ep1x = ep2x;
177 ep1y = ep2y;
178 }
179}
180
181inline double toRadians(float theta) { return theta * M_PI / 180;}
182
183static void drawArc(SkPath* p,
184 float x0,
185 float y0,
186 float x1,
187 float y1,
188 float a,
189 float b,
190 float theta,
191 bool isMoreThanHalf,
192 bool isPositiveArc) {
193
194 /* Convert rotation angle from degrees to radians */
195 double thetaD = toRadians(theta);
196 /* Pre-compute rotation matrix entries */
197 double cosTheta = cos(thetaD);
198 double sinTheta = sin(thetaD);
199 /* Transform (x0, y0) and (x1, y1) into unit space */
200 /* using (inverse) rotation, followed by (inverse) scale */
201 double x0p = (x0 * cosTheta + y0 * sinTheta) / a;
202 double y0p = (-x0 * sinTheta + y0 * cosTheta) / b;
203 double x1p = (x1 * cosTheta + y1 * sinTheta) / a;
204 double y1p = (-x1 * sinTheta + y1 * cosTheta) / b;
205
206 /* Compute differences and averages */
207 double dx = x0p - x1p;
208 double dy = y0p - y1p;
209 double xm = (x0p + x1p) / 2;
210 double ym = (y0p + y1p) / 2;
211 /* Solve for intersecting unit circles */
212 double dsq = dx * dx + dy * dy;
213 if (dsq == 0.0) {
214 ALOGW("Points are coincident");
215 return; /* Points are coincident */
216 }
217 double disc = 1.0 / dsq - 1.0 / 4.0;
218 if (disc < 0.0) {
219 ALOGW("Points are too far apart %f", dsq);
220 float adjust = (float) (sqrt(dsq) / 1.99999);
221 drawArc(p, x0, y0, x1, y1, a * adjust,
222 b * adjust, theta, isMoreThanHalf, isPositiveArc);
223 return; /* Points are too far apart */
224 }
225 double s = sqrt(disc);
226 double sdx = s * dx;
227 double sdy = s * dy;
228 double cx;
229 double cy;
230 if (isMoreThanHalf == isPositiveArc) {
231 cx = xm - sdy;
232 cy = ym + sdx;
233 } else {
234 cx = xm + sdy;
235 cy = ym - sdx;
236 }
237
238 double eta0 = atan2((y0p - cy), (x0p - cx));
239
240 double eta1 = atan2((y1p - cy), (x1p - cx));
241
242 double sweep = (eta1 - eta0);
243 if (isPositiveArc != (sweep >= 0)) {
244 if (sweep > 0) {
245 sweep -= 2 * M_PI;
246 } else {
247 sweep += 2 * M_PI;
248 }
249 }
250
251 cx *= a;
252 cy *= b;
253 double tcx = cx;
254 cx = cx * cosTheta - cy * sinTheta;
255 cy = tcx * sinTheta + cy * cosTheta;
256
257 arcToBezier(p, cx, cy, a, b, x0, y0, thetaD, eta0, sweep);
258}
259
Doris Liu1e67f082015-11-12 15:57:45 -0800260// Use the given verb, and points in the range [start, end) to insert a command into the SkPath.
Doris Liu30bcf692015-11-04 14:56:24 -0800261void PathResolver::addCommand(SkPath* outPath, char previousCmd,
262 char cmd, const std::vector<float>* points, size_t start, size_t end) {
263
264 int incr = 2;
265 float reflectiveCtrlPointX;
266 float reflectiveCtrlPointY;
267
268 switch (cmd) {
269 case 'z':
270 case 'Z':
271 outPath->close();
272 // Path is closed here, but we need to move the pen to the
273 // closed position. So we cache the segment's starting position,
274 // and restore it here.
275 currentX = currentSegmentStartX;
276 currentY = currentSegmentStartY;
277 ctrlPointX = currentSegmentStartX;
278 ctrlPointY = currentSegmentStartY;
279 outPath->moveTo(currentX, currentY);
280 break;
281 case 'm':
282 case 'M':
283 case 'l':
284 case 'L':
285 case 't':
286 case 'T':
287 incr = 2;
288 break;
289 case 'h':
290 case 'H':
291 case 'v':
292 case 'V':
293 incr = 1;
294 break;
295 case 'c':
296 case 'C':
297 incr = 6;
298 break;
299 case 's':
300 case 'S':
301 case 'q':
302 case 'Q':
303 incr = 4;
304 break;
305 case 'a':
306 case 'A':
307 incr = 7;
308 break;
309 }
310
Doris Liu1e67f082015-11-12 15:57:45 -0800311 for (unsigned int k = start; k < end; k += incr) {
Doris Liu30bcf692015-11-04 14:56:24 -0800312 switch (cmd) {
313 case 'm': // moveto - Start a new sub-path (relative)
314 currentX += points->at(k + 0);
315 currentY += points->at(k + 1);
316 if (k > start) {
317 // According to the spec, if a moveto is followed by multiple
318 // pairs of coordinates, the subsequent pairs are treated as
319 // implicit lineto commands.
320 outPath->rLineTo(points->at(k + 0), points->at(k + 1));
321 } else {
322 outPath->rMoveTo(points->at(k + 0), points->at(k + 1));
323 currentSegmentStartX = currentX;
324 currentSegmentStartY = currentY;
325 }
326 break;
327 case 'M': // moveto - Start a new sub-path
328 currentX = points->at(k + 0);
329 currentY = points->at(k + 1);
330 if (k > start) {
331 // According to the spec, if a moveto is followed by multiple
332 // pairs of coordinates, the subsequent pairs are treated as
333 // implicit lineto commands.
334 outPath->lineTo(points->at(k + 0), points->at(k + 1));
335 } else {
336 outPath->moveTo(points->at(k + 0), points->at(k + 1));
337 currentSegmentStartX = currentX;
338 currentSegmentStartY = currentY;
339 }
340 break;
341 case 'l': // lineto - Draw a line from the current point (relative)
342 outPath->rLineTo(points->at(k + 0), points->at(k + 1));
343 currentX += points->at(k + 0);
344 currentY += points->at(k + 1);
345 break;
346 case 'L': // lineto - Draw a line from the current point
347 outPath->lineTo(points->at(k + 0), points->at(k + 1));
348 currentX = points->at(k + 0);
349 currentY = points->at(k + 1);
350 break;
351 case 'h': // horizontal lineto - Draws a horizontal line (relative)
352 outPath->rLineTo(points->at(k + 0), 0);
353 currentX += points->at(k + 0);
354 break;
355 case 'H': // horizontal lineto - Draws a horizontal line
356 outPath->lineTo(points->at(k + 0), currentY);
357 currentX = points->at(k + 0);
358 break;
359 case 'v': // vertical lineto - Draws a vertical line from the current point (r)
360 outPath->rLineTo(0, points->at(k + 0));
361 currentY += points->at(k + 0);
362 break;
363 case 'V': // vertical lineto - Draws a vertical line from the current point
364 outPath->lineTo(currentX, points->at(k + 0));
365 currentY = points->at(k + 0);
366 break;
367 case 'c': // curveto - Draws a cubic Bézier curve (relative)
368 outPath->rCubicTo(points->at(k + 0), points->at(k + 1), points->at(k + 2), points->at(k + 3),
369 points->at(k + 4), points->at(k + 5));
370
371 ctrlPointX = currentX + points->at(k + 2);
372 ctrlPointY = currentY + points->at(k + 3);
373 currentX += points->at(k + 4);
374 currentY += points->at(k + 5);
375
376 break;
377 case 'C': // curveto - Draws a cubic Bézier curve
378 outPath->cubicTo(points->at(k + 0), points->at(k + 1), points->at(k + 2), points->at(k + 3),
379 points->at(k + 4), points->at(k + 5));
380 currentX = points->at(k + 4);
381 currentY = points->at(k + 5);
382 ctrlPointX = points->at(k + 2);
383 ctrlPointY = points->at(k + 3);
384 break;
385 case 's': // smooth curveto - Draws a cubic Bézier curve (reflective cp)
386 reflectiveCtrlPointX = 0;
387 reflectiveCtrlPointY = 0;
388 if (previousCmd == 'c' || previousCmd == 's'
389 || previousCmd == 'C' || previousCmd == 'S') {
390 reflectiveCtrlPointX = currentX - ctrlPointX;
391 reflectiveCtrlPointY = currentY - ctrlPointY;
392 }
393 outPath->rCubicTo(reflectiveCtrlPointX, reflectiveCtrlPointY,
394 points->at(k + 0), points->at(k + 1),
395 points->at(k + 2), points->at(k + 3));
396 ctrlPointX = currentX + points->at(k + 0);
397 ctrlPointY = currentY + points->at(k + 1);
398 currentX += points->at(k + 2);
399 currentY += points->at(k + 3);
400 break;
401 case 'S': // shorthand/smooth curveto Draws a cubic Bézier curve(reflective cp)
402 reflectiveCtrlPointX = currentX;
403 reflectiveCtrlPointY = currentY;
404 if (previousCmd == 'c' || previousCmd == 's'
405 || previousCmd == 'C' || previousCmd == 'S') {
406 reflectiveCtrlPointX = 2 * currentX - ctrlPointX;
407 reflectiveCtrlPointY = 2 * currentY - ctrlPointY;
408 }
409 outPath->cubicTo(reflectiveCtrlPointX, reflectiveCtrlPointY,
410 points->at(k + 0), points->at(k + 1), points->at(k + 2), points->at(k + 3));
411 ctrlPointX = points->at(k + 0);
412 ctrlPointY = points->at(k + 1);
413 currentX = points->at(k + 2);
414 currentY = points->at(k + 3);
415 break;
416 case 'q': // Draws a quadratic Bézier (relative)
417 outPath->rQuadTo(points->at(k + 0), points->at(k + 1), points->at(k + 2), points->at(k + 3));
418 ctrlPointX = currentX + points->at(k + 0);
419 ctrlPointY = currentY + points->at(k + 1);
420 currentX += points->at(k + 2);
421 currentY += points->at(k + 3);
422 break;
423 case 'Q': // Draws a quadratic Bézier
424 outPath->quadTo(points->at(k + 0), points->at(k + 1), points->at(k + 2), points->at(k + 3));
425 ctrlPointX = points->at(k + 0);
426 ctrlPointY = points->at(k + 1);
427 currentX = points->at(k + 2);
428 currentY = points->at(k + 3);
429 break;
430 case 't': // Draws a quadratic Bézier curve(reflective control point)(relative)
431 reflectiveCtrlPointX = 0;
432 reflectiveCtrlPointY = 0;
433 if (previousCmd == 'q' || previousCmd == 't'
434 || previousCmd == 'Q' || previousCmd == 'T') {
435 reflectiveCtrlPointX = currentX - ctrlPointX;
436 reflectiveCtrlPointY = currentY - ctrlPointY;
437 }
438 outPath->rQuadTo(reflectiveCtrlPointX, reflectiveCtrlPointY,
439 points->at(k + 0), points->at(k + 1));
440 ctrlPointX = currentX + reflectiveCtrlPointX;
441 ctrlPointY = currentY + reflectiveCtrlPointY;
442 currentX += points->at(k + 0);
443 currentY += points->at(k + 1);
444 break;
445 case 'T': // Draws a quadratic Bézier curve (reflective control point)
446 reflectiveCtrlPointX = currentX;
447 reflectiveCtrlPointY = currentY;
448 if (previousCmd == 'q' || previousCmd == 't'
449 || previousCmd == 'Q' || previousCmd == 'T') {
450 reflectiveCtrlPointX = 2 * currentX - ctrlPointX;
451 reflectiveCtrlPointY = 2 * currentY - ctrlPointY;
452 }
453 outPath->quadTo(reflectiveCtrlPointX, reflectiveCtrlPointY,
454 points->at(k + 0), points->at(k + 1));
455 ctrlPointX = reflectiveCtrlPointX;
456 ctrlPointY = reflectiveCtrlPointY;
457 currentX = points->at(k + 0);
458 currentY = points->at(k + 1);
459 break;
460 case 'a': // Draws an elliptical arc
461 // (rx ry x-axis-rotation large-arc-flag sweep-flag x y)
462 drawArc(outPath,
463 currentX,
464 currentY,
465 points->at(k + 5) + currentX,
466 points->at(k + 6) + currentY,
467 points->at(k + 0),
468 points->at(k + 1),
469 points->at(k + 2),
470 points->at(k + 3) != 0,
471 points->at(k + 4) != 0);
472 currentX += points->at(k + 5);
473 currentY += points->at(k + 6);
474 ctrlPointX = currentX;
475 ctrlPointY = currentY;
476 break;
477 case 'A': // Draws an elliptical arc
478 drawArc(outPath,
479 currentX,
480 currentY,
481 points->at(k + 5),
482 points->at(k + 6),
483 points->at(k + 0),
484 points->at(k + 1),
485 points->at(k + 2),
486 points->at(k + 3) != 0,
487 points->at(k + 4) != 0);
488 currentX = points->at(k + 5);
489 currentY = points->at(k + 6);
490 ctrlPointX = currentX;
491 ctrlPointY = currentY;
492 break;
493 }
494 previousCmd = cmd;
495 }
496}
497
498}; // namespace uirenderer
499}; // namespace android