mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
76 lines
2.0 KiB
Rust
76 lines
2.0 KiB
Rust
use super::prelude::*;
|
|
use crate::style::{Paper, PaperClass};
|
|
|
|
/// `page`: Configure pages.
|
|
pub fn page(ctx: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
|
|
castable! {
|
|
Paper,
|
|
Expected: "string",
|
|
Value::Str(string) => Paper::from_name(&string).ok_or("unknown paper")?,
|
|
}
|
|
|
|
let paper = args.named::<Paper>("paper")?.or_else(|| args.find());
|
|
let width = args.named("width")?;
|
|
let height = args.named("height")?;
|
|
let margins = args.named("margins")?;
|
|
let left = args.named("left")?;
|
|
let top = args.named("top")?;
|
|
let right = args.named("right")?;
|
|
let bottom = args.named("bottom")?;
|
|
let flip = args.named("flip")?;
|
|
|
|
ctx.template.modify(move |style| {
|
|
let page = style.page_mut();
|
|
|
|
if let Some(paper) = paper {
|
|
page.class = paper.class();
|
|
page.size = paper.size();
|
|
}
|
|
|
|
if let Some(width) = width {
|
|
page.class = PaperClass::Custom;
|
|
page.size.w = width;
|
|
}
|
|
|
|
if let Some(height) = height {
|
|
page.class = PaperClass::Custom;
|
|
page.size.h = height;
|
|
}
|
|
|
|
if let Some(margins) = margins {
|
|
page.margins = Sides::splat(Some(margins));
|
|
}
|
|
|
|
if let Some(left) = left {
|
|
page.margins.left = Some(left);
|
|
}
|
|
|
|
if let Some(top) = top {
|
|
page.margins.top = Some(top);
|
|
}
|
|
|
|
if let Some(right) = right {
|
|
page.margins.right = Some(right);
|
|
}
|
|
|
|
if let Some(bottom) = bottom {
|
|
page.margins.bottom = Some(bottom);
|
|
}
|
|
|
|
if flip.unwrap_or(false) {
|
|
std::mem::swap(&mut page.size.w, &mut page.size.h);
|
|
}
|
|
});
|
|
|
|
ctx.template.pagebreak(false);
|
|
|
|
Ok(Value::None)
|
|
}
|
|
|
|
/// `pagebreak`: Start a new page.
|
|
pub fn pagebreak(_: &mut EvalContext, _: &mut Args) -> TypResult<Value> {
|
|
let mut template = Template::new();
|
|
template.pagebreak(true);
|
|
Ok(Value::Template(template))
|
|
}
|