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
@@ -6327,6 +6327,47 @@ mod tests {
|
||||
assert!(svg.contains("font-style=\"italic\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linked_text_paints_link_annotations() {
|
||||
let segments = vec![
|
||||
crate::paragraph::InlineSegment::Text(ParagraphFont::Roman, "Visit ".into()),
|
||||
crate::paragraph::InlineSegment::Link {
|
||||
font: ParagraphFont::Roman,
|
||||
url: "https://example.com".into(),
|
||||
text: "the site".into(),
|
||||
},
|
||||
];
|
||||
let style = crate::paragraph::ParagraphStyle {
|
||||
line_width: 480 * 65_536,
|
||||
..crate::paragraph::ParagraphStyle::default()
|
||||
};
|
||||
let unit = Unit::paragraph_with_style_and_segments(1, "Visit the site", style, segments);
|
||||
let page = Page {
|
||||
items: unit.items,
|
||||
insertions: Vec::new(),
|
||||
marks: Vec::new(),
|
||||
floats: Vec::new(),
|
||||
sources: Vec::new(),
|
||||
hash: 0,
|
||||
};
|
||||
let list = page.display_list();
|
||||
let links: Vec<(&String, i64, i64)> = list
|
||||
.items
|
||||
.iter()
|
||||
.filter_map(|item| match item {
|
||||
crate::render::DisplayItem::Link { url, x, width, .. } => Some((url, *x, *width)),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(links.len(), 1);
|
||||
assert_eq!(links[0].0, "https://example.com");
|
||||
assert!(links[0].1 > 0);
|
||||
assert!(links[0].2 > 0);
|
||||
let svg = list.to_svg();
|
||||
assert!(!svg.contains("#008000"));
|
||||
assert!(svg.contains(">s</text>"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sized_paragraphs_paint_at_their_font_size() {
|
||||
let style = crate::paragraph::ParagraphStyle {
|
||||
|
||||
@@ -17,6 +17,9 @@ pub struct HorizontalBox {
|
||||
/// The inline formula this box paints, when it came from `$...$`.
|
||||
#[serde(default)]
|
||||
pub math: Option<Box<crate::math::MathAtom>>,
|
||||
/// Link target when this glyph came from `\href`.
|
||||
#[serde(default)]
|
||||
pub link: Option<String>,
|
||||
}
|
||||
|
||||
/// A run of paragraph material: text in one face or an inline formula.
|
||||
@@ -24,6 +27,11 @@ pub struct HorizontalBox {
|
||||
pub enum InlineSegment {
|
||||
Text(ParagraphFont, String),
|
||||
Math(crate::math::MathAtom),
|
||||
Link {
|
||||
font: ParagraphFont,
|
||||
url: String,
|
||||
text: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
@@ -621,6 +629,7 @@ pub fn text_items(text: &str, style: &ParagraphStyle) -> Vec<HorizontalItem> {
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
link: None,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -1088,6 +1097,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
link: None,
|
||||
}));
|
||||
} else if *no_break_width > 0 {
|
||||
line.push(HorizontalItem::Box(HorizontalBox {
|
||||
@@ -1098,6 +1108,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
link: None,
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -1442,6 +1453,7 @@ pub fn text_items_with_font(
|
||||
italic: font.italic(character),
|
||||
font: None,
|
||||
math: None,
|
||||
link: None,
|
||||
}));
|
||||
let boundary = character_index + 1;
|
||||
// TeX's defaults for American English are \lefthyphenmin=2 and
|
||||
@@ -1608,6 +1620,44 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::Link { font, url, text } => {
|
||||
if text.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let starts_with_space = text.chars().next().is_some_and(char::is_whitespace);
|
||||
let boundary_space = match &previous {
|
||||
Some((_previous_font, previous_text)) => {
|
||||
previous_text
|
||||
.chars()
|
||||
.next_back()
|
||||
.is_some_and(char::is_whitespace)
|
||||
|| starts_with_space
|
||||
}
|
||||
None => seen_content && starts_with_space,
|
||||
};
|
||||
if boundary_space {
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(
|
||||
previous.as_ref().map_or(*font, |(font, _)| *font),
|
||||
);
|
||||
items.push(HorizontalItem::Glue {
|
||||
width: metrics.space(),
|
||||
stretch: metrics.stretch(),
|
||||
shrink: metrics.shrink(),
|
||||
order: 0,
|
||||
});
|
||||
}
|
||||
let metrics = crate::cmr10::Cmr10Metrics::for_paragraph_font(*font);
|
||||
let mut run = text_items_with_font(text, style, &metrics);
|
||||
for item in &mut run {
|
||||
if let HorizontalItem::Box(box_) = item {
|
||||
box_.font = Some(*font);
|
||||
box_.link = Some(url.clone());
|
||||
}
|
||||
}
|
||||
items.extend(run);
|
||||
previous = Some((*font, text.clone()));
|
||||
seen_content = true;
|
||||
}
|
||||
InlineSegment::Math(atom) => {
|
||||
if let Some((previous_font, previous_text)) = &previous
|
||||
&& previous_text
|
||||
@@ -1632,6 +1682,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: Some(Box::new(atom.clone())),
|
||||
link: None,
|
||||
}));
|
||||
previous = None;
|
||||
seen_content = true;
|
||||
@@ -1641,7 +1692,7 @@ pub fn layout_paragraph_segments(segments: &[InlineSegment], style: &ParagraphSt
|
||||
scale_items_for_size(&mut items, style.font_size);
|
||||
let mut paragraph = layout_items(&items, &sized_style(style));
|
||||
paragraph.font = match segments.first() {
|
||||
Some(InlineSegment::Text(font, _)) => *font,
|
||||
Some(InlineSegment::Text(font, _)) | Some(InlineSegment::Link { font, .. }) => *font,
|
||||
_ => ParagraphFont::Roman,
|
||||
};
|
||||
paragraph.font_size = style.font_size;
|
||||
@@ -1896,6 +1947,37 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linked_boxes_carry_their_target() {
|
||||
let style = ParagraphStyle::default();
|
||||
let segments = vec![
|
||||
InlineSegment::Text(ParagraphFont::Roman, "Go ".into()),
|
||||
InlineSegment::Link {
|
||||
font: ParagraphFont::Roman,
|
||||
url: "https://example.com".into(),
|
||||
text: "there".into(),
|
||||
},
|
||||
InlineSegment::Text(ParagraphFont::Roman, " now".into()),
|
||||
];
|
||||
let paragraph = layout_paragraph_segments(&segments, &style);
|
||||
let linked: Vec<&HorizontalBox> = paragraph
|
||||
.lines
|
||||
.iter()
|
||||
.flatten()
|
||||
.filter_map(|item| match item {
|
||||
HorizontalItem::Box(box_) if box_.link.is_some() => Some(box_),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(linked.len(), 5);
|
||||
assert!(
|
||||
linked
|
||||
.iter()
|
||||
.all(|box_| box_.link.as_deref() == Some("https://example.com"))
|
||||
);
|
||||
assert!(linked.iter().all(|box_| box_.character.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn inline_math_boxes_sit_between_words() {
|
||||
let style = ParagraphStyle {
|
||||
@@ -1949,6 +2031,7 @@ mod tests {
|
||||
italic: 0,
|
||||
font: None,
|
||||
math: None,
|
||||
link: None,
|
||||
})
|
||||
};
|
||||
let items = vec![
|
||||
|
||||
@@ -87,6 +87,18 @@ pub struct PdfImage {
|
||||
pub rgb: Vec<u8>,
|
||||
}
|
||||
|
||||
fn flush_link(run: &mut Option<(String, i64, i64, i64, i64)>, items: &mut Vec<DisplayItem>) {
|
||||
if let Some((url, x, y, width, height)) = run.take() {
|
||||
items.push(DisplayItem::Link {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// A page-local list of drawing primitives.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub struct DisplayList {
|
||||
@@ -220,22 +232,8 @@ impl DisplayList {
|
||||
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"black\"/>\n"
|
||||
));
|
||||
}
|
||||
DisplayItem::Link {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
..
|
||||
} => {
|
||||
let x = *x as f64 / 65_536.0;
|
||||
let y = *y as f64 / 65_536.0;
|
||||
let width = *width as f64 / 65_536.0;
|
||||
let height = *height as f64 / 65_536.0;
|
||||
out.push_str(&format!(
|
||||
"<rect x=\"{x:.3}\" y=\"{y:.3}\" width=\"{width:.3}\" height=\"{height:.3}\" fill=\"none\" stroke=\"#008000\"/>
|
||||
"
|
||||
));
|
||||
}
|
||||
// Link annotations are PDF metadata; the SVG preview omits them.
|
||||
DisplayItem::Link { .. } => {}
|
||||
DisplayItem::Special(content) => {
|
||||
out.push_str(&format!(
|
||||
"<!-- special: {} -->
|
||||
@@ -526,9 +524,32 @@ impl Page {
|
||||
} else {
|
||||
paragraph.left_skip + indent
|
||||
};
|
||||
let mut link_run: Option<(String, i64, i64, i64, i64)> = None;
|
||||
for item in line {
|
||||
match item {
|
||||
HorizontalItem::Box(box_) => {
|
||||
if let Some(url) = &box_.link {
|
||||
let top = baseline - box_.height;
|
||||
let height = box_.height + box_.depth;
|
||||
match &mut link_run {
|
||||
Some((open, _, open_y, width, open_height))
|
||||
if open == url =>
|
||||
{
|
||||
let bottom = top + height;
|
||||
let open_bottom = *open_y + *open_height;
|
||||
*open_y = (*open_y).min(top);
|
||||
*open_height = bottom.max(open_bottom) - *open_y;
|
||||
*width += box_.width;
|
||||
}
|
||||
_ => {
|
||||
flush_link(&mut link_run, &mut list.items);
|
||||
link_run =
|
||||
Some((url.clone(), x, top, box_.width, height));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
flush_link(&mut link_run, &mut list.items);
|
||||
}
|
||||
if let Some(math) = &box_.math {
|
||||
let math_font = Cmr10Metrics::default();
|
||||
Self::paint_math(
|
||||
@@ -551,11 +572,17 @@ impl Page {
|
||||
}
|
||||
x += box_.width;
|
||||
}
|
||||
HorizontalItem::Glue { width, .. } => x += *width,
|
||||
HorizontalItem::Glue { width, .. } => {
|
||||
if let Some((_, _, _, run_width, _)) = &mut link_run {
|
||||
*run_width += *width;
|
||||
}
|
||||
x += *width;
|
||||
}
|
||||
HorizontalItem::Penalty { .. }
|
||||
| HorizontalItem::Discretionary { .. } => {}
|
||||
}
|
||||
}
|
||||
flush_link(&mut link_run, &mut list.items);
|
||||
}
|
||||
y += paragraph.height;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user