Move path part files to libraries (flutter/engine#25842)

This commit is contained in:
Ferhat
2021-04-30 11:14:46 -07:00
committed by GitHub
parent c6998ba927
commit c3bebcf344
22 changed files with 628 additions and 538 deletions

View File

@@ -482,6 +482,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/color_filter.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/dom_canvas.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/dom_renderer.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/engine_canvas.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/font_change_util.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/frame_reference.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/backdrop_filter.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/canvas.dart
@@ -495,12 +496,14 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/painting.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/conic.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/cubic.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path_iterator.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path_metrics.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path_ref.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path_to_svg.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path_utils.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/path_windings.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path/tangent.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/path_to_svg_clip.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/picture.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/platform_view.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart

View File

@@ -38,6 +38,27 @@ export 'engine/alarm_clock.dart';
import 'engine/browser_detection.dart';
export 'engine/browser_detection.dart';
import 'engine/html/path_to_svg_clip.dart';
export 'engine/html/path_to_svg_clip.dart';
import 'engine/html/path/conic.dart';
export 'engine/html/path/conic.dart';
import 'engine/html/path/cubic.dart';
export 'engine/html/path/cubic.dart';
import 'engine/html/path/path_iterator.dart';
export 'engine/html/path/path_iterator.dart';
import 'engine/html/path/path_metrics.dart';
export 'engine/html/path/path_metrics.dart';
import 'engine/html/path/path_ref.dart';
export 'engine/html/path/path_ref.dart';
import 'engine/html/path/path_utils.dart';
export 'engine/html/path/path_utils.dart';
import 'engine/mouse_cursor.dart';
export 'engine/mouse_cursor.dart';
@@ -83,6 +104,9 @@ export 'engine/test_embedding.dart';
import 'engine/ulps.dart';
export 'engine/ulps.dart';
import 'engine/util.dart';
export 'engine/util.dart';
import 'engine/validators.dart';
export 'engine/validators.dart';
@@ -141,13 +165,8 @@ part 'engine/html/image_filter.dart';
part 'engine/html/offset.dart';
part 'engine/html/opacity.dart';
part 'engine/html/painting.dart';
part 'engine/html/path/conic.dart';
part 'engine/html/path/cubic.dart';
part 'engine/html/path/path.dart';
part 'engine/html/path/path_metrics.dart';
part 'engine/html/path/path_ref.dart';
part 'engine/html/path/path_to_svg.dart';
part 'engine/html/path/path_utils.dart';
part 'engine/html/path/path_windings.dart';
part 'engine/html/path/tangent.dart';
part 'engine/html/picture.dart';
@@ -200,7 +219,7 @@ part 'engine/text_editing/autofill_hint.dart';
part 'engine/text_editing/input_type.dart';
part 'engine/text_editing/text_capitalization.dart';
part 'engine/text_editing/text_editing.dart';
part 'engine/util.dart';
part 'engine/font_change_util.dart';
part 'engine/window.dart';
// The mode the app is running in.

View File

@@ -193,7 +193,7 @@ class _CanvasPool extends _SaveStackTracking {
} catch (e) {
// Firefox may explode here:
// https://bugzilla.mozilla.org/show_bug.cgi?id=941146
if (!_isNsErrorFailureException(e)) {
if (!isNsErrorFailureException(e)) {
rethrow;
}
}

View File

@@ -135,8 +135,8 @@ class CanvasKitCanvas implements ui.Canvas {
@override
void drawLine(ui.Offset p1, ui.Offset p2, ui.Paint paint) {
assert(_offsetIsValid(p1));
assert(_offsetIsValid(p2));
assert(offsetIsValid(p1));
assert(offsetIsValid(p2));
assert(paint != null); // ignore: unnecessary_null_comparison
_drawLine(p1, p2, paint);
}
@@ -202,7 +202,7 @@ class CanvasKitCanvas implements ui.Canvas {
@override
void drawCircle(ui.Offset c, double radius, ui.Paint paint) {
assert(_offsetIsValid(c));
assert(offsetIsValid(c));
assert(paint != null); // ignore: unnecessary_null_comparison
_drawCircle(c, radius, paint);
}
@@ -236,7 +236,7 @@ class CanvasKitCanvas implements ui.Canvas {
void drawImage(ui.Image image, ui.Offset p, ui.Paint paint) {
// ignore: unnecessary_null_comparison
assert(image != null); // image is checked on the engine side
assert(_offsetIsValid(p));
assert(offsetIsValid(p));
assert(paint != null); // ignore: unnecessary_null_comparison
_canvas.drawImage(image as CkImage, p, paint as CkPaint);
}
@@ -272,7 +272,7 @@ class CanvasKitCanvas implements ui.Canvas {
@override
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) {
assert(paragraph != null); // ignore: unnecessary_null_comparison
assert(_offsetIsValid(offset));
assert(offsetIsValid(offset));
_drawParagraph(paragraph, offset);
}

View File

@@ -17,14 +17,14 @@ abstract class CkShader extends ManagedSkiaObject<SkShader>
class CkGradientSweep extends CkShader implements ui.Gradient {
CkGradientSweep(this.center, this.colors, this.colorStops, this.tileMode,
this.startAngle, this.endAngle, this.matrix4)
: assert(_offsetIsValid(center)),
: assert(offsetIsValid(center)),
assert(colors != null), // ignore: unnecessary_null_comparison
assert(tileMode != null), // ignore: unnecessary_null_comparison
assert(startAngle != null), // ignore: unnecessary_null_comparison
assert(endAngle != null), // ignore: unnecessary_null_comparison
assert(startAngle < endAngle),
assert(matrix4 == null || _matrix4IsValid(matrix4)) {
_validateColorStops(colors, colorStops);
assert(matrix4 == null || matrix4IsValid(matrix4)) {
validateColorStops(colors, colorStops);
}
final ui.Offset center;
@@ -65,14 +65,14 @@ class CkGradientLinear extends CkShader implements ui.Gradient {
this.colorStops,
this.tileMode,
Float32List? matrix,
) : assert(_offsetIsValid(from)),
assert(_offsetIsValid(to)),
) : assert(offsetIsValid(from)),
assert(offsetIsValid(to)),
assert(colors != null), // ignore: unnecessary_null_comparison
assert(tileMode != null), // ignore: unnecessary_null_comparison
this.matrix4 = matrix {
if (assertionsEnabled) {
assert(matrix4 == null || _matrix4IsValid(matrix4!));
_validateColorStops(colors, colorStops);
assert(matrix4 == null || matrix4IsValid(matrix4!));
validateColorStops(colors, colorStops);
}
}

View File

@@ -0,0 +1,28 @@
// 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;
final ByteData? _fontChangeMessage =
JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'fontsChange'});
// Font load callbacks will typically arrive in sequence, we want to prevent
// sendFontChangeMessage of causing multiple synchronous rebuilds.
// This flag ensures we properly schedule a single call to framework.
bool _fontChangeScheduled = false;
FutureOr<void> sendFontChangeMessage() async {
if (!_fontChangeScheduled) {
_fontChangeScheduled = true;
// Batch updates into next animationframe.
html.window.requestAnimationFrame((num _) {
_fontChangeScheduled = false;
EnginePlatformDispatcher.instance.invokeOnPlatformMessage(
'flutter/system',
_fontChangeMessage,
(_) {},
);
});
}
}

View File

@@ -326,12 +326,12 @@ class PersistedPhysicalShape extends PersistedContainerSurface
/// to clipping rect bounds (which is the case for elevation == 0.0 where
/// we shift outer/inner clip area instead to position clip-path).
final String svgClipPath = elevation == 0.0
? _pathToSvgClipPath(path,
? pathToSvgClipPath(path,
offsetX: -pathBounds.left,
offsetY: -pathBounds.top,
scaleX: 1.0 / pathBounds.width,
scaleY: 1.0 / pathBounds.height)
: _pathToSvgClipPath(path,
: pathToSvgClipPath(path,
offsetX: 0.0,
offsetY: 0.0,
scaleX: 1.0 / pathBounds.right,
@@ -345,7 +345,7 @@ class PersistedPhysicalShape extends PersistedContainerSurface
html.Element.html(svgClipPath, treeSanitizer: _NullTreeSanitizer());
domRenderer.append(rootElement!, _clipElement!);
if (elevation == 0.0) {
DomRenderer.setClipPath(rootElement!, 'url(#svgClip$_clipIdCounter)');
DomRenderer.setClipPath(rootElement!, createSvgClipUrl());
final html.CssStyleDeclaration rootElementStyle = rootElement!.style;
rootElementStyle
..overflow = ''
@@ -360,7 +360,7 @@ class PersistedPhysicalShape extends PersistedContainerSurface
return;
}
DomRenderer.setClipPath(childContainer!, 'url(#svgClip$_clipIdCounter)');
DomRenderer.setClipPath(childContainer!, createSvgClipUrl());
final html.CssStyleDeclaration rootElementStyle = rootElement!.style;
rootElementStyle
..overflow = ''
@@ -500,9 +500,9 @@ class PersistedClipPath extends PersistedContainerSurface
/// Creates an svg clipPath and applies it to [element].
String createSvgClipDef(html.HtmlElement element, ui.Path clipPath) {
final ui.Rect pathBounds = clipPath.getBounds();
final String svgClipPath = _pathToSvgClipPath(clipPath,
final String svgClipPath = pathToSvgClipPath(clipPath,
scaleX: 1.0 / pathBounds.right, scaleY: 1.0 / pathBounds.bottom);
DomRenderer.setClipPath(element, 'url(#svgClip$_clipIdCounter)');
DomRenderer.setClipPath(element, createSvgClipUrl());
// We need to set width and height for the clipElement to cover the
// bounds of the path since browsers such as Safari and Edge
// seem to incorrectly intersect the element bounding rect with

View File

@@ -2,7 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of engine;
import 'dart:typed_data';
import 'dart:math' as math;
import 'package:ui/ui.dart' as ui;
import 'path_utils.dart';
/// Converts conic curve to a list of quadratic curves for rendering on
/// canvas or conversion to svg.
@@ -198,7 +203,7 @@ class Conic {
final double coeff0 = fW * p20 - p20;
final double coeff1 = p20 - 2 * wP10;
final double coeff2 = wP10;
final _QuadRoots quadRoots = _QuadRoots();
final QuadRoots quadRoots = QuadRoots();
int rootCount = quadRoots.findRoots(coeff0, coeff1, coeff2);
assert(rootCount == 0 || rootCount == 1);
if (rootCount == 1) {
@@ -230,12 +235,12 @@ class Conic {
final double dz1 = dz0 + (dz2 - dz0) * t;
// Compute new weights.
final double root = math.sqrt(dz1);
if (_nearlyEqual(root, 0)) {
if (SPath.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)) {
if (SPath.nearlyEqual(dz0, 0) || SPath.nearlyEqual(dz1, 0) || SPath.nearlyEqual(dz2, 0)) {
return false;
}
// Now we can construct the 2 conics by projecting 3D down to 2D.
@@ -314,33 +319,34 @@ class Conic {
double ay = fW * p20y - p20y;
double bx = p20x - cx - cx;
double by = p20y - cy - cy;
_SkQuadCoefficients quadC = _SkQuadCoefficients(ax, ay, bx, by, cx, cy);
SkQuadCoefficients quadC = SkQuadCoefficients(ax, ay, bx, by, cx, cy);
return ui.Offset(quadC.evalX(t), quadC.evalY(t));
}
static double evalNumerator(
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);
}
static double evalDenominator(double w, double t) {
double B = 2 * (w - 1);
double C = 1;
double A = -B;
return polyEval(A, B, C, 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 {
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++];
@@ -398,7 +404,7 @@ class _QuadBounds {
}
}
class _ConicBounds {
class ConicBounds {
double minX = 0;
double minY = 0;
double maxX = 0;
@@ -420,7 +426,7 @@ class _ConicBounds {
// ------------------------------------------------
// {t^2 (2 - 2 w), t (-2 + 2 w), 1}
// Calculate coefficients and solve root.
_QuadRoots roots = _QuadRoots();
QuadRoots roots = QuadRoots();
final double P20x = x2 - x1;
final double P10x = cpX - x1;
final double wP10x = w * P10x;
@@ -431,10 +437,10 @@ class _ConicBounds {
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 denom = Conic.evalDenominator(w, t1);
double numerator = Conic.evalNumerator(x1, cpX, x2, w, t1);
final double extremaX = numerator / denom;
numerator = _conicEvalNumerator(y1, cpY, y2, w, t1);
numerator = Conic.evalNumerator(y1, cpY, y2, w, t1);
final double extremaY = numerator / denom;
// Expand bounds.
minX = math.min(minX, extremaX);
@@ -454,10 +460,10 @@ class _ConicBounds {
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 denom = Conic.evalDenominator(w, t2);
double numerator = Conic.evalNumerator(x1, cpX, x2, w, t2);
final double extrema2X = numerator / denom;
numerator = _conicEvalNumerator(y1, cpY, y2, w, t2);
numerator = Conic.evalNumerator(y1, cpY, y2, w, t2);
final double extrema2Y = numerator / denom;
// Expand bounds.
minX = math.min(minX, extrema2X);

View File

@@ -2,17 +2,21 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of engine;
import 'dart:typed_data';
import 'dart:math' as math;
import '../../util.dart';
import 'path_utils.dart';
/// 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) {
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);
QuadRoots _quadRoots = _findCubicExtrema(y0, y1, y2, y3);
final List<double> roots = _quadRoots.roots;
if (roots.isEmpty) {
// No roots, just use input cubic.
@@ -30,12 +34,12 @@ int _chopCubicAtYExtrema(Float32List points, Float32List dest) {
return rootCount;
}
_QuadRoots _findCubicExtrema(double a, double b, double c, double d) {
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);
return QuadRoots()..findRoots(A, B, C);
}
/// Subdivides cubic curve for a list of t values.
@@ -72,7 +76,7 @@ void _chopCubicAt(
bufferPos += 6;
// watch out in case the renormalized t isn't in range
if ((t = _validUnitDivide(
if ((t = validUnitDivide(
tValues[i + 1] - tValues[i], 1.0 - tValues[i])) ==
null) {
// Can't renormalize last point, just create a degenerate cubic.
@@ -99,18 +103,18 @@ void _chopCubicAtT(Float32List points, int bufferPos, Float32List outPts,
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);
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;
@@ -134,7 +138,7 @@ void _chopCubicAtT(Float32List points, int bufferPos, Float32List outPts,
//
// Options are Newton Raphson (quadratic convergence with typically
// 3 iterations or bisection with 16 iterations.
double? _chopMonoAtY(Float32List _buffer, int bufferStartPos, double y) {
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;
@@ -180,7 +184,7 @@ double? _chopMonoAtY(Float32List _buffer, int bufferStartPos, double y) {
return (tNeg + tPos) / 2;
}
double _evalCubicPts(double c0, double c1, double c2, double c3, double t) {
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);
@@ -189,7 +193,7 @@ double _evalCubicPts(double c0, double c1, double c2, double c3, double t) {
}
// Reusable class to compute bounds without object allocation.
class _CubicBounds {
class CubicBounds {
double minX = 0.0;
double maxX = 0.0;
double minY = 0.0;
@@ -337,7 +341,7 @@ class _CubicBounds {
}
/// Chops cubic spline at startT and stopT, writes result to buffer.
void _chopCubicBetweenT(
void chopCubicBetweenT(
List<double> points, double startT, double stopT, Float32List buffer) {
assert(startT != 0 || stopT != 0);
final double p3y = points[7];
@@ -352,18 +356,18 @@ void _chopCubicBetweenT(
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);
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;
@@ -391,18 +395,18 @@ void _chopCubicBetweenT(
// 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);
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;

View File

@@ -64,7 +64,7 @@ class SurfacePath implements ui.Path {
}
SurfacePath._shallowCopy(SurfacePath source)
: pathRef = PathRef._shallowCopy(source.pathRef) {
: pathRef = PathRef.shallowCopy(source.pathRef) {
_copyFields(source);
}
@@ -453,7 +453,7 @@ class SurfacePath implements ui.Path {
// e.g. canvas.drawArc(0, 359.99, ...)
// -vs- canvas.drawArc(0, 359.9, ...)
// Detect this edge case, and tweak the stop vector.
if (_nearlyEqual(cosStart, cosStop) && _nearlyEqual(sinStart, sinStop)) {
if (SPath.nearlyEqual(cosStart, cosStop) && SPath.nearlyEqual(sinStart, sinStop)) {
final double sweep = sweepAngle.abs() * 180.0 / math.pi;
if (sweep <= 360 && sweep > 359) {
// Use tiny angle (in radians) to tweak.
@@ -583,8 +583,8 @@ class SurfacePath implements ui.Path {
assert(unscaledLength > SPath.scalarNearlyZero);
offCurveX /= cosThetaOver2 * unscaledLength;
offCurveY /= cosThetaOver2 * unscaledLength;
if (!_nearlyEqual(offCurveX, lastQuadrantPoint.dx) ||
!_nearlyEqual(offCurveY, lastQuadrantPoint.dy)) {
if (!SPath.nearlyEqual(offCurveX, lastQuadrantPoint.dx) ||
!SPath.nearlyEqual(offCurveY, lastQuadrantPoint.dy)) {
conics.add(Conic(lastQuadrantPoint.dx, lastQuadrantPoint.dy, offCurveX,
offCurveY, finalPx, finalPy, cosThetaOver2));
++conicCount;
@@ -634,7 +634,8 @@ class SurfacePath implements ui.Path {
final ui.Offset lastPoint = pathRef.atPoint(pointCount - 1);
final double lastPointX = lastPoint.dx;
final double lastPointY = lastPoint.dy;
if (!_nearlyEqual(px, lastPointX) || !_nearlyEqual(py, lastPointY)) {
if (!SPath.nearlyEqual(px, lastPointX)
|| SPath.nearlyEqual(py, lastPointY)) {
lineTo(px, py);
}
}
@@ -796,16 +797,16 @@ class SurfacePath implements ui.Path {
// to start outside their marks. A round rect may lose convexity as a
// result. If the input values are on integers, place the conic on
// integers as well.
bool expectIntegers = _nearlyEqual((math.pi / 2 - thetaWidth.abs()), 0) &&
_isInteger(rx) &&
_isInteger(ry) &&
_isInteger(x) &&
_isInteger(y);
bool expectIntegers = SPath.nearlyEqual((math.pi / 2 - thetaWidth.abs()), 0) &&
SPath.isInteger(rx) &&
SPath.isInteger(ry) &&
SPath.isInteger(x) &&
SPath.isInteger(y);
for (int i = 0; i < segments; i++) {
final double endTheta = startTheta + thetaWidth;
final double sinEndTheta = _snapToZero(math.sin(endTheta));
final double cosEndTheta = _snapToZero(math.cos(endTheta));
final double sinEndTheta = SPath.snapToZero(math.sin(endTheta));
final double cosEndTheta = SPath.snapToZero(math.cos(endTheta));
double unitPts1x = cosEndTheta + centerPointX;
double unitPts1y = sinEndTheta + centerPointY;
double unitPts0x = unitPts1x + t * sinEndTheta;
@@ -943,7 +944,7 @@ class SurfacePath implements ui.Path {
final double startOver90 = startAngle / (math.pi / 2.0);
final double startOver90I = (startOver90 + 0.5).floorToDouble();
final double error = startOver90 - startOver90I;
if (_nearlyEqual(error, 0)) {
if (SPath.nearlyEqual(error, 0)) {
// Index 1 is at startAngle == 0.
double startIndex = startOver90I + 1.0 % 4.0;
startIndex = startIndex < 0 ? startIndex + 4.0 : startIndex;
@@ -1001,7 +1002,7 @@ class SurfacePath implements ui.Path {
if (rrect.isRect || rrect.isEmpty) {
// degenerate(rect) => radii points are collapsing.
addRectWithDirection(bounds, direction, (startIndex + 1) ~/ 2);
} else if (_isRRectOval(rrect)) {
} else if (isRRectOval(rrect)) {
// degenerate(oval) => line points are collapsing.
_addOval(bounds, direction, startIndex ~/ 2);
} else {
@@ -1077,7 +1078,7 @@ class SurfacePath implements ui.Path {
// we are not extending.
if (mode == SPathAddPathMode.kAppend &&
(matrix4 == null || _isSimple2dTransform(matrix4))) {
pathRef._append(source.pathRef);
pathRef.append(source.pathRef);
} else {
bool firstVerb = true;
final PathRefIterator iter = PathRefIterator(source.pathRef);
@@ -1251,13 +1252,13 @@ class SurfacePath implements ui.Path {
if (tangents.length > oldCount) {
int last = tangents.length - 1;
final ui.Offset tangent = tangents[last];
if (_nearlyEqual(_lengthSquaredOffset(tangent), 0)) {
if (SPath.nearlyEqual(lengthSquaredOffset(tangent), 0)) {
tangents.remove(last);
} else {
for (int index = 0; index < last; ++index) {
final ui.Offset test = tangents[index];
double crossProduct = test.dx * tangent.dy - test.dy * tangent.dx;
if (_nearlyEqual(crossProduct, 0) &&
if (SPath.nearlyEqual(crossProduct, 0) &&
SPath.scalarSignedAsInt(tangent.dx * test.dx) <= 0 &&
SPath.scalarSignedAsInt(tangent.dy * test.dy) <= 0) {
ui.Offset offset = tangents.removeAt(last);
@@ -1454,9 +1455,9 @@ class SurfacePath implements ui.Path {
final PathRefIterator iter = PathRefIterator(pathRef);
final Float32List points = pathRef.points;
int verb;
_CubicBounds? cubicBounds;
_QuadBounds? quadBounds;
_ConicBounds? conicBounds;
CubicBounds? cubicBounds;
QuadBounds? quadBounds;
ConicBounds? conicBounds;
while ((verb = iter.nextIndex()) != SPath.kDoneVerb) {
final int pIndex = iter.iterIndex;
switch (verb) {
@@ -1469,7 +1470,7 @@ class SurfacePath implements ui.Path {
minY = maxY = points[pIndex + 3];
break;
case SPath.kQuadVerb:
quadBounds ??= _QuadBounds();
quadBounds ??= QuadBounds();
quadBounds.calculateBounds(points, pIndex);
minX = quadBounds.minX;
minY = quadBounds.minY;
@@ -1477,7 +1478,7 @@ class SurfacePath implements ui.Path {
maxY = quadBounds.maxY;
break;
case SPath.kConicVerb:
conicBounds ??= _ConicBounds();
conicBounds ??= ConicBounds();
conicBounds.calculateBounds(points, iter.conicWeight, pIndex);
minX = conicBounds.minX;
minY = conicBounds.minY;
@@ -1485,7 +1486,7 @@ class SurfacePath implements ui.Path {
maxY = conicBounds.maxY;
break;
case SPath.kCubicVerb:
cubicBounds ??= _CubicBounds();
cubicBounds ??= CubicBounds();
cubicBounds.calculateBounds(points, pIndex);
minX = cubicBounds.minX;
minY = cubicBounds.minY;
@@ -1520,7 +1521,7 @@ class SurfacePath implements ui.Path {
/// as if they had been closed, even if they were not explicitly closed.
@override
SurfacePathMetrics computeMetrics({bool forceClosed = false}) {
return SurfacePathMetrics._(this, forceClosed);
return SurfacePathMetrics(this.pathRef, forceClosed);
}
/// Detects if path is rounded rectangle.
@@ -1650,11 +1651,3 @@ bool _isSimple2dTransform(Float32List m) =>
m[2] == 0.0;
// m[1] - 2D rotation is simple
// m[0] - scale x is simple
double _lengthSquaredOffset(ui.Offset offset) {
final double dx = offset.dx;
final double dy = offset.dy;
return dx * dx + dy * dy;
}
double _lengthSquared(double dx, double dy) => dx * dx + dy * dy;

View File

@@ -0,0 +1,220 @@
// 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:typed_data';
import 'package:ui/ui.dart' as ui;
import 'path_ref.dart';
import 'path_utils.dart';
// 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;
int get pathVerbIndex => _verbIndex;
int get conicWeightIndex => _conicWeightIndex;
/// Maximum buffer size required for points in [next] calls.
static const int kMaxBufferSize = 8;
/// 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() {
if (_verbIndex < pathRef.countVerbs()) {
return pathRef.atVerb(_verbIndex);
}
if (_needClose && _segmentState == SPathSegmentState.kAfterPrimitive) {
return (_lastPointX != _moveToX || _lastPointY != _moveToY)
? SPath.kLineVerb
: SPath.kCloseVerb;
}
return SPath.kDoneVerb;
}
// 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.atVerb(_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);
}

View File

@@ -2,7 +2,17 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of engine;
import 'dart:collection' show IterableBase;
import 'dart:typed_data';
import 'dart:math' as math;
import 'package:ui/ui.dart' as ui;
import 'conic.dart';
import 'cubic.dart';
import 'path_iterator.dart';
import 'path_ref.dart';
import 'path_utils.dart';
const double kEpsilon = 0.000000001;
@@ -22,7 +32,7 @@ const double kEpsilon = 0.000000001;
/// are only valid until the next one is obtained.
class SurfacePathMetrics extends IterableBase<ui.PathMetric>
implements ui.PathMetrics {
SurfacePathMetrics._(SurfacePath path, bool forceClosed)
SurfacePathMetrics(PathRef path, bool forceClosed)
: _iterator =
SurfacePathMetricIterator._(_SurfacePathMeasure(path, forceClosed));
@@ -41,10 +51,10 @@ class _SurfacePathMeasure {
:
// nextContour will increment this to the zero based index.
_currentContourIndex = -1,
_pathIterator = PathIterator(_path.pathRef, forceClosed);
_pathIterator = PathIterator(_path, forceClosed);
final double resScale;
final SurfacePath _path;
final PathRef _path;
PathIterator _pathIterator;
final List<_PathContourMeasure> _contours = [];
@@ -103,11 +113,11 @@ class _SurfacePathMeasure {
// calling `_moveNext` - `_moveNext` should be called after the first
// iteration is done instead of before.
bool _nativeNextContour() {
if (_verbIterIndex == _path.pathRef.countVerbs()) {
if (_verbIterIndex == _path.countVerbs()) {
return false;
}
_PathContourMeasure measure =
_PathContourMeasure(_path.pathRef, _pathIterator, forceClosed);
_PathContourMeasure(_path, _pathIterator, forceClosed);
_verbIterIndex = measure.verbEndIndex;
_contours.add(measure);
return true;
@@ -253,7 +263,7 @@ class _PathContourMeasure {
path.lineTo(toX, toY);
break;
case SPath.kCubicVerb:
_chopCubicBetweenT(points, startT, stopT, _buffer);
chopCubicBetweenT(points, startT, stopT, _buffer);
path.cubicTo(_buffer[2], _buffer[3], _buffer[4], _buffer[5], _buffer[6],
_buffer[7]);
break;
@@ -351,13 +361,13 @@ class _PathContourMeasure {
break;
case SPath.kCloseVerb:
_contourLength = distance;
return iter._verbIndex;
return iter.pathVerbIndex;
default:
break;
}
} while (verb != SPath.kDoneVerb);
_contourLength = distance;
return iter._verbIndex;
return iter.pathVerbIndex;
}
static bool _tspanBigEnough(int tSpan) => (tSpan >> 10) != 0;
@@ -631,8 +641,8 @@ class _PathSegment {
_SurfaceTangent tangentForQuadAt(double t, double x0, double y0, double x1,
double y1, double x2, double y2) {
assert(t >= 0 && t <= 1);
final _SkQuadCoefficients _quadEval =
_SkQuadCoefficients(x0, y0, x1, y1, x2, y2);
final SkQuadCoefficients _quadEval =
SkQuadCoefficients(x0, y0, x1, y1, x2, y2);
final ui.Offset pos = ui.Offset(_quadEval.evalX(t), _quadEval.evalY(t));
// Derivative of quad curve is 2(b - a + (a - 2b + c)t).
// If control point is at start or end point, this yields 0 for t = 0 and
@@ -681,23 +691,6 @@ class _PathSegment {
}
}
/// Evaluates A * t^2 + B * t + C = 0 for quadratic curve.
class _SkQuadCoefficients {
_SkQuadCoefficients(
double x0, double y0, double x1, double y1, double x2, double y2)
: cx = x0,
cy = y0,
bx = 2 * (x1 - x0),
by = 2 * (y1 - y0),
ax = x2 - (2 * x1) + x0,
ay = y2 - (2 * y1) + y0;
final double ax, ay, bx, by, cx, cy;
double evalX(double t) => (ax * t + bx) * t + cx;
double evalY(double t) => (ay * t + by) * t + cy;
}
// Evaluates A * t^3 + B * t^2 + Ct + D = 0 for cubic curve.
class _SkCubicCoefficients {
final double ax, ay, bx, by, cx, cy, dx, dy;
@@ -732,12 +725,12 @@ void _chopQuadBetweenT(
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 abc1x = _interpolate(ab1x, bc1x, t);
final double abc1y = _interpolate(ab1y, bc1y, t);
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 abc1x = interpolate(ab1x, bc1x, t);
final double abc1y = interpolate(ab1y, bc1y, t);
if (!chopStart) {
// Return left side of curve.
buffer[0] = p0x;
@@ -761,12 +754,12 @@ void _chopQuadBetweenT(
// We chopped at startT, now the right hand side of curve is at
// abc1x, abc1y, bc1x, bc1y, p2x, p2y
final double endT = (stopT - startT) / (1 - startT);
final double ab2x = _interpolate(abc1x, bc1x, endT);
final double ab2y = _interpolate(abc1y, bc1y, endT);
final double bc2x = _interpolate(bc1x, p2x, endT);
final double bc2y = _interpolate(bc1y, p2y, endT);
final double abc2x = _interpolate(ab2x, bc2x, endT);
final double abc2y = _interpolate(ab2y, bc2y, endT);
final double ab2x = interpolate(abc1x, bc1x, endT);
final double ab2y = interpolate(abc1y, bc1y, endT);
final double bc2x = interpolate(bc1x, p2x, endT);
final double bc2y = interpolate(bc1y, p2y, endT);
final double abc2x = interpolate(ab2x, bc2x, endT);
final double abc2y = interpolate(ab2y, bc2y, endT);
buffer[0] = abc1x;
buffer[1] = abc1y;

View File

@@ -2,7 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of engine;
import 'dart:typed_data';
import 'dart:js_util' as js_util;
import 'dart:math' as math;
import 'package:ui/ui.dart' as ui;
import '../../util.dart';
import 'path_utils.dart';
/// Stores the path verbs, points and conic weights.
///
@@ -83,7 +90,7 @@ class PathRef {
/// points,verbs and weights arrays. If original path is mutated by adding
/// more verbs, this copy only returns path at the time of copy and shares
/// typed arrays of original path.
PathRef._shallowCopy(PathRef ref)
PathRef.shallowCopy(PathRef ref)
: _fPoints = ref._fPoints,
_fVerbs = ref._fVerbs {
_fVerbsCapacity = ref._fVerbsCapacity;
@@ -126,6 +133,10 @@ class PathRef {
return ui.Offset(_fPoints[index * 2], _fPoints[index * 2 + 1]);
}
double pointXAt(int index) => _fPoints[index * 2];
double pointYAt(int index) => _fPoints[index * 2 + 1];
double atWeight(int index) {
return _conicWeights![index];
}
@@ -266,11 +277,11 @@ class PathRef {
dy = vector1_0y.abs();
}
if (assertionsEnabled) {
final int checkCornerIndex = _nearlyEqual(controlPx, bounds.left)
? (_nearlyEqual(controlPy, bounds.top)
final int checkCornerIndex = SPath.nearlyEqual(controlPx, bounds.left)
? (SPath.nearlyEqual(controlPy, bounds.top)
? _Corner.kUpperLeft
: _Corner.kLowerLeft)
: (_nearlyEqual(controlPy, bounds.top)
: (SPath.nearlyEqual(controlPy, bounds.top)
? _Corner.kUpperRight
: _Corner.kLowerRight);
assert(checkCornerIndex == cornerIndex);
@@ -465,7 +476,7 @@ class PathRef {
_conicWeightsLength = newLength;
}
void _append(PathRef source) {
void append(PathRef source) {
final int pointCount = source.countPoints();
final int curLength = _fPointsLength;
final int newPointCount = curLength + pointCount;

View File

@@ -2,7 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of engine;
import 'dart:math' as math;
import 'package:ui/ui.dart' as ui;
import 'path_ref.dart';
/// Mask used to keep track of types of verbs used in a path segment.
class SPathSegmentMask {
@@ -55,6 +59,14 @@ class SPath {
static int scalarSignedAsInt(double x) {
return x < 0 ? -1 : ((x > 0) ? 1 : 0);
}
// Snaps a value to zero if almost zero (within tolerance).
static double snapToZero(double value) => nearlyEqual(value, 0.0) ? 0.0 : value;
static bool nearlyEqual(double value1, double value2) =>
(value1 - value2).abs() < SPath.scalarNearlyZero;
static bool isInteger(double value) => value.floor() == value;
}
class SPathAddPathMode {
@@ -98,11 +110,11 @@ class SPathSegmentState {
/// Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C])
/// x1 = Q / A
/// x2 = C / Q
class _QuadRoots {
class QuadRoots {
double? root0;
double? root1;
_QuadRoots();
QuadRoots();
/// Returns roots as list.
List<double> get roots => (root0 == null)
@@ -112,7 +124,7 @@ class _QuadRoots {
int findRoots(double a, double b, double c) {
int rootCount = 0;
if (a == 0) {
root0 = _validUnitDivide(-c, b);
root0 = validUnitDivide(-c, b);
return root0 == null ? 0 : 1;
}
@@ -126,12 +138,12 @@ class _QuadRoots {
}
double q = (b < 0) ? -(b - dr) / 2 : -(b + dr) / 2;
double? res = _validUnitDivide(q, a);
double? res = validUnitDivide(q, a);
if (res != null) {
root0 = res;
++rootCount;
}
res = _validUnitDivide(c, q);
res = validUnitDivide(c, q);
if (res != null) {
if (rootCount == 0) {
root0 = res;
@@ -154,7 +166,7 @@ class _QuadRoots {
}
}
double? _validUnitDivide(double numer, double denom) {
double? validUnitDivide(double numer, double denom) {
if (numer < 0) {
numer = -numer;
denom = -denom;
@@ -173,15 +185,7 @@ double? _validUnitDivide(double numer, double denom) {
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) {
bool isRRectOval(ui.RRect rrect) {
if ((rrect.tlRadiusX + rrect.trRadiusX) != rrect.width) {
return false;
}
@@ -206,10 +210,10 @@ double polyEval4(double A, double B, double C, double D, double t) =>
// 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) =>
double interpolate(double startValue, double endValue, double t) =>
(startValue * (1 - t)) + endValue * t;
double _dotProduct(double x0, double y0, double x1, double y1) {
double dotProduct(double x0, double y0, double x1, double y1) {
return x0 * x1 + y0 * y1;
}
@@ -305,18 +309,18 @@ class Convexicator {
final double largest = math.max(
math.max(curVecX, math.max(curVecY, math.max(lastX, lastY))),
-smallest);
if (_nearlyEqual(largest, largest + cross)) {
if (SPath.nearlyEqual(largest, largest + cross)) {
final double nearlyZeroSquared =
SPath.scalarNearlyZero * SPath.scalarNearlyZero;
if (_nearlyEqual(_lengthSquared(lastX, lastY), nearlyZeroSquared) ||
_nearlyEqual(_lengthSquared(curVecX, curVecY), nearlyZeroSquared)) {
if (SPath.nearlyEqual(lengthSquared(lastX, lastY), nearlyZeroSquared) ||
SPath.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
return dotProduct(lastX, lastY, curVecX, curVecY) < 0
? DirChange.kBackwards
: DirChange.kStraight;
}
@@ -371,10 +375,10 @@ class Convexicator {
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];
double vecX = pathRef.pointXAt(pointIndex) -
pathRef.pointXAt(currentPoint);
double vecY = pathRef.pointYAt(pointIndex) -
pathRef.pointYAt(currentPoint);
if (!(vecX == 0 && vecY == 0)) {
// Give up if vector construction failed.
// give up if vector construction failed
@@ -410,3 +414,28 @@ enum DirChange {
kBackwards, // if double back, allow simple lines to be convex
kInvalid
}
double lengthSquaredOffset(ui.Offset offset) {
final double dx = offset.dx;
final double dy = offset.dy;
return dx * dx + dy * dy;
}
double lengthSquared(double dx, double dy) => dx * dx + dy * dy;
/// Evaluates A * t^2 + B * t + C = 0 for quadratic curve.
class SkQuadCoefficients {
SkQuadCoefficients(
double x0, double y0, double x1, double y1, double x2, double y2)
: cx = x0,
cy = y0,
bx = 2 * (x1 - x0),
by = 2 * (y1 - y0),
ax = x2 - (2 * x1) + x0,
ay = y2 - (2 * y1) + y0;
final double ax, ay, bx, by, cx, cy;
double evalX(double t) => (ax * t + bx) * t + cx;
double evalY(double t) => (ay * t + by) * t + cy;
}

View File

@@ -39,7 +39,7 @@ class PathWinding {
_computeQuadWinding();
break;
case SPath.kConicVerb:
_computeConicWinding(pathRef._conicWeights![iter._conicWeightIndex]);
_computeConicWinding(pathRef.conicWeights![iter.conicWeightIndex]);
break;
case SPath.kCubicVerb:
_computeCubicWinding();
@@ -143,7 +143,7 @@ class PathWinding {
return 0;
}
_QuadRoots quadRoots = _QuadRoots();
QuadRoots quadRoots = QuadRoots();
final int n = quadRoots.findRoots(
startY - 2 * y1 + endY, 2 * (y1 - startY), startY - y);
assert(n <= 1);
@@ -158,7 +158,7 @@ class PathWinding {
final double B = 2 * (x1 - C);
xt = polyEval(A, B, C, t);
}
if (_nearlyEqual(xt, x)) {
if (SPath.nearlyEqual(xt, x)) {
if (x != x2 || y != endY) {
// don't test end points; they're start points
_onCurveCount += 1;
@@ -177,7 +177,7 @@ class PathWinding {
final double y1 = buffer[3];
final double x2 = buffer[4];
final double y2 = buffer[5];
double? tValueAtExtrema = _validUnitDivide(y0 - y1, y0 - y1 - y1 + y2);
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));
@@ -259,7 +259,7 @@ class PathWinding {
// B = b*w - w * yCept + yCept - a
B -= C;
C -= y;
final _QuadRoots quadRoots = _QuadRoots();
final QuadRoots quadRoots = QuadRoots();
int n = quadRoots.findRoots(A, 2 * B, C);
assert(n <= 1);
double xt;
@@ -271,10 +271,10 @@ class PathWinding {
} else {
final double root = quadRoots.root0!;
xt =
_conicEvalNumerator(conic.p0x, conic.p1x, conic.p2x, conic.fW, root) /
_conicEvalDenominator(conic.fW, root);
Conic.evalNumerator(conic.p0x, conic.p1x, conic.p2x, conic.fW, root) /
Conic.evalDenominator(conic.fW, root);
}
if (_nearlyEqual(xt, x)) {
if (SPath.nearlyEqual(xt, x)) {
if (x != conic.p2x || y != conic.p2y) {
// don't test end points; they're start points
_onCurveCount += 1;
@@ -285,7 +285,7 @@ class PathWinding {
}
void _computeCubicWinding() {
int n = _chopCubicAtYExtrema(_buffer, _buffer);
int n = chopCubicAtYExtrema(_buffer, _buffer);
for (int i = 0; i <= n; ++i) {
_windingMonoCubic(i * 3 * 2);
}
@@ -334,12 +334,12 @@ class PathWinding {
return;
}
// Compute the actual x(t) value.
double? t = _chopMonoAtY(_buffer, bufferStartPos, y);
double? t = chopMonoAtY(_buffer, bufferStartPos, y);
if (t == null) {
return;
}
double xt = _evalCubicPts(px0, px1, px2, px3, t);
if (_nearlyEqual(xt, x)) {
double xt = evalCubicPts(px0, px1, px2, px3, t);
if (SPath.nearlyEqual(xt, x)) {
if (x != px3 || y != py3) {
// don't test end points; they're start points
_onCurveCount += 1;
@@ -349,210 +349,3 @@ class PathWinding {
_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;
/// Maximum buffer size required for points in [next] calls.
static const int kMaxBufferSize = 8;
/// 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() {
if (_verbIndex < pathRef.countVerbs()) {
return pathRef._fVerbs[_verbIndex];
}
if (_needClose && _segmentState == SPathSegmentState.kAfterPrimitive) {
return (_lastPointX != _moveToX || _lastPointY != _moveToY)
? SPath.kLineVerb
: SPath.kCloseVerb;
}
return SPath.kDoneVerb;
}
// 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);
}

View File

@@ -19,7 +19,7 @@ void tangentLine(
}
final double dx = x1 - x0;
final double dy = y1 - y0;
if (!_nearlyEqual((x - x0) * dy, dx * (y - y0))) {
if (!SPath.nearlyEqual((x - x0) * dy, dx * (y - y0))) {
return;
}
tangents.add(ui.Offset(dx, dy));
@@ -40,7 +40,7 @@ void tangentQuad(
if (!SPath.between(x0, x, x1) && !SPath.between(x1, x, x2)) {
return;
}
final _QuadRoots roots = _QuadRoots();
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!;
@@ -48,7 +48,7 @@ void tangentQuad(
double A = x2 - 2 * x1 + C;
double B = 2 * (x1 - C);
double xt = polyEval(A, B, C, t);
if (!_nearlyEqual(x, xt)) {
if (!SPath.nearlyEqual(x, xt)) {
continue;
}
tangents.add(_evalQuadTangentAt(x0, y0, x1, y1, x2, y2, t));
@@ -100,13 +100,13 @@ void tangentConic(Float32List pts, double x, double y, double weight,
// B = b*w - w * yCept + yCept - a
B -= C;
C -= y;
final _QuadRoots quadRoots = _QuadRoots();
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)) {
double xt = Conic.evalNumerator(x0, x1, x2, weight, t) /
Conic.evalDenominator(weight, t);
if (!SPath.nearlyEqual(x, xt)) {
continue;
}
Conic conic = Conic(x0, y0, x1, y1, x2, y2, weight);
@@ -136,16 +136,16 @@ void tangentCubic(
return;
}
final Float32List dst = Float32List(20);
int n = _chopCubicAtYExtrema(pts, dst);
int n = chopCubicAtYExtrema(pts, dst);
for (int i = 0; i <= n; ++i) {
int bufferPos = i * 6;
double? t = _chopMonoAtY(dst, i * 6, y);
double? t = chopMonoAtY(dst, i * 6, y);
if (t == null) {
continue;
}
double xt = _evalCubicPts(dst[bufferPos], dst[bufferPos + 2],
double xt = evalCubicPts(dst[bufferPos], dst[bufferPos + 2],
dst[bufferPos + 4], dst[bufferPos + 6], t);
if (!_nearlyEqual(x, xt)) {
if (!SPath.nearlyEqual(x, xt)) {
continue;
}
tangents.add(_evalCubicTangentAt(dst, bufferPos, t));
@@ -186,7 +186,7 @@ ui.Offset _evalCubicTangentAt(Float32List points, int bufferPos, double 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(
final SkQuadCoefficients coeff = SkQuadCoefficients(
x3 + 3 * (x1 - x2) - x0,
y3 + 3 * (y1 - y2) - y0,
2 * (x2 - (2 * x1) + x0),

View File

@@ -0,0 +1,56 @@
// 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 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import '../browser_detection.dart';
/// Counter used for generating clip path id inside an svg <defs> tag.
int _clipIdCounter = 0;
/// Used for clipping and filter svg resources.
///
/// Position needs to be absolute since these svgs are sandwiched between
/// canvas elements and can cause layout shifts otherwise.
const String kSvgResourceHeader = '<svg width="0" height="0" '
'style="position:absolute">';
/// Converts Path to svg element that contains a clip-path definition.
///
/// Calling this method updates [_clipIdCounter]. The HTML id of the generated
/// clip is set to "svgClip${_clipIdCounter}", e.g. "svgClip123".
String pathToSvgClipPath(ui.Path path,
{double offsetX = 0,
double offsetY = 0,
double scaleX = 1.0,
double scaleY = 1.0}) {
_clipIdCounter += 1;
final StringBuffer sb = StringBuffer();
sb.write(kSvgResourceHeader);
sb.write('<defs>');
final String clipId = 'svgClip$_clipIdCounter';
if (browserEngine == BrowserEngine.firefox) {
// Firefox objectBoundingBox fails to scale to 1x1 units, instead use
// no clipPathUnits but write the path in target units.
sb.write('<clipPath id=$clipId>');
sb.write('<path fill="#FFFFFF" d="');
} else {
sb.write('<clipPath id=$clipId clipPathUnits="objectBoundingBox">');
sb.write('<path transform="scale($scaleX, $scaleY)" fill="#FFFFFF" d="');
}
pathToSvg(path as SurfacePath, sb, offsetX: offsetX, offsetY: offsetY);
sb.write('"></path></clipPath></defs></svg');
return sb.toString();
}
String createSvgClipUrl() => 'url(#svgClip$_clipIdCounter)';
/// Resets clip ids. Used for testing by [debugForgetFrameScene] API.
void resetSvgClipIds() {
_clipIdCounter = 0;
}

View File

@@ -189,7 +189,7 @@ class RecordingCanvas {
} catch (e) {
// commands should never fail, but...
// https://bugzilla.mozilla.org/show_bug.cgi?id=941146
if (!_isNsErrorFailureException(e)) {
if (!isNsErrorFailureException(e)) {
rethrow;
}
}

View File

@@ -525,7 +525,7 @@ class SurfaceSceneBuilder implements ui.SceneBuilder {
static void debugForgetFrameScene() {
_lastFrameScene?.rootElement?.remove();
_lastFrameScene = null;
_clipIdCounter = 0;
resetSvgClipIds();
_recycledCanvases.clear();
}

View File

@@ -20,14 +20,14 @@ abstract class EngineGradient implements ui.Gradient {
class GradientSweep extends EngineGradient {
GradientSweep(this.center, this.colors, this.colorStops, this.tileMode,
this.startAngle, this.endAngle, this.matrix4)
: assert(_offsetIsValid(center)),
: assert(offsetIsValid(center)),
assert(colors != null), // ignore: unnecessary_null_comparison
assert(tileMode != null), // ignore: unnecessary_null_comparison
assert(startAngle != null), // ignore: unnecessary_null_comparison
assert(endAngle != null), // ignore: unnecessary_null_comparison
assert(startAngle < endAngle),
super._() {
_validateColorStops(colors, colorStops);
validateColorStops(colors, colorStops);
}
@override
@@ -155,14 +155,14 @@ class GradientLinear extends EngineGradient {
this.colorStops,
this.tileMode,
Float32List? matrix,
) : assert(_offsetIsValid(from)),
assert(_offsetIsValid(to)),
) : assert(offsetIsValid(from)),
assert(offsetIsValid(to)),
assert(colors != null), // ignore: unnecessary_null_comparison
assert(tileMode != null), // ignore: unnecessary_null_comparison
this.matrix4 = matrix == null ? null : _FastMatrix32(matrix),
this.matrix4 = matrix == null ? null : FastMatrix32(matrix),
super._() {
if (assertionsEnabled) {
_validateColorStops(colors, colorStops);
validateColorStops(colors, colorStops);
}
}
@@ -171,7 +171,7 @@ class GradientLinear extends EngineGradient {
final List<ui.Color> colors;
final List<double>? colorStops;
final ui.TileMode tileMode;
final _FastMatrix32? matrix4;
final FastMatrix32? matrix4;
@override
Object createPaintStyle(html.CanvasRenderingContext2D? ctx,
@@ -185,7 +185,7 @@ class GradientLinear extends EngineGradient {
html.CanvasGradient _createCanvasGradient(html.CanvasRenderingContext2D? ctx,
ui.Rect? shaderBounds, double density) {
_FastMatrix32? matrix4 = this.matrix4;
FastMatrix32? matrix4 = this.matrix4;
html.CanvasGradient gradient;
final double offsetX = shaderBounds!.left;
final double offsetY = shaderBounds.top;

View File

@@ -2,7 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of engine;
import 'dart:async';
import 'dart:html' as html;
import 'dart:js_util' as js_util;
import 'dart:typed_data';
import 'dart:math' as math;
import 'package:ui/ui.dart' as ui;
import 'browser_detection.dart';
import 'vector_math.dart';
/// Generic callback signature, used by [_futurize].
typedef Callback<T> = void Function(T result);
@@ -313,47 +322,6 @@ bool rectContainsOther(ui.Rect rect, ui.Rect other) {
rect.bottom >= other.bottom;
}
/// Counter used for generating clip path id inside an svg <defs> tag.
int _clipIdCounter = 0;
/// Used for clipping and filter svg resources.
///
/// Position needs to be absolute since these svgs are sandwiched between
/// canvas elements and can cause layout shifts otherwise.
const String kSvgResourceHeader = '<svg width="0" height="0" '
'style="position:absolute">';
/// Converts Path to svg element that contains a clip-path definition.
///
/// Calling this method updates [_clipIdCounter]. The HTML id of the generated
/// clip is set to "svgClip${_clipIdCounter}", e.g. "svgClip123".
String _pathToSvgClipPath(ui.Path path,
{double offsetX = 0,
double offsetY = 0,
double scaleX = 1.0,
double scaleY = 1.0}) {
_clipIdCounter += 1;
final StringBuffer sb = StringBuffer();
sb.write(kSvgResourceHeader);
sb.write('<defs>');
final String clipId = 'svgClip$_clipIdCounter';
if (browserEngine == BrowserEngine.firefox) {
// Firefox objectBoundingBox fails to scale to 1x1 units, instead use
// no clipPathUnits but write the path in target units.
sb.write('<clipPath id=$clipId>');
sb.write('<path fill="#FFFFFF" d="');
} else {
sb.write('<clipPath id=$clipId clipPathUnits="objectBoundingBox">');
sb.write('<path transform="scale($scaleX, $scaleY)" fill="#FFFFFF" d="');
}
pathToSvg(path as SurfacePath, sb, offsetX: offsetX, offsetY: offsetY);
sb.write('"></path></clipPath></defs></svg');
return sb.toString();
}
/// Converts color to a css compatible attribute value.
String? colorToCssString(ui.Color? color) {
if (color == null) {
@@ -418,7 +386,7 @@ String colorComponentsToCssString(int r, int g, int b, int a) {
/// We need this in [BitmapCanvas] and [RecordingCanvas] to swallow this
/// Firefox exception without interfering with others (potentially useful
/// for the programmer).
bool _isNsErrorFailureException(dynamic e) {
bool isNsErrorFailureException(dynamic e) {
return js_util.getProperty(e, 'name') == 'NS_ERROR_FAILURE';
}
@@ -507,34 +475,11 @@ void applyWebkitClipFix(html.Element? containerElement) {
}
}
final ByteData? _fontChangeMessage =
JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'fontsChange'});
// Font load callbacks will typically arrive in sequence, we want to prevent
// sendFontChangeMessage of causing multiple synchronous rebuilds.
// This flag ensures we properly schedule a single call to framework.
bool _fontChangeScheduled = false;
FutureOr<void> sendFontChangeMessage() async {
if (!_fontChangeScheduled) {
_fontChangeScheduled = true;
// Batch updates into next animationframe.
html.window.requestAnimationFrame((num _) {
_fontChangeScheduled = false;
EnginePlatformDispatcher.instance.invokeOnPlatformMessage(
'flutter/system',
_fontChangeMessage,
(_) {},
);
});
}
}
// Stores matrix in a form that allows zero allocation transforms.
class _FastMatrix32 {
class FastMatrix32 {
final Float32List matrix;
double transformedX = 0, transformedY = 0;
_FastMatrix32(this.matrix);
FastMatrix32(this.matrix);
void transform(double x, double y) {
transformedX = matrix[12] + (matrix[0] * x) + (matrix[4] * y);
@@ -567,29 +512,6 @@ bool isUnsoundNull(dynamic object) {
return object == null;
}
bool _offsetIsValid(ui.Offset offset) {
assert(!offset.dx.isNaN && !offset.dy.isNaN,
'Offset argument contained a NaN value.');
return true;
}
bool _matrix4IsValid(Float32List matrix4) {
assert(matrix4.length == 16, 'Matrix4 must have 16 entries.');
return true;
}
void _validateColorStops(List<ui.Color> colors, List<double>? colorStops) {
if (colorStops == null) {
if (colors.length != 2)
throw ArgumentError(
'"colors" must have length 2 if "colorStops" is omitted.');
} else {
if (colors.length != colorStops.length)
throw ArgumentError(
'"colors" and "colorStops" arguments must have equal length.');
}
}
int clampInt(int value, int min, int max) {
assert(min <= max);
if (value < min) {

View File

@@ -47,3 +47,16 @@ bool radiusIsValid(ui.Radius radius) {
'Radius argument contained a NaN value.');
return true;
}
/// Validates color and color stops used for a gradient.
void validateColorStops(List<ui.Color> colors, List<double>? colorStops) {
if (colorStops == null) {
if (colors.length != 2)
throw ArgumentError(
'"colors" must have length 2 if "colorStops" is omitted.');
} else {
if (colors.length != colorStops.length)
throw ArgumentError(
'"colors" and "colorStops" arguments must have equal length.');
}
}