1use crate::bitmap::PremultipliedImage;
5use crate::error::Error;
6use crate::size::Size;
7
8pub type ImageStretch = (f32, f32);
9pub type ImageStretches = Vec<ImageStretch>;
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, mln_wire::Wire)]
12pub enum TextFit {
13 StretchOrShrink,
14 StretchOnly,
15 Proportional,
16}
17
18#[derive(Clone, Copy, Debug, Default, PartialEq, mln_wire::Wire)]
19pub struct ImageContent {
20 pub left: f32,
21 pub top: f32,
22 pub right: f32,
23 pub bottom: f32,
24}
25
26#[derive(Clone, Debug, mln_wire::Wire)]
28pub struct StyleImage {
29 pub id: String,
30 pub image: PremultipliedImage,
31 pub pixel_ratio: f32,
33 pub sdf: bool,
36 pub stretch_x: ImageStretches,
38 pub stretch_y: ImageStretches,
39 pub content: Option<ImageContent>,
41 pub text_fit_width: Option<TextFit>,
44 pub text_fit_height: Option<TextFit>,
47 pub keep_unused: bool,
50}
51
52fn validate_stretch(stretches: &ImageStretches, size: f32) -> bool {
53 if stretches.is_empty() {
54 return true;
55 }
56 let mut last = 0.0f32;
57 for part in stretches {
58 if part.0 < last || part.1 < part.0 || size < part.1 {
59 return false;
60 }
61 last = part.1;
62 }
63 true
64}
65
66fn validate_content(content: &ImageContent, size: Size) -> bool {
67 let (width, height) = (size.width as f32, size.height as f32);
68 if content.left < 0.0 || width < content.left {
69 return false;
70 }
71 if content.top < 0.0 || height < content.top {
72 return false;
73 }
74 if content.right < 0.0 || width < content.right {
75 return false;
76 }
77 if content.bottom < 0.0 || height < content.bottom {
78 return false;
79 }
80 if content.right < content.left {
81 return false;
82 }
83 if content.bottom < content.top {
84 return false;
85 }
86 true
87}
88
89impl StyleImage {
90 #[expect(clippy::too_many_arguments, reason = "each argument is a field of the image")]
95 pub fn new(
96 id: String,
97 image: PremultipliedImage,
98 pixel_ratio: f32,
99 sdf: bool,
100 stretch_x: ImageStretches,
101 stretch_y: ImageStretches,
102 content: Option<ImageContent>,
103 text_fit_width: Option<TextFit>,
104 text_fit_height: Option<TextFit>,
105 ) -> Result<Self, Error> {
106 if !image.valid() {
107 return Err(Error::invalid_argument("dimensions may not be zero"));
108 } else if pixel_ratio <= 0.0 {
109 return Err(Error::invalid_argument("pixelRatio may not be <= 0"));
110 } else if !validate_stretch(&stretch_x, image.size.width as f32) {
111 return Err(Error::invalid_argument("stretchX is out of bounds or overlapping"));
112 } else if !validate_stretch(&stretch_y, image.size.height as f32) {
113 return Err(Error::invalid_argument("stretchY is out of bounds or overlapping"));
114 } else if content.as_ref().is_some_and(|content| !validate_content(content, image.size)) {
115 return Err(Error::invalid_argument("content area is invalid"));
116 }
117 Ok(StyleImage {
118 id,
119 image,
120 pixel_ratio,
121 sdf,
122 stretch_x,
123 stretch_y,
124 content,
125 text_fit_width,
126 text_fit_height,
127 keep_unused: false,
128 })
129 }
130}