Annotate href links in route-D PDFs

This commit is contained in:
gloamxun committed 2026-10-24 23:00:41 +08:00
1 parent fbb316baa9
commit b30bafca2b
7 files changed
+315 -39

No files matched your search

+44 -17
View File
@@ -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;
}