New page margins API

This commit is contained in:
Martin Haug 2022-05-02 18:48:32 +02:00
parent 9b4397cdab
commit 33213abe7d
7 changed files with 32 additions and 36 deletions

View File

@ -18,14 +18,9 @@ impl PageNode {
/// Whether the page is flipped into landscape orientation. /// Whether the page is flipped into landscape orientation.
pub const FLIPPED: bool = false; pub const FLIPPED: bool = false;
/// The left margin. /// The page margin.
pub const LEFT: Smart<Relative<RawLength>> = Smart::Auto; #[property(fold)]
/// The right margin. pub const MARGINS: Sides<Smart<Relative<RawLength>>> = Sides::splat(Smart::Auto);
pub const RIGHT: Smart<Relative<RawLength>> = Smart::Auto;
/// The top margin.
pub const TOP: Smart<Relative<RawLength>> = Smart::Auto;
/// The bottom margin.
pub const BOTTOM: Smart<Relative<RawLength>> = Smart::Auto;
/// How many columns the page has. /// How many columns the page has.
pub const COLUMNS: NonZeroUsize = NonZeroUsize::new(1).unwrap(); pub const COLUMNS: NonZeroUsize = NonZeroUsize::new(1).unwrap();
@ -54,13 +49,7 @@ impl PageNode {
styles.set_opt(Self::WIDTH, args.named("width")?); styles.set_opt(Self::WIDTH, args.named("width")?);
styles.set_opt(Self::HEIGHT, args.named("height")?); styles.set_opt(Self::HEIGHT, args.named("height")?);
let all = args.named("margins")?; styles.set_opt(Self::MARGINS, args.named("margins")?);
let hor = args.named("horizontal")?;
let ver = args.named("vertical")?;
styles.set_opt(Self::LEFT, args.named("left")?.or(hor).or(all));
styles.set_opt(Self::TOP, args.named("top")?.or(ver).or(all));
styles.set_opt(Self::RIGHT, args.named("right")?.or(hor).or(all));
styles.set_opt(Self::BOTTOM, args.named("bottom")?.or(ver).or(all));
styles.set_opt(Self::FLIPPED, args.named("flipped")?); styles.set_opt(Self::FLIPPED, args.named("flipped")?);
styles.set_opt(Self::FILL, args.named("fill")?); styles.set_opt(Self::FILL, args.named("fill")?);
@ -96,12 +85,7 @@ impl PageNode {
// Determine the margins. // Determine the margins.
let default = Relative::from(0.1190 * min); let default = Relative::from(0.1190 * min);
let padding = Sides { let padding = styles.get(Self::MARGINS).map(|side| side.unwrap_or(default));
left: styles.get(Self::LEFT).unwrap_or(default),
right: styles.get(Self::RIGHT).unwrap_or(default),
top: styles.get(Self::TOP).unwrap_or(default),
bottom: styles.get(Self::BOTTOM).unwrap_or(default),
};
let mut child = self.0.clone(); let mut child = self.0.clone();

View File

@ -7,7 +7,7 @@ use std::sync::Arc;
use super::{Content, Show, ShowNode}; use super::{Content, Show, ShowNode};
use crate::diag::{At, TypResult}; use crate::diag::{At, TypResult};
use crate::eval::{Args, Func, Node, Smart, Value}; use crate::eval::{Args, Func, Node, RawLength, Smart, Value};
use crate::geom::{Length, Numeric, Relative, Sides, Spec}; use crate::geom::{Length, Numeric, Relative, Sides, Spec};
use crate::library::layout::PageNode; use crate::library::layout::PageNode;
use crate::library::structure::{EnumNode, ListNode}; use crate::library::structure::{EnumNode, ListNode};
@ -488,6 +488,19 @@ impl Fold for Sides<Option<Relative<Length>>> {
} }
} }
impl Fold for Sides<Smart<Relative<RawLength>>> {
type Output = Sides<Smart<Relative<RawLength>>>;
fn fold(self, outer: Self::Output) -> Self::Output {
Sides {
left: self.left.or(outer.left),
top: self.top.or(outer.top),
right: self.right.or(outer.right),
bottom: self.bottom.or(outer.bottom),
}
}
}
/// A scoped property barrier. /// A scoped property barrier.
/// ///
/// Barriers interact with [scoped](StyleMap::scoped) styles: A scoped style /// Barriers interact with [scoped](StyleMap::scoped) styles: A scoped style

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.2 KiB

After

Width:  |  Height:  |  Size: 7.2 KiB

View File

@ -11,10 +11,10 @@
--- ---
// Set individual margins. // Set individual margins.
#set page(height: 40pt) #set page(height: 40pt)
[#set page(left: 0pt); #align(left)[Left]] [#set page(margins: (left: 0pt,)); #align(left)[Left]]
[#set page(right: 0pt); #align(right)[Right]] [#set page(margins: (right: 0pt,)); #align(right)[Right]]
[#set page(top: 0pt); #align(top)[Top]] [#set page(margins: (top: 0pt,)); #align(top)[Top]]
[#set page(bottom: 0pt); #align(bottom)[Bottom]] [#set page(margins: (bottom: 0pt,)); #align(bottom)[Bottom]]
// Ensure that specific margins override general margins. // Ensure that specific margins override general margins.
[#set page(margins: 0pt, left: 20pt); Overriden] [#set page(margins: (rest: 0pt, left: 20pt)); Overriden]

View File

@ -1,7 +1,6 @@
#set page( #set page(
paper: "a8", paper: "a8",
margins: 30pt, margins: (x: 15pt, y: 30pt),
horizontal: 15pt,
header: align(horizon, { header: align(horizon, {
text(eastern)[*Typst*] text(eastern)[*Typst*]
h(1fr) h(1fr)
@ -18,5 +17,5 @@ do wear it; cast it off. It is my lady, O, it is my love! O, that she knew she
were! She speaks yet she says nothing: what of that? Her eye discourses; I will were! She speaks yet she says nothing: what of that? Her eye discourses; I will
answer it. answer it.
#set page(header: none, height: auto, top: 15pt, bottom: 25pt) #set page(header: none, height: auto, margins: (top: 15pt, bottom: 25pt))
The END. The END.

View File

@ -24,7 +24,7 @@
// Test page fill. // Test page fill.
#set page(width: 80pt, height: 40pt, fill: eastern) #set page(width: 80pt, height: 40pt, fill: eastern)
#text(15pt, "Roboto", fill: white, smallcaps: true)[Typst] #text(15pt, "Roboto", fill: white, smallcaps: true)[Typst]
#page(width: 40pt, fill: none, margins: auto, top: 10pt)[Hi] #page(width: 40pt, fill: none, margins: (top: 10pt, rest: auto))[Hi]
--- ---
// Just page followed by pagebreak. // Just page followed by pagebreak.

View File

@ -12,7 +12,7 @@ use walkdir::WalkDir;
use typst::diag::Error; use typst::diag::Error;
use typst::eval::{Smart, Value}; use typst::eval::{Smart, Value};
use typst::frame::{Element, Frame}; use typst::frame::{Element, Frame};
use typst::geom::{Length, RgbaColor}; use typst::geom::{Length, RgbaColor, Sides};
use typst::library::layout::PageNode; use typst::library::layout::PageNode;
use typst::library::text::{TextNode, TextSize}; use typst::library::text::{TextNode, TextSize};
use typst::loading::FsLoader; use typst::loading::FsLoader;
@ -64,10 +64,10 @@ fn main() {
let mut styles = StyleMap::new(); let mut styles = StyleMap::new();
styles.set(PageNode::WIDTH, Smart::Custom(Length::pt(120.0).into())); styles.set(PageNode::WIDTH, Smart::Custom(Length::pt(120.0).into()));
styles.set(PageNode::HEIGHT, Smart::Auto); styles.set(PageNode::HEIGHT, Smart::Auto);
styles.set(PageNode::LEFT, Smart::Custom(Length::pt(10.0).into())); styles.set(
styles.set(PageNode::TOP, Smart::Custom(Length::pt(10.0).into())); PageNode::MARGINS,
styles.set(PageNode::RIGHT, Smart::Custom(Length::pt(10.0).into())); Sides::splat(Smart::Custom(Length::pt(10.0).into())),
styles.set(PageNode::BOTTOM, Smart::Custom(Length::pt(10.0).into())); );
styles.set(TextNode::SIZE, TextSize(Length::pt(10.0).into())); styles.set(TextNode::SIZE, TextSize(Length::pt(10.0).into()));
// Hook up an assert function into the global scope. // Hook up an assert function into the global scope.