diff options
| author | Laurenz <laurmaedje@gmail.com> | 2024-01-16 10:24:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-16 09:24:36 +0000 |
| commit | c2dfbd39a024f277f7a18775590f135ff7da074f (patch) | |
| tree | 5da5ea13e282fbc77f47372c45265a314090e916 /crates/typst-pdf | |
| parent | ad901c2cdbcb421355f5ecef5dc86c14de293c9a (diff) | |
Migrate metadata fields out of individual elements (#3200)
Diffstat (limited to 'crates/typst-pdf')
| -rw-r--r-- | crates/typst-pdf/src/outline.rs | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/crates/typst-pdf/src/outline.rs b/crates/typst-pdf/src/outline.rs index 78698cf8..172f6d3c 100644 --- a/crates/typst-pdf/src/outline.rs +++ b/crates/typst-pdf/src/outline.rs @@ -1,7 +1,7 @@ use std::num::NonZeroUsize; use pdf_writer::{Finish, Ref, TextStr}; -use typst::foundations::{Content, NativeElement, Smart}; +use typst::foundations::{NativeElement, Packed, StyleChain}; use typst::layout::Abs; use typst::model::HeadingElem; @@ -17,8 +17,10 @@ pub(crate) fn write_outline(ctx: &mut PdfContext) -> Option<Ref> { // Therefore, its next descendant must be added at its level, which is // enforced in the manner shown below. let mut last_skipped_level = None; - for heading in ctx.document.introspector.query(&HeadingElem::elem().select()).iter() { - let leaf = HeadingNode::leaf((**heading).clone()); + let elements = ctx.document.introspector.query(&HeadingElem::elem().select()); + for elem in elements.iter() { + let heading = elem.to::<HeadingElem>().unwrap(); + let leaf = HeadingNode::leaf(heading); if leaf.bookmarked { let mut children = &mut tree; @@ -103,21 +105,21 @@ pub(crate) fn write_outline(ctx: &mut PdfContext) -> Option<Ref> { /// A heading in the outline panel. #[derive(Debug, Clone)] -struct HeadingNode { - element: Content, +struct HeadingNode<'a> { + element: &'a Packed<HeadingElem>, level: NonZeroUsize, bookmarked: bool, - children: Vec<HeadingNode>, + children: Vec<HeadingNode<'a>>, } -impl HeadingNode { - fn leaf(element: Content) -> Self { +impl<'a> HeadingNode<'a> { + fn leaf(element: &'a Packed<HeadingElem>) -> Self { HeadingNode { - level: element.expect_field_by_name::<NonZeroUsize>("level"), + level: element.level(StyleChain::default()), // 'bookmarked' set to 'auto' falls back to the value of 'outlined'. bookmarked: element - .expect_field_by_name::<Smart<bool>>("bookmarked") - .unwrap_or_else(|| element.expect_field_by_name::<bool>("outlined")), + .bookmarked(StyleChain::default()) + .unwrap_or_else(|| element.outlined(StyleChain::default())), element, children: Vec::new(), } @@ -157,7 +159,7 @@ fn write_outline_item( outline.count(-(node.children.len() as i32)); } - let body = node.element.expect_field_by_name::<Content>("body"); + let body = node.element.body(); outline.title(TextStr(body.plain_text().trim())); let loc = node.element.location().unwrap(); |
