Apply native font protrusion during line breaking and packing
This commit is contained in:
1 parent
361e8d070f
commit
4777d66fc8
22 files changed
+441
-32
No files matched your search
@@ -4,6 +4,7 @@ use crate::{HorizontalBox, Paragraph, ParagraphFont};
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
mod break_cache;
|
||||
mod protrusion;
|
||||
use break_cache::BreakNode;
|
||||
pub use break_cache::{LineBreakCache, LineBreakWork};
|
||||
|
||||
@@ -72,6 +73,7 @@ pub enum TextNode {
|
||||
},
|
||||
/// Spacing owned by a text accent and protected from leading discard.
|
||||
AccentKern(i32),
|
||||
MarginKern { width: i32, right: bool, font: FontId, slot: u8 },
|
||||
}
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct NativeParagraph {
|
||||
@@ -386,6 +388,7 @@ fn width(n: &TextNode) -> i64 {
|
||||
TextNode::Output(node) => node.dimensions().map_or(0, |(width, _, _)| width),
|
||||
TextNode::Kern { width: n, .. }
|
||||
| TextNode::AccentKern(n)
|
||||
| TextNode::MarginKern { width: n, .. }
|
||||
| TextNode::MathBoundary { width: n, .. }
|
||||
| TextNode::Indent(n) => *n as i64,
|
||||
_ => 0,
|
||||
@@ -553,6 +556,7 @@ fn metrics(nodes: &[TextNode], left: Glue, right: Glue) -> (i64, [i64; 4], i64)
|
||||
fn break_lines(
|
||||
nodes: &[TextNode],
|
||||
projected: &[BreakNode],
|
||||
margins: Option<&protrusion::Search>,
|
||||
p: &BTreeMap<Parameter, ParameterValue>,
|
||||
shape: &[ParagraphShapeLine],
|
||||
pass: u8,
|
||||
@@ -732,7 +736,14 @@ fn break_lines(
|
||||
if target < 0 && shape.is_empty() {
|
||||
return Err("negative native paragraph measure".into());
|
||||
}
|
||||
let delta = target - natural;
|
||||
let hanging = margins.map_or(0, |margins| {
|
||||
// The active paragraph still ends in parfillskip. A nonzero
|
||||
// fill stops the right search at the terminal breakpoint.
|
||||
let right_visible = !final_break
|
||||
|| p[&Parameter::Parfillskip].glue().canonical_zero;
|
||||
margins.width(a.start, b.end, a.post_break, b.pre_break, right_visible)
|
||||
});
|
||||
let delta = target - natural + hanging;
|
||||
let infinite = stretch[1..].iter().any(|n| *n != 0);
|
||||
let bad = if delta >= 0 {
|
||||
if infinite {
|
||||
@@ -918,17 +929,32 @@ fn pack(
|
||||
p: &BTreeMap<Parameter, ParameterValue>,
|
||||
(indent, target): (i64, i64),
|
||||
last: bool,
|
||||
terminal_marker: bool,
|
||||
ends_in_pre_break: bool,
|
||||
state: &State,
|
||||
span: Span,
|
||||
) -> Result<(Paragraph, NativeBox, super::marks::MigratingMaterial), String> {
|
||||
) -> Result<(Paragraph, NativeBox, super::marks::MigratingMaterial, bool), String> {
|
||||
let left = p[&Parameter::Leftskip].glue();
|
||||
let mut material = Vec::new();
|
||||
if left != Glue::default() {
|
||||
material.push(TextNode::NamedGlue(left, Parameter::Leftskip));
|
||||
}
|
||||
material.extend_from_slice(nodes);
|
||||
let enabled = p[&Parameter::Pdfprotrudechars].width() > 0;
|
||||
if enabled && let Some(kern) = protrusion::margin_kern(state, nodes, false, false) {
|
||||
material.push(kern);
|
||||
}
|
||||
let right_end = nodes.len() - usize::from(terminal_marker);
|
||||
material.extend_from_slice(&nodes[..right_end]);
|
||||
if last {
|
||||
material.push(TextNode::Penalty(10000));
|
||||
}
|
||||
if enabled
|
||||
&& let Some(kern) = protrusion::margin_kern(state, &nodes[..right_end], true, ends_in_pre_break)
|
||||
{
|
||||
material.push(kern);
|
||||
}
|
||||
material.extend_from_slice(&nodes[right_end..]);
|
||||
if last {
|
||||
material.push(TextNode::NamedGlue(
|
||||
p[&Parameter::Parfillskip].glue(),
|
||||
Parameter::Parfillskip,
|
||||
@@ -939,6 +965,7 @@ fn pack(
|
||||
Parameter::Rightskip,
|
||||
));
|
||||
let (material, adjustments) = super::marks::extract(material);
|
||||
let shrinking = material.iter().map(width).sum::<i64>() > target;
|
||||
let mut b = super::boxes::hpack(
|
||||
material,
|
||||
super::boxes::PackSpec::To(
|
||||
@@ -962,7 +989,7 @@ fn pack(
|
||||
centered: false,
|
||||
right_aligned: false,
|
||||
};
|
||||
Ok((paragraph, b, adjustments))
|
||||
Ok((paragraph, b, adjustments, shrinking))
|
||||
}
|
||||
// TeX init_math scans natural widths and visible nodes, independently of
|
||||
// rounded glue-setting coordinates. Stretch/shrink before the last visible
|
||||
@@ -1085,6 +1112,7 @@ pub(super) fn finish(
|
||||
if let Some(path) = break_cache::solve(
|
||||
&b.nodes,
|
||||
¶meters,
|
||||
state,
|
||||
shape,
|
||||
pass,
|
||||
&mut work,
|
||||
@@ -1114,13 +1142,6 @@ pub(super) fn finish(
|
||||
let mut packed_shrinking = Vec::new();
|
||||
let mut pre_display_size = -1073741823;
|
||||
for (i, (point, bad)) in path.iter().enumerate() {
|
||||
let left = parameters[&Parameter::Leftskip].glue();
|
||||
let mut right = parameters[&Parameter::Rightskip].glue();
|
||||
if i + 1 == path.len() {
|
||||
right = right
|
||||
.add(parameters[&Parameter::Parfillskip].glue())
|
||||
.map_err(str::to_owned)?;
|
||||
}
|
||||
let material = line_material(
|
||||
&b.nodes,
|
||||
start,
|
||||
@@ -1130,23 +1151,26 @@ pub(super) fn finish(
|
||||
true,
|
||||
point.zero_tail,
|
||||
);
|
||||
let (natural, stretch, shrink) = metrics(&material, left, right);
|
||||
let dimensions = geometry(¶meters, shape, i + b.previous_lines);
|
||||
let target = dimensions.1;
|
||||
let (set, packed_bad, excess) =
|
||||
super::boxes::setting(target - natural, stretch, [shrink, 0, 0, 0], true);
|
||||
packed_diagnostic.push(target != natural && set.order == 0);
|
||||
packed_shrinking.push(target < natural);
|
||||
packed_badness.push(packed_bad);
|
||||
packed_excess.push(excess);
|
||||
let (line, line_box, adjustments) = pack(
|
||||
let terminal_marker = point.zero_tail.is_some()
|
||||
|| matches!(b.nodes.get(point.end), Some(TextNode::Penalty(_)));
|
||||
let ends_in_pre_break = point.pre_break.is_some_and(|index| {
|
||||
matches!(&b.nodes[index], TextNode::Discretionary { pre_break, .. } if !pre_break.is_empty())
|
||||
});
|
||||
let (line, line_box, adjustments, shrinking) = pack(
|
||||
&material,
|
||||
¶meters,
|
||||
dimensions,
|
||||
i + 1 == path.len(),
|
||||
terminal_marker,
|
||||
ends_in_pre_break,
|
||||
state,
|
||||
b.span,
|
||||
)?;
|
||||
packed_diagnostic.push(line_box.diagnostic_eligible);
|
||||
packed_shrinking.push(shrinking);
|
||||
packed_badness.push(line_box.badness);
|
||||
packed_excess.push(line_box.excess);
|
||||
if i + 1 == path.len() {
|
||||
let BoxContent::Horizontal(material) = &line_box.content else {
|
||||
unreachable!()
|
||||
|
||||
Reference in new issue
Block a user