Normalize native XeTeX physical input before tokenization

This commit is contained in:
gloamxun committed 2026-11-26 13:02:38 +08:00
1 parent 048478c346
commit 76df2d6832
10 files changed
+214 -63

No files matched your search

+57 -19
View File
@@ -112,6 +112,39 @@ mod tests {
);
assert!(mouth.next(&state, &mut reads).unwrap().is_none());
}
#[test]
fn xe_normalization_preserves_reordered_bytes_and_control_word_extent() {
use super::super::{InputProfile, Parameter, ParameterValue, state::Key};
let mut state = State::default().with_input_profile(InputProfile::XeTeX);
state.assign_integer(Key::Endlinechar, -1, false);
for mark in ['\u{0300}', '\u{0315}'] {
state.assign_catcode(mark, CatCode::Letter, false);
}
let mut mouth = Mouth::new(0, "é\n\\a\u{0315}\u{0300}!\na\u{0315}\u{0300}é".into(), InputProfile::XeTeX);
let mut reads = BTreeSet::new();
let first = mouth.next(&state, &mut reads).unwrap().unwrap();
assert!(matches!(first.kind, TokenKind::Character('é', _)));
assert_eq!((first.span.start, first.span.end), (0, 2));
state.assign_parameter(Parameter::XeTeXinputnormalization, ParameterValue::Integer(2), false);
let name = mouth.next(&state, &mut reads).unwrap().unwrap();
assert_eq!(name.kind, TokenKind::Command(CommandName::Control("a\u{0300}\u{0315}".into())));
assert_eq!((name.span.start, name.span.end), (3, 9));
for (value, range) in [
('!', (9, 10)),
('a', (11, 12)),
('\u{0300}', (14, 16)),
('\u{0315}', (12, 14)),
('e', (16, 18)),
('\u{0301}', (16, 18)),
] {
let token = mouth.next(&state, &mut reads).unwrap().unwrap();
assert!(matches!(token.kind, TokenKind::Character(c, _) if c == value));
assert_eq!((token.span.start, token.span.end), range);
}
assert!(reads.contains(&Dependency::Parameter(Parameter::XeTeXinputnormalization)));
}
#[test]
fn disabled_endlinechar_does_not_merge_control_names_across_lines() {
let mut mouth = Mouth::new(
@@ -369,8 +402,15 @@ impl Mouth {
pub(super) fn xe_location(&self, offset: usize) -> Option<(usize, usize)> {
let xe = self.xe.as_ref()?;
(xe.line_start <= offset && offset <= self.offset && self.line_number > 0)
.then(|| (self.line_number, 1 + self.line.partition_point(|c| c.end <= offset && c.start < offset)))
(xe.line_start <= offset && offset <= self.offset && self.line_number > 0).then(|| {
// Canonical ordering can move a mark ahead of an earlier byte range.
let preceding = self
.line
.iter()
.filter(|c| c.end <= offset && c.start < offset)
.count();
(self.line_number, 1 + preceding)
})
}
pub(super) fn take_utf8_warnings(&mut self) -> Vec<(usize, usize)> {
@@ -518,12 +558,10 @@ impl Mouth {
self.line_number += 1;
self.line.clear();
self.cursor = 0;
let (mut chars, invalid_utf8) = xe.encoding.decode(&self.data[start..content_end], start)
let (characters, invalid_utf8) = xe.encoding
.decode_line(&self.data[start..content_end], start, state, reads)
.map_err(|message| (Span { source: self.source, start, end: content_end }, message))?;
while chars.last().is_some_and(|c| matches!(c.value, ' ' | '\r' | '\n')) {
chars.pop();
}
self.line = chars;
self.line = characters;
if invalid_utf8 > 0 {
self.invalid_utf8.push((self.line_number, invalid_utf8));
}
@@ -607,15 +645,11 @@ impl Mouth {
let Some((value, extra)) = self.expanded_superscript(c.value, self.cursor + 1) else {
return Some(c);
};
let end = self.line[self.cursor + extra].end;
self.line.splice(
self.cursor..=self.cursor + extra,
[Character {
value,
start: c.start,
end,
}],
);
let mut replacement = Character { value, ..c };
for &part in &self.line[self.cursor + 1..=self.cursor + extra] {
replacement.cover(part);
}
self.line.splice(self.cursor..=self.cursor + extra, [replacement]);
}
}
/// get_next's ordinary ^^ conversion changes cur_chr and advances loc.
@@ -629,8 +663,10 @@ impl Mouth {
break;
};
c.value = value;
for &part in &self.line[self.cursor..self.cursor + extra] {
c.cover(part);
}
self.cursor += extra;
c.end = self.line[self.cursor - 1].end;
}
c
}
@@ -688,7 +724,8 @@ impl Mouth {
let mut name = String::new();
if let Some(next) = self.peek(state, reads) {
self.cursor += 1;
span.end = next.end;
span.start = span.start.min(next.start);
span.end = span.end.max(next.end);
name.push(next.value);
let code = Self::catcode(state, reads, next.value);
if code == CatCode::Letter {
@@ -697,7 +734,8 @@ impl Mouth {
break;
}
name.push(next.value);
span.end = next.end;
span.start = span.start.min(next.start);
span.end = span.end.max(next.end);
self.cursor += 1;
}
self.state = LineState::Skip;