From 5becb32ba463d6b0ace914ab06bb237483a94fbc Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 17 Oct 2021 14:38:48 +0200 Subject: [PATCH] Introduce page / block / inline levels --- benches/oneshot.rs | 2 +- src/eval/array.rs | 6 +- src/eval/dict.rs | 6 +- src/eval/template.rs | 50 +++---- src/eval/walk.rs | 10 +- src/layout/constraints.rs | 2 +- src/layout/frame.rs | 208 +++++++++++++-------------- src/layout/grid.rs | 14 +- src/layout/image.rs | 28 ++-- src/layout/incremental.rs | 4 +- src/layout/mod.rs | 218 +++++++++++++++++++++++++++-- src/layout/pad.rs | 26 ++-- src/layout/par.rs | 76 +++++----- src/layout/shape.rs | 64 +++------ src/layout/spacing.rs | 50 ------- src/layout/stack.rs | 77 +++++----- src/layout/{shaping.rs => text.rs} | 13 +- src/layout/tree.rs | 132 ----------------- src/lib.rs | 11 +- src/library/layout.rs | 8 +- src/library/text.rs | 12 +- src/style/mod.rs | 14 +- src/util/eco_string.rs | 7 +- src/util/mod.rs | 20 +++ tests/typeset.rs | 4 +- 25 files changed, 530 insertions(+), 532 deletions(-) delete mode 100644 src/layout/spacing.rs rename src/layout/{shaping.rs => text.rs} (98%) delete mode 100644 src/layout/tree.rs diff --git a/benches/oneshot.rs b/benches/oneshot.rs index 57e3b339d..6ce81a639 100644 --- a/benches/oneshot.rs +++ b/benches/oneshot.rs @@ -60,7 +60,7 @@ fn bench_eval(iai: &mut Iai) { fn bench_to_tree(iai: &mut Iai) { let (mut ctx, id) = context(); let module = ctx.evaluate(id).unwrap(); - iai.run(|| module.template.to_tree(ctx.style())); + iai.run(|| module.template.to_pages(ctx.style())); } fn bench_layout(iai: &mut Iai) { diff --git a/src/eval/array.rs b/src/eval/array.rs index 17192cb34..f6adee6d8 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -7,6 +7,7 @@ use std::rc::Rc; use super::Value; use crate::diag::StrResult; +use crate::util::RcExt; /// Create a new [`Array`] from values. #[allow(unused_macros)] @@ -169,10 +170,7 @@ impl IntoIterator for Array { type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { - match Rc::try_unwrap(self.0) { - Ok(vec) => vec.into_iter(), - Err(rc) => (*rc).clone().into_iter(), - } + Rc::take(self.0).into_iter() } } diff --git a/src/eval/dict.rs b/src/eval/dict.rs index c0ddf3289..e7a46b400 100644 --- a/src/eval/dict.rs +++ b/src/eval/dict.rs @@ -6,6 +6,7 @@ use std::rc::Rc; use super::{Str, Value}; use crate::diag::StrResult; +use crate::util::RcExt; /// Create a new [`Dict`] from key-value pairs. #[allow(unused_macros)] @@ -135,10 +136,7 @@ impl IntoIterator for Dict { type IntoIter = std::collections::btree_map::IntoIter; fn into_iter(self) -> Self::IntoIter { - match Rc::try_unwrap(self.0) { - Ok(map) => map.into_iter(), - Err(rc) => (*rc).clone().into_iter(), - } + Rc::take(self.0).into_iter() } } diff --git a/src/eval/template.rs b/src/eval/template.rs index 639163394..11ea3f56c 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -6,9 +6,9 @@ use std::rc::Rc; use super::Str; use crate::diag::StrResult; -use crate::geom::{Align, Dir, GenAxis, Length, Linear, Sides, Size, SpecAxis}; +use crate::geom::{Align, Dir, GenAxis, Length, Linear, Sides, Size}; use crate::layout::{ - Decoration, LayoutNode, LayoutTree, PadNode, PageRun, ParChild, ParNode, StackChild, + BlockNode, Decoration, InlineNode, PadNode, PageNode, ParChild, ParNode, StackChild, StackNode, }; use crate::style::Style; @@ -34,9 +34,9 @@ enum TemplateNode { /// Spacing. Spacing(GenAxis, Linear), /// An inline node builder. - Inline(Rc LayoutNode>, Vec), + Inline(Rc InlineNode>, Vec), /// An block node builder. - Block(Rc LayoutNode>), + Block(Rc BlockNode>), /// Save the current style. Save, /// Restore the last saved style. @@ -55,7 +55,7 @@ impl Template { pub fn from_inline(f: F) -> Self where F: Fn(&Style) -> T + 'static, - T: Into, + T: Into, { let node = TemplateNode::Inline(Rc::new(move |s| f(s).into()), vec![]); Self(Rc::new(vec![node])) @@ -65,7 +65,7 @@ impl Template { pub fn from_block(f: F) -> Self where F: Fn(&Style) -> T + 'static, - T: Into, + T: Into, { let node = TemplateNode::Block(Rc::new(move |s| f(s).into())); Self(Rc::new(vec![node])) @@ -164,10 +164,10 @@ impl Template { /// Build the layout tree resulting from instantiating the template with the /// given style. - pub fn to_tree(&self, style: &Style) -> LayoutTree { + pub fn to_pages(&self, style: &Style) -> Vec { let mut builder = Builder::new(style, true); builder.template(self); - builder.build_tree() + builder.build_pages() } /// Repeat this template `n` times. @@ -243,8 +243,8 @@ struct Builder { style: Style, /// Snapshots of the style. snapshots: Vec