Annotate href labels with PDF link targets
This commit is contained in:
1 parent
88646c9453
commit
05d7980561
7 files changed
+68
-35
No files matched your search
+28
-3
@@ -537,6 +537,7 @@ impl PositionedPages {
|
||||
&self,
|
||||
fonts: &fonts::FontCache,
|
||||
page_height_mm: f64,
|
||||
link_targets: &HashMap<String, String>,
|
||||
) -> Vec<LinkAnnotation> {
|
||||
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<String, String>,
|
||||
) {
|
||||
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::<String>();
|
||||
if url.starts_with("http://") || url.starts_with("https://") {
|
||||
let spaced = fragments
|
||||
.iter()
|
||||
.map(|(text, _, _)| text.as_str())
|
||||
.collect::<Vec<_>>()
|
||||
.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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user