Offset route-D paragraphs by their left skip

This commit is contained in:
gloamxun committed 2026-10-20 18:49:27 +08:00
1 parent 7e45732602
commit 34c4f0d84b
2 files changed
+25 -2

No files matched your search

+24 -1
View File
@@ -35,6 +35,8 @@ pub enum HorizontalItem {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ParagraphStyle {
pub line_width: i64,
/// Left skip inserted before every line (`\leftskip`).
pub left_skip: i64,
/// Right skip reserved at the end of every line (`\rightskip`).
pub right_skip: i64,
pub par_indent: i64,
@@ -49,6 +51,7 @@ impl Default for ParagraphStyle {
fn default() -> Self {
Self {
line_width: 400,
left_skip: 0,
right_skip: 0,
par_indent: 0,
line_height: 12,
@@ -81,6 +84,8 @@ pub struct Paragraph {
pub width: i64,
#[serde(default)]
pub font: ParagraphFont,
#[serde(default)]
pub left_skip: i64,
}
impl Paragraph {
@@ -138,6 +143,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
height: 0,
width: 0,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
};
}
let mut breakpoints = vec![0_usize];
@@ -206,7 +212,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
if !has_box {
continue;
}
let target = (style.line_width - style.right_skip).max(1)
let target = (style.line_width - style.left_skip - style.right_skip).max(1)
- if start_index == 0 {
style.par_indent
} else {
@@ -307,6 +313,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
width: max_width,
lines,
font: ParagraphFont::Roman,
left_skip: style.left_skip,
}
}
@@ -505,6 +512,22 @@ mod tests {
assert!(paragraph.height > 0);
}
#[test]
fn left_skip_narrows_the_measure_and_offsets_the_paragraph() {
let narrow = ParagraphStyle {
line_width: 150,
left_skip: 50,
..ParagraphStyle::default()
};
let full = ParagraphStyle {
line_width: 150,
..ParagraphStyle::default()
};
let text = "alpha beta gamma delta epsilon zeta eta theta";
let narrow_paragraph = layout_paragraph(text, &narrow);
assert_eq!(narrow_paragraph.left_skip, 50);
assert!(narrow_paragraph.lines.len() > layout_paragraph(text, &full).lines.len());
}
#[test]
fn right_skip_narrows_the_measure() {
let narrow = ParagraphStyle {
+1 -1
View File
@@ -350,7 +350,7 @@ impl Page {
let size = ((line_height as f64) * 0.7) as i64;
for line in &paragraph.lines {
let baseline = y + ((line_height as f64) * 0.8) as i64;
let mut x = 0_i64;
let mut x = paragraph.left_skip;
for item in line {
match item {
HorizontalItem::Box(box_) => {