1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//! The hyperlinks and the bookmarks of a paragraph by the runs that hold them, and where they lie
//! in the paragraph's story text, for a viewer that follows a link to its target.
use crate::body::links::{LinkMark, LinkTarget};
use crate::body::{Content, Paragraph};
use crate::layout::RunSpan;
use crate::text_map::{StoryBookmark, StoryLink};
/// The hyperlinks and the bookmarks of a paragraph by the runs that hold them (**Run marks**).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct RunMarks {
/// The hyperlinks whose text lies in the paragraph, with the runs of their text.
links: Vec<RunLink>,
/// The names of the bookmarks that start in the paragraph, each with the index of the run
/// that holds its start.
bookmarks: Vec<(String, u32)>,
}
/// A hyperlink of a paragraph with the runs of its text.
#[derive(Clone, Debug, PartialEq, Eq)]
struct RunLink {
/// The index of the first run of its text: 0 for a link whose text an earlier paragraph
/// started.
first: u32,
/// The index of the last run of its text: [`u32::MAX`] for a link whose text runs on into
/// the next paragraph.
last: u32,
/// Where it goes.
target: LinkTarget,
/// Its tooltip.
tooltip: Option<String>,
}
/// A hyperlink whose text is open at a place of a story, with where it goes and its tooltip, or
/// none for a link that goes nowhere.
pub(crate) type OpenLink = Option<(LinkTarget, Option<String>)>;
impl RunMarks {
/// The marks of `paragraph`, where the hyperlinks `open`, innermost last, are open at its
/// start; `open` then holds the links open at its end.
pub(crate) fn of(paragraph: &Paragraph, open: &mut Vec<OpenLink>) -> Self {
let mut marks = Self::default();
let mut here: Vec<(u32, OpenLink)> = open.drain(..).map(|link| (0, link)).collect();
for (place, run) in paragraph.runs.iter().enumerate() {
let index = u32::try_from(place).unwrap_or(u32::MAX);
marks
.bookmarks
.extend(run.content.iter().filter_map(|content| match content {
Content::Bookmark(name) => Some((name.clone(), index)),
_ => None,
}));
for mark in &run.links {
marks.take(mark, index, &mut here);
}
}
for (first, link) in &here {
if let Some((target, tooltip)) = link {
marks.push(*first, (u32::MAX, target.clone(), tooltip.clone()));
}
}
*open = here.into_iter().map(|(_, link)| link).collect();
marks
}
/// Takes the mark `mark` of the run `index`, where the links `here`, innermost last, are
/// open with their first runs: a start opens a link, and an end closes the innermost one.
fn take(&mut self, mark: &LinkMark, index: u32, here: &mut Vec<(u32, OpenLink)>) {
match mark {
LinkMark::Start(link) => {
let target = link.target().map(|target| (target, link.tooltip.clone()));
here.push((index, target));
}
LinkMark::End => {
if let Some((first, Some((target, tooltip)))) = here.pop() {
self.push(first, (index, target, tooltip));
}
}
}
}
/// Adds the link from the run `first` to the run `last` that goes to `target`.
fn push(&mut self, first: u32, (last, target, tooltip): (u32, LinkTarget, Option<String>)) {
self.links.push(RunLink {
first,
last,
target,
tooltip,
});
}
/// Whether the paragraph has no link and no bookmark.
pub(crate) fn is_empty(&self) -> bool {
self.links.is_empty() && self.bookmarks.is_empty()
}
/// The hyperlinks and the bookmarks in the paragraph's story text, which is `length` bytes
/// long and whose runs' glyphs lie at `spans`. A link spans the glyphs of its runs, and a
/// link whose runs show no glyph is left out. A bookmark lies at the first glyph of its run
/// or of a later one, or at the end of the text. The links come in the order of their
/// starts, a link before the links inside it.
pub(crate) fn placed(
&self,
spans: &[RunSpan],
length: u32,
) -> (Vec<StoryLink>, Vec<StoryBookmark>) {
let mut links: Vec<StoryLink> = self
.links
.iter()
.filter_map(|link| {
let runs = link.first..=link.last;
let inside = spans
.iter()
.filter(|span| span.run.is_some_and(|run| runs.contains(&run)));
let (start, end) = inside.fold(None, |found: Option<(u32, u32)>, span| {
Some(found.map_or((span.start, span.end), |(start, end)| {
(start.min(span.start), end.max(span.end))
}))
})?;
Some(StoryLink::new(
start,
end,
link.target.clone(),
link.tooltip.clone(),
))
})
.collect();
links.sort_by_key(|link| (link.start_byte, std::cmp::Reverse(link.end_byte)));
let bookmarks = self
.bookmarks
.iter()
.map(|(name, run)| {
let offset = spans
.iter()
.find(|span| span.run.is_some_and(|own| own >= *run))
.map_or(length, |span| span.start);
StoryBookmark::new(name.clone(), offset)
})
.collect();
(links, bookmarks)
}
}
#[cfg(test)]
mod tests;