From eb5b234609667e6e8e2ef25d7b486f2124064a7c Mon Sep 17 00:00:00 2001 From: ferhatb Date: Wed, 28 Oct 2020 18:14:57 -0700 Subject: [PATCH 1/2] Assign default natural width/height for svgs that report 0,0 on firefox and ie11 --- lib/web_ui/lib/src/engine/html_image_codec.dart | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/lib/src/engine/html_image_codec.dart b/lib/web_ui/lib/src/engine/html_image_codec.dart index db1fa461852ea..b46fe7a66ac3e 100644 --- a/lib/web_ui/lib/src/engine/html_image_codec.dart +++ b/lib/web_ui/lib/src/engine/html_image_codec.dart @@ -39,10 +39,19 @@ class HtmlCodec implements ui.Codec { js_util.setProperty(imgElement, 'decoding', 'async'); imgElement.decode().then((dynamic _) { chunkCallback?.call(100, 100); + int naturalWidth = imgElement.naturalWidth; + int naturalHeight = imgElement.naturalHeight; + // Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=700533. + if (naturalWidth == 0 && naturalHeight == 0 && ( + browserEngine == BrowserEngine.firefox || + browserEngine == BrowserEngine.ie11)) { + naturalWidth = 300; + naturalHeight = 300; + } final HtmlImage image = HtmlImage( imgElement, - imgElement.naturalWidth, - imgElement.naturalHeight, + naturalWidth, + naturalHeight, ); completer.complete(SingleFrameInfo(image)); }).catchError((dynamic e) { From 7c966b0a51ba09c80cad49008df0262c4ccf0c3c Mon Sep 17 00:00:00 2001 From: ferhatb Date: Wed, 28 Oct 2020 18:24:05 -0700 Subject: [PATCH 2/2] Add test --- .../lib/src/engine/html_image_codec.dart | 5 +++-- .../engine/image/html_image_codec_test.dart | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/lib/src/engine/html_image_codec.dart b/lib/web_ui/lib/src/engine/html_image_codec.dart index b46fe7a66ac3e..c7a5e7e911b3d 100644 --- a/lib/web_ui/lib/src/engine/html_image_codec.dart +++ b/lib/web_ui/lib/src/engine/html_image_codec.dart @@ -45,8 +45,9 @@ class HtmlCodec implements ui.Codec { if (naturalWidth == 0 && naturalHeight == 0 && ( browserEngine == BrowserEngine.firefox || browserEngine == BrowserEngine.ie11)) { - naturalWidth = 300; - naturalHeight = 300; + const int kDefaultImageSizeFallback = 300; + naturalWidth = kDefaultImageSizeFallback; + naturalHeight = kDefaultImageSizeFallback; } final HtmlImage image = HtmlImage( imgElement, diff --git a/lib/web_ui/test/engine/image/html_image_codec_test.dart b/lib/web_ui/test/engine/image/html_image_codec_test.dart index 331462571a573..8723bb00d7552 100644 --- a/lib/web_ui/test/engine/image/html_image_codec_test.dart +++ b/lib/web_ui/test/engine/image/html_image_codec_test.dart @@ -80,6 +80,24 @@ void testMain() async { await codec.getNextFrame(); expect(buffer.toString(), '0/100,100/100,'); }); + + /// Regression test for Firefox/ie11 + /// https://github.com/flutter/flutter/issues/66412 + test('Returns nonzero natural width/height', () async { + final HtmlCodec codec = HtmlCodec( + 'data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHZpZXdCb3g9I' + 'jAgMCAyNCAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48dG' + 'l0bGU+QWJzdHJhY3QgaWNvbjwvdGl0bGU+PHBhdGggZD0iTTEyIDBjOS42MDEgMCAx' + 'MiAyLjM5OSAxMiAxMiAwIDkuNjAxLTIuMzk5IDEyLTEyIDEyLTkuNjAxIDAtMTItMi' + '4zOTktMTItMTJDMCAyLjM5OSAyLjM5OSAwIDEyIDB6bS0xLjk2OSAxOC41NjRjMi41' + 'MjQuMDAzIDQuNjA0LTIuMDcgNC42MDktNC41OTUgMC0yLjUyMS0yLjA3NC00LjU5NS' + '00LjU5NS00LjU5NVM1LjQ1IDExLjQ0OSA1LjQ1IDEzLjk2OWMwIDIuNTE2IDIuMDY1' + 'IDQuNTg4IDQuNTgxIDQuNTk1em04LjM0NC0uMTg5VjUuNjI1SDUuNjI1djIuMjQ3aD' + 'EwLjQ5OHYxMC41MDNoMi4yNTJ6bS04LjM0NC02Ljc0OGEyLjM0MyAyLjM0MyAwIDEx' + 'LS4wMDIgNC42ODYgMi4zNDMgMi4zNDMgMCAwMS4wMDItNC42ODZ6Ii8+PC9zdmc+'); + final ui.FrameInfo frameInfo = await codec.getNextFrame(); + expect(frameInfo.image.width, isNot(0)); + }); }); group('ImageCodecUrl', () {