From 601cb5058fd3a3cd721c7cfa3f3257cd0fd1fce9 Mon Sep 17 00:00:00 2001 From: Jackson Gardner Date: Wed, 27 Mar 2024 13:18:57 -0700 Subject: [PATCH] [Skwasm] Correctly handle paragraphs with empty text. (flutter/engine#51695) Instead of just returning, if our paragraph builder has empty text, we still need to generate a set of line breaks for skia to use. Otherwise, it will actually cause subtle memory access errors. --- .../engine/skwasm/skwasm_impl/paragraph.dart | 40 ++++++++++--------- .../lib/web_ui/test/ui/line_metrics_test.dart | 2 +- .../test/ui/paragraph_builder_test.dart | 10 +++++ 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart index 6f6d724121..b83233189b 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart @@ -940,26 +940,28 @@ class SkwasmParagraphBuilder extends SkwasmObjectWrapper im // just create both up front here. final Pointer outSize = scope.allocUint32Array(1); final Pointer utf8Data = paragraphBuilderGetUtf8Text(handle, outSize); - if (utf8Data == nullptr) { - return; - } - // TODO(jacksongardner): We could make a subclass of `List` here to - // avoid this copy. - final List codeUnitList = List.generate( - outSize.value, - (int index) => utf8Data[index] - ); - final String text = utf8.decode(codeUnitList); - final JSString jsText = _utf8Decoder.decode( - // In an ideal world we would just use a subview of wasm memory rather - // than a slice, but the TextDecoder API doesn't work on shared buffer - // sources yet. - // See https://bugs.chromium.org/p/chromium/issues/detail?id=1012656 - createUint8ArrayFromBuffer(skwasmInstance.wasmMemory.buffer).slice( - utf8Data.address.toJS, - (utf8Data.address + outSize.value).toJS - )); + final String text; + final JSString jsText; + if (utf8Data == nullptr) { + text = ''; + jsText = ''.toJS; + } else { + final List codeUnitList = List.generate( + outSize.value, + (int index) => utf8Data[index] + ); + text = utf8.decode(codeUnitList); + jsText = _utf8Decoder.decode( + // In an ideal world we would just use a subview of wasm memory rather + // than a slice, but the TextDecoder API doesn't work on shared buffer + // sources yet. + // See https://bugs.chromium.org/p/chromium/issues/detail?id=1012656 + createUint8ArrayFromBuffer(skwasmInstance.wasmMemory.buffer).slice( + utf8Data.address.toJS, + (utf8Data.address + outSize.value).toJS + )); + } _addGraphemeBreakData(text, jsText); _addWordBreakData(text, jsText); diff --git a/engine/src/flutter/lib/web_ui/test/ui/line_metrics_test.dart b/engine/src/flutter/lib/web_ui/test/ui/line_metrics_test.dart index e287cb6d79..678f5d0920 100644 --- a/engine/src/flutter/lib/web_ui/test/ui/line_metrics_test.dart +++ b/engine/src/flutter/lib/web_ui/test/ui/line_metrics_test.dart @@ -176,5 +176,5 @@ Future testMain() async { // In Roboto, the width should be 11 here. In the test font, it would be square (16 points) expect(metrics!.width, 11); - }, solo: true); + }); } diff --git a/engine/src/flutter/lib/web_ui/test/ui/paragraph_builder_test.dart b/engine/src/flutter/lib/web_ui/test/ui/paragraph_builder_test.dart index 72f733ec69..368b653a5e 100644 --- a/engine/src/flutter/lib/web_ui/test/ui/paragraph_builder_test.dart +++ b/engine/src/flutter/lib/web_ui/test/ui/paragraph_builder_test.dart @@ -39,4 +39,14 @@ Future testMain() async { expect(() => builder.build(), returnsNormally); }); + + test('build and layout a paragraph with an empty addText', () { + final ParagraphBuilder builder = ParagraphBuilder(ParagraphStyle()); + builder.addText(''); + final Paragraph paragraph = builder.build(); + expect( + () => paragraph.layout(const ParagraphConstraints(width: double.infinity)), + returnsNormally, + ); + }); }