From 34c4f0d84b04e070a9b87c38f8e73919af1fedbe Mon Sep 17 00:00:00 2001 From: GloamXun Date: Tue, 20 Oct 2026 18:49:27 +0800 Subject: [PATCH] Offset route-D paragraphs by their left skip --- crates/texrra-tex-core/src/paragraph.rs | 25 ++++++++++++++++++++++++- crates/texrra-tex-core/src/render.rs | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/texrra-tex-core/src/paragraph.rs b/crates/texrra-tex-core/src/paragraph.rs index a43a116..9d18a7e 100755 --- a/crates/texrra-tex-core/src/paragraph.rs +++ b/crates/texrra-tex-core/src/paragraph.rs @@ -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 { diff --git a/crates/texrra-tex-core/src/render.rs b/crates/texrra-tex-core/src/render.rs index 5bbe52a..e2fead0 100755 --- a/crates/texrra-tex-core/src/render.rs +++ b/crates/texrra-tex-core/src/render.rs @@ -350,7 +350,7 @@ impl Page { let size = ((line_height as f64) * 0.7) as i64; for line in ¶graph.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_) => {