Measure external font expansion at paragraph break margins

This commit is contained in:
gloamxun committed 2026-11-21 08:08:11 +08:00
1 parent 91b3e6a8fe
commit 1e4c113b27
6 files changed
+242 -91

No files matched your search

@@ -110,8 +110,9 @@ pub struct ParagraphTraceReport {
pub span: Span,
}
pub(super) struct Tracer<'a> {
state: &'a State,
pub(super) struct Tracer {
escapechar: i32,
trailing_fill: bool,
enabled: bool,
records: Vec<ParagraphTrace>,
printed: Option<usize>,
@@ -120,10 +121,11 @@ pub(super) struct Tracer<'a> {
bytes: usize,
limit: usize,
}
impl<'a> Tracer<'a> {
pub fn new(state: &'a State, limit: usize) -> Self {
impl Tracer {
pub fn new(state: &State, limit: usize) -> Self {
Self {
state,
escapechar: state.escapechar,
trailing_fill: !state.parameter(Parameter::Parfillskip).glue().canonical_zero,
enabled: state.parameter(Parameter::Tracingparagraphs).width() > 0,
records: Vec::new(),
printed: None,
@@ -140,7 +142,7 @@ impl<'a> Tracer<'a> {
if self.enabled {
self.bytes = self.bytes.saturating_add(
record
.text(self.state.escapechar)
.text(self.escapechar)
.map_or(0, |s| s.len() + 1),
);
if self.bytes > self.limit {
@@ -159,11 +161,11 @@ impl<'a> Tracer<'a> {
announced: pass != 1 || first_was_run,
})
}
fn character(&mut self, font: FontId, text: &str, out: &mut String) -> Result<(), String> {
fn character(&mut self, state: &State, font: FontId, text: &str, out: &mut String) -> Result<(), String> {
if font != self.font {
out.push_str(&escape(
self.state.escapechar,
&self.state.fonts.font(font)?.identifier_text(),
self.escapechar,
&state.fonts.font(font)?.identifier_text(),
));
out.push(' ');
self.font = font;
@@ -171,7 +173,7 @@ impl<'a> Tracer<'a> {
out.push_str(text);
Ok(())
}
fn short(&mut self, nodes: &[TextNode], out: &mut String) -> Result<(), String> {
fn short(&mut self, state: &State, nodes: &[TextNode], out: &mut String) -> Result<(), String> {
let mut i = 0;
while let Some(n) = nodes.get(i) {
match n {
@@ -183,7 +185,7 @@ impl<'a> Tracer<'a> {
..
} => {
let character = (*slot as char).to_string();
self.character(*font, if *ligature { original } else { &character }, out)?;
self.character(state, *font, if *ligature { original } else { &character }, out)?;
}
TextNode::MathChar(b) => {
if let Some(g) = &b.math_geometry
@@ -191,7 +193,7 @@ impl<'a> Tracer<'a> {
&& let crate::math_engine_tex::MathItem::NativeGlyph { font, slot, .. } =
&g.items[0].item
{
self.character(font.id, &(*slot as char).to_string(), out)?;
self.character(state, font.id, &(*slot as char).to_string(), out)?;
} else {
return Err(
"native math character short diagnostic structure is unavailable"
@@ -226,8 +228,8 @@ impl<'a> Tracer<'a> {
replacement_count,
..
} => {
self.short(pre_break, out)?;
self.short(post_break, out)?;
self.short(state, pre_break, out)?;
self.short(state, post_break, out)?;
i += usize::from(*replacement_count);
}
TextNode::Kern { .. } | TextNode::AccentKern(_) | TextNode::Penalty(_)
@@ -240,19 +242,16 @@ impl<'a> Tracer<'a> {
}
Ok(())
}
pub fn material(&mut self, nodes: &[TextNode], end: usize) -> Result<(), String> {
pub fn material(&mut self, state: &State, nodes: &[TextNode], end: usize) -> Result<(), String> {
if !self.enabled || self.printed == Some(end) {
return Ok(());
}
let mut text = String::new();
// short_display includes the breakpoint itself (not its following link).
let stop = (end + 1).min(nodes.len());
self.short(&nodes[self.next_material.min(stop)..stop], &mut text)?;
if end == nodes.len() {
let g = self.state.parameter(Parameter::Parfillskip).glue();
if !g.canonical_zero {
text.push(' ');
}
self.short(state, &nodes[self.next_material.min(stop)..stop], &mut text)?;
if end == nodes.len() && self.trailing_fill {
text.push(' ');
}
self.push(ParagraphTrace::Material { text })?;
self.printed = Some(end);