Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,268 @@
|
||||
//! 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<'a> {
|
||||
state: &'a State,
|
||||
enabled: bool,
|
||||
records: Vec<ParagraphTrace>,
|
||||
printed: Option<usize>,
|
||||
next_material: usize,
|
||||
font: FontId,
|
||||
bytes: usize,
|
||||
limit: usize,
|
||||
}
|
||||
impl<'a> Tracer<'a> {
|
||||
pub fn new(state: &'a State, limit: usize) -> Self {
|
||||
Self {
|
||||
state,
|
||||
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.state.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, 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(),
|
||||
));
|
||||
out.push(' ');
|
||||
self.font = font;
|
||||
}
|
||||
out.push_str(text);
|
||||
Ok(())
|
||||
}
|
||||
fn short(&mut self, 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(*font, if *ligature { original } else { &character }, out)?;
|
||||
}
|
||||
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(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::NamedGlue(g, _) => {
|
||||
if !g.canonical_zero {
|
||||
out.push(' ');
|
||||
}
|
||||
}
|
||||
TextNode::MathBoundary { .. } => out.push('$'),
|
||||
TextNode::Discretionary {
|
||||
pre_break,
|
||||
post_break,
|
||||
replacement_count,
|
||||
..
|
||||
} => {
|
||||
self.short(pre_break, out)?;
|
||||
self.short(post_break, out)?;
|
||||
i += usize::from(*replacement_count);
|
||||
}
|
||||
TextNode::Kern { .. } | TextNode::Penalty(_) => {}
|
||||
}
|
||||
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, 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.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user