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

@@ -75,6 +75,7 @@ pub enum TextNode {
},
/// Spacing owned by a text accent and protected from leading discard.
AccentKern(i32),
AutoKern(i32),
MarginKern { width: i32, right: bool, font: FontId, slot: u8 },
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
@@ -161,8 +162,8 @@ impl Builder {
));
}
}
pub fn set_language(&mut self, language: HyphenationLanguage) -> Result<(), String> {
self.finish_text(true)?;
pub fn set_language(&mut self, language: HyphenationLanguage, state: &State) -> Result<(), String> {
self.finish_text(true, state)?;
self.current_language = language;
self.nodes.push(TextNode::Language(language));
Ok(())
@@ -174,6 +175,7 @@ impl Builder {
) -> Vec<super::ligature::Component> {
use super::ligature::Output;
let mut missing = Vec::new();
let mut hyphen_break = None;
for item in output {
match item {
Output::Glyph(g) => {
@@ -196,6 +198,7 @@ impl Builder {
hyphen_char: run.hyphen_char,
});
if explicit_hyphen && !self.restricted {
hyphen_break = Some(self.nodes.len());
self.nodes.push(TextNode::Discretionary {
font: run.font.id,
automatic: false,
@@ -210,18 +213,27 @@ impl Builder {
width,
explicit: false,
}),
Output::AutoKern { width, after_wrap } => {
let node = TextNode::AutoKern(width);
if after_wrap && hyphen_break.is_some_and(|i| i + 1 == self.nodes.len()) {
self.nodes.insert(self.nodes.len() - 1, node);
} else {
self.nodes.push(node);
}
}
Output::Missing(c) => missing.push(c),
}
}
missing
}
pub fn finish_text(&mut self, right_boundary: bool) -> Result<(), String> {
pub fn finish_text(&mut self, right_boundary: bool, state: &State) -> Result<(), String> {
if let Some(mut run) = self.run.take() {
run.machine
.put_carry(std::mem::take(&mut self.ligature_carry));
let output = run.machine.finish(
run.font.metrics(),
right_boundary,
FontCatalog::auto_kern_for(state, run.font.id),
&mut self.font_steps,
self.font_step_limit,
)?;
@@ -251,7 +263,7 @@ impl Builder {
.as_ref()
.is_some_and(|r| r.font.id != font || r.hyphen_char != hyphen_char)
{
self.finish_text(true)?;
self.finish_text(true, state)?;
}
let cancel = self.cancel_boundary;
self.cancel_boundary = false;
@@ -269,6 +281,7 @@ impl Builder {
run.font.metrics(),
c,
cancel,
FontCatalog::auto_kern_for(state, font),
&mut self.font_steps,
self.font_step_limit,
)?;
@@ -289,7 +302,7 @@ impl Builder {
};
}
pub fn space(&mut self, state: &State) -> Result<(), String> {
self.finish_text(true)?;
self.finish_text(true, state)?;
let f = state.fonts.font(state.text_font())?;
let g = f.space();
let (space, stretch, shrink, extra) = (
@@ -390,6 +403,7 @@ fn width(n: &TextNode) -> i64 {
TextNode::Output(node) => node.dimensions().map_or(0, |(width, _, _)| width),
TextNode::Kern { width: n, .. }
| TextNode::AccentKern(n)
| TextNode::AutoKern(n)
| TextNode::MarginKern { width: n, .. }
| TextNode::MathBoundary { width: n, .. }
| TextNode::Indent(n) => *n as i64,
@@ -1050,7 +1064,7 @@ pub(super) fn finish(
trace: &mut super::paragraph_trace::Tracer<'_>,
cache: Option<&LineBreakCache>,
) -> Result<NativeParagraph, String> {
b.finish_text(true)?;
b.finish_text(true, state)?;
if b.nodes.last().is_some_and(|n| {
matches!(
n,
@@ -1161,11 +1175,12 @@ pub(super) fn finish(
point.zero_tail,
);
let dimensions = geometry(&parameters, shape, i + b.previous_lines);
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 terminal_marker = point.zero_tail.is_some()
|| matches!(b.nodes.get(point.end), Some(TextNode::Penalty(_)))
|| (point.pre_break.is_some() && !ends_in_pre_break);
let (line, line_box, adjustments, shrinking) = pack(
&material,
&parameters,
@@ -1354,6 +1369,7 @@ fn add_hyphens(
| TextNode::Leaders(_)
| TextNode::Penalty(_)
| TextNode::AccentKern(_)
| TextNode::AutoKern(_)
| TextNode::Kern { explicit: true, .. }
)
})