Support hang indentation and parshape measures

This commit is contained in:
gloamxun committed 2026-10-23 01:18:19 +08:00
1 parent 24a96d1c7c
commit 8993186005
7 files changed
+269 -88

No files matched your search

+51 -4
View File
@@ -27,6 +27,9 @@ struct Word {
#[derive(Clone, Debug)]
struct Line {
words: Vec<Word>,
/// Absolute x of the line's first word in TeX points (page origin).
#[allow(dead_code)]
start: f64,
}
fn temp_dir(tag: &str) -> PathBuf {
@@ -112,7 +115,10 @@ fn parse_bbox(xhtml: &str) -> Vec<Line> {
let x_min = attr(tag, "xMin");
let y_min = attr(tag, "yMin");
if lines.is_empty() || (y_min - current_y).abs() > 2.0 {
lines.push(Line { words: Vec::new() });
lines.push(Line {
words: Vec::new(),
start: 0.0,
});
current_y = y_min;
}
lines
@@ -124,6 +130,7 @@ fn parse_bbox(xhtml: &str) -> Vec<Line> {
}
for line in &mut lines {
let origin = line.words.first().map(|word| word.x).unwrap_or(0.0);
line.start = origin * BP_TO_PT;
for word in &mut line.words {
word.x = (word.x - origin) * BP_TO_PT;
}
@@ -155,8 +162,13 @@ fn route_d_lines_with(
other => panic!("expected a paragraph unit, got {other:?}"),
};
let mut lines = Vec::new();
for line in &paragraph.lines {
let mut x = paragraph.left_skip as f64 / PT as f64;
for (line_number, line) in paragraph.lines.iter().enumerate() {
let indent = paragraph
.line_indents
.get(line_number)
.copied()
.unwrap_or(0);
let mut x = (paragraph.left_skip + indent) as f64 / PT as f64;
let mut words: Vec<Word> = Vec::new();
let mut word = String::new();
let mut word_x = 0.0;
@@ -189,12 +201,13 @@ fn route_d_lines_with(
x: word_x,
});
}
let start = words.first().map(|word| word.x).unwrap_or(0.0);
if let Some(origin) = words.first().map(|word| word.x) {
for word in &mut words {
word.x -= origin;
}
}
lines.push(Line { words });
lines.push(Line { words, start });
}
lines
}
@@ -286,6 +299,40 @@ fn hyphen_penalty_inhibits_hyphenation() {
});
}
/// `\hangindent`/`\hangafter` shift the affected lines and narrow their
/// measure exactly like pdfLaTeX.
#[test]
fn hang_indent_shifts_lines_like_tex() {
let width = 120;
let Some(reference) = pdftex_lines(&paragraph_fixture_with(
WORDS,
width,
"\\hangindent=20pt\\hangafter=1\\relax ",
)) else {
eprintln!("skipping: pdflatex/pdftotext are not installed");
return;
};
let ours = route_d_lines_with(WORDS, f64::from(width), |style| {
style.hang_indent = 20 * PT;
style.hang_after = 1;
});
assert_lines_match("hangindent 120pt", &ours, &reference);
assert!(ours.len() >= 3, "fixture must span at least three lines");
for index in 1..ours.len() {
let our_step = ours[index].start - ours[index - 1].start;
let tex_step = reference[index].start - reference[index - 1].start;
assert!(
(our_step - tex_step).abs() <= 0.1,
"line {} moved {our_step:.3}pt, pdfLaTeX {tex_step:.3}pt",
index + 1
);
}
assert!(
ours[1].start - ours[0].start > 18.0,
"the second line must start 20pt further right"
);
}
#[test]
fn paragraph_breaks_match_pdftex_at_150pt() {
compare_fixture(WORDS, 150);