1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2025 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0
use crate::core::Property;
use crate::kurbo::{Affine, Axis, Rect, Size};
use crate::layout::{LenReq, Length};
use crate::util::Sanitize;
// These are based on https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
/// Strategies for inscribing a rectangle inside another rectangle.
///
/// Default value is [`Self::Contain`].
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ObjectFit {
/// The content is scaled to fully fit within the container.
///
/// The content's aspect ratio is maintained.
///
/// If the content's aspect ratio does not match the aspect ratio of its container,
/// then the content will not cover the whole container and nothing will overflow.
Contain,
/// The content is scaled to fully fill the container.
///
/// The content's aspect ratio is maintained.
///
/// If the content's aspect ratio does not match the aspect ratio of its container,
/// then the content will overflow the container.
Cover,
/// The content is scaled to fully fill the container's height.
///
/// The content's aspect ratio is maintained.
///
/// This may result in letterboxed or overflowing width.
FitHeight,
/// The content is scaled to fully fill the container's width.
///
/// The content's aspect ratio is maintained.
///
/// This may result in letterboxed or overflowing height.
FitWidth,
/// The content's size is not changed at all.
None,
/// The content is only scaled down.
///
/// This behaves as a mix of [`None`] and [`Contain`], resulting in whichever variant
/// gives the smaller size.
///
/// [`None`]: ObjectFit::None
/// [`Contain`]: ObjectFit::Contain
ScaleDown,
/// The content is stretched to fully fill the container.
///
/// If the content's aspect ratio does not match the aspect ratio of its container,
/// then the content will be stretched to fit exactly, changing its aspect ratio.
Stretch,
}
impl Property for ObjectFit {
fn static_default() -> &'static Self {
&Self::Contain
}
}
impl Default for ObjectFit {
fn default() -> Self {
*Self::static_default()
}
}
impl ObjectFit {
/// Calculates an [`Affine`] transform to fit `content` inside `container`.
///
/// Both `content` and `container` can have arbitrary origins, which will be
/// correctly translated in the returned `Affine`.
///
/// See [`ObjectFit`] variant documentation for fitting details.
///
/// # Panics
///
/// Panics if either `content` or `container` has non-finite or negative size
/// and debug assertions are enabled.
pub fn affine(self, container: Rect, content: Rect) -> Affine {
// Guard against invalid input
let container_width = container.width().sanitize("container width");
let container_height = container.height().sanitize("container height");
let content_width = content.width().sanitize("content width");
let content_height = content.height().sanitize("content height");
// Guard against division by zero
if content_width == 0. || content_height == 0. {
return Affine::IDENTITY;
}
let raw_scalex = container_width / content_width;
let raw_scaley = container_height / content_height;
let (scalex, scaley) = match self {
Self::Contain => {
let scale = raw_scalex.min(raw_scaley);
(scale, scale)
}
Self::Cover => {
let scale = raw_scalex.max(raw_scaley);
(scale, scale)
}
Self::FitHeight => (raw_scaley, raw_scaley),
Self::FitWidth => (raw_scalex, raw_scalex),
Self::None => (1.0, 1.0),
Self::ScaleDown => {
let scale = raw_scalex.min(raw_scaley).min(1.0);
(scale, scale)
}
Self::Stretch => (raw_scalex, raw_scaley),
};
let origin_x = container.x0 + (container_width - (content_width * scalex)) * 0.5;
let origin_y = container.y0 + (container_height - (content_height * scaley)) * 0.5;
Affine::new([
scalex,
0.,
0.,
scaley,
origin_x - content.x0 * scalex,
origin_y - content.y0 * scaley,
])
}
/// Calculates the [`Length`] of `axis`.
///
/// `preferred_size` is the natural size that is used for
/// both aspect ratio and minimum preferred size.
pub fn measure(
self,
axis: Axis,
len_req: LenReq,
cross_length: Option<Length>,
preferred_size: Size,
) -> Length {
let preferred_length = preferred_size.get_coord(axis);
let (space, space_or_preferred) = match len_req {
LenReq::MinContent => return Length::px(preferred_length),
LenReq::MaxContent => (f64::INFINITY, preferred_length),
LenReq::FitContent(space) => (space.get(), space.get()),
};
let mut ar = preferred_size.get_coord(axis) / preferred_size.get_coord(axis.cross());
if !ar.is_finite() {
ar = 1.;
}
let length = match self {
// Use all available space or if cross is known attempt to maintain AR,
// but don't exceed available space (will letterbox cross).
Self::Contain => cross_length
.map(|cl| (cl.get() * ar).min(space))
.unwrap_or(space_or_preferred),
// Always use all available space.
Self::Cover | Self::Stretch => space_or_preferred,
// Always use all available vertical space.
// Greedily take all horizontal space unless cross is known.
Self::FitHeight => match axis {
Axis::Horizontal => cross_length
.map(|cl| (cl.get() * ar).min(space))
.unwrap_or(space_or_preferred),
Axis::Vertical => space_or_preferred,
},
// Always use all available horizontal space.
// Greedily take all vertical space unless cross is known.
Self::FitWidth => match axis {
Axis::Horizontal => space_or_preferred,
Axis::Vertical => cross_length
.map(|cl| (cl.get() * ar).min(space))
.unwrap_or(space_or_preferred),
},
// None == preferred size
Self::None => preferred_length,
// ScaleDown == min(Contain, None)
Self::ScaleDown => cross_length
.map(|cl| (cl.get() * ar).min(space))
.unwrap_or(space_or_preferred)
.min(preferred_length),
};
Length::px(length)
}
}