use std::sync::Arc;
use crate::types::Rect;
#[derive(Clone, PartialEq)]
pub enum ImageRef {
Id { id: String, size: Option<(u32, u32)> },
Rgba { data: Arc<[u8]>, width: u32, height: u32 },
}
impl ImageRef {
pub fn id(id: impl Into<String>) -> Self {
ImageRef::Id { id: id.into(), size: None }
}
pub fn id_sized(id: impl Into<String>, width: u32, height: u32) -> Self {
ImageRef::Id { id: id.into(), size: Some((width, height)) }
}
pub fn rgba(data: impl Into<Arc<[u8]>>, width: u32, height: u32) -> Option<Self> {
let data = data.into();
(data.len() == width as usize * height as usize * 4 && width > 0 && height > 0).then_some(ImageRef::Rgba { data, width, height })
}
pub fn size(&self) -> Option<(u32, u32)> {
match self {
ImageRef::Id { size, .. } => *size,
ImageRef::Rgba { width, height, .. } => Some((*width, *height)),
}
}
}
impl std::fmt::Debug for ImageRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ImageRef::Id { id, size } => f.debug_struct("Id").field("id", id).field("size", size).finish(),
ImageRef::Rgba { width, height, .. } => write!(f, "Rgba({width}x{height})"),
}
}
}
pub fn object_fit_rect(content: Rect, size: Option<(u32, u32)>, fit: Option<&str>) -> Rect {
let Some((iw, ih)) = size.filter(|(w, h)| *w > 0 && *h > 0) else { return content };
let (iw, ih) = (iw as f64, ih as f64);
let scale = match fit.map(str::trim) {
Some("contain") => (content.width / iw).min(content.height / ih),
Some("cover") => (content.width / iw).max(content.height / ih),
Some("none") => 1.0,
Some("scale-down") => (content.width / iw).min(content.height / ih).min(1.0),
_ => return content,
};
let (w, h) = (iw * scale, ih * scale);
Rect::new(content.x + (content.width - w) / 2.0, content.y + (content.height - h) / 2.0, w, h)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn object_fit_places_like_css() {
let c = Rect::new(10.0, 20.0, 200.0, 100.0);
let s = Some((100, 100));
assert_eq!(object_fit_rect(c, s, None), c);
assert_eq!(object_fit_rect(c, s, Some("fill")), c);
assert_eq!(object_fit_rect(c, s, Some("contain")), Rect::new(60.0, 20.0, 100.0, 100.0));
assert_eq!(object_fit_rect(c, s, Some("cover")), Rect::new(10.0, -30.0, 200.0, 200.0));
assert_eq!(object_fit_rect(c, Some((40, 20)), Some("none")), Rect::new(90.0, 60.0, 40.0, 20.0));
assert_eq!(object_fit_rect(c, Some((40, 20)), Some("scale-down")), Rect::new(90.0, 60.0, 40.0, 20.0));
assert_eq!(object_fit_rect(c, Some((400, 200)), Some("scale-down")), c);
assert_eq!(object_fit_rect(c, None, Some("cover")), c, "no size: fill");
}
#[test]
fn rgba_checks_its_length() {
assert!(ImageRef::rgba(vec![0u8; 16], 2, 2).is_some());
assert!(ImageRef::rgba(vec![0u8; 15], 2, 2).is_none());
assert!(ImageRef::rgba(Vec::<u8>::new(), 0, 0).is_none());
assert_eq!(format!("{:?}", ImageRef::rgba(vec![0u8; 16], 2, 2).unwrap()), "Rgba(2x2)");
}
}