* Fix Path.from to deep copy. Add regression test.
This commit is contained in:
Ferhat
2020-01-08 12:48:35 -08:00
committed by GitHub
parent 530cf1c010
commit 8131de3e75
3 changed files with 33 additions and 1 deletions

View File

@@ -287,10 +287,28 @@ class SurfacePath implements ui.Path {
/// This copy is fast and does not require additional memory unless either
/// the `source` path or the path returned by this constructor are modified.
SurfacePath.from(SurfacePath source)
: subpaths = _deepCopy(source.subpaths);
SurfacePath._shallowCopy(SurfacePath source)
: subpaths = List<Subpath>.from(source.subpaths);
SurfacePath._clone(this.subpaths, this._fillType);
static List<Subpath> _deepCopy(List<Subpath> source) {
// The last sub path can potentially still be mutated by calling ops.
// Copy all sub paths except the last active one which needs a deep copy.
final List<Subpath> paths = [];
int len = source.length;
if (len != 0) {
--len;
for (int i = 0; i < len; i++) {
paths.add(source[i]);
}
paths.add(source[len].shift(const ui.Offset(0, 0)));
}
return paths;
}
/// Determines how the interior of this path is calculated.
///
/// Defaults to the non-zero winding rule, [PathFillType.nonZero].

View File

@@ -309,7 +309,7 @@ class RecordingCanvas {
}
_paintBounds.grow(pathBounds);
// Clone path so it can be reused for subsequent draw calls.
final ui.Path clone = ui.Path.from(path);
final ui.Path clone = SurfacePath._shallowCopy(path);
clone.fillType = path.fillType;
_commands.add(PaintDrawPath(clone, paint.paintData));
}

View File

@@ -238,4 +238,18 @@ void main() {
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));
});
}