Simplify layout_inline's signature

This commit is contained in:
Laurenz 2022-11-17 21:57:15 +01:00
parent cabd0908e2
commit ddb617390c
10 changed files with 45 additions and 54 deletions

View File

@ -17,11 +17,9 @@ impl LayoutInline for HideNode {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
let mut frames = self.0.layout_inline(world, regions, styles)?;
for frame in &mut frames {
) -> SourceResult<Frame> {
let mut frame = self.0.layout_inline(world, regions, styles)?;
frame.clear();
}
Ok(frames)
Ok(frame)
}
}

View File

@ -43,7 +43,7 @@ impl LayoutInline for ImageNode {
_: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
) -> SourceResult<Frame> {
let pxw = self.0.width() as f64;
let pxh = self.0.height() as f64;
let px_ratio = pxw / pxh;
@ -94,7 +94,7 @@ impl LayoutInline for ImageNode {
frame.link(url.clone());
}
Ok(vec![frame])
Ok(frame)
}
}

View File

@ -42,7 +42,7 @@ impl LayoutInline for LineNode {
_: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
) -> SourceResult<Frame> {
let stroke = styles.get(Self::STROKE).unwrap_or_default();
let origin = self
@ -63,6 +63,6 @@ impl LayoutInline for LineNode {
let shape = Geometry::Line(delta.to_point()).stroked(stroke);
frame.push(origin.to_point(), Element::Shape(shape));
Ok(vec![frame])
Ok(frame)
}
}

View File

@ -78,8 +78,8 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
let mut frames;
) -> SourceResult<Frame> {
let mut frame;
if let Some(child) = &self.0 {
let mut inset = styles.get(Self::INSET);
if is_round(S) {
@ -90,7 +90,7 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> {
let child = child.clone().padded(inset.map(|side| side.map(Length::from)));
let mut pod = Regions::one(regions.first, regions.base, regions.expand);
frames = child.layout_inline(world, &pod, styles)?;
frame = child.layout_inline(world, &pod, styles)?;
// Relayout with full expansion into square region to make sure
// the result is really a square or circle.
@ -99,14 +99,14 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> {
let target = regions.expand.select(regions.first, Size::zero());
target.x.max(target.y)
} else {
let size = frames[0].size();
let size = frame.size();
let desired = size.x.max(size.y);
desired.min(regions.first.x).min(regions.first.y)
};
pod.first = Size::splat(length);
pod.expand = Axes::splat(true);
frames = child.layout_inline(world, &pod, styles)?;
frame = child.layout_inline(world, &pod, styles)?;
}
} else {
// The default size that a shape takes on if it has no child and
@ -125,11 +125,9 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> {
size = regions.expand.select(regions.first, size);
}
frames = vec![Frame::new(size)];
frame = Frame::new(size);
}
let frame = &mut frames[0];
// Add fill and/or stroke.
let fill = styles.get(Self::FILL);
let stroke = match styles.get(Self::STROKE) {
@ -167,7 +165,7 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> {
frame.link(url.clone());
}
Ok(frames)
Ok(frame)
}
}

View File

@ -26,7 +26,7 @@ impl LayoutInline for BoxNode {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
) -> SourceResult<Frame> {
// The "pod" is the region into which the child will be layouted.
let pod = {
// Resolve the sizing to a concrete size.
@ -47,14 +47,13 @@ impl LayoutInline for BoxNode {
};
// Layout the child.
let mut frames = self.child.layout_inline(world, &pod, styles)?;
let mut frame = self.child.layout_inline(world, &pod, styles)?;
// Ensure frame size matches regions size if expansion is on.
let frame = &mut frames[0];
let target = regions.expand.select(regions.first, frame.size());
frame.resize(target, Align::LEFT_TOP);
Ok(frames)
Ok(frame)
}
}

View File

@ -112,7 +112,7 @@ pub trait LayoutInline: 'static + Sync + Send {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>>;
) -> SourceResult<Frame>;
}
impl LayoutInline for Content {
@ -122,7 +122,10 @@ impl LayoutInline for Content {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
) -> SourceResult<Frame> {
assert!(regions.backlog.is_empty());
assert!(regions.last.is_none());
if !self.has::<dyn Show>() || !styles.applicable(self) {
if let Some(node) = self.to::<dyn LayoutInline>() {
let barrier = StyleEntry::Barrier(self.id());
@ -133,7 +136,7 @@ impl LayoutInline for Content {
if let Some(node) = self.to::<dyn LayoutBlock>() {
let barrier = StyleEntry::Barrier(self.id());
let styles = barrier.chain(&styles);
return node.layout_block(world, regions, styles);
return Ok(node.layout_block(world, regions, styles)?.remove(0));
}
}
@ -141,7 +144,7 @@ impl LayoutInline for Content {
let mut builder = Builder::new(world, &scratch, false);
builder.accept(self, styles)?;
let (flow, shared) = builder.into_flow(styles)?;
flow.layout_block(world, regions, shared)
Ok(flow.layout_block(world, regions, shared)?.remove(0))
}
}

View File

@ -30,16 +30,12 @@ impl LayoutInline for MoveNode {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
let mut frames = self.child.layout_inline(world, regions, styles)?;
) -> SourceResult<Frame> {
let mut frame = self.child.layout_inline(world, regions, styles)?;
let delta = self.delta.resolve(styles);
for frame in &mut frames {
let delta = delta.zip(frame.size()).map(|(d, s)| d.relative_to(s));
frame.translate(delta.to_point());
}
Ok(frames)
Ok(frame)
}
}
@ -88,20 +84,17 @@ impl<const T: TransformKind> LayoutInline for TransformNode<T> {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON);
let mut frames = self.child.layout_inline(world, regions, styles)?;
) -> SourceResult<Frame> {
let mut frame = self.child.layout_inline(world, regions, styles)?;
for frame in &mut frames {
let origin = styles.get(Self::ORIGIN).unwrap_or(Align::CENTER_HORIZON);
let Axes { x, y } = origin.zip(frame.size()).map(|(o, s)| o.position(s));
let transform = Transform::translate(x, y)
.pre_concat(self.transform)
.pre_concat(Transform::translate(-x, -y));
frame.transform(transform);
}
Ok(frames)
Ok(frame)
}
}

View File

@ -51,8 +51,8 @@ impl LayoutInline for MathNode {
world: Tracked<dyn World>,
_: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
Ok(vec![layout_tex(&self.texify(), self.display, world, styles)?])
) -> SourceResult<Frame> {
layout_tex(&self.texify(), self.display, world, styles)
}
}

View File

@ -141,7 +141,7 @@ impl LayoutInline for RepeatNode {
world: Tracked<dyn World>,
regions: &Regions,
styles: StyleChain,
) -> SourceResult<Vec<Frame>> {
) -> SourceResult<Frame> {
self.0.layout_inline(world, regions, styles)
}
}
@ -526,7 +526,7 @@ fn prepare<'a>(
} else {
let size = Size::new(regions.first.x, regions.base.y);
let pod = Regions::one(size, regions.base, Axes::splat(false));
let mut frame = inline.layout_inline(world, &pod, styles)?.remove(0);
let mut frame = inline.layout_inline(world, &pod, styles)?;
frame.translate(Point::with_y(styles.get(TextNode::BASELINE)));
items.push(Item::Frame(frame));
}
@ -1151,7 +1151,7 @@ fn commit(
let fill = Fr::one().share(fr, remaining);
let size = Size::new(fill, regions.base.y);
let pod = Regions::one(size, regions.base, Axes::new(false, false));
let frame = repeat.layout_inline(world, &pod, *styles)?.remove(0);
let frame = repeat.layout_inline(world, &pod, *styles)?;
let width = frame.width();
let count = (fill / width).floor();
let remaining = fill % width;

View File

@ -23,7 +23,7 @@ pub static LANG_ITEMS: OnceCell<LangItems> = OnceCell::new();
/// break incremental, but only when different sets of lang items are used in
/// the same program. For this reason, if this function is called multiple
/// times, the items must be the same.
pub fn set_lang_items(items: LangItems) {
pub(crate) fn set_lang_items(items: LangItems) {
if LANG_ITEMS.set(items).is_err() {
let first = hash128(LANG_ITEMS.get().unwrap());
let second = hash128(&items);