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
@@ -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![
|
||||
|
||||
Reference in new issue
Block a user