summaryrefslogtreecommitdiff
path: root/library/src/meta/document.rs
blob: 309e1bda70589be83341c5312088ac36391d17c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::layout::{LayoutRoot, PageNode};
use crate::prelude::*;

/// The root node that represents a full document.
#[derive(Hash)]
pub struct DocumentNode(pub StyleVec<PageNode>);

#[node(LayoutRoot)]
impl DocumentNode {
    /// The document's title.
    #[property(referenced)]
    pub const TITLE: Option<EcoString> = None;

    /// The document's author.
    #[property(referenced)]
    pub const AUTHOR: Option<EcoString> = None;
}

impl LayoutRoot for DocumentNode {
    /// Layout the document into a sequence of frames, one per page.
    fn layout_root(
        &self,
        world: Tracked<dyn World>,
        styles: StyleChain,
    ) -> SourceResult<Document> {
        let mut pages = vec![];
        for (page, map) in self.0.iter() {
            let number = 1 + pages.len();
            let fragment = page.layout(world, number, styles.chain(map))?;
            pages.extend(fragment);
        }

        Ok(Document {
            metadata: Metadata {
                title: styles.get(Self::TITLE).clone(),
                author: styles.get(Self::AUTHOR).clone(),
            },
            pages,
        })
    }
}

impl Debug for DocumentNode {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.write_str("Document ")?;
        self.0.fmt(f)
    }
}