From 01ef302fb026fc05330e11ab8e28af15ab3886d4 Mon Sep 17 00:00:00 2001 From: GloamXun Date: Sat, 21 Nov 2026 14:04:00 +0800 Subject: [PATCH] Scan extended XeTeX superscript character notation --- crates/texrra-tex-core/src/execution/mouth.rs | 137 +++++++++++------- docs/P2_M1_PROGRESS.md | 21 ++- 2 files changed, 107 insertions(+), 51 deletions(-) diff --git a/crates/texrra-tex-core/src/execution/mouth.rs b/crates/texrra-tex-core/src/execution/mouth.rs index e3be833..5288267 100644 --- a/crates/texrra-tex-core/src/execution/mouth.rs +++ b/crates/texrra-tex-core/src/execution/mouth.rs @@ -146,6 +146,44 @@ mod tests { .any(|t| t.kind == TokenKind::Character('!', CatCode::Other)) ); } + #[test] + fn xetex_extended_superscripts_preserve_spans_in_characters_and_control_words() { + use super::super::InputProfile; + let text = r"^^^^2032 \wide^^^^4e2d^^^^^^01f600 ^^41 ^^^041 ^^^^^1f600"; + let mut state = State::default().with_input_profile(InputProfile::XeTeX); + state.assign_catcode('中', CatCode::Letter, false); + state.assign_catcode('😀', CatCode::Letter, false); + let mut mouth = Mouth::new(0, text.into(), InputProfile::XeTeX); + let mut reads = BTreeSet::new(); + let prime = mouth.next(&state, &mut reads).unwrap().unwrap(); + assert_eq!(prime.kind, TokenKind::Character('′', state.catcode('′'))); + assert_eq!((prime.span.start, prime.span.end), (0, 8)); + assert!( + mouth + .next(&state, &mut reads) + .unwrap() + .unwrap() + .kind + .is_space() + ); + let word = mouth.next(&state, &mut reads).unwrap().unwrap(); + let name = r"\wide^^^^4e2d^^^^^^01f600"; + assert_eq!( + word.kind, + TokenKind::Command(CommandName::Control("wide中😀".into())) + ); + assert_eq!(&text[word.span.start..word.span.end], name); + for expected in ['A', 'A', '😀'] { + let token = loop { + let token = mouth.next(&state, &mut reads).unwrap().unwrap(); + if !token.kind.is_space() { + break token; + } + }; + assert_eq!(token.kind, TokenKind::Character(expected, CatCode::Letter)); + } + } + #[test] fn physical_line_endings_and_utf8_byte_offsets_are_preserved() { let result = tokens("é界 \r\nB\rC"); @@ -380,38 +418,60 @@ impl Mouth { reads.insert(Dependency::CatCode(c)); state.catcode(c) } + // next points at the second superscript mark. The returned length + // includes that mark and all remaining characters in the encoding. + fn expanded_superscript(&self, mark: char, next: usize) -> Option<(char, usize)> { + if self.line.get(next)?.value != mark { + return None; + } + let single = self.line.get(next + 1)?.value; + let mut count = 2; + if self.profile == super::InputProfile::XeTeX { + // XeTeX counts matching marks while the current encoding still + // has enough physical line slots. It supports up to six digits. + while count < 6 + && next + 2 * count - 2 < self.line.len() + && self.line[next + count - 1].value == mark + { + count += 1; + } + } + let first_digit = next + count - 1; + let hex = self + .line + .get(first_digit..first_digit + count) + .and_then(|digits| { + digits.iter().try_fold(0, |value, digit| { + let digit = match digit.value { + '0'..='9' => digit.value as u32 - u32::from('0'), + 'a'..='f' => digit.value as u32 - u32::from('a') + 10, + _ => return None, + }; + Some(value * 16 + digit) + }) + }); + if let Some(code) = hex { + return char::from_u32(code).map(|value| (value, 2 * count - 1)); + } + single + .is_ascii() + .then(|| (char::from(single as u8 ^ 64), 2)) + } + /// Decode TeX's ^^ form before category lookup. Category changes affect /// unread characters; tokens already in macro bodies are never retokenized. fn peek(&mut self, state: &State, reads: &mut BTreeSet) -> Option { loop { let c = *self.line.get(self.cursor)?; - if Self::catcode(state, reads, c.value) != CatCode::Superscript - || self.line.get(self.cursor + 1).map(|c| c.value) != Some(c.value) - { + if Self::catcode(state, reads, c.value) != CatCode::Superscript { return Some(c); } - let Some(next) = self.line.get(self.cursor + 2).copied() else { + let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor + 1) else { return Some(c); }; - let hex = |c: char| { - if c.is_ascii_digit() || ('a'..='f').contains(&c) { - c.to_digit(16) - } else { - None - } - }; - let (value, consumed) = if let Some(a) = hex(next.value) - && let Some(b) = self.line.get(self.cursor + 3).and_then(|c| hex(c.value)) - { - (char::from_u32(16 * a + b).unwrap(), 4) - } else if (next.value as u32) < 128 { - (char::from_u32((next.value as u32) ^ 64).unwrap(), 3) - } else { - return Some(c); - }; - let end = self.line[self.cursor + consumed - 1].end; + let end = self.line[self.cursor + extra].end; self.line.splice( - self.cursor..self.cursor + consumed, + self.cursor..=self.cursor + extra, [Character { value, start: c.start, @@ -425,38 +485,15 @@ impl Mouth { /// Keeping these paths distinct also preserves TeX's displayed context. fn next_character(&mut self, state: &State, reads: &mut BTreeSet) -> Character { let mut c = self.line[self.cursor]; - let mut consumed = 1; - loop { - let second = self.cursor + consumed; - if Self::catcode(state, reads, c.value) != CatCode::Superscript - || self.line.get(second).map(|v| v.value) != Some(c.value) - { - break; - } - let Some(next) = self.line.get(second + 1).copied() else { - break; - }; - let hex = |c: char| { - if c.is_ascii_digit() || ('a'..='f').contains(&c) { - c.to_digit(16) - } else { - None - } - }; - let (value, extra) = if let Some(a) = hex(next.value) - && let Some(b) = self.line.get(second + 2).and_then(|v| hex(v.value)) - { - (char::from_u32(16 * a + b).unwrap(), 3) - } else if (next.value as u32) < 128 { - (char::from_u32(next.value as u32 ^ 64).unwrap(), 2) - } else { + self.cursor += 1; + while Self::catcode(state, reads, c.value) == CatCode::Superscript { + let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor) else { break; }; c.value = value; - c.end = self.line[self.cursor + consumed + extra - 1].end; - consumed += extra; + self.cursor += extra; + c.end = self.line[self.cursor - 1].end; } - self.cursor += consumed; c } pub fn next( diff --git a/docs/P2_M1_PROGRESS.md b/docs/P2_M1_PROGRESS.md index 2babc6c..cacc664 100644 --- a/docs/P2_M1_PROGRESS.md +++ b/docs/P2_M1_PROGRESS.md @@ -2175,6 +2175,25 @@ PDF,恢复实际 PFB 并反转斜切时复用 1277 步,只替换第二页。 全量编译一致。incremental-receipt.json 的 SHA-256 为 bbc0991b0316e6d76aa58649ec873525f5544ab530d93fe491fc6e16bd00a27d。 +### XeTeX 扩展 ^^ 字符输入 + +XeTeX profile 在字符与控制序列的扫描路径共用扩展字符转换。 +两个至六个相同的上标类字符可读取对应数量的十六进制位。 +沿用类别查找、经典 ^^X 回退和原始字节范围。控制序列的 +输入缓冲区仍按真实被消耗的字符重写,普通字符扫描只前移位置。 + +9 项输入扫描专项通过。原样 INITEX 输入两侧的命令名称、 +非 BMP 字符、畸形十六进制的回退和重新赋予上标类别的四组 +诊断逐项一致。核对 schema64 的 83 份实际文本资源后直接重载 +同一格式。原样 unicode-math 通过 ^^^^2032 的输入入口, +随后在 fontspec 的真实字体查询遇到 \suppressfontnotfounderror。 +同一包与字体的 XeTeX 参考已出页。原始与当前日志分别位于 +target/p3-xetex-raster/math-next/native 和 native-after。 +下一步接通局部字体查询参数和真实缺失字体的处理,然后继续 +OpenType MATH。 + +[XeTeX 的扩展字符扫描](https://github.com/TeX-Live/texlive-source/blob/trunk/texk/web2c/xetexdir/xetex.web#L8612-L8739) + ### XeTeX 位图原语和出页设备 \XeTeXpicfile 接入扩展原语表。PNG 读取 pHYs,JPEG 读取 JFIF @@ -2202,7 +2221,7 @@ target/p3-xetex-raster/receipt.json。23 项定向回归通过。三页项目在 [LaTeX 的 XeTeX 图形驱动](https://github.com/latex3/graphics-def/blob/main/xetex.def) 同一 schema64 推进至原样 unicode-math-xetex.sty 第 3253 行。 -XeTeX 参考已用真实 Latin Modern Math 出页。当前首处差异为 +XeTeX 参考已用真实 Latin Modern Math 出页。该次重载的首处差异为 ^^^^2032 被输入扫描拆成 U+001E。原样输入、从 CTAN TeX Live 取得的包与字体、参考 PDF 和失败日志保存在 target/p3-xetex-raster/math-next。接续 XeTeX 的扩展 ^^ 字符