From f776f0a75fb36deabab8e8cdf880389e4e2eb6e8 Mon Sep 17 00:00:00 2001 From: Heinenen <37484430+Heinenen@users.noreply.github.com> Date: Mon, 12 Feb 2024 13:57:31 +0100 Subject: [PATCH] Named destinations (#2954) --- Cargo.lock | 5 ++-- Cargo.toml | 5 +++- crates/typst-pdf/src/lib.rs | 57 +++++++++++++++++++++++++++++++++--- crates/typst-pdf/src/page.rs | 27 +++++++++++++++-- 4 files changed, 83 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4900bdd48..842e9abe7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1616,9 +1616,8 @@ checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" [[package]] name = "pdf-writer" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "644b654f2de28457bf1e25a4905a76a563d1128a33ce60cf042f721f6818feaf" +version = "0.9.0" +source = "git+https://github.com/heinenen/pdf-writer?branch=named_destinations#58c6dc1552aa72f5e2c07a37045526fcf365d34a" dependencies = [ "bitflags 1.3.2", "itoa", diff --git a/Cargo.toml b/Cargo.toml index 130fb5ee6..b711b35f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,7 +71,7 @@ oxipng = { version = "9.0", default-features = false, features = ["filetime", "p palette = { version = "0.7.3", default-features = false, features = ["approx", "libm"] } parking_lot = "0.12.1" pathdiff = "0.2" -pdf-writer = "0.9.2" +pdf-writer = "0.9" phf = { version = "0.11", features = ["macros"] } pixglyph = "0.3" proc-macro2 = "1" @@ -122,6 +122,9 @@ xz2 = "0.1" yaml-front-matter = "0.1" zip = { version = "0.6", default-features = false, features = ["deflate"] } +[patch.crates-io] +pdf-writer = { git = 'https://github.com/heinenen/pdf-writer', branch = "named_destinations" } + [profile.dev.package."*"] opt-level = 2 diff --git a/crates/typst-pdf/src/lib.rs b/crates/typst-pdf/src/lib.rs index 80d4d67f7..9cf345a7f 100644 --- a/crates/typst-pdf/src/lib.rs +++ b/crates/typst-pdf/src/lib.rs @@ -10,17 +10,17 @@ mod page; mod pattern; use std::cmp::Eq; -use std::collections::{BTreeMap, HashMap}; +use std::collections::{BTreeMap, HashMap, HashSet}; use std::hash::Hash; use std::sync::Arc; use base64::Engine; use ecow::{eco_format, EcoString}; use pdf_writer::types::Direction; -use pdf_writer::{Finish, Name, Pdf, Ref, TextStr}; -use typst::foundations::Datetime; +use pdf_writer::{Finish, Name, Pdf, Ref, Str, TextStr}; +use typst::foundations::{Datetime, NativeElement}; use typst::layout::{Abs, Dir, Em, Transform}; -use typst::model::Document; +use typst::model::{Document, HeadingElem}; use typst::text::{Font, Lang}; use typst::util::Deferred; use typst::visualize::Image; @@ -252,12 +252,25 @@ fn write_catalog(ctx: &mut PdfContext, ident: Option<&str>, timestamp: Option, timestamp: Option( + ctx: &mut PdfContext, +) -> Vec<(Str<'a>, Ref, Ref, f32, f32)> { + let mut destinations = vec![]; + + let mut seen_labels = HashSet::new(); + let elements = ctx.document.introspector.query(&HeadingElem::elem().select()); + for elem in elements.iter() { + let heading = elem.to_packed::().unwrap(); + if let Some(label) = heading.label() { + if !seen_labels.contains(&label) { + let loc = heading.location().unwrap(); + let name = Str(label.as_str().as_bytes()); + let pos = ctx.document.introspector.position(loc); + let index = pos.page.get() - 1; + let y = (pos.point.y - Abs::pt(10.0)).max(Abs::zero()); + if let Some(page) = ctx.pages.get(index) { + seen_labels.insert(label); + let page_ref = ctx.page_refs[index]; + let x = pos.point.x.to_f32(); + let y = (page.size.y - y).to_f32(); + let dest_ref = ctx.alloc.bump(); + destinations.push((name, dest_ref, page_ref, x, y)) + } + } + } + } + destinations.sort_by_key(|i| i.0); + for (_name, dest_ref, page_ref, x, y) in destinations.iter().copied() { + ctx.pdf.destination(dest_ref).page(page_ref).xyz(x, y, None); + } + destinations } /// Compress data with the DEFLATE algorithm. diff --git a/crates/typst-pdf/src/page.rs b/crates/typst-pdf/src/page.rs index 6f6728482..b18c2878d 100644 --- a/crates/typst-pdf/src/page.rs +++ b/crates/typst-pdf/src/page.rs @@ -8,11 +8,12 @@ use pdf_writer::types::{ }; use pdf_writer::writers::PageLabel; use pdf_writer::{Content, Filter, Finish, Name, Rect, Ref, Str, TextStr}; -use typst::introspection::Meta; +use typst::foundations::{NativeElement, Selector}; +use typst::introspection::{Location, Meta}; use typst::layout::{ Abs, Em, Frame, FrameItem, GroupItem, Page, Point, Ratio, Size, Transform, }; -use typst::model::{Destination, Numbering}; +use typst::model::{Destination, Document, HeadingElem, Numbering}; use typst::text::{Case, Font, TextItem}; use typst::util::{Deferred, Numeric}; use typst::visualize::{ @@ -141,6 +142,16 @@ pub(crate) fn write_page_tree(ctx: &mut PdfContext) { ctx.colors.write_functions(&mut ctx.pdf); } +fn name_from_loc<'a>(doc: &Document, loc: &Location) -> Option> { + let elem = doc.introspector.query_first(&Selector::Location(*loc))?; + let label = elem.label()?; + debug_assert!(doc.introspector.query_label(label).is_ok()); + if elem.elem() != HeadingElem::elem() { + return None; + } + Some(Name(label.as_str().as_bytes())) +} + /// Write a page tree node. fn write_page(ctx: &mut PdfContext, i: usize) { let page = &ctx.pages[i]; @@ -179,7 +190,17 @@ fn write_page(ctx: &mut PdfContext, i: usize) { continue; } Destination::Position(pos) => *pos, - Destination::Location(loc) => ctx.document.introspector.position(*loc), + Destination::Location(loc) => { + if let Some(name) = name_from_loc(ctx.document, loc) { + annotation + .action() + .action_type(ActionType::GoTo) + .destination_named(name); + continue; + } else { + ctx.document.introspector.position(*loc) + } + } }; let index = pos.page.get() - 1;