Space list items with topsep and itemsep

This commit is contained in:
gloamxun committed 2026-10-25 01:25:21 +08:00
1 parent fa99aaccfa
commit 70ef8f3c8c
4 files changed
+136 -18

No files matched your search

+132 -14
View File
@@ -60,10 +60,11 @@ pub fn project_preview_json_with_images(
) -> Result<String, String> {
let (body, mut geometry) = geometry_and_body(source);
geometry.images = images.clone();
let paragraphs = paragraphs_with_counters(
let paragraphs = paragraphs_with_geometry(
&body,
&geometry.theorem_definitions,
geometry_counter(&geometry, "equation"),
&geometry,
)?;
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
@@ -146,6 +147,10 @@ struct PageGeometry {
linespread: Option<i64>,
/// `\tabcolsep` on each side of a tabular column.
tab_col_sep: Option<i64>,
/// `\itemsep` between list items, as width/stretch/shrink.
item_sep: Option<(i64, i64, i64)>,
/// `\topsep` above a list, as width/stretch/shrink.
top_sep: Option<(i64, i64, i64)>,
/// `\arraystretch` factor in thousandths.
array_stretch: Option<i64>,
/// `\\figurename` used as the caption prefix.
@@ -1213,6 +1218,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.par_skip = Some(width);
geometry.par_skip_stretch = Some(stretch);
geometry.par_skip_shrink = Some(shrink);
} else if let Some(spacing) = setlength_glue(trimmed, "itemsep") {
geometry.item_sep = Some(spacing);
} else if let Some(spacing) = setlength_glue(trimmed, "topsep") {
geometry.top_sep = Some(spacing);
} else if let Some((name, raw)) = addtolength_assignment(trimmed) {
if let Some(delta) = length_value(&geometry, &raw)
&& let Some((_, current)) = geometry
@@ -1276,6 +1285,10 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
geometry.par_skip = Some(width);
geometry.par_skip_stretch = Some(stretch);
geometry.par_skip_shrink = Some(shrink);
} else if let Some(spacing) = glue_assignment(trimmed, "itemsep") {
geometry.item_sep = Some(spacing);
} else if let Some(spacing) = glue_assignment(trimmed, "topsep") {
geometry.top_sep = Some(spacing);
} else if let Some(value) = dimension_assignment(trimmed, "baselineskip") {
geometry.baseline_skip = Some(value);
} else if let Some(value) = dimension_assignment(trimmed, "tabcolsep") {
@@ -1849,10 +1862,11 @@ fn layout_document(document: &Document) -> Engine {
/// Build a JSON preview from blank-line-separated plain-text paragraphs.
pub fn preview_json(source: &str) -> Result<String, String> {
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs_with_counters(
let paragraphs = paragraphs_with_geometry(
&body,
&geometry.theorem_definitions,
geometry_counter(&geometry, "equation"),
&geometry,
)?;
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
@@ -1878,10 +1892,11 @@ pub fn pdf_bytes_with_images(
) -> Result<Vec<u8>, String> {
let (body, mut geometry) = geometry_and_body(source);
geometry.images = images.clone();
let paragraphs = paragraphs_with_counters(
let paragraphs = paragraphs_with_geometry(
&body,
&geometry.theorem_definitions,
geometry_counter(&geometry, "equation"),
&geometry,
)?;
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
@@ -1920,10 +1935,11 @@ pub fn pdf_bytes_with_images(
/// checkpoints that were reused versus rebuilt.
pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
let (body, geometry) = geometry_and_body(source);
let paragraphs = paragraphs_with_counters(
let paragraphs = paragraphs_with_geometry(
&body,
&geometry.theorem_definitions,
geometry_counter(&geometry, "equation"),
&geometry,
)?;
if unit >= paragraphs.len() {
return Err(format!(
@@ -2175,6 +2191,34 @@ fn paragraphs_with_counters(
source: &str,
definitions: &[TheoremDefinition],
equation_start: u32,
) -> Result<Vec<SourceParagraph>, String> {
paragraphs_with_spacing(
source,
definitions,
equation_start,
ListSpacing::article(&PageGeometry::default()),
)
}
fn paragraphs_with_geometry(
source: &str,
definitions: &[TheoremDefinition],
equation_start: u32,
geometry: &PageGeometry,
) -> Result<Vec<SourceParagraph>, String> {
paragraphs_with_spacing(
source,
definitions,
equation_start,
ListSpacing::article(geometry),
)
}
fn paragraphs_with_spacing(
source: &str,
definitions: &[TheoremDefinition],
equation_start: u32,
spacing: ListSpacing,
) -> Result<Vec<SourceParagraph>, String> {
let paragraphs: Vec<&str> = source
.split("\n\n")
@@ -2228,7 +2272,7 @@ fn paragraphs_with_counters(
pending.2 += skip.2;
continue;
}
let mut blocks = parse_blocks_raw(chunk, definitions);
let mut blocks = parse_blocks_raw(chunk, definitions, spacing);
if let Some(first) = blocks.first_mut() {
first.space_before += pending.0;
first.space_stretch += pending.1;
@@ -2934,7 +2978,37 @@ fn bullet_label(depth: usize) -> &'static str {
}
}
fn list_paragraph(text: String, depth: usize) -> SourceParagraph {
/// Vertical glue around list items, in points as [width, stretch, shrink].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct ListSpacing {
top: (i64, i64, i64),
item: (i64, i64, i64),
}
impl ListSpacing {
fn article(geometry: &PageGeometry) -> Self {
let base = geometry.base_font_size.unwrap_or(10 * SCALE);
let (top, item) = match base / SCALE {
12 => (10, 6),
11 => (9, 5),
_ => (8, 4),
};
let points = |value: (i64, i64, i64)| (value.0 / SCALE, value.1 / SCALE, value.2 / SCALE);
Self {
top: geometry.top_sep.map(points).unwrap_or((top, 2, 4)),
item: geometry.item_sep.map(points).unwrap_or((item, 2, 1)),
}
}
fn next(&self, placed: &mut bool) -> Option<(i64, i64, i64)> {
let space = if *placed { self.item } else { self.top };
*placed = true;
Some(space)
}
}
fn list_paragraph(text: String, depth: usize, space: Option<(i64, i64, i64)>) -> SourceParagraph {
let (before, stretch, shrink) = space.unwrap_or((0, 0, 0));
SourceParagraph {
text,
font: ParagraphFont::Roman,
@@ -2943,9 +3017,9 @@ fn list_paragraph(text: String, depth: usize) -> SourceParagraph {
left_skip: 12 * (depth as i64 + 1),
centered: false,
right_aligned: false,
space_before: 0,
space_stretch: 0,
space_shrink: 0,
space_before: before,
space_stretch: stretch,
space_shrink: shrink,
page_break: false,
equation_number: None,
equation_tag: None,
@@ -3005,9 +3079,11 @@ fn list_blocks(
raw: &str,
depth: usize,
_definitions: &[TheoremDefinition],
spacing: ListSpacing,
) -> Option<Vec<SourceParagraph>> {
let (environment, body) = list_environment(raw)?;
let mut blocks = Vec::new();
let mut placed = false;
let mut number = 0_u32;
for item in split_list_items(body) {
let item = item.trim();
@@ -3055,15 +3131,19 @@ fn list_blocks(
};
let (before, nested, after) = split_nested_list(rest);
if !before.trim().is_empty() {
blocks.push(list_paragraph(format!("{label}{}", before.trim()), depth));
blocks.push(list_paragraph(
format!("{label}{}", before.trim()),
depth,
spacing.next(&mut placed),
));
}
if let Some(nested) = nested {
if let Some(nested_blocks) = list_blocks(&nested, depth + 1, _definitions) {
if let Some(nested_blocks) = list_blocks(&nested, depth + 1, _definitions, spacing) {
blocks.extend(nested_blocks);
}
}
if !after.trim().is_empty() {
blocks.push(list_paragraph(after.trim().to_string(), depth));
blocks.push(list_paragraph(after.trim().to_string(), depth, None));
}
}
if blocks.is_empty() {
@@ -3126,7 +3206,11 @@ fn standalone_graphic(raw: &str) -> Option<SourceParagraph> {
})
}
fn parse_blocks_raw(raw: &str, definitions: &[TheoremDefinition]) -> Vec<SourceParagraph> {
fn parse_blocks_raw(
raw: &str,
definitions: &[TheoremDefinition],
spacing: ListSpacing,
) -> Vec<SourceParagraph> {
if let Some(block) = float_block(raw) {
return vec![block];
}
@@ -3221,7 +3305,7 @@ fn parse_blocks_raw(raw: &str, definitions: &[TheoremDefinition]) -> Vec<SourceP
if let Some(blocks) = verbatim_blocks(raw) {
return blocks;
}
if let Some(blocks) = list_blocks(raw, 0, definitions) {
if let Some(blocks) = list_blocks(raw, 0, definitions, spacing) {
return blocks;
}
if let Some(body) = raw
@@ -6471,6 +6555,40 @@ See \ref{fig:a} and \ref{tab:b}.";
assert!(document.units.iter().all(|unit| unit.source != today));
}
#[test]
fn list_items_take_article_separations() {
let blocks =
paragraphs("\\begin{itemize}\n\\item One\n\\item Two\n\\end{itemize}").expect("blocks");
assert_eq!(blocks[0].space_before, 8, "article topsep at 10pt");
assert_eq!(blocks[0].space_stretch, 2);
assert_eq!(blocks[0].space_shrink, 4);
assert_eq!(blocks[1].space_before, 4, "article itemsep at 10pt");
assert_eq!(blocks[1].space_stretch, 2);
assert_eq!(blocks[1].space_shrink, 1);
}
#[test]
fn list_separations_follow_setlength() {
let source = "\\setlength{\\topsep}{3pt plus 1pt minus 2pt}\n\\setlength{\\itemsep}{6pt plus 2pt minus 1pt}\n\n\\begin{enumerate}\n\\item One\n\\item Two\n\\end{enumerate}";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs_with_geometry(&body, &[], 0, &geometry).expect("blocks");
assert_eq!(blocks[0].space_before, 3);
assert_eq!(blocks[0].space_stretch, 1);
assert_eq!(blocks[0].space_shrink, 2);
assert_eq!(blocks[1].space_before, 6);
assert_eq!(blocks[1].space_stretch, 2);
assert_eq!(blocks[1].space_shrink, 1);
}
#[test]
fn list_separations_follow_the_class_size() {
let source = "\\documentclass[12pt]{article}\n\\begin{document}\n\n\\begin{itemize}\n\\item One\n\\item Two\n\\end{itemize}\n\n\\end{document}";
let (body, geometry) = geometry_and_body(source);
let blocks = paragraphs_with_geometry(&body, &[], 0, &geometry).expect("blocks");
assert_eq!(blocks[0].space_before, 10);
assert_eq!(blocks[1].space_before, 6);
}
#[test]
fn nested_itemize_indents_and_relabels() {
let source = "\\begin{itemize}\n\\item Outer\n\\begin{itemize}\n\\item Inner\n\\end{itemize}\n\\end{itemize}";