Apply Web2C TCX tables to native byte streams

This commit is contained in:
gloamxun committed 2026-11-28 09:55:09 +08:00
1 parent 3bb2330676
commit 509f229a52
13 files changed
+393 -38

No files matched your search

+37 -22
View File
@@ -369,6 +369,11 @@ impl EntryCheckpoint {
}
impl Executor {
pub fn with_byte_translation(mut self, table: ByteTranslation) -> Self {
self.state.byte_translation = Some(Arc::new(table));
self
}
// Native jobs enforce the no-shell-escape policy for immediate and shipped writes.
const SHELL_ESCAPE: i32 = 0;
// Compatibility level for the eight-bit native PDF interface.
@@ -2919,7 +2924,7 @@ impl Executor {
},
))?
.map(|(offset, value)| xe_encoding::Character {
value,
value: self.state.translate_input_character(value),
start: base + offset,
end: base + offset + profile.width(value),
})
@@ -3155,18 +3160,21 @@ impl Executor {
OutputOperation::Write { stream, tokens } => {
let expanded = self.fully_expand(tokens.clone(), files)?;
let text = self.token_list_text(&expanded);
let mut text = self.external_print(&text);
let mut bytes = self.external_print_bytes(&text);
if *stream == 18 {
text = format!("runsystem({text})...disabled.\n");
let mut message = b"runsystem(".to_vec();
message.extend_from_slice(&bytes);
message.extend_from_slice(b")...disabled.\n");
bytes = message;
} else {
text.push('\n');
bytes.push(b'\n');
}
if (0..16).contains(stream) {
self.reads.insert(Dependency::OutputStream(*stream as u8));
}
if (0..16).contains(stream) && self.io.outputs.contains(&(*stream as u8)) {
files
.write_out(*stream as u8, &text)
.write_out_bytes(*stream as u8, &bytes)
.map_err(|e| self.error(e, node.span))?;
} else {
let destination = if matches!(*stream, 17 | 18) {
@@ -3175,11 +3183,11 @@ impl Executor {
WriteDestination::TerminalAndLog
};
files
.diagnostic(destination, &text)
.diagnostic_bytes(destination, &bytes)
.map_err(|e| self.error(e, node.span))?;
self.events.push(Event::Write {
destination,
text,
text: super::byte_translation::diagnostic_text(&bytes),
span: node.span,
});
}
@@ -3261,27 +3269,33 @@ impl Executor {
/// terminal/log/file printers encode ASCII controls unless newlinechar
/// selects that code as a physical output line break.
fn external_print(&mut self, text: &str) -> std::string::String {
super::byte_translation::diagnostic_text(&self.external_print_bytes(text))
}
fn external_print_bytes(&mut self, text: &str) -> Vec<u8> {
self.reads.insert(Dependency::NewLineChar);
let newline = u8::try_from(self.state.newlinechar).ok().map(char::from);
let mut printed = std::string::String::new();
let byte_mode = self.state.input_profile() == super::InputProfile::TeX8Bit;
let table = self.state.byte_translation.as_deref().filter(|_| byte_mode);
let mut bytes = Vec::with_capacity(text.len());
for c in text.chars() {
if Some(c) == newline {
printed.push('\n');
} else if self.state.input_profile() == super::InputProfile::TeX8Bit
&& (c as u32) >= 128
&& (c as u32) <= 255
bytes.push(b'\n');
} else if let Some(table) = table
&& let Ok(value) = u8::try_from(c as u32)
{
printed.push_str(&super::context::encoded_character(c));
} else if c < ' ' {
printed.push_str("^^");
printed.push(char::from(c as u8 + 64));
} else if c == '\u{7f}' {
printed.push_str("^^?");
if table.printable(value) {
bytes.push(table.output(value));
} else {
bytes.extend(super::context::encoded_character(c).bytes().map(|b| table.output(b)));
}
} else if c < ' ' || c == '\u{7f}' || (byte_mode && ('\u{80}'..='\u{ff}').contains(&c)) {
bytes.extend_from_slice(super::context::encoded_character(c).as_bytes());
} else {
printed.push(c);
bytes.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
}
}
printed
bytes
}
fn meaning_string(&mut self, token: &Token) -> Result<std::string::String, Error> {
self.reads.insert(Dependency::EscapeChar);
@@ -4174,7 +4188,8 @@ impl Executor {
Message | Errmessage => {
let expanded = self.scan_expanded_group(files)?;
let text = self.token_list_text(&expanded);
let text = self.external_print(&text);
let bytes = self.external_print_bytes(&text);
let text = super::byte_translation::diagnostic_text(&bytes);
if primitive == Errmessage {
let mut error = self.error(
format!("TeX errmessage: {text}"),
@@ -4191,7 +4206,7 @@ impl Executor {
return Err(error);
}
files
.observe_message(&text)
.observe_message_bytes(&bytes)
.map_err(|e| self.error(e, token.span))?;
self.events.push(Event::Message {
text,