Add bounded line expansion for Times body text
This commit is contained in:
1 parent
8fef7df187
commit
01b3f518cb
10 files changed
+113
-9
No files matched your search
@@ -136,6 +136,11 @@ fn default_font_size() -> i64 {
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ParagraphStyle {
|
||||
pub line_width: i64,
|
||||
/// Bounded line expansion in thousandths, microtype-like. A line may be
|
||||
/// broken up to `line_width * (1000 + line_expansion) / 1000` and is
|
||||
/// compressed back to the real measure when painted.
|
||||
#[serde(default)]
|
||||
pub line_expansion: i64,
|
||||
/// Left skip inserted before every line (`\leftskip`).
|
||||
pub left_skip: i64,
|
||||
/// Right skip reserved at the end of every line (`\rightskip`).
|
||||
@@ -299,16 +304,22 @@ impl ParagraphStyle {
|
||||
}
|
||||
}
|
||||
|
||||
fn line_target(&self, line: usize) -> i64 {
|
||||
pub fn line_measure(&self, line: usize) -> i64 {
|
||||
let (_, width) = self.line_geometry(line);
|
||||
(width - self.left_skip - self.right_skip).max(1) - self.par_indent_for(line)
|
||||
}
|
||||
|
||||
fn line_target(&self, line: usize) -> i64 {
|
||||
let measure = self.line_measure(line);
|
||||
(measure * (1000 + self.line_expansion) / 1000).max(measure)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ParagraphStyle {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
line_width: 400,
|
||||
line_expansion: 0,
|
||||
left_skip: 0,
|
||||
right_skip: 0,
|
||||
hang_indent: 0,
|
||||
@@ -559,6 +570,10 @@ pub struct Paragraph {
|
||||
/// Baseline of each line relative to the top of the paragraph box.
|
||||
#[serde(default)]
|
||||
pub line_baselines: Vec<i64>,
|
||||
/// Horizontal compression per line (thousandths), used by the bounded
|
||||
/// expansion model when a line was broken against an expanded measure.
|
||||
#[serde(default)]
|
||||
pub line_scales: Vec<i64>,
|
||||
#[serde(default)]
|
||||
pub centered: bool,
|
||||
#[serde(default)]
|
||||
@@ -597,6 +612,11 @@ impl Paragraph {
|
||||
let tail_depth = line_extent(tail_lines.last().map(Vec::as_slice).unwrap_or(&[])).1;
|
||||
self.height = head_height;
|
||||
self.depth = head_depth;
|
||||
let tail_scales = if self.line_scales.len() > lines {
|
||||
self.line_scales.split_off(lines)
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
Paragraph {
|
||||
lines: tail_lines,
|
||||
height: tail_height,
|
||||
@@ -607,6 +627,7 @@ impl Paragraph {
|
||||
left_skip: self.left_skip,
|
||||
line_indents: tail_indents,
|
||||
line_baselines: tail_baselines,
|
||||
line_scales: tail_scales,
|
||||
centered: self.centered,
|
||||
right_aligned: self.right_aligned,
|
||||
}
|
||||
@@ -778,6 +799,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
left_skip: style.left_skip,
|
||||
line_indents: Vec::new(),
|
||||
line_baselines: Vec::new(),
|
||||
line_scales: Vec::new(),
|
||||
centered: style.centered,
|
||||
right_aligned: style.right_aligned,
|
||||
};
|
||||
@@ -1436,6 +1458,27 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
}
|
||||
}
|
||||
let depth = extents.last().map_or(0, |(_, descent)| *descent);
|
||||
let line_scales: Vec<i64> = lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, line)| {
|
||||
let actual = style.line_measure(index).max(1);
|
||||
let natural: i64 = line
|
||||
.iter()
|
||||
.map(|item| match item {
|
||||
HorizontalItem::Box(box_item) => box_item.width,
|
||||
HorizontalItem::Glue { width, .. } => *width,
|
||||
HorizontalItem::Penalty { width, .. } => *width,
|
||||
HorizontalItem::Discretionary { .. } => 0,
|
||||
})
|
||||
.sum();
|
||||
if natural > actual {
|
||||
(actual * 1000 / natural).clamp(900, 1000)
|
||||
} else {
|
||||
1000
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Paragraph {
|
||||
// The page builder still budgets paragraphs on the line grid; the
|
||||
// baseline table below carries TeX's interline glue for painting.
|
||||
@@ -1452,6 +1495,7 @@ pub fn layout_items(items: &[HorizontalItem], style: &ParagraphStyle) -> Paragra
|
||||
left_skip: style.left_skip,
|
||||
line_indents,
|
||||
line_baselines,
|
||||
line_scales,
|
||||
centered: style.centered,
|
||||
right_aligned: style.right_aligned,
|
||||
}
|
||||
@@ -2142,6 +2186,37 @@ mod tests {
|
||||
.count()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bounded_expansion_records_per_line_scales() {
|
||||
let text = "alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu";
|
||||
let base = ParagraphStyle {
|
||||
line_width: 180,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let expanded = ParagraphStyle {
|
||||
line_width: 180,
|
||||
line_expansion: 30,
|
||||
..ParagraphStyle::default()
|
||||
};
|
||||
let plain = layout_paragraph(text, &base);
|
||||
let stretched = layout_paragraph(text, &expanded);
|
||||
assert!(stretched.lines.len() <= plain.lines.len());
|
||||
assert_eq!(stretched.line_scales.len(), stretched.lines.len());
|
||||
assert!(
|
||||
stretched
|
||||
.line_scales
|
||||
.iter()
|
||||
.all(|scale| (900..=1000).contains(scale)),
|
||||
"{:?}",
|
||||
stretched.line_scales
|
||||
);
|
||||
assert!(
|
||||
stretched.line_scales.iter().any(|scale| *scale < 1000),
|
||||
"no line was compressed: {:?}",
|
||||
stretched.line_scales
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn font_size_scales_glyphs_and_line_spacing() {
|
||||
let font = crate::cmr10::Cmr10Metrics::Roman;
|
||||
|
||||
Reference in new issue
Block a user