From 29f55bb7729341f03b2e3a5fb843d0fe7d6d5d96 Mon Sep 17 00:00:00 2001 From: Ferhat Date: Wed, 10 Feb 2021 17:35:29 -0800 Subject: [PATCH] [web] Fix svg stroke when style is not provided (flutter/engine#24153) --- .../flutter/lib/web_ui/dev/goldens_lock.yaml | 2 +- .../web_ui/lib/src/engine/bitmap_canvas.dart | 23 ++++++++- .../lib/web_ui/lib/src/engine/dom_canvas.dart | 6 ++- .../lib/web_ui/lib/src/engine/html/clip.dart | 4 +- .../lib/src/engine/html/recording_canvas.dart | 15 ++++-- .../engine/path_to_svg_golden_test.dart | 49 ++++++++++++++----- 6 files changed, 78 insertions(+), 21 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml index c6d6a25dd3..9f569f3ce3 100644 --- a/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml +++ b/engine/src/flutter/lib/web_ui/dev/goldens_lock.yaml @@ -1,2 +1,2 @@ repository: https://github.com/flutter/goldens.git -revision: 92381b8ca48729e9b21c6b19dd39ed605a98d4ee +revision: 578ecb91ea33004cd0ba0af513884cc28ba88cf4 diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart index eab08433a9..b5af7c633c 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart @@ -544,14 +544,35 @@ class BitmapCanvas extends EngineCanvas { ..transformOrigin = '0 0 0'; } } + _applyFilter(svgElm, paint); _drawElement(svgElm, ui.Offset(0, 0), paint); } else { _setUpPaint(paint, paint.shader != null ? path.getBounds() : null); - _canvasPool.drawPath(path, paint.style); + if (paint.style == null && paint.strokeWidth != null) { + _canvasPool.drawPath(path, ui.PaintingStyle.stroke); + } else { + _canvasPool.drawPath(path, paint.style); + } _tearDownPaint(); } } + void _applyFilter(html.Element element, SurfacePaintData paint) { + if (paint.maskFilter != null) { + final bool isStroke = paint.style == ui.PaintingStyle.stroke; + String cssColor = + paint.color == null ? '#000000' : colorToCssString(paint.color)!;// ignore: unnecessary_null_comparison + final double sigma = paint.maskFilter!.webOnlySigma; + if (browserEngine == BrowserEngine.webkit && !isStroke) { + // A bug in webkit leaves artifacts when this element is animated + // with filter: blur, we use boxShadow instead. + element.style.boxShadow = '0px 0px ${sigma * 2.0}px $cssColor'; + } else { + element.style.filter = 'blur(${sigma}px)'; + } + } + } + @override void drawShadow(ui.Path path, ui.Color color, double elevation, bool transparentOccluder) { diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart index 7c7bf34517..81a8f81162 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/dom_canvas.dart @@ -253,9 +253,11 @@ html.Element _pathToSvgElement(SurfacePath path, SurfacePaintData paint, ''); sb.write(' testMain); } +enum PaintMode { + kStrokeAndFill, + kStroke, + kFill, + kStrokeWidthOnly, +} + void testMain() async { - final Rect region = Rect.fromLTWH(8, 8, 600, 800); // Compensate for old scuba tester padding + final Rect region = Rect.fromLTWH(8, 8, 600, 400); // Compensate for old scuba tester padding Future testPath(Path path, String scubaFileName, {Paint paint, double maxDiffRatePercent = null, bool write = false, - bool strokeEnabled = true, bool enableFill = true}) async { - const Rect canvasBounds = Rect.fromLTWH(0, 0, 600, 800); + PaintMode mode = PaintMode.kStrokeAndFill}) async { + const Rect canvasBounds = Rect.fromLTWH(0, 0, 600, 400); final BitmapCanvas bitmapCanvas = BitmapCanvas(canvasBounds, RenderStrategy()); final RecordingCanvas canvas = RecordingCanvas(canvasBounds); + bool enableFill = mode == PaintMode.kStrokeAndFill || + mode == PaintMode.kFill; if (enableFill) { paint ??= Paint() ..color = const Color(0x807F7F7F) @@ -34,7 +43,7 @@ void testMain() async { canvas.drawPath(path, paint); } - if (strokeEnabled) { + if (mode == PaintMode.kStrokeAndFill || mode == PaintMode.kStroke) { paint = Paint() ..strokeWidth = 2 ..color = enableFill ? const Color(0xFFFF0000) : @@ -42,6 +51,12 @@ void testMain() async { ..style = PaintingStyle.stroke; } + if (mode == PaintMode.kStrokeWidthOnly) { + paint = Paint() + ..color = const Color(0xFF4060E0) + ..strokeWidth = 10; + } + canvas.drawPath(path, paint); final html.Element svgElement = pathToSvgElement(path, paint, @@ -95,17 +110,17 @@ void testMain() async { largeArc: false, clockwise: false, distance: 20), ArcSample(const Offset(200, 0), largeArc: true, clockwise: false, distance: 20), - ArcSample(const Offset(0, 150), + ArcSample(const Offset(0, 0), largeArc: false, clockwise: true, distance: 20), - ArcSample(const Offset(200, 150), + ArcSample(const Offset(200, 0), largeArc: true, clockwise: true, distance: 20), - ArcSample(const Offset(0, 300), + ArcSample(const Offset(0, 0), largeArc: false, clockwise: false, distance: -20), - ArcSample(const Offset(200, 300), + ArcSample(const Offset(200, 0), largeArc: true, clockwise: false, distance: -20), - ArcSample(const Offset(0, 450), + ArcSample(const Offset(0, 0), largeArc: false, clockwise: true, distance: -20), - ArcSample(const Offset(200, 450), + ArcSample(const Offset(200, 0), largeArc: true, clockwise: true, distance: -20) ]; int sampleIndex = 0; @@ -157,7 +172,16 @@ void testMain() async { path.lineTo(4, 51.5); path.conicTo(0.5, 51.5, 0.5, 48, w); path.lineTo(0.5, 14); - await testPath(path, 'svg_editoutline', enableFill: false); + await testPath(path, 'svg_editoutline', mode: PaintMode.kStroke); + }); + + /// Regression test for https://github.com/flutter/flutter/issues/74416 + test('render stroke', () async { + final Path path = Path(); + path.moveTo(20, 20); + path.lineTo(200, 200); + await testPath(path, 'svg_stroke_width', + mode: PaintMode.kStrokeWidthOnly); }); } @@ -169,7 +193,8 @@ html.Element pathToSvgElement(Path path, Paint paint, ''); sb.write('