Apply native automatic kerns and protrusion at real line edges

This commit is contained in:
gloamxun committed 2026-11-21 06:00:55 +08:00
1 parent f66a5a7e20
commit 1822ab2d32
22 files changed
+260 -36

No files matched your search

@@ -1,7 +1,7 @@
//! Streaming TFM cursor/ligature-stack execution. A raw input character is
//! fetched only after the pending cursor asks for one; inserted ligatures can
//! therefore retain or consume components without inventing source characters.
use super::Span;
use super::{Span, font_catalog::AutoKern};
use crate::provenance::{Derivation, PaintSource};
use crate::tfm::{LigKernCommand, TfmMetrics};
use std::collections::VecDeque;
@@ -27,6 +27,7 @@ pub(super) struct Glyph {
pub(super) enum Output {
Glyph(Glyph),
Kern(i32),
AutoKern { width: i32, after_wrap: bool },
Missing(Component),
}
/// TeX's main-loop flags are job globals, including across a missing raw
@@ -223,7 +224,7 @@ impl Machine {
cursor.stack.push_front(r);
}
7 | 11 => {
cursor.wrap(false, &mut out);
cursor.wrap(false, None, &mut out);
cursor.left = Some(Right::inserted(replacement).glyph);
cursor.left.as_mut().unwrap().source = source;
}
@@ -257,7 +258,7 @@ impl Machine {
if !done {
continue;
}
cursor.wrap(true, &mut out);
cursor.wrap(true, None, &mut out);
if kern != 0 {
out.push(Output::Kern(
i32::try_from(kern).map_err(|_| "native reconstitute kern overflow")?,
@@ -304,6 +305,7 @@ impl Machine {
f: &TfmMetrics,
c: Component,
cancel_left: bool,
auto_kern: Option<AutoKern<'_>>,
steps: &mut usize,
limit: usize,
) -> Result<Vec<Output>, String> {
@@ -318,8 +320,15 @@ impl Machine {
right_hit: carry.right,
..Self::default()
};
let prepend = auto_kern.and_then(|kern| kern.width(None, Some(c.slot)));
if let Some(width) = prepend {
out.push(Output::AutoKern {
width,
after_wrap: false,
});
}
self.stack.push_back(Right::raw(c, false));
if cancel_left || f.boundary_label().is_none() {
if prepend.is_some() || cancel_left || f.boundary_label().is_none() {
self.advance(f, &mut out);
}
} else {
@@ -327,13 +336,14 @@ impl Machine {
let suppress_match = Some(c.slot) == f.false_boundary_char();
self.stack.push_back(Right::raw(c, suppress_match));
}
self.pump(f, false, steps, limit, &mut out)?;
self.pump(f, false, auto_kern, steps, limit, &mut out)?;
Ok(out)
}
pub fn finish(
&mut self,
f: &TfmMetrics,
right_boundary: bool,
auto_kern: Option<AutoKern<'_>>,
steps: &mut usize,
limit: usize,
) -> Result<Vec<Output>, String> {
@@ -341,10 +351,10 @@ impl Machine {
self.boundary = None;
}
let mut out = Vec::new();
self.pump(f, true, steps, limit, &mut out)?;
self.pump(f, true, auto_kern, steps, limit, &mut out)?;
Ok(out)
}
fn wrap(&mut self, right: bool, out: &mut Vec<Output>) {
fn wrap(&mut self, right: bool, auto_kern: Option<AutoKern<'_>>, out: &mut Vec<Output>) {
if let Some(mut glyph) = self.left.take() {
if glyph.ligature {
self.orphan_ligature = false;
@@ -354,6 +364,12 @@ impl Machine {
glyph.right_boundary = self.right_hit;
self.right_hit = false;
}
if let Some(width) = auto_kern.and_then(|kern| kern.width(None, Some(glyph.slot))) {
out.push(Output::AutoKern {
width,
after_wrap: false,
});
}
}
out.push(Output::Glyph(glyph));
}
@@ -378,6 +394,7 @@ impl Machine {
&mut self,
f: &TfmMetrics,
ending: bool,
auto_kern: Option<AutoKern<'_>>,
steps: &mut usize,
limit: usize,
out: &mut Vec<Output>,
@@ -399,14 +416,27 @@ impl Machine {
} else {
self.boundary
};
let command = right.and_then(|r| f.lig_kern(self.left.as_ref().map(|g| g.slot), r));
let left = self.left.as_ref().map(|g| g.slot);
// The initial boundary program enters after the automatic kern check.
if left.is_some()
&& let Some(width) = auto_kern.and_then(|kern| kern.width(left, right))
{
self.wrap(true, auto_kern, out);
out.push(Output::AutoKern {
width,
after_wrap: true,
});
self.advance(f, out);
continue;
}
let command = right.and_then(|r| f.lig_kern(left, r));
match command {
None => {
self.wrap(true, out);
self.wrap(true, auto_kern, out);
self.advance(f, out);
}
Some(LigKernCommand::Kern(k)) => {
self.wrap(true, out);
self.wrap(true, auto_kern, out);
out.push(Output::Kern(
i32::try_from(k).map_err(|_| "native font kern overflow")?,
));
@@ -459,7 +489,7 @@ impl Machine {
self.stack.push_front(r);
}
7 | 11 => {
self.wrap(false, out);
self.wrap(false, auto_kern, out);
self.left = Some(Right::inserted(replacement).glyph);
self.left.as_mut().unwrap().source = source;
}
@@ -487,7 +517,7 @@ impl Machine {
g.components.extend(r.glyph.components);
} else {
self.left = Some(g);
self.wrap(true, out);
self.wrap(true, auto_kern, out);
self.started = false;
continue;
}
@@ -498,7 +528,7 @@ impl Machine {
}
}
if operation > 4 && operation != 7 {
self.wrap(true, out);
self.wrap(true, auto_kern, out);
self.advance(f, out);
}
}