Support explicit vertical skips between paragraphs
This commit is contained in:
1 parent
d596ef2a95
commit
0102a20120
4 files changed
+150
-7
No files matched your search
+59
-4
@@ -510,6 +510,20 @@ struct SourceParagraph {
|
||||
line_height: i64,
|
||||
math: bool,
|
||||
left_skip: i64,
|
||||
space_before: i64,
|
||||
}
|
||||
|
||||
/// Recognise an explicit vertical skip command standing alone between
|
||||
/// paragraphs.
|
||||
fn vertical_skip(text: &str) -> Option<i64> {
|
||||
match text.trim() {
|
||||
"\\smallskip" => Some(3 * SCALE),
|
||||
"\\medskip" => Some(6 * SCALE),
|
||||
"\\bigskip" => Some(12 * SCALE),
|
||||
other => other
|
||||
.strip_prefix("\\vskip")
|
||||
.and_then(|rest| parse_dimension(rest.trim_start())),
|
||||
}
|
||||
}
|
||||
|
||||
fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
@@ -521,7 +535,21 @@ fn paragraphs(source: &str) -> Result<Vec<SourceParagraph>, String> {
|
||||
if paragraphs.is_empty() {
|
||||
return Err("route-D preview needs at least one paragraph".to_string());
|
||||
}
|
||||
Ok(paragraphs.into_iter().flat_map(parse_blocks).collect())
|
||||
let mut result = Vec::new();
|
||||
let mut pending = 0_i64;
|
||||
for chunk in paragraphs {
|
||||
if let Some(skip) = vertical_skip(chunk) {
|
||||
pending += skip;
|
||||
continue;
|
||||
}
|
||||
let mut blocks = parse_blocks(chunk);
|
||||
if let Some(first) = blocks.first_mut() {
|
||||
first.space_before += pending;
|
||||
pending = 0;
|
||||
}
|
||||
result.extend(blocks);
|
||||
}
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn command_body<'a>(text: &'a str, command: &str) -> Option<&'a str> {
|
||||
@@ -560,6 +588,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
left_skip: 12,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -581,6 +610,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
left_skip: 12,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
}];
|
||||
}
|
||||
}
|
||||
@@ -611,6 +641,7 @@ fn parse_blocks(raw: &str) -> Vec<SourceParagraph> {
|
||||
left_skip: 12,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
@@ -641,6 +672,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
left_skip: 0,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
};
|
||||
}
|
||||
let candidates = [
|
||||
@@ -658,6 +690,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
left_skip: 0,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -669,6 +702,7 @@ fn parse_paragraph(raw: &str) -> SourceParagraph {
|
||||
left_skip: 0,
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
space_before: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -678,11 +712,13 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
.enumerate()
|
||||
.map(|(index, paragraph)| {
|
||||
if paragraph.math {
|
||||
return Unit::math(
|
||||
let mut unit = Unit::math(
|
||||
index as u64,
|
||||
parse_math(¶graph.text),
|
||||
&Cmr10Metrics::default(),
|
||||
);
|
||||
unit.space_before = paragraph.space_before;
|
||||
return unit;
|
||||
}
|
||||
let style = ParagraphStyle {
|
||||
line_width: 480 * SCALE,
|
||||
@@ -732,12 +768,14 @@ fn build_document(paragraphs: &[SourceParagraph], geometry: &PageGeometry) -> Do
|
||||
space_skip: geometry.space_skip,
|
||||
xspace_skip: geometry.xspace_skip,
|
||||
};
|
||||
Unit::paragraph_with_style_and_face(
|
||||
let mut unit = Unit::paragraph_with_style_and_face(
|
||||
index as u64,
|
||||
paragraph.text.clone(),
|
||||
style,
|
||||
paragraph.font,
|
||||
)
|
||||
);
|
||||
unit.space_before = paragraph.space_before;
|
||||
unit
|
||||
})
|
||||
.collect();
|
||||
Document::new(700 * SCALE, units)
|
||||
@@ -1046,4 +1084,21 @@ mod source_tests {
|
||||
plain_y
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn paragraphs_read_vertical_skip_commands() {
|
||||
let source = "Alpha.\n\n\\medskip\n\nBeta.\n\n\\bigskip\n\nGamma.";
|
||||
let parsed = paragraphs(source).expect("paragraphs");
|
||||
assert_eq!(parsed.len(), 3);
|
||||
assert_eq!(parsed[0].space_before, 0);
|
||||
assert_eq!(parsed[1].space_before, 6 * SCALE);
|
||||
assert_eq!(parsed[2].space_before, 12 * SCALE);
|
||||
assert!(
|
||||
!parsed
|
||||
.iter()
|
||||
.any(|paragraph| paragraph.text.contains("skip"))
|
||||
);
|
||||
let document = build_document(&parsed, &PageGeometry::default());
|
||||
assert_eq!(document.units[1].space_before, 6 * SCALE);
|
||||
assert_eq!(document.units[2].space_before, 12 * SCALE);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user