[web] Fix path rendering when addPolygon is chained with other verbs. (flutter/engine#20803)

This commit is contained in:
Ferhat
2020-08-27 20:25:38 -07:00
committed by GitHub
parent 2752ef1dcd
commit 7ad78711f9
3 changed files with 38 additions and 10 deletions

View File

@@ -1,2 +1,2 @@
repository: https://github.com/flutter/goldens.git
revision: 8a831253654d151635ee9cfb71389c257413de5d
revision: 86a069d478c3512e4a91baa07ecb421156e10fce

View File

@@ -21,9 +21,9 @@ part of engine;
/// 3. if we encounter Move without a preceeding Close, and forceClose is true, goto #2
/// 4. if we encounter Line | Quad | Cubic after Close, cons up a Move
class SurfacePath implements ui.Path {
// Flag to require a moveTo if we begin with something else,
// for example empty path lineTo call will inject moveTo.
static const int kInitialLastMoveToIndexValue = -1;
// Initial valid of last move to index so we can detect if a move to
// needs to be inserted after contour closure. See [close].
static const int kInitialLastMoveToIndexValue = 0;
PathRef pathRef;
ui.PathFillType _fillType = ui.PathFillType.nonZero;
@@ -173,8 +173,8 @@ class SurfacePath implements ui.Path {
@override
void moveTo(double x, double y) {
// remember our index
fLastMoveToIndex = pathRef.countPoints() + 1;
int pointIndex = pathRef.growForVerb(SPathVerb.kMove, 0);
fLastMoveToIndex = pointIndex + 1;
pathRef.setPoint(pointIndex, x, y);
_resetAfterEdit();
}
@@ -194,7 +194,7 @@ class SurfacePath implements ui.Path {
}
void _injectMoveToIfNeeded() {
if (fLastMoveToIndex < 0) {
if (fLastMoveToIndex <= 0) {
double x, y;
if (pathRef.countPoints() == 0) {
x = y = 0.0;
@@ -211,7 +211,7 @@ class SurfacePath implements ui.Path {
/// point.
@override
void lineTo(double x, double y) {
if (fLastMoveToIndex < 0) {
if (fLastMoveToIndex <= 0) {
_injectMoveToIfNeeded();
}
int pointIndex = pathRef.growForVerb(SPathVerb.kLine, 0);
@@ -381,6 +381,7 @@ class SurfacePath implements ui.Path {
int finalDirection =
_hasOnlyMoveTos() ? direction : SPathDirection.kUnknown;
int pointIndex0 = pathRef.growForVerb(SPathVerb.kMove, 0);
fLastMoveToIndex = pointIndex0 + 1;
int pointIndex1 = pathRef.growForVerb(SPathVerb.kLine, 0);
int pointIndex2 = pathRef.growForVerb(SPathVerb.kLine, 0);
int pointIndex3 = pathRef.growForVerb(SPathVerb.kLine, 0);
@@ -971,6 +972,7 @@ class SurfacePath implements ui.Path {
return;
}
int pointIndex = pathRef.growForVerb(SPathVerb.kMove, 0);
fLastMoveToIndex = pointIndex + 1;
pathRef.setPoint(pointIndex, points[0].dx, points[0].dy);
pathRef.growForRepeatedVerb(SPathVerb.kLine, pointCount - 1);
for (int i = 1; i < pointCount; i++) {
@@ -1110,7 +1112,7 @@ class SurfacePath implements ui.Path {
lastPointY = pathRef.points[listIndex];
}
// don't add lineTo if it is degenerate.
if (fLastMoveToIndex < 0 ||
if (fLastMoveToIndex <= 0 ||
(previousPointCount != 0) ||
lastPointX != point0X ||
lastPointY != point0Y) {

View File

@@ -27,7 +27,8 @@ void testMain() 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) }) async {
{ Rect region = const Rect.fromLTWH(0, 0, 500, 500),
bool write = false }) async {
final EngineCanvas engineCanvas = BitmapCanvas(screenRect);
@@ -50,7 +51,8 @@ void testMain() async {
try {
sceneElement.append(engineCanvas.rootElement);
html.document.body.append(sceneElement);
await matchGoldenFile('paint_bounds_for_$fileName.png', region: region);
await matchGoldenFile('paint_bounds_for_$fileName.png', region: region,
write: write);
} finally {
// The page is reused across tests, so remove the element after taking the
// Scuba screenshot.
@@ -498,6 +500,30 @@ void testMain() async {
await _checkScreenshot(rc, 'path_with_line_and_roundrect');
});
// Regression test for https://github.com/flutter/flutter/issues/64371.
test('Should draw line following a polygon without closing path.', () async {
final RecordingCanvas rc =
RecordingCanvas(const Rect.fromLTRB(0, 0, 200, 400));
rc.save();
rc.translate(50.0, 100.0);
final Path path = Path();
// Draw a vertical small line (caret).
path.addPolygon(<Offset>[Offset(0, 10), Offset(20,5), Offset(50,10)],
false);
path.lineTo(60, 80);
path.lineTo(0, 80);
path.close();
rc.drawPath(
path,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2.0
..color = const Color(0xFF404000));
rc.restore();
rc.endRecording();
await _checkScreenshot(rc, 'path_with_addpolygon');
});
test('should include paint spread in bounds estimates', () async {
final SurfaceSceneBuilder sb = SurfaceSceneBuilder();