Implement Path.computeMetrics in the CanvasKit backend (flutter/engine#13760)

* Implement Path.computeMetrics in the CanvasKit backend

* Add license for path_metrics.dart
This commit is contained in:
Harry Terkelsen
2019-11-08 15:53:42 -08:00
committed by GitHub
parent b374cbe299
commit 3efc91706b
5 changed files with 131 additions and 5 deletions

View File

@@ -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

View File

@@ -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';

View File

@@ -156,7 +156,7 @@ class SkPath implements ui.Path {
@override
ui.PathMetrics computeMetrics({bool forceClosed = false}) {
throw 'computeMetrics';
return SkPathMetrics(this, forceClosed);
}
@override

View File

@@ -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<ui.PathMetric>
implements ui.PathMetrics {
SkPathMetrics(SkPath path, bool forceClosed)
: _iterator = SkPathMetricIterator._(_SkPathMeasure(path, forceClosed));
final Iterator<ui.PathMetric> _iterator;
@override
Iterator<ui.PathMetric> get iterator => _iterator;
}
class SkPathMetricIterator implements Iterator<ui.PathMetric> {
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'], <dynamic>[
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', <double>[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;
}

View File

@@ -2343,11 +2343,11 @@ class PathMetricIterator implements Iterator<PathMetric> {
/// 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();