Lead table-of-contents entries with dots
This commit is contained in:
1 parent
7dd94d7fbe
commit
ae61d11859
5 files changed
+88
-16
No files matched your search
+63
-12
@@ -5792,6 +5792,34 @@ fn stamp_page_numbers(
|
||||
|
||||
/// Fill in table-of-contents page numbers from a laid-out engine. Returns
|
||||
/// true when at least one entry changed and the document needs re-layout.
|
||||
fn text_width_roman(text: &str) -> i64 {
|
||||
let metrics = Cmr10Metrics::Roman;
|
||||
text.chars()
|
||||
.map(|character| match character {
|
||||
' ' => metrics.space(),
|
||||
_ => metrics.width(character),
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
/// Fill the space between entry text and page number with dot leaders.
|
||||
fn toc_leader(head: &str, number: &str, measure: i64) -> Option<String> {
|
||||
let metrics = Cmr10Metrics::Roman;
|
||||
let head_width = text_width_roman(head);
|
||||
let number_width = text_width_roman(number);
|
||||
let dot = metrics.width('.');
|
||||
let space = metrics.space();
|
||||
if dot <= 0 || measure <= 0 {
|
||||
return None;
|
||||
}
|
||||
let room = measure - head_width - number_width - 2 * space;
|
||||
let count = room / dot;
|
||||
if count < 4 {
|
||||
return None;
|
||||
}
|
||||
Some(format!("{head} {} {number}", ".".repeat(count as usize)))
|
||||
}
|
||||
|
||||
fn stamp_toc_pages(document: &mut Document, engine: &Engine, geometry: &PageGeometry) -> bool {
|
||||
let mut page_of = std::collections::HashMap::new();
|
||||
for (index, page) in engine.pages().iter().enumerate() {
|
||||
@@ -5803,18 +5831,22 @@ fn stamp_toc_pages(document: &mut Document, engine: &Engine, geometry: &PageGeom
|
||||
let updates: Vec<(u64, String)> = document
|
||||
.units
|
||||
.iter()
|
||||
.filter(|unit| unit.toc_target.is_some() && !unit.source.contains("\\hfill"))
|
||||
.filter(|unit| unit.toc_target.is_some() && !unit.toc_stamped)
|
||||
.filter_map(|unit| {
|
||||
let target = unit.toc_target?;
|
||||
let page = page_of.get(&target)?;
|
||||
Some((
|
||||
unit.id,
|
||||
format!(
|
||||
"{} \\hfill {}",
|
||||
unit.source.trim_end(),
|
||||
page_number_text(*page, numbering)
|
||||
),
|
||||
))
|
||||
let page_number = page_number_text(*page, numbering);
|
||||
let measure = unit
|
||||
.paragraph_style
|
||||
.as_ref()
|
||||
.map(|style| {
|
||||
(style.line_width - style.left_skip - style.right_skip - style.par_indent)
|
||||
.max(1)
|
||||
})
|
||||
.unwrap_or(0);
|
||||
let source = toc_leader(unit.source.trim_end(), &page_number, measure)
|
||||
.unwrap_or_else(|| format!("{} \\hfill {}", unit.source.trim_end(), page_number));
|
||||
Some((unit.id, source))
|
||||
})
|
||||
.collect();
|
||||
let mut changed = false;
|
||||
@@ -5839,6 +5871,7 @@ fn stamp_toc_pages(document: &mut Document, engine: &Engine, geometry: &PageGeom
|
||||
replacement.space_stretch = previous.space_stretch;
|
||||
replacement.space_shrink = previous.space_shrink;
|
||||
replacement.toc_target = previous.toc_target;
|
||||
replacement.toc_stamped = true;
|
||||
document.units[index] = replacement;
|
||||
changed = true;
|
||||
}
|
||||
@@ -7789,18 +7822,36 @@ A definition.
|
||||
.collect();
|
||||
assert_eq!(entries.len(), 2, "{entries:?}");
|
||||
assert!(
|
||||
entries[0].source.ends_with("\\hfill 1"),
|
||||
entries[0].source.contains("..") && entries[0].source.ends_with(" 1"),
|
||||
"{}",
|
||||
entries[0].source
|
||||
);
|
||||
assert!(
|
||||
entries[1].source.ends_with("\\hfill 2"),
|
||||
entries[1].source.contains("..") && entries[1].source.ends_with(" 2"),
|
||||
"{}",
|
||||
entries[1].source
|
||||
);
|
||||
assert!(!stamp_toc_pages(&mut document, &engine, &geometry));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn toc_leaders_fill_the_measure() {
|
||||
let measure = 300 * SCALE;
|
||||
let leader = toc_leader("1 Intro", "1", measure).expect("leader");
|
||||
assert!(leader.starts_with("1 Intro "), "{leader}");
|
||||
assert!(leader.ends_with(" 1"), "{leader}");
|
||||
assert!(leader.contains(".."), "{leader}");
|
||||
let width = text_width_roman(&leader);
|
||||
assert!(width <= measure, "leader overflows: {width} > {measure}");
|
||||
let dot = Cmr10Metrics::Roman.width('.');
|
||||
assert!(
|
||||
measure - width < dot + SCALE / 2,
|
||||
"short by {}",
|
||||
measure - width
|
||||
);
|
||||
assert!(toc_leader("1 Intro", "1", 10 * SCALE).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn float_lists_gain_page_numbers() {
|
||||
let source = "\\listoffigures\n\n\\newpage\n\n\\begin{figure}[t]\n\\caption{Picture}\n\\end{figure}\n\nBody.";
|
||||
@@ -7818,7 +7869,7 @@ A definition.
|
||||
.collect();
|
||||
assert_eq!(entries.len(), 1, "{entries:?}");
|
||||
assert!(
|
||||
entries[0].source.ends_with("\\hfill 2"),
|
||||
entries[0].source.contains("..") && entries[0].source.ends_with(" 2"),
|
||||
"{}",
|
||||
entries[0].source
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user