use std::cell::LazyCell;
use smallvec::SmallVec;
use typst_library::diag::SourceResult;
use typst_library::engine::Engine;
use typst_library::foundations::{Packed, Resolve, StyleChain};
use typst_library::introspection::Locator;
use typst_library::layout::{
Abs, Axes, BlockBody, BlockElem, Fragment, Frame, FrameKind, Region, Regions, Rel,
Sides, Size, Sizing,
};
use typst_library::visualize::Stroke;
use typst_utils::Numeric;
use crate::shapes::{clip_rect, fill_and_stroke};
#[typst_macros::time(name = "block", span = elem.span())]
pub fn layout_single_block(
elem: &Packed<BlockElem>,
engine: &mut Engine,
locator: Locator,
styles: StyleChain,
region: Region,
) -> SourceResult<Frame> {
let width = elem.width.get(styles);
let height = elem.height.get(styles);
let inset = elem.inset.resolve(styles).unwrap_or_default();
let pod = unbreakable_pod(&width.into(), &height, &inset, styles, region.size);
let body = elem.body.get_ref(styles);
let mut frame = match body {
None => Frame::hard(Size::zero()),
Some(BlockBody::Content(body)) => {
crate::layout_frame(engine, body, locator.relayout(), styles, pod)?
}
Some(BlockBody::SingleLayouter(callback)) => {
callback.call(engine, locator, styles, pod)?
}
Some(BlockBody::MultiLayouter(callback)) => {
let expand = (pod.expand | region.expand) & pod.size.map(Abs::is_finite);
let pod = Region { expand, ..pod };
callback.call(engine, locator, styles, pod.into())?.into_frame()
}
};
if matches!(body, None | Some(BlockBody::Content(_))) {
frame.set_kind(FrameKind::Hard);
}
frame.set_size(pod.expand.select(pod.size, frame.size()));
if !inset.is_zero() {
crate::pad::grow(&mut frame, &inset);
}
let fill = elem.fill.get_cloned(styles);
let stroke = elem
.stroke
.resolve(styles)
.unwrap_or_default()
.map(|s| s.map(Stroke::unwrap_or_default));
let outset = LazyCell::new(|| elem.outset.resolve(styles).unwrap_or_default());
let radius = LazyCell::new(|| elem.radius.resolve(styles).unwrap_or_default());
if elem.clip.get(styles) {
frame.clip(clip_rect(frame.size(), &radius, &stroke, &outset));
}
if fill.is_some() || stroke.iter().any(Option::is_some) {
fill_and_stroke(&mut frame, fill, &stroke, &outset, &radius, elem.span());
}
if let Some(label) = elem.label() {
frame.label(label);
}
Ok(frame)
}
#[typst_macros::time(name = "block", span = elem.span())]
pub fn layout_multi_block(
elem: &Packed<BlockElem>,
engine: &mut Engine,
locator: Locator,
styles: StyleChain,
regions: Regions,
) -> SourceResult<Fragment> {
let width = elem.width.get(styles);
let height = elem.height.get(styles);
let inset = elem.inset.resolve(styles).unwrap_or_default();
let mut buf = SmallVec::<[Abs; 2]>::new();
let pod = breakable_pod(&width.into(), &height, &inset, styles, regions, &mut buf);
let body = elem.body.get_ref(styles);
let mut fragment = match body {
None => {
let mut frames = vec![];
frames.push(Frame::hard(Size::zero()));
if pod.expand.y {
let mut iter = pod;
while !iter.backlog.is_empty() {
frames.push(Frame::hard(Size::zero()));
iter.next();
}
}
Fragment::frames(frames)
}
Some(BlockBody::Content(body)) => {
let mut fragment =
crate::layout_fragment(engine, body, locator.relayout(), styles, pod)?;
if !pod.expand.x
&& fragment
.as_slice()
.windows(2)
.any(|w| !w[0].width().approx_eq(w[1].width()))
{
let max_width =
fragment.iter().map(|frame| frame.width()).max().unwrap_or_default();
let pod = Regions {
size: Size::new(max_width, pod.size.y),
expand: Axes::new(true, pod.expand.y),
..pod
};
fragment = crate::layout_fragment(engine, body, locator, styles, pod)?;
}
fragment
}
Some(BlockBody::SingleLayouter(callback)) => {
let pod = Region::new(pod.base(), pod.expand);
callback.call(engine, locator, styles, pod).map(Fragment::frame)?
}
Some(BlockBody::MultiLayouter(callback)) => {
let expand = (pod.expand | regions.expand) & pod.size.map(Abs::is_finite);
let pod = Regions { expand, ..pod };
callback.call(engine, locator, styles, pod)?
}
};
let fill = elem.fill.get_ref(styles);
let stroke = elem
.stroke
.resolve(styles)
.unwrap_or_default()
.map(|s| s.map(Stroke::unwrap_or_default));
let outset = LazyCell::new(|| elem.outset.resolve(styles).unwrap_or_default());
let radius = LazyCell::new(|| elem.radius.resolve(styles).unwrap_or_default());
let clip = elem.clip.get(styles);
let has_fill_or_stroke = fill.is_some() || stroke.iter().any(Option::is_some);
let has_inset = !inset.is_zero();
let is_explicit = matches!(body, None | Some(BlockBody::Content(_)));
let mut skip_first = false;
if let [first, rest @ ..] = fragment.as_slice() {
skip_first = first.is_empty() && rest.iter().any(|frame| !frame.is_empty());
}
for (i, (frame, region)) in fragment.iter_mut().zip(pod.iter()).enumerate() {
if is_explicit {
frame.set_kind(FrameKind::Hard);
}
frame.set_size(pod.expand.select(region, frame.size()));
if has_inset {
crate::pad::grow(frame, &inset);
}
if clip {
frame.clip(clip_rect(frame.size(), &radius, &stroke, &outset));
}
if has_fill_or_stroke && (i > 0 || !skip_first) {
fill_and_stroke(frame, fill.clone(), &stroke, &outset, &radius, elem.span());
}
}
if let Some(label) = elem.label() {
for frame in fragment.iter_mut().skip(if skip_first { 1 } else { 0 }) {
frame.label(label);
}
}
Ok(fragment)
}
pub(crate) fn unbreakable_pod(
width: &Sizing,
height: &Sizing,
inset: &Sides<Rel<Abs>>,
styles: StyleChain,
base: Size,
) -> Region {
let mut size = Size::new(
match width {
Sizing::Auto | Sizing::Fr(_) => base.x,
Sizing::Rel(rel) => rel.resolve(styles).relative_to(base.x),
},
match height {
Sizing::Auto | Sizing::Fr(_) => base.y,
Sizing::Rel(rel) => rel.resolve(styles).relative_to(base.y),
},
);
if !inset.is_zero() {
size = crate::pad::shrink(size, inset);
}
let expand = Axes::new(
*width != Sizing::Auto && size.x.is_finite(),
*height != Sizing::Auto && size.y.is_finite(),
);
Region::new(size, expand)
}
fn breakable_pod<'a>(
width: &Sizing,
height: &Sizing,
inset: &Sides<Rel<Abs>>,
styles: StyleChain,
regions: Regions,
buf: &'a mut SmallVec<[Abs; 2]>,
) -> Regions<'a> {
let base = regions.base();
let first;
let full;
let backlog: &mut [Abs];
let last;
match height {
Sizing::Auto | Sizing::Fr(_) => {
first = regions.size.y;
full = regions.full;
buf.extend_from_slice(regions.backlog);
backlog = buf;
last = regions.last;
}
Sizing::Rel(rel) => {
let resolved = rel.resolve(styles).relative_to(base.y);
full = resolved;
(first, backlog) = distribute(resolved, regions, buf);
last = None;
}
};
let mut size = Size::new(
match width {
Sizing::Auto | Sizing::Fr(_) => regions.size.x,
Sizing::Rel(rel) => rel.resolve(styles).relative_to(base.x),
},
first,
);
let (mut full, mut last) = (full, last);
if !inset.is_zero() {
crate::pad::shrink_multiple(&mut size, &mut full, backlog, &mut last, inset);
}
let expand = Axes::new(
*width != Sizing::Auto && size.x.is_finite(),
*height != Sizing::Auto && size.y.is_finite(),
);
Regions { size, full, backlog, last, expand }
}
fn distribute<'a>(
height: Abs,
mut regions: Regions,
buf: &'a mut SmallVec<[Abs; 2]>,
) -> (Abs, &'a mut [Abs]) {
let mut remaining = height;
if remaining <= Abs::zero() {
buf.push(remaining);
return (buf[0], &mut buf[1..]);
}
loop {
let limited = regions.size.y.clamp(Abs::zero(), remaining);
buf.push(limited);
remaining -= limited;
if remaining.approx_empty()
|| !regions.may_break()
|| (!regions.may_progress() && limited.approx_empty())
{
break;
}
regions.next();
}
if !remaining.approx_empty()
&& let Some(last) = buf.last_mut()
{
*last += remaining;
}
(buf[0], &mut buf[1..])
}