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

+71 -14
View File
@@ -353,6 +353,12 @@ pub enum Primitive {
Rightmarginkern,
Pdflastlink,
Special,
Umathcode,
Umathcodenum,
Umathchar,
Umathcharnum,
Umathchardef,
Umathcharnumdef,
}
impl Primitive {
@@ -483,6 +489,7 @@ pub enum Meaning {
Register(Register),
Parameter(Parameter),
Font(FontId),
UnicodeMathCharGiven(super::MathCode),
}
/// Explicit bootstrap language selection. EtexBootstrap implements the native
@@ -517,11 +524,12 @@ pub(super) enum Key {
ParagraphShape,
Spacefactor,
Box(u16),
Mathcode(u8),
Mathcode(u32),
Delcode(u8),
Sfcode(u8),
CaseCode(super::CaseCode, char),
Mathfont(u8, u8),
Mathcodenum(u32),
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
@@ -537,7 +545,7 @@ pub(super) enum Saved {
ParagraphShape(std::sync::Arc<[super::paragraph::ParagraphShapeLine]>),
Font(FontId),
Box(Option<NativeBox>),
Mathcode(Option<u16>),
Mathcode(Option<super::MathCode>),
Delcode(Option<i32>),
Sfcode(Option<u16>),
CaseCode(Option<u32>),
@@ -654,7 +662,7 @@ pub struct State {
boxes: BTreeMap<u16, NativeBox>,
pub(super) split_marks: BTreeMap<u16, super::SplitMarks>,
pub(super) page_marks: BTreeMap<u16, super::PageMarks>,
mathcodes: BTreeMap<u8, u16>,
mathcodes: BTreeMap<u32, super::MathCode>,
delcodes: BTreeMap<u8, i32>,
#[serde(default)]
sfcodes: BTreeMap<u8, u16>,
@@ -1124,6 +1132,7 @@ impl Default for State {
] {
state.parameters.insert(p, v);
}
state.install_unicode_math_commands();
state
}
}
@@ -1543,8 +1552,36 @@ impl State {
}
}
}
self.install_unicode_math_commands();
self
}
fn install_unicode_math_commands(&mut self) {
use Primitive::*;
for (name, primitive) in [
("Umathcode", Umathcode),
("XeTeXmathcode", Umathcode),
("Umathcodenum", Umathcodenum),
("XeTeXmathcodenum", Umathcodenum),
("Umathchar", Umathchar),
("XeTeXmathchar", Umathchar),
("Umathcharnum", Umathcharnum),
("XeTeXmathcharnum", Umathcharnum),
("Umathchardef", Umathchardef),
("XeTeXmathchardef", Umathchardef),
("Umathcharnumdef", Umathcharnumdef),
("XeTeXmathcharnumdef", Umathcharnumdef),
] {
let name = CommandName::Control(name.into());
let meaning = Meaning::Primitive(primitive);
if self.input_profile == super::InputProfile::UnicodeScalar {
self.commands.insert(name, meaning);
} else if self.commands.get(&name) == Some(&meaning) {
self.commands.remove(&name);
}
}
}
pub fn input_profile(&self) -> super::InputProfile {
self.input_profile
}
@@ -1583,16 +1620,35 @@ impl State {
.cloned()
.unwrap_or(Meaning::Undefined)
}
pub fn mathcode(&self, c: u8) -> u16 {
self.mathcodes.get(&c).copied().unwrap_or_else(|| {
if c.is_ascii_alphabetic() {
0x7100 + c as u16
} else if c.is_ascii_digit() {
0x7000 + c as u16
} else {
c as u16
}
})
/// Classic projection for byte-table consumers. A code outside the
/// representable classic range has TeX's recovery value of zero.
pub fn mathcode(&self, character: u8) -> u16 {
self.math_code(u32::from(character)).legacy().unwrap_or(0)
}
pub fn math_code(&self, character: u32) -> super::MathCode {
self.mathcodes
.get(&character)
.copied()
.unwrap_or_else(|| super::MathCode::default_for(character))
}
pub(super) fn restore_legacy_mathcodes(&mut self) -> Result<(), String> {
for value in self.mathcodes.values_mut() {
let legacy = u16::try_from(value.word())
.ok()
.filter(|&code| code <= 0x8000)
.ok_or("invalid legacy mathcode in native format")?;
*value = super::MathCode::from_legacy(legacy);
}
Ok(())
}
pub(super) fn math_family_count(&self) -> i32 {
match self.input_profile {
super::InputProfile::TeX8Bit => 16,
super::InputProfile::UnicodeScalar => 256,
}
}
pub fn math_font(&self, size: u8, family: u8) -> FontId {
self.mathfonts
@@ -1642,7 +1698,7 @@ impl State {
self.save(Key::Delcode(c), global);
self.delcodes.insert(c, n);
}
pub(super) fn assign_mathcode(&mut self, c: u8, n: u16, global: bool) {
pub(super) fn assign_mathcode(&mut self, c: u32, n: super::MathCode, global: bool) {
self.save(Key::Mathcode(c), global);
self.mathcodes.insert(c, n);
}
@@ -1917,6 +1973,7 @@ impl State {
Key::Font => Saved::Font(self.text_font),
Key::Box(n) => Saved::Box(self.boxes.get(n).cloned()),
Key::Mathcode(c) => Saved::Mathcode(self.mathcodes.get(c).copied()),
Key::Mathcodenum(_) => unreachable!("packed math code reads do not create save records"),
Key::Delcode(c) => Saved::Delcode(self.delcodes.get(c).copied()),
Key::Sfcode(c) => Saved::Sfcode(self.sfcodes.get(c).copied()),
Key::CaseCode(kind, c) => Saved::CaseCode(self.case_codes.get(&(*kind, *c)).copied()),