Skip to content

Commit

Permalink
Adopt new whitespace handling for HTML. Always try to load images. (#69)
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
kevinaboos authored Apr 17, 2024
1 parent cd4270d commit c2c0555
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
67 changes: 34 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 9 additions & 4 deletions src/shared/html_or_plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) } }
Expand All @@ -59,6 +58,13 @@ live_design! {
font = <MatrixHtmlSpan> { }
span = <MatrixHtmlSpan> { }

a = {
padding: {left: 1.0, right: 1.5},
draw_text: {
text_style: <MESSAGE_TEXT_STYLE> { height_factor: (HTML_TEXT_HEIGHT_FACTOR), line_spacing: (HTML_LINE_SPACING), top_drop: 1.2, },
}
}

body: "[<i> HTML message placeholder</i>]",
}

Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 17 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit c2c0555

Please sign in to comment.