layout: an Image measure with an unsound extent or ratio is named and leaves the image unconstrained

This commit is contained in:
Meta Vens 2026-11-28 05:56:12 +01:00
commit ce37f22671
3 changed files with 56 additions and 8 deletions

View file

@ -95,10 +95,21 @@ pub(super) fn measure_leaf(
dev.size(measure.measure(leaf, constraint))
}
/// The logical size a leaf has when nothing constrains it; an Image's aspect ratio comes from it.
pub(super) fn intrinsic(measure: &mut dyn Measure, leaf: &Leaf) -> (f32, f32) {
/// The aspect ratio an Image keeps when stretched, from what its measure says unconstrained
/// (logical px). `Ok(None)` is no extent at all (0x0: nothing known yet). A side that is not
/// finite, negative, past MAX_DEVICE, or only one side zero, or a ratio outside
/// [1/MAX_DEVICE, MAX_DEVICE] (so that ratio times a legal side stays finite and whole-pixel
/// arithmetic holds) is an invalid measure: `Err`.
pub(super) fn intrinsic_ratio(measure: &mut dyn Measure, leaf: &Leaf) -> Result<Option<f32>, ()> {
let constraint = Constraint { known: [None, None], available: [Avail::MaxContent, Avail::MaxContent] };
let (w, h) = measure.measure(leaf, constraint);
let clean = |v: f32| if v.is_finite() && v > 0.0 { v.min(MAX_DEVICE) } else { 0.0 };
(clean(w), clean(h))
let legal = |v: f32| v.is_finite() && (0.0..=MAX_DEVICE).contains(&v);
if !legal(w) || !legal(h) {
return Err(());
}
if w == 0.0 && h == 0.0 {
return Ok(None);
}
let ratio = w / h;
if w > 0.0 && h > 0.0 && (1.0 / MAX_DEVICE..=MAX_DEVICE).contains(&ratio) { Ok(Some(ratio)) } else { Err(()) }
}

View file

@ -3,7 +3,7 @@
use taffy::{Style, TaffyTree};
use thing_contract::{Node, NodeId, Overflow};
use super::measure::{Device, Leaf, intrinsic};
use super::measure::{Device, Leaf, intrinsic_ratio};
use super::style::{Invalid, box_style, embed_style};
use crate::bind::{Bound, BoundTree, Field};
use crate::diagnostic::{Code, Diagnostic};
@ -102,9 +102,14 @@ impl<'a> Builder<'a, '_> {
let mut style = Style::default();
if matches!(leaf, Leaf::Image { .. }) {
// A stretched image keeps its shape: the ratio is what the measure says it is unconstrained.
let (w, h) = intrinsic(self.measure, &leaf);
style.item_is_replaced = true;
style.aspect_ratio = (w > 0.0 && h > 0.0).then(|| w / h);
// An invalid measure leaves the image unconstrained (no ratio, not replaced) and is named.
match intrinsic_ratio(self.measure, &leaf) {
Ok(ratio) => {
style.item_is_replaced = true;
style.aspect_ratio = ratio;
}
Err(()) => self.report(id, vec!["measure"]),
}
}
let index = self.entry(id, parent, &Overflow::Visible);
let context = self.leaves.len();

View file

@ -621,6 +621,38 @@ fn kind_style(kind: Kind) -> BoxStyle {
}
/// `levels` nested boxes of one kind with a text leaf at the bottom.
#[test]
fn invalid_image_measures_are_named_and_harmless() {
struct Image((f32, f32));
impl Measure for Image {
fn measure(&mut self, leaf: &Leaf, _: Constraint) -> (f32, f32) {
match leaf {
Leaf::Image { .. } => self.0,
_ => (8.0, 16.0),
}
}
}
let picture = || Node::Image(ImageNode { common: common("pic"), catalog: "c".into(), asset: "a".into(), fit: ImageFit::Contain, alt: lit_text("alt") });
let root = || bx("root", BoxStyle { direction: Direction::Column, size: size2(px(200.0), Dimension::Auto), ..plain() }, vec![picture(), text("sibling", "hi")]);
let tree = bind(&mut MemState::new(), &surface_of(root())).unwrap();
let run = |size| layout(&tree, &pixels(800.0, 600.0, 1.0), &mut Image(size)).unwrap();
for size in [(1e7, 1e-30), (1e-30, 1e7), (0.0, 5.0), (f32::NAN, 1.0), (f32::INFINITY, 1.0), (-1.0, 2.0), (1e8, 1e8)] {
let laid = run(size);
assert_eq!(laid.diagnostics.len(), 1, "{size:?}");
let d = &laid.diagnostics[0];
assert_eq!((d.code, d.node.as_ref().map(|n| n.as_str()), d.detail.as_str()), (Code::StyleValueInvalid, Some("pic"), "measure"), "{size:?}");
// The sibling keeps its own size and sits below the picture.
let sibling = rect(&laid, "sibling");
assert_eq!((sibling.2, sibling.3), (16.0, 16.0), "{size:?}");
assert!(laid.nodes.iter().all(|n| [n.rect.x, n.rect.y, n.rect.w, n.rect.h].iter().all(|v| v.is_finite())), "{size:?}");
}
// No extent yet (0x0) is not a fault, and a sound ratio is kept.
assert!(run((0.0, 0.0)).diagnostics.is_empty());
let sound = run((40.0, 20.0));
assert!(sound.diagnostics.is_empty());
assert_eq!(rect(&sound, "pic").3, 100.0);
}
fn chain(kind: Kind, levels: usize) -> Node {
let mut node = text("bottom", "x");
for level in (0..levels).rev() {