From c2c055567eaec33b4c92f827fde8a8653874658e Mon Sep 17 00:00:00 2001 From: Kevin Boos <1139460+kevinaboos@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:33:54 -0700 Subject: [PATCH] Adopt new whitespace handling for HTML. Always try to load images. (#69) `imghdr` doesn't always correctly determine whether an image data stream is PNG or JPG, so we just try both if we're not sure. --- Cargo.lock | 67 +++++++++++++++++---------------- Cargo.toml | 3 +- src/shared/html_or_plaintext.rs | 13 +++++-- src/utils.rs | 22 ++++++++--- 4 files changed, 62 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5dcdb67..03b2f0b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1651,7 +1651,7 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "jni-sys" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "js-sys" @@ -1833,7 +1833,7 @@ dependencies = [ [[package]] name = "makepad-derive-live" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-id", "makepad-micro-proc-macro", @@ -1842,7 +1842,7 @@ dependencies = [ [[package]] name = "makepad-derive-wasm-bridge" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-micro-proc-macro", ] @@ -1850,7 +1850,7 @@ dependencies = [ [[package]] name = "makepad-derive-widget" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-id", "makepad-micro-proc-macro", @@ -1859,7 +1859,7 @@ dependencies = [ [[package]] name = "makepad-draw" version = "0.6.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "ab_glyph_rasterizer", "makepad-html", @@ -1873,17 +1873,17 @@ dependencies = [ [[package]] name = "makepad-futures" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "makepad-futures-legacy" version = "0.7.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "makepad-html" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-id", ] @@ -1891,12 +1891,12 @@ dependencies = [ [[package]] name = "makepad-http" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "makepad-live-compiler" version = "0.5.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-derive-live", "makepad-live-tokenizer", @@ -1906,7 +1906,7 @@ dependencies = [ [[package]] name = "makepad-live-id" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-id-macros", ] @@ -1914,7 +1914,7 @@ dependencies = [ [[package]] name = "makepad-live-id-macros" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-micro-proc-macro", ] @@ -1922,7 +1922,7 @@ dependencies = [ [[package]] name = "makepad-live-tokenizer" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-id", "makepad-math", @@ -1932,7 +1932,7 @@ dependencies = [ [[package]] name = "makepad-markdown" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-id", ] @@ -1940,17 +1940,17 @@ dependencies = [ [[package]] name = "makepad-math" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "makepad-micro-proc-macro" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "makepad-micro-serde" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-micro-serde-derive", ] @@ -1958,7 +1958,7 @@ dependencies = [ [[package]] name = "makepad-micro-serde-derive" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-micro-proc-macro", ] @@ -1966,12 +1966,12 @@ dependencies = [ [[package]] name = "makepad-objc-sys" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" [[package]] name = "makepad-platform" version = "0.6.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "jni-sys", "makepad-futures", @@ -1981,13 +1981,14 @@ dependencies = [ "makepad-shader-compiler", "makepad-wasm-bridge", "makepad-windows", + "smallvec", "windows-targets", ] [[package]] name = "makepad-shader-compiler" version = "0.5.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-live-compiler", ] @@ -1995,7 +1996,7 @@ dependencies = [ [[package]] name = "makepad-vector" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "ttf-parser", ] @@ -2003,7 +2004,7 @@ dependencies = [ [[package]] name = "makepad-wasm-bridge" version = "0.4.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-derive-wasm-bridge", "makepad-live-id", @@ -2012,7 +2013,7 @@ dependencies = [ [[package]] name = "makepad-widgets" version = "0.6.0" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-derive-widget", "makepad-draw", @@ -2025,16 +2026,16 @@ dependencies = [ [[package]] name = "makepad-windows" version = "0.51.1" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ - "windows-core 0.51.1 (git+https://github.com/makepad/makepad?branch=rik)", + "windows-core 0.51.1 (git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace)", "windows-targets", ] [[package]] name = "makepad-zune-core" version = "0.2.14" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "bitflags 2.4.1", ] @@ -2042,7 +2043,7 @@ dependencies = [ [[package]] name = "makepad-zune-inflate" version = "0.2.54" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "simd-adler32", ] @@ -2050,7 +2051,7 @@ dependencies = [ [[package]] name = "makepad-zune-jpeg" version = "0.3.17" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-zune-core", ] @@ -2058,7 +2059,7 @@ dependencies = [ [[package]] name = "makepad-zune-png" version = "0.2.1" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "makepad-zune-core", "makepad-zune-inflate", @@ -3927,9 +3928,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.11.1" +version = "1.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "socket2" @@ -4777,7 +4778,7 @@ dependencies = [ [[package]] name = "windows-core" version = "0.51.1" -source = "git+https://github.com/makepad/makepad?branch=rik#1552b853182c940eac19b76026537658bbdd4d5b" +source = "git+https://github.com/kevinaboos/makepad?branch=fix_panic_improve_html_whitespace#40a470859b50cfae552e5bd8cda92e19c776d3d2" dependencies = [ "windows-targets", ] diff --git a/Cargo.toml b/Cargo.toml index bb3f896a..041e02a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,8 @@ metadata.makepad-auto-version = "zqpv-Yj-K7WNVK2I8h5Okhho46Q=" [dependencies] -makepad-widgets = { git = "https://github.com/makepad/makepad", branch = "rik" } +# makepad-widgets = { git = "https://github.com/makepad/makepad", branch = "rik" } +makepad-widgets = { git = "https://github.com/kevinaboos/makepad", branch = "fix_panic_improve_html_whitespace" } anyhow = "1.0" chrono = "0.4" diff --git a/src/shared/html_or_plaintext.rs b/src/shared/html_or_plaintext.rs index aa27e130..2c1d1e46 100644 --- a/src/shared/html_or_plaintext.rs +++ b/src/shared/html_or_plaintext.rs @@ -37,7 +37,6 @@ live_design! { padding: 0.0, line_spacing: (HTML_LINE_SPACING), width: Fill, height: Fit, // see comment in `HtmlOrPlaintext` - keep_whitespace: true, font_size: (MESSAGE_FONT_SIZE), draw_normal: { color: (MESSAGE_TEXT_COLOR), text_style: { height_factor: (HTML_TEXT_HEIGHT_FACTOR), line_spacing: (HTML_LINE_SPACING) } } draw_italic: { color: (MESSAGE_TEXT_COLOR), text_style: { height_factor: (HTML_TEXT_HEIGHT_FACTOR), line_spacing: (HTML_LINE_SPACING) } } @@ -59,6 +58,13 @@ live_design! { font = { } span = { } + a = { + padding: {left: 1.0, right: 1.5}, + draw_text: { + text_style: { height_factor: (HTML_TEXT_HEIGHT_FACTOR), line_spacing: (HTML_LINE_SPACING), top_drop: 1.2, }, + } + } + body: "[ HTML message placeholder]", } @@ -155,12 +161,11 @@ impl LiveHook for MatrixHtmlSpan { // Set the Label's foreground text color and background color if let Some(fg_color) = self.fg_color { self.ll.apply_over(cx, live!{ draw_text: { color: (fg_color) } }); - }; + } if let Some(_bg_color) = self.bg_color { log!("TODO: Html span/font background color is not yet implemented.") // self.apply_over(cx, live!{ draw_bg: { color: (bg_color) } }); - }; - + } // TODO: need to handle labelĀ events to handle the spoiler, so we can toggle it upon click. } } else { diff --git a/src/utils.rs b/src/utils.rs index 171c53b1..4b3623fe 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -24,16 +24,28 @@ impl ImageFormat { /// /// Returns an error if either load fails or if the image format is unknown. pub fn load_png_or_jpg(img: &ImageRef, cx: &mut Cx, data: &[u8]) -> Result<(), ImageError> { - let res = match imghdr::from_bytes(data) { + + fn attempt_both(img: &ImageRef, cx: &mut Cx, data: &[u8]) -> Result<(), ImageError> { + img.load_png_from_data(cx, data) + .or_else(|_| img.load_jpg_from_data(cx, data)) + } + + let res = match imghdr::from_bytes(data) { Some(imghdr::Type::Png) => img.load_png_from_data(cx, data), Some(imghdr::Type::Jpeg) => img.load_jpg_from_data(cx, data), Some(unsupported) => { - error!("load_png_or_jpg(): The {unsupported:?} image format is unsupported"); - Err(ImageError::UnsupportedFormat) + // Attempt to load it as a PNG or JPEG anyway, since imghdr isn't perfect. + attempt_both(img, cx, data).map_err(|_| { + error!("load_png_or_jpg(): The {unsupported:?} image format is unsupported"); + ImageError::UnsupportedFormat + }) } None => { - error!("load_png_or_jpg(): Unknown image format"); - Err(ImageError::UnsupportedFormat) + // Attempt to load it as a PNG or JPEG anyway, since imghdr isn't perfect. + attempt_both(img, cx, data).map_err(|_| { + error!("load_png_or_jpg(): Unknown image format"); + ImageError::UnsupportedFormat + }) } }; if let Err(err) = res.as_ref() {