Select a native XeTeX compatibility profile and wide spacing codes

This commit is contained in:
gloamxun committed 2026-11-21 12:17:53 +08:00
1 parent 651fab42f1
commit 752af43f32
19 files changed
+471 -115

No files matched your search

+165 -69
View File
@@ -361,6 +361,8 @@ pub enum Primitive {
Umathcharnumdef,
Uchar,
Ucharcat,
XeTeXversion,
XeTeXrevision,
}
impl Primitive {
@@ -413,6 +415,7 @@ impl Primitive {
| Jobname
| Pdfcreationdate
| Pdftexrevision
| XeTeXrevision
| Pdfstrcmp
| Pdfmatch
| Pdflastmatch
@@ -530,7 +533,7 @@ pub(super) enum Key {
Box(u16),
Mathcode(u32),
Delcode(u8),
Sfcode(u8),
Sfcode(u32),
CaseCode(super::CaseCode, char),
Mathfont(u8, u8),
Mathcodenum(u32),
@@ -669,7 +672,7 @@ pub struct State {
mathcodes: BTreeMap<u32, super::MathCode>,
delcodes: BTreeMap<u8, i32>,
#[serde(default)]
sfcodes: BTreeMap<u8, u16>,
sfcodes: BTreeMap<u32, u16>,
#[serde(with = "ordered_map")]
case_codes: BTreeMap<(super::CaseCode, char), u32>,
#[serde(with = "ordered_map")]
@@ -751,6 +754,82 @@ fn buffered_fingerprint_matches_the_ordered_state_hash() {
check(&state);
}
// These primitives belong to the pdfTeX command surface. Position queries,
// page dimensions and the shared margin-kern extension are installed separately.
const PDFTEX_ONLY_PRIMITIVES: &[(&str, Primitive)] = {
use Primitive::*;
&[
("knbccode", Knbccode),
("knaccode", Knaccode),
("efcode", Efcode),
("pdffontexpand", Pdffontexpand),
("letterspacefont", Letterspacefont),
("pdffilesize", Pdffilesize),
("pdffilemoddate", Pdffilemoddate),
("pdfmdfivesum", Pdfmdfivesum),
("pdfximage", Pdfximage),
("pdfrefximage", Pdfrefximage),
("pdflastximage", Pdflastximage),
("pdflastximagepages", Pdflastximagepages),
("pdflastximagecolordepth", Pdflastximagecolordepth),
("pdfcreationdate", Pdfcreationdate),
("pdfshellescape", Pdfshellescape),
("pdfstrcmp", Pdfstrcmp),
#[cfg(all(target_os = "linux", target_env = "gnu"))]
("pdfmatch", Pdfmatch),
#[cfg(all(target_os = "linux", target_env = "gnu"))]
("pdflastmatch", Pdflastmatch),
("pdfcolorstackinit", Pdfcolorstackinit),
("pdfobj", Pdfobj),
("pdfrefobj", Pdfrefobj),
("pdflastobj", Pdflastobj),
("pdflastlink", Pdflastlink),
("pdfrandomseed", Pdfrandomseed),
("pdfsetrandomseed", Pdfsetrandomseed),
("pdfuniformdeviate", Pdfuniformdeviate),
("pdfnormaldeviate", Pdfnormaldeviate),
("pdfpageref", Pdfpageref),
("pdfliteral", Pdfliteral),
("pdfcolorstack", Pdfcolorstack),
("pdfsave", Pdfsave),
("pdfstartlink", Pdfstartlink),
("pdfendlink", Pdfendlink),
("pdfrunninglinkoff", Pdfrunninglinkoff),
("pdfrunninglinkon", Pdfrunninglinkon),
("pdfinterwordspaceon", Pdfinterwordspaceon),
("pdfinterwordspaceoff", Pdfinterwordspaceoff),
("pdffakespace", Pdffakespace),
("pdfspacefont", Pdfspacefont),
("pdfdest", Pdfdest),
("pdfcatalog", Pdfcatalog),
("pdfoutline", Pdfoutline),
("pdfinfo", Pdfinfo),
("pdfsetmatrix", Pdfsetmatrix),
("pdfrestore", Pdfrestore),
("pdfglyphtounicode", Pdfglyphtounicode),
("pdfmapfile", Pdfmapfile),
("pdfmapline", Pdfmapline),
("pdffontattr", Pdffontattr),
("pdfnobuiltintounicode", Pdfnobuiltintounicode),
]
};
const XETEX_PRIMITIVE_ALIASES: &[(&str, Primitive)] = {
use Primitive::*;
&[
("creationdate", Pdfcreationdate),
("filemoddate", Pdffilemoddate),
("filesize", Pdffilesize),
("mdfivesum", Pdfmdfivesum),
("normaldeviate", Pdfnormaldeviate),
("randomseed", Pdfrandomseed),
("setrandomseed", Pdfsetrandomseed),
("shellescape", Pdfshellescape),
("strcmp", Pdfstrcmp),
("uniformdeviate", Pdfuniformdeviate),
]
};
impl Default for State {
fn default() -> Self {
use Primitive::*;
@@ -825,12 +904,7 @@ impl Default for State {
("hyphenchar", Hyphenchar),
("lpcode", Lpcode),
("rpcode", Rpcode),
("knbccode", Knbccode),
("knaccode", Knaccode),
("efcode", Efcode),
("pdffontexpand", Pdffontexpand),
("fontname", Fontname),
("letterspacefont", Letterspacefont),
("fontdimen", Fontdimen),
("skewchar", Skewchar),
("mathchar", Mathchar),
@@ -1009,60 +1083,13 @@ impl Default for State {
("message", Message),
("errmessage", Errmessage),
("end", End),
("pdffilesize", Pdffilesize),
("pdffilemoddate", Pdffilemoddate),
("pdfmdfivesum", Pdfmdfivesum),
("pdfximage", Pdfximage),
("pdfrefximage", Pdfrefximage),
("pdflastximage", Pdflastximage),
("pdflastximagepages", Pdflastximagepages),
("pdflastximagecolordepth", Pdflastximagecolordepth),
("jobname", Jobname),
("pdfcreationdate", Pdfcreationdate),
("pdfshellescape", Pdfshellescape),
("pdfstrcmp", Pdfstrcmp),
#[cfg(all(target_os = "linux", target_env = "gnu"))]
("pdfmatch", Pdfmatch),
#[cfg(all(target_os = "linux", target_env = "gnu"))]
("pdflastmatch", Pdflastmatch),
("pdfcolorstackinit", Pdfcolorstackinit),
("pdfobj", Pdfobj),
("pdfrefobj", Pdfrefobj),
("pdflastobj", Pdflastobj),
("pdflastlink", Pdflastlink),
("pdfrandomseed", Pdfrandomseed),
("pdfsetrandomseed", Pdfsetrandomseed),
("pdfuniformdeviate", Pdfuniformdeviate),
("pdfnormaldeviate", Pdfnormaldeviate),
("pdfpageref", Pdfpageref),
("partokenname", Partokenname),
("pdfliteral", Pdfliteral),
("leftmarginkern", Leftmarginkern),
("rightmarginkern", Rightmarginkern),
("pdfcolorstack", Pdfcolorstack),
("pdfsave", Pdfsave),
("pdfstartlink", Pdfstartlink),
("pdfendlink", Pdfendlink),
("pdfrunninglinkoff", Pdfrunninglinkoff),
("pdfrunninglinkon", Pdfrunninglinkon),
("pdfinterwordspaceon", Pdfinterwordspaceon),
("pdfinterwordspaceoff", Pdfinterwordspaceoff),
("pdffakespace", Pdffakespace),
("pdfspacefont", Pdfspacefont),
("pdfdest", Pdfdest),
("pdfcatalog", Pdfcatalog),
("pdfoutline", Pdfoutline),
("pdfinfo", Pdfinfo),
("pdfsetmatrix", Pdfsetmatrix),
("pdfsavepos", Pdfsavepos),
("pdflastxpos", Pdflastxpos),
("pdflastypos", Pdflastypos),
("pdfrestore", Pdfrestore),
("pdfglyphtounicode", Pdfglyphtounicode),
("pdfmapfile", Pdfmapfile),
("pdfmapline", Pdfmapline),
("pdffontattr", Pdffontattr),
("pdfnobuiltintounicode", Pdfnobuiltintounicode),
("dump", Dump),
("indent", Indent),
("noindent", Noindent),
@@ -1136,6 +1163,7 @@ impl Default for State {
] {
state.parameters.insert(p, v);
}
state.install_pdftex_primitives();
state.install_unicode_commands();
state
}
@@ -1404,6 +1432,7 @@ impl State {
Meaning::Primitive(primitive),
);
}
self.install_xetex_etex_commands();
self
}
pub fn register_limit(&self) -> u16 {
@@ -1544,22 +1573,81 @@ impl State {
("ifpdfabsnum", Primitive::Ifpdfabsnum),
("ifpdfabsdim", Primitive::Ifpdfabsdim),
] {
let name = CommandName::Control(name.into());
match profile {
super::InputProfile::TeX8Bit => {
self.commands.insert(name, Meaning::Primitive(primitive));
}
super::InputProfile::UnicodeScalar => {
if self.commands.get(&name) == Some(&Meaning::Primitive(primitive)) {
self.commands.remove(&name);
}
}
}
self.update_profile_command(
name,
Meaning::Primitive(primitive),
profile == super::InputProfile::TeX8Bit,
);
}
self.install_pdftex_primitives();
for &(name, primitive) in XETEX_PRIMITIVE_ALIASES {
self.update_profile_command(
name,
Meaning::Primitive(primitive),
profile == super::InputProfile::XeTeX,
);
}
self.install_unicode_commands();
self.install_xetex_etex_commands();
self
}
fn update_profile_command(&mut self, name: &str, meaning: Meaning, enabled: bool) {
let name = CommandName::Control(name.into());
if enabled {
self.commands.entry(name).or_insert(meaning);
} else if self.commands.get(&name) == Some(&meaning) {
self.commands.remove(&name);
}
}
fn install_pdftex_primitives(&mut self) {
let enabled = self.input_profile != super::InputProfile::XeTeX;
for &(name, primitive) in PDFTEX_ONLY_PRIMITIVES {
self.update_profile_command(name, Meaning::Primitive(primitive), enabled);
}
for parameter in [
TokenParameter::Pdfpageresources,
TokenParameter::Pdfpageattr,
TokenParameter::Pdfpagesattr,
] {
self.update_profile_command(
&parameter.name(),
Meaning::TokenParameter(parameter),
enabled,
);
}
for &parameter in Parameter::ALL {
if parameter.pdftex_only() {
self.update_profile_command(
&parameter.name(),
Meaning::Parameter(parameter),
enabled,
);
}
}
}
fn install_xetex_etex_commands(&mut self) {
let enabled = self.input_profile == super::InputProfile::XeTeX
&& self.profile == Profile::EtexBootstrap;
for (name, primitive) in [
("XeTeXversion", Primitive::XeTeXversion),
("XeTeXrevision", Primitive::XeTeXrevision),
] {
self.update_profile_command(name, Meaning::Primitive(primitive), enabled);
}
}
pub(super) fn profile_primitive_name(&self, primitive: Primitive) -> Option<&'static str> {
if self.input_profile == super::InputProfile::XeTeX {
return XETEX_PRIMITIVE_ALIASES
.iter()
.find_map(|&(name, meaning)| (meaning == primitive).then_some(name));
}
None
}
fn install_unicode_commands(&mut self) {
use Primitive::*;
for (name, primitive) in [
@@ -1580,7 +1668,7 @@ impl State {
] {
let name = CommandName::Control(name.into());
let meaning = Meaning::Primitive(primitive);
if self.input_profile == super::InputProfile::UnicodeScalar {
if self.input_profile != super::InputProfile::TeX8Bit {
self.commands.insert(name, meaning);
} else if self.commands.get(&name) == Some(&meaning) {
self.commands.remove(&name);
@@ -1653,7 +1741,7 @@ impl State {
pub(super) fn math_family_count(&self) -> i32 {
match self.input_profile {
super::InputProfile::TeX8Bit => 16,
super::InputProfile::UnicodeScalar => 256,
super::InputProfile::UnicodeScalar | super::InputProfile::XeTeX => 256,
}
}
pub fn math_font(&self, size: u8, family: u8) -> FontId {
@@ -1663,12 +1751,20 @@ impl State {
.unwrap_or(FontId::NULL)
}
pub fn sfcode(&self, c: u8) -> u16 {
self.space_factor_code(u32::from(c))
}
pub(super) fn space_factor_code(&self, c: u32) -> u16 {
self.sfcodes
.get(&c)
.copied()
.unwrap_or(if c.is_ascii_uppercase() { 999 } else { 1000 })
.unwrap_or(if (u32::from(b'A')..=u32::from(b'Z')).contains(&c) {
999
} else {
1000
})
}
pub(super) fn assign_sfcode(&mut self, c: u8, value: u16, global: bool) {
pub(super) fn assign_sfcode(&mut self, c: u32, value: u16, global: bool) {
self.save(Key::Sfcode(c), global);
self.sfcodes.insert(c, value);
}