Honour textwidth and setlength as the paragraph measure
This commit is contained in:
1 parent
c494ad04ae
commit
6ae609b60e
4 files changed
+128
-7
No files matched your search
+124
-3
@@ -107,6 +107,8 @@ struct PageGeometry {
|
||||
paper_height: Option<i64>,
|
||||
vsize: Option<i64>,
|
||||
textheight: Option<i64>,
|
||||
/// `\textwidth` / `\hsize` / `\linewidth`, in scaled points.
|
||||
line_width: Option<i64>,
|
||||
par_indent: Option<i64>,
|
||||
par_skip: Option<i64>,
|
||||
par_skip_stretch: Option<i64>,
|
||||
@@ -221,6 +223,35 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
|
||||
geometry.vsize = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "textheight") {
|
||||
geometry.textheight = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "textwidth") {
|
||||
geometry.line_width = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "hsize") {
|
||||
geometry.line_width = Some(value);
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "linewidth") {
|
||||
geometry.line_width = Some(value);
|
||||
} else if let Some((width, stretch, shrink)) = setlength_glue(trimmed, "parskip") {
|
||||
geometry.par_skip = Some(width);
|
||||
geometry.par_skip_stretch = Some(stretch);
|
||||
geometry.par_skip_shrink = Some(shrink);
|
||||
} else if let Some((name, value)) = setlength_assignment(trimmed) {
|
||||
match name.as_str() {
|
||||
"textwidth" | "hsize" | "linewidth" => geometry.line_width = Some(value),
|
||||
"parindent" => geometry.par_indent = Some(value),
|
||||
"baselineskip" => geometry.baseline_skip = Some(value),
|
||||
"textheight" => geometry.textheight = Some(value),
|
||||
"vsize" => geometry.vsize = Some(value),
|
||||
"leftskip" => geometry.left_skip = Some(value),
|
||||
"rightskip" => geometry.right_skip = Some(value),
|
||||
"emergencystretch" => geometry.emergency_stretch = Some(value),
|
||||
"hangindent" => geometry.hang_indent = Some(value),
|
||||
"lineskip" => geometry.lineskip = Some(value),
|
||||
"lineskiplimit" => geometry.lineskiplimit = Some(value),
|
||||
"topskip" => geometry.top_skip = Some(value),
|
||||
"maxdepth" => geometry.max_depth = Some(value),
|
||||
"splittopskip" => geometry.split_top_skip = Some(value),
|
||||
"splitmaxdepth" => geometry.split_max_depth = Some(value),
|
||||
_ => {}
|
||||
}
|
||||
} else if let Some(value) = dimension_assignment(trimmed, "parindent") {
|
||||
geometry.par_indent = Some(value);
|
||||
} else if let Some((width, stretch, shrink)) = glue_assignment(trimmed, "parskip") {
|
||||
@@ -563,6 +594,46 @@ fn dimension_assignment(line: &str, name: &str) -> Option<i64> {
|
||||
parse_dimension(rest.trim())
|
||||
}
|
||||
|
||||
/// Read a LaTeX `\setlength{\parskip}{<glue>}` assignment.
|
||||
fn setlength_glue(line: &str, name: &str) -> Option<(i64, i64, i64)> {
|
||||
let rest = line.strip_prefix("\\setlength")?;
|
||||
let rest = rest.trim_start().strip_prefix('{')?;
|
||||
let (target, rest) = rest.split_once('}')?;
|
||||
if target.trim() != format!("\\{name}") {
|
||||
return None;
|
||||
}
|
||||
let rest = rest.trim_start().strip_prefix('{')?;
|
||||
let (value, _) = rest.split_once('}')?;
|
||||
let (width, rest) = split_glue_part(value.trim())?;
|
||||
let mut stretch = 0_i64;
|
||||
let mut shrink = 0_i64;
|
||||
let rest = rest.trim_start();
|
||||
let rest = if let Some(rest) = rest.strip_prefix("plus") {
|
||||
let (value, rest) = split_glue_part(rest.trim_start())?;
|
||||
stretch = value;
|
||||
rest
|
||||
} else {
|
||||
rest
|
||||
};
|
||||
let rest = rest.trim_start();
|
||||
let _ = if let Some(rest) = rest.strip_prefix("minus") {
|
||||
let (value, _) = split_glue_part(rest.trim_start())?;
|
||||
shrink = value;
|
||||
};
|
||||
Some((width, stretch, shrink))
|
||||
}
|
||||
|
||||
/// Read a LaTeX `\setlength{\name}{<dimension>}` assignment.
|
||||
fn setlength_assignment(line: &str) -> Option<(String, i64)> {
|
||||
let rest = line.strip_prefix("\\setlength")?;
|
||||
let rest = rest.trim_start().strip_prefix('{')?;
|
||||
let (target, rest) = rest.split_once('}')?;
|
||||
let name = target.trim().strip_prefix('\\')?;
|
||||
let rest = rest.trim_start().strip_prefix('{')?;
|
||||
let (value, _) = rest.split_once('}')?;
|
||||
parse_dimension(value.trim()).map(|value| (name.to_string(), value))
|
||||
}
|
||||
|
||||
fn parse_dimension(text: &str) -> Option<i64> {
|
||||
let end = text
|
||||
.find(|character: char| {
|
||||
@@ -1638,6 +1709,7 @@ fn aligned_cell(source: &str) -> MathAtom {
|
||||
}
|
||||
|
||||
fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Document {
|
||||
let line_width = geometry.line_width.unwrap_or(480 * SCALE);
|
||||
let units = paragraphs
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -1695,7 +1767,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
},
|
||||
None => atom,
|
||||
};
|
||||
let lines = split_math_lines(&atom, &font, 480 * SCALE);
|
||||
let lines = split_math_lines(&atom, &font, line_width);
|
||||
let mut unit = Unit::math_lines(index as u64, &lines, &font);
|
||||
unit.space_before = paragraph.space_before;
|
||||
unit.space_stretch = paragraph.space_stretch;
|
||||
@@ -1710,7 +1782,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
.baseline_skip
|
||||
.unwrap_or_else(|| class_baselineskip(base_font_size));
|
||||
let style = ParagraphStyle {
|
||||
line_width: 480 * SCALE,
|
||||
line_width,
|
||||
left_skip: paragraph.left_skip * SCALE + geometry.left_skip.unwrap_or(0),
|
||||
centered: paragraph.centered,
|
||||
right_aligned: paragraph.right_aligned,
|
||||
@@ -1781,7 +1853,7 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
let mut document = Document::new(700 * SCALE, units);
|
||||
// LaTeX sets \maxdepth=.5\topskip; sources may override it later.
|
||||
document.max_depth = 5 * SCALE;
|
||||
document.text_width = 480 * SCALE;
|
||||
document.text_width = line_width;
|
||||
document.page_breaks = paragraphs
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -2214,6 +2286,55 @@ mod source_tests {
|
||||
assert_eq!(document.units[3].space_stretch, 2 * SCALE);
|
||||
assert_eq!(document.units[3].space_shrink, SCALE);
|
||||
}
|
||||
#[test]
|
||||
fn text_width_sets_the_paragraph_measure() {
|
||||
let long =
|
||||
"A paragraph with enough words to wrap across several lines in a narrow measure.";
|
||||
let line_count = |source: &str| {
|
||||
let (body, geometry) = geometry_and_body(source);
|
||||
let parsed = paragraphs(&body).expect("paragraphs");
|
||||
let document = build_document(&parsed, &geometry);
|
||||
let engine = texrra_tex_core::Engine::layout(&document);
|
||||
let mut ys: Vec<i64> = Vec::new();
|
||||
for page in engine.pages() {
|
||||
let list = page.display_list();
|
||||
ys.extend(list.items.iter().filter_map(|item| match item {
|
||||
texrra_tex_core::render::DisplayItem::Text { y, .. } => Some(*y),
|
||||
_ => None,
|
||||
}));
|
||||
}
|
||||
ys.sort_unstable();
|
||||
ys.dedup();
|
||||
ys.len()
|
||||
};
|
||||
let wide = line_count(long);
|
||||
let narrow = line_count(&format!("\\textwidth=240pt\n\n{long}"));
|
||||
assert!(wide >= 1, "the wide measure should paint at least one line");
|
||||
assert!(
|
||||
narrow > wide,
|
||||
"narrow measure should wrap into more lines: {narrow} vs {wide}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn setlength_and_hsize_set_the_measure() {
|
||||
let (_, textwidth) = geometry_and_body("\\setlength{\\textwidth}{300pt}\n\nA paragraph.");
|
||||
assert_eq!(textwidth.line_width, Some(300 * SCALE));
|
||||
let (_, hsize) = geometry_and_body("\\hsize=360pt\n\nA paragraph.");
|
||||
assert_eq!(hsize.line_width, Some(360 * SCALE));
|
||||
let (_, lengths) = geometry_and_body(
|
||||
"\\setlength{\\parindent}{20pt}\n\n\\setlength{\\textheight}{500pt}\n\nA paragraph.",
|
||||
);
|
||||
assert_eq!(lengths.line_width, None);
|
||||
assert_eq!(lengths.par_indent, Some(20 * SCALE));
|
||||
assert_eq!(lengths.textheight, Some(500 * SCALE));
|
||||
let (_, glue) =
|
||||
geometry_and_body("\\setlength{\\parskip}{6pt plus 2pt minus 1pt}\n\nA paragraph.");
|
||||
assert_eq!(glue.par_skip, Some(6 * SCALE));
|
||||
assert_eq!(glue.par_skip_stretch, Some(2 * SCALE));
|
||||
assert_eq!(glue.par_skip_shrink, Some(SCALE));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn newpage_starts_a_new_page() {
|
||||
let source = "Alpha beta gamma delta.\n\n\\newpage\n\nEpsilon zeta eta theta.";
|
||||
|
||||
Reference in new issue
Block a user