diff options
Diffstat (limited to 'library/src')
31 files changed, 131 insertions, 160 deletions
diff --git a/library/src/core/ext.rs b/library/src/core/ext.rs index 44479e9d..f90260ad 100644 --- a/library/src/core/ext.rs +++ b/library/src/core/ext.rs @@ -70,7 +70,7 @@ impl ContentExt for Content { } } -/// Additional methods for the style chain. +/// Additional methods for style maps. pub trait StyleMapExt { /// Set a font family composed of a preferred family and existing families /// from a style chain. @@ -106,10 +106,10 @@ impl LayoutBlock for FillNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { - let mut frames = self.child.layout_block(world, regions, styles)?; + let mut frames = self.child.layout_block(world, styles, regions)?; for frame in &mut frames { let shape = Geometry::Rect(frame.size()).filled(self.fill); frame.prepend(Point::zero(), Element::Shape(shape)); @@ -134,10 +134,10 @@ impl LayoutBlock for StrokeNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { - let mut frames = self.child.layout_block(world, regions, styles)?; + let mut frames = self.child.layout_block(world, styles, regions)?; for frame in &mut frames { let shape = Geometry::Rect(frame.size()).stroked(self.stroke); frame.prepend(Point::zero(), Element::Shape(shape)); diff --git a/library/src/graphics/hide.rs b/library/src/graphics/hide.rs index f79b31ae..3a21c2c7 100644 --- a/library/src/graphics/hide.rs +++ b/library/src/graphics/hide.rs @@ -15,10 +15,10 @@ impl LayoutInline for HideNode { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { - let mut frame = self.0.layout_inline(world, regions, styles)?; + let mut frame = self.0.layout_inline(world, styles, regions)?; frame.clear(); Ok(frame) } diff --git a/library/src/graphics/image.rs b/library/src/graphics/image.rs index 202abebe..de3384df 100644 --- a/library/src/graphics/image.rs +++ b/library/src/graphics/image.rs @@ -41,8 +41,8 @@ impl LayoutInline for ImageNode { fn layout_inline( &self, _: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { let pxw = self.0.width() as f64; let pxh = self.0.height() as f64; diff --git a/library/src/graphics/line.rs b/library/src/graphics/line.rs index e7c347b2..11f0be32 100644 --- a/library/src/graphics/line.rs +++ b/library/src/graphics/line.rs @@ -40,8 +40,8 @@ impl LayoutInline for LineNode { fn layout_inline( &self, _: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { let stroke = styles.get(Self::STROKE).unwrap_or_default(); diff --git a/library/src/graphics/shape.rs b/library/src/graphics/shape.rs index ebdc1717..4c9fec07 100644 --- a/library/src/graphics/shape.rs +++ b/library/src/graphics/shape.rs @@ -76,8 +76,8 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { let mut frame; if let Some(child) = &self.0 { @@ -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); - frame = child.layout_inline(world, &pod, styles)?; + frame = child.layout_inline(world, styles, &pod)?; // Relayout with full expansion into square region to make sure // the result is really a square or circle. @@ -106,7 +106,7 @@ impl<const S: ShapeKind> LayoutInline for ShapeNode<S> { pod.first = Size::splat(length); pod.expand = Axes::splat(true); - frame = child.layout_inline(world, &pod, styles)?; + frame = child.layout_inline(world, styles, &pod)?; } } else { // The default size that a shape takes on if it has no child and diff --git a/library/src/layout/align.rs b/library/src/layout/align.rs index 6337c941..10a4a2ed 100644 --- a/library/src/layout/align.rs +++ b/library/src/layout/align.rs @@ -30,21 +30,21 @@ impl LayoutBlock for AlignNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { // The child only needs to expand along an axis if there's no alignment. let mut pod = regions.clone(); pod.expand &= self.aligns.as_ref().map(Option::is_none); // Align paragraphs inside the child. - let mut passed = StyleMap::new(); + let mut map = StyleMap::new(); if let Some(align) = self.aligns.x { - passed.set(ParNode::ALIGN, HorizontalAlign(align)); + map.set(ParNode::ALIGN, HorizontalAlign(align)); } // Layout the child. - let mut frames = self.child.layout_block(world, &pod, passed.chain(&styles))?; + let mut frames = self.child.layout_block(world, styles.chain(&map), &pod)?; for (region, frame) in regions.iter().zip(&mut frames) { // Align in the target size. The target size depends on whether we // should expand. diff --git a/library/src/layout/columns.rs b/library/src/layout/columns.rs index 6e1aaeae..b18ba49f 100644 --- a/library/src/layout/columns.rs +++ b/library/src/layout/columns.rs @@ -30,13 +30,13 @@ impl LayoutBlock for ColumnsNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { // Separating the infinite space into infinite columns does not make // much sense. if !regions.first.x.is_finite() { - return self.child.layout_block(world, regions, styles); + return self.child.layout_block(world, styles, regions); } // Determine the width of the gutter and each column. @@ -58,7 +58,7 @@ impl LayoutBlock for ColumnsNode { }; // Layout the children. - let mut frames = self.child.layout_block(world, &pod, styles)?.into_iter(); + let mut frames = self.child.layout_block(world, styles, &pod)?.into_iter(); let mut finished = vec![]; let dir = styles.get(TextNode::DIR); diff --git a/library/src/layout/container.rs b/library/src/layout/container.rs index 9a6ccb9f..b299c0fc 100644 --- a/library/src/layout/container.rs +++ b/library/src/layout/container.rs @@ -24,8 +24,8 @@ impl LayoutInline for BoxNode { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { // The "pod" is the region into which the child will be layouted. let pod = { @@ -47,7 +47,7 @@ impl LayoutInline for BoxNode { }; // Layout the child. - let mut frame = self.child.layout_inline(world, &pod, styles)?; + let mut frame = self.child.layout_inline(world, styles, &pod)?; // Ensure frame size matches regions size if expansion is on. let target = regions.expand.select(regions.first, frame.size()); @@ -91,9 +91,9 @@ impl LayoutBlock for BlockNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { - self.0.layout_block(world, regions, styles) + self.0.layout_block(world, styles, regions) } } diff --git a/library/src/layout/flow.rs b/library/src/layout/flow.rs index 4508023a..3338da09 100644 --- a/library/src/layout/flow.rs +++ b/library/src/layout/flow.rs @@ -18,13 +18,13 @@ impl LayoutBlock for FlowNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { let mut layouter = FlowLayouter::new(regions); for (child, map) in self.0.iter() { - let styles = map.chain(&styles); + let styles = styles.chain(&map); if let Some(&node) = child.to::<VNode>() { layouter.layout_spacing(node.amount, styles); } else if child.has::<dyn LayoutBlock>() { @@ -136,7 +136,7 @@ impl FlowLayouter { // aligned later. if let Some(placed) = block.to::<PlaceNode>() { if placed.out_of_flow() { - let frame = block.layout_block(world, &self.regions, styles)?.remove(0); + let frame = block.layout_block(world, styles, &self.regions)?.remove(0); self.items.push(FlowItem::Placed(frame)); return Ok(()); } @@ -162,11 +162,11 @@ impl FlowLayouter { if !self.last_block_was_par && is_par && !styles.get(ParNode::INDENT).is_zero() { let property = Property::new(ParNode::INDENT, Length::zero()); reset = Style::Property(property); - chained = reset.chain(&styles); + chained = styles.chain_one(&reset); } // Layout the block itself. - let frames = block.layout_block(world, &self.regions, chained)?; + let frames = block.layout_block(world, chained, &self.regions)?; let len = frames.len(); for (i, frame) in frames.into_iter().enumerate() { // Grow our size, shrink the region and save the frame for later. diff --git a/library/src/layout/grid.rs b/library/src/layout/grid.rs index 3b5afcc5..4cbef421 100644 --- a/library/src/layout/grid.rs +++ b/library/src/layout/grid.rs @@ -37,8 +37,8 @@ impl LayoutBlock for GridNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { // Prepare grid layout by unifying content and gutter tracks. let layouter = GridLayouter::new( @@ -321,7 +321,7 @@ impl<'a> GridLayouter<'a> { } let frame = - cell.layout_block(self.world, &pod, self.styles)?.remove(0); + cell.layout_block(self.world, self.styles, &pod)?.remove(0); resolved.set_max(frame.width()); } } @@ -391,7 +391,7 @@ impl<'a> GridLayouter<'a> { } let mut sizes = cell - .layout_block(self.world, &pod, self.styles)? + .layout_block(self.world, self.styles, &pod)? .into_iter() .map(|frame| frame.height()); @@ -480,7 +480,7 @@ impl<'a> GridLayouter<'a> { .select(self.regions.base, size); let pod = Regions::one(size, base, Axes::splat(true)); - let frame = cell.layout_block(self.world, &pod, self.styles)?.remove(0); + let frame = cell.layout_block(self.world, self.styles, &pod)?.remove(0); output.push_frame(pos, frame); } @@ -520,7 +520,7 @@ impl<'a> GridLayouter<'a> { } // Push the layouted frames into the individual output frames. - let frames = cell.layout_block(self.world, &pod, self.styles)?; + let frames = cell.layout_block(self.world, self.styles, &pod)?; for (output, frame) in outputs.iter_mut().zip(frames) { output.push_frame(pos, frame); } diff --git a/library/src/layout/mod.rs b/library/src/layout/mod.rs index 983b96ba..44b7b6d8 100644 --- a/library/src/layout/mod.rs +++ b/library/src/layout/mod.rs @@ -32,8 +32,8 @@ use typst::diag::SourceResult; use typst::frame::Frame; use typst::geom::*; use typst::model::{ - capability, Content, Node, SequenceNode, Style, StyleChain, StyleVecBuilder, - StyledNode, + applicable, capability, realize, Content, Node, SequenceNode, Style, StyleChain, + StyleVecBuilder, StyledNode, }; use typst::World; @@ -77,8 +77,8 @@ pub trait LayoutBlock { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>>; } @@ -87,17 +87,17 @@ impl LayoutBlock for Content { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { let scratch = Scratch::default(); let (realized, styles) = realize_block(world, &scratch, self, styles)?; let barrier = Style::Barrier(realized.id()); - let styles = barrier.chain(&styles); + let styles = styles.chain_one(&barrier); realized .with::<dyn LayoutBlock>() .unwrap() - .layout_block(world, regions, styles) + .layout_block(world, styles, regions) } } @@ -108,8 +108,8 @@ pub trait LayoutInline { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame>; } @@ -118,22 +118,22 @@ impl LayoutInline for Content { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { assert!(regions.backlog.is_empty()); assert!(regions.last.is_none()); - if self.has::<dyn LayoutInline>() && !styles.applicable(self) { + if self.has::<dyn LayoutInline>() && !applicable(self, styles) { let barrier = Style::Barrier(self.id()); - let styles = barrier.chain(&styles); + let styles = styles.chain_one(&barrier); return self .with::<dyn LayoutInline>() .unwrap() - .layout_inline(world, regions, styles); + .layout_inline(world, styles, regions); } - Ok(self.layout_block(world, regions, styles)?.remove(0)) + Ok(self.layout_block(world, styles, regions)?.remove(0)) } } @@ -237,7 +237,7 @@ fn realize_root<'a>( content: &'a Content, styles: StyleChain<'a>, ) -> SourceResult<(Content, StyleChain<'a>)> { - if content.has::<dyn LayoutRoot>() && !styles.applicable(content) { + if content.has::<dyn LayoutRoot>() && !applicable(content, styles) { return Ok((content.clone(), styles)); } @@ -255,7 +255,7 @@ fn realize_block<'a>( content: &'a Content, styles: StyleChain<'a>, ) -> SourceResult<(Content, StyleChain<'a>)> { - if content.has::<dyn LayoutBlock>() && !styles.applicable(content) { + if content.has::<dyn LayoutBlock>() && !applicable(content, styles) { return Ok((content.clone(), styles)); } @@ -319,7 +319,7 @@ impl<'a> Builder<'a> { return Ok(()); } - if let Some(realized) = styles.show(self.world, content)? { + if let Some(realized) = realize(self.world, content, styles)? { let stored = self.scratch.content.alloc(realized); return self.accept(stored, styles); } @@ -366,7 +366,7 @@ impl<'a> Builder<'a> { styles: StyleChain<'a>, ) -> SourceResult<()> { let stored = self.scratch.styles.alloc(styles); - let styles = styled.map.chain(stored); + let styles = stored.chain(&styled.map); self.interrupt_style(&styled.map, None)?; self.accept(&styled.sub, styles)?; self.interrupt_style(&styled.map, Some(styles))?; diff --git a/library/src/layout/pad.rs b/library/src/layout/pad.rs index d860e54d..4389d990 100644 --- a/library/src/layout/pad.rs +++ b/library/src/layout/pad.rs @@ -29,13 +29,13 @@ impl LayoutBlock for PadNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { // Layout child into padded regions. let padding = self.padding.resolve(styles); let pod = regions.map(|size| shrink(size, padding)); - let mut frames = self.child.layout_block(world, &pod, styles)?; + let mut frames = self.child.layout_block(world, styles, &pod)?; for frame in &mut frames { // Apply the padding inversely such that the grown size padded diff --git a/library/src/layout/page.rs b/library/src/layout/page.rs index f3249347..9d5d4f56 100644 --- a/library/src/layout/page.rs +++ b/library/src/layout/page.rs @@ -97,7 +97,7 @@ impl PageNode { // Layout the child. let regions = Regions::repeat(size, size, size.map(Abs::is_finite)); - let mut frames = child.layout_block(world, ®ions, styles)?; + let mut frames = child.layout_block(world, styles, ®ions)?; let header = styles.get(Self::HEADER); let footer = styles.get(Self::FOOTER); @@ -118,7 +118,7 @@ impl PageNode { ] { if let Some(content) = marginal.resolve(world, page)? { let pod = Regions::one(area, area, Axes::splat(true)); - let sub = content.layout_block(world, &pod, styles)?.remove(0); + let sub = content.layout_block(world, styles, &pod)?.remove(0); if std::ptr::eq(marginal, background) { frame.prepend_frame(pos, sub); } else { @@ -402,9 +402,9 @@ papers! { // ---------------------------------------------------------------------- // // Other - (NEWSPAPER_COMPACT: 280.0, 430.0, "newspaper-compact") - (NEWSPAPER_BERLINER: 315.0, 470.0, "newspaper-berliner") - (NEWSPAPER_BROADSHEET: 381.0, 578.0, "newspaper-broadsheet") + (NEWSPAPER_COMPACT: 280.0, 430.0, "newspaper-compact") + (NEWSPAPER_BERLINER: 315.0, 470.0, "newspaper-berliner") + (NEWSPAPER_BROADSHEET: 381.0, 578.0, "newspaper-broadsheet") (PRESENTATION_16_9: 297.0, 167.0625, "presentation-16-9") - (PRESENTATION_4_3: 280.0, 210.0, "presentation-4-3") + (PRESENTATION_4_3: 280.0, 210.0, "presentation-4-3") } diff --git a/library/src/layout/place.rs b/library/src/layout/place.rs index 221e9008..af313073 100644 --- a/library/src/layout/place.rs +++ b/library/src/layout/place.rs @@ -20,8 +20,8 @@ impl LayoutBlock for PlaceNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { let out_of_flow = self.out_of_flow(); @@ -33,7 +33,7 @@ impl LayoutBlock for PlaceNode { Regions::one(regions.base, regions.base, expand) }; - let mut frames = self.0.layout_block(world, &pod, styles)?; + let mut frames = self.0.layout_block(world, styles, &pod)?; // If expansion is off, zero all sizes so that we don't take up any // space in our parent. Otherwise, respect the expand settings. diff --git a/library/src/layout/stack.rs b/library/src/layout/stack.rs index 52de2c48..c935d971 100644 --- a/library/src/layout/stack.rs +++ b/library/src/layout/stack.rs @@ -31,8 +31,8 @@ impl LayoutBlock for StackNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { let mut layouter = StackLayouter::new(self.dir, regions, styles); @@ -189,14 +189,14 @@ impl<'a> StackLayouter<'a> { if let Some(styled) = block.to::<StyledNode>() { let map = &styled.map; if map.contains(ParNode::ALIGN) { - return StyleChain::with_root(map).get(ParNode::ALIGN); + return StyleChain::new(map).get(ParNode::ALIGN); } } self.dir.start().into() }); - let frames = block.layout_block(world, &self.regions, styles)?; + let frames = block.layout_block(world, styles, &self.regions)?; let len = frames.len(); for (i, frame) in frames.into_iter().enumerate() { // Grow our size, shrink the region and save the frame for later. diff --git a/library/src/layout/transform.rs b/library/src/layout/transform.rs index bee38cb4..f09b4e65 100644 --- a/library/src/layout/transform.rs +++ b/library/src/layout/transform.rs @@ -28,10 +28,10 @@ impl LayoutInline for MoveNode { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { - let mut frame = self.child.layout_inline(world, regions, styles)?; + let mut frame = self.child.layout_inline(world, styles, regions)?; let delta = self.delta.resolve(styles); let delta = delta.zip(frame.size()).map(|(d, s)| d.relative_to(s)); frame.translate(delta.to_point()); @@ -82,10 +82,10 @@ impl<const T: TransformKind> LayoutInline for TransformNode<T> { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { - let mut frame = self.child.layout_inline(world, regions, styles)?; + let mut frame = self.child.layout_inline(world, styles, regions)?; 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)); diff --git a/library/src/lib.rs b/library/src/lib.rs index 7fcad183..c2234446 100644 --- a/library/src/lib.rs +++ b/library/src/lib.rs @@ -159,7 +159,7 @@ fn styles() -> StyleMap { /// Construct the standard lang item mapping. fn items() -> LangItems { LangItems { - layout: |content, world, styles| content.layout_root(world, styles), + layout: |world, content, styles| content.layout_root(world, styles), em: |styles| styles.get(text::TextNode::SIZE), dir: |styles| styles.get(text::TextNode::DIR), space: || text::SpaceNode.pack(), diff --git a/library/src/math/mod.rs b/library/src/math/mod.rs index 1ea03d70..3b1d66e9 100644 --- a/library/src/math/mod.rs +++ b/library/src/math/mod.rs @@ -4,6 +4,8 @@ mod tex; use std::fmt::Write; +use typst::model::Guard; + use self::tex::{layout_tex, Texify}; use crate::prelude::*; use crate::text::FontFamily; @@ -28,21 +30,21 @@ impl MathNode { } impl Show for MathNode { - fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> SourceResult<Content> { + fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> Content { let mut map = StyleMap::new(); map.set_family(FontFamily::new("NewComputerModernMath"), styles); let mut realized = self .clone() .pack() - .guarded(RecipeId::Base(NodeId::of::<Self>())) + .guarded(Guard::Base(NodeId::of::<Self>())) .styled_with_map(map); if self.display { realized = realized.aligned(Axes::with_x(Some(Align::Center.into()))) } - Ok(realized) + realized } } @@ -50,10 +52,10 @@ impl LayoutInline for MathNode { fn layout_inline( &self, world: Tracked<dyn World>, - _: &Regions, styles: StyleChain, + _: &Regions, ) -> SourceResult<Frame> { - layout_tex(&self.texify(), self.display, world, styles) + layout_tex(world, &self.texify(), self.display, styles) } } diff --git a/library/src/math/tex.rs b/library/src/math/tex.rs index ffde719b..a85bab18 100644 --- a/library/src/math/tex.rs +++ b/library/src/math/tex.rs @@ -35,9 +35,9 @@ impl Texify for Content { /// Layout a TeX formula into a frame. pub fn layout_tex( + world: Tracked<dyn World>, tex: &str, display: bool, - world: Tracked<dyn World>, styles: StyleChain, ) -> SourceResult<Frame> { // Load the font. diff --git a/library/src/prelude.rs b/library/src/prelude.rs index 02d4ad10..36ff0561 100644 --- a/library/src/prelude.rs +++ b/library/src/prelude.rs @@ -16,8 +16,8 @@ pub use typst::geom::*; #[doc(no_inline)] pub use typst::model::{ array, capability, castable, dict, dynamic, format_str, node, Args, Array, Cast, - Content, Dict, Finalize, Fold, Func, Node, NodeId, RecipeId, Resolve, Show, Smart, - Str, StyleChain, StyleMap, StyleVec, Unlabellable, Value, Vm, + Content, Dict, Finalize, Fold, Func, Node, NodeId, Resolve, Show, Smart, Str, + StyleChain, StyleMap, StyleVec, Unlabellable, Value, Vm, }; #[doc(no_inline)] pub use typst::syntax::{Span, Spanned}; diff --git a/library/src/structure/doc.rs b/library/src/structure/doc.rs index 42ad628e..e471a852 100644 --- a/library/src/structure/doc.rs +++ b/library/src/structure/doc.rs @@ -18,7 +18,7 @@ impl LayoutRoot for DocNode { let mut frames = vec![]; for (page, map) in self.0.iter() { let number = 1 + frames.len(); - frames.extend(page.layout(world, number, map.chain(&styles))?); + frames.extend(page.layout(world, number, styles.chain(map))?); } Ok(frames) } diff --git a/library/src/structure/heading.rs b/library/src/structure/heading.rs index cf732a4e..063f3c97 100644 --- a/library/src/structure/heading.rs +++ b/library/src/structure/heading.rs @@ -34,18 +34,13 @@ impl HeadingNode { } impl Show for HeadingNode { - fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(BlockNode(self.body.clone()).pack()) + fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { + BlockNode(self.body.clone()).pack() } } impl Finalize for HeadingNode { - fn finalize( - &self, - _: Tracked<dyn World>, - _: StyleChain, - realized: Content, - ) -> SourceResult<Content> { + fn finalize(&self, realized: Content) -> Content { let scale = match self.level.get() { 1 => 1.4, 2 => 1.2, @@ -61,7 +56,6 @@ impl Finalize for HeadingNode { map.set(TextNode::WEIGHT, FontWeight::BOLD); map.set(BlockNode::ABOVE, VNode::block_around(above.into())); map.set(BlockNode::BELOW, VNode::block_around(below.into())); - - Ok(realized.styled_with_map(map)) + realized.styled_with_map(map) } } diff --git a/library/src/structure/list.rs b/library/src/structure/list.rs index 222dcbcf..c35ffd44 100644 --- a/library/src/structure/list.rs +++ b/library/src/structure/list.rs @@ -81,8 +81,8 @@ impl<const L: ListKind> LayoutBlock for ListNode<L> { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { let mut cells = vec![]; let mut number = 1; @@ -139,7 +139,7 @@ impl<const L: ListKind> LayoutBlock for ListNode<L> { gutter: Axes::with_y(vec![gutter.into()]), cells, } - .layout_block(world, regions, styles) + .layout_block(world, styles, regions) } } diff --git a/library/src/structure/reference.rs b/library/src/structure/reference.rs index 361f8c25..948aa6f6 100644 --- a/library/src/structure/reference.rs +++ b/library/src/structure/reference.rs @@ -20,7 +20,7 @@ impl RefNode { } impl Show for RefNode { - fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(TextNode::packed(format_eco!("@{}", self.0))) + fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { + TextNode::packed(format_eco!("@{}", self.0)) } } diff --git a/library/src/structure/table.rs b/library/src/structure/table.rs index 54413b63..4dd14cdd 100644 --- a/library/src/structure/table.rs +++ b/library/src/structure/table.rs @@ -54,8 +54,8 @@ impl LayoutBlock for TableNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { let fill = styles.get(Self::FILL); let stroke = styles.get(Self::STROKE).map(PartialStroke::unwrap_or_default); @@ -89,7 +89,7 @@ impl LayoutBlock for TableNode { gutter: self.gutter.clone(), cells, } - .layout_block(world, regions, styles) + .layout_block(world, styles, regions) } } diff --git a/library/src/text/deco.rs b/library/src/text/deco.rs index 3357f76c..33c13e90 100644 --- a/library/src/text/deco.rs +++ b/library/src/text/deco.rs @@ -47,8 +47,8 @@ impl<const L: DecoLine> DecoNode<L> { } impl<const L: DecoLine> Show for DecoNode<L> { - fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> SourceResult<Content> { - Ok(self.0.clone().styled( + fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> Content { + self.0.clone().styled( TextNode::DECO, Decoration { line: L, @@ -57,7 +57,7 @@ impl<const L: DecoLine> Show for DecoNode<L> { extent: styles.get(Self::EXTENT), evade: styles.get(Self::EVADE), }, - )) + ) } } diff --git a/library/src/text/link.rs b/library/src/text/link.rs index f682eea9..1b87def2 100644 --- a/library/src/text/link.rs +++ b/library/src/text/link.rs @@ -54,18 +54,13 @@ impl LinkNode { } impl Show for LinkNode { - fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(self.body.clone()) + fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { + self.body.clone() } } impl Finalize for LinkNode { - fn finalize( - &self, - _: Tracked<dyn World>, - _: StyleChain, - realized: Content, - ) -> SourceResult<Content> { - Ok(realized.styled(Self::DEST, Some(self.dest.clone()))) + fn finalize(&self, realized: Content) -> Content { + realized.styled(Self::DEST, Some(self.dest.clone())) } } diff --git a/library/src/text/mod.rs b/library/src/text/mod.rs index 81343210..3fd30e9e 100644 --- a/library/src/text/mod.rs +++ b/library/src/text/mod.rs @@ -527,8 +527,8 @@ impl StrongNode { } impl Show for StrongNode { - fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(self.0.clone().styled(TextNode::BOLD, Toggle)) + fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { + self.0.clone().styled(TextNode::BOLD, Toggle) } } @@ -551,8 +551,8 @@ impl EmphNode { } impl Show for EmphNode { - fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> SourceResult<Content> { - Ok(self.0.clone().styled(TextNode::ITALIC, Toggle)) + fn show(&self, _: Tracked<dyn World>, _: StyleChain) -> Content { + self.0.clone().styled(TextNode::ITALIC, Toggle) } } diff --git a/library/src/text/par.rs b/library/src/text/par.rs index 1dd6a42c..4c22c034 100644 --- a/library/src/text/par.rs +++ b/library/src/text/par.rs @@ -47,8 +47,8 @@ impl LayoutBlock for ParNode { fn layout_block( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Vec<Frame>> { // Collect all text into one string for BiDi analysis. let (text, segments) = collect(self, &styles); @@ -59,10 +59,10 @@ impl LayoutBlock for ParNode { let p = prepare(world, self, &text, segments, regions, styles)?; // Break the paragraph into lines. - let lines = linebreak(&p, world, regions.first.x); + let lines = linebreak(&p, regions.first.x); // Stack the lines into one frame per region. - stack(&p, world, &lines, regions) + stack(&p, &lines, regions) } } @@ -141,10 +141,10 @@ impl LayoutInline for RepeatNode { fn layout_inline( &self, world: Tracked<dyn World>, - regions: &Regions, styles: StyleChain, + regions: &Regions, ) -> SourceResult<Frame> { - self.0.layout_inline(world, regions, styles) + self.0.layout_inline(world, styles, regions) } } @@ -163,6 +163,8 @@ const NODE_REPLACE: char = '\u{FFFC}'; // Object Replacement Character /// Only when a line break falls onto a text index that is not safe-to-break per /// rustybuzz, we have to reshape that portion. struct Preparation<'a> { + /// The compilation environment. + world: Tracked<'a, dyn World>, /// Bidirectional text embedding levels for the paragraph. bidi: BidiInfo<'a>, /// Text runs, spacing and layouted nodes. @@ -416,7 +418,7 @@ fn collect<'a>( } while let Some((child, map)) = iter.next() { - let styles = map.chain(styles); + let styles = styles.chain(map); let segment = if child.is::<SpaceNode>() { full.push(' '); Segment::Text(1) @@ -487,7 +489,7 @@ fn collect<'a>( /// Prepare paragraph layout by shaping the whole paragraph and layouting all /// contained inline-level content. fn prepare<'a>( - world: Tracked<dyn World>, + world: Tracked<'a, dyn World>, par: &'a ParNode, text: &'a str, segments: Vec<(Segment<'a>, StyleChain<'a>)>, @@ -528,7 +530,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)?; + let mut frame = inline.layout_inline(world, styles, &pod)?; frame.translate(Point::with_y(styles.get(TextNode::BASELINE))); items.push(Item::Frame(frame)); } @@ -539,6 +541,7 @@ fn prepare<'a>( } Ok(Preparation { + world, bidi, items, styles, @@ -618,11 +621,7 @@ fn shared_get<'a, K: Key>( } /// Find suitable linebreaks. -fn linebreak<'a>( - p: &'a Preparation<'a>, - world: Tracked<dyn World>, - width: Abs, -) -> Vec<Line<'a>> { +fn linebreak<'a>(p: &'a Preparation<'a>, width: Abs) -> Vec<Line<'a>> { let linebreaks = p.styles.get(ParNode::LINEBREAKS).unwrap_or_else(|| { if p.styles.get(ParNode::JUSTIFY) { Linebreaks::Optimized @@ -632,26 +631,22 @@ fn linebreak<'a>( }); match linebreaks { - Linebreaks::Simple => linebreak_simple(p, world, width), - Linebreaks::Optimized => linebreak_optimized(p, world, width), + Linebreaks::Simple => linebreak_simple(p, width), + Linebreaks::Optimized => linebreak_optimized(p, width), } } /// Perform line breaking in simple first-fit style. This means that we build /// lines greedily, always taking the longest possible line. This may lead to /// very unbalanced line, but is fast and simple. -fn linebreak_simple<'a>( - p: &'a Preparation<'a>, - world: Tracked<dyn World>, - width: Abs, -) -> Vec<Line<'a>> { +fn linebreak_simple<'a>(p: &'a Preparation<'a>, width: Abs) -> Vec<Line<'a>> { let mut lines = vec![]; let mut start = 0; let mut last = None; for (end, mandatory, hyphen) in breakpoints(p) { // Compute the line and its size. - let mut attempt = line(p, world, start..end, mandatory, hyphen); + let mut attempt = line(p, start..end, mandatory, hyphen); // If the line doesn't fit anymore, we push the last fitting attempt // into the stack and rebuild the line from the attempt's end. The @@ -660,7 +655,7 @@ fn linebreak_simple<'a>( if let Some((last_attempt, last_end)) = last.take() { lines.push(last_attempt); start = last_end; - attempt = line(p, world, start..end, mandatory, hyphen); + attempt = line(p, start..end, mandatory, hyphen); } } @@ -700,11 +695,7 @@ fn linebreak_simple<'a>( /// computed and stored in dynamic programming table) is minimal. The final /// result is simply the layout determined for the last breakpoint at the end of /// text. -fn linebreak_optimized<'a>( - p: &'a Preparation<'a>, - world: Tracked<dyn World>, - width: Abs, -) -> Vec<Line<'a>> { +fn linebreak_optimized<'a>(p: &'a Preparation<'a>, width: Abs) -> Vec<Line<'a>> { /// The cost of a line or paragraph layout. type Cost = f64; @@ -727,7 +718,7 @@ fn linebreak_optimized<'a>( let mut table = vec![Entry { pred: 0, total: 0.0, - line: line(p, world, 0..0, false, false), + line: line(p, 0..0, false, false), }]; let em = p.styles.get(TextNode::SIZE); @@ -741,7 +732,7 @@ fn linebreak_optimized<'a>( for (i, pred) in table.iter_mut().enumerate().skip(active) { // Layout the line. let start = pred.line.end; - let attempt = line(p, world, start..end, mandatory, hyphen); + let attempt = line(p, start..end, mandatory, hyphen); // Determine how much the line's spaces would need to be stretched // to make it the desired width. @@ -915,7 +906,6 @@ impl Breakpoints<'_> { /// Create a line which spans the given range. fn line<'a>( p: &'a Preparation, - world: Tracked<dyn World>, mut range: Range, mandatory: bool, hyphen: bool, @@ -970,9 +960,9 @@ fn line<'a>( if hyphen || start + shaped.text.len() > range.end { if hyphen || start < range.end || before.is_empty() { let shifted = start - base..range.end - base; - let mut reshaped = shaped.reshape(world, shifted); + let mut reshaped = shaped.reshape(p.world, shifted); if hyphen || shy { - reshaped.push_hyphen(world); + reshaped.push_hyphen(p.world); } width += reshaped.width; last = Some(Item::Text(reshaped)); @@ -993,7 +983,7 @@ fn line<'a>( if range.start + shaped.text.len() > end { if range.start < end { let shifted = range.start - base..end - base; - let reshaped = shaped.reshape(world, shifted); + let reshaped = shaped.reshape(p.world, shifted); width += reshaped.width; first = Some(Item::Text(reshaped)); } @@ -1021,12 +1011,7 @@ fn line<'a>( } /// Combine layouted lines into one frame per region. -fn stack( - p: &Preparation, - world: Tracked<dyn World>, - lines: &[Line], - regions: &Regions, -) -> SourceResult<Vec<Frame>> { +fn stack(p: &Preparation, lines: &[Line], regions: &Regions) -> SourceResult<Vec<Frame>> { // Determine the paragraph's width: Full width of the region if we // should expand or there's fractional spacing, fit-to-width otherwise. let mut width = regions.first.x; @@ -1042,7 +1027,7 @@ fn stack( // Stack the lines into one frame per region. for line in lines { - let frame = commit(p, world, line, ®ions, width)?; + let frame = commit(p, line, ®ions, width)?; let height = frame.size().y; while !regions.first.y.fits(height) && !regions.in_last() { @@ -1071,7 +1056,6 @@ fn stack( /// Commit to a line and build its frame. fn commit( p: &Preparation, - world: Tracked<dyn World>, line: &Line, regions: &Regions, width: Abs, @@ -1142,7 +1126,7 @@ fn commit( offset += v.share(fr, remaining); } Item::Text(shaped) => { - let frame = shaped.build(world, justification); + let frame = shaped.build(p.world, justification); push(&mut offset, frame); } Item::Frame(frame) => { @@ -1153,7 +1137,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)?; + let frame = repeat.layout_inline(p.world, *styles, &pod)?; let width = frame.width(); let count = (fill / width).floor(); let remaining = fill % width; diff --git a/library/src/text/raw.rs b/library/src/text/raw.rs index e47875dc..6c726513 100644 --- a/library/src/text/raw.rs +++ b/library/src/text/raw.rs @@ -43,7 +43,7 @@ impl RawNode { } impl Show for RawNode { - fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> SourceResult<Content> { + fn show(&self, _: Tracked<dyn World>, styles: StyleChain) -> Content { let lang = styles.get(Self::LANG).as_ref().map(|s| s.to_lowercase()); let foreground = THEME .settings @@ -96,7 +96,7 @@ impl Show for RawNode { map.set(TextNode::SMART_QUOTES, false); map.set_family(FontFamily::new("IBM Plex Mono"), styles); - Ok(realized.styled_with_map(map)) + realized.styled_with_map(map) } } diff --git a/library/src/text/shift.rs b/library/src/text/shift.rs index b05f68b7..df8ec5e6 100644 --- a/library/src/text/shift.rs +++ b/library/src/text/shift.rs @@ -43,11 +43,7 @@ impl<const S: ShiftKind> ShiftNode<S> { } impl<const S: ShiftKind> Show for ShiftNode<S> { - fn show( - &self, - world: Tracked<dyn World>, - styles: StyleChain, - ) -> SourceResult<Content> { + fn show(&self, world: Tracked<dyn World>, styles: StyleChain) -> Content { let mut transformed = None; if styles.get(Self::TYPOGRAPHIC) { if let Some(text) = search_text(&self.0, S) { @@ -57,12 +53,12 @@ impl<const S: ShiftKind> Show for ShiftNode<S> { } }; - Ok(transformed.unwrap_or_else(|| { + transformed.unwrap_or_else(|| { let mut map = StyleMap::new(); map.set(TextNode::BASELINE, styles.get(Self::BASELINE)); map.set(TextNode::SIZE, styles.get(Self::SIZE)); self.0.clone().styled_with_map(map) - })) + }) } } |
