Execute Unicode math codes and measured OpenType math characters

This commit is contained in:
gloamxun committed 2026-11-21 11:47:56 +08:00
1 parent ce8a780031
commit d17680fcd8
24 files changed
+687 -123

No files matched your search

@@ -378,11 +378,15 @@ impl Executor {
| Futurelet
| Chardef
| Mathchardef
| Umathchardef
| Umathcharnumdef
| Catcode
| Lccode
| Uccode
| Sfcode
| Mathcode
| Umathcode
| Umathcodenum
| Delcode
| Count
| Countdef
@@ -117,20 +117,14 @@ impl Executor {
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 family = self.scan_math_family(files)?;
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)
self.reads.insert(Dependency::MathFamily { size, family });
self.state.math_font(size, family)
}
_ => return self.fail("expected native font identifier"),
};
@@ -140,10 +134,11 @@ impl Executor {
pub(super) fn missing_glyph(
&mut self,
font: FontId,
slot: u8,
slot: impl Into<u32>,
span: Span,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let slot = slot.into();
let tracing = self.read_parameter(Parameter::Tracinglostchars).width();
let online = self.read_parameter(Parameter::Tracingonline).width();
let destination = if tracing <= 0 {
@@ -156,14 +151,16 @@ impl Executor {
// 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) {
let printed = if u32::try_from(self.state.newlinechar).ok() == Some(slot) {
"\n".into()
} else {
match slot {
0..=31 => format!("^^{}", char::from(slot + 64)),
0..=31 => format!("^^{}", char::from(slot as u8 + 64)),
127 => "^^?".into(),
128..=255 => format!("^^{slot:02x}"),
_ => char::from(slot).to_string(),
_ => char::from_u32(slot)
.map(|c| c.to_string())
.unwrap_or_else(|| format!("U+{slot:04X}")),
}
};
let newline = u8::try_from(self.state.newlinechar).ok().map(char::from);
@@ -83,10 +83,7 @@ impl Executor {
global: bool,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let family = self.integer(files)?;
if !(0..16).contains(&family) {
return self.fail("native math family must be in 0..15");
};
let family = self.scan_math_family(files)?;
self.optional_equals(files)?;
let font = self.font_identifier(files)?;
let size = match p {
@@ -94,7 +91,6 @@ impl Executor {
Primitive::Scriptfont => 1,
_ => 2,
};
let family = family as u8;
let equivalent = self.state.math_font(size, family) == font;
self.assign_equivalence(
Key::Mathfont(size, family),
@@ -112,37 +108,46 @@ impl Executor {
c: char,
token: &Token,
) -> Result<Option<(NoadClass, MathChar)>, Error> {
let c = u8::try_from(c as u32).map_err(|_| {
self.error(
"native 8-bit mathcode requires a byte character",
token.span,
)
})?;
self.reads.insert(Dependency::MathCode(c));
let code = self.state.mathcode(c);
if code == 32768 {
let code = self.read_math_code(c as u32);
if code.is_active() {
let mut t = token.clone();
t.kind = TokenKind::Command(CommandName::Active(c as char));
t.kind = TokenKind::Command(CommandName::Active(c));
self.unread(t.into())?;
return Ok(None);
}
Ok(Some(self.decode_math_char(code)))
Ok(Some(self.decode_math_code(code)))
}
fn decode_math_char(&mut self, code: u16) -> (NoadClass, MathChar) {
let raw = (code >> 12) as u8;
let fam = self.read_parameter(Parameter::Fam).width();
self.decode_math_code(MathCode::from_legacy(code))
}
fn decode_math_code(&mut self, code: MathCode) -> (NoadClass, MathChar) {
let class = code.class();
let mut family = code.family();
if class == 7 {
let current = self.read_parameter(Parameter::Fam).width();
if (0..self.state.math_family_count()).contains(&current) {
family = current as u8;
}
}
if family >= 16 {
for size in 0..3 {
self.reads.insert(Dependency::MathFamily { size, family });
}
}
(
NoadClass::from_code(if raw == 7 { 0 } else { raw }),
MathChar {
family: if raw == 7 && (0..16).contains(&fam) {
fam as u8
} else {
((code >> 8) & 15) as u8
},
slot: code as u8,
},
NoadClass::from_code(if class == 7 { 0 } else { class }),
MathChar { family, slot: code.slot() },
)
}
fn explicit_math_code(&mut self, code: MathCode) -> Result<(NoadClass, MathChar), Error> {
if code.is_active() {
return self.fail("active math code requires a character table entry");
}
Ok(self.decode_math_code(code))
}
pub(super) fn start_math(
&mut self,
token: &Token,
@@ -482,7 +487,8 @@ impl Executor {
Meaning::Character(TokenKind::Character(_, CatCode::Letter | CatCode::Other))
| Meaning::CharGiven(_)
| Meaning::MathCharGiven(_)
| Meaning::Primitive(Mathchar | Char | Delimiter) => {}
| Meaning::UnicodeMathCharGiven(_)
| Meaning::Primitive(Mathchar | Umathchar | Umathcharnum | Char | Delimiter) => {}
_ => return self.fail("expected a native math field character or group"),
}
}
@@ -532,6 +538,10 @@ impl Executor {
let (class, c) = self.decode_math_char(*code);
self.accept_math_field(MathField::Char(c), class, TokenOrigin::from(token))
}
Meaning::UnicodeMathCharGiven(code) => {
let (class, c) = self.explicit_math_code(*code)?;
self.accept_math_field(MathField::Char(c), class, TokenOrigin::from(token));
}
Meaning::Primitive(p) => match p {
Halign => self.start_alignment(*p, token.span, files)?,
Mathchoice => self.start_math_choice(token.span, files)?,
@@ -558,6 +568,11 @@ impl Executor {
self.math.last_mut().unwrap().nodes.push(MathNode::Rule(r));
}
Left | Right => self.left_right(*p == Left, token.span, files)?,
Umathchar | Umathcharnum => {
let code = self.scan_unicode_math_code(*p == Umathchar, files)?;
let (class, c) = self.explicit_math_code(code)?;
self.accept_math_field(MathField::Char(c), class, TokenOrigin::from(token));
}
Mathchar | Char | Delimiter => {
let n = self.integer(files)?;
if *p == Char {
@@ -778,8 +793,10 @@ impl Executor {
}
Noboundary => {}
Global | Long | Outer | Protected | Def | Gdef | Edef | Xdef | Let | Futurelet
| Afterassignment | Aftergroup | Immediate | Chardef | Mathchardef | Catcode
| Afterassignment | Aftergroup | Immediate | Chardef | Mathchardef
| Umathchardef | Umathcharnumdef | Catcode
| Lccode | Uccode | Lowercase | Uppercase | Hyphenation | Patterns | Mathcode
| Umathcode | Umathcodenum
| Delcode | Count | Countdef | Dimen | Dimendef | Skip | Skipdef | Muskip
| Muskipdef | Toks | Toksdef | Advance | Multiply | Divide | Read | Readline
| Font | Fontdimen | Hyphenchar | Skewchar | Lpcode | Rpcode | Efcode | Pdffontexpand
@@ -0,0 +1,88 @@
use super::*;
impl Executor {
pub(super) fn scan_math_code_index(
&mut self,
files: &mut impl SourceProvider,
) -> Result<u32, Error> {
let value = self.integer(files)?;
match self.state.input_profile() {
InputProfile::TeX8Bit if !(0..=255).contains(&value) => {
self.fail("mathcode index must be in 0..255")
}
InputProfile::UnicodeScalar if !(0..=0x10_ffff).contains(&value) => {
self.fail("Bad character code")
}
_ => Ok(value as u32),
}
}
pub(super) fn scan_math_family(
&mut self,
files: &mut impl SourceProvider,
) -> Result<u8, Error> {
let value = self.integer(files)?;
let count = self.state.math_family_count();
if !(0..count).contains(&value) {
return self.fail(format!("native math family must be in 0..{}", count - 1));
}
Ok(value as u8)
}
pub(super) fn scan_unicode_math_code(
&mut self,
fields: bool,
files: &mut impl SourceProvider,
) -> Result<MathCode, Error> {
let value = self.integer(files)?;
if !fields {
return MathCode::from_word(value).map_err(|message| self.error(message, self.current));
}
if !(0..=7).contains(&value) {
return self.fail("Bad math class");
}
let family = self.scan_math_family(files)?;
let character = self.scan_math_code_index(files)?;
Ok(MathCode::new(value as u8, family, character))
}
pub(super) fn read_math_code(&mut self, character: u32) -> MathCode {
self.reads.insert(match u8::try_from(character) {
Ok(slot) => Dependency::MathCode(slot),
Err(_) => Dependency::UnicodeMathCode(character),
});
self.state.math_code(character)
}
pub(super) fn scan_math_code_assignment(
&mut self,
primitive: Primitive,
global: bool,
files: &mut impl SourceProvider,
) -> Result<(), Error> {
let character = self.scan_math_code_index(files)?;
self.optional_equals(files)?;
let code = match primitive {
Primitive::Mathcode => {
let value = self.integer(files)?;
if !(0..=32768).contains(&value) {
return self.fail("mathcode must be in 0..32768");
}
MathCode::from_legacy(value as u16)
}
Primitive::Umathcode => self.scan_unicode_math_code(true, files)?,
Primitive::Umathcodenum => self.scan_unicode_math_code(false, files)?,
_ => unreachable!(),
};
let equivalent = self.state.math_code(character) == code;
self.assign_equivalence(
Key::Mathcode(character),
global,
equivalent,
files,
|state| {
state.assign_mathcode(character, code, global);
},
)
}
}
@@ -191,12 +191,15 @@ impl Executor {
Meaning::Count(_)
| Meaning::CharGiven(_)
| Meaning::MathCharGiven(_)
| Meaning::UnicodeMathCharGiven(_)
| Meaning::Primitive(
Primitive::Count
| Primitive::Lccode
| Primitive::Uccode
| Primitive::Sfcode
| Primitive::Mathcode
| Primitive::Umathcode
| Primitive::Umathcodenum
| Primitive::Catcode
| Primitive::Endlinechar
| Primitive::Newlinechar
@@ -218,7 +218,12 @@ impl Executor {
let (_, value) = rendered.split_once('=').expect("box diagnostic header");
(format!("box{index}"), value.trim_end_matches('\n').into())
}
Key::Mathcode(c) => (format!("mathcode{c}"), self.state.mathcode(*c).to_string()),
Key::Mathcode(c) => {
let code = self.state.math_code(*c);
let value = code.legacy().map_or(code.word(), i32::from);
(format!("mathcode{c}"), value.to_string())
}
Key::Mathcodenum(_) => unreachable!("packed math code reads do not create save records"),
Key::Delcode(c) => (format!("delcode{c}"), self.state.delcode(*c).to_string()),
Key::Sfcode(c) => (format!("sfcode{c}"), self.state.sfcode(*c).to_string()),
Key::CaseCode(kind, c) => (