From 9c40609d3740da51ccc9521adbfcb97d0d94d86b Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Fri, 30 Jul 2021 09:38:21 -0700 Subject: [PATCH] [web] Fixing clipping not being applied when height is zero (flutter/engine#27771) --- .../flutter/lib/web_ui/dev/goldens_lock.yaml | 2 +- .../lib/src/engine/html/recording_canvas.dart | 8 +-- .../web_ui/test/html/clip_op_golden_test.dart | 57 +++++++++++++++++-- 3 files changed, 57 insertions(+), 10 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 6fbe3e9874..8395991f96 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: f1baad46498ce1a754a15d02249827bb042b446b +revision: 8517dd15b8eb62e07ecde82170f8a4016566ea49 diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart index f0c731305d..b581557496 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/html/recording_canvas.dart @@ -1832,19 +1832,19 @@ class _PaintBounds { } if (_clipRectInitialized) { - if (transformedPointLeft > _currentClipRight) { + if (transformedPointLeft >= _currentClipRight) { command.isClippedOut = true; return; } - if (transformedPointRight < _currentClipLeft) { + if (transformedPointRight <= _currentClipLeft) { command.isClippedOut = true; return; } - if (transformedPointTop > _currentClipBottom) { + if (transformedPointTop >= _currentClipBottom) { command.isClippedOut = true; return; } - if (transformedPointBottom < _currentClipTop) { + if (transformedPointBottom <= _currentClipTop) { command.isClippedOut = true; return; } diff --git a/engine/src/flutter/lib/web_ui/test/html/clip_op_golden_test.dart b/engine/src/flutter/lib/web_ui/test/html/clip_op_golden_test.dart index e8196dcd8c..2d2b34535d 100644 --- a/engine/src/flutter/lib/web_ui/test/html/clip_op_golden_test.dart +++ b/engine/src/flutter/lib/web_ui/test/html/clip_op_golden_test.dart @@ -6,6 +6,8 @@ import 'package:test/bootstrap/browser.dart'; import 'package:test/test.dart'; import 'package:ui/src/engine.dart'; import 'package:ui/ui.dart'; + +import 'paragraph/helper.dart'; import 'screenshot.dart'; void main() { @@ -13,22 +15,21 @@ void main() { } Future testMain() async { - setUp(() async { - debugEmulateFlutterTesterEnvironment = true; - }); + setUpStableTestFonts(); /// Regression test for https://github.com/flutter/flutter/issues/64734. test('Clips using difference', () async { + const Offset shift = Offset(8, 8); const Rect region = Rect.fromLTRB(0, 0, 400, 300); final RecordingCanvas canvas = RecordingCanvas(region); - const Rect titleRect = Rect.fromLTWH(20, 0, 50, 20); + final Rect titleRect = const Rect.fromLTWH(20, 0, 50, 20).shift(shift); final SurfacePaint paint = SurfacePaint() ..style = PaintingStyle.stroke ..color = const Color(0xff000000) ..strokeWidth = 1; canvas.save(); try { - final Rect borderRect = Rect.fromLTRB(0, 10, region.width, region.height); + final Rect borderRect = Rect.fromLTRB(0, 10, region.width, region.height).shift(shift); canvas.clipRect(titleRect, ClipOp.difference); canvas.drawRect(borderRect, paint); } finally { @@ -38,4 +39,50 @@ Future testMain() async { await canvasScreenshot(canvas, 'clip_op_difference', region: const Rect.fromLTRB(0, 0, 420, 360)); }); + + /// Regression test for https://github.com/flutter/flutter/issues/86345 + test('Clips with zero width or height', () async { + const Rect region = Rect.fromLTRB(0, 0, 400, 300); + final RecordingCanvas canvas = RecordingCanvas(region); + + final SurfacePaint paint = SurfacePaint() + ..style = PaintingStyle.fill + ..color = const Color(0xff00ff00); + final SurfacePaint borderPaint = SurfacePaint() + ..style = PaintingStyle.stroke + ..color = const Color(0xffff0000) + ..strokeWidth = 1; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + final double x = 10 + i * 70; + final double y = 10 + j * 70; + canvas.save(); + // Clip. + canvas.clipRect( + Rect.fromLTWH(x, y, i * 25, j * 25), + ClipOp.intersect, + ); + // Draw the blue (clipped) rect. + canvas.drawRect( + Rect.fromLTWH(x, y, 50, 50), + paint, + ); + final Paragraph p = plain( + EngineParagraphStyle(fontFamily: 'Roboto', fontSize: 34), + '23', + textStyle: EngineTextStyle.only(color: const Color(0xff0000ff)), + ); + p.layout(const ParagraphConstraints(width: double.infinity)); + canvas.drawParagraph(p, Offset(x, y)); + canvas.restore(); + // Draw the red border. + canvas.drawRect( + Rect.fromLTWH(x, y, 50, 50), + borderPaint, + ); + } + } + await canvasScreenshot(canvas, 'clip_zero_width_height', region: region); + }); }