Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

@@ -0,0 +1,165 @@
use super::*;
impl Executor {
pub(super) fn font_dimension_target(
&mut self,
files: &mut impl SourceProvider,
) -> Result<(FontId, i32), Error> {
let n = self.integer(files)?;
let font = self.font_identifier(files)?;
self.state
.fonts
.fontdimen(font, n)
.map_err(|e| self.error(e, self.current))?;
Ok((font, n))
}
pub(super) fn font_dimension(
&mut self,
p: Primitive,
files: &mut impl SourceProvider,
) -> Result<i32, Error> {
if p == Primitive::Fontdimen {
let (font, n) = self.font_dimension_target(files)?;
return self
.state
.fonts
.fontdimen(font, n)
.map_err(|e| self.error(e, self.current));
}
let font = self.font_identifier(files)?;
let slot = self.integer(files)?;
let slot = u8::try_from(slot)
.map_err(|_| self.error("font character index must be in 0..255", self.current))?;
let Some(m) = self
.state
.fonts
.font(font)
.map_err(|e| self.error(e, self.current))?
.metrics()
else {
return Ok(0);
};
let c = char::from(slot);
Ok(match p {
Primitive::Fontcharwd => m.width(c),
Primitive::Fontcharht => m.height(c),
Primitive::Fontchardp => m.depth(c),
Primitive::Fontcharic => m.italic(c),
_ => unreachable!(),
} as i32)
}
pub(super) fn font_identifier(
&mut self,
files: &mut impl SourceProvider,
) -> Result<FontId, Error> {
let t = self.expanded_non_space(files)?.token;
let font = match self.meaning(&t) {
Meaning::Font(f) => f,
Meaning::Primitive(Primitive::Font) => {
self.reads.insert(Dependency::TextFont);
self.state.text_font()
}
Meaning::Primitive(
p @ (Primitive::Textfont | Primitive::Scriptfont | Primitive::Scriptscriptfont),
) => {
let family = self.integer(files)?;
if !(0..16).contains(&family) {
return self.fail("native math family must be in 0..15");
}
let size = match p {
Primitive::Textfont => 0,
Primitive::Scriptfont => 1,
_ => 2,
};
self.reads.insert(Dependency::MathFamily {
size,
family: family as u8,
});
self.state.math_font(size, family as u8)
}
_ => return self.fail("expected native font identifier"),
};
self.reads.insert(Dependency::FontResources);
Ok(font)
}
pub(super) fn missing_glyph(
&mut self,
font: FontId,
slot: u8,
span: Span,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let tracing = self.read_parameter(Parameter::Tracinglostchars).width();
let online = self.read_parameter(Parameter::Tracingonline).width();
let destination = if tracing <= 0 {
None
} else if tracing > 1 || online > 0 {
Some(WriteDestination::TerminalAndLog)
} else {
Some(WriteDestination::Log)
};
// TeX print_ASCII renders nonprinting bytes in ^^ notation. This is
// the requested slot, not a Unicode-to-font substitution.
self.reads.insert(Dependency::NewLineChar);
let printed = if u8::try_from(self.state.newlinechar).ok() == Some(slot) {
"\n".into()
} else {
match slot {
0..=31 => format!("^^{}", char::from(slot + 64)),
127 => "^^?".into(),
128..=255 => format!("^^{slot:02x}"),
_ => char::from(slot).to_string(),
}
};
let newline = u8::try_from(self.state.newlinechar).ok().map(char::from);
let literal = |s: &str| {
s.chars()
.map(|c| if Some(c) == newline { '\n' } else { c })
.collect::<String>()
};
let message = destination.map(|_| {
// print_ASCII expands a byte's canonical ^^ string with newlinechar
// temporarily disabled. Literal diagnostic strings do not disable it.
let head = literal(if tracing > 2 {
"! Missing character: There is no "
} else {
"Missing character: There is no "
});
let tail = literal(&if tracing > 2 {
format!(
" (\"{slot:X}) in font {}.",
self.state.fonts.font(font).expect("loaded font").name()
)
} else {
format!(
" in font {}!",
self.state.fonts.font(font).expect("loaded font").name()
)
});
format!("{head}{printed}{tail}")
});
self.events.push(Event::MissingGlyph(MissingGlyph {
font,
slot,
tracing,
destination,
message: message.clone(),
span,
}));
if let (Some(destination), Some(message)) = (destination, message) {
let output = if message.ends_with('\n') {
message.clone()
} else {
format!("{message}\n")
};
files
.observe_diagnostic(destination, &output)
.map_err(|e| self.error(e, span))?;
if tracing > 2 {
let mut error = self.error(message, span);
error.message_reported = true;
return Err(error);
}
}
Ok(())
}
}