diff --git a/lib/src/blocked_text_image.dart b/lib/src/blocked_text_image.dart index a2e3960..597ad6b 100644 --- a/lib/src/blocked_text_image.dart +++ b/lib/src/blocked_text_image.dart @@ -72,8 +72,17 @@ class BlockedTextCanvasAdapter implements Canvas { /// Draws a rectangle on the canvas where the [paragraph] /// would otherwise be rendered @override - void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) => parent - .drawRect(offset & Size(paragraph.width, paragraph.height), Paint()); + void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) => + parent.drawRect( + offset & + Size( + paragraph.width.isFinite + ? paragraph.width + : paragraph.longestLine, + paragraph.height, + ), + Paint(), + ); @override void clipPath(ui.Path path, {bool doAntiAlias = true}) => diff --git a/test/smoke_tests/goldens/ci/render_object_text_smoke_test.png b/test/smoke_tests/goldens/ci/render_object_text_smoke_test.png new file mode 100644 index 0000000..c0b0efe Binary files /dev/null and b/test/smoke_tests/goldens/ci/render_object_text_smoke_test.png differ diff --git a/test/smoke_tests/render_object_smoke_test.dart b/test/smoke_tests/render_object_smoke_test.dart new file mode 100644 index 0000000..983fdc3 --- /dev/null +++ b/test/smoke_tests/render_object_smoke_test.dart @@ -0,0 +1,62 @@ +import 'package:alchemist/alchemist.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + group('smoke test', () { + goldenTest( + 'succeeds for custom render object drawing text', + fileName: 'render_object_text_smoke_test', + builder: () => const SizedBox( + height: 400, + width: 400, + child: _CustomExampleRenderObject(), + ), + ); + }); +} + +class _CustomExampleRenderObject extends LeafRenderObjectWidget { + const _CustomExampleRenderObject({Key? key}) : super(key: key); + + @override + _CustomExampleRenderBox createRenderObject(BuildContext context) { + return _CustomExampleRenderBox(); + } +} + +class _CustomExampleRenderBox extends RenderBox { + @override + void paint(PaintingContext context, Offset offset) { + final textPainter = TextPainter( + text: const TextSpan( + text: 'text', + style: TextStyle( + fontFamily: 'Roboto', + color: Colors.black, + fontSize: 24, + ), + ), + textDirection: TextDirection.ltr, + )..layout(); + textPainter.paint( + context.canvas, + const Offset(200, 200) - textPainter.size.center(Offset.zero), + ); + } + + @override + void performLayout() { + size = computeDryLayout(constraints); + } + + @override + Size computeDryLayout(BoxConstraints constraints) { + return constraints.constrain( + Size( + 200, + constraints.maxHeight, + ), + ); + } +} diff --git a/test/src/blocked_text_canvas_adapter_test.dart b/test/src/blocked_text_canvas_adapter_test.dart index 023040b..3a67604 100644 --- a/test/src/blocked_text_canvas_adapter_test.dart +++ b/test/src/blocked_text_canvas_adapter_test.dart @@ -18,6 +18,10 @@ void main() { late ui.Canvas parent; late BlockedTextCanvasAdapter subject; + setUpAll(() { + registerFallbackValue(ui.Paint()); + }); + setUp(() { parent = MockCanvas(); subject = BlockedTextCanvasAdapter(parent); @@ -31,7 +35,22 @@ void main() { subject.drawParagraph(paragraph, offset); verify( () => parent.drawRect( - offset & ui.Size(paragraph.width, paragraph.height), + offset & const ui.Size(200, 400), + any(that: isA()), + ), + ).called(1); + }); + + test('drawParagraph draws a rectangle for infinite width', () { + const offset = ui.Offset(20, 40); + final paragraph = MockParagraph(); + when(() => paragraph.width).thenReturn(double.infinity); + when(() => paragraph.longestLine).thenReturn(400); + when(() => paragraph.height).thenReturn(400); + subject.drawParagraph(paragraph, offset); + verify( + () => parent.drawRect( + offset & const ui.Size(400, 400), any(that: isA()), ), ).called(1);