diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 4e36d64a55..d144243bbc 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -366,6 +366,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/layer.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/layer_scene_builder.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/layer_tree.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/path.dart +FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/picture.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/picture_recorder.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/platform_message.dart diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine.dart b/engine/src/flutter/lib/web_ui/lib/src/engine.dart index 0579928cfd..d44e33dd9d 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine.dart @@ -5,7 +5,7 @@ library engine; import 'dart:async'; -import 'dart:collection' show ListBase; +import 'dart:collection' show ListBase, IterableBase; import 'dart:convert' hide Codec; import 'dart:developer' as developer; import 'dart:html' as html; @@ -34,6 +34,7 @@ part 'engine/compositor/layer.dart'; part 'engine/compositor/layer_scene_builder.dart'; part 'engine/compositor/layer_tree.dart'; part 'engine/compositor/path.dart'; +part 'engine/compositor/path_metrics.dart'; part 'engine/compositor/picture.dart'; part 'engine/compositor/picture_recorder.dart'; part 'engine/compositor/platform_message.dart'; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart index 43932a0fdb..44f07bb671 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart @@ -156,7 +156,7 @@ class SkPath implements ui.Path { @override ui.PathMetrics computeMetrics({bool forceClosed = false}) { - throw 'computeMetrics'; + return SkPathMetrics(this, forceClosed); } @override diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart new file mode 100644 index 0000000000..71f7320528 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path_metrics.dart @@ -0,0 +1,124 @@ +// 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; + +class SkPathMetrics extends IterableBase + implements ui.PathMetrics { + SkPathMetrics(SkPath path, bool forceClosed) + : _iterator = SkPathMetricIterator._(_SkPathMeasure(path, forceClosed)); + + final Iterator _iterator; + + @override + Iterator get iterator => _iterator; +} + +class SkPathMetricIterator implements Iterator { + SkPathMetricIterator._(this._pathMeasure) : assert(_pathMeasure != null); + + _SkPathMetric _pathMetric; + _SkPathMeasure _pathMeasure; + + @override + ui.PathMetric get current => _pathMetric; + + @override + bool moveNext() { + if (_pathMeasure._nextContour()) { + _pathMetric = _SkPathMetric._(_pathMeasure); + return true; + } + _pathMetric = null; + return false; + } +} + +class _SkPathMetric implements ui.PathMetric { + _SkPathMetric._(this._measure) + : assert(_measure != null), + length = _measure.length(_measure.currentContourIndex), + isClosed = _measure.isClosed(_measure.currentContourIndex), + contourIndex = _measure.currentContourIndex; + + @override + final double length; + + @override + final bool isClosed; + + @override + final int contourIndex; + + final _SkPathMeasure _measure; + + @override + ui.Tangent getTangentForOffset(double distance) { + return _measure.getTangentForOffset(contourIndex, distance); + } + + @override + ui.Path extractPath(double start, double end, {bool startWithMoveTo = true}) { + return _measure.extractPath(contourIndex, start, end, + startWithMoveTo: startWithMoveTo); + } + + @override + String toString() => 'PathMetric{length: $length, isClosed: $isClosed, ' + 'contourIndex: $contourIndex}'; +} + +class _SkPathMeasure { + _SkPathMeasure(SkPath path, bool forceClosed) { + currentContourIndex = -1; + pathMeasure = js.JsObject(canvasKit['SkPathMeasure'], [ + path._skPath, + forceClosed, + 1, + ]); + } + + js.JsObject pathMeasure; + + double length(int contourIndex) { + assert(contourIndex == currentContourIndex, + 'PathMetrics are invalid if it is not the current contour.'); + return pathMeasure.callMethod('getLength'); + } + + ui.Tangent getTangentForOffset(int contourIndex, double distance) { + assert(contourIndex == currentContourIndex, + 'PathMetrics are invalid if it is not the current contour.'); + final js.JsObject posTan = + pathMeasure.callMethod('getPosTan', [distance]); + return ui.Tangent( + ui.Offset(posTan[0], posTan[1]), + ui.Offset(posTan[2], posTan[3]), + ); + } + + ui.Path extractPath(int contourIndex, double start, double end, + {bool startWithMoveTo = true}) { + assert(contourIndex == currentContourIndex, + 'PathMetrics are invalid if it is not the current contour.'); + // TODO(het): Add this once `getSegment` is added to CanvasKit. + throw UnimplementedError('extractPath'); + } + + bool isClosed(int contourIndex) { + assert(contourIndex == currentContourIndex, + 'PathMetrics are invalid if it is not the current contour.'); + return pathMeasure.callMethod('isClosed'); + } + + bool _nextContour() { + final bool next = pathMeasure.callMethod('nextContour'); + if (next) { + currentContourIndex++; + } + return next; + } + + int currentContourIndex; +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart index 7c861fd0f1..941786abd5 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/canvas.dart @@ -2343,11 +2343,11 @@ class PathMetricIterator implements Iterator { /// contour. When the next contour's [PathMetric] is obtained, this object /// becomes invalid. class PathMetric { - final Path path; - final bool forceClosed; + final Path _path; + final bool _forceClosed; /// Create a new empty [Path] object. - PathMetric._(this.path, this.forceClosed); + PathMetric._(this._path, this._forceClosed); /// Return the total length of the current contour. double get length => throw UnimplementedError();