273 lines
9.4 KiB
Rust
273 lines
9.4 KiB
Rust
//! Observations of the actual native line-breaking passes, edges and nodes.
|
|
use super::*;
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum ParagraphBreakKind {
|
|
Glue,
|
|
Penalty,
|
|
Discretionary,
|
|
Kern,
|
|
Math,
|
|
End,
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub enum ParagraphTrace {
|
|
Pass {
|
|
pass: u8,
|
|
announced: bool,
|
|
},
|
|
Material {
|
|
text: String,
|
|
},
|
|
Feasible {
|
|
position: usize,
|
|
kind: ParagraphBreakKind,
|
|
via: usize,
|
|
badness: i32,
|
|
penalty: i32,
|
|
/// None is TeX's artificial edge, whose incremental cost prints `*`.
|
|
demerits: Option<i64>,
|
|
},
|
|
Active {
|
|
serial: usize,
|
|
position: usize,
|
|
line: usize,
|
|
fitness: u8,
|
|
hyphenated: bool,
|
|
demerits: i64,
|
|
previous: usize,
|
|
},
|
|
/// The actual chosen final active node; intentionally no invented log line.
|
|
Selection {
|
|
serial: usize,
|
|
requested_looseness: i64,
|
|
actual_looseness: i64,
|
|
accepted: bool,
|
|
},
|
|
}
|
|
fn escape(value: i32, name: &str) -> String {
|
|
let mut s = String::new();
|
|
if let Ok(c) = u8::try_from(value) {
|
|
s.push(c as char);
|
|
}
|
|
s.push_str(name);
|
|
s
|
|
}
|
|
impl ParagraphTrace {
|
|
pub fn text(&self, escapechar: i32) -> Option<String> {
|
|
match self {
|
|
Self::Pass { pass, announced } => announced.then(|| match pass {
|
|
0 => "@firstpass".into(),
|
|
1 => "@secondpass".into(),
|
|
_ => "@emergencypass".into(),
|
|
}),
|
|
Self::Material { text } => Some(text.clone()),
|
|
Self::Feasible {
|
|
kind,
|
|
via,
|
|
badness,
|
|
penalty,
|
|
demerits,
|
|
..
|
|
} => {
|
|
let name = match kind {
|
|
ParagraphBreakKind::Glue => String::new(),
|
|
ParagraphBreakKind::Penalty => escape(escapechar, "penalty"),
|
|
ParagraphBreakKind::Discretionary => escape(escapechar, "discretionary"),
|
|
ParagraphBreakKind::Kern => escape(escapechar, "kern"),
|
|
ParagraphBreakKind::Math => escape(escapechar, "math"),
|
|
ParagraphBreakKind::End => escape(escapechar, "par"),
|
|
};
|
|
let b = if *badness > 10000 {
|
|
"*".into()
|
|
} else {
|
|
badness.to_string()
|
|
};
|
|
let d = demerits.map_or_else(|| "*".into(), |d| d.to_string());
|
|
Some(format!("@{name} via @@{via} b={b} p={penalty} d={d}"))
|
|
}
|
|
Self::Active {
|
|
serial,
|
|
line,
|
|
fitness,
|
|
hyphenated,
|
|
demerits,
|
|
previous,
|
|
..
|
|
} => Some(format!(
|
|
"@@{serial}: line {line}.{fitness}{} t={demerits} -> @@{previous}",
|
|
if *hyphenated { "-" } else { "" }
|
|
)),
|
|
Self::Selection { .. } => None,
|
|
}
|
|
}
|
|
}
|
|
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
|
pub struct ParagraphTraceReport {
|
|
pub records: Vec<ParagraphTrace>,
|
|
pub text: String,
|
|
pub destination: WriteDestination,
|
|
pub span: Span,
|
|
}
|
|
|
|
pub(super) struct Tracer {
|
|
escapechar: i32,
|
|
trailing_fill: bool,
|
|
enabled: bool,
|
|
records: Vec<ParagraphTrace>,
|
|
printed: Option<usize>,
|
|
next_material: usize,
|
|
font: FontId,
|
|
bytes: usize,
|
|
limit: usize,
|
|
}
|
|
impl Tracer {
|
|
pub fn new(state: &State, limit: usize) -> Self {
|
|
Self {
|
|
escapechar: state.escapechar,
|
|
trailing_fill: !state.parameter(Parameter::Parfillskip).glue().canonical_zero,
|
|
enabled: state.parameter(Parameter::Tracingparagraphs).width() > 0,
|
|
records: Vec::new(),
|
|
printed: None,
|
|
next_material: 0,
|
|
font: FontId::NULL,
|
|
bytes: 0,
|
|
limit,
|
|
}
|
|
}
|
|
pub fn into_records(self) -> Vec<ParagraphTrace> {
|
|
self.records
|
|
}
|
|
pub fn push(&mut self, record: ParagraphTrace) -> Result<(), String> {
|
|
if self.enabled {
|
|
self.bytes = self.bytes.saturating_add(
|
|
record
|
|
.text(self.escapechar)
|
|
.map_or(0, |s| s.len() + 1),
|
|
);
|
|
if self.bytes > self.limit {
|
|
return Err("native diagnostic output limit exceeded".into());
|
|
}
|
|
self.records.push(record);
|
|
}
|
|
Ok(())
|
|
}
|
|
pub fn pass(&mut self, pass: u8, first_was_run: bool) -> Result<(), String> {
|
|
self.printed = None;
|
|
self.next_material = 0;
|
|
self.font = FontId::NULL;
|
|
self.push(ParagraphTrace::Pass {
|
|
pass,
|
|
announced: pass != 1 || first_was_run,
|
|
})
|
|
}
|
|
fn character(&mut self, state: &State, font: FontId, text: &str, out: &mut String) -> Result<(), String> {
|
|
if font != self.font {
|
|
out.push_str(&escape(
|
|
self.escapechar,
|
|
&state.fonts.font(font)?.identifier_text(),
|
|
));
|
|
out.push(' ');
|
|
self.font = font;
|
|
}
|
|
out.push_str(text);
|
|
Ok(())
|
|
}
|
|
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 {
|
|
TextNode::Glyph {
|
|
font,
|
|
slot,
|
|
original,
|
|
ligature,
|
|
..
|
|
} => {
|
|
let character = (*slot as char).to_string();
|
|
self.character(state, *font, if *ligature { original } else { &character }, out)?;
|
|
}
|
|
TextNode::OpenType(run) => self.character(state, run.font.id, &run.text, out)?,
|
|
TextNode::ExplicitOpenTypeGlyph(_) => out.push_str("[]"),
|
|
TextNode::MathChar(b) => {
|
|
if let Some(g) = &b.math_geometry
|
|
&& g.items.len() == 1
|
|
&& let crate::math_engine_tex::MathItem::NativeGlyph { font, slot, .. } =
|
|
&g.items[0].item
|
|
{
|
|
self.character(state, font.id, &(*slot as char).to_string(), out)?;
|
|
} else {
|
|
return Err(
|
|
"native math character short diagnostic structure is unavailable"
|
|
.into(),
|
|
);
|
|
}
|
|
}
|
|
TextNode::Box(b) if b.native_box.is_none() => {
|
|
return Err(
|
|
"compound math short diagnostics require retained native math boxes".into(),
|
|
);
|
|
}
|
|
TextNode::Box(_)
|
|
| TextNode::Indent(_)
|
|
| TextNode::Language(_)
|
|
| TextNode::Adjustment(_)
|
|
| TextNode::Insertion(_)
|
|
| TextNode::Mark(_)
|
|
| TextNode::Output(_) => out.push_str("[]"),
|
|
TextNode::Rule(_) => out.push('|'),
|
|
TextNode::Glue(g) | TextNode::FontSpace(g, _, _)
|
|
| TextNode::NamedGlue(g, _)
|
|
| TextNode::Leaders(LeaderGlue { glue: g, .. }) => {
|
|
if !g.canonical_zero {
|
|
out.push(' ');
|
|
}
|
|
}
|
|
TextNode::MathBoundary { .. } | TextNode::Direction(_) => out.push('$'),
|
|
TextNode::Discretionary {
|
|
pre_break,
|
|
post_break,
|
|
replacement_count,
|
|
..
|
|
} => {
|
|
self.short(state, pre_break, out)?;
|
|
self.short(state, post_break, out)?;
|
|
i += usize::from(*replacement_count);
|
|
}
|
|
TextNode::Kern { .. } | TextNode::AccentKern(_) | TextNode::Penalty(_)
|
|
| TextNode::AutoKern(_) | TextNode::SpaceAdjustment(_) | TextNode::MarginKern { .. } => {}
|
|
}
|
|
i += 1;
|
|
}
|
|
if self.bytes.saturating_add(out.len()) > self.limit {
|
|
return Err("native diagnostic output limit exceeded".into());
|
|
}
|
|
Ok(())
|
|
}
|
|
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(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);
|
|
self.next_material = stop;
|
|
Ok(())
|
|
}
|
|
pub fn after_break(&mut self, nodes: &[TextNode], end: usize) {
|
|
if self.printed == Some(end)
|
|
&& let Some(TextNode::Discretionary {
|
|
replacement_count, ..
|
|
}) = nodes.get(end)
|
|
{
|
|
self.next_material = end + 1 + usize::from(*replacement_count);
|
|
}
|
|
}
|
|
}
|