diff --git a/Cargo.lock b/Cargo.lock index a1024fd35..c7e8b0099 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -537,7 +537,7 @@ checksum = "db8bcd96cb740d03149cbad5518db9fd87126a10ab519c011893b1754134c468" [[package]] name = "pixglyph" version = "0.1.0" -source = "git+https://github.com/typst/pixglyph#da648abb60d0e0f4353cd7602652c832132e6a39" +source = "git+https://github.com/typst/pixglyph#8ee0d4517d887125e9184916780ac230e40a042a" dependencies = [ "ttf-parser", ] diff --git a/benches/oneshot.rs b/benches/oneshot.rs index 92721013b..5dbf993fe 100644 --- a/benches/oneshot.rs +++ b/benches/oneshot.rs @@ -77,7 +77,7 @@ fn bench_layout(iai: &mut Iai) { let (mut ctx, id) = context(); let mut vm = Vm::new(&mut ctx); let module = vm.evaluate(id).unwrap(); - iai.run(|| module.template.layout(&mut vm)); + iai.run(|| module.template.layout_pages(&mut vm)); } fn bench_highlight(iai: &mut Iai) { diff --git a/src/eval/func.rs b/src/eval/func.rs index 9a8a9c94d..128509f85 100644 --- a/src/eval/func.rs +++ b/src/eval/func.rs @@ -167,6 +167,21 @@ pub struct Arg { } impl Args { + /// Create positional arguments from a span and values. + pub fn from_values(span: Span, values: impl IntoIterator) -> Self { + Self { + span, + items: values + .into_iter() + .map(|value| Arg { + span, + name: None, + value: Spanned::new(value, span), + }) + .collect(), + } + } + /// Consume and cast the first positional argument. /// /// Returns a `missing argument: {what}` error if no positional argument is diff --git a/src/eval/template.rs b/src/eval/template.rs index 689744524..1f1544e64 100644 --- a/src/eval/template.rs +++ b/src/eval/template.rs @@ -169,7 +169,7 @@ impl Template { } /// Layout this template into a collection of pages. - pub fn layout(&self, vm: &mut Vm) -> TypResult>> { + pub fn layout_pages(&self, vm: &mut Vm) -> TypResult>> { let sya = Arena::new(); let tpa = Arena::new(); @@ -180,8 +180,10 @@ impl Template { let mut frames = vec![]; let (pages, shared) = builder.pages.unwrap().finish(); + for (page, map) in pages.iter() { - frames.extend(page.layout(vm, map.chain(&shared))?); + let number = 1 + frames.len(); + frames.extend(page.layout(vm, number, map.chain(&shared))?); } Ok(frames) diff --git a/src/lib.rs b/src/lib.rs index 46bcad8d8..6ad30d2f6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -283,7 +283,7 @@ impl<'a> Vm<'a> { /// diagnostics in the form of a vector of error message with file and span /// information. pub fn typeset(&mut self, id: SourceId) -> TypResult>> { - self.evaluate(id)?.template.layout(self) + self.evaluate(id)?.template.layout_pages(self) } /// Resolve a user-entered path (relative to the source file) to be diff --git a/src/library/heading.rs b/src/library/heading.rs index 3438c7b78..1b8fcef90 100644 --- a/src/library/heading.rs +++ b/src/library/heading.rs @@ -117,14 +117,7 @@ impl Leveled { Self::Value(value) => value, Self::Mapping(mapping) => mapping(level), Self::Func(func, span) => { - let args = Args { - span, - items: vec![Arg { - span, - name: None, - value: Spanned::new(Value::Int(level as i64), span), - }], - }; + let args = Args::from_values(span, [Value::Int(level as i64)]); func.call(vm, args)?.cast().at(span)? } }) diff --git a/src/library/list.rs b/src/library/list.rs index fe499cb19..13f21a047 100644 --- a/src/library/list.rs +++ b/src/library/list.rs @@ -146,14 +146,7 @@ impl Label { Self::Template(template) => template.clone(), Self::Mapping(mapping) => mapping(number), &Self::Func(ref func, span) => { - let args = Args { - span, - items: vec![Arg { - span, - name: None, - value: Spanned::new(Value::Int(number as i64), span), - }], - }; + let args = Args::from_values(span, [Value::Int(number as i64)]); func.call(vm, args)?.cast().at(span)? } }) diff --git a/src/library/pad.rs b/src/library/pad.rs index 766476450..ca45a1cae 100644 --- a/src/library/pad.rs +++ b/src/library/pad.rs @@ -15,18 +15,14 @@ pub struct PadNode { impl PadNode { fn construct(_: &mut Vm, args: &mut Args) -> TypResult