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

+11 -2
View File
@@ -30,7 +30,7 @@ fn run() -> Result<(), String> {
let mut args = env::args().skip(1);
let Some(command) = args.next() else {
return Err(
"usage: texrra serve [input.tex] [port] | texrra native [--job] [--initex] [--etex] [--dump-format | --load-format FILE] [--step-limit N] [--input-profile tex8bit|unicode|xetex] [--texmf-root DIR] [--input-policy tex|project] [--project-snapshot FILE] [--generated-initial JSON | --observe-generated-initial] [--resource-manifest JSON] [--layout-manifest JSON] [--failure-manifest JSON] input.tex [output.pdf|output.dvi]".into(),
"usage: texrra serve [input.tex] [port] | texrra native [--job] [--initex] [--etex] [--dump-format | --load-format FILE] [--step-limit N] [--input-profile tex8bit|unicode|xetex] [--translate-file FILE] [--texmf-root DIR] [--input-policy tex|project] [--project-snapshot FILE] [--generated-initial JSON | --observe-generated-initial] [--resource-manifest JSON] [--layout-manifest JSON] [--failure-manifest JSON] input.tex [output.pdf|output.dvi]".into(),
);
};
if command == "native" {
@@ -49,6 +49,7 @@ fn run() -> Result<(), String> {
let mut generated_initial = None;
let mut observe_generated_initial = false;
let mut input_policy_explicit = false;
let mut translation_file = None;
let mut input_policy = texrra_tex_core::resources::InputPolicy::Project;
let input = loop {
match args.next().as_deref() {
@@ -60,6 +61,11 @@ fn run() -> Result<(), String> {
_ => return Err("--input-profile requires tex8bit, unicode or xetex".into()),
};
}
Some("--translate-file") => {
translation_file = Some(PathBuf::from(
args.next().ok_or("--translate-file requires a TCX filename")?,
));
}
Some("--job") => native_job_target = true,
Some("--initex") => initex = true,
Some("--etex") => etex = true,
@@ -146,7 +152,7 @@ fn run() -> Result<(), String> {
.map(PathBuf::from)
.unwrap_or_else(|| input.with_extension(if dump_format { "tfx" } else { "pdf" }));
if args.next().is_some() {
return Err("usage: texrra native [--job] [--initex] [--etex] [--dump-format | --load-format FILE] [--step-limit N] [--input-profile tex8bit|unicode|xetex] [--texmf-root DIR] [--input-policy tex|project] [--project-snapshot FILE] [--generated-initial JSON | --observe-generated-initial] [--resource-manifest JSON] [--layout-manifest JSON] [--failure-manifest JSON] input.tex [output.pdf|output.dvi]".into());
return Err("usage: texrra native [--job] [--initex] [--etex] [--dump-format | --load-format FILE] [--step-limit N] [--input-profile tex8bit|unicode|xetex] [--translate-file FILE] [--texmf-root DIR] [--input-policy tex|project] [--project-snapshot FILE] [--generated-initial JSON | --observe-generated-initial] [--resource-manifest JSON] [--layout-manifest JSON] [--failure-manifest JSON] input.tex [output.pdf|output.dvi]".into());
}
let diagnostic = native_compile(
&input,
@@ -162,6 +168,7 @@ fn run() -> Result<(), String> {
observe_generated_initial,
input_policy,
input_profile,
translation_file.as_deref(),
native_job_target,
dump_format,
load_format.as_deref(),
@@ -245,6 +252,7 @@ fn native_compile(
observe_generated_initial: bool,
input_policy: texrra_tex_core::resources::InputPolicy,
input_profile: texrra_tex_core::execution::InputProfile,
translation_file: Option<&Path>,
native_job_target: bool,
dump_format: bool,
load_format: Option<&Path>,
@@ -299,6 +307,7 @@ fn native_compile(
};
request.etex = etex;
request.input_profile = input_profile;
request.translation_file = translation_file.map(Path::to_owned);
request.limits = limits;
request.resource_manifest = manifest.map(Path::to_owned);
request.layout_manifest = layout_manifest.map(Path::to_owned);
+17
View File
@@ -65,6 +65,7 @@ pub struct CompileRequest {
pub target: Target,
pub etex: bool,
pub input_profile: InputProfile,
pub translation_file: Option<PathBuf>,
pub time: JobTime,
pub limits: Limits,
pub resource_manifest: Option<PathBuf>,
@@ -164,6 +165,7 @@ impl CompileRequest {
target: Target::Shipouts,
etex: false,
input_profile: InputProfile::TeX8Bit,
translation_file: None,
time,
limits: Limits::default(),
resource_manifest: None,
@@ -336,6 +338,8 @@ pub struct AttemptRequest {
pub target: Target,
pub etex: bool,
pub input_profile: InputProfile,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub translation_file: Option<PathBuf>,
pub time: JobTime,
pub limits: Limits,
pub resource_manifest: Option<PathBuf>,
@@ -358,6 +362,7 @@ impl From<&CompileRequest> for AttemptRequest {
target: r.target,
etex: r.etex,
input_profile: r.input_profile,
translation_file: r.translation_file.clone(),
time: r.time,
limits: r.limits,
resource_manifest: r.resource_manifest.clone(),
@@ -784,6 +789,14 @@ fn prepare_checked(
} else {
state
};
let translation = request.translation_file.as_ref().map(|name| {
if request.input_profile != InputProfile::TeX8Bit {
return Err("a TCX file requires the TeX 8-bit profile".to_owned());
}
let loaded = world.read_shared(ResourceKind::Tex, &name.to_string_lossy(), &path)?;
texrra_tex_core::execution::ByteTranslation::from_tcx(&loaded.data)
.map_err(|e| format!("TCX {}: {e}", loaded.version.path.display()))
}).transpose()?;
let (executor, format_preparation) = if let Entry::Format(name) = &request.entry {
let loaded =
world.read_shared(ResourceKind::Tex, &name.display().to_string(), &path)?;
@@ -896,6 +909,10 @@ fn prepare_checked(
(kernel == KernelPreparation::Fresh).then(|| Executor::new(state, request.limits));
(executor, None)
};
let executor = match (executor, translation) {
(Some(executor), Some(table)) => Some(executor.with_byte_translation(table)),
(executor, _) => executor,
};
check(cancellation)?;
Ok((path, source, executor, output, format_preparation))
})();
+1
View File
@@ -713,6 +713,7 @@ impl PrefixInputGuard {
|| old.target != request.target
|| old.etex != request.etex
|| old.input_profile != request.input_profile
|| old.translation_file != request.translation_file
|| old.time != request.time
|| old.limits != request.limits
|| old.terminal != request.terminal
+12
View File
@@ -222,6 +222,10 @@ impl SourceProvider for RecordingHost<'_> {
let text = text.to_owned();
call!(self, observe_message(&text))
}
fn observe_message_bytes(&mut self, bytes: &[u8]) -> Result<(), String> {
let bytes = bytes.to_owned();
call!(self, observe_message_bytes(&bytes))
}
fn observe_diagnostic(
&mut self,
destination: WriteDestination,
@@ -282,6 +286,10 @@ impl SourceProvider for RecordingHost<'_> {
let text = text.to_owned();
call!(self, write_out(stream, &text))
}
fn write_out_bytes(&mut self, stream: u8, bytes: &[u8]) -> Result<(), String> {
let bytes = bytes.to_owned();
call!(self, write_out_bytes(stream, &bytes))
}
fn close_out(&mut self, stream: u8) -> Result<(), String> {
call!(self, close_out(stream))
}
@@ -289,6 +297,10 @@ impl SourceProvider for RecordingHost<'_> {
let text = text.to_owned();
call!(self, diagnostic(destination, &text))
}
fn diagnostic_bytes(&mut self, destination: WriteDestination, bytes: &[u8]) -> Result<(), String> {
let bytes = bytes.to_owned();
call!(self, diagnostic_bytes(destination, &bytes))
}
}
fn digest(bytes: &[u8]) -> String {
+35 -10
View File
@@ -1066,6 +1066,23 @@ impl Files {
text: &str,
start_line: bool,
) -> Result<(), String> {
self.emit_diagnostic_bytes(destination, text.as_bytes(), start_line)
}
fn emit_diagnostic_bytes(
&mut self,
destination: WriteDestination,
bytes: &[u8],
start_line: bool,
) -> Result<(), String> {
let column = |previous| {
let previous = if start_line { 0 } else { previous };
match std::str::from_utf8(bytes) {
Ok(text) => output_column(previous, text),
Err(_) => bytes.iter().rposition(|&b| b == b'\n')
.map_or(previous + bytes.len(), |at| bytes.len() - at - 1),
}
};
if destination != WriteDestination::TerminalOnly {
if self.log.is_none() {
self.log = Some(
@@ -1081,17 +1098,16 @@ impl Files {
if start_line && self.log_column != 0 {
log.write_all(b"\n").map_err(|e| e.to_string())?;
}
log.write_all(text.as_bytes())
.map_err(|e| format!("native log: {e}"))?;
self.log_column = output_column(if start_line { 0 } else { self.log_column }, text);
log.write_all(bytes).map_err(|e| format!("native log: {e}"))?;
self.log_column = column(self.log_column);
}
if self.terminal_sink && self.terminal_enabled && destination != WriteDestination::Log {
let mut terminal = io::stderr().lock();
if start_line && self.terminal_column != 0 {
eprintln!();
terminal.write_all(b"\n").map_err(|e| e.to_string())?;
}
eprint!("{text}");
self.terminal_column =
output_column(if start_line { 0 } else { self.terminal_column }, text);
terminal.write_all(bytes).map_err(|e| e.to_string())?;
self.terminal_column = column(self.terminal_column);
}
Ok(())
}
@@ -1615,6 +1631,9 @@ impl SourceProvider for Files {
fn observe_message(&mut self, text: &str) -> Result<(), String> {
self.emit_diagnostic(WriteDestination::TerminalAndLog, text, false)
}
fn observe_message_bytes(&mut self, bytes: &[u8]) -> Result<(), String> {
self.emit_diagnostic_bytes(WriteDestination::TerminalAndLog, bytes, false)
}
fn observe_diagnostic_pair(&mut self, log: &str, terminal: &str) -> Result<(), String> {
if self.log_column != 0 || self.terminal_column != 0 {
self.emit_diagnostic(WriteDestination::TerminalAndLog, "\n", false)?;
@@ -1871,13 +1890,16 @@ impl SourceProvider for Files {
result
}
fn write_out(&mut self, stream: u8, text: &str) -> Result<(), String> {
self.write_out_bytes(stream, text.as_bytes())
}
fn write_out_bytes(&mut self, stream: u8, bytes: &[u8]) -> Result<(), String> {
let Some(output) = self.outputs.get_mut(&stream) else {
let error = "host output stream is not open".to_owned();
self.stream_io.push(StreamEffect::Write {
stream,
handle: None,
accepted_before: None,
requested: std::sync::Arc::from(text.as_bytes()),
requested: std::sync::Arc::from(bytes),
accepted: 0,
attempts: Vec::new(),
error: Some(error.clone()),
@@ -1885,7 +1907,7 @@ impl SourceProvider for Files {
return Err(error);
};
let before = output.accepted;
let transfer = native_stream::write_all(&mut output.file, text.as_bytes());
let transfer = native_stream::write_all(&mut output.file, bytes);
output.accepted += transfer.accepted as u64;
let error = transfer
.error
@@ -1894,7 +1916,7 @@ impl SourceProvider for Files {
stream,
handle: Some(output.handle),
accepted_before: Some(before),
requested: std::sync::Arc::from(text.as_bytes()),
requested: std::sync::Arc::from(bytes),
accepted: transfer.accepted as u64,
attempts: transfer.attempts,
error: error.clone(),
@@ -1935,6 +1957,9 @@ impl SourceProvider for Files {
fn diagnostic(&mut self, destination: WriteDestination, text: &str) -> Result<(), String> {
self.emit_diagnostic(destination, text, true)
}
fn diagnostic_bytes(&mut self, destination: WriteDestination, bytes: &[u8]) -> Result<(), String> {
self.emit_diagnostic_bytes(destination, bytes, true)
}
}
#[derive(PartialEq, Eq)]