Thing 2.0 cut 6b: fit table expectations per asset rect

This commit is contained in:
Meta Vens 2026-11-28 08:08:58 +01:00
commit 56c7bb6653

View file

@ -801,31 +801,32 @@ fn fills_of(rendered: &Rendered, token: &str) -> Vec<Rect> {
#[test]
fn fit_table() {
let provider = ProviderId::new(PROVIDER);
let run = |asset: &str, fit: ImageFit| {
// A row 100 high stretches an image to 100 high and leaves it its extent's width, so the rect
// is the ratio of none of the assets: wide 400x100, tall 100x100, small 40x100.
let run = |asset: &str, fit: ImageFit, at: Rect| {
let mut state = MemState::new();
state.put(&provider, &catalog(&[("wide", 400, 200), ("tall", 100, 400), ("small", 40, 20)]));
// A row 100 high stretches the image to 100 high and leaves it its 400 wide extent width:
// a 400x100 rect, which is the ratio of none of the three assets.
let rendered = with_state(&mut state, flat("root", sized(100.0, 100.0), vec![image("pic", asset, fit)]), &pixels(1.0));
assert_eq!(placed(&rendered, "pic").rect, rect(0.0, 0.0, 400.0, 100.0), "the image's rect");
assert_eq!(placed(&rendered, "pic").rect, at, "{asset}: the image's rect");
rendered.commands
};
let wide = rect(0.0, 0.0, 400.0, 100.0);
let tall = rect(0.0, 0.0, 100.0, 100.0);
let small = rect(0.0, 0.0, 40.0, 100.0);
let img = |r: Rect, uri: &str| RenderCommand::Image { rect: r, uri: format!("asset://{uri}"), alt: "alt".into() };
let clip = |inner: RenderCommand| {
[RenderCommand::ClipPush { rect: rect(0.0, 0.0, 400.0, 100.0), radius: 0.0 }, inner, RenderCommand::ClipPop].to_vec()
};
let whole = rect(0.0, 0.0, 400.0, 100.0);
for asset in ["wide", "tall", "small"] {
assert_eq!(run(asset, ImageFit::Fill), [img(whole, asset)], "{asset}");
let clip = |at: Rect, inner: RenderCommand| [RenderCommand::ClipPush { rect: at, radius: 0.0 }, inner, RenderCommand::ClipPop].to_vec();
for (asset, at) in [("wide", wide), ("tall", tall), ("small", small)] {
assert_eq!(run(asset, ImageFit::Fill, at), [img(at, asset)], "{asset}");
}
assert_eq!(run("wide", ImageFit::Contain), [img(rect(100.0, 0.0, 200.0, 100.0), "wide")]);
assert_eq!(run("tall", ImageFit::Contain), [img(rect(187.5, 0.0, 25.0, 100.0), "tall")]);
assert_eq!(run("small", ImageFit::Contain), [img(rect(100.0, 0.0, 200.0, 100.0), "small")]);
assert_eq!(run("wide", ImageFit::Cover), clip(img(rect(0.0, -50.0, 400.0, 200.0), "wide")));
assert_eq!(run("tall", ImageFit::Cover), clip(img(rect(0.0, -750.0, 400.0, 1600.0), "tall")));
assert_eq!(run("small", ImageFit::Cover), clip(img(rect(0.0, -50.0, 400.0, 200.0), "small")));
assert_eq!(run("tall", ImageFit::Unscaled), clip(img(rect(150.0, -150.0, 100.0, 400.0), "tall")));
assert_eq!(run("small", ImageFit::Unscaled), clip(img(rect(180.0, 40.0, 40.0, 20.0), "small")));
assert_eq!(run("wide", ImageFit::Contain, wide), [img(rect(100.0, 0.0, 200.0, 100.0), "wide")]);
assert_eq!(run("tall", ImageFit::Contain, tall), [img(rect(37.5, 0.0, 25.0, 100.0), "tall")]);
assert_eq!(run("small", ImageFit::Contain, small), [img(rect(0.0, 40.0, 40.0, 20.0), "small")]);
assert_eq!(run("wide", ImageFit::Cover, wide), clip(wide, img(rect(0.0, -50.0, 400.0, 200.0), "wide")));
assert_eq!(run("tall", ImageFit::Cover, tall), clip(tall, img(rect(0.0, -150.0, 100.0, 400.0), "tall")));
assert_eq!(run("small", ImageFit::Cover, small), clip(small, img(rect(-80.0, 0.0, 200.0, 100.0), "small")));
assert_eq!(run("wide", ImageFit::Unscaled, wide), clip(wide, img(rect(0.0, -50.0, 400.0, 200.0), "wide")));
assert_eq!(run("tall", ImageFit::Unscaled, tall), clip(tall, img(rect(0.0, -150.0, 100.0, 400.0), "tall")));
assert_eq!(run("small", ImageFit::Unscaled, small), clip(small, img(rect(0.0, 40.0, 40.0, 20.0), "small")));
}
#[test]