use comemo::Tracked;
use crate::diag::{warning, At, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
dict, func, Content, Context, Dict, Resolve, Smart, StyleChain, Styles,
};
use crate::introspection::{Locator, LocatorLink};
use crate::layout::{layout_frame, Abs, Axes, Length, Region, Size};
use crate::syntax::Span;
#[func(contextual)]
pub fn measure(
engine: &mut Engine,
context: Tracked<Context>,
span: Span,
#[named]
#[default(Smart::Auto)]
width: Smart<Length>,
#[named]
#[default(Smart::Auto)]
height: Smart<Length>,
content: Content,
#[default]
styles: Option<Styles>,
) -> SourceResult<Dict> {
let styles = match &styles {
Some(styles) => {
engine.sink.warn(warning!(
span, "calling `measure` with a styles argument is deprecated";
hint: "try removing the styles argument"
));
StyleChain::new(styles)
}
None => context.styles().at(span)?,
};
let pod = Region::new(
Axes::new(
width.resolve(styles).unwrap_or(Abs::inf()),
height.resolve(styles).unwrap_or(Abs::inf()),
),
Axes::splat(false),
);
let here = context.location().at(span)?;
let link = LocatorLink::measure(here);
let locator = Locator::link(&link);
let frame = layout_frame(engine, &content, locator, styles, pod)?;
let Size { x, y } = frame.size();
Ok(dict! { "width" => x, "height" => y })
}