Skip to content

Commit f414a9e

Browse files
committed
A drop cap joins the paragraph it opens, so it is erased with it
1 parent aa2887b commit f414a9e

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

‎crates/rpt-core/src/segment.rs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,7 @@ fn assemble(
21562156
let notes = table_note_glyphs(&lines, &kept);
21572157
kept.extend(notes);
21582158
kept.extend(margin_stamps(&lines));
2159+
let caps = drop_caps(&lines);
21592160
release_hyphen_continuations(&lines, &mut kept);
21602161
// A theorem frame is a box, so a short wrap (`version. We write…`,
21612162
// `to x + y = z.`) was kept as a figure label. It is the next line of
@@ -2289,6 +2290,21 @@ fn assemble(
22892290
kept.extend(seg.glyph_ids.iter().map(|id| (*id, "contact".to_string())));
22902291
false
22912292
});
2293+
// The drop cap is the first letter of the paragraph beside it, and the
2294+
// translation says it (`无人驾驶` for `U` + `NMANNED`). It joins that
2295+
// paragraph's glyphs, so it is erased with them, and the text stays as
2296+
// the extractor read it.
2297+
for (cap, host) in caps {
2298+
let Some(seg) = segments
2299+
.iter_mut()
2300+
.find(|seg| seg.glyph_ids.contains(&host))
2301+
else {
2302+
continue;
2303+
};
2304+
let at = seg.glyph_ids.iter().position(|id| *id == host).unwrap_or(0);
2305+
seg.glyph_ids.insert(at, cap);
2306+
kept.retain(|(id, _)| *id != cap);
2307+
}
22922308
for (id, seg) in segments.iter_mut().enumerate() {
22932309
seg.id = id as u32;
22942310
}
@@ -4926,6 +4942,49 @@ fn reading_order(lines: &[VisualLine<'_>]) -> Vec<usize> {
49264942
order
49274943
}
49284944

4945+
/// `U` of `UNMANNED aerial vehicles…` is a drop cap: one capital letter with
4946+
/// the body lines starting just right of it, inside its height. A margin
4947+
/// stamp has no body beside it. Kept as a stamp, `U` stayed in front of
4948+
/// `无人驾驶` and the wrapped line ran through it.
4949+
/// The first glyph of the topmost body line beside a drop cap.
4950+
fn drop_cap_host(cap: &VisualLine<'_>, lines: &[VisualLine<'_>]) -> Option<u32> {
4951+
let [glyph] = cap.glyphs.as_slice() else {
4952+
return None;
4953+
};
4954+
if !glyph.unicode.trim().chars().all(|ch| ch.is_uppercase()) || glyph.unicode.trim().is_empty()
4955+
{
4956+
return None;
4957+
}
4958+
let top = glyph.bbox[1].max(glyph.bbox[3]);
4959+
let bottom = glyph.bbox[1].min(glyph.bbox[3]);
4960+
let right = glyph.bbox[0].max(glyph.bbox[2]);
4961+
lines
4962+
.iter()
4963+
.filter(|line| {
4964+
line.page == cap.page
4965+
&& line.size <= cap.size * 0.6
4966+
&& line.glyphs.len() >= 8
4967+
&& line.y >= bottom - 1.0
4968+
&& line.y <= top
4969+
&& line.left >= right - 1.0
4970+
&& line.left <= right + cap.size * 0.5
4971+
})
4972+
.max_by(|a, b| a.y.total_cmp(&b.y))
4973+
.and_then(|line| line.glyphs.first().map(|glyph| glyph.id))
4974+
}
4975+
4976+
/// Drop caps and the first glyph of the body line each one opens.
4977+
fn drop_caps(lines: &[VisualLine<'_>]) -> Vec<(u32, u32)> {
4978+
lines
4979+
.iter()
4980+
.filter(|line| line.left <= 56.0 && line.size >= 14.0)
4981+
.filter_map(|line| {
4982+
let host = drop_cap_host(line, lines)?;
4983+
Some((line.glyphs[0].id, host))
4984+
})
4985+
.collect()
4986+
}
4987+
49294988
fn margin_stamps(lines: &[VisualLine<'_>]) -> Vec<(u32, String)> {
49304989
let mut kept = Vec::new();
49314990
let mut seen = crate::detmap::HashSet::default();

0 commit comments

Comments
 (0)