From 05d798056120413e3bcf945e07a4e238bbc0d0b8 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Mon, 19 Oct 2026 21:33:26 +0800 Subject: [PATCH] Annotate href labels with PDF link targets --- crates/texrra-core/src/document.rs | 3 +++ crates/texrra-core/src/document_parser.rs | 3 +++ crates/texrra-core/src/expanded.rs | 5 ++++ examples/hello.tex | 33 +---------------------- src/rust_typeset/pdf.rs | 31 ++++++++++++++++++--- src/rust_typeset/texlang_adapter.rs | 8 ++++++ tests/typeset.rs | 20 ++++++++++++++ 7 files changed, 68 insertions(+), 35 deletions(-) diff --git a/crates/texrra-core/src/document.rs b/crates/texrra-core/src/document.rs index 3192784..660fff2 100644 --- a/crates/texrra-core/src/document.rs +++ b/crates/texrra-core/src/document.rs @@ -99,6 +99,8 @@ pub struct DocumentModel { pub labels: HashMap, pub citations: HashMap, pub citation_metadata: HashMap, + /// Display text -> hyperlink URL for `\href` annotations. + pub link_targets: HashMap, /// Bibliography keys in the order their metadata was declared. Needed to /// assign natbib-style year suffixes (2014a, 2014b, ...) deterministically. pub bibliography_order: Vec, @@ -128,6 +130,7 @@ impl Default for DocumentModel { labels: HashMap::new(), citations: HashMap::new(), citation_metadata: HashMap::new(), + link_targets: HashMap::new(), bibliography_order: Vec::new(), author_year_citations: false, footnotes: Vec::new(), diff --git a/crates/texrra-core/src/document_parser.rs b/crates/texrra-core/src/document_parser.rs index 3fe46cd..f6283ce 100644 --- a/crates/texrra-core/src/document_parser.rs +++ b/crates/texrra-core/src/document_parser.rs @@ -1155,6 +1155,9 @@ impl Parser { expanded::FrontendEventKind::LinkStart => { self.link_depth = self.link_depth.saturating_add(1); } + expanded::FrontendEventKind::LinkTarget { text, url } => { + self.document.link_targets.insert(text.clone(), url.clone()); + } expanded::FrontendEventKind::LinkEnd => { self.link_depth = self.link_depth.saturating_sub(1); } diff --git a/crates/texrra-core/src/expanded.rs b/crates/texrra-core/src/expanded.rs index a617cea..6d34a8a 100644 --- a/crates/texrra-core/src/expanded.rs +++ b/crates/texrra-core/src/expanded.rs @@ -266,6 +266,11 @@ pub enum FrontendEventKind { PageNumber, /// Start of hyperlink material. LinkStart, + /// `\href` target: display text and URL, used for PDF annotations. + LinkTarget { + text: String, + url: String, + }, /// End of hyperlink material. LinkEnd, /// A package or class was loaded from a local file. diff --git a/examples/hello.tex b/examples/hello.tex index 04b9dd9..d7748d3 100644 --- a/examples/hello.tex +++ b/examples/hello.tex @@ -1,35 +1,4 @@ \documentclass{article} \begin{document} - -\section*{Compatibility examples} - -Spacing: A\quad B\qquad C, A\thinspace B\enspace C. - -Symbols: \S\P\dag\ddag\copyright\textbullet\textregistered\texttrademark -\ldots\textendash\textemdash\guillemotleft x\guillemotright -\texteuro\textsterling\textyen\textdegree\textpm. - -Letters: \ss\ae\AE\oe\OE\aa\AA\o\O\l\L\i. - -Styles: {\bfseries bold}, {\itshape italic}, {\ttfamily mono}, \textsc{small caps}. - -Breaks: A\nopagebreak[4] B\linebreak[2] C\samepage. - -A\vspace*{6pt} B - -\vspace{6pt} - -A\hspace*{1cm}B - -\begin{center} -Centered text -\end{center} - -\hrule - -\fbox{Framed box} and \mbox{unbreakable box}. - -\parbox{60mm}{A parbox wraps its content to sixty millimetres and stays -in the vertical flow.} - +See \href{https://example.com/target}{Click here} now. \end{document} diff --git a/src/rust_typeset/pdf.rs b/src/rust_typeset/pdf.rs index 32b939c..3f6a796 100644 --- a/src/rust_typeset/pdf.rs +++ b/src/rust_typeset/pdf.rs @@ -537,6 +537,7 @@ impl PositionedPages { &self, fonts: &fonts::FontCache, page_height_mm: f64, + link_targets: &HashMap, ) -> Vec { let mut annotations = Vec::new(); for (index, page) in self.pages.iter().enumerate() { @@ -576,6 +577,7 @@ impl PositionedPages { *font_size, index as u32 + 1, page_height_mm, + link_targets, ); } PositionedPageItem::Centered { element, y, .. } => { @@ -601,6 +603,7 @@ impl PositionedPages { font_size, index as u32 + 1, page_height_mm, + link_targets, ); line_y += em * 1.2; } @@ -622,6 +625,7 @@ fn push_url_annotations( font_size: u8, page: u32, page_height_mm: f64, + link_targets: &HashMap, ) { let mut fragments: Vec<(String, f64, f64)> = Vec::new(); let em = f64::from(font_size) * 25.4 / 72.27; @@ -629,11 +633,24 @@ fn push_url_annotations( if fragments.is_empty() { return; } - let url = fragments + let text = fragments .iter() .map(|(text, _, _)| text.as_str()) .collect::(); - if url.starts_with("http://") || url.starts_with("https://") { + let spaced = fragments + .iter() + .map(|(text, _, _)| text.as_str()) + .collect::>() + .join(" "); + let url = if text.starts_with("http://") || text.starts_with("https://") { + Some(text.clone()) + } else { + link_targets + .get(&spaced) + .or_else(|| link_targets.get(&text)) + .cloned() + }; + if let Some(url) = url { for (_, x, width) in fragments.iter() { let left = line_x_mm + *x; annotations.push(LinkAnnotation { @@ -652,6 +669,10 @@ fn push_url_annotations( }; for span in &layout.spans { if !span.link { + if !fragments.is_empty() && span.text.chars().all(char::is_whitespace) { + fragments.push((" ".to_string(), f64::from(span.x), 0.0)); + continue; + } flush(&mut fragments, annotations); continue; } @@ -2346,7 +2367,11 @@ pub(super) fn render_pdf( mono_family, sans_family, ); - let links = positioned.link_annotations(document.font_cache(), model.page.height_mm); + let links = positioned.link_annotations( + document.font_cache(), + model.page.height_mm, + &model.link_targets, + ); document.push(positioned); let mut pdf = Vec::new(); document diff --git a/src/rust_typeset/texlang_adapter.rs b/src/rust_typeset/texlang_adapter.rs index 3e9f83b..c718172 100644 --- a/src/rust_typeset/texlang_adapter.rs +++ b/src/rust_typeset/texlang_adapter.rs @@ -3664,6 +3664,14 @@ fn href_primitive( let url = read_unexpanded_argument(input, token, "expects a link URL")?; let content = read_unexpanded_argument(input, token, "expects link text")?; let inner = strip_braced_tokens(&content).to_vec(); + write_event( + input, + token, + FrontendEventKind::LinkTarget { + text: simple_token_text(&content).trim().to_string(), + url: simple_token_text(&url).trim().to_string(), + }, + ); write_event(input, token, FrontendEventKind::LinkStart); let trace_key = token.trace_key(); let mut expansion = Vec::new(); diff --git a/tests/typeset.rs b/tests/typeset.rs index cd02b6f..fcef876 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -1551,6 +1551,26 @@ After text. ); } +#[test] +fn href_text_creates_a_pdf_link_annotation() { + let source = r"\documentclass{article} +\begin{document} +See \href{https://example.com/target}{Click here} now. +\end{document}"; + let pdf = CompilerSession::default() + .compile(source, "href-link.tex") + .expect("href PDF"); + let text = String::from_utf8_lossy(&pdf); + assert!( + text.contains("/Subtype/Link"), + "href text should be clickable" + ); + assert!( + text.contains("https://example.com/target"), + "annotation should carry the href target" + ); +} + #[test] fn url_text_creates_a_pdf_link_annotation() { let source = r"\documentclass{article}