diff --git a/src/ng/svg.rs b/src/ng/svg.rs index 56f0ae4..92b5a08 100644 --- a/src/ng/svg.rs +++ b/src/ng/svg.rs @@ -2,6 +2,16 @@ use leptos::*; use crate::ng::{use_frame, Align, Frame, VAlign}; +struct SvgProperties { + width: i32, + height: i32, + align: Align, + valign: VAlign, + scale: f32, + flip_x: bool, + flip_y: bool, +} + /// SVG widget. #[component] pub fn Svg( @@ -15,7 +25,16 @@ pub fn Svg( children: Children, ) -> impl IntoView { let f = use_frame(); - let transform = calc_transform(&f, width, height, align, valign, scale, flip_x, flip_y); + let props = SvgProperties { + width, + height, + align, + valign, + scale, + flip_x, + flip_y, + }; + let transform = calc_transform(&f, &props); view! { {children()} } } @@ -33,38 +52,38 @@ pub fn SvgFile( src: &'static str, ) -> impl IntoView { let f = use_frame(); - let transform = calc_transform(&f, width, height, align, valign, scale, flip_x, flip_y); + let props = SvgProperties { + width, + height, + align, + valign, + scale, + flip_x, + flip_y, + }; + let transform = calc_transform(&f, &props); view! { } } -fn calc_transform( - f: &Frame, - width: i32, - height: i32, - align: Align, - valign: VAlign, - scale: f32, - flip_x: bool, - flip_y: bool, -) -> String { - let scale = if matches!(align, Align::Fill) || matches!(valign, VAlign::Fill) { - let sx = f.width as f32 / width as f32; - let sy = f.height as f32 / height as f32; +fn calc_transform(f: &Frame, props: &SvgProperties) -> String { + let scale = if matches!(props.align, Align::Fill) || matches!(props.valign, VAlign::Fill) { + let sx = f.width as f32 / props.width as f32; + let sy = f.height as f32 / props.height as f32; sx.min(sy) } else { - scale + props.scale }; - let width = (scale * width as f32).round() as i32; - let height = (scale * height as f32).round() as i32; + let width = (scale * props.width as f32).round() as i32; + let height = (scale * props.height as f32).round() as i32; - let mut x = match align { + let mut x = match props.align { Align::Left => f.x, Align::Center | Align::Fill => f.x + (f.width - width) / 2, Align::Right => f.x + f.width - width, }; - let mut y = match valign { + let mut y = match props.valign { VAlign::Top => f.y, VAlign::Middle | VAlign::Fill => f.y + (f.height - height) / 2, VAlign::Bottom => f.y + f.height - height, @@ -73,11 +92,11 @@ fn calc_transform( let mut sx = scale; let mut sy = scale; - if flip_x { + if props.flip_x { sx = -sx; x += width; } - if flip_y { + if props.flip_y { sy = -sy; y += height; } diff --git a/src/ng/text.rs b/src/ng/text.rs index 4b1cf43..f995b36 100644 --- a/src/ng/text.rs +++ b/src/ng/text.rs @@ -170,7 +170,12 @@ fn text_width(text: &str, canvas: &CanvasRenderingContext2d) -> i32 { canvas.measure_text(text).unwrap().width() as i32 } -fn wrap(children: &[View], canvas: &CanvasRenderingContext2d, props: &TextProperties, frame: &Frame) -> Output { +fn wrap( + children: &[View], + canvas: &CanvasRenderingContext2d, + props: &TextProperties, + frame: &Frame, +) -> Output { let children = children.iter().map(|item| { if let View::Text(text) = item { text.content.clone()