[web] Update SurfacePath to use PathRef (flutter/engine#19557)
* Update SurfacePath to use PathRef * Fix test analysis errors * Switch DDRect to use path to reduce paint time processing * Implement toString() for debug mode * Fix contains (bounds inclusive) edge case, add test * Update recording canvas test for drawDRRect change * Update diffrate for arc/conic render change * Add test for winding for beveled border. Fix checkOnCurve * Fix mono quad winding on curve check * fix _chopCubicAtYExtrema and add test case * Address reviewer comments / setAll API usage
This commit is contained in:
@@ -492,9 +492,14 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/offset.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/opacity.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/painting.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/conic.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/cubic.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/path.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/path_metrics.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/path_ref.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/path_to_svg.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/path_utils.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/path_windings.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/path/tangent.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/picture.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/platform_view.dart
|
||||
FILE: ../../../flutter/lib/web_ui/lib/src/engine/surface/recording_canvas.dart
|
||||
|
||||
@@ -100,9 +100,14 @@ part 'engine/surface/offset.dart';
|
||||
part 'engine/surface/opacity.dart';
|
||||
part 'engine/surface/painting.dart';
|
||||
part 'engine/surface/path/conic.dart';
|
||||
part 'engine/surface/path/cubic.dart';
|
||||
part 'engine/surface/path/path.dart';
|
||||
part 'engine/surface/path/path_metrics.dart';
|
||||
part 'engine/surface/path/path_ref.dart';
|
||||
part 'engine/surface/path/path_to_svg.dart';
|
||||
part 'engine/surface/path/path_utils.dart';
|
||||
part 'engine/surface/path/path_windings.dart';
|
||||
part 'engine/surface/path/tangent.dart';
|
||||
part 'engine/surface/picture.dart';
|
||||
part 'engine/surface/platform_view.dart';
|
||||
part 'engine/surface/recording_canvas.dart';
|
||||
|
||||
@@ -509,69 +509,47 @@ class _CanvasPool extends _SaveStackTracking {
|
||||
}
|
||||
}
|
||||
|
||||
// Float buffer used for path iteration.
|
||||
static Float32List _runBuffer = Float32List(PathRefIterator.kMaxBufferSize);
|
||||
|
||||
/// 'Runs' the given [path] by applying all of its commands to the canvas.
|
||||
void _runPath(html.CanvasRenderingContext2D ctx, SurfacePath path) {
|
||||
ctx.beginPath();
|
||||
final List<Subpath> subpaths = path.subpaths;
|
||||
final int subpathCount = subpaths.length;
|
||||
for (int subPathIndex = 0; subPathIndex < subpathCount; subPathIndex++) {
|
||||
final Subpath subpath = subpaths[subPathIndex];
|
||||
final List<PathCommand> commands = subpath.commands;
|
||||
final int commandCount = commands.length;
|
||||
for (int c = 0; c < commandCount; c++) {
|
||||
final PathCommand command = commands[c];
|
||||
switch (command.type) {
|
||||
case PathCommandTypes.bezierCurveTo:
|
||||
final BezierCurveTo curve = command as BezierCurveTo;
|
||||
ctx.bezierCurveTo(
|
||||
curve.x1, curve.y1, curve.x2, curve.y2, curve.x3, curve.y3);
|
||||
break;
|
||||
case PathCommandTypes.close:
|
||||
ctx.closePath();
|
||||
break;
|
||||
case PathCommandTypes.ellipse:
|
||||
final Ellipse ellipse = command as Ellipse;
|
||||
if (c == 0) {
|
||||
// Ellipses that start a new path need to set start point,
|
||||
// otherwise it incorrectly uses last point.
|
||||
ctx.moveTo(subpath.startX, subpath.startY);
|
||||
}
|
||||
DomRenderer.ellipse(ctx,
|
||||
ellipse.x,
|
||||
ellipse.y,
|
||||
ellipse.radiusX,
|
||||
ellipse.radiusY,
|
||||
ellipse.rotation,
|
||||
ellipse.startAngle,
|
||||
ellipse.endAngle,
|
||||
ellipse.anticlockwise);
|
||||
break;
|
||||
case PathCommandTypes.lineTo:
|
||||
final LineTo lineTo = command as LineTo;
|
||||
ctx.lineTo(lineTo.x, lineTo.y);
|
||||
break;
|
||||
case PathCommandTypes.moveTo:
|
||||
final MoveTo moveTo = command as MoveTo;
|
||||
ctx.moveTo(moveTo.x, moveTo.y);
|
||||
break;
|
||||
case PathCommandTypes.rRect:
|
||||
final RRectCommand rrectCommand = command as RRectCommand;
|
||||
_RRectToCanvasRenderer(ctx)
|
||||
.render(rrectCommand.rrect, startNewPath: false);
|
||||
break;
|
||||
case PathCommandTypes.rect:
|
||||
final RectCommand rectCommand = command as RectCommand;
|
||||
ctx.rect(rectCommand.x, rectCommand.y, rectCommand.width,
|
||||
rectCommand.height);
|
||||
break;
|
||||
case PathCommandTypes.quadraticCurveTo:
|
||||
final QuadraticCurveTo quadraticCurveTo = command as QuadraticCurveTo;
|
||||
ctx.quadraticCurveTo(quadraticCurveTo.x1, quadraticCurveTo.y1,
|
||||
quadraticCurveTo.x2, quadraticCurveTo.y2);
|
||||
break;
|
||||
default:
|
||||
throw UnimplementedError('Unknown path command $command');
|
||||
}
|
||||
final Float32List p = _runBuffer;
|
||||
final PathRefIterator iter = PathRefIterator(path.pathRef);
|
||||
int verb = 0;
|
||||
while ((verb = iter.next(p)) != SPath.kDoneVerb) {
|
||||
switch (verb) {
|
||||
case SPath.kMoveVerb:
|
||||
ctx.moveTo(p[0], p[1]);
|
||||
break;
|
||||
case SPath.kLineVerb:
|
||||
ctx.lineTo(p[2], p[3]);
|
||||
break;
|
||||
case SPath.kCubicVerb:
|
||||
ctx.bezierCurveTo(p[2], p[3], p[4], p[5], p[6], p[7]);
|
||||
break;
|
||||
case SPath.kQuadVerb:
|
||||
ctx.quadraticCurveTo(p[2], p[3], p[4], p[5]);
|
||||
break;
|
||||
case SPath.kConicVerb:
|
||||
final double w = iter.conicWeight;
|
||||
Conic conic = Conic(p[0], p[1], p[2], p[3], p[4], p[5], w);
|
||||
List<ui.Offset> points = conic.toQuads();
|
||||
final int len = points.length;
|
||||
for (int i = 1; i < len; i += 2) {
|
||||
final double p1x = points[i].dx;
|
||||
final double p1y = points[i].dy;
|
||||
final double p2x = points[i + 1].dx;
|
||||
final double p2y = points[i + 1].dy;
|
||||
ctx.quadraticCurveTo(p1x, p1y, p2x, p2y);
|
||||
}
|
||||
break;
|
||||
case SPath.kCloseVerb:
|
||||
ctx.closePath();
|
||||
break;
|
||||
default:
|
||||
throw UnimplementedError('Unknown path verb $verb');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
|
||||
part of engine;
|
||||
|
||||
/// Mixin used by surfaces that clip their contents using an overflowing DOM
|
||||
@@ -261,15 +260,15 @@ class PersistedPhysicalShape extends PersistedContainerSurface
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
final Ellipse? ellipse = path.webOnlyPathAsCircle;
|
||||
if (ellipse != null) {
|
||||
final double rx = ellipse.radiusX;
|
||||
final double ry = ellipse.radiusY;
|
||||
final ui.Rect? ovalRect = path.webOnlyPathAsCircle;
|
||||
if (ovalRect != null) {
|
||||
final double rx = ovalRect.width / 2.0;
|
||||
final double ry = ovalRect.height / 2.0;
|
||||
final String borderRadius =
|
||||
rx == ry ? '${rx}px ' : '${rx}px ${ry}px ';
|
||||
final html.CssStyleDeclaration style = rootElement!.style;
|
||||
final double left = ellipse.x - rx;
|
||||
final double top = ellipse.y - ry;
|
||||
final double left = ovalRect.left;
|
||||
final double top = ovalRect.top;
|
||||
style
|
||||
..left = '${left}px'
|
||||
..top = '${top}px'
|
||||
|
||||
@@ -78,10 +78,6 @@ class Conic {
|
||||
return pointList;
|
||||
}
|
||||
|
||||
static bool _between(double a, double b, double c) {
|
||||
return (a - b) * (c - b) <= 0;
|
||||
}
|
||||
|
||||
// Subdivides a conic and writes to points list.
|
||||
static void _subdivide(Conic src, int level, List<ui.Offset> pointList) {
|
||||
assert(level >= 0);
|
||||
@@ -99,30 +95,30 @@ class Conic {
|
||||
final double startY = src.p0y;
|
||||
final double endY = src.p2y;
|
||||
final double cpY = src.p1y;
|
||||
if (_between(startY, cpY, endY)) {
|
||||
if (SPath.between(startY, cpY, endY)) {
|
||||
// Ensure that chopped conics maintain their y-order.
|
||||
final double midY = conic0.p2y;
|
||||
if (!_between(startY, midY, endY)) {
|
||||
if (!SPath.between(startY, midY, endY)) {
|
||||
// The computed midpoint is outside end points, move it to
|
||||
// closer one.
|
||||
final double closerY =
|
||||
(midY - startY).abs() < (midY - endY).abs() ? startY : endY;
|
||||
conic0.p2y = conic1.p0y = closerY;
|
||||
}
|
||||
if (!_between(startY, conic0.p1y, conic0.p2y)) {
|
||||
if (!SPath.between(startY, conic0.p1y, conic0.p2y)) {
|
||||
// First control point not between start and end points, move it
|
||||
// to start.
|
||||
conic0.p1y = startY;
|
||||
}
|
||||
if (!_between(conic1.p0y, conic1.p1y, endY)) {
|
||||
if (!SPath.between(conic1.p0y, conic1.p1y, endY)) {
|
||||
// Second control point not between start and end points, move it
|
||||
// to end.
|
||||
conic1.p1y = endY;
|
||||
}
|
||||
// Verify that conics points are ordered.
|
||||
assert(_between(startY, conic0.p1y, conic0.p2y));
|
||||
assert(_between(conic0.p1y, conic0.p2y, conic1.p1y));
|
||||
assert(_between(conic0.p2y, conic1.p1y, endY));
|
||||
assert(SPath.between(startY, conic0.p1y, conic0.p2y));
|
||||
assert(SPath.between(conic0.p1y, conic0.p2y, conic1.p1y));
|
||||
assert(SPath.between(conic0.p2y, conic1.p1y, endY));
|
||||
}
|
||||
--level;
|
||||
_subdivide(conic0, level, pointList);
|
||||
@@ -152,6 +148,118 @@ class Conic {
|
||||
(p2y + wp1.dy) * scale, p2x, p2y, newW);
|
||||
}
|
||||
|
||||
void chopAtYExtrema(List<Conic> dst) {
|
||||
double? t = _findYExtrema();
|
||||
if (t == null) {
|
||||
dst.add(this);
|
||||
return;
|
||||
}
|
||||
if (!_chopAt(t, dst, cleanupMiddle: true)) {
|
||||
// If chop can't return finite values, don't chop.
|
||||
dst.add(this);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// NURB representation for conics. Helpful explanations at:
|
||||
//
|
||||
// http://citeseerx.ist.psu.edu/viewdoc/
|
||||
// download?doi=10.1.1.44.5740&rep=rep1&type=ps
|
||||
// and
|
||||
// http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html
|
||||
//
|
||||
// F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w)
|
||||
// ------------------------------------------
|
||||
// ((1 - t)^2 + t^2 + 2 (1 - t) t w)
|
||||
//
|
||||
// = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0}
|
||||
// ------------------------------------------------
|
||||
// {t^2 (2 - 2 w), t (-2 + 2 w), 1}
|
||||
//
|
||||
// F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w)
|
||||
//
|
||||
// t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w)
|
||||
// t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w)
|
||||
// t^0 : -2 P0 w + 2 P1 w
|
||||
//
|
||||
// We disregard magnitude, so we can freely ignore the denominator of F', and
|
||||
// divide the numerator by 2
|
||||
//
|
||||
// coeff[0] for t^2
|
||||
// coeff[1] for t^1
|
||||
// coeff[2] for t^0
|
||||
//
|
||||
double? _findYExtrema() {
|
||||
final double p20 = p2y - p0y;
|
||||
final double p10 = p1y - p0y;
|
||||
final double wP10 = fW * p10;
|
||||
final double coeff0 = fW * p20 - p20;
|
||||
final double coeff1 = p20 - 2 * wP10;
|
||||
final double coeff2 = wP10;
|
||||
final _QuadRoots quadRoots = _QuadRoots();
|
||||
int rootCount = quadRoots.findRoots(coeff0, coeff1, coeff2);
|
||||
assert(rootCount == 0 || rootCount == 1);
|
||||
if (rootCount == 1) {
|
||||
return quadRoots.root0;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
bool _chopAt(double t, List<Conic> dst, {bool cleanupMiddle = false}) {
|
||||
// Map conic to 3D.
|
||||
final double tx0 = p0x;
|
||||
final double ty0 = p0y;
|
||||
final double tz0 = 1;
|
||||
final double tx1 = p1x * fW;
|
||||
final double ty1 = p1y * fW;
|
||||
final double tz1 = fW;
|
||||
final double tx2 = p2x;
|
||||
final double ty2 = p2y;
|
||||
final double tz2 = 1;
|
||||
// Now interpolate each dimension.
|
||||
final double dx0 = tx0 + (tx1 - tx0) * t;
|
||||
final double dx2 = tx1 + (tx2 - tx1) * t;
|
||||
final double dx1 = dx0 + (dx2 - dx0) * t;
|
||||
final double dy0 = ty0 + (ty1 - ty0) * t;
|
||||
final double dy2 = ty1 + (ty2 - ty1) * t;
|
||||
final double dy1 = dy0 + (dy2 - dy0) * t;
|
||||
final double dz0 = tz0 + (tz1 - tz0) * t;
|
||||
final double dz2 = tz1 + (tz2 - tz1) * t;
|
||||
final double dz1 = dz0 + (dz2 - dz0) * t;
|
||||
// Compute new weights.
|
||||
final double root = math.sqrt(dz1);
|
||||
if (_nearlyEqual(root, 0)) {
|
||||
return false;
|
||||
}
|
||||
final double w0 = dz0 / root;
|
||||
final double w2 = dz2 / root;
|
||||
if (_nearlyEqual(dz0, 0) || _nearlyEqual(dz1, 0) || _nearlyEqual(dz2, 0)) {
|
||||
return false;
|
||||
}
|
||||
// Now we can construct the 2 conics by projecting 3D down to 2D.
|
||||
final double chopPointX = dx1 / dz1;
|
||||
final double chopPointY = dy1 / dz1;
|
||||
|
||||
double cp0y = dy0 / dz0;
|
||||
double cp1y = dy2 / dz2;
|
||||
if (cleanupMiddle) {
|
||||
// Clean-up the middle, since we know t was meant to be at
|
||||
// an Y-extrema.
|
||||
cp0y = chopPointY;
|
||||
cp1y = chopPointY;
|
||||
}
|
||||
|
||||
final Conic conic0 =
|
||||
Conic(p0x, p0y, dx0 / dz0, cp0y, chopPointX, chopPointY, w0);
|
||||
final Conic conic1 =
|
||||
Conic(chopPointX, chopPointY, dx2 / dz2, cp1y, p2x, p2y, w2);
|
||||
dst.add(conic0);
|
||||
dst.add(conic1);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Computes number of binary subdivisions of the curve given
|
||||
/// the tolerance.
|
||||
///
|
||||
@@ -186,6 +294,179 @@ class Conic {
|
||||
}
|
||||
return pow2;
|
||||
}
|
||||
|
||||
ui.Offset evalTangentAt(double t) {
|
||||
// The derivative equation returns a zero tangent vector when t is 0 or 1,
|
||||
// and the control point is equal to the end point.
|
||||
// In this case, use the conic endpoints to compute the tangent.
|
||||
if ((t == 0 && p0x == p1x && p0y == p1y) ||
|
||||
(t == 1 && p1x == p2x && p1y == p2y)) {
|
||||
return ui.Offset(p2x - p0x, p2y - p0y);
|
||||
}
|
||||
double p20x = p2x - p0x;
|
||||
double p20y = p2y - p0y;
|
||||
double p10x = p1x - p0x;
|
||||
double p10y = p1y - p0y;
|
||||
|
||||
double cx = fW * p10x;
|
||||
double cy = fW * p10y;
|
||||
double ax = fW * p20x - p20x;
|
||||
double ay = fW * p20y - p20y;
|
||||
double bx = p20x - cx - cx;
|
||||
double by = p20y - cy - cy;
|
||||
_SkQuadCoefficients quadC = _SkQuadCoefficients(ax, ay, bx, by, cx, cy);
|
||||
return ui.Offset(quadC.evalX(t), quadC.evalY(t));
|
||||
}
|
||||
}
|
||||
|
||||
double _conicEvalNumerator(
|
||||
double p0, double p1, double p2, double w, double t) {
|
||||
assert(t >= 0 && t <= 1);
|
||||
final double src2w = p1 * w;
|
||||
final C = p0;
|
||||
final A = p2 - 2 * src2w + C;
|
||||
final B = 2 * (src2w - C);
|
||||
return polyEval(A, B, C, t);
|
||||
}
|
||||
|
||||
double _conicEvalDenominator(double w, double t) {
|
||||
double B = 2 * (w - 1);
|
||||
double C = 1;
|
||||
double A = -B;
|
||||
return polyEval(A, B, C, t);
|
||||
}
|
||||
|
||||
class _QuadBounds {
|
||||
double minX = 0;
|
||||
double minY = 0;
|
||||
double maxX = 0;
|
||||
double maxY = 0;
|
||||
void calculateBounds(Float32List points, int pointIndex) {
|
||||
final double x1 = points[pointIndex++];
|
||||
final double y1 = points[pointIndex++];
|
||||
final double cpX = points[pointIndex++];
|
||||
final double cpY = points[pointIndex++];
|
||||
final double x2 = points[pointIndex++];
|
||||
final double y2 = points[pointIndex++];
|
||||
|
||||
minX = math.min(x1, x2);
|
||||
minY = math.min(y1, y2);
|
||||
maxX = math.max(x1, x2);
|
||||
maxY = math.max(y1, y2);
|
||||
|
||||
// At extrema's derivative = 0.
|
||||
// Solve for
|
||||
// -2x1+2tx1 + 2cpX + 4tcpX + 2tx2 = 0
|
||||
// -2x1 + 2cpX +2t(x1 + 2cpX + x2) = 0
|
||||
// t = (x1 - cpX) / (x1 - 2cpX + x2)
|
||||
double denom = x1 - (2 * cpX) + x2;
|
||||
if (denom.abs() > SPath.scalarNearlyZero) {
|
||||
final double t1 = (x1 - cpX) / denom;
|
||||
if ((t1 >= 0) && (t1 <= 1.0)) {
|
||||
// Solve (x,y) for curve at t = tx to find extrema
|
||||
final double tprime = 1.0 - t1;
|
||||
final double extremaX =
|
||||
(tprime * tprime * x1) + (2 * t1 * tprime * cpX) + (t1 * t1 * x2);
|
||||
final double extremaY =
|
||||
(tprime * tprime * y1) + (2 * t1 * tprime * cpY) + (t1 * t1 * y2);
|
||||
// Expand bounds.
|
||||
minX = math.min(minX, extremaX);
|
||||
maxX = math.max(maxX, extremaX);
|
||||
minY = math.min(minY, extremaY);
|
||||
maxY = math.max(maxY, extremaY);
|
||||
}
|
||||
}
|
||||
// Now calculate dy/dt = 0
|
||||
denom = y1 - (2 * cpY) + y2;
|
||||
if (denom.abs() > SPath.scalarNearlyZero) {
|
||||
final double t2 = (y1 - cpY) / denom;
|
||||
if ((t2 >= 0) && (t2 <= 1.0)) {
|
||||
final double tprime2 = 1.0 - t2;
|
||||
final double extrema2X = (tprime2 * tprime2 * x1) +
|
||||
(2 * t2 * tprime2 * cpX) +
|
||||
(t2 * t2 * x2);
|
||||
final double extrema2Y = (tprime2 * tprime2 * y1) +
|
||||
(2 * t2 * tprime2 * cpY) +
|
||||
(t2 * t2 * y2);
|
||||
// Expand bounds.
|
||||
minX = math.min(minX, extrema2X);
|
||||
maxX = math.max(maxX, extrema2X);
|
||||
minY = math.min(minY, extrema2Y);
|
||||
maxY = math.max(maxY, extrema2Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _ConicBounds {
|
||||
double minX = 0;
|
||||
double minY = 0;
|
||||
double maxX = 0;
|
||||
double maxY = 0;
|
||||
void calculateBounds(Float32List points, double w, int pointIndex) {
|
||||
final double x1 = points[pointIndex++];
|
||||
final double y1 = points[pointIndex++];
|
||||
final double cpX = points[pointIndex++];
|
||||
final double cpY = points[pointIndex++];
|
||||
final double x2 = points[pointIndex++];
|
||||
final double y2 = points[pointIndex++];
|
||||
|
||||
minX = math.min(x1, x2);
|
||||
minY = math.min(y1, y2);
|
||||
maxX = math.max(x1, x2);
|
||||
maxY = math.max(y1, y2);
|
||||
|
||||
// {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0}
|
||||
// ------------------------------------------------
|
||||
// {t^2 (2 - 2 w), t (-2 + 2 w), 1}
|
||||
// Calculate coefficients and solve root.
|
||||
_QuadRoots roots = _QuadRoots();
|
||||
final double P20x = x2 - x1;
|
||||
final double P10x = cpX - x1;
|
||||
final double wP10x = w * P10x;
|
||||
double ax = w * P20x - P20x;
|
||||
double bx = P20x - 2 * wP10x;
|
||||
double cx = wP10x;
|
||||
int n = roots.findRoots(ax, bx, cx);
|
||||
if (n != 0) {
|
||||
final double t1 = roots.root0!;
|
||||
if ((t1 >= 0) && (t1 <= 1.0)) {
|
||||
final double denom = _conicEvalDenominator(w, t1);
|
||||
double numerator = _conicEvalNumerator(x1, cpX, x2, w, t1);
|
||||
final double extremaX = numerator / denom;
|
||||
numerator = _conicEvalNumerator(y1, cpY, y2, w, t1);
|
||||
final double extremaY = numerator / denom;
|
||||
// Expand bounds.
|
||||
minX = math.min(minX, extremaX);
|
||||
maxX = math.max(maxX, extremaX);
|
||||
minY = math.min(minY, extremaY);
|
||||
maxY = math.max(maxY, extremaY);
|
||||
}
|
||||
}
|
||||
final double P20y = y2 - y1;
|
||||
final double P10y = cpY - y1;
|
||||
final double wP10y = w * P10y;
|
||||
double a = w * P20y - P20y;
|
||||
double b = P20y - 2 * wP10y;
|
||||
double c = wP10y;
|
||||
n = roots.findRoots(a, b, c);
|
||||
|
||||
if (n != 0) {
|
||||
final double t2 = roots.root0!;
|
||||
if ((t2 >= 0) && (t2 <= 1.0)) {
|
||||
final double denom = _conicEvalDenominator(w, t2);
|
||||
double numerator = _conicEvalNumerator(x1, cpX, x2, w, t2);
|
||||
final double extrema2X = numerator / denom;
|
||||
numerator = _conicEvalNumerator(y1, cpY, y2, w, t2);
|
||||
final double extrema2Y = numerator / denom;
|
||||
// Expand bounds.
|
||||
minX = math.min(minX, extrema2X);
|
||||
maxX = math.max(maxX, extrema2X);
|
||||
minY = math.min(minY, extrema2Y);
|
||||
maxY = math.max(maxY, extrema2Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _ConicPair {
|
||||
|
||||
@@ -0,0 +1,414 @@
|
||||
// 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.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// Chops cubic at Y extrema points and writes result to [dest].
|
||||
///
|
||||
/// [points] and [dest] are allowed to share underlying storage as long.
|
||||
int _chopCubicAtYExtrema(Float32List points, Float32List dest) {
|
||||
final double y0 = points[1];
|
||||
final double y1 = points[3];
|
||||
final double y2 = points[5];
|
||||
final double y3 = points[7];
|
||||
_QuadRoots _quadRoots = _findCubicExtrema(y0, y1, y2, y3);
|
||||
final List<double> roots = _quadRoots.roots;
|
||||
if (roots.isEmpty) {
|
||||
// No roots, just use input cubic.
|
||||
return 0;
|
||||
}
|
||||
_chopCubicAt(roots, points, dest);
|
||||
final int rootCount = roots.length;
|
||||
if (rootCount > 0) {
|
||||
// Cleanup to ensure Y extrema are flat.
|
||||
dest[5] = dest[9] = dest[7];
|
||||
if (rootCount == 2) {
|
||||
dest[11] = dest[15] = dest[13];
|
||||
}
|
||||
}
|
||||
return rootCount;
|
||||
}
|
||||
|
||||
_QuadRoots _findCubicExtrema(double a, double b, double c, double d) {
|
||||
// A,B,C scaled by 1/3 to simplify
|
||||
final double A = d - a + 3 * (b - c);
|
||||
final double B = 2 * (a - b - b + c);
|
||||
final double C = b - a;
|
||||
return _QuadRoots()..findRoots(A, B, C);
|
||||
}
|
||||
|
||||
/// Subdivides cubic curve for a list of t values.
|
||||
void _chopCubicAt(
|
||||
List<double> tValues, Float32List points, Float32List outPts) {
|
||||
if (assertionsEnabled) {
|
||||
for (int i = 0; i < tValues.length - 1; i++) {
|
||||
final double tValue = tValues[i];
|
||||
assert(tValue > 0 && tValue < 1,
|
||||
'Not expecting to chop curve at start, end points');
|
||||
}
|
||||
for (int i = 0; i < tValues.length - 1; i++) {
|
||||
final double tValue = tValues[i];
|
||||
final double nextTValue = tValues[i + 1];
|
||||
assert(
|
||||
nextTValue > tValue, 'Expecting t value to monotonically increase');
|
||||
}
|
||||
}
|
||||
int rootCount = tValues.length;
|
||||
if (0 == rootCount) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
outPts[i] = points[i];
|
||||
}
|
||||
} else {
|
||||
// Chop curve at t value and loop through right side of curve
|
||||
// while normalizing t value based on prior t.
|
||||
double? t = tValues[0];
|
||||
int bufferPos = 0;
|
||||
for (int i = 0; i < rootCount; i++) {
|
||||
_chopCubicAtT(points, bufferPos, outPts, bufferPos, t!);
|
||||
if (i == rootCount - 1) {
|
||||
break;
|
||||
}
|
||||
bufferPos += 6;
|
||||
|
||||
// watch out in case the renormalized t isn't in range
|
||||
if ((t = _validUnitDivide(
|
||||
tValues[i + 1] - tValues[i], 1.0 - tValues[i])) ==
|
||||
null) {
|
||||
// Can't renormalize last point, just create a degenerate cubic.
|
||||
outPts[bufferPos + 4] = outPts[bufferPos + 5] =
|
||||
outPts[bufferPos + 6] = points[bufferPos + 3];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Subdivides cubic curve at [t] and writes to [outPts] at position [outIndex].
|
||||
///
|
||||
/// The cubic points are read from [points] at [bufferPos] offset.
|
||||
void _chopCubicAtT(Float32List points, int bufferPos, Float32List outPts,
|
||||
int outIndex, double t) {
|
||||
assert(t > 0 && t < 1);
|
||||
final double p3y = points[bufferPos + 7];
|
||||
final double p0x = points[bufferPos + 0];
|
||||
final double p0y = points[bufferPos + 1];
|
||||
final double p1x = points[bufferPos + 2];
|
||||
final double p1y = points[bufferPos + 3];
|
||||
final double p2x = points[bufferPos + 4];
|
||||
final double p2y = points[bufferPos + 5];
|
||||
final double p3x = points[bufferPos + 6];
|
||||
// If startT == 0 chop at end point and return curve.
|
||||
final double ab1x = _interpolate(p0x, p1x, t);
|
||||
final double ab1y = _interpolate(p0y, p1y, t);
|
||||
final double bc1x = _interpolate(p1x, p2x, t);
|
||||
final double bc1y = _interpolate(p1y, p2y, t);
|
||||
final double cd1x = _interpolate(p2x, p3x, t);
|
||||
final double cd1y = _interpolate(p2y, p3y, t);
|
||||
final double abc1x = _interpolate(ab1x, bc1x, t);
|
||||
final double abc1y = _interpolate(ab1y, bc1y, t);
|
||||
final double bcd1x = _interpolate(bc1x, cd1x, t);
|
||||
final double bcd1y = _interpolate(bc1y, cd1y, t);
|
||||
final double abcd1x = _interpolate(abc1x, bcd1x, t);
|
||||
final double abcd1y = _interpolate(abc1y, bcd1y, t);
|
||||
|
||||
// Return left side of curve.
|
||||
outPts[outIndex++] = p0x;
|
||||
outPts[outIndex++] = p0y;
|
||||
outPts[outIndex++] = ab1x;
|
||||
outPts[outIndex++] = ab1y;
|
||||
outPts[outIndex++] = abc1x;
|
||||
outPts[outIndex++] = abc1y;
|
||||
outPts[outIndex++] = abcd1x;
|
||||
outPts[outIndex++] = abcd1y;
|
||||
// Return right side of curve.
|
||||
outPts[outIndex++] = bcd1x;
|
||||
outPts[outIndex++] = bcd1y;
|
||||
outPts[outIndex++] = cd1x;
|
||||
outPts[outIndex++] = cd1y;
|
||||
outPts[outIndex++] = p3x;
|
||||
outPts[outIndex++] = p3y;
|
||||
}
|
||||
|
||||
// Returns t at Y for cubic curve. null if y is out of range.
|
||||
//
|
||||
// Options are Newton Raphson (quadratic convergence with typically
|
||||
// 3 iterations or bisection with 16 iterations.
|
||||
double? _chopMonoAtY(Float32List _buffer, int bufferStartPos, double y) {
|
||||
// Translate curve points relative to y.
|
||||
final double ycrv0 = _buffer[1 + bufferStartPos] - y;
|
||||
final double ycrv1 = _buffer[3 + bufferStartPos] - y;
|
||||
final double ycrv2 = _buffer[5 + bufferStartPos] - y;
|
||||
final double ycrv3 = _buffer[7 + bufferStartPos] - y;
|
||||
// Positive and negative function parameters.
|
||||
double tNeg, tPos;
|
||||
// Set initial t points to converge from.
|
||||
if (ycrv0 < 0) {
|
||||
if (ycrv3 < 0) {
|
||||
// Start and end points out of range.
|
||||
return null;
|
||||
}
|
||||
tNeg = 0;
|
||||
tPos = 1.0;
|
||||
} else if (ycrv0 > 0) {
|
||||
tNeg = 1.0;
|
||||
tPos = 0;
|
||||
} else {
|
||||
// Start is at y.
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
// Bisection / linear convergance.
|
||||
final double tolerance = 1.0 / 65536;
|
||||
do {
|
||||
final double tMid = (tPos + tNeg) / 2.0;
|
||||
final double y01 = ycrv0 + (ycrv1 - ycrv0) * tMid;
|
||||
final double y12 = ycrv1 + (ycrv2 - ycrv1) * tMid;
|
||||
final double y23 = ycrv2 + (ycrv3 - ycrv2) * tMid;
|
||||
final double y012 = y01 + (y12 - y01) * tMid;
|
||||
final double y123 = y12 + (y23 - y12) * tMid;
|
||||
final double y0123 = y012 + (y123 - y012) * tMid;
|
||||
if (y0123 == 0) {
|
||||
return tMid;
|
||||
}
|
||||
if (y0123 < 0) {
|
||||
tNeg = tMid;
|
||||
} else {
|
||||
tPos = tMid;
|
||||
}
|
||||
} while (((tPos - tNeg).abs() > tolerance));
|
||||
return (tNeg + tPos) / 2;
|
||||
}
|
||||
|
||||
double _evalCubicPts(double c0, double c1, double c2, double c3, double t) {
|
||||
double A = c3 + 3 * (c1 - c2) - c0;
|
||||
double B = 3 * (c2 - c1 - c1 + c0);
|
||||
double C = 3 * (c1 - c0);
|
||||
double D = c0;
|
||||
return polyEval4(A, B, C, D, t);
|
||||
}
|
||||
|
||||
// Reusable class to compute bounds without object allocation.
|
||||
class _CubicBounds {
|
||||
double minX = 0.0;
|
||||
double maxX = 0.0;
|
||||
double minY = 0.0;
|
||||
double maxY = 0.0;
|
||||
|
||||
/// Sets resulting bounds as [minX], [minY], [maxX], [maxY].
|
||||
///
|
||||
/// The cubic is defined by 4 points (8 floats) in [points].
|
||||
void calculateBounds(Float32List points, int pointIndex) {
|
||||
final double startX = points[pointIndex++];
|
||||
final double startY = points[pointIndex++];
|
||||
final double cpX1 = points[pointIndex++];
|
||||
final double cpY1 = points[pointIndex++];
|
||||
final double cpX2 = points[pointIndex++];
|
||||
final double cpY2 = points[pointIndex++];
|
||||
final double endX = points[pointIndex++];
|
||||
final double endY = points[pointIndex++];
|
||||
// Bounding box is defined by all points on the curve where
|
||||
// monotonicity changes.
|
||||
minX = math.min(startX, endX);
|
||||
minY = math.min(startY, endY);
|
||||
maxX = math.max(startX, endX);
|
||||
maxY = math.max(startY, endY);
|
||||
|
||||
double extremaX;
|
||||
double extremaY;
|
||||
double a, b, c;
|
||||
|
||||
// Check for simple case of strong ordering before calculating
|
||||
// extrema
|
||||
if (!(((startX < cpX1) && (cpX1 < cpX2) && (cpX2 < endX)) ||
|
||||
((startX > cpX1) && (cpX1 > cpX2) && (cpX2 > endX)))) {
|
||||
// The extrema point is dx/dt B(t) = 0
|
||||
// The derivative of B(t) for cubic bezier is a quadratic equation
|
||||
// with multiple roots
|
||||
// B'(t) = a*t*t + b*t + c*t
|
||||
a = -startX + (3 * (cpX1 - cpX2)) + endX;
|
||||
b = 2 * (startX - (2 * cpX1) + cpX2);
|
||||
c = -startX + cpX1;
|
||||
|
||||
// Now find roots for quadratic equation with known coefficients
|
||||
// a,b,c
|
||||
// The roots are (-b+-sqrt(b*b-4*a*c)) / 2a
|
||||
num s = (b * b) - (4 * a * c);
|
||||
// If s is negative, we have no real roots
|
||||
if ((s >= 0.0) && (a.abs() > SPath.scalarNearlyZero)) {
|
||||
if (s == 0.0) {
|
||||
// we have only 1 root
|
||||
final double t = -b / (2 * a);
|
||||
final double tprime = 1.0 - t;
|
||||
if ((t >= 0.0) && (t <= 1.0)) {
|
||||
extremaX = ((tprime * tprime * tprime) * startX) +
|
||||
((3 * tprime * tprime * t) * cpX1) +
|
||||
((3 * tprime * t * t) * cpX2) +
|
||||
(t * t * t * endX);
|
||||
minX = math.min(extremaX, minX);
|
||||
maxX = math.max(extremaX, maxX);
|
||||
}
|
||||
} else {
|
||||
// we have 2 roots
|
||||
s = math.sqrt(s);
|
||||
double t = (-b - s) / (2 * a);
|
||||
double tprime = 1.0 - t;
|
||||
if ((t >= 0.0) && (t <= 1.0)) {
|
||||
extremaX = ((tprime * tprime * tprime) * startX) +
|
||||
((3 * tprime * tprime * t) * cpX1) +
|
||||
((3 * tprime * t * t) * cpX2) +
|
||||
(t * t * t * endX);
|
||||
minX = math.min(extremaX, minX);
|
||||
maxX = math.max(extremaX, maxX);
|
||||
}
|
||||
// check 2nd root
|
||||
t = (-b + s) / (2 * a);
|
||||
tprime = 1.0 - t;
|
||||
if ((t >= 0.0) && (t <= 1.0)) {
|
||||
extremaX = ((tprime * tprime * tprime) * startX) +
|
||||
((3 * tprime * tprime * t) * cpX1) +
|
||||
((3 * tprime * t * t) * cpX2) +
|
||||
(t * t * t * endX);
|
||||
|
||||
minX = math.min(extremaX, minX);
|
||||
maxX = math.max(extremaX, maxX);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now calc extremes for dy/dt = 0 just like above
|
||||
if (!(((startY < cpY1) && (cpY1 < cpY2) && (cpY2 < endY)) ||
|
||||
((startY > cpY1) && (cpY1 > cpY2) && (cpY2 > endY)))) {
|
||||
// The extrema point is dy/dt B(t) = 0
|
||||
// The derivative of B(t) for cubic bezier is a quadratic equation
|
||||
// with multiple roots
|
||||
// B'(t) = a*t*t + b*t + c*t
|
||||
a = -startY + (3 * (cpY1 - cpY2)) + endY;
|
||||
b = 2 * (startY - (2 * cpY1) + cpY2);
|
||||
c = -startY + cpY1;
|
||||
|
||||
// Now find roots for quadratic equation with known coefficients
|
||||
// a,b,c
|
||||
// The roots are (-b+-sqrt(b*b-4*a*c)) / 2a
|
||||
double s = (b * b) - (4 * a * c);
|
||||
// If s is negative, we have no real roots
|
||||
if ((s >= 0.0) && (a.abs() > SPath.scalarNearlyZero)) {
|
||||
if (s == 0.0) {
|
||||
// we have only 1 root
|
||||
final double t = -b / (2 * a);
|
||||
final double tprime = 1.0 - t;
|
||||
if ((t >= 0.0) && (t <= 1.0)) {
|
||||
extremaY = ((tprime * tprime * tprime) * startY) +
|
||||
((3 * tprime * tprime * t) * cpY1) +
|
||||
((3 * tprime * t * t) * cpY2) +
|
||||
(t * t * t * endY);
|
||||
minY = math.min(extremaY, minY);
|
||||
maxY = math.max(extremaY, maxY);
|
||||
}
|
||||
} else {
|
||||
// we have 2 roots
|
||||
s = math.sqrt(s);
|
||||
final double t = (-b - s) / (2 * a);
|
||||
final double tprime = 1.0 - t;
|
||||
if ((t >= 0.0) && (t <= 1.0)) {
|
||||
extremaY = ((tprime * tprime * tprime) * startY) +
|
||||
((3 * tprime * tprime * t) * cpY1) +
|
||||
((3 * tprime * t * t) * cpY2) +
|
||||
(t * t * t * endY);
|
||||
minY = math.min(extremaY, minY);
|
||||
maxY = math.max(extremaY, maxY);
|
||||
}
|
||||
// check 2nd root
|
||||
final double t2 = (-b + s) / (2 * a);
|
||||
final double tprime2 = 1.0 - t2;
|
||||
if ((t2 >= 0.0) && (t2 <= 1.0)) {
|
||||
extremaY = ((tprime2 * tprime2 * tprime2) * startY) +
|
||||
((3 * tprime2 * tprime2 * t2) * cpY1) +
|
||||
((3 * tprime2 * t2 * t2) * cpY2) +
|
||||
(t2 * t2 * t2 * endY);
|
||||
minY = math.min(extremaY, minY);
|
||||
maxY = math.max(extremaY, maxY);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Chops cubic spline at startT and stopT, writes result to buffer.
|
||||
void _chopCubicBetweenT(
|
||||
List<double> points, double startT, double stopT, Float32List buffer) {
|
||||
assert(startT != 0 || stopT != 0);
|
||||
final double p3y = points[7];
|
||||
final double p0x = points[0];
|
||||
final double p0y = points[1];
|
||||
final double p1x = points[2];
|
||||
final double p1y = points[3];
|
||||
final double p2x = points[4];
|
||||
final double p2y = points[5];
|
||||
final double p3x = points[6];
|
||||
// If startT == 0 chop at end point and return curve.
|
||||
final bool chopStart = startT != 0;
|
||||
final double t = chopStart ? startT : stopT;
|
||||
|
||||
final double ab1x = _interpolate(p0x, p1x, t);
|
||||
final double ab1y = _interpolate(p0y, p1y, t);
|
||||
final double bc1x = _interpolate(p1x, p2x, t);
|
||||
final double bc1y = _interpolate(p1y, p2y, t);
|
||||
final double cd1x = _interpolate(p2x, p3x, t);
|
||||
final double cd1y = _interpolate(p2y, p3y, t);
|
||||
final double abc1x = _interpolate(ab1x, bc1x, t);
|
||||
final double abc1y = _interpolate(ab1y, bc1y, t);
|
||||
final double bcd1x = _interpolate(bc1x, cd1x, t);
|
||||
final double bcd1y = _interpolate(bc1y, cd1y, t);
|
||||
final double abcd1x = _interpolate(abc1x, bcd1x, t);
|
||||
final double abcd1y = _interpolate(abc1y, bcd1y, t);
|
||||
if (!chopStart) {
|
||||
// Return left side of curve.
|
||||
buffer[0] = p0x;
|
||||
buffer[1] = p0y;
|
||||
buffer[2] = ab1x;
|
||||
buffer[3] = ab1y;
|
||||
buffer[4] = abc1x;
|
||||
buffer[5] = abc1y;
|
||||
buffer[6] = abcd1x;
|
||||
buffer[7] = abcd1y;
|
||||
return;
|
||||
}
|
||||
if (stopT == 1) {
|
||||
// Return right side of curve.
|
||||
buffer[0] = abcd1x;
|
||||
buffer[1] = abcd1y;
|
||||
buffer[2] = bcd1x;
|
||||
buffer[3] = bcd1y;
|
||||
buffer[4] = cd1x;
|
||||
buffer[5] = cd1y;
|
||||
buffer[6] = p3x;
|
||||
buffer[7] = p3y;
|
||||
return;
|
||||
}
|
||||
// We chopped at startT, now the right hand side of curve is at
|
||||
// abcd1, bcd1, cd1, p3x, p3y. Chop this part using endT;
|
||||
final double endT = (stopT - startT) / (1 - startT);
|
||||
final double ab2x = _interpolate(abcd1x, bcd1x, endT);
|
||||
final double ab2y = _interpolate(abcd1y, bcd1y, endT);
|
||||
final double bc2x = _interpolate(bcd1x, cd1x, endT);
|
||||
final double bc2y = _interpolate(bcd1y, cd1y, endT);
|
||||
final double cd2x = _interpolate(cd1x, p3x, endT);
|
||||
final double cd2y = _interpolate(cd1y, p3y, endT);
|
||||
final double abc2x = _interpolate(ab2x, bc2x, endT);
|
||||
final double abc2y = _interpolate(ab2y, bc2y, endT);
|
||||
final double bcd2x = _interpolate(bc2x, cd2x, endT);
|
||||
final double bcd2y = _interpolate(bc2y, cd2y, endT);
|
||||
final double abcd2x = _interpolate(abc2x, bcd2x, endT);
|
||||
final double abcd2y = _interpolate(abc2y, bcd2y, endT);
|
||||
buffer[0] = abcd1x;
|
||||
buffer[1] = abcd1y;
|
||||
buffer[2] = ab2x;
|
||||
buffer[3] = ab2y;
|
||||
buffer[4] = abc2x;
|
||||
buffer[5] = abc2y;
|
||||
buffer[6] = abcd2x;
|
||||
buffer[7] = abcd2y;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -34,23 +34,27 @@ class SurfacePathMetrics extends IterableBase<ui.PathMetric>
|
||||
|
||||
/// Maintains a single instance of computed segments for set of PathMetric
|
||||
/// objects exposed through iterator.
|
||||
///
|
||||
/// [resScale] controls the precision of measure when values > 1.
|
||||
class _SurfacePathMeasure {
|
||||
_SurfacePathMeasure(this._path, this.forceClosed);
|
||||
_SurfacePathMeasure(this._path, this.forceClosed, {this.resScale = 1.0})
|
||||
:
|
||||
// nextContour will increment this to the zero based index.
|
||||
_currentContourIndex = -1,
|
||||
_pathIterator = PathIterator(_path.pathRef, forceClosed);
|
||||
|
||||
final double resScale;
|
||||
final SurfacePath _path;
|
||||
PathIterator _pathIterator;
|
||||
final List<_PathContourMeasure> _contours = [];
|
||||
|
||||
// If the contour ends with a call to [Path.close] (which may
|
||||
// have been implied when using [Path.addRect])
|
||||
final bool forceClosed;
|
||||
|
||||
// nextContour will increment this to the zero based index.
|
||||
int _currentContourIndex = -1;
|
||||
int _currentContourIndex;
|
||||
int get currentContourIndex => _currentContourIndex;
|
||||
|
||||
// Iterator index into [Path.subPaths]
|
||||
int _subPathIndex = -1;
|
||||
|
||||
double length(int contourIndex) {
|
||||
assert(contourIndex <= currentContourIndex,
|
||||
'Iterator must be advanced before index $contourIndex can be used.');
|
||||
@@ -85,6 +89,9 @@ class _SurfacePathMeasure {
|
||||
return next;
|
||||
}
|
||||
|
||||
// Iterator index into next contour.
|
||||
int _verbIterIndex = 0;
|
||||
|
||||
// Move to the next contour in the path.
|
||||
//
|
||||
// A path can have a next contour if [Path.moveTo] was called after drawing
|
||||
@@ -96,12 +103,13 @@ class _SurfacePathMeasure {
|
||||
// calling `_moveNext` - `_moveNext` should be called after the first
|
||||
// iteration is done instead of before.
|
||||
bool _nativeNextContour() {
|
||||
if (_subPathIndex == (_path.subpaths.length - 1)) {
|
||||
if (_verbIterIndex == _path.pathRef.countVerbs()) {
|
||||
return false;
|
||||
}
|
||||
++_subPathIndex;
|
||||
_contours
|
||||
.add(_PathContourMeasure(_path.subpaths[_subPathIndex], forceClosed));
|
||||
_PathContourMeasure measure =
|
||||
_PathContourMeasure(_path.pathRef, _pathIterator, forceClosed);
|
||||
_verbIterIndex = measure.verbEndIndex;
|
||||
_contours.add(measure);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -114,21 +122,23 @@ class _SurfacePathMeasure {
|
||||
/// Builds segments for a single contour to measure distance, compute tangent
|
||||
/// and extract a sub path.
|
||||
class _PathContourMeasure {
|
||||
_PathContourMeasure(this.subPath, this.forceClosed) {
|
||||
_buildSegments();
|
||||
_PathContourMeasure(this.pathRef, PathIterator iter, this.forceClosed) {
|
||||
_verbEndIndex = _buildSegments(iter);
|
||||
}
|
||||
|
||||
final PathRef pathRef;
|
||||
int _verbEndIndex = 0;
|
||||
final List<_PathSegment> _segments = [];
|
||||
// Allocate buffer large enough for returning cubic curve chop result.
|
||||
// 2 floats for each coordinate x (start, end & control point 1 & 2).
|
||||
static final Float32List _buffer = Float32List(8);
|
||||
|
||||
final Subpath subPath;
|
||||
final bool forceClosed;
|
||||
double get length => _contourLength;
|
||||
bool get isClosed => _isClosed;
|
||||
int get verbEndIndex => _verbEndIndex;
|
||||
|
||||
double _contourLength = 0;
|
||||
double _contourLength = 0.0;
|
||||
bool _isClosed = false;
|
||||
|
||||
ui.Tangent? getTangentForOffset(double distance) {
|
||||
@@ -237,183 +247,117 @@ class _PathContourMeasure {
|
||||
_PathSegment segment, double startT, double stopT, ui.Path path) {
|
||||
final List<double> points = segment.points;
|
||||
switch (segment.segmentType) {
|
||||
case PathCommandTypes.lineTo:
|
||||
case SPath.kLineVerb:
|
||||
final double toX = (points[2] * stopT) + (points[0] * (1.0 - stopT));
|
||||
final double toY = (points[3] * stopT) + (points[1] * (1.0 - stopT));
|
||||
path.lineTo(toX, toY);
|
||||
break;
|
||||
case PathCommandTypes.bezierCurveTo:
|
||||
_chopCubicAt(points, startT, stopT, _buffer);
|
||||
case SPath.kCubicVerb:
|
||||
_chopCubicBetweenT(points, startT, stopT, _buffer);
|
||||
path.cubicTo(_buffer[2], _buffer[3], _buffer[4], _buffer[5], _buffer[6],
|
||||
_buffer[7]);
|
||||
break;
|
||||
case PathCommandTypes.quadraticCurveTo:
|
||||
_chopQuadAt(points, startT, stopT, _buffer);
|
||||
case SPath.kQuadVerb:
|
||||
_chopQuadBetweenT(points, startT, stopT, _buffer);
|
||||
path.quadraticBezierTo(_buffer[2], _buffer[3], _buffer[4], _buffer[5]);
|
||||
break;
|
||||
case SPath.kConicVerb:
|
||||
// Implement this once we start writing out conic segments.
|
||||
throw UnimplementedError();
|
||||
default:
|
||||
throw UnsupportedError('Invalid segment type');
|
||||
}
|
||||
}
|
||||
|
||||
void _buildSegments() {
|
||||
/// Builds segments from contour starting at verb [_verbStartIndex] and
|
||||
/// returns next contour verb index.
|
||||
int _buildSegments(PathIterator iter) {
|
||||
assert(_segments.isEmpty, '_buildSegments should be called once');
|
||||
_isClosed = false;
|
||||
double distance = 0.0;
|
||||
bool haveSeenMoveTo = false;
|
||||
|
||||
final List<PathCommand> commands = subPath.commands;
|
||||
double currentX = 0.0, currentY = 0.0;
|
||||
final Function lineToHandler = (double x, double y) {
|
||||
final double dx = currentX - x;
|
||||
final double dy = currentY - y;
|
||||
final Function lineToHandler =
|
||||
(double fromX, double fromY, double x, double y) {
|
||||
final double dx = fromX - x;
|
||||
final double dy = fromY - y;
|
||||
final double prevDistance = distance;
|
||||
distance += math.sqrt(dx * dx + dy * dy);
|
||||
// As we accumulate distance, we have to check that the result of +=
|
||||
// actually made it larger, since a very small delta might be > 0, but
|
||||
// still have no effect on distance (if distance >>> delta).
|
||||
if (distance > prevDistance) {
|
||||
_segments.add(_PathSegment(
|
||||
PathCommandTypes.lineTo, distance, [currentX, currentY, x, y]));
|
||||
_segments
|
||||
.add(_PathSegment(SPath.kLineVerb, distance, [fromX, fromY, x, y]));
|
||||
}
|
||||
currentX = x;
|
||||
currentY = y;
|
||||
};
|
||||
|
||||
// TODO(yjbanov): make late final (https://github.com/dart-lang/sdk/issues/42422)
|
||||
_EllipseSegmentResult? ellipseResult;
|
||||
for (PathCommand command in commands) {
|
||||
switch (command.type) {
|
||||
case PathCommandTypes.moveTo:
|
||||
final MoveTo moveTo = command as MoveTo;
|
||||
currentX = moveTo.x;
|
||||
currentY = moveTo.y;
|
||||
int verb = 0;
|
||||
final Float32List points = Float32List(PathRefIterator.kMaxBufferSize);
|
||||
do {
|
||||
if (iter.peek() == SPath.kMoveVerb && haveSeenMoveTo) {
|
||||
break;
|
||||
}
|
||||
verb = iter.next(points);
|
||||
switch (verb) {
|
||||
case SPath.kMoveVerb:
|
||||
haveSeenMoveTo = true;
|
||||
break;
|
||||
case PathCommandTypes.lineTo:
|
||||
case SPath.kLineVerb:
|
||||
assert(haveSeenMoveTo);
|
||||
final LineTo lineTo = command as LineTo;
|
||||
lineToHandler(lineTo.x, lineTo.y);
|
||||
lineToHandler(points[0], points[1], points[2], points[3]);
|
||||
break;
|
||||
case PathCommandTypes.bezierCurveTo:
|
||||
case SPath.kCubicVerb:
|
||||
assert(haveSeenMoveTo);
|
||||
final BezierCurveTo curve = command as BezierCurveTo;
|
||||
// Compute cubic curve distance.
|
||||
distance = _computeCubicSegments(
|
||||
currentX,
|
||||
currentY,
|
||||
curve.x1,
|
||||
curve.y1,
|
||||
curve.x2,
|
||||
curve.y2,
|
||||
curve.x3,
|
||||
curve.y3,
|
||||
points[0],
|
||||
points[1],
|
||||
points[2],
|
||||
points[3],
|
||||
points[4],
|
||||
points[5],
|
||||
points[6],
|
||||
points[7],
|
||||
distance,
|
||||
0,
|
||||
_kMaxTValue,
|
||||
_segments);
|
||||
break;
|
||||
case PathCommandTypes.quadraticCurveTo:
|
||||
case SPath.kConicVerb:
|
||||
assert(haveSeenMoveTo);
|
||||
final double w = iter.conicWeight;
|
||||
Conic conic = Conic(points[0], points[1], points[2], points[3],
|
||||
points[4], points[5], w);
|
||||
List<ui.Offset> conicPoints = conic.toQuads();
|
||||
final int len = conicPoints.length;
|
||||
double startX = conicPoints[0].dx;
|
||||
double startY = conicPoints[0].dy;
|
||||
for (int i = 1; i < len; i += 2) {
|
||||
final double p1x = conicPoints[i].dx;
|
||||
final double p1y = conicPoints[i].dy;
|
||||
final double p2x = conicPoints[i + 1].dx;
|
||||
final double p2y = conicPoints[i + 1].dy;
|
||||
distance = _computeQuadSegments(
|
||||
startX, startY, p1x, p1y, p2x, p2y, distance, 0, _kMaxTValue);
|
||||
startX = p2x;
|
||||
startY = p2y;
|
||||
}
|
||||
break;
|
||||
case SPath.kQuadVerb:
|
||||
assert(haveSeenMoveTo);
|
||||
final QuadraticCurveTo quadraticCurveTo = command as QuadraticCurveTo;
|
||||
// Compute quad curve distance.
|
||||
distance = _computeQuadSegments(
|
||||
currentX,
|
||||
currentY,
|
||||
quadraticCurveTo.x1,
|
||||
quadraticCurveTo.y1,
|
||||
quadraticCurveTo.x2,
|
||||
quadraticCurveTo.y2,
|
||||
distance,
|
||||
0,
|
||||
_kMaxTValue);
|
||||
break;
|
||||
case PathCommandTypes.close:
|
||||
break;
|
||||
case PathCommandTypes.ellipse:
|
||||
final Ellipse ellipse = command as Ellipse;
|
||||
ellipseResult ??= _EllipseSegmentResult();
|
||||
_computeEllipseSegments(
|
||||
currentX,
|
||||
currentY,
|
||||
distance,
|
||||
ellipse.x,
|
||||
ellipse.y,
|
||||
ellipse.startAngle,
|
||||
ellipse.endAngle,
|
||||
ellipse.rotation,
|
||||
ellipse.radiusX,
|
||||
ellipse.radiusY,
|
||||
ellipse.anticlockwise,
|
||||
ellipseResult!,
|
||||
_segments);
|
||||
distance = ellipseResult!.distance;
|
||||
currentX = ellipseResult!.endPointX;
|
||||
currentY = ellipseResult!.endPointY;
|
||||
_isClosed = true;
|
||||
break;
|
||||
case PathCommandTypes.rRect:
|
||||
final RRectCommand rrectCommand = command as RRectCommand;
|
||||
final ui.RRect rrect = rrectCommand.rrect;
|
||||
RRectMetricsRenderer(moveToCallback: (double x, double y) {
|
||||
currentX = x;
|
||||
currentY = y;
|
||||
_isClosed = true;
|
||||
haveSeenMoveTo = true;
|
||||
}, lineToCallback: (double x, double y) {
|
||||
lineToHandler(x, y);
|
||||
}, ellipseCallback: (double centerX,
|
||||
double centerY,
|
||||
double radiusX,
|
||||
double radiusY,
|
||||
double rotation,
|
||||
double startAngle,
|
||||
double endAngle,
|
||||
bool antiClockwise) {
|
||||
ellipseResult ??= _EllipseSegmentResult();
|
||||
_computeEllipseSegments(
|
||||
currentX,
|
||||
currentY,
|
||||
distance,
|
||||
centerX,
|
||||
centerY,
|
||||
startAngle,
|
||||
endAngle,
|
||||
rotation,
|
||||
radiusX,
|
||||
radiusY,
|
||||
antiClockwise,
|
||||
ellipseResult!,
|
||||
_segments);
|
||||
distance = ellipseResult!.distance;
|
||||
currentX = ellipseResult!.endPointX;
|
||||
currentY = ellipseResult!.endPointY;
|
||||
}).render(rrect);
|
||||
_isClosed = true;
|
||||
break;
|
||||
case PathCommandTypes.rect:
|
||||
final RectCommand rectCommand = command as RectCommand;
|
||||
final double x = rectCommand.x;
|
||||
final double y = rectCommand.y;
|
||||
final double width = rectCommand.width;
|
||||
final double height = rectCommand.height;
|
||||
currentX = x;
|
||||
currentY = y;
|
||||
lineToHandler(x + width, y);
|
||||
lineToHandler(x + width, y + height);
|
||||
lineToHandler(x, y + height);
|
||||
lineToHandler(x, y);
|
||||
_isClosed = true;
|
||||
distance = _computeQuadSegments(points[0], points[1], points[2],
|
||||
points[3], points[4], points[5], distance, 0, _kMaxTValue);
|
||||
break;
|
||||
case SPath.kCloseVerb:
|
||||
_contourLength = distance;
|
||||
return iter._verbIndex;
|
||||
default:
|
||||
throw UnimplementedError('Unknown path command $command');
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!_isClosed && forceClosed && _segments.isNotEmpty) {
|
||||
_PathSegment firstSegment = _segments.first;
|
||||
lineToHandler(firstSegment.points[0], firstSegment.points[1]);
|
||||
}
|
||||
} while (verb != SPath.kDoneVerb);
|
||||
_contourLength = distance;
|
||||
return iter._verbIndex;
|
||||
}
|
||||
|
||||
static bool _tspanBigEnough(int tSpan) => (tSpan >> 10) != 0;
|
||||
@@ -487,7 +431,7 @@ class _PathContourMeasure {
|
||||
final double prevDistance = distance;
|
||||
distance += startToEndDistance;
|
||||
if (distance > prevDistance) {
|
||||
segments.add(_PathSegment(PathCommandTypes.bezierCurveTo, distance,
|
||||
segments.add(_PathSegment(SPath.kCubicVerb, distance,
|
||||
<double>[x0, y0, x1, y1, x2, y2, x3, y3]));
|
||||
}
|
||||
}
|
||||
@@ -529,88 +473,17 @@ class _PathContourMeasure {
|
||||
final double prevDistance = distance;
|
||||
distance += startToEndDistance;
|
||||
if (distance > prevDistance) {
|
||||
_segments.add(_PathSegment(PathCommandTypes.quadraticCurveTo, distance,
|
||||
<double>[x0, y0, x1, y1, x2, y2]));
|
||||
_segments.add(_PathSegment(
|
||||
SPath.kQuadVerb, distance, <double>[x0, y0, x1, y1, x2, y2]));
|
||||
}
|
||||
}
|
||||
return distance;
|
||||
}
|
||||
|
||||
// Create segments by converting arc to cubics.
|
||||
// See http://www.w3.org/TR/SVG/implnote.html#ArcConversionEndpointToCenter.
|
||||
static void _computeEllipseSegments(
|
||||
double startX,
|
||||
double startY,
|
||||
double distance,
|
||||
double cx,
|
||||
double cy,
|
||||
double startAngle,
|
||||
double endAngle,
|
||||
double rotation,
|
||||
double radiusX,
|
||||
double radiusY,
|
||||
bool anticlockwise,
|
||||
_EllipseSegmentResult result,
|
||||
List<_PathSegment> segments) {
|
||||
final double endX = cx + (radiusX * math.cos(endAngle));
|
||||
final double endY = cy + (radiusY * math.sin(endAngle));
|
||||
result.endPointX = endX;
|
||||
result.endPointY = endY;
|
||||
// Check for http://www.w3.org/TR/SVG/implnote.html#ArcOutOfRangeParameters
|
||||
// Treat as line segment from start to end if arc has zero radii.
|
||||
// If start and end point are the same treat as zero length path.
|
||||
if ((radiusX == 0 || radiusY == 0) || (startX == endX && startY == endY)) {
|
||||
result.distance = distance;
|
||||
return;
|
||||
}
|
||||
final double rxAbs = radiusX.abs();
|
||||
final double ryAbs = radiusY.abs();
|
||||
|
||||
final double theta1 = startAngle;
|
||||
final double theta2 = endAngle;
|
||||
final double thetaArc = theta2 - theta1;
|
||||
|
||||
// Add 0.01f to make sure we have enough segments when thetaArc is close
|
||||
// to pi/2.
|
||||
final int numSegments = (thetaArc / ((math.pi / 2.0) + 0.01)).abs().ceil();
|
||||
double x0 = startX;
|
||||
double y0 = startY;
|
||||
for (int segmentIndex = 0; segmentIndex < numSegments; segmentIndex++) {
|
||||
final double startTheta =
|
||||
theta1 + (segmentIndex * thetaArc / numSegments);
|
||||
final double endTheta =
|
||||
theta1 + ((segmentIndex + 1) * thetaArc / numSegments);
|
||||
final double t = (4.0 / 3.0) * math.tan((endTheta - startTheta) / 4);
|
||||
if (!t.isFinite) {
|
||||
result.distance = distance;
|
||||
return;
|
||||
}
|
||||
final double sinStartTheta = math.sin(startTheta);
|
||||
final double cosStartTheta = math.cos(startTheta);
|
||||
final double sinEndTheta = math.sin(endTheta);
|
||||
final double cosEndTheta = math.cos(endTheta);
|
||||
|
||||
// Compute cubic segment start, control point and end (target).
|
||||
final double p1x = rxAbs * (cosStartTheta - t * sinStartTheta) + cx;
|
||||
final double p1y = ryAbs * (sinStartTheta + t * cosStartTheta) + cy;
|
||||
final double targetPointX = rxAbs * cosEndTheta + cx;
|
||||
final double targetPointY = ryAbs * sinEndTheta + cy;
|
||||
final double p2x = targetPointX + rxAbs * (t * sinEndTheta);
|
||||
final double p2y = targetPointY + ryAbs * (-t * cosEndTheta);
|
||||
|
||||
distance = _computeCubicSegments(x0, y0, p1x, p1y, p2x, p2y, targetPointX,
|
||||
targetPointY, distance, 0, _kMaxTValue, segments);
|
||||
x0 = targetPointX;
|
||||
y0 = targetPointY;
|
||||
}
|
||||
result.distance = distance;
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks iteration from one segment of a path to the next for measurement.
|
||||
class SurfacePathMetricIterator implements Iterator<ui.PathMetric> {
|
||||
// ignore: unnecessary_null_comparison
|
||||
SurfacePathMetricIterator._(this._pathMeasure) : assert(_pathMeasure != null);
|
||||
SurfacePathMetricIterator._(this._pathMeasure);
|
||||
|
||||
SurfacePathMetric? _pathMetric;
|
||||
_SurfacePathMeasure _pathMeasure;
|
||||
@@ -653,8 +526,7 @@ const double _fTolerance = 0.5;
|
||||
/// to maintain consistency with native platforms.
|
||||
class SurfacePathMetric implements ui.PathMetric {
|
||||
SurfacePathMetric._(this._measure)
|
||||
: assert(_measure != null), // ignore: unnecessary_null_comparison
|
||||
length = _measure.length(_measure.currentContourIndex),
|
||||
: length = _measure.length(_measure.currentContourIndex),
|
||||
isClosed = _measure.isClosed(_measure.currentContourIndex),
|
||||
contourIndex = _measure.currentContourIndex;
|
||||
|
||||
@@ -716,12 +588,6 @@ class SurfacePathMetric implements ui.PathMetric {
|
||||
String toString() => 'PathMetric';
|
||||
}
|
||||
|
||||
class _EllipseSegmentResult {
|
||||
double endPointX = 0;
|
||||
double endPointY = 0;
|
||||
double distance = 0;
|
||||
}
|
||||
|
||||
// Given a vector dx, dy representing slope, normalize and return as [ui.Offset].
|
||||
ui.Offset _normalizeSlope(double dx, double dy) {
|
||||
final double length = math.sqrt(dx * dx + dy * dy);
|
||||
@@ -732,10 +598,7 @@ ui.Offset _normalizeSlope(double dx, double dy) {
|
||||
|
||||
class _SurfaceTangent extends ui.Tangent {
|
||||
const _SurfaceTangent(ui.Offset position, ui.Offset vector, this.t)
|
||||
: assert(position != null), // ignore: unnecessary_null_comparison
|
||||
assert(vector != null), // ignore: unnecessary_null_comparison
|
||||
assert(t != null), // ignore: unnecessary_null_comparison
|
||||
super(position, vector);
|
||||
: super(position, vector);
|
||||
|
||||
// Normalized distance of tangent point from start of a contour.
|
||||
final double t;
|
||||
@@ -750,16 +613,16 @@ class _PathSegment {
|
||||
|
||||
_SurfaceTangent computeTangent(double t) {
|
||||
switch (segmentType) {
|
||||
case PathCommandTypes.lineTo:
|
||||
case SPath.kLineVerb:
|
||||
// Simple line. Position is simple interpolation from start to end point.
|
||||
final double xAtDistance = (points[2] * t) + (points[0] * (1.0 - t));
|
||||
final double yAtDistance = (points[3] * t) + (points[1] * (1.0 - t));
|
||||
return _SurfaceTangent(ui.Offset(xAtDistance, yAtDistance),
|
||||
_normalizeSlope(points[2] - points[0], points[3] - points[1]), t);
|
||||
case PathCommandTypes.bezierCurveTo:
|
||||
case SPath.kCubicVerb:
|
||||
return tangentForCubicAt(t, points[0], points[1], points[2], points[3],
|
||||
points[4], points[5], points[6], points[7]);
|
||||
case PathCommandTypes.quadraticCurveTo:
|
||||
case SPath.kQuadVerb:
|
||||
return tangentForQuadAt(t, points[0], points[1], points[2], points[3],
|
||||
points[4], points[5]);
|
||||
default:
|
||||
@@ -856,85 +719,8 @@ class _SkCubicCoefficients {
|
||||
double evalY(double t) => (((ay * t + by) * t) + cy) * t + dy;
|
||||
}
|
||||
|
||||
/// Chops cubic spline at startT and stopT, writes result to buffer.
|
||||
void _chopCubicAt(
|
||||
List<double> points, double startT, double stopT, Float32List buffer) {
|
||||
assert(startT != 0 || stopT != 0);
|
||||
final double p3y = points[7];
|
||||
final double p0x = points[0];
|
||||
final double p0y = points[1];
|
||||
final double p1x = points[2];
|
||||
final double p1y = points[3];
|
||||
final double p2x = points[4];
|
||||
final double p2y = points[5];
|
||||
final double p3x = points[6];
|
||||
// If startT == 0 chop at end point and return curve.
|
||||
final bool chopStart = startT != 0;
|
||||
final double t = chopStart ? startT : stopT;
|
||||
|
||||
final double ab1x = _interpolate(p0x, p1x, t);
|
||||
final double ab1y = _interpolate(p0y, p1y, t);
|
||||
final double bc1x = _interpolate(p1x, p2x, t);
|
||||
final double bc1y = _interpolate(p1y, p2y, t);
|
||||
final double cd1x = _interpolate(p2x, p3x, t);
|
||||
final double cd1y = _interpolate(p2y, p3y, t);
|
||||
final double abc1x = _interpolate(ab1x, bc1x, t);
|
||||
final double abc1y = _interpolate(ab1y, bc1y, t);
|
||||
final double bcd1x = _interpolate(bc1x, cd1x, t);
|
||||
final double bcd1y = _interpolate(bc1y, cd1y, t);
|
||||
final double abcd1x = _interpolate(abc1x, bcd1x, t);
|
||||
final double abcd1y = _interpolate(abc1y, bcd1y, t);
|
||||
if (!chopStart) {
|
||||
// Return left side of curve.
|
||||
buffer[0] = p0x;
|
||||
buffer[1] = p0y;
|
||||
buffer[2] = ab1x;
|
||||
buffer[3] = ab1y;
|
||||
buffer[4] = abc1x;
|
||||
buffer[5] = abc1y;
|
||||
buffer[6] = abcd1x;
|
||||
buffer[7] = abcd1y;
|
||||
return;
|
||||
}
|
||||
if (stopT == 1) {
|
||||
// Return right side of curve.
|
||||
buffer[0] = abcd1x;
|
||||
buffer[1] = abcd1y;
|
||||
buffer[2] = bcd1x;
|
||||
buffer[3] = bcd1y;
|
||||
buffer[4] = cd1x;
|
||||
buffer[5] = cd1y;
|
||||
buffer[6] = p3x;
|
||||
buffer[7] = p3y;
|
||||
return;
|
||||
}
|
||||
// We chopped at startT, now the right hand side of curve is at
|
||||
// abcd1, bcd1, cd1, p3x, p3y. Chop this part using endT;
|
||||
final double endT = (stopT - startT) / (1 - startT);
|
||||
final double ab2x = _interpolate(abcd1x, bcd1x, endT);
|
||||
final double ab2y = _interpolate(abcd1y, bcd1y, endT);
|
||||
final double bc2x = _interpolate(bcd1x, cd1x, endT);
|
||||
final double bc2y = _interpolate(bcd1y, cd1y, endT);
|
||||
final double cd2x = _interpolate(cd1x, p3x, endT);
|
||||
final double cd2y = _interpolate(cd1y, p3y, endT);
|
||||
final double abc2x = _interpolate(ab2x, bc2x, endT);
|
||||
final double abc2y = _interpolate(ab2y, bc2y, endT);
|
||||
final double bcd2x = _interpolate(bc2x, cd2x, endT);
|
||||
final double bcd2y = _interpolate(bc2y, cd2y, endT);
|
||||
final double abcd2x = _interpolate(abc2x, bcd2x, endT);
|
||||
final double abcd2y = _interpolate(abc2y, bcd2y, endT);
|
||||
buffer[0] = abcd1x;
|
||||
buffer[1] = abcd1y;
|
||||
buffer[2] = ab2x;
|
||||
buffer[3] = ab2y;
|
||||
buffer[4] = abc2x;
|
||||
buffer[5] = abc2y;
|
||||
buffer[6] = abcd2x;
|
||||
buffer[7] = abcd2y;
|
||||
}
|
||||
|
||||
/// Chops quadratic curve at startT and stopT and writes result to buffer.
|
||||
void _chopQuadAt(
|
||||
void _chopQuadBetweenT(
|
||||
List<double> points, double startT, double stopT, Float32List buffer) {
|
||||
assert(startT != 0 || stopT != 0);
|
||||
final double p2y = points[5];
|
||||
@@ -991,8 +777,3 @@ void _chopQuadAt(
|
||||
buffer[4] = abc2x;
|
||||
buffer[5] = abc2y;
|
||||
}
|
||||
|
||||
// Interpolate between two doubles (Not using lerpDouble here since it null
|
||||
// checks and treats values as 0).
|
||||
double _interpolate(double startValue, double endValue, double t) =>
|
||||
(startValue * (1 - t)) + endValue * t;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -8,184 +8,45 @@ part of engine;
|
||||
/// element.
|
||||
void pathToSvg(SurfacePath path, StringBuffer sb,
|
||||
{double offsetX = 0, double offsetY = 0}) {
|
||||
for (Subpath subPath in path.subpaths) {
|
||||
for (PathCommand command in subPath.commands) {
|
||||
switch (command.type) {
|
||||
case PathCommandTypes.moveTo:
|
||||
final MoveTo moveTo = command as MoveTo;
|
||||
sb.write('M ${moveTo.x + offsetX} ${moveTo.y + offsetY}');
|
||||
break;
|
||||
case PathCommandTypes.lineTo:
|
||||
final LineTo lineTo = command as LineTo;
|
||||
sb.write('L ${lineTo.x + offsetX} ${lineTo.y + offsetY}');
|
||||
break;
|
||||
case PathCommandTypes.bezierCurveTo:
|
||||
final BezierCurveTo curve = command as BezierCurveTo;
|
||||
sb.write('C ${curve.x1 + offsetX} ${curve.y1 + offsetY} '
|
||||
'${curve.x2 + offsetX} ${curve.y2 + offsetY} ${curve.x3 + offsetX} ${curve.y3 + offsetY}');
|
||||
break;
|
||||
case PathCommandTypes.quadraticCurveTo:
|
||||
final QuadraticCurveTo quadraticCurveTo = command as QuadraticCurveTo;
|
||||
sb.write(
|
||||
'Q ${quadraticCurveTo.x1 + offsetX} ${quadraticCurveTo.y1 + offsetY} '
|
||||
'${quadraticCurveTo.x2 + offsetX} ${quadraticCurveTo.y2 + offsetY}');
|
||||
break;
|
||||
case PathCommandTypes.close:
|
||||
sb.write('Z');
|
||||
break;
|
||||
case PathCommandTypes.ellipse:
|
||||
final Ellipse ellipse = command as Ellipse;
|
||||
// Handle edge case where start and end points are the same by drawing
|
||||
// 2 half arcs.
|
||||
if ((ellipse.endAngle - ellipse.startAngle) % (2 * math.pi) == 0.0) {
|
||||
_writeEllipse(
|
||||
sb,
|
||||
ellipse.x + offsetX,
|
||||
ellipse.y + offsetY,
|
||||
ellipse.radiusX,
|
||||
ellipse.radiusY,
|
||||
ellipse.rotation,
|
||||
-math.pi,
|
||||
0,
|
||||
ellipse.anticlockwise,
|
||||
moveToStartPoint: true);
|
||||
_writeEllipse(
|
||||
sb,
|
||||
ellipse.x + offsetX,
|
||||
ellipse.y + offsetY,
|
||||
ellipse.radiusX,
|
||||
ellipse.radiusY,
|
||||
ellipse.rotation,
|
||||
0,
|
||||
math.pi,
|
||||
ellipse.anticlockwise);
|
||||
} else {
|
||||
_writeEllipse(
|
||||
sb,
|
||||
ellipse.x + offsetX,
|
||||
ellipse.y + offsetY,
|
||||
ellipse.radiusX,
|
||||
ellipse.radiusY,
|
||||
ellipse.rotation,
|
||||
ellipse.startAngle,
|
||||
ellipse.endAngle,
|
||||
ellipse.anticlockwise);
|
||||
}
|
||||
break;
|
||||
case PathCommandTypes.rRect:
|
||||
final RRectCommand rrectCommand = command as RRectCommand;
|
||||
final ui.RRect rrect = rrectCommand.rrect;
|
||||
double left = rrect.left + offsetX;
|
||||
double right = rrect.right + offsetX;
|
||||
double top = rrect.top + offsetY;
|
||||
double bottom = rrect.bottom + offsetY;
|
||||
if (left > right) {
|
||||
left = right;
|
||||
right = rrect.left + offsetX;
|
||||
}
|
||||
if (top > bottom) {
|
||||
top = bottom;
|
||||
bottom = rrect.top + offsetY;
|
||||
}
|
||||
final double trRadiusX = rrect.trRadiusX.abs();
|
||||
final double tlRadiusX = rrect.tlRadiusX.abs();
|
||||
final double trRadiusY = rrect.trRadiusY.abs();
|
||||
final double tlRadiusY = rrect.tlRadiusY.abs();
|
||||
final double blRadiusX = rrect.blRadiusX.abs();
|
||||
final double brRadiusX = rrect.brRadiusX.abs();
|
||||
final double blRadiusY = rrect.blRadiusY.abs();
|
||||
final double brRadiusY = rrect.brRadiusY.abs();
|
||||
|
||||
sb.write('M ${left + trRadiusX} $top ');
|
||||
// Top side and top-right corner
|
||||
sb.write('L ${right - trRadiusX} $top ');
|
||||
_writeEllipse(sb, right - trRadiusX, top + trRadiusY, trRadiusX,
|
||||
trRadiusY, 0, 1.5 * math.pi, 2.0 * math.pi, false);
|
||||
// Right side and bottom-right corner
|
||||
sb.write('L $right ${bottom - brRadiusY} ');
|
||||
_writeEllipse(sb, right - brRadiusX, bottom - brRadiusY, brRadiusX,
|
||||
brRadiusY, 0, 0, 0.5 * math.pi, false);
|
||||
// Bottom side and bottom-left corner
|
||||
sb.write('L ${left + blRadiusX} $bottom ');
|
||||
_writeEllipse(sb, left + blRadiusX, bottom - blRadiusY, blRadiusX,
|
||||
blRadiusY, 0, 0.5 * math.pi, math.pi, false);
|
||||
// Left side and top-left corner
|
||||
sb.write('L $left ${top + tlRadiusY} ');
|
||||
_writeEllipse(
|
||||
sb,
|
||||
left + tlRadiusX,
|
||||
top + tlRadiusY,
|
||||
tlRadiusX,
|
||||
tlRadiusY,
|
||||
0,
|
||||
math.pi,
|
||||
1.5 * math.pi,
|
||||
false,
|
||||
);
|
||||
break;
|
||||
case PathCommandTypes.rect:
|
||||
final RectCommand rectCommand = command as RectCommand;
|
||||
final bool horizontalSwap = rectCommand.width < 0;
|
||||
final double left = offsetX +
|
||||
(horizontalSwap
|
||||
? rectCommand.x - rectCommand.width
|
||||
: rectCommand.x);
|
||||
final double width =
|
||||
horizontalSwap ? -rectCommand.width : rectCommand.width;
|
||||
final bool verticalSwap = rectCommand.height < 0;
|
||||
final double top = offsetY +
|
||||
(verticalSwap
|
||||
? rectCommand.y - rectCommand.height
|
||||
: rectCommand.y);
|
||||
final double height =
|
||||
verticalSwap ? -rectCommand.height : rectCommand.height;
|
||||
sb.write('M $left $top ');
|
||||
sb.write('L ${left + width} $top ');
|
||||
sb.write('L ${left + width} ${top + height} ');
|
||||
sb.write('L $left ${top + height} ');
|
||||
sb.write('L $left $top ');
|
||||
break;
|
||||
default:
|
||||
throw UnimplementedError('Unknown path command $command');
|
||||
}
|
||||
final PathRefIterator iter = PathRefIterator(path.pathRef);
|
||||
int verb = 0;
|
||||
final Float32List outPts = Float32List(PathRefIterator.kMaxBufferSize);
|
||||
while ((verb = iter.next(outPts)) != SPath.kDoneVerb) {
|
||||
switch (verb) {
|
||||
case SPath.kMoveVerb:
|
||||
sb.write('M ${outPts[0] + offsetX} ${outPts[1] + offsetY}');
|
||||
break;
|
||||
case SPath.kLineVerb:
|
||||
sb.write('L ${outPts[2] + offsetX} ${outPts[3] + offsetY}');
|
||||
break;
|
||||
case SPath.kCubicVerb:
|
||||
sb.write('C ${outPts[2] + offsetX} ${outPts[3] + offsetY} '
|
||||
'${outPts[4] + offsetX} ${outPts[5] + offsetY} ${outPts[6] + offsetX} ${outPts[7] + offsetY}');
|
||||
break;
|
||||
case SPath.kQuadVerb:
|
||||
sb.write('Q ${outPts[2] + offsetX} ${outPts[3] + offsetY} '
|
||||
'${outPts[4] + offsetX} ${outPts[5] + offsetY}');
|
||||
break;
|
||||
case SPath.kConicVerb:
|
||||
final double w = iter.conicWeight;
|
||||
Conic conic = Conic(outPts[0], outPts[1], outPts[2], outPts[3],
|
||||
outPts[4], outPts[5], w);
|
||||
List<ui.Offset> points = conic.toQuads();
|
||||
final int len = points.length;
|
||||
for (int i = 1; i < len; i += 2) {
|
||||
final double p1x = points[i].dx;
|
||||
final double p1y = points[i].dy;
|
||||
final double p2x = points[i + 1].dx;
|
||||
final double p2y = points[i + 1].dy;
|
||||
sb.write('Q ${p1x + offsetX} ${p1y + offsetY} '
|
||||
'${p2x + offsetX} ${p2y + offsetY}');
|
||||
}
|
||||
break;
|
||||
case SPath.kCloseVerb:
|
||||
sb.write('Z');
|
||||
break;
|
||||
default:
|
||||
throw UnimplementedError('Unknown path verb $verb');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// See https://www.w3.org/TR/SVG/implnote.html B.2.3. Conversion from center to
|
||||
// endpoint parameterization.
|
||||
void _writeEllipse(
|
||||
StringBuffer sb,
|
||||
double cx,
|
||||
double cy,
|
||||
double radiusX,
|
||||
double radiusY,
|
||||
double rotation,
|
||||
double startAngle,
|
||||
double endAngle,
|
||||
bool antiClockwise,
|
||||
{bool moveToStartPoint = false}) {
|
||||
final double cosRotation = math.cos(rotation);
|
||||
final double sinRotation = math.sin(rotation);
|
||||
final double x = math.cos(startAngle) * radiusX;
|
||||
final double y = math.sin(startAngle) * radiusY;
|
||||
|
||||
final double startPx = cx + (cosRotation * x - sinRotation * y);
|
||||
final double startPy = cy + (sinRotation * x + cosRotation * y);
|
||||
|
||||
final double xe = math.cos(endAngle) * radiusX;
|
||||
final double ye = math.sin(endAngle) * radiusY;
|
||||
|
||||
final double endPx = cx + (cosRotation * xe - sinRotation * ye);
|
||||
final double endPy = cy + (sinRotation * xe + cosRotation * ye);
|
||||
|
||||
final double delta = endAngle - startAngle;
|
||||
final bool largeArc = delta.abs() > math.pi;
|
||||
|
||||
final double rotationDeg = rotation / math.pi * 180.0;
|
||||
if (moveToStartPoint) {
|
||||
sb.write('M $startPx $startPy ');
|
||||
}
|
||||
sb.write('A $radiusX $radiusY $rotationDeg '
|
||||
'${largeArc ? 1 : 0} ${antiClockwise ? 0 : 1} $endPx $endPy');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,412 @@
|
||||
// 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.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// Mask used to keep track of types of verbs used in a path segment.
|
||||
class SPathSegmentMask {
|
||||
static const int kLine_SkPathSegmentMask = 1 << 0;
|
||||
static const int kQuad_SkPathSegmentMask = 1 << 1;
|
||||
static const int kConic_SkPathSegmentMask = 1 << 2;
|
||||
static const int kCubic_SkPathSegmentMask = 1 << 3;
|
||||
}
|
||||
|
||||
/// Types of path operations.
|
||||
class SPathVerb {
|
||||
static const int kMove = 1; // 1 point
|
||||
static const int kLine = 2; // 2 points
|
||||
static const int kQuad = 3; // 3 points
|
||||
static const int kConic = 4; // 3 points + 1 weight
|
||||
static const int kCubic = 5; // 4 points
|
||||
static const int kClose = 6; // 0 points
|
||||
}
|
||||
|
||||
class SPath {
|
||||
static const int kMoveVerb = SPathVerb.kMove;
|
||||
static const int kLineVerb = SPathVerb.kLine;
|
||||
static const int kQuadVerb = SPathVerb.kQuad;
|
||||
static const int kConicVerb = SPathVerb.kConic;
|
||||
static const int kCubicVerb = SPathVerb.kCubic;
|
||||
static const int kCloseVerb = SPathVerb.kClose;
|
||||
static const int kDoneVerb = SPathVerb.kClose + 1;
|
||||
|
||||
static const int kLineSegmentMask = SPathSegmentMask.kLine_SkPathSegmentMask;
|
||||
static const int kQuadSegmentMask = SPathSegmentMask.kQuad_SkPathSegmentMask;
|
||||
static const int kConicSegmentMask =
|
||||
SPathSegmentMask.kConic_SkPathSegmentMask;
|
||||
static const int kCubicSegmentMask =
|
||||
SPathSegmentMask.kCubic_SkPathSegmentMask;
|
||||
|
||||
static const double scalarNearlyZero = 1.0 / (1 << 12);
|
||||
|
||||
/// Square root of 2 divided by 2. Useful for sin45 = cos45 = 1/sqrt(2).
|
||||
static const double scalarRoot2Over2 = 0.707106781;
|
||||
|
||||
/// True if (a <= b <= c) || (a >= b >= c)
|
||||
static bool between(double a, double b, double c) {
|
||||
return (a - b) * (c - b) <= 0;
|
||||
}
|
||||
|
||||
/// Returns -1 || 0 || 1 depending on the sign of value:
|
||||
/// -1 if x < 0
|
||||
/// 0 if x == 0
|
||||
/// 1 if x > 0
|
||||
static int scalarSignedAsInt(double x) {
|
||||
return x < 0 ? -1 : ((x > 0) ? 1 : 0);
|
||||
}
|
||||
}
|
||||
|
||||
class SPathAddPathMode {
|
||||
// Append to destination unaltered.
|
||||
static const int kAppend = 0;
|
||||
// Add line if prior contour is not closed.
|
||||
static const int kExtend = 1;
|
||||
}
|
||||
|
||||
class SPathDirection {
|
||||
/// Uninitialized value for empty paths.
|
||||
static const int kUnknown = -1;
|
||||
|
||||
/// clockwise direction for adding closed contours.
|
||||
static const int kCW = 0;
|
||||
|
||||
/// counter-clockwise direction for adding closed contours.
|
||||
static const int kCCW = 1;
|
||||
}
|
||||
|
||||
class SPathConvexityType {
|
||||
static const int kUnknown = -1;
|
||||
static const int kConvex = 0;
|
||||
static const int kConcave = 1;
|
||||
}
|
||||
|
||||
class SPathSegmentState {
|
||||
/// The current contour is empty. Starting processing or have just closed
|
||||
/// a contour.
|
||||
static const int kEmptyContour = 0;
|
||||
|
||||
/// Have seen a move, but nothing else.
|
||||
static const int kAfterMove = 1;
|
||||
|
||||
/// Have seen a primitive but not yet closed the path. Also the initial state.
|
||||
static const int kAfterPrimitive = 2;
|
||||
}
|
||||
|
||||
/// Quadratic roots. See Numerical Recipes in C.
|
||||
///
|
||||
/// Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C])
|
||||
/// x1 = Q / A
|
||||
/// x2 = C / Q
|
||||
class _QuadRoots {
|
||||
double? root0;
|
||||
double? root1;
|
||||
|
||||
_QuadRoots();
|
||||
|
||||
/// Returns roots as list.
|
||||
List<double> get roots => (root0 == null)
|
||||
? []
|
||||
: (root1 == null ? <double>[root0!] : <double>[root0!, root1!]);
|
||||
|
||||
int findRoots(double a, double b, double c) {
|
||||
int rootCount = 0;
|
||||
if (a == 0) {
|
||||
root0 = _validUnitDivide(-c, b);
|
||||
return root0 == null ? 0 : 1;
|
||||
}
|
||||
|
||||
double dr = b * b - 4 * a * c;
|
||||
if (dr < 0) {
|
||||
return 0;
|
||||
}
|
||||
dr = math.sqrt(dr);
|
||||
if (!dr.isFinite) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
double q = (b < 0) ? -(b - dr) / 2 : -(b + dr) / 2;
|
||||
double? res = _validUnitDivide(q, a);
|
||||
if (res != null) {
|
||||
root0 = res;
|
||||
++rootCount;
|
||||
}
|
||||
res = _validUnitDivide(c, q);
|
||||
if (res != null) {
|
||||
if (rootCount == 0) {
|
||||
root0 = res;
|
||||
++rootCount;
|
||||
} else {
|
||||
root1 = res;
|
||||
++rootCount;
|
||||
}
|
||||
}
|
||||
if (rootCount == 2) {
|
||||
if (root0! > root1!) {
|
||||
final double swap = root0!;
|
||||
root0 = root1;
|
||||
root1 = swap;
|
||||
} else if (root0 == root1) {
|
||||
return 1; // skip the double root
|
||||
}
|
||||
}
|
||||
return rootCount;
|
||||
}
|
||||
}
|
||||
|
||||
double? _validUnitDivide(double numer, double denom) {
|
||||
if (numer < 0) {
|
||||
numer = -numer;
|
||||
denom = -denom;
|
||||
}
|
||||
if (denom == 0 || numer == 0 || numer >= denom) {
|
||||
return null;
|
||||
}
|
||||
final double r = numer / denom;
|
||||
if (r.isNaN) {
|
||||
return null;
|
||||
}
|
||||
if (r == 0) {
|
||||
// catch underflow if numer <<<< denom
|
||||
return null;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
// Snaps a value to zero if almost zero (within tolerance).
|
||||
double _snapToZero(double value) => _nearlyEqual(value, 0.0) ? 0.0 : value;
|
||||
|
||||
bool _nearlyEqual(double value1, double value2) =>
|
||||
(value1 - value2).abs() < SPath.scalarNearlyZero;
|
||||
|
||||
bool _isInteger(double value) => value.floor() == value;
|
||||
|
||||
bool _isRRectOval(ui.RRect rrect) {
|
||||
if ((rrect.tlRadiusX + rrect.trRadiusX) != rrect.width) {
|
||||
return false;
|
||||
}
|
||||
if ((rrect.tlRadiusY + rrect.trRadiusY) != rrect.height) {
|
||||
return false;
|
||||
}
|
||||
if (rrect.tlRadiusX != rrect.blRadiusX ||
|
||||
rrect.trRadiusX != rrect.brRadiusX ||
|
||||
rrect.tlRadiusY != rrect.blRadiusY ||
|
||||
rrect.trRadiusY != rrect.brRadiusY) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Evaluates degree 2 polynomial (quadratic).
|
||||
double polyEval(double A, double B, double C, double t) => (A * t + B) * t + C;
|
||||
|
||||
/// Evaluates degree 3 polynomial (cubic).
|
||||
double polyEval4(double A, double B, double C, double D, double t) =>
|
||||
((A * t + B) * t + C) * t + D;
|
||||
|
||||
// Interpolate between two doubles (Not using lerpDouble here since it null
|
||||
// checks and treats values as 0).
|
||||
double _interpolate(double startValue, double endValue, double t) =>
|
||||
(startValue * (1 - t)) + endValue * t;
|
||||
|
||||
double _dotProduct(double x0, double y0, double x1, double y1) {
|
||||
return x0 * x1 + y0 * y1;
|
||||
}
|
||||
|
||||
// Helper class for computing convexity for a single contour.
|
||||
//
|
||||
// Iteratively looks at angle (using cross product) between consecutive vectors
|
||||
// formed by path.
|
||||
class Convexicator {
|
||||
static const int kValueNeverReturnedBySign = 2;
|
||||
|
||||
// Second point of contour start that forms a vector.
|
||||
// Used to handle close operator to compute angle between last vector and
|
||||
// first.
|
||||
double? firstVectorEndPointX;
|
||||
double? firstVectorEndPointY;
|
||||
|
||||
double? priorX;
|
||||
double? priorY;
|
||||
|
||||
double? lastX;
|
||||
double? lastY;
|
||||
|
||||
double? currX;
|
||||
double? currY;
|
||||
|
||||
// Last vector to use to compute angle.
|
||||
double? lastVecX;
|
||||
double? lastVecY;
|
||||
|
||||
bool _isFinite = true;
|
||||
int _firstDirection = SPathDirection.kUnknown;
|
||||
int _reversals = 0;
|
||||
|
||||
/// SPathDirection of contour.
|
||||
int get firstDirection => _firstDirection;
|
||||
|
||||
DirChange _expectedDirection = DirChange.kInvalid;
|
||||
|
||||
void setMovePt(double x, double y) {
|
||||
currX = priorX = lastX = x;
|
||||
currY = priorY = lastY = y;
|
||||
}
|
||||
|
||||
bool addPoint(double x, double y) {
|
||||
if (x == currX && y == currY) {
|
||||
// Skip zero length vector.
|
||||
return true;
|
||||
}
|
||||
currX = x;
|
||||
currY = y;
|
||||
final double vecX = currX! - lastX!;
|
||||
final double vecY = currY! - lastY!;
|
||||
if (priorX == lastX && priorY == lastY) {
|
||||
// First non-zero vector.
|
||||
lastVecX = vecX;
|
||||
lastVecY = vecY;
|
||||
firstVectorEndPointX = x;
|
||||
firstVectorEndPointY = y;
|
||||
} else if (!_addVector(vecX, vecY)) {
|
||||
return false;
|
||||
}
|
||||
priorX = lastX;
|
||||
priorY = lastY;
|
||||
lastX = x;
|
||||
lastY = y;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool close() {
|
||||
// Add another point from path closing point to end of first vector.
|
||||
return addPoint(firstVectorEndPointX!, firstVectorEndPointY!);
|
||||
}
|
||||
|
||||
bool get isFinite => _isFinite;
|
||||
|
||||
int get reversals => _reversals;
|
||||
|
||||
DirChange _directionChange(double curVecX, double curVecY) {
|
||||
// Cross product = ||lastVec|| * ||curVec|| * sin(theta) * N
|
||||
// sin(theta) angle between two vectors is positive for angles 0..180 and
|
||||
// negative for greater, providing left or right direction.
|
||||
double lastX = lastVecX!;
|
||||
double lastY = lastVecY!;
|
||||
double cross = lastX * curVecY - lastY * curVecX;
|
||||
if (!cross.isFinite) {
|
||||
return DirChange.kUnknown;
|
||||
}
|
||||
// Detect straight and backwards direction change.
|
||||
// Instead of comparing absolute crossproduct size, compare
|
||||
// largest component double+crossproduct.
|
||||
final double smallest =
|
||||
math.min(curVecX, math.min(curVecY, math.min(lastX, lastY)));
|
||||
final double largest = math.max(
|
||||
math.max(curVecX, math.max(curVecY, math.max(lastX, lastY))),
|
||||
-smallest);
|
||||
if (_nearlyEqual(largest, largest + cross)) {
|
||||
final double nearlyZeroSquared =
|
||||
SPath.scalarNearlyZero * SPath.scalarNearlyZero;
|
||||
if (_nearlyEqual(_lengthSquared(lastX, lastY), nearlyZeroSquared) ||
|
||||
_nearlyEqual(_lengthSquared(curVecX, curVecY), nearlyZeroSquared)) {
|
||||
// Length of either vector is smaller than tolerance to be able
|
||||
// to compute direction.
|
||||
return DirChange.kUnknown;
|
||||
}
|
||||
// The vectors are parallel, sign of dot product gives us direction.
|
||||
// cosine is positive for straight -90 < Theta < 90
|
||||
return _dotProduct(lastX, lastY, curVecX, curVecY) < 0
|
||||
? DirChange.kBackwards
|
||||
: DirChange.kStraight;
|
||||
}
|
||||
return cross > 0 ? DirChange.kRight : DirChange.kLeft;
|
||||
}
|
||||
|
||||
bool _addVector(double curVecX, double curVecY) {
|
||||
DirChange dir = _directionChange(curVecX, curVecY);
|
||||
final bool isDirectionRight = dir == DirChange.kRight;
|
||||
if (dir == DirChange.kLeft || isDirectionRight) {
|
||||
if (_expectedDirection == DirChange.kInvalid) {
|
||||
// First valid direction. From this point on expect always left.
|
||||
_expectedDirection = dir;
|
||||
_firstDirection =
|
||||
isDirectionRight ? SPathDirection.kCW : SPathDirection.kCCW;
|
||||
} else if (dir != _expectedDirection) {
|
||||
_firstDirection = SPathDirection.kUnknown;
|
||||
return false;
|
||||
}
|
||||
lastVecX = curVecX;
|
||||
lastVecY = curVecY;
|
||||
} else {
|
||||
switch (dir) {
|
||||
case DirChange.kBackwards:
|
||||
// Allow path to reverse direction twice.
|
||||
// Given path.moveTo(0,0) lineTo(1,1)
|
||||
// - First reversal: direction change formed by line (0,0 1,1),
|
||||
// line (1,1 0,0)
|
||||
// - Second reversal: direction change formed by line (1,1 0,0),
|
||||
// line (0,0 1,1)
|
||||
lastVecX = curVecX;
|
||||
lastVecY = curVecY;
|
||||
return ++_reversals < 3;
|
||||
case DirChange.kUnknown:
|
||||
return _isFinite = false;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Quick test to detect concave by looking at number of changes in direction
|
||||
// of vectors formed by path points (excluding control points).
|
||||
static int bySign(PathRef pathRef, int pointIndex, int numPoints) {
|
||||
int lastPointIndex = pointIndex + numPoints;
|
||||
int currentPoint = pointIndex++;
|
||||
int firstPointIndex = currentPoint;
|
||||
int signChangeCountX = 0;
|
||||
int signChangeCountY = 0;
|
||||
int lastSx = kValueNeverReturnedBySign;
|
||||
int lastSy = kValueNeverReturnedBySign;
|
||||
for (int outerLoop = 0; outerLoop < 2; ++outerLoop) {
|
||||
while (pointIndex != lastPointIndex) {
|
||||
double vecX = pathRef._fPoints[pointIndex * 2] -
|
||||
pathRef._fPoints[currentPoint * 2];
|
||||
double vecY = pathRef._fPoints[pointIndex * 2 + 1] -
|
||||
pathRef._fPoints[currentPoint * 2 + 1];
|
||||
if (!(vecX == 0 && vecY == 0)) {
|
||||
// Give up if vector construction failed.
|
||||
// give up if vector construction failed
|
||||
if (!(vecX.isFinite && vecY.isFinite)) {
|
||||
return SPathConvexityType.kUnknown;
|
||||
}
|
||||
int sx = vecX < 0 ? 1 : 0;
|
||||
int sy = vecY < 0 ? 1 : 0;
|
||||
signChangeCountX += (sx != lastSx) ? 1 : 0;
|
||||
signChangeCountY += (sy != lastSy) ? 1 : 0;
|
||||
if (signChangeCountX > 3 || signChangeCountY > 3) {
|
||||
return SPathConvexityType.kConcave;
|
||||
}
|
||||
lastSx = sx;
|
||||
lastSy = sy;
|
||||
}
|
||||
currentPoint = pointIndex++;
|
||||
if (outerLoop != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
pointIndex = firstPointIndex;
|
||||
}
|
||||
return SPathConvexityType.kConvex;
|
||||
}
|
||||
}
|
||||
|
||||
enum DirChange {
|
||||
kUnknown,
|
||||
kLeft,
|
||||
kRight,
|
||||
kStraight,
|
||||
kBackwards, // if double back, allow simple lines to be convex
|
||||
kInvalid
|
||||
}
|
||||
@@ -0,0 +1,544 @@
|
||||
// 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.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// Computes winding number and onCurveCount for a path and point.
|
||||
class PathWinding {
|
||||
PathWinding(this.pathRef, this.x, this.y) {
|
||||
_walkPath();
|
||||
}
|
||||
|
||||
final PathRef pathRef;
|
||||
final double x;
|
||||
final double y;
|
||||
int _w = 0;
|
||||
int _onCurveCount = 0;
|
||||
|
||||
int get w => _w;
|
||||
|
||||
int get onCurveCount => _onCurveCount;
|
||||
|
||||
/// Buffer used for max(iterator result, chopped 3 cubics).
|
||||
final Float32List _buffer = Float32List(8 + 10);
|
||||
|
||||
/// Iterates through path and computes winding.
|
||||
void _walkPath() {
|
||||
final PathIterator iter = PathIterator(pathRef, true);
|
||||
int verb;
|
||||
while ((verb = iter.next(_buffer)) != SPath.kDoneVerb) {
|
||||
switch (verb) {
|
||||
case SPath.kMoveVerb:
|
||||
case SPath.kCloseVerb:
|
||||
break;
|
||||
case SPath.kLineVerb:
|
||||
_computeLineWinding();
|
||||
break;
|
||||
case SPath.kQuadVerb:
|
||||
_computeQuadWinding();
|
||||
break;
|
||||
case SPath.kConicVerb:
|
||||
_computeConicWinding(pathRef._conicWeights![iter._conicWeightIndex]);
|
||||
break;
|
||||
case SPath.kCubicVerb:
|
||||
_computeCubicWinding();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _computeLineWinding() {
|
||||
final double x0 = _buffer[0];
|
||||
final double startY = _buffer[1];
|
||||
double y0 = startY;
|
||||
final double x1 = _buffer[2];
|
||||
final double endY = _buffer[3];
|
||||
double y1 = endY;
|
||||
final double dy = y1 - y0;
|
||||
int dir = 1;
|
||||
// Swap so that y0 <= y1 holds.
|
||||
if (y0 > y1) {
|
||||
double temp = y0;
|
||||
y0 = y1;
|
||||
y1 = temp;
|
||||
dir = -1;
|
||||
}
|
||||
// If point is outside top/bottom bounds, winding is 0.
|
||||
if (y < y0 || y > y1) {
|
||||
return;
|
||||
}
|
||||
if (_checkOnCurve(x, y, x0, startY, x1, endY)) {
|
||||
_onCurveCount++;
|
||||
return;
|
||||
}
|
||||
if (y == y1) {
|
||||
return;
|
||||
}
|
||||
// c = ax*by − ay*bx where a is the line and b is line formed from start
|
||||
// to the given point(x,y).
|
||||
final double crossProduct = (x1 - x0) * (y - startY) - dy * (x - x0);
|
||||
if (crossProduct == 0) {
|
||||
// zero cross means the point is on the line, and since the case where
|
||||
// y of the query point is at the end point is handled above, we can be
|
||||
// sure that we're on the line (excluding the end point) here.
|
||||
if (x != x1 || y != endY) {
|
||||
_onCurveCount++;
|
||||
}
|
||||
dir = 0;
|
||||
} else if (SPath.scalarSignedAsInt(crossProduct) == dir) {
|
||||
// Direction of cross product and line the same.
|
||||
dir = 0;
|
||||
}
|
||||
_w += dir;
|
||||
}
|
||||
|
||||
// Check if point starts the line, handle special case for horizontal lines
|
||||
// where and point except the end point is considered on curve.
|
||||
static bool _checkOnCurve(double x, double y, double startX, double startY,
|
||||
double endX, double endY) {
|
||||
if (startY == endY) {
|
||||
// Horizontal line.
|
||||
return SPath.between(startX, x, endX) && x != endX;
|
||||
} else {
|
||||
return x == startX && y == startY;
|
||||
}
|
||||
}
|
||||
|
||||
void _computeQuadWinding() {
|
||||
// Check if we need to chop quadratic at extrema to compute 2 separate
|
||||
// windings.
|
||||
int n = 0;
|
||||
if (!_isQuadMonotonic(_buffer)) {
|
||||
n = _chopQuadAtExtrema(_buffer);
|
||||
}
|
||||
int winding = _computeMonoQuadWinding(
|
||||
_buffer[0], _buffer[1], _buffer[2], _buffer[3], _buffer[4], _buffer[5]);
|
||||
if (n > 0) {
|
||||
winding += _computeMonoQuadWinding(_buffer[4], _buffer[5], _buffer[6],
|
||||
_buffer[7], _buffer[8], _buffer[9]);
|
||||
}
|
||||
_w += winding;
|
||||
}
|
||||
|
||||
int _computeMonoQuadWinding(
|
||||
double x0, double y0, double x1, double y1, double x2, double y2) {
|
||||
int dir = 1;
|
||||
final double startY = y0;
|
||||
final double endY = y2;
|
||||
if (y0 > y2) {
|
||||
final double temp = y0;
|
||||
y0 = y2;
|
||||
y2 = temp;
|
||||
dir = -1;
|
||||
}
|
||||
if (y < y0 || y > y2) {
|
||||
return 0;
|
||||
}
|
||||
if (_checkOnCurve(x, y, x0, startY, x2, endY)) {
|
||||
_onCurveCount++;
|
||||
return 0;
|
||||
}
|
||||
if (y == y2) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
_QuadRoots quadRoots = _QuadRoots();
|
||||
final int n = quadRoots.findRoots(startY - 2 * y1 + endY, 2 * (y1 - startY), endY - y);
|
||||
assert(n <= 1);
|
||||
double xt;
|
||||
if (0 == n) {
|
||||
// zero roots are returned only when y0 == y
|
||||
xt = dir == 1 ? x0 : x2;
|
||||
} else {
|
||||
final double t = quadRoots.root0!;
|
||||
final double C = x0;
|
||||
final double A = x2 - 2 * x1 + C;
|
||||
final double B = 2 * (x1 - C);
|
||||
xt = polyEval(A, B, C, t);
|
||||
}
|
||||
if (_nearlyEqual(xt, x)) {
|
||||
if (x != x2 || y != endY) {
|
||||
// don't test end points; they're start points
|
||||
_onCurveCount += 1;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return xt < x ? dir : 0;
|
||||
}
|
||||
|
||||
/// Chops a non-monotonic quadratic curve, returns subdivisions and writes
|
||||
/// result into [buffer].
|
||||
static int _chopQuadAtExtrema(Float32List buffer) {
|
||||
final double x0 = buffer[0];
|
||||
final double y0 = buffer[1];
|
||||
final double x1 = buffer[2];
|
||||
final double y1 = buffer[3];
|
||||
final double x2 = buffer[4];
|
||||
final double y2 = buffer[5];
|
||||
double? tValueAtExtrema = _validUnitDivide(y0 - y1, y0 - y1 - y1 + y2);
|
||||
if (tValueAtExtrema != null) {
|
||||
// Chop quad at t value by interpolating along p0-p1 and p1-p2.
|
||||
double p01x = x0 + (tValueAtExtrema * (x1 - x0));
|
||||
double p01y = y0 + (tValueAtExtrema * (y1 - y0));
|
||||
double p12x = x1 + (tValueAtExtrema * (x2 - x1));
|
||||
double p12y = y1 + (tValueAtExtrema * (y2 - y1));
|
||||
double cx = p01x + (tValueAtExtrema * (p12x - p01x));
|
||||
double cy = p01y + (tValueAtExtrema * (p12y - p01y));
|
||||
buffer[2] = p01x;
|
||||
buffer[3] = p01y;
|
||||
buffer[4] = cx;
|
||||
buffer[5] = cy;
|
||||
buffer[6] = p12x;
|
||||
buffer[7] = p12y;
|
||||
buffer[8] = x2;
|
||||
buffer[9] = y2;
|
||||
return 1;
|
||||
}
|
||||
// if we get here, we need to force output to be monotonic, even though
|
||||
// we couldn't compute a unit divide value (probably underflow).
|
||||
buffer[3] = (y0 - y1).abs() < (y1 - y2).abs() ? y0 : y2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool _isQuadMonotonic(Float32List quad) {
|
||||
final double y0 = quad[1];
|
||||
final double y1 = quad[3];
|
||||
final double y2 = quad[5];
|
||||
if (y0 == y1) {
|
||||
return true;
|
||||
}
|
||||
if (y0 < y1) {
|
||||
return y1 <= y2;
|
||||
} else {
|
||||
return y1 >= y2;
|
||||
}
|
||||
}
|
||||
|
||||
void _computeConicWinding(double weight) {
|
||||
Conic conic = Conic(_buffer[0], _buffer[1], _buffer[2], _buffer[3],
|
||||
_buffer[4], _buffer[5], weight);
|
||||
// If the data points are very large, the conic may not be monotonic but may also
|
||||
// fail to chop. Then, the chopper does not split the original conic in two.
|
||||
bool isMono = _isQuadMonotonic(_buffer);
|
||||
List<Conic> conics = [];
|
||||
conic.chopAtYExtrema(conics);
|
||||
_computeMonoConicWinding(conics[0]);
|
||||
if (!isMono && conics.length == 2) {
|
||||
_computeMonoConicWinding(conics[1]);
|
||||
}
|
||||
}
|
||||
|
||||
void _computeMonoConicWinding(Conic conic) {
|
||||
double y0 = conic.p0y;
|
||||
double y2 = conic.p2y;
|
||||
int dir = 1;
|
||||
if (y0 > y2) {
|
||||
final double swap = y0;
|
||||
y0 = y2;
|
||||
y2 = swap;
|
||||
dir = -1;
|
||||
}
|
||||
if (y < y0 || y > y2) {
|
||||
return;
|
||||
}
|
||||
if (_checkOnCurve(x, y, conic.p0x, conic.p0y, conic.p2x, conic.p2y)) {
|
||||
_onCurveCount += 1;
|
||||
return;
|
||||
}
|
||||
if (y == y2) {
|
||||
return;
|
||||
}
|
||||
|
||||
double A = conic.p2y;
|
||||
double B = conic.p1y * conic.fW - y * conic.fW + y;
|
||||
double C = conic.p0y;
|
||||
// A = a + c - 2*(b*w - yCept*w + yCept)
|
||||
A += C - 2 * B;
|
||||
// B = b*w - w * yCept + yCept - a
|
||||
B -= C;
|
||||
C -= y;
|
||||
final _QuadRoots quadRoots = _QuadRoots();
|
||||
int n = quadRoots.findRoots(A, 2 * B, C);
|
||||
assert(n <= 1);
|
||||
double xt;
|
||||
if (0 == n) {
|
||||
// zero roots are returned only when y0 == y
|
||||
// Need [0] if dir == 1
|
||||
// and [2] if dir == -1
|
||||
xt = dir == 1 ? conic.p0x : conic.p2x;
|
||||
} else {
|
||||
final double root = quadRoots.root0!;
|
||||
xt =
|
||||
_conicEvalNumerator(conic.p0x, conic.p1x, conic.p2x, conic.fW, root) /
|
||||
_conicEvalDenominator(conic.fW, root);
|
||||
}
|
||||
if (_nearlyEqual(xt, x)) {
|
||||
if (x != conic.p2x || y != conic.p2y) {
|
||||
// don't test end points; they're start points
|
||||
_onCurveCount += 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_w += xt < x ? dir : 0;
|
||||
}
|
||||
|
||||
void _computeCubicWinding() {
|
||||
int n = _chopCubicAtYExtrema(_buffer, _buffer);
|
||||
for (int i = 0; i <= n; ++i) {
|
||||
_windingMonoCubic(i * 3 * 2);
|
||||
}
|
||||
}
|
||||
|
||||
void _windingMonoCubic(int bufferIndex) {
|
||||
final int bufferStartPos = bufferIndex;
|
||||
final double px0 = _buffer[bufferIndex++];
|
||||
final double py0 = _buffer[bufferIndex++];
|
||||
final double px1 = _buffer[bufferIndex++];
|
||||
bufferIndex++;
|
||||
final double px2 = _buffer[bufferIndex++];
|
||||
bufferIndex++;
|
||||
final double px3 = _buffer[bufferIndex++];
|
||||
final double py3 = _buffer[bufferIndex++];
|
||||
|
||||
double y0 = py0;
|
||||
double y3 = py3;
|
||||
|
||||
int dir = 1;
|
||||
if (y0 > y3) {
|
||||
final double swap = y0;
|
||||
y0 = y3;
|
||||
y3 = swap;
|
||||
dir = -1;
|
||||
}
|
||||
if (y < y0 || y > y3) {
|
||||
return;
|
||||
}
|
||||
if (_checkOnCurve(x, y, px0, py0, px3, py3)) {
|
||||
_onCurveCount += 1;
|
||||
return;
|
||||
}
|
||||
if (y == y3) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Quickly reject or accept
|
||||
final double min = math.min(px0, math.min(px1, math.min(px2, px3)));
|
||||
final double max = math.max(px0, math.max(px1, math.max(px2, px3)));
|
||||
if (x < min) {
|
||||
return;
|
||||
}
|
||||
if (x > max) {
|
||||
_w += dir;
|
||||
return;
|
||||
}
|
||||
// Compute the actual x(t) value.
|
||||
double? t = _chopMonoAtY(_buffer, bufferStartPos, y);
|
||||
if (t == null) {
|
||||
return;
|
||||
}
|
||||
double xt = _evalCubicPts(px0, px1, px2, px3, t);
|
||||
if (_nearlyEqual(xt, x)) {
|
||||
if (x != px3 || y != py3) {
|
||||
// don't test end points; they're start points
|
||||
_onCurveCount += 1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_w += xt < x ? dir : 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Iterates through path including generating closing segments.
|
||||
class PathIterator {
|
||||
PathIterator(this.pathRef, bool forceClose)
|
||||
: _forceClose = forceClose,
|
||||
_verbCount = pathRef.countVerbs() {
|
||||
_pointIndex = 0;
|
||||
if (!pathRef.isFinite) {
|
||||
// Don't allow iteration through non-finite points, prepare to return
|
||||
// done verb.
|
||||
_verbIndex = pathRef.countVerbs();
|
||||
}
|
||||
}
|
||||
|
||||
final PathRef pathRef;
|
||||
final bool _forceClose;
|
||||
final int _verbCount;
|
||||
|
||||
bool _needClose = false;
|
||||
int _segmentState = SPathSegmentState.kEmptyContour;
|
||||
int _conicWeightIndex = -1;
|
||||
double _lastPointX = 0;
|
||||
double _lastPointY = 0;
|
||||
double _moveToX = 0;
|
||||
double _moveToY = 0;
|
||||
int _verbIndex = 0;
|
||||
int _pointIndex = 0;
|
||||
|
||||
/// Returns true if first contour on path is closed.
|
||||
bool isClosedContour() {
|
||||
if (_verbCount == 0 || _verbIndex == _verbCount) {
|
||||
return false;
|
||||
}
|
||||
if (_forceClose) {
|
||||
return true;
|
||||
}
|
||||
int verbIndex = 0;
|
||||
// Skip starting moveTo.
|
||||
if (pathRef.atVerb(verbIndex) == SPath.kMoveVerb) {
|
||||
++verbIndex;
|
||||
}
|
||||
while (verbIndex < _verbCount) {
|
||||
int verb = pathRef.atVerb(verbIndex++);
|
||||
if (SPath.kMoveVerb == verb) {
|
||||
break;
|
||||
}
|
||||
if (SPath.kCloseVerb == verb) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int _autoClose(Float32List outPts) {
|
||||
if (_lastPointX != _moveToX || _lastPointY != _moveToY) {
|
||||
// Handle special case where comparison above will return true for
|
||||
// NaN != NaN although it should be false.
|
||||
if (_lastPointX.isNaN ||
|
||||
_lastPointY.isNaN ||
|
||||
_moveToX.isNaN ||
|
||||
_moveToY.isNaN) {
|
||||
return SPath.kCloseVerb;
|
||||
}
|
||||
outPts[0] = _lastPointX;
|
||||
outPts[1] = _lastPointY;
|
||||
outPts[2] = _moveToX;
|
||||
outPts[3] = _moveToY;
|
||||
_lastPointX = _moveToX;
|
||||
_lastPointY = _moveToY;
|
||||
return SPath.kLineVerb;
|
||||
} else {
|
||||
outPts[0] = _moveToX;
|
||||
outPts[1] = _moveToY;
|
||||
return SPath.kCloseVerb;
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if caller should use moveTo, false if last point of
|
||||
// previous primitive.
|
||||
ui.Offset _constructMoveTo() {
|
||||
if (_segmentState == SPathSegmentState.kAfterMove) {
|
||||
// Set the first return point to move point.
|
||||
_segmentState = SPathSegmentState.kAfterPrimitive;
|
||||
return ui.Offset(_moveToX, _moveToY);
|
||||
}
|
||||
return ui.Offset(
|
||||
pathRef.points[_pointIndex - 2], pathRef.points[_pointIndex - 1]);
|
||||
}
|
||||
|
||||
int peek() => pathRef._fVerbs[_verbIndex];
|
||||
|
||||
// Returns next verb and reads associated points into [outPts].
|
||||
int next(Float32List outPts) {
|
||||
if (_verbIndex == pathRef.countVerbs()) {
|
||||
// Close the curve if requested and if there is some curve to close
|
||||
if (_needClose && _segmentState == SPathSegmentState.kAfterPrimitive) {
|
||||
if (SPath.kLineVerb == _autoClose(outPts)) {
|
||||
return SPath.kLineVerb;
|
||||
}
|
||||
_needClose = false;
|
||||
return SPath.kCloseVerb;
|
||||
}
|
||||
return SPath.kDoneVerb;
|
||||
}
|
||||
int verb = pathRef._fVerbs[_verbIndex++];
|
||||
switch (verb) {
|
||||
case SPath.kMoveVerb:
|
||||
if (_needClose) {
|
||||
// Move back one verb.
|
||||
_verbIndex--;
|
||||
final int autoVerb = _autoClose(outPts);
|
||||
if (autoVerb == SPath.kCloseVerb) {
|
||||
_needClose = false;
|
||||
}
|
||||
return autoVerb;
|
||||
}
|
||||
if (_verbIndex == _verbCount) {
|
||||
return SPath.kDoneVerb;
|
||||
}
|
||||
double offsetX = pathRef.points[_pointIndex++];
|
||||
double offsetY = pathRef.points[_pointIndex++];
|
||||
_moveToX = offsetX;
|
||||
_moveToY = offsetY;
|
||||
outPts[0] = offsetX;
|
||||
outPts[1] = offsetY;
|
||||
_segmentState = SPathSegmentState.kAfterMove;
|
||||
_lastPointX = _moveToX;
|
||||
_lastPointY = _moveToY;
|
||||
_needClose = _forceClose;
|
||||
break;
|
||||
case SPath.kLineVerb:
|
||||
final ui.Offset start = _constructMoveTo();
|
||||
double offsetX = pathRef.points[_pointIndex++];
|
||||
double offsetY = pathRef.points[_pointIndex++];
|
||||
outPts[0] = start.dx;
|
||||
outPts[1] = start.dy;
|
||||
outPts[2] = offsetX;
|
||||
outPts[3] = offsetY;
|
||||
_lastPointX = offsetX;
|
||||
_lastPointY = offsetY;
|
||||
break;
|
||||
case SPath.kConicVerb:
|
||||
_conicWeightIndex++;
|
||||
final ui.Offset start = _constructMoveTo();
|
||||
outPts[0] = start.dx;
|
||||
outPts[1] = start.dy;
|
||||
outPts[2] = pathRef.points[_pointIndex++];
|
||||
outPts[3] = pathRef.points[_pointIndex++];
|
||||
_lastPointX = outPts[4] = pathRef.points[_pointIndex++];
|
||||
_lastPointY = outPts[5] = pathRef.points[_pointIndex++];
|
||||
break;
|
||||
case SPath.kQuadVerb:
|
||||
final ui.Offset start = _constructMoveTo();
|
||||
outPts[0] = start.dx;
|
||||
outPts[1] = start.dy;
|
||||
outPts[2] = pathRef.points[_pointIndex++];
|
||||
outPts[3] = pathRef.points[_pointIndex++];
|
||||
_lastPointX = outPts[4] = pathRef.points[_pointIndex++];
|
||||
_lastPointY = outPts[5] = pathRef.points[_pointIndex++];
|
||||
break;
|
||||
case SPath.kCubicVerb:
|
||||
final ui.Offset start = _constructMoveTo();
|
||||
outPts[0] = start.dx;
|
||||
outPts[1] = start.dy;
|
||||
outPts[2] = pathRef.points[_pointIndex++];
|
||||
outPts[3] = pathRef.points[_pointIndex++];
|
||||
outPts[4] = pathRef.points[_pointIndex++];
|
||||
outPts[5] = pathRef.points[_pointIndex++];
|
||||
_lastPointX = outPts[6] = pathRef.points[_pointIndex++];
|
||||
_lastPointY = outPts[7] = pathRef.points[_pointIndex++];
|
||||
break;
|
||||
case SPath.kCloseVerb:
|
||||
verb = _autoClose(outPts);
|
||||
if (verb == SPath.kLineVerb) {
|
||||
// Move back one verb since we constructed line for this close verb.
|
||||
_verbIndex--;
|
||||
} else {
|
||||
_needClose = false;
|
||||
_segmentState = SPathSegmentState.kEmptyContour;
|
||||
}
|
||||
_lastPointX = _moveToX;
|
||||
_lastPointY = _moveToY;
|
||||
break;
|
||||
case SPath.kDoneVerb:
|
||||
assert(_verbIndex == pathRef.countVerbs());
|
||||
break;
|
||||
default:
|
||||
throw FormatException('Unsupport Path verb $verb');
|
||||
}
|
||||
return verb;
|
||||
}
|
||||
|
||||
double get conicWeight => pathRef.atWeight(_conicWeightIndex);
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
// 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.
|
||||
|
||||
part of engine;
|
||||
|
||||
/// Computes tangent at point x,y on a line.
|
||||
void tangentLine(
|
||||
Float32List pts, double x, double y, List<ui.Offset> tangents) {
|
||||
final double y0 = pts[1];
|
||||
final double y1 = pts[3];
|
||||
if (!SPath.between(y0, y, y1)) {
|
||||
return;
|
||||
}
|
||||
final double x0 = pts[0];
|
||||
final double x1 = pts[2];
|
||||
if (!SPath.between(x0, x, x1)) {
|
||||
return;
|
||||
}
|
||||
final double dx = x1 - x0;
|
||||
final double dy = y1 - y0;
|
||||
if (!_nearlyEqual((x - x0) * dy, dx * (y - y0))) {
|
||||
return;
|
||||
}
|
||||
tangents.add(ui.Offset(dx, dy));
|
||||
}
|
||||
|
||||
/// Computes tangent at point x,y on a quadratic curve.
|
||||
void tangentQuad(
|
||||
Float32List pts, double x, double y, List<ui.Offset> tangents) {
|
||||
final double y0 = pts[1];
|
||||
final double y1 = pts[3];
|
||||
final double y2 = pts[5];
|
||||
if (!SPath.between(y0, y, y1) && !SPath.between(y1, y, y2)) {
|
||||
return;
|
||||
}
|
||||
final double x0 = pts[0];
|
||||
final double x1 = pts[2];
|
||||
final double x2 = pts[4];
|
||||
if (!SPath.between(x0, x, x1) && !SPath.between(x1, x, x2)) {
|
||||
return;
|
||||
}
|
||||
final _QuadRoots roots = _QuadRoots();
|
||||
int n = roots.findRoots(y0 - 2 * y1 + y2, 2 * (y1 - y0), y0 - y);
|
||||
for (int index = 0; index < n; ++index) {
|
||||
double t = index == 0 ? roots.root0! : roots.root1!;
|
||||
double C = x0;
|
||||
double A = x2 - 2 * x1 + C;
|
||||
double B = 2 * (x1 - C);
|
||||
double xt = polyEval(A, B, C, t);
|
||||
if (!_nearlyEqual(x, xt)) {
|
||||
continue;
|
||||
}
|
||||
tangents.add(_evalQuadTangentAt(x0, y0, x1, y1, x2, y2, t));
|
||||
}
|
||||
}
|
||||
|
||||
ui.Offset _evalQuadTangentAt(double x0, double y0, double x1, double y1,
|
||||
double x2, double y2, double t) {
|
||||
// The derivative of a quad equation is 2(b - a +(a - 2b +c)t).
|
||||
// This returns a zero tangent vector when t is 0 or 1, and the control
|
||||
// point is equal to the end point. In this case, use the quad end points to
|
||||
// compute the tangent.
|
||||
|
||||
if ((t == 0 && x0 == x1 && y0 == y1) || (t == 1 && x1 == x2 && y1 == y2)) {
|
||||
return ui.Offset(x2 - x0, y2 - y0);
|
||||
}
|
||||
assert(t >= 0 && t <= 1.0);
|
||||
|
||||
double bx = x1 - x0;
|
||||
double by = y1 - y0;
|
||||
double ax = x2 - x1 - bx;
|
||||
double ay = y2 - y1 - by;
|
||||
double tx = ax * t + bx;
|
||||
double ty = ay * t + by;
|
||||
return ui.Offset(tx * 2, ty * 2);
|
||||
}
|
||||
|
||||
/// Computes tangent at point x,y on a conic curve.
|
||||
void tangentConic(Float32List pts, double x, double y, double weight,
|
||||
List<ui.Offset> tangents) {
|
||||
final double y0 = pts[1];
|
||||
final double y1 = pts[3];
|
||||
final double y2 = pts[5];
|
||||
if (!SPath.between(y0, y, y1) && !SPath.between(y1, y, y2)) {
|
||||
return;
|
||||
}
|
||||
final double x0 = pts[0];
|
||||
final double x1 = pts[2];
|
||||
final double x2 = pts[4];
|
||||
if (!SPath.between(x0, x, x1) && !SPath.between(x1, x, x2)) {
|
||||
return;
|
||||
}
|
||||
// Check extrema.
|
||||
double A = y2;
|
||||
double B = y1 * weight - y * weight + y;
|
||||
double C = y0;
|
||||
// A = a + c - 2*(b*w - yCept*w + yCept)
|
||||
A += C - 2 * B;
|
||||
// B = b*w - w * yCept + yCept - a
|
||||
B -= C;
|
||||
C -= y;
|
||||
final _QuadRoots quadRoots = _QuadRoots();
|
||||
int n = quadRoots.findRoots(A, 2 * B, C);
|
||||
for (int index = 0; index < n; ++index) {
|
||||
double t = index == 0 ? quadRoots.root0! : quadRoots.root1!;
|
||||
double xt = _conicEvalNumerator(x0, x1, x2, weight, t) /
|
||||
_conicEvalDenominator(weight, t);
|
||||
if (!_nearlyEqual(x, xt)) {
|
||||
continue;
|
||||
}
|
||||
Conic conic = Conic(x0, y0, x1, y1, x2, y2, weight);
|
||||
tangents.add(conic.evalTangentAt(t));
|
||||
}
|
||||
}
|
||||
|
||||
/// Computes tangent at point x,y on a cubic curve.
|
||||
void tangentCubic(
|
||||
Float32List pts, double x, double y, List<ui.Offset> tangents) {
|
||||
final double y3 = pts[7];
|
||||
final double y0 = pts[1];
|
||||
final double y1 = pts[3];
|
||||
final double y2 = pts[5];
|
||||
if (!SPath.between(y0, y, y1) &&
|
||||
!SPath.between(y1, y, y2) &&
|
||||
!SPath.between(y2, y, y3)) {
|
||||
return;
|
||||
}
|
||||
final double x0 = pts[0];
|
||||
final double x1 = pts[2];
|
||||
final double x2 = pts[4];
|
||||
final double x3 = pts[6];
|
||||
if (!SPath.between(x0, x, x1) &&
|
||||
!SPath.between(x1, x, x2) &&
|
||||
!SPath.between(x2, x, x3)) {
|
||||
return;
|
||||
}
|
||||
final Float32List dst = Float32List(20);
|
||||
int n = _chopCubicAtYExtrema(pts, dst);
|
||||
for (int i = 0; i <= n; ++i) {
|
||||
int bufferPos = i * 6;
|
||||
double? t = _chopMonoAtY(dst, i * 6, y);
|
||||
if (t == null) {
|
||||
continue;
|
||||
}
|
||||
double xt = _evalCubicPts(dst[bufferPos], dst[bufferPos + 2],
|
||||
dst[bufferPos + 4], dst[bufferPos + 6], t);
|
||||
if (!_nearlyEqual(x, xt)) {
|
||||
continue;
|
||||
}
|
||||
tangents.add(_evalCubicTangentAt(dst, bufferPos, t));
|
||||
}
|
||||
}
|
||||
|
||||
ui.Offset _evalCubicTangentAt(Float32List points, int bufferPos, double t) {
|
||||
assert(t >= 0 && t <= 1.0);
|
||||
final double y3 = points[7 + bufferPos];
|
||||
final double y0 = points[1 + bufferPos];
|
||||
final double y1 = points[3 + bufferPos];
|
||||
final double y2 = points[5 + bufferPos];
|
||||
final double x0 = points[0 + bufferPos];
|
||||
final double x1 = points[2 + bufferPos];
|
||||
final double x2 = points[4 + bufferPos];
|
||||
final double x3 = points[6 + bufferPos];
|
||||
// The derivative equation returns a zero tangent vector when t is 0 or 1,
|
||||
// and the adjacent control point is equal to the end point. In this case,
|
||||
// use the next control point or the end points to compute the tangent.
|
||||
if ((t == 0 && x0 == x1 && y0 == y1) || (t == 1 && x2 == x3 && y2 == y3)) {
|
||||
double dx, dy;
|
||||
if (t == 0) {
|
||||
dx = x2 - x0;
|
||||
dy = y2 - y0;
|
||||
} else {
|
||||
dx = x3 - x1;
|
||||
dy = y3 - y1;
|
||||
}
|
||||
if (dx == 0 && dy == 0) {
|
||||
dx = x3 - x0;
|
||||
dy = y3 - y0;
|
||||
}
|
||||
return ui.Offset(dx, dy);
|
||||
} else {
|
||||
return _evalCubicDerivative(x0, y0, x1, y1, x2, y2, x3, y3, t);
|
||||
}
|
||||
}
|
||||
|
||||
ui.Offset _evalCubicDerivative(double x0, double y0, double x1, double y1,
|
||||
double x2, double y2, double x3, double y3, double t) {
|
||||
final _SkQuadCoefficients coeff = _SkQuadCoefficients(
|
||||
x3 + 3 * (x1 - x2) - x0,
|
||||
y3 + 3 * (y1 - y2) - y0,
|
||||
2 * (x2 - (2 * x1) + x0),
|
||||
2 * (y2 - (2 * y1) + y0),
|
||||
x1 - x0,
|
||||
y1 - y0,
|
||||
);
|
||||
return ui.Offset(coeff.evalX(t), coeff.evalY(t));
|
||||
}
|
||||
@@ -466,7 +466,7 @@ class RecordingCanvas {
|
||||
}
|
||||
}
|
||||
SurfacePath sPath = path as SurfacePath;
|
||||
if (sPath.subpaths.isNotEmpty) {
|
||||
if (!sPath.pathRef.isEmpty) {
|
||||
_hasArbitraryPaint = true;
|
||||
_didDraw = true;
|
||||
ui.Rect pathBounds = sPath.getBounds();
|
||||
@@ -1099,12 +1099,18 @@ class PaintDrawDRRect extends DrawCommand {
|
||||
final ui.RRect outer;
|
||||
final ui.RRect inner;
|
||||
final SurfacePaintData paint;
|
||||
|
||||
PaintDrawDRRect(this.outer, this.inner, this.paint);
|
||||
ui.Path? path;
|
||||
PaintDrawDRRect(this.outer, this.inner, this.paint) {
|
||||
path = ui.Path()
|
||||
..fillType = ui.PathFillType.evenOdd
|
||||
..addRRect(outer)
|
||||
..addRRect(inner)
|
||||
..close();
|
||||
}
|
||||
|
||||
@override
|
||||
void apply(EngineCanvas? canvas) {
|
||||
canvas!.drawDRRect(outer, inner, paint);
|
||||
canvas!.drawPath(path!, paint);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -1570,7 +1576,7 @@ class Ellipse extends PathCommand {
|
||||
anticlockwise ? startAngle - endAngle : endAngle - startAngle,
|
||||
matrix4,
|
||||
bezierPath);
|
||||
targetPath._addPathWithMatrix(bezierPath as SurfacePath, 0, 0, matrix4);
|
||||
targetPath._addPath(bezierPath, 0, 0, matrix4, SPathAddPathMode.kAppend);
|
||||
}
|
||||
|
||||
void _drawArcWithBezier(
|
||||
@@ -1848,7 +1854,7 @@ class RRectCommand extends PathCommand {
|
||||
void transform(Float32List matrix4, SurfacePath targetPath) {
|
||||
final ui.Path roundRectPath = ui.Path();
|
||||
_RRectToPathRenderer(roundRectPath).render(rrect);
|
||||
targetPath._addPathWithMatrix(roundRectPath as SurfacePath, 0, 0, matrix4);
|
||||
targetPath._addPath(roundRectPath, 0, 0, matrix4, SPathAddPathMode.kAppend);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.6
|
||||
import 'dart:math' as math;
|
||||
|
||||
import 'package:ui/ui.dart';
|
||||
@@ -103,7 +102,7 @@ void main() {
|
||||
path.addRRect(RRect.fromLTRBR(20, 30, 220, 130, Radius.elliptical(8, 4)));
|
||||
final List<double> contourLengths = computeLengths(path.computeMetrics());
|
||||
expect(contourLengths.length, 1);
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 590.361));
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 590.408));
|
||||
});
|
||||
|
||||
test('arcToPoint < 90 degrees', () {
|
||||
@@ -132,7 +131,7 @@ void main() {
|
||||
rotation: 0.0);
|
||||
final List<double> contourLengths = computeLengths(path.computeMetrics());
|
||||
expect(contourLengths.length, 1);
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 156.994));
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 156.827));
|
||||
});
|
||||
|
||||
test('arcToPoint 180 degrees', () {
|
||||
@@ -161,7 +160,7 @@ void main() {
|
||||
rotation: 0.0);
|
||||
final List<double> contourLengths = computeLengths(path.computeMetrics());
|
||||
expect(contourLengths.length, 1);
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 313.989));
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 313.654));
|
||||
});
|
||||
|
||||
test('arcToPoint 270 degrees', () {
|
||||
@@ -190,7 +189,7 @@ void main() {
|
||||
rotation: 0.0);
|
||||
final List<double> contourLengths = computeLengths(path.computeMetrics());
|
||||
expect(contourLengths.length, 1);
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 470.983));
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 470.482));
|
||||
});
|
||||
|
||||
test('arcToPoint 270 degrees rx!=ry', () {
|
||||
@@ -219,7 +218,7 @@ void main() {
|
||||
rotation: 0.0);
|
||||
final List<double> contourLengths = computeLengths(path.computeMetrics());
|
||||
expect(contourLengths.length, 1);
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 363.090));
|
||||
expect(contourLengths[0], within(distance: kTolerance, from: 362.733));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -30,9 +30,8 @@ void main() {
|
||||
underTest.apply(mockCanvas, screenRect);
|
||||
|
||||
_expectDrawDRRectCall(mockCanvas, <String, dynamic>{
|
||||
'outer': rrect,
|
||||
'inner': rrect.deflate(1),
|
||||
'paint': somePaint.paintData,
|
||||
'path': 'Path(MoveTo(10, 47) LineTo(10, 13) Conic(10, 10, 10, 13, w = 0.7071067690849304) LineTo(47, 10) Conic(50, 10, 10, 50, w = 0.7071067690849304) LineTo(50, 47) Conic(50, 50, 50, 47, w = 0.7071067690849304) LineTo(13, 50) Conic(10, 50, 50, 10, w = 0.7071067690849304) Close() MoveTo(11, 47) LineTo(11, 13) Conic(11, 11, 11, 13, w = 0.7071067690849304) LineTo(47, 11) Conic(49, 11, 11, 49, w = 0.7071067690849304) LineTo(49, 47) Conic(49, 49, 49, 47, w = 0.7071067690849304) LineTo(13, 49) Conic(11, 49, 49, 11, w = 0.7071067690849304) Close())',
|
||||
'paint': somePaint.paintData,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -82,8 +81,7 @@ void main() {
|
||||
|
||||
// Expect to draw, even when inner has negative radii (which get ignored by canvas)
|
||||
_expectDrawDRRectCall(mockCanvas, <String, dynamic>{
|
||||
'outer': outer,
|
||||
'inner': inner,
|
||||
'path': 'Path(MoveTo(0, 42) LineTo(0, 6) Conic(0, 0, 0, 6, w = 0.7071067690849304) LineTo(88, 0) Conic(88, 0, 0, 88, w = 0.7071067690849304) LineTo(88, 48) Conic(88, 48, 48, 88, w = 0.7071067690849304) LineTo(6, 48) Conic(0, 48, 48, 0, w = 0.7071067690849304) Close() MoveTo(1, 42) LineTo(1, 6) Conic(1, 1, 1, 6, w = 0.7071067690849304) LineTo(87, 1) Conic(87, 1, 1, 87, w = 0.7071067690849304) LineTo(87, 47) Conic(87, 47, 47, 87, w = 0.7071067690849304) LineTo(6, 47) Conic(1, 47, 47, 1, w = 0.7071067690849304) Close())',
|
||||
'paint': somePaint.paintData,
|
||||
});
|
||||
});
|
||||
@@ -99,8 +97,7 @@ void main() {
|
||||
underTest.apply(mockCanvas, screenRect);
|
||||
|
||||
_expectDrawDRRectCall(mockCanvas, <String, dynamic>{
|
||||
'outer': outer,
|
||||
'inner': inner,
|
||||
'path': 'Path(MoveTo(10, 20) LineTo(30, 20) LineTo(30, 40) LineTo(10, 40) Close() MoveTo(12, 22) LineTo(28, 22) LineTo(28, 38) LineTo(12, 38) Close())',
|
||||
'paint': somePaint.paintData,
|
||||
});
|
||||
});
|
||||
@@ -197,6 +194,11 @@ void _expectDrawDRRectCall(
|
||||
MockEngineCanvas mock, Map<String, dynamic> expectedArguments) {
|
||||
expect(mock.methodCallLog.length, equals(2));
|
||||
MockCanvasCall mockCall = mock.methodCallLog[0];
|
||||
expect(mockCall.methodName, equals('drawDRRect'));
|
||||
expect(mockCall.arguments, equals(expectedArguments));
|
||||
expect(mockCall.methodName, equals('drawPath'));
|
||||
Map<String, dynamic> argMap = mockCall.arguments as Map<String, dynamic>;
|
||||
Map<String, dynamic> argContents = <String, dynamic>{};
|
||||
argMap.forEach((String key, dynamic value) {
|
||||
argContents[key] = value is SurfacePath ? value.toString() : value;
|
||||
});
|
||||
expect(argContents, equals(expectedArguments));
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import 'package:web_engine_tester/golden_tester.dart';
|
||||
void main() async {
|
||||
final Rect region = Rect.fromLTWH(8, 8, 600, 800); // Compensate for old scuba tester padding
|
||||
|
||||
Future<void> testPath(Path path, String scubaFileName, {Paint paint}) async {
|
||||
Future<void> testPath(Path path, String scubaFileName, {Paint paint, double maxDiffRatePercent = null}) async {
|
||||
const Rect canvasBounds = Rect.fromLTWH(0, 0, 600, 800);
|
||||
final BitmapCanvas bitmapCanvas = BitmapCanvas(canvasBounds);
|
||||
final RecordingCanvas canvas = RecordingCanvas(canvasBounds);
|
||||
@@ -40,7 +40,7 @@ void main() async {
|
||||
canvas.endRecording();
|
||||
canvas.apply(bitmapCanvas, canvasBounds);
|
||||
|
||||
await matchGoldenFile('$scubaFileName.png', region: region);
|
||||
await matchGoldenFile('$scubaFileName.png', region: region, maxDiffRatePercent: maxDiffRatePercent);
|
||||
|
||||
bitmapCanvas.rootElement.remove();
|
||||
svgElement.remove();
|
||||
@@ -106,7 +106,7 @@ void main() async {
|
||||
final Path path = Path();
|
||||
path.addRect(const Rect.fromLTRB(15, 15, 60, 20));
|
||||
path.addRect(const Rect.fromLTRB(35, 160, 15, 100));
|
||||
await testPath(path, 'svg_rect');
|
||||
await testPath(path, 'svg_rect', maxDiffRatePercent: 1.0);
|
||||
});
|
||||
|
||||
test('render notch', () async {
|
||||
|
||||
@@ -20,6 +20,7 @@ void main() async {
|
||||
// Commit a recording canvas to a bitmap, and compare with the expected
|
||||
Future<void> _checkScreenshot(RecordingCanvas rc, String fileName,
|
||||
{Rect region = const Rect.fromLTWH(0, 0, 500, 500),
|
||||
double maxDiffRatePercent = null,
|
||||
bool write = false}) async {
|
||||
final EngineCanvas engineCanvas = BitmapCanvas(screenRect);
|
||||
rc.endRecording();
|
||||
@@ -30,7 +31,7 @@ void main() async {
|
||||
try {
|
||||
sceneElement.append(engineCanvas.rootElement);
|
||||
html.document.body.append(sceneElement);
|
||||
await matchGoldenFile('$fileName.png', region: region);
|
||||
await matchGoldenFile('$fileName.png', region: region, maxDiffRatePercent: maxDiffRatePercent);
|
||||
} finally {
|
||||
// The page is reused across tests, so remove the element after taking the
|
||||
// Scuba screenshot.
|
||||
@@ -190,7 +191,8 @@ void main() async {
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeWidth = 2.0
|
||||
..color = const Color.fromRGBO(0, 128, 255, 1.0));
|
||||
await _checkScreenshot(rc, 'path_transform_with_arc');
|
||||
await _checkScreenshot(rc, 'path_transform_with_arc',
|
||||
maxDiffRatePercent: 1.4);
|
||||
});
|
||||
|
||||
test('Should draw transformed rrect.', () async {
|
||||
|
||||
@@ -641,7 +641,7 @@ void main() async {
|
||||
await matchGoldenFile(
|
||||
'paint_spread_bounds.png',
|
||||
region: const Rect.fromLTRB(0, 0, 250, 600),
|
||||
maxDiffRatePercent: 0.0,
|
||||
maxDiffRatePercent: 0.01,
|
||||
pixelComparison: PixelComparison.precise,
|
||||
);
|
||||
} finally {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.6
|
||||
import 'dart:js_util' as js_util;
|
||||
import 'dart:html' as html;
|
||||
import 'dart:typed_data';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/ui.dart' hide window;
|
||||
import 'package:ui/src/engine.dart';
|
||||
@@ -12,307 +12,522 @@ import 'package:ui/src/engine.dart';
|
||||
import 'matchers.dart';
|
||||
|
||||
void main() {
|
||||
test('Should have no subpaths when created', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.subpaths.length, 0);
|
||||
group('Path', () {
|
||||
test('Should have no subpaths when created', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.isEmpty, true);
|
||||
});
|
||||
|
||||
test('LineTo should add command', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(5.0, 10.0);
|
||||
path.lineTo(20.0, 40.0);
|
||||
path.lineTo(30.0, 50.0);
|
||||
expect(path.pathRef.countPoints(), 3);
|
||||
expect(path.pathRef.atPoint(2).dx, 30.0);
|
||||
expect(path.pathRef.atPoint(2).dy, 50.0);
|
||||
});
|
||||
|
||||
test('LineTo should add moveTo 0,0 when first call to Path API', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.lineTo(20.0, 40.0);
|
||||
expect(path.pathRef.countPoints(), 2);
|
||||
expect(path.pathRef.atPoint(0).dx, 0);
|
||||
expect(path.pathRef.atPoint(0).dy, 0);
|
||||
expect(path.pathRef.atPoint(1).dx, 20.0);
|
||||
expect(path.pathRef.atPoint(1).dy, 40.0);
|
||||
});
|
||||
|
||||
test('relativeLineTo should increments currentX', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(5.0, 10.0);
|
||||
path.lineTo(20.0, 40.0);
|
||||
path.relativeLineTo(5.0, 5.0);
|
||||
expect(path.pathRef.countPoints(), 3);
|
||||
expect(path.pathRef.atPoint(2).dx, 25.0);
|
||||
expect(path.pathRef.atPoint(2).dy, 45.0);
|
||||
});
|
||||
|
||||
test('Should allow calling relativeLineTo before moveTo', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.relativeLineTo(5.0, 5.0);
|
||||
path.moveTo(5.0, 10.0);
|
||||
expect(path.pathRef.countPoints(), 3);
|
||||
expect(path.pathRef.atPoint(1).dx, 5.0);
|
||||
expect(path.pathRef.atPoint(1).dy, 5.0);
|
||||
expect(path.pathRef.atPoint(2).dx, 5.0);
|
||||
expect(path.pathRef.atPoint(2).dy, 10.0);
|
||||
});
|
||||
|
||||
test('Should allow relativeLineTo after reset', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
final Path subPath = Path();
|
||||
subPath.moveTo(50.0, 60.0);
|
||||
subPath.lineTo(200.0, 200.0);
|
||||
path.extendWithPath(subPath, const Offset(0.0, 0.0));
|
||||
path.reset();
|
||||
path.relativeLineTo(5.0, 5.0);
|
||||
expect(path.pathRef.countPoints(), 2);
|
||||
expect(path.pathRef.atPoint(0).dx, 0);
|
||||
expect(path.pathRef.atPoint(0).dy, 0);
|
||||
expect(path.pathRef.atPoint(1).dx, 5.0);
|
||||
});
|
||||
|
||||
test('Should detect rectangular path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
expect(path.webOnlyPathAsRect, const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
});
|
||||
|
||||
test('Should detect non rectangular path if empty', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.webOnlyPathAsRect, null);
|
||||
});
|
||||
|
||||
test('Should detect non rectangular path if there are multiple subpaths',
|
||||
() {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
path.addRect(const Rect.fromLTWH(5.0, 6.0, 7.0, 8.0));
|
||||
expect(path.webOnlyPathAsRect, null);
|
||||
});
|
||||
|
||||
test('Should detect rounded rectangular path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRRect(RRect.fromRectAndRadius(
|
||||
const Rect.fromLTRB(1.0, 2.0, 30.0, 40.0),
|
||||
const Radius.circular(2.0)));
|
||||
expect(
|
||||
path.webOnlyPathAsRoundedRect,
|
||||
RRect.fromRectAndRadius(const Rect.fromLTRB(1.0, 2.0, 30.0, 40.0),
|
||||
const Radius.circular(2.0)));
|
||||
});
|
||||
|
||||
test('Should detect non rounded rectangular path if empty', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.webOnlyPathAsRoundedRect, null);
|
||||
});
|
||||
|
||||
test('Should detect rectangular path is not round', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
expect(path.webOnlyPathAsRoundedRect, null);
|
||||
});
|
||||
|
||||
test(
|
||||
'Should detect non rounded rectangular path if there are '
|
||||
'multiple subpaths', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRRect(RRect.fromRectAndRadius(
|
||||
const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), const Radius.circular(2.0)));
|
||||
path.addRRect(RRect.fromRectAndRadius(
|
||||
const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), const Radius.circular(2.0)));
|
||||
expect(path.webOnlyPathAsRoundedRect, null);
|
||||
});
|
||||
|
||||
test('Should compute bounds as empty for empty and moveTo only path', () {
|
||||
final Path emptyPath = Path();
|
||||
expect(emptyPath.getBounds(), Rect.zero);
|
||||
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(50, 60);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(50, 60, 50, 60));
|
||||
});
|
||||
|
||||
test('Should compute bounds for multiple addRect calls', () {
|
||||
final Path emptyPath = Path();
|
||||
expect(emptyPath.getBounds(), Rect.zero);
|
||||
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(Rect.fromLTWH(0, 0, 270, 45));
|
||||
path.addRect(Rect.fromLTWH(134.5, 0, 1, 45));
|
||||
expect(path.getBounds(), const Rect.fromLTRB(0, 0, 270, 45));
|
||||
});
|
||||
|
||||
test('Should compute bounds for addRRect', () {
|
||||
SurfacePath path = SurfacePath();
|
||||
final Rect bounds = Rect.fromLTRB(30, 40, 400, 300);
|
||||
RRect rrect = RRect.fromRectAndCorners(bounds,
|
||||
topLeft: Radius.elliptical(1, 2),
|
||||
topRight: Radius.elliptical(3, 4),
|
||||
bottomLeft: Radius.elliptical(5, 6),
|
||||
bottomRight: Radius.elliptical(7, 8));
|
||||
path.addRRect(rrect);
|
||||
expect(path.getBounds(), bounds);
|
||||
expect(path.webOnlyPathAsRoundedRect, rrect);
|
||||
path = SurfacePath();
|
||||
rrect = RRect.fromRectAndCorners(bounds,
|
||||
topLeft: Radius.elliptical(0, 2),
|
||||
topRight: Radius.elliptical(3, 4),
|
||||
bottomLeft: Radius.elliptical(5, 6),
|
||||
bottomRight: Radius.elliptical(7, 8));
|
||||
path.addRRect(rrect);
|
||||
expect(path.getBounds(), bounds);
|
||||
expect(path.webOnlyPathAsRoundedRect, rrect);
|
||||
path = SurfacePath();
|
||||
rrect = RRect.fromRectAndCorners(bounds,
|
||||
topLeft: Radius.elliptical(0, 0),
|
||||
topRight: Radius.elliptical(3, 4),
|
||||
bottomLeft: Radius.elliptical(5, 6),
|
||||
bottomRight: Radius.elliptical(7, 8));
|
||||
path.addRRect(rrect);
|
||||
expect(path.getBounds(), bounds);
|
||||
expect(path.webOnlyPathAsRoundedRect, rrect);
|
||||
path = SurfacePath();
|
||||
rrect = RRect.fromRectAndCorners(bounds,
|
||||
topLeft: Radius.elliptical(1, 2),
|
||||
topRight: Radius.elliptical(0, 0),
|
||||
bottomLeft: Radius.elliptical(5, 6),
|
||||
bottomRight: Radius.elliptical(7, 8));
|
||||
path.addRRect(rrect);
|
||||
expect(path.getBounds(), bounds);
|
||||
expect(path.webOnlyPathAsRoundedRect, rrect);
|
||||
path = SurfacePath();
|
||||
rrect = RRect.fromRectAndCorners(bounds,
|
||||
topLeft: Radius.elliptical(1, 2),
|
||||
topRight: Radius.elliptical(3, 4),
|
||||
bottomLeft: Radius.elliptical(0, 0),
|
||||
bottomRight: Radius.elliptical(7, 8));
|
||||
path.addRRect(rrect);
|
||||
expect(path.getBounds(), bounds);
|
||||
expect(path.webOnlyPathAsRoundedRect, rrect);
|
||||
path = SurfacePath();
|
||||
rrect = RRect.fromRectAndCorners(bounds,
|
||||
topLeft: Radius.elliptical(1, 2),
|
||||
topRight: Radius.elliptical(3, 4),
|
||||
bottomLeft: Radius.elliptical(5, 6),
|
||||
bottomRight: Radius.elliptical(0, 0));
|
||||
path.addRRect(rrect);
|
||||
expect(path.getBounds(), bounds);
|
||||
expect(path.webOnlyPathAsRoundedRect, rrect);
|
||||
});
|
||||
|
||||
test('Should compute bounds for lines', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(25, 30);
|
||||
path.lineTo(100, 200);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(25, 30, 100, 200));
|
||||
|
||||
final SurfacePath path2 = SurfacePath();
|
||||
path2.moveTo(250, 300);
|
||||
path2.lineTo(50, 60);
|
||||
expect(path2.getBounds(), const Rect.fromLTRB(50, 60, 250, 300));
|
||||
});
|
||||
|
||||
test('Should compute bounds for polygon', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addPolygon(<Offset>[
|
||||
Offset(50, 100),
|
||||
Offset(250, 100),
|
||||
Offset(152, 180),
|
||||
Offset(159, 200),
|
||||
Offset(151, 190)
|
||||
], true);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(50, 100, 250, 200));
|
||||
});
|
||||
|
||||
test('Should compute bounds for quadraticBezierTo', () {
|
||||
final SurfacePath path1 = SurfacePath();
|
||||
path1.moveTo(285.2, 682.1);
|
||||
path1.quadraticBezierTo(432.0, 431.4, 594.9, 681.2);
|
||||
expect(
|
||||
path1.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(285.2, 556.5, 594.9, 682.1)));
|
||||
|
||||
// Control point below start , end.
|
||||
final SurfacePath path2 = SurfacePath();
|
||||
path2.moveTo(285.2, 682.1);
|
||||
path2.quadraticBezierTo(447.4, 946.8, 594.9, 681.2);
|
||||
expect(
|
||||
path2.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(285.2, 681.2, 594.9, 814.2)));
|
||||
|
||||
// Control point to the right of end point.
|
||||
final SurfacePath path3 = SurfacePath();
|
||||
path3.moveTo(468.3, 685.6);
|
||||
path3.quadraticBezierTo(644.7, 555.2, 594.9, 681.2);
|
||||
expect(
|
||||
path3.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(468.3, 619.3, 605.9, 685.6)));
|
||||
});
|
||||
|
||||
test('Should compute bounds for cubicTo', () {
|
||||
final SurfacePath path1 = SurfacePath();
|
||||
path1.moveTo(220, 300);
|
||||
path1.cubicTo(230, 120, 400, 125, 410, 280);
|
||||
expect(
|
||||
path1.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 164.3, 410.0, 300.0)));
|
||||
|
||||
// control point 1 to the right of control point 2
|
||||
final SurfacePath path2 = SurfacePath();
|
||||
path2.moveTo(220, 300);
|
||||
path2.cubicTo(564.2, 13.7, 400.0, 125.0, 410.0, 280.0);
|
||||
expect(
|
||||
path2.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 122.8, 440.5, 300.0)));
|
||||
|
||||
// control point 1 to the right of control point 2 inflection
|
||||
final SurfacePath path3 = SurfacePath();
|
||||
path3.moveTo(220, 300);
|
||||
path3.cubicTo(839.8, 67.9, 400.0, 125.0, 410.0, 280.0);
|
||||
expect(
|
||||
path3.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 144.5, 552.1, 300.0)));
|
||||
|
||||
// control point 1 below and between start and end points
|
||||
final SurfacePath path4 = SurfacePath();
|
||||
path4.moveTo(220.0, 300.0);
|
||||
path4.cubicTo(354.8, 388.3, 400.0, 125.0, 410.0, 280.0);
|
||||
expect(
|
||||
path4.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 230.0, 410.0, 318.6)));
|
||||
|
||||
// control points inverted below
|
||||
final SurfacePath path5 = SurfacePath();
|
||||
path5.moveTo(220.0, 300.0);
|
||||
path5.cubicTo(366.5, 487.3, 256.4, 489.9, 410.0, 280.0);
|
||||
expect(
|
||||
path5.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 280.0, 410.0, 439.0)));
|
||||
|
||||
// control points inverted below wide
|
||||
final SurfacePath path6 = SurfacePath();
|
||||
path6.moveTo(220.0, 300.0);
|
||||
path6.cubicTo(496.1, 485.5, 121.4, 491.6, 410.0, 280.0);
|
||||
expect(
|
||||
path6.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 280.0, 410.0, 439.0)));
|
||||
|
||||
// control point 2 and end point swapped
|
||||
final SurfacePath path7 = SurfacePath();
|
||||
path7.moveTo(220.0, 300.0);
|
||||
path7.cubicTo(230.0, 120.0, 394.5, 296.1, 382.3, 124.1);
|
||||
expect(
|
||||
path7.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 124.1, 382.9, 300.0)));
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/46813.
|
||||
test('Should deep copy path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(25, 30);
|
||||
path.lineTo(100, 200);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(25, 30, 100, 200));
|
||||
|
||||
final SurfacePath path2 = SurfacePath.from(path);
|
||||
path2.lineTo(250, 300);
|
||||
expect(path2.getBounds(), const Rect.fromLTRB(25, 30, 250, 300));
|
||||
// Expect original path to stay the same.
|
||||
expect(path.getBounds(), const Rect.fromLTRB(25, 30, 100, 200));
|
||||
});
|
||||
|
||||
test('Should handle contains inclusive right,bottom coordinates', () {
|
||||
final Path path = Path();
|
||||
path.moveTo(50, 60);
|
||||
path.lineTo(110, 60);
|
||||
path.lineTo(110, 190);
|
||||
path.lineTo(50, 190);
|
||||
path.close();
|
||||
expect(path.contains(Offset(80, 190)), true);
|
||||
expect(path.contains(Offset(110, 80)), true);
|
||||
expect(path.contains(Offset(110, 190)), true);
|
||||
expect(path.contains(Offset(110, 191)), false);
|
||||
});
|
||||
|
||||
test('Should not contain top-left of beveled border', () {
|
||||
final Path path = Path();
|
||||
path.moveTo(10, 25);
|
||||
path.lineTo(15, 20);
|
||||
path.lineTo(25, 20);
|
||||
path.lineTo(30, 25);
|
||||
path.lineTo(30, 35);
|
||||
path.lineTo(25, 40);
|
||||
path.lineTo(15, 40);
|
||||
path.lineTo(10, 35);
|
||||
path.close();
|
||||
expect(path.contains(Offset(10, 20)), false);
|
||||
});
|
||||
|
||||
test('Computes contains for cubic curves', () {
|
||||
final Path path = Path();
|
||||
path.moveTo(10, 25);
|
||||
path.cubicTo(10, 20, 10, 20, 20, 15);
|
||||
path.lineTo(25, 20);
|
||||
path.cubicTo(30, 20, 30, 20, 30, 25);
|
||||
path.lineTo(30, 35);
|
||||
path.cubicTo(30, 40, 30, 40, 25, 40);
|
||||
path.lineTo(15, 40);
|
||||
path.cubicTo(10, 40, 10, 40, 10, 35);
|
||||
path.close();
|
||||
expect(path.contains(Offset(10, 20)), false);
|
||||
expect(path.contains(Offset(30, 40)), false);
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/44470
|
||||
test('Should handle contains for devicepixelratio != 1.0', () {
|
||||
js_util.setProperty(html.window, 'devicePixelRatio', 4.0);
|
||||
window.debugOverrideDevicePixelRatio(4.0);
|
||||
final Path path = Path()
|
||||
..moveTo(50, 0)
|
||||
..lineTo(100, 100)
|
||||
..lineTo(0, 100)
|
||||
..lineTo(50, 0)
|
||||
..close();
|
||||
expect(path.contains(Offset(50, 50)), isTrue);
|
||||
js_util.setProperty(html.window, 'devicePixelRatio', 1.0);
|
||||
window.debugOverrideDevicePixelRatio(1.0);
|
||||
// TODO: Investigate failure on CI. Locally this passes.
|
||||
// [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)"
|
||||
}, skip: browserEngine == BrowserEngine.firefox);
|
||||
|
||||
// Path contains should handle case where invalid RRect with large
|
||||
// corner radius is used for hit test. Use case is a RenderPhysicalShape
|
||||
// with a clipper that contains RRect of width/height 50 but corner radius
|
||||
// of 100.
|
||||
//
|
||||
// Regression test for https://github.com/flutter/flutter/issues/48887
|
||||
test('Should hit test correctly for malformed rrect', () {
|
||||
// Correctly formed rrect.
|
||||
final Path path1 = Path()
|
||||
..addRRect(RRect.fromLTRBR(50, 50, 100, 100, Radius.circular(20)));
|
||||
expect(path1.contains(Offset(75, 75)), isTrue);
|
||||
expect(path1.contains(Offset(52, 75)), isTrue);
|
||||
expect(path1.contains(Offset(50, 50)), isFalse);
|
||||
expect(path1.contains(Offset(100, 50)), isFalse);
|
||||
expect(path1.contains(Offset(100, 100)), isFalse);
|
||||
expect(path1.contains(Offset(50, 100)), isFalse);
|
||||
|
||||
final Path path2 = Path()
|
||||
..addRRect(RRect.fromLTRBR(50, 50, 100, 100, Radius.circular(100)));
|
||||
expect(path2.contains(Offset(75, 75)), isTrue);
|
||||
expect(path2.contains(Offset(52, 75)), isTrue);
|
||||
expect(path2.contains(Offset(50, 50)), isFalse);
|
||||
expect(path2.contains(Offset(100, 50)), isFalse);
|
||||
expect(path2.contains(Offset(100, 100)), isFalse);
|
||||
expect(path2.contains(Offset(50, 100)), isFalse);
|
||||
});
|
||||
|
||||
test('Should set segment masks', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.pathRef.computeSegmentMask();
|
||||
expect(path.pathRef.segmentMasks, 0);
|
||||
path.moveTo(20, 40);
|
||||
path.pathRef.computeSegmentMask();
|
||||
expect(path.pathRef.segmentMasks, 0);
|
||||
path.lineTo(200, 40);
|
||||
path.pathRef.computeSegmentMask();
|
||||
expect(
|
||||
path.pathRef.segmentMasks, SPathSegmentMask.kLine_SkPathSegmentMask);
|
||||
});
|
||||
|
||||
test('Should convert conic to quad when approximation error is small', () {
|
||||
final Conic conic = Conic(120.0, 20.0, 160.99470420829266, 20.0,
|
||||
190.19301120261332, 34.38770865870253, 0.9252691032413082);
|
||||
expect(conic.toQuads().length, 3);
|
||||
});
|
||||
|
||||
test('Should be able to construct from empty path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
final SurfacePath? path2 = SurfacePath.from(path);
|
||||
assert(path2 != null, true);
|
||||
});
|
||||
});
|
||||
|
||||
test('LineTo should add command', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(5.0, 10.0);
|
||||
path.lineTo(20.0, 40.0);
|
||||
path.lineTo(30.0, 50.0);
|
||||
expect(path.subpaths.length, 1);
|
||||
expect(path.subpaths[0].currentX, 30.0);
|
||||
expect(path.subpaths[0].currentY, 50.0);
|
||||
group('PathRef', () {
|
||||
test('Should return empty when created', () {
|
||||
final PathRef pathRef = PathRef();
|
||||
expect(pathRef.isEmpty, true);
|
||||
});
|
||||
|
||||
test('Should return non-empty when mutated', () {
|
||||
final PathRef pathRef = PathRef();
|
||||
pathRef.growForVerb(SPath.kMoveVerb, 0);
|
||||
expect(pathRef.isEmpty, false);
|
||||
});
|
||||
});
|
||||
group('PathRefIterator', () {
|
||||
test('Should iterate through empty path', () {
|
||||
final Float32List points = Float32List(20);
|
||||
final PathRef pathRef = PathRef();
|
||||
final PathRefIterator iter = PathRefIterator(pathRef);
|
||||
expect(iter.next(points), SPath.kDoneVerb);
|
||||
});
|
||||
|
||||
test('LineTo should add moveTo 0,0 when first call to Path API', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.lineTo(20.0, 40.0);
|
||||
expect(path.subpaths.length, 1);
|
||||
expect(path.subpaths[0].currentX, 20.0);
|
||||
expect(path.subpaths[0].currentY, 40.0);
|
||||
});
|
||||
test('Should iterate through verbs', () {
|
||||
final Float32List points = Float32List(20);
|
||||
final PathRef pathRef = PathRef();
|
||||
pathRef.growForVerb(SPath.kMoveVerb, 0);
|
||||
pathRef.growForVerb(SPath.kLineVerb, 0);
|
||||
pathRef.growForVerb(SPath.kQuadVerb, 0);
|
||||
pathRef.growForVerb(SPath.kCubicVerb, 0);
|
||||
pathRef.growForVerb(SPath.kConicVerb, 0.8);
|
||||
pathRef.growForVerb(SPath.kLineVerb, 0.8);
|
||||
final PathRefIterator iter = PathRefIterator(pathRef);
|
||||
expect(iter.next(points), SPath.kMoveVerb);
|
||||
expect(iter.next(points), SPath.kLineVerb);
|
||||
expect(iter.next(points), SPath.kQuadVerb);
|
||||
expect(iter.next(points), SPath.kCubicVerb);
|
||||
expect(iter.next(points), SPath.kConicVerb);
|
||||
expect(iter.next(points), SPath.kLineVerb);
|
||||
expect(iter.next(points), SPath.kDoneVerb);
|
||||
});
|
||||
|
||||
test('relativeLineTo should increments currentX', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(5.0, 10.0);
|
||||
path.lineTo(20.0, 40.0);
|
||||
path.relativeLineTo(5.0, 5.0);
|
||||
expect(path.subpaths.length, 1);
|
||||
expect(path.subpaths[0].currentX, 25.0);
|
||||
expect(path.subpaths[0].currentY, 45.0);
|
||||
});
|
||||
test('Should iterate by index through empty path', () {
|
||||
final PathRef pathRef = PathRef();
|
||||
final PathRefIterator iter = PathRefIterator(pathRef);
|
||||
expect(iter.nextIndex(), SPath.kDoneVerb);
|
||||
});
|
||||
|
||||
test('Should allow calling relativeLineTo before moveTo', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.relativeLineTo(5.0, 5.0);
|
||||
path.moveTo(5.0, 10.0);
|
||||
expect(path.subpaths.length, 2);
|
||||
expect(path.subpaths[0].currentX, 5.0);
|
||||
expect(path.subpaths[0].currentY, 5.0);
|
||||
expect(path.subpaths[1].currentX, 5.0);
|
||||
expect(path.subpaths[1].currentY, 10.0);
|
||||
});
|
||||
test('Should iterate through contours', () {
|
||||
final PathRef pathRef = PathRef();
|
||||
pathRef.growForVerb(SPath.kMoveVerb, 0);
|
||||
pathRef.growForVerb(SPath.kLineVerb, 0);
|
||||
pathRef.growForVerb(SPath.kQuadVerb, 0);
|
||||
pathRef.growForVerb(SPath.kCubicVerb, 0);
|
||||
pathRef.growForVerb(SPath.kMoveVerb, 0);
|
||||
pathRef.growForVerb(SPath.kConicVerb, 0.8);
|
||||
pathRef.growForVerb(SPath.kLineVerb, 0.8);
|
||||
pathRef.growForVerb(SPath.kCloseVerb, 0.8);
|
||||
pathRef.growForVerb(SPath.kMoveVerb, 0);
|
||||
pathRef.growForVerb(SPath.kLineVerb, 0);
|
||||
pathRef.growForVerb(SPath.kLineVerb, 0);
|
||||
final PathRefIterator iter = PathRefIterator(pathRef);
|
||||
int start = iter.pointIndex;
|
||||
int end = iter.skipToNextContour();
|
||||
expect(end - start, 7);
|
||||
|
||||
test('Should allow relativeLineTo after reset', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
final Path subPath = Path();
|
||||
subPath.moveTo(50.0, 60.0);
|
||||
subPath.lineTo(200.0, 200.0);
|
||||
path.extendWithPath(subPath, const Offset(0.0, 0.0));
|
||||
path.reset();
|
||||
path.relativeLineTo(5.0, 5.0);
|
||||
expect(path.subpaths.length, 1);
|
||||
expect(path.subpaths[0].currentX, 5.0);
|
||||
expect(path.subpaths[0].currentY, 5.0);
|
||||
});
|
||||
start = end;
|
||||
end = iter.skipToNextContour();
|
||||
expect(end - start, 4);
|
||||
|
||||
test('Should detect rectangular path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
expect(path.webOnlyPathAsRect, const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
});
|
||||
start = end;
|
||||
end = iter.skipToNextContour();
|
||||
expect(end - start, 3);
|
||||
|
||||
test('Should detect non rectangular path if empty', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.webOnlyPathAsRect, null);
|
||||
});
|
||||
|
||||
test('Should detect non rectangular path if there are multiple subpaths', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
path.addRect(const Rect.fromLTWH(5.0, 6.0, 7.0, 8.0));
|
||||
expect(path.webOnlyPathAsRect, null);
|
||||
});
|
||||
|
||||
test('Should detect rounded rectangular path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRRect(RRect.fromRectAndRadius(
|
||||
const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), const Radius.circular(2.0)));
|
||||
expect(
|
||||
path.webOnlyPathAsRoundedRect,
|
||||
RRect.fromRectAndRadius(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0),
|
||||
const Radius.circular(2.0)));
|
||||
});
|
||||
|
||||
test('Should detect non rounded rectangular path if empty', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.webOnlyPathAsRoundedRect, null);
|
||||
});
|
||||
|
||||
test('Should detect rectangular path is not round', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0));
|
||||
expect(path.webOnlyPathAsRoundedRect, null);
|
||||
});
|
||||
|
||||
test(
|
||||
'Should detect non rounded rectangular path if there are '
|
||||
'multiple subpaths', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRRect(RRect.fromRectAndRadius(
|
||||
const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), const Radius.circular(2.0)));
|
||||
path.addRRect(RRect.fromRectAndRadius(
|
||||
const Rect.fromLTWH(1.0, 2.0, 3.0, 4.0), const Radius.circular(2.0)));
|
||||
expect(path.webOnlyPathAsRoundedRect, null);
|
||||
});
|
||||
|
||||
test('Should compute bounds as empty for empty and moveTo only path', () {
|
||||
final Path emptyPath = Path();
|
||||
expect(emptyPath.getBounds(), Rect.zero);
|
||||
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(50, 60);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(50, 60, 50, 60));
|
||||
});
|
||||
|
||||
test('Should compute bounds for multiple addRect calls', () {
|
||||
final Path emptyPath = Path();
|
||||
expect(emptyPath.getBounds(), Rect.zero);
|
||||
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addRect(Rect.fromLTWH(0, 0, 270, 45));
|
||||
path.addRect(Rect.fromLTWH(134.5, 0, 1, 45));
|
||||
expect(path.getBounds(), const Rect.fromLTRB(0, 0, 270, 45));
|
||||
});
|
||||
|
||||
test('Should compute bounds for lines', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(25, 30);
|
||||
path.lineTo(100, 200);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(25, 30, 100, 200));
|
||||
|
||||
final SurfacePath path2 = SurfacePath();
|
||||
path2.moveTo(250, 300);
|
||||
path2.lineTo(50, 60);
|
||||
expect(path2.getBounds(), const Rect.fromLTRB(50, 60, 250, 300));
|
||||
});
|
||||
|
||||
test('Should compute bounds for quadraticBezierTo', () {
|
||||
final SurfacePath path1 = SurfacePath();
|
||||
path1.moveTo(285.2, 682.1);
|
||||
path1.quadraticBezierTo(432.0, 431.4, 594.9, 681.2);
|
||||
expect(
|
||||
path1.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(285.2, 556.5, 594.9, 682.1)));
|
||||
|
||||
// Control point below start , end.
|
||||
final SurfacePath path2 = SurfacePath();
|
||||
path2.moveTo(285.2, 682.1);
|
||||
path2.quadraticBezierTo(447.4, 946.8, 594.9, 681.2);
|
||||
expect(
|
||||
path2.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(285.2, 681.2, 594.9, 814.2)));
|
||||
|
||||
// Control point to the right of end point.
|
||||
final SurfacePath path3 = SurfacePath();
|
||||
path3.moveTo(468.3, 685.6);
|
||||
path3.quadraticBezierTo(644.7, 555.2, 594.9, 681.2);
|
||||
expect(
|
||||
path3.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(468.3, 619.3, 605.9, 685.6)));
|
||||
});
|
||||
|
||||
test('Should compute bounds for cubicTo', () {
|
||||
final SurfacePath path1 = SurfacePath();
|
||||
path1.moveTo(220, 300);
|
||||
path1.cubicTo(230, 120, 400, 125, 410, 280);
|
||||
expect(
|
||||
path1.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 164.3, 410.0, 300.0)));
|
||||
|
||||
// control point 1 to the right of control point 2
|
||||
final SurfacePath path2 = SurfacePath();
|
||||
path2.moveTo(220, 300);
|
||||
path2.cubicTo(564.2, 13.7, 400.0, 125.0, 410.0, 280.0);
|
||||
expect(
|
||||
path2.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 122.8, 440.5, 300.0)));
|
||||
|
||||
// control point 1 to the right of control point 2 inflection
|
||||
final SurfacePath path3 = SurfacePath();
|
||||
path3.moveTo(220, 300);
|
||||
path3.cubicTo(839.8, 67.9, 400.0, 125.0, 410.0, 280.0);
|
||||
expect(
|
||||
path3.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 144.5, 552.1, 300.0)));
|
||||
|
||||
// control point 1 below and between start and end points
|
||||
final SurfacePath path4 = SurfacePath();
|
||||
path4.moveTo(220.0, 300.0);
|
||||
path4.cubicTo(354.8, 388.3, 400.0, 125.0, 410.0, 280.0);
|
||||
expect(
|
||||
path4.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 230.0, 410.0, 318.6)));
|
||||
|
||||
// control points inverted below
|
||||
final SurfacePath path5 = SurfacePath();
|
||||
path5.moveTo(220.0, 300.0);
|
||||
path5.cubicTo(366.5, 487.3, 256.4, 489.9, 410.0, 280.0);
|
||||
expect(
|
||||
path5.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 280.0, 410.0, 439.0)));
|
||||
|
||||
// control points inverted below wide
|
||||
final SurfacePath path6 = SurfacePath();
|
||||
path6.moveTo(220.0, 300.0);
|
||||
path6.cubicTo(496.1, 485.5, 121.4, 491.6, 410.0, 280.0);
|
||||
expect(
|
||||
path6.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 280.0, 410.0, 439.0)));
|
||||
|
||||
// control point 2 and end point swapped
|
||||
final SurfacePath path7 = SurfacePath();
|
||||
path7.moveTo(220.0, 300.0);
|
||||
path7.cubicTo(230.0, 120.0, 394.5, 296.1, 382.3, 124.1);
|
||||
expect(
|
||||
path7.getBounds(),
|
||||
within<Rect>(
|
||||
distance: 0.1,
|
||||
from: const Rect.fromLTRB(220.0, 124.1, 382.9, 300.0)));
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/46813.
|
||||
test('Should deep copy path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(25, 30);
|
||||
path.lineTo(100, 200);
|
||||
expect(path.getBounds(), const Rect.fromLTRB(25, 30, 100, 200));
|
||||
|
||||
final SurfacePath path2 = SurfacePath.from(path);
|
||||
path2.lineTo(250, 300);
|
||||
expect(path2.getBounds(), const Rect.fromLTRB(25, 30, 250, 300));
|
||||
// Expect original path to stay the same.
|
||||
expect(path.getBounds(), const Rect.fromLTRB(25, 30, 100, 200));
|
||||
});
|
||||
|
||||
// Regression test for https://github.com/flutter/flutter/issues/44470
|
||||
test('Should handle contains for devicepixelratio != 1.0', () {
|
||||
js_util.setProperty(html.window, 'devicePixelRatio', 4.0);
|
||||
window.debugOverrideDevicePixelRatio(4.0);
|
||||
final path = Path()
|
||||
..moveTo(50, 0)
|
||||
..lineTo(100, 100)
|
||||
..lineTo(0, 100)
|
||||
..lineTo(50, 0)
|
||||
..close();
|
||||
expect(path.contains(Offset(50, 50)), isTrue);
|
||||
js_util.setProperty(html.window, 'devicePixelRatio', 1.0);
|
||||
window.debugOverrideDevicePixelRatio(1.0);
|
||||
// TODO: Investigate failure on CI. Locally this passes.
|
||||
// [Exception... "Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)"
|
||||
}, skip: browserEngine == BrowserEngine.firefox);
|
||||
|
||||
// Path contains should handle case where invalid RRect with large
|
||||
// corner radius is used for hit test. Use case is a RenderPhysicalShape
|
||||
// with a clipper that contains RRect of width/height 50 but corner radius
|
||||
// of 100.
|
||||
//
|
||||
// Regression test for https://github.com/flutter/flutter/issues/48887
|
||||
test('Should hit test correctly for malformed rrect', () {
|
||||
// Correctly formed rrect.
|
||||
final path1 = Path()
|
||||
..addRRect(RRect.fromLTRBR(50, 50, 100, 100, Radius.circular(20)));
|
||||
expect(path1.contains(Offset(75, 75)), isTrue);
|
||||
expect(path1.contains(Offset(52, 75)), isTrue);
|
||||
expect(path1.contains(Offset(50, 50)), isFalse);
|
||||
expect(path1.contains(Offset(100, 50)), isFalse);
|
||||
expect(path1.contains(Offset(100, 100)), isFalse);
|
||||
expect(path1.contains(Offset(50, 100)), isFalse);
|
||||
|
||||
final path2 = Path()
|
||||
..addRRect(RRect.fromLTRBR(50, 50, 100, 100, Radius.circular(100)));
|
||||
expect(path2.contains(Offset(75, 75)), isTrue);
|
||||
expect(path2.contains(Offset(52, 75)), isTrue);
|
||||
expect(path2.contains(Offset(50, 50)), isFalse);
|
||||
expect(path2.contains(Offset(100, 50)), isFalse);
|
||||
expect(path2.contains(Offset(100, 100)), isFalse);
|
||||
expect(path2.contains(Offset(50, 100)), isFalse);
|
||||
});
|
||||
|
||||
test('Should convert conic to quad when approximation error is small', () {
|
||||
Conic conic = Conic(120.0, 20.0, 160.99470420829266, 20.0,
|
||||
190.19301120261332, 34.38770865870253, 0.9252691032413082);
|
||||
expect(conic.toQuads().length, 3);
|
||||
start = end;
|
||||
end = iter.skipToNextContour();
|
||||
expect(start, end);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
451
engine/src/flutter/lib/web_ui/test/path_winding_test.dart
Normal file
451
engine/src/flutter/lib/web_ui/test/path_winding_test.dart
Normal file
@@ -0,0 +1,451 @@
|
||||
// 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.
|
||||
|
||||
import 'dart:math' as math;
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/ui.dart' hide window;
|
||||
import 'package:ui/src/engine.dart';
|
||||
|
||||
/// Test winding and convexity of a path.
|
||||
void main() {
|
||||
group('Convexity', () {
|
||||
test('Empty path should be convex', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
expect(path.isConvex, true);
|
||||
});
|
||||
|
||||
test('Circle should be convex', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.addOval(Rect.fromLTRB(0, 0, 20, 20));
|
||||
expect(path.isConvex, true);
|
||||
// 2nd circle.
|
||||
path.addOval(Rect.fromLTRB(0, 0, 20, 20));
|
||||
expect(path.isConvex, false);
|
||||
});
|
||||
|
||||
test('addRect should be convex', () {
|
||||
SurfacePath path = SurfacePath();
|
||||
path.addRect(Rect.fromLTRB(0, 0, 20, 20));
|
||||
assert(path.isConvex, true);
|
||||
|
||||
path = SurfacePath();
|
||||
path.addRectWithDirection(
|
||||
Rect.fromLTRB(0, 0, 20, 20), SPathDirection.kCW, 0);
|
||||
assert(path.isConvex, true);
|
||||
|
||||
path = SurfacePath();
|
||||
path.addRectWithDirection(
|
||||
Rect.fromLTRB(0, 0, 20, 20), SPathDirection.kCCW, 0);
|
||||
assert(path.isConvex, true);
|
||||
});
|
||||
|
||||
test('Quad should be convex', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.quadraticBezierTo(100, 100, 50, 50);
|
||||
expect(path.isConvex, true);
|
||||
});
|
||||
|
||||
test('moveto/lineto convexity', () {
|
||||
final List<LineTestCase> testCases = <LineTestCase>[
|
||||
LineTestCase('', SPathConvexityType.kConvex, SPathDirection.kUnknown),
|
||||
LineTestCase(
|
||||
'0 0', SPathConvexityType.kConvex, SPathDirection.kUnknown),
|
||||
LineTestCase(
|
||||
'0 0 10 10', SPathConvexityType.kConvex, SPathDirection.kUnknown),
|
||||
LineTestCase('0 0 10 10 20 20 0 0 10 10', SPathConvexityType.kConcave,
|
||||
SPathDirection.kUnknown),
|
||||
LineTestCase(
|
||||
'0 0 10 10 10 20', SPathConvexityType.kConvex, SPathDirection.kCW),
|
||||
LineTestCase(
|
||||
'0 0 10 10 10 0', SPathConvexityType.kConvex, SPathDirection.kCCW),
|
||||
LineTestCase('0 0 10 10 10 0 0 10', SPathConvexityType.kConcave, null),
|
||||
LineTestCase('0 0 10 0 0 10 -10 -10', SPathConvexityType.kConcave,
|
||||
SPathDirection.kCW),
|
||||
];
|
||||
|
||||
for (LineTestCase testCase in testCases) {
|
||||
final SurfacePath path = SurfacePath();
|
||||
setFromString(path, testCase.pathContent);
|
||||
expect(path.convexityType, testCase.convexity);
|
||||
}
|
||||
});
|
||||
|
||||
test('Convexity of path with infinite points should return unknown', () {
|
||||
final List<Offset> nonFinitePts = <Offset>[
|
||||
Offset(double.infinity, 0),
|
||||
Offset(0, double.infinity),
|
||||
Offset(double.infinity, double.infinity),
|
||||
Offset(double.negativeInfinity, 0),
|
||||
Offset(0, double.negativeInfinity),
|
||||
Offset(double.negativeInfinity, double.negativeInfinity),
|
||||
Offset(double.negativeInfinity, double.infinity),
|
||||
Offset(double.infinity, double.negativeInfinity),
|
||||
Offset(double.nan, 0),
|
||||
Offset(0, double.nan),
|
||||
Offset(double.nan, double.nan)
|
||||
];
|
||||
final int nonFinitePointsCount = nonFinitePts.length;
|
||||
|
||||
final List<Offset> axisAlignedPts = <Offset>[
|
||||
Offset(kScalarMax, 0),
|
||||
Offset(0, kScalarMax),
|
||||
Offset(kScalarMin, 0),
|
||||
Offset(0, kScalarMin)
|
||||
];
|
||||
final int axisAlignedPointsCount = axisAlignedPts.length;
|
||||
|
||||
final SurfacePath path = SurfacePath();
|
||||
|
||||
for (int index = 0;
|
||||
index < (13 * nonFinitePointsCount * axisAlignedPointsCount);
|
||||
index++) {
|
||||
final int i = index % nonFinitePointsCount;
|
||||
final int f = index % axisAlignedPointsCount;
|
||||
final int g = (f + 1) % axisAlignedPointsCount;
|
||||
path.reset();
|
||||
switch (index % 13) {
|
||||
case 0:
|
||||
path.lineTo(nonFinitePts[i].dx, nonFinitePts[i].dy);
|
||||
break;
|
||||
case 1:
|
||||
path.quadraticBezierTo(nonFinitePts[i].dx, nonFinitePts[i].dy,
|
||||
nonFinitePts[i].dx, nonFinitePts[i].dy);
|
||||
break;
|
||||
case 2:
|
||||
path.quadraticBezierTo(nonFinitePts[i].dx, nonFinitePts[i].dy,
|
||||
axisAlignedPts[f].dx, axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 3:
|
||||
path.quadraticBezierTo(axisAlignedPts[f].dx, axisAlignedPts[f].dy,
|
||||
nonFinitePts[i].dx, nonFinitePts[i].dy);
|
||||
break;
|
||||
case 4:
|
||||
path.cubicTo(
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 5:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 6:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy);
|
||||
break;
|
||||
case 7:
|
||||
path.cubicTo(
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 8:
|
||||
path.cubicTo(
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy);
|
||||
break;
|
||||
case 9:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy);
|
||||
break;
|
||||
case 10:
|
||||
path.cubicTo(
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy);
|
||||
break;
|
||||
case 11:
|
||||
path.cubicTo(
|
||||
nonFinitePts[i].dx,
|
||||
nonFinitePts[i].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy);
|
||||
break;
|
||||
case 12:
|
||||
path.moveTo(nonFinitePts[i].dx, nonFinitePts[i].dy);
|
||||
break;
|
||||
}
|
||||
expect(path.convexityType, SPathConvexityType.kUnknown);
|
||||
}
|
||||
|
||||
for (int index = 0; index < (11 * axisAlignedPointsCount); ++index) {
|
||||
int f = index % axisAlignedPointsCount;
|
||||
int g = (f + 1) % axisAlignedPointsCount;
|
||||
path.reset();
|
||||
int curveSelect = index % 11;
|
||||
switch (curveSelect) {
|
||||
case 0:
|
||||
path.moveTo(axisAlignedPts[f].dx, axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 1:
|
||||
path.lineTo(axisAlignedPts[f].dx, axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 2:
|
||||
path.quadraticBezierTo(axisAlignedPts[f].dx, axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx, axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 3:
|
||||
path.quadraticBezierTo(axisAlignedPts[f].dx, axisAlignedPts[f].dy,
|
||||
axisAlignedPts[g].dx, axisAlignedPts[g].dy);
|
||||
break;
|
||||
case 4:
|
||||
path.quadraticBezierTo(axisAlignedPts[g].dx, axisAlignedPts[g].dy,
|
||||
axisAlignedPts[f].dx, axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 5:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 6:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy);
|
||||
break;
|
||||
case 7:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 8:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy,
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy);
|
||||
break;
|
||||
case 9:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy);
|
||||
break;
|
||||
case 10:
|
||||
path.cubicTo(
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy,
|
||||
axisAlignedPts[f].dx,
|
||||
axisAlignedPts[f].dy,
|
||||
axisAlignedPts[g].dx,
|
||||
axisAlignedPts[g].dy);
|
||||
break;
|
||||
}
|
||||
if (curveSelect != 7 && curveSelect != 10) {
|
||||
int result = path.convexityType;
|
||||
expect(result, SPathConvexityType.kConvex);
|
||||
} else {
|
||||
// we make a copy so that we don't cache the result on the passed
|
||||
// in path.
|
||||
SurfacePath path2 = SurfacePath.from(path);
|
||||
int c = path2.convexityType;
|
||||
assert(SPathConvexityType.kUnknown == c ||
|
||||
SPathConvexityType.kConcave == c);
|
||||
}
|
||||
}
|
||||
});
|
||||
test('Concave lines path', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(-0.284071773, -0.0622361786);
|
||||
path.lineTo(-0.284072, -0.0622351);
|
||||
path.lineTo(-0.28407, -0.0622307);
|
||||
path.lineTo(-0.284067, -0.0622182);
|
||||
path.lineTo(-0.284084, -0.0622269);
|
||||
path.lineTo(-0.284072, -0.0622362);
|
||||
path.close();
|
||||
expect(path.convexityType, SPathConvexityType.kConcave);
|
||||
});
|
||||
|
||||
test('Single moveTo origin', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(0, 0);
|
||||
expect(path.convexityType, SPathConvexityType.kConvex);
|
||||
});
|
||||
|
||||
test('Single diagonal line', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(12, 20);
|
||||
path.lineTo(-12, -20);
|
||||
expect(path.convexityType, SPathConvexityType.kConvex);
|
||||
});
|
||||
|
||||
test('TriLeft', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(0, 0);
|
||||
path.lineTo(1, 0);
|
||||
path.lineTo(1, 1);
|
||||
path.close();
|
||||
expect(path.convexityType, SPathConvexityType.kConvex);
|
||||
});
|
||||
|
||||
test('TriRight', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(0, 0);
|
||||
path.lineTo(-1, 0);
|
||||
path.lineTo(1, 1);
|
||||
path.close();
|
||||
expect(path.convexityType, SPathConvexityType.kConvex);
|
||||
});
|
||||
|
||||
test('square', () {
|
||||
final SurfacePath path = SurfacePath();
|
||||
path.moveTo(0, 0);
|
||||
path.lineTo(1, 0);
|
||||
path.lineTo(1, 1);
|
||||
path.lineTo(0, 1);
|
||||
path.close();
|
||||
expect(path.convexityType, SPathConvexityType.kConvex);
|
||||
});
|
||||
|
||||
test('redundant square', () {
|
||||
final SurfacePath redundantSquare = SurfacePath();
|
||||
redundantSquare.moveTo(0, 0);
|
||||
redundantSquare.lineTo(0, 0);
|
||||
redundantSquare.lineTo(0, 0);
|
||||
redundantSquare.lineTo(1, 0);
|
||||
redundantSquare.lineTo(1, 0);
|
||||
redundantSquare.lineTo(1, 0);
|
||||
redundantSquare.lineTo(1, 1);
|
||||
redundantSquare.lineTo(1, 1);
|
||||
redundantSquare.lineTo(1, 1);
|
||||
redundantSquare.lineTo(0, 1);
|
||||
redundantSquare.lineTo(0, 1);
|
||||
redundantSquare.lineTo(0, 1);
|
||||
redundantSquare.close();
|
||||
expect(redundantSquare.convexityType, SPathConvexityType.kConvex);
|
||||
});
|
||||
|
||||
test('bowtie', () {
|
||||
final SurfacePath bowTie = SurfacePath();
|
||||
bowTie.moveTo(0, 0);
|
||||
bowTie.lineTo(0, 0);
|
||||
bowTie.lineTo(0, 0);
|
||||
bowTie.lineTo(1, 1);
|
||||
bowTie.lineTo(1, 1);
|
||||
bowTie.lineTo(1, 1);
|
||||
bowTie.lineTo(1, 0);
|
||||
bowTie.lineTo(1, 0);
|
||||
bowTie.lineTo(1, 0);
|
||||
bowTie.lineTo(0, 1);
|
||||
bowTie.lineTo(0, 1);
|
||||
bowTie.lineTo(0, 1);
|
||||
bowTie.close();
|
||||
expect(bowTie.convexityType, SPathConvexityType.kConcave);
|
||||
});
|
||||
|
||||
test('sprial', () {
|
||||
final SurfacePath spiral = SurfacePath();
|
||||
spiral.moveTo(0, 0);
|
||||
spiral.lineTo(100, 0);
|
||||
spiral.lineTo(100, 100);
|
||||
spiral.lineTo(0, 100);
|
||||
spiral.lineTo(0, 50);
|
||||
spiral.lineTo(50, 50);
|
||||
spiral.lineTo(50, 75);
|
||||
spiral.close();
|
||||
expect(spiral.convexityType, SPathConvexityType.kConcave);
|
||||
});
|
||||
|
||||
test('dent', () {
|
||||
final SurfacePath dent = SurfacePath();
|
||||
dent.moveTo(0, 0);
|
||||
dent.lineTo(100, 100);
|
||||
dent.lineTo(0, 100);
|
||||
dent.lineTo(-50, 200);
|
||||
dent.lineTo(-200, 100);
|
||||
dent.close();
|
||||
expect(dent.convexityType, SPathConvexityType.kConcave);
|
||||
});
|
||||
|
||||
test('degenerate segments1', () {
|
||||
final SurfacePath strokedSin = SurfacePath();
|
||||
for (int i = 0; i < 2000; i++) {
|
||||
double x = i.toDouble() / 2.0;
|
||||
double y = 500 - (x + math.sin(x / 100) * 40) / 3;
|
||||
if (0 == i) {
|
||||
strokedSin.moveTo(x, y);
|
||||
} else {
|
||||
strokedSin.lineTo(x, y);
|
||||
}
|
||||
}
|
||||
expect(strokedSin.convexityType, SPathConvexityType.kConcave);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class LineTestCase {
|
||||
final String pathContent;
|
||||
final int convexity;
|
||||
final int? direction;
|
||||
LineTestCase(this.pathContent, this.convexity, this.direction);
|
||||
}
|
||||
|
||||
/// Parses a string of the format "mx my lx1 ly1 lx2 ly2..." into a path
|
||||
/// with moveTo/lineTo instructions for points.
|
||||
void setFromString(SurfacePath path, String value) {
|
||||
bool first = true;
|
||||
List<String> points = value.split(' ');
|
||||
if (points.length < 2) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < points.length; i += 2) {
|
||||
if (first) {
|
||||
path.moveTo(double.parse(points[i]), double.parse(points[i + 1]));
|
||||
first = false;
|
||||
} else {
|
||||
path.lineTo(double.parse(points[i]), double.parse(points[i + 1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Scalar max is based on 32 bit float since [PathRef] stores values in
|
||||
// Float32List.
|
||||
const double kScalarMax = 3.402823466e+38;
|
||||
const double kScalarMin = -kScalarMax;
|
||||
Reference in New Issue
Block a user