Advance native LaTeX execution and preview

This commit is contained in:
gloamxun committed 2026-11-18 22:17:21 +08:00
1 parent 91f9d76b16
commit 2741546ca8
414 files changed
+99872 -933

No files matched your search

+243 -2
View File
@@ -26,10 +26,150 @@ fn main() {
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]".into());
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] [--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]".into(),
);
};
if command == "native" {
let mut native_job_target = false;
let mut initex = false;
let mut dump_format = false;
let mut limits = texrra_tex_core::execution::Limits::default();
let mut load_format = None;
let mut etex = false;
let mut input_profile = texrra_tex_core::execution::InputProfile::TeX8Bit;
let mut texmf_roots = Vec::new();
let mut resource_manifest = None;
let mut layout_manifest = None;
let mut failure_manifest = None;
let mut project_snapshot = None;
let mut generated_initial = None;
let mut observe_generated_initial = false;
let mut input_policy_explicit = false;
let mut input_policy = texrra_tex_core::resources::InputPolicy::Project;
let input = loop {
match args.next().as_deref() {
Some("--input-profile") => {
input_profile = match args.next().as_deref() {
Some("tex8bit") => texrra_tex_core::execution::InputProfile::TeX8Bit,
Some("unicode") => texrra_tex_core::execution::InputProfile::UnicodeScalar,
_ => return Err("--input-profile requires tex8bit or unicode".into()),
};
}
Some("--job") => native_job_target = true,
Some("--initex") => initex = true,
Some("--etex") => etex = true,
Some("--dump-format") => dump_format = true,
Some("--step-limit") => {
limits.steps = args
.next()
.and_then(|value| value.parse::<usize>().ok())
.filter(|value| *value > 0)
.ok_or("--step-limit requires a positive integer")?;
}
Some("--load-format") => {
load_format = Some(PathBuf::from(
args.next().ok_or("--load-format requires a filename")?,
))
}
Some("--texmf-root") => texmf_roots.push(PathBuf::from(
args.next().ok_or("--texmf-root requires a directory")?,
)),
Some("--layout-manifest") => {
layout_manifest = Some(PathBuf::from(
args.next().ok_or("--layout-manifest requires a filename")?,
));
}
Some("--resource-manifest") => {
resource_manifest = Some(PathBuf::from(
args.next()
.ok_or("--resource-manifest requires a filename")?,
))
}
Some("--failure-manifest") => {
failure_manifest = Some(PathBuf::from(
args.next()
.ok_or("--failure-manifest requires a filename")?,
));
}
Some("--project-snapshot") => {
project_snapshot = Some(PathBuf::from(
args.next()
.ok_or("--project-snapshot requires a filename")?,
));
}
Some("--generated-initial") => {
generated_initial = Some(PathBuf::from(
args.next()
.ok_or("--generated-initial requires a filename")?,
));
}
Some("--observe-generated-initial") => observe_generated_initial = true,
Some("--input-policy") => {
input_policy_explicit = true;
input_policy = match args.next().as_deref() {
Some("tex") => texrra_tex_core::resources::InputPolicy::Tex,
Some("project") => texrra_tex_core::resources::InputPolicy::Project,
_ => return Err("--input-policy requires tex or project".into()),
}
}
Some(value) if value.starts_with('-') => {
return Err(format!("unknown native option: {value}"));
}
Some(value) => break PathBuf::from(value),
None => return Err("native requires input.tex".into()),
}
};
if generated_initial.is_some() && observe_generated_initial {
return Err("select --generated-initial or --observe-generated-initial".into());
}
if project_snapshot.is_some() && (!texmf_roots.is_empty() || input_policy_explicit) {
return Err("--project-snapshot defines its resource paths and input policy; do not combine it with --texmf-root or --input-policy".into());
}
if dump_format && (!initex || load_format.is_some()) || initex && load_format.is_some() {
return Err(
"format dump requires INITEX, and format load requires a new document job".into(),
);
}
if dump_format || load_format.is_some() {
native_job_target = true;
}
if layout_manifest.is_some() && (!native_job_target || dump_format) {
return Err("--layout-manifest requires a native document job".into());
}
let output = args
.next()
.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] [--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]".into());
}
let diagnostic = native_pdf(
&input,
&output,
initex,
etex,
&texmf_roots,
resource_manifest.as_deref(),
layout_manifest.as_deref(),
failure_manifest.as_deref(),
project_snapshot.as_deref(),
generated_initial.as_deref(),
observe_generated_initial,
input_policy,
input_profile,
native_job_target,
dump_format,
load_format.as_deref(),
limits,
)?;
if diagnostic {
process::exit(1);
}
return Ok(());
}
if command != "serve" {
return Err("the only command is `serve`".into());
return Err("expected serve or native".into());
}
let input_path = args
@@ -85,6 +225,107 @@ fn run() -> Result<(), String> {
Ok(())
}
/// Strict native bootstrap entry: typed execution -> Route-D lists -> PDF.
/// CLI adapter; the library owns compilation, export and publication.
fn native_pdf(
input: &Path,
output: &Path,
initex: bool,
etex: bool,
texmf_roots: &[PathBuf],
manifest: Option<&Path>,
layout_manifest: Option<&Path>,
failure_manifest: Option<&Path>,
project_snapshot: Option<&Path>,
generated_initial: Option<&Path>,
observe_generated_initial: bool,
input_policy: texrra_tex_core::resources::InputPolicy,
input_profile: texrra_tex_core::execution::InputProfile,
native_job_target: bool,
dump_format: bool,
load_format: Option<&Path>,
limits: texrra_tex_core::execution::Limits,
) -> Result<bool, String> {
use texrra_engine::native::{self, CompileRequest, Entry, NativeCompiler, Target};
let project = project_snapshot
.map(|path| -> Result<_, String> {
let bytes =
fs::read(path).map_err(|e| format!("project snapshot {}: {e}", path.display()))?;
let project: native::ProjectSnapshot =
serde_json::from_slice(&bytes).map_err(|e| e.to_string())?;
project.validate()?;
Ok(std::sync::Arc::new(project))
})
.transpose()?;
let world = if let Some(project) = &project {
project.configuration()
} else {
let mut world = texrra_tex_core::resources::WorldConfig::from_environment(
env::current_dir().map_err(|e| e.to_string())?,
texmf_roots.to_vec(),
)?;
world.input_policy = input_policy;
world
};
let mut request = CompileRequest::new(world, input, output, native::job_time()?);
if let Some(project) = project {
request = request.with_project(project);
}
if let Some(path) = generated_initial {
let bytes = fs::read(path)
.map_err(|e| format!("generated initial state {}: {e}", path.display()))?;
let state: native::GeneratedInitialState =
serde_json::from_slice(&bytes).map_err(|e| e.to_string())?;
state.validate()?;
request = request.with_generated_initial(state);
} else if observe_generated_initial {
request = request.observe_generated_initial();
}
request.entry = if let Some(path) = load_format {
Entry::Format(path.into())
} else if initex {
Entry::Initex
} else {
Entry::Bootstrap
};
request.target = if native_job_target {
Target::Shipouts
} else {
Target::Paragraphs
};
request.etex = etex;
request.input_profile = input_profile;
request.limits = limits;
request.resource_manifest = manifest.map(Path::to_owned);
request.layout_manifest = layout_manifest.map(Path::to_owned);
request.failure_manifest = failure_manifest.map(Path::to_owned);
request.terminal = true;
let failure_message = |failure: native::CompileFailure| {
for gap in &failure.evidence_gaps {
eprintln!("native failure evidence: {gap}");
}
failure.to_string()
};
if dump_format {
native::dump_format_detailed(request, texrra_tex_core::execution::Cancellation::default())
.map_err(failure_message)?;
return Ok(false);
}
let result = NativeCompiler::default()
.compile_detailed(request)
.map_err(failure_message)?;
if result.output.is_none() {
println!("native Route-D execution complete (no pages)");
} else {
println!(
"wrote {} ({} pages, native Route-D)",
output.display(),
result.pages.len()
);
}
Ok(result.has_show_diagnostic())
}
fn handle_request(
mut request: Request,
input_path: &PathBuf,