Hang list continuation lines at the text indent

This commit is contained in:
gloamxun committed 2026-10-25 01:32:05 +08:00
1 parent 16b9ea4a79
commit a9ab265be4
4 files changed
+121 -12

No files matched your search

+117 -8
View File
@@ -2014,6 +2014,10 @@ struct SourceParagraph {
float: Option<FloatSpec>,
/// A theorem-like environment heading.
theorem: Option<TheoremSpec>,
/// Nesting depth when this paragraph is a list item.
list_depth: Option<u8>,
/// Width of the generated list label, in points.
list_label: i64,
}
/// The kind of a LaTeX float environment.
@@ -2240,6 +2244,8 @@ fn paragraphs_with_spacing(
for chunk in paragraphs {
if let Some(index) = table_reference(chunk) {
result.push(SourceParagraph {
list_depth: None,
list_label: 0,
text: format!("\\texrratable{{{index}}}"),
font: ParagraphFont::Roman,
line_height: 12,
@@ -2422,6 +2428,8 @@ fn float_block(raw: &str) -> Option<SourceParagraph> {
figure_graphic(inner).unwrap_or((None, None, None, None, false));
let table = embedded_table(inner);
Some(SourceParagraph {
list_depth: None,
list_label: 0,
text: String::new(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -2735,6 +2743,8 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
return None;
}
let heading = SourceParagraph {
list_depth: None,
list_label: 0,
text: format!("{display}."),
font: ParagraphFont::Bold,
line_height: 14,
@@ -2771,6 +2781,8 @@ fn theorem_block(raw: &str, definitions: &[TheoremDefinition]) -> Option<Vec<Sou
"theorem" | "lemma" | "corollary" | "proposition" | "proof"
);
let statement = SourceParagraph {
list_depth: None,
list_label: 0,
text: body,
font: if italic {
ParagraphFont::Italic
@@ -3048,7 +3060,24 @@ impl ListSpacing {
}
}
fn list_paragraph(text: String, depth: usize, space: Option<(i64, i64, i64)>) -> SourceParagraph {
fn list_label_width(label: &str) -> i64 {
let metrics = Cmr10Metrics::Roman;
let total: i64 = label
.chars()
.map(|character| match character {
' ' => metrics.space(),
_ => metrics.width(character),
})
.sum();
((total + SCALE - 1) / SCALE).max(1)
}
fn list_paragraph(
text: String,
depth: usize,
space: Option<(i64, i64, i64)>,
label_width: i64,
) -> SourceParagraph {
let (before, stretch, shrink) = space.unwrap_or((0, 0, 0));
SourceParagraph {
text,
@@ -3074,6 +3103,8 @@ fn list_paragraph(text: String, depth: usize, space: Option<(i64, i64, i64)>) ->
theorem: None,
float: None,
font_size: None,
list_depth: Some(depth as u8),
list_label: label_width,
}
}
@@ -3088,6 +3119,8 @@ fn verbatim_blocks(raw: &str) -> Option<Vec<SourceParagraph>> {
for (index, line) in inner.lines().enumerate() {
let text = line.trim_end();
blocks.push(SourceParagraph {
list_depth: None,
list_label: 0,
text: text.to_string(),
page_break: false,
centered: false,
@@ -3182,6 +3215,7 @@ fn list_blocks(
format!("{label}{}", before.trim()),
depth,
space,
list_label_width(&label),
));
}
if let Some(nested) = nested {
@@ -3190,7 +3224,12 @@ fn list_blocks(
}
}
if !after.trim().is_empty() {
blocks.push(list_paragraph(after.trim().to_string(), depth, None));
blocks.push(list_paragraph(
after.trim().to_string(),
depth,
None,
list_label_width(&label),
));
}
}
if blocks.is_empty() {
@@ -3214,6 +3253,8 @@ fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
}
let (width, height, image, scale, keep_aspectratio) = figure_graphic(trimmed)?;
Some(SourceParagraph {
list_depth: None,
list_label: 0,
text: String::new(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -3273,6 +3314,8 @@ fn parse_blocks_raw(
.and_then(|rest| rest.strip_suffix("\\end{abstract}"))
{
let heading = SourceParagraph {
list_depth: None,
list_label: 0,
text: "Abstract".to_string(),
font: ParagraphFont::Bold,
line_height: 12,
@@ -3298,6 +3341,8 @@ fn parse_blocks_raw(
font_size: Some((12.0 * SCALE as f64) as i64),
};
let body = SourceParagraph {
list_depth: None,
list_label: 0,
text: body.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -3362,6 +3407,8 @@ fn parse_blocks_raw(
let text = body.trim();
if !text.is_empty() {
return vec![SourceParagraph {
list_depth: None,
list_label: 0,
text: text.to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -4137,6 +4184,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
})
.collect();
return SourceParagraph {
list_depth: None,
list_label: 0,
text: inner.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -4165,6 +4214,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
if let Some((font, font_size, body)) = nfss_font_with_size(raw, ParagraphFont::Roman) {
return SourceParagraph {
list_depth: None,
list_label: 0,
text: body,
font,
font_size,
@@ -4195,6 +4246,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
.and_then(|text| text.strip_suffix("\\end{equation}"))
{
return SourceParagraph {
list_depth: None,
list_label: 0,
text: inner.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -4225,6 +4278,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
.and_then(|text| text.strip_suffix("\\end{equation*}"))
{
return SourceParagraph {
list_depth: None,
list_label: 0,
text: inner.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -4259,6 +4314,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
})
{
return SourceParagraph {
list_depth: None,
list_label: 0,
text: math.trim().to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -4286,6 +4343,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
if let Some((level, starred, title, short)) = heading_command(raw) {
return SourceParagraph {
list_depth: None,
list_label: 0,
text: title,
font: ParagraphFont::Bold,
line_height: if level == 1 {
@@ -4333,6 +4392,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
for (command, font, line_height) in candidates {
if let Some(text) = command_body(raw, command) {
return SourceParagraph {
list_depth: None,
list_label: 0,
text: text.to_string(),
font,
line_height,
@@ -4360,6 +4421,8 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
}
}
SourceParagraph {
list_depth: None,
list_label: 0,
text: raw.to_string(),
font: ParagraphFont::Roman,
line_height: 12,
@@ -4539,6 +4602,8 @@ fn clean_title_value(text: &str) -> String {
fn title_block(text: String, size: f64, space_before: i64) -> SourceParagraph {
SourceParagraph {
list_depth: None,
list_label: 0,
text,
font: ParagraphFont::Roman,
line_height: 12,
@@ -4908,6 +4973,8 @@ fn number_floats(
/// Append `thebibliography` entries as numbered paragraphs.
fn bibliography_heading(text: &str) -> SourceParagraph {
SourceParagraph {
list_depth: None,
list_label: 0,
text: text.to_string(),
page_break: false,
centered: false,
@@ -4945,6 +5012,8 @@ fn append_bibliography(
paragraphs.push(bibliography_heading(heading));
for (index, (_, entry)) in bibliography.iter().enumerate() {
paragraphs.push(SourceParagraph {
list_depth: None,
list_label: 0,
text: format!("[{}] {entry}", index + 1),
page_break: false,
centered: false,
@@ -5008,6 +5077,8 @@ fn expand_toc(paragraphs: &mut Vec<SourceParagraph>, tocdepth: u32, contents_nam
toc.push(heading);
for (level, number, text) in entries.iter() {
toc.push(SourceParagraph {
list_depth: None,
list_label: 0,
text: format!("\\texrratocpage{{{number}}} {text}"),
page_break: false,
centered: false,
@@ -5088,6 +5159,8 @@ fn expand_float_lists(
list.push(heading);
for (prefix, text) in entries.iter() {
list.push(SourceParagraph {
list_depth: None,
list_label: 0,
text: format!("\\texrrafloatpage{{{prefix}}} {text}"),
page_break: false,
centered: false,
@@ -5361,9 +5434,24 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
.linespread
.map(|spread| baselineskip.saturating_mul(spread) / 1000)
.unwrap_or(baselineskip);
let (left_skip, par_indent) = match paragraph.list_depth {
Some(_) => {
let indent = paragraph.left_skip * SCALE + geometry.left_skip.unwrap_or(0);
let hang = (paragraph.list_label + 5) * SCALE;
(indent + hang, -hang)
}
None => (
paragraph.left_skip * SCALE + geometry.left_skip.unwrap_or(0),
if no_indent {
0
} else {
geometry.par_indent.unwrap_or(0)
},
),
};
let style = ParagraphStyle {
line_width,
left_skip: paragraph.left_skip * SCALE + geometry.left_skip.unwrap_or(0),
left_skip,
centered: paragraph.centered,
right_aligned: paragraph.right_aligned,
looseness: geometry.looseness.unwrap_or(0),
@@ -5386,11 +5474,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
baselineskip,
lineskip: geometry.lineskip.unwrap_or(65536),
lineskiplimit: geometry.lineskiplimit.unwrap_or(0),
par_indent: if no_indent {
0
} else {
geometry.par_indent.unwrap_or(0)
},
par_indent,
emergencystretch: geometry.emergency_stretch.unwrap_or(0),
pretolerance: geometry.pretolerance.unwrap_or(100),
tolerance: geometry.tolerance.unwrap_or(200),
@@ -6636,6 +6720,31 @@ See \ref{fig:a} and \ref{tab:b}.";
assert_eq!(blocks[1].space_before, 5, "12pt itemsep");
}
#[test]
fn list_items_hang_their_continuation_lines() {
let source = "\\begin{itemize}\n\\item alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu nu xi omicron\n\\end{itemize}";
let blocks = paragraphs(source).expect("paragraphs");
assert_eq!(blocks[0].list_depth, Some(0));
let geometry = PageGeometry {
line_width: Some(140 * SCALE),
..PageGeometry::default()
};
let document = build_document(&blocks, &geometry);
let paragraph = document.units[0]
.items
.iter()
.find_map(|item| match item {
VerticalItem::Paragraph(paragraph) => Some(paragraph),
_ => None,
})
.expect("list paragraph");
assert!(paragraph.lines.len() > 1, "the item must wrap");
let hang = (blocks[0].list_label + 5) * SCALE;
assert_eq!(paragraph.left_skip, 12 * SCALE + hang);
assert_eq!(paragraph.line_indents[0], -hang);
assert_eq!(paragraph.line_indents[1], 0);
}
#[test]
fn nested_lists_use_the_second_level_skips() {
let source = "\\begin{itemize}\n\\item Outer\n\\begin{itemize}\n\\item Inner\n\\item More\n\\end{itemize}\n\\item Next\n\\end{itemize}";