Apply simple suggestions from code review

Co-Authored-By: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
Martin 2021-12-26 17:34:04 +01:00 committed by Martin Haug
parent b22ce6f8b8
commit 7efdcdf247
7 changed files with 32 additions and 22 deletions

View File

@ -354,8 +354,8 @@ impl Packer {
// Take the flow and erase any styles that will be inherited anyway. // Take the flow and erase any styles that will be inherited anyway.
let Builder { mut children, styles, .. } = mem::take(&mut self.flow); let Builder { mut children, styles, .. } = mem::take(&mut self.flow);
for child in &mut children { for local in children.iter_mut().filter_map(FlowChild::styles_mut) {
child.styles_mut().map(|s| s.erase(&styles)); local.erase(&styles);
} }
let flow = FlowNode(children).pack(); let flow = FlowNode(children).pack();

View File

@ -3,12 +3,11 @@ use super::ParNode;
/// `columns`: Stack children along an axis. /// `columns`: Stack children along an axis.
pub fn columns(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> { pub fn columns(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let count = args.expect("column count")?; let columns = args.expect("column count")?;
let gutter = args.named("gutter")?.unwrap_or(Relative::new(0.04).into()); let gutter = args.named("gutter")?.unwrap_or(Relative::new(0.04).into());
let body: Node = args.expect("body")?; let body: Node = args.expect("body")?;
Ok(Value::block(ColumnsNode { Ok(Value::block(ColumnsNode {
columns: count, columns,
gutter, gutter,
child: body.into_block(), child: body.into_block(),
})) }))
@ -23,7 +22,7 @@ pub fn colbreak(_: &mut EvalContext, _: &mut Args) -> TypResult<Value> {
#[derive(Debug, Hash)] #[derive(Debug, Hash)]
pub struct ColumnsNode { pub struct ColumnsNode {
/// How many columns there should be. /// How many columns there should be.
pub columns: usize, pub columns: NonZeroUsize,
/// The size of the gutter space between each column. /// The size of the gutter space between each column.
pub gutter: Linear, pub gutter: Linear,
/// The child to be layouted into the columns. Most likely, this should be a /// The child to be layouted into the columns. Most likely, this should be a
@ -50,11 +49,10 @@ impl Layout for ColumnsNode {
// `region.backlog`. // `region.backlog`.
let mut sizes = vec![]; let mut sizes = vec![];
// Assure there is at least one column. let columns = self.columns.get();
let columns = self.columns.max(1);
for (current, base) in std::iter::once((regions.current, regions.base)) for (current, base) in std::iter::once((regions.current, regions.base))
.chain(regions.backlog.clone().into_iter().map(|s| (s, s))) .chain(regions.backlog.as_slice().iter().map(|&s| (s, s)))
{ {
let gutter = self.gutter.resolve(base.x); let gutter = self.gutter.resolve(base.x);
gutters.push(gutter); gutters.push(gutter);
@ -68,8 +66,9 @@ impl Layout for ColumnsNode {
} }
let first = sizes.remove(0); let first = sizes.remove(0);
let mut col_regions = Regions::one(first, first, regions.expand); let mut pod =
col_regions.backlog = sizes.clone().into_iter(); Regions::one(first, Spec::new(first.x, regions.base.y), regions.expand);
pod.backlog = sizes.clone().into_iter();
// We have to treat the last region separately. // We have to treat the last region separately.
let last_column_gutter = regions.last.map(|last| { let last_column_gutter = regions.last.map(|last| {
@ -78,11 +77,11 @@ impl Layout for ColumnsNode {
(last.x - gutter * (columns - 1) as f64) / columns as f64, (last.x - gutter * (columns - 1) as f64) / columns as f64,
last.y, last.y,
); );
col_regions.last = Some(size); pod.last = Some(size);
(size, gutter) (size, gutter)
}); });
let frames = self.child.layout(ctx, &col_regions); let frames = self.child.layout(ctx, &pod);
let dir = ctx.styles.get(ParNode::DIR); let dir = ctx.styles.get(ParNode::DIR);
// Dealing with infinite height areas here. // Dealing with infinite height areas here.

View File

@ -32,12 +32,12 @@ impl Debug for FlowNode {
pub enum FlowChild { pub enum FlowChild {
/// A paragraph/block break. /// A paragraph/block break.
Break(Styles), Break(Styles),
/// Skip the rest of the region and move to the next.
Skip,
/// Vertical spacing between other children. /// Vertical spacing between other children.
Spacing(SpacingNode), Spacing(SpacingNode),
/// An arbitrary node. /// An arbitrary node.
Node(PackedNode), Node(PackedNode),
/// Skip the rest of the region and move to the next.
Skip,
} }
impl FlowChild { impl FlowChild {
@ -73,7 +73,7 @@ impl Debug for FlowChild {
} }
Self::Spacing(node) => node.fmt(f), Self::Spacing(node) => node.fmt(f),
Self::Node(node) => node.fmt(f), Self::Node(node) => node.fmt(f),
Self::Skip => write!(f, "Skip"), Self::Skip => f.pad("Skip"),
} }
} }
} }

View File

@ -26,6 +26,7 @@ mod utility;
/// Helpful imports for creating library functionality. /// Helpful imports for creating library functionality.
mod prelude { mod prelude {
pub use std::fmt::{self, Debug, Formatter}; pub use std::fmt::{self, Debug, Formatter};
pub use std::num::NonZeroUsize;
pub use std::rc::Rc; pub use std::rc::Rc;
pub use typst_macros::properties; pub use typst_macros::properties;
@ -171,6 +172,15 @@ castable! {
Value::Int(int) => int.try_into().map_err(|_| "must be at least zero")?, Value::Int(int) => int.try_into().map_err(|_| "must be at least zero")?,
} }
castable! {
prelude::NonZeroUsize,
Expected: "positive integer",
Value::Int(int) => int
.try_into()
.and_then(|n: usize| n.try_into())
.map_err(|_| "must be positive")?,
}
castable! { castable! {
String, String,
Expected: "string", Expected: "string",

View File

@ -41,7 +41,7 @@ impl PageNode {
/// The page's background color. /// The page's background color.
pub const FILL: Option<Paint> = None; pub const FILL: Option<Paint> = None;
/// How many columns the page has. /// How many columns the page has.
pub const COLUMNS: usize = 1; pub const COLUMNS: NonZeroUsize = NonZeroUsize::new(1).unwrap();
/// How many columns the page has. /// How many columns the page has.
pub const COLUMN_GUTTER: Linear = Relative::new(0.04).into(); pub const COLUMN_GUTTER: Linear = Relative::new(0.04).into();
} }
@ -119,11 +119,11 @@ impl PageNode {
}; };
let columns = ctx.styles.get(Self::COLUMNS); let columns = ctx.styles.get(Self::COLUMNS);
let child = if ctx.styles.get(Self::COLUMNS) > 1 { let child = if columns.get() > 1 {
ColumnsNode { ColumnsNode {
child: self.child.clone(),
columns, columns,
gutter: ctx.styles.get(Self::COLUMN_GUTTER), gutter: ctx.styles.get(Self::COLUMN_GUTTER),
child: self.child.clone(),
} }
.pack() .pack()
} else { } else {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 37 KiB

View File

@ -48,6 +48,7 @@ a page for a test but it does get the job done.
إلى جانب كل من البروتينات والليبيدات والسكريات المتعددة إلى جانب كل من البروتينات والليبيدات والسكريات المتعددة
#rect(fill: eastern, height: 8pt, width: 6pt) #rect(fill: eastern, height: 8pt, width: 6pt)
الجزيئات الضخمة الأربعة الضرورية للحياة. الجزيئات الضخمة الأربعة الضرورية للحياة.
--- ---
// Test the `colbreak` function. // Test the `colbreak` function.
#set page(height: 1cm, width: 7.05cm, columns: 2) #set page(height: 1cm, width: 7.05cm, columns: 2)
@ -69,7 +70,7 @@ C
// Test columns when one of them is empty. // Test columns when one of them is empty.
#set page(width: auto, columns: 3) #set page(width: auto, columns: 3)
The page can grow as much as it wants horizontally. Arbitrary horizontal growth.
--- ---
// Test columns in an infinitely wide frame. // Test columns in an infinitely wide frame.
@ -91,7 +92,7 @@ This is a normal page. Very normal.
--- ---
// Test a page with zero columns. // Test a page with zero columns.
// Error: 49-50 must be positive
#set page(height: auto, width: 7.05cm, columns: 0) #set page(height: auto, width: 7.05cm, columns: 0)
This makes less sense but will still This makes less sense.
produce a normal page.