1use mln_style::image::{ImageContent, ImageStretches, Impl, TextFit};
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, mln_wire::Wire)]
7pub struct RectU16 {
8 pub x: u16,
9 pub y: u16,
10 pub w: u16,
11 pub h: u16,
12}
13
14#[derive(Clone, Debug, PartialEq, mln_wire::Wire)]
16pub struct ImagePosition {
17 pub padded_rect: RectU16,
18 pub pixel_ratio: f32,
19 pub sdf: bool,
20 pub stretch_x: ImageStretches,
21 pub stretch_y: ImageStretches,
22 pub content: Option<ImageContent>,
23 pub(crate) text_fit_width: Option<TextFit>,
24 pub(crate) text_fit_height: Option<TextFit>,
25}
26
27impl ImagePosition {
28 pub const PADDING: u16 = 1;
30
31 #[must_use]
33 pub fn new(rect: RectU16, image: &Impl) -> Self {
34 ImagePosition {
35 padded_rect: rect,
36 pixel_ratio: image.pixel_ratio,
37 sdf: image.sdf,
38 stretch_x: image.stretch_x.clone(),
39 stretch_y: image.stretch_y.clone(),
40 content: image.content,
41 text_fit_width: image.text_fit_width,
42 text_fit_height: image.text_fit_height,
43 }
44 }
45
46 #[must_use]
49 pub fn tl(&self) -> [u16; 2] {
50 [
51 (i32::from(self.padded_rect.x) + i32::from(Self::PADDING)) as u16,
52 (i32::from(self.padded_rect.y) + i32::from(Self::PADDING)) as u16,
53 ]
54 }
55
56 #[must_use]
58 pub fn br(&self) -> [u16; 2] {
59 [
60 (i32::from(self.padded_rect.x) + i32::from(self.padded_rect.w) - i32::from(Self::PADDING)) as u16,
61 (i32::from(self.padded_rect.y) + i32::from(self.padded_rect.h) - i32::from(Self::PADDING)) as u16,
62 ]
63 }
64
65 #[must_use]
67 pub fn tlbr(&self) -> [u16; 4] {
68 let tl = self.tl();
69 let br = self.br();
70 [tl[0], tl[1], br[0], br[1]]
71 }
72
73 #[must_use]
76 pub fn display_size(&self) -> [f32; 2] {
77 [
78 (i32::from(self.padded_rect.w) - i32::from(Self::PADDING) * 2) as f32 / self.pixel_ratio,
79 (i32::from(self.padded_rect.h) - i32::from(Self::PADDING) * 2) as f32 / self.pixel_ratio,
80 ]
81 }
82}
83
84pub type ImagePositions = mln_style::hash::HashMap<String, ImagePosition>;