Annotate href links in route-D PDFs
This commit is contained in:
1 parent
fbb316baa9
commit
b30bafca2b
7 files changed
+315
-39
No files matched your search
+142
-17
@@ -418,7 +418,8 @@ fn expand_links_line(line: &str) -> String {
|
||||
let mut output = String::new();
|
||||
let mut rest = line;
|
||||
loop {
|
||||
let href = rest.find("\\href");
|
||||
// `\href` survives to the paragraph builder so it can become a link annotation.
|
||||
let href: Option<usize> = None;
|
||||
let url = rest.find("\\url");
|
||||
let choice = match (href, url) {
|
||||
(Some(left), Some(right)) if left <= right => Some((left, "\\href")),
|
||||
@@ -3004,6 +3005,47 @@ fn split_inline_math(text: &str) -> Vec<(bool, String)> {
|
||||
///
|
||||
/// Math metrics are measured at 10pt, so sized paragraphs keep the literal
|
||||
/// delimiters until per-size math layout exists.
|
||||
enum HrefPart {
|
||||
Plain(String),
|
||||
Link(String, String),
|
||||
}
|
||||
|
||||
fn split_hrefs(text: &str) -> Vec<HrefPart> {
|
||||
let mut parts = Vec::new();
|
||||
let mut rest = text;
|
||||
while let Some(position) = rest.find("\\href") {
|
||||
if position > 0 {
|
||||
parts.push(HrefPart::Plain(rest[..position].to_string()));
|
||||
}
|
||||
let after = &rest[position + "\\href".len()..];
|
||||
let Some((url, used)) = braced_group(after) else {
|
||||
parts.push(HrefPart::Plain(rest[position..].to_string()));
|
||||
return parts;
|
||||
};
|
||||
let remainder = &after[used..];
|
||||
let Some((label, used_label)) = braced_group(remainder) else {
|
||||
parts.push(HrefPart::Plain(rest[position..].to_string()));
|
||||
return parts;
|
||||
};
|
||||
parts.push(HrefPart::Link(url.trim().to_string(), label.to_string()));
|
||||
rest = &remainder[used_label..];
|
||||
}
|
||||
if !rest.is_empty() {
|
||||
parts.push(HrefPart::Plain(rest.to_string()));
|
||||
}
|
||||
parts
|
||||
}
|
||||
|
||||
fn strip_hrefs(text: &str) -> String {
|
||||
let mut output = String::new();
|
||||
for part in split_hrefs(text) {
|
||||
match part {
|
||||
HrefPart::Plain(piece) | HrefPart::Link(_, piece) => output.push_str(&piece),
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
fn inline_math_segments(
|
||||
text: &str,
|
||||
base: ParagraphFont,
|
||||
@@ -3012,22 +3054,41 @@ fn inline_math_segments(
|
||||
if style.font_size != 655_360 {
|
||||
return None;
|
||||
}
|
||||
let runs = inline_runs(text, base).unwrap_or_else(|| vec![(base, text.to_string())]);
|
||||
let mut segments = Vec::new();
|
||||
let mut found = false;
|
||||
for (font, run_text) in runs {
|
||||
for (is_math, piece) in split_inline_math(&run_text) {
|
||||
if is_math {
|
||||
found = true;
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Math(parse_math(
|
||||
&piece,
|
||||
)));
|
||||
} else if !piece.is_empty() {
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Text(font, piece));
|
||||
let mut found_math = false;
|
||||
let mut found_link = false;
|
||||
for part in split_hrefs(text) {
|
||||
match part {
|
||||
HrefPart::Plain(piece) => {
|
||||
let runs = inline_runs(&piece, base).unwrap_or_else(|| vec![(base, piece.clone())]);
|
||||
for (font, run_text) in runs {
|
||||
for (is_math, piece) in split_inline_math(&run_text) {
|
||||
if is_math {
|
||||
found_math = true;
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Math(
|
||||
parse_math(&piece),
|
||||
));
|
||||
} else if !piece.is_empty() {
|
||||
segments
|
||||
.push(texrra_tex_core::paragraph::InlineSegment::Text(font, piece));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
HrefPart::Link(url, label) => {
|
||||
found_link = true;
|
||||
let runs = inline_runs(&label, base).unwrap_or_else(|| vec![(base, label.clone())]);
|
||||
for (font, text) in runs {
|
||||
segments.push(texrra_tex_core::paragraph::InlineSegment::Link {
|
||||
font,
|
||||
url: url.clone(),
|
||||
text,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
found.then_some(segments)
|
||||
(found_math || found_link).then_some(segments)
|
||||
}
|
||||
|
||||
fn inline_runs(text: &str, base: ParagraphFont) -> Option<Vec<(ParagraphFont, String)>> {
|
||||
@@ -4219,6 +4280,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
space_skip: geometry.space_skip,
|
||||
xspace_skip: geometry.xspace_skip,
|
||||
};
|
||||
let plain = strip_hrefs(¶graph.text);
|
||||
let mut unit = match inline_math_segments(¶graph.text, paragraph.font, &style) {
|
||||
Some(segments) => Unit::paragraph_with_style_and_segments(
|
||||
index as u64,
|
||||
@@ -4226,16 +4288,16 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
style.clone(),
|
||||
segments,
|
||||
),
|
||||
None => match inline_runs(¶graph.text, paragraph.font) {
|
||||
None => match inline_runs(&plain, paragraph.font) {
|
||||
Some(runs) => Unit::paragraph_with_style_and_runs(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
plain.clone(),
|
||||
style,
|
||||
runs,
|
||||
),
|
||||
None => Unit::paragraph_with_style_and_face(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
plain,
|
||||
style,
|
||||
paragraph.font,
|
||||
),
|
||||
@@ -4726,7 +4788,8 @@ See \ref{fig:a} and \ref{tab:b}.";
|
||||
"See \\href{https://example.com}{Example} and \\url{https://x.test/a_b}.",
|
||||
);
|
||||
assert!(
|
||||
body.contains("See Example and \\texttt{https://x.test/a_b}."),
|
||||
body.contains(r"\href{https://example.com}{Example}")
|
||||
&& body.contains(r"\texttt{https://x.test/a_b}."),
|
||||
"{body}"
|
||||
);
|
||||
}
|
||||
@@ -5480,6 +5543,68 @@ A definition.
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn href_creates_link_boxes() {
|
||||
let source = "Visit \\href{https://example.com}{the site} now.";
|
||||
let blocks = paragraphs(source).expect("paragraphs");
|
||||
let document = build_document(&blocks, &PageGeometry::default());
|
||||
let mut linked = 0;
|
||||
let mut backslashes = 0;
|
||||
for unit in &document.units {
|
||||
for item in &unit.items {
|
||||
let VerticalItem::Paragraph(paragraph) = item else {
|
||||
continue;
|
||||
};
|
||||
for line in ¶graph.lines {
|
||||
for item in line {
|
||||
let texrra_tex_core::paragraph::HorizontalItem::Box(box_) = item else {
|
||||
continue;
|
||||
};
|
||||
if box_.link.as_deref() == Some("https://example.com") {
|
||||
linked += 1;
|
||||
}
|
||||
if box_.character == Some('\\') {
|
||||
backslashes += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(linked > 0);
|
||||
assert_eq!(backslashes, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_paragraphs_strip_href_commands() {
|
||||
let source = "\\Large Visit \\href{https://example.com}{the site}.";
|
||||
let blocks = paragraphs(source).expect("paragraphs");
|
||||
let document = build_document(&blocks, &PageGeometry::default());
|
||||
let mut linked = 0;
|
||||
let mut backslashes = 0;
|
||||
for unit in &document.units {
|
||||
for item in &unit.items {
|
||||
let VerticalItem::Paragraph(paragraph) = item else {
|
||||
continue;
|
||||
};
|
||||
for line in ¶graph.lines {
|
||||
for item in line {
|
||||
let texrra_tex_core::paragraph::HorizontalItem::Box(box_) = item else {
|
||||
continue;
|
||||
};
|
||||
if box_.link.is_some() {
|
||||
linked += 1;
|
||||
}
|
||||
if box_.character == Some('\\') {
|
||||
backslashes += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(linked, 0);
|
||||
assert_eq!(backslashes, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_stays_literal_in_sized_paragraphs() {
|
||||
let blocks = paragraphs("\\large Value $x$ tail.").expect("paragraphs");
|
||||
|
||||
Reference in new issue
Block a user