diff --git a/render-wasm/src/render/fills.rs b/render-wasm/src/render/fills.rs index ad508f8999..a6692b8c9f 100644 --- a/render-wasm/src/render/fills.rs +++ b/render-wasm/src/render/fills.rs @@ -23,25 +23,29 @@ fn draw_image_fill( let width = size.0 as f32; let height = size.1 as f32; - let image_aspect_ratio = width / height; // Container size let container_width = container.width(); let container_height = container.height(); - let container_aspect_ratio = container_width / container_height; - // Calculate scale to ensure the image covers the container - let scale = if image_aspect_ratio > container_aspect_ratio { - // Image is wider, scale based on height to cover container - container_height / height - } else { - // Image is taller, scale based on width to cover container - container_width / width - }; + let mut scaled_width = container_width; + let mut scaled_height = container_height; - // Scaled size of the image - let scaled_width = width * scale; - let scaled_height = height * scale; + if image_fill.keep_aspect_ratio() { + // Calculate scale to ensure the image covers the container + let image_aspect_ratio = width / height; + let container_aspect_ratio = container_width / container_height; + let scale = if image_aspect_ratio > container_aspect_ratio { + // Image is wider, scale based on height to cover container + container_height / height + } else { + // Image is taller, scale based on width to cover container + container_width / width + }; + // Scaled size of the image + scaled_width = width * scale; + scaled_height = height * scale; + } let dest_rect = MathRect::from_xywh( container.left - (scaled_width - container_width) / 2.0, diff --git a/render-wasm/src/render/strokes.rs b/render-wasm/src/render/strokes.rs index 36748a8dff..febbf594b9 100644 --- a/render-wasm/src/render/strokes.rs +++ b/render-wasm/src/render/strokes.rs @@ -326,23 +326,33 @@ fn draw_triangle_cap( canvas.draw_path(&path, paint); } -fn calculate_scaled_rect(size: (i32, i32), container: &Rect, delta: f32) -> Rect { +fn calculate_scaled_rect( + size: (i32, i32), + container: &Rect, + delta: f32, + keep_aspect_ratio: bool, +) -> Rect { let (width, height) = (size.0 as f32, size.1 as f32); - let image_aspect_ratio = width / height; // Container size let container_width = container.width(); let container_height = container.height(); - let container_aspect_ratio = container_width / container_height; - let scale = if image_aspect_ratio > container_aspect_ratio { - container_height / height - } else { - container_width / width - }; + let mut scaled_width = container_width; + let mut scaled_height = container_height; - let scaled_width = width * scale; - let scaled_height = height * scale; + if keep_aspect_ratio { + let image_aspect_ratio = width / height; + let container_aspect_ratio = container_width / container_height; + let scale = if image_aspect_ratio > container_aspect_ratio { + container_height / height + } else { + container_width / width + }; + + scaled_width = width * scale; + scaled_height = height * scale; + } Rect::from_xywh( container.left - delta - (scaled_width - container_width) / 2.0, @@ -457,7 +467,12 @@ fn draw_image_stroke_in_container( image_paint.set_anti_alias(antialias); // Compute scaled rect and clip to it - let dest_rect = calculate_scaled_rect(size, container, stroke.delta()); + let dest_rect = calculate_scaled_rect( + size, + container, + stroke.delta(), + image_fill.keep_aspect_ratio(), + ); canvas.clip_rect(dest_rect, skia::ClipOp::Intersect, antialias); canvas.draw_image_rect_with_sampling_options( image.unwrap(), diff --git a/render-wasm/src/shapes/fills.rs b/render-wasm/src/shapes/fills.rs index dd73f81ec8..d18d9e9df8 100644 --- a/render-wasm/src/shapes/fills.rs +++ b/render-wasm/src/shapes/fills.rs @@ -101,15 +101,17 @@ pub struct ImageFill { opacity: u8, width: i32, height: i32, + keep_aspect_ratio: bool, } impl ImageFill { - pub fn new(id: Uuid, opacity: u8, width: i32, height: i32) -> Self { + pub fn new(id: Uuid, opacity: u8, width: i32, height: i32, keep_aspect_ratio: bool) -> Self { Self { id, opacity, width, height, + keep_aspect_ratio, } } @@ -124,6 +126,10 @@ impl ImageFill { pub fn opacity(&self) -> u8 { self.opacity } + + pub fn keep_aspect_ratio(&self) -> bool { + self.keep_aspect_ratio + } } #[derive(Debug, Clone, PartialEq, Copy)] diff --git a/render-wasm/src/wasm/fills/image.rs b/render-wasm/src/wasm/fills/image.rs index b669ad2b32..c35c5426d9 100644 --- a/render-wasm/src/wasm/fills/image.rs +++ b/render-wasm/src/wasm/fills/image.rs @@ -18,6 +18,7 @@ impl From for ImageFill { let id = uuid_from_u32_quartet(value.a, value.b, value.c, value.d); let opacity = (value.opacity * 255.).floor() as u8; - Self::new(id, opacity, value.width, value.height) + // TODO: read keep_aspect_ratio from RawImageFillData when implemented + Self::new(id, opacity, value.width, value.height, true) } }