Offset route-D paragraphs by their left skip
This commit is contained in:
1 parent
7e45732602
commit
34c4f0d84b
2 files changed
+25
-2
No files matched your search
@@ -35,6 +35,8 @@ pub enum HorizontalItem {
|
|||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct ParagraphStyle {
|
pub struct ParagraphStyle {
|
||||||
pub line_width: i64,
|
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`).
|
/// Right skip reserved at the end of every line (`\rightskip`).
|
||||||
pub right_skip: i64,
|
pub right_skip: i64,
|
||||||
pub par_indent: i64,
|
pub par_indent: i64,
|
||||||
@@ -49,6 +51,7 @@ impl Default for ParagraphStyle {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
line_width: 400,
|
line_width: 400,
|
||||||
|
left_skip: 0,
|
||||||
right_skip: 0,
|
right_skip: 0,
|
||||||
par_indent: 0,
|
par_indent: 0,
|
||||||
line_height: 12,
|
line_height: 12,
|
||||||
@@ -81,6 +84,8 @@ pub struct Paragraph {
|
|||||||
pub width: i64,
|
pub width: i64,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub font: ParagraphFont,
|
pub font: ParagraphFont,
|
||||||
|
#[serde(default)]
|
||||||
|
pub left_skip: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Paragraph {
|
impl Paragraph {
|
||||||
@@ -138,6 +143,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
|||||||
height: 0,
|
height: 0,
|
||||||
width: 0,
|
width: 0,
|
||||||
font: ParagraphFont::Roman,
|
font: ParagraphFont::Roman,
|
||||||
|
left_skip: style.left_skip,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
let mut breakpoints = vec![0_usize];
|
let mut breakpoints = vec![0_usize];
|
||||||
@@ -206,7 +212,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
|||||||
if !has_box {
|
if !has_box {
|
||||||
continue;
|
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 {
|
- if start_index == 0 {
|
||||||
style.par_indent
|
style.par_indent
|
||||||
} else {
|
} else {
|
||||||
@@ -307,6 +313,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
|||||||
width: max_width,
|
width: max_width,
|
||||||
lines,
|
lines,
|
||||||
font: ParagraphFont::Roman,
|
font: ParagraphFont::Roman,
|
||||||
|
left_skip: style.left_skip,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -505,6 +512,22 @@ mod tests {
|
|||||||
assert!(paragraph.height > 0);
|
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]
|
#[test]
|
||||||
fn right_skip_narrows_the_measure() {
|
fn right_skip_narrows_the_measure() {
|
||||||
let narrow = ParagraphStyle {
|
let narrow = ParagraphStyle {
|
||||||
|
|||||||
@@ -350,7 +350,7 @@ impl Page {
|
|||||||
let size = ((line_height as f64) * 0.7) as i64;
|
let size = ((line_height as f64) * 0.7) as i64;
|
||||||
for line in ¶graph.lines {
|
for line in ¶graph.lines {
|
||||||
let baseline = y + ((line_height as f64) * 0.8) as i64;
|
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 {
|
for item in line {
|
||||||
match item {
|
match item {
|
||||||
HorizontalItem::Box(box_) => {
|
HorizontalItem::Box(box_) => {
|
||||||
|
|||||||
Reference in new issue
Block a user