Rebind native format TFM resources after project relocation

This commit is contained in:
gloamxun committed 2026-11-28 01:46:37 +08:00
1 parent 1ad9c4db82
commit 65204a7dae
9 files changed
+304 -35

No files matched your search

+156
View File
@@ -1732,6 +1732,162 @@ fn relocated_format_checks_current_sources_and_consumed_stream_ranges() {
fs::remove_dir_all(root).unwrap();
}
#[test]
fn relocated_format_binds_packed_and_new_glyphs_to_the_current_tfm() {
use texrra_tex_core::render::DisplayItem;
let root = directory("format-tfm-relocation").canonicalize().unwrap();
let origin = root.join("origin");
let project = root.join("project");
let world = |cwd: &Path| {
let mut configuration = WorldConfig::new(cwd);
for (kind, directory) in [
(ResourceKind::Tfm, "metrics"),
(ResourceKind::Map, "fonts"),
(ResourceKind::Type1, "fonts"),
] {
configuration
.paths
.insert(kind, vec![SearchPath::directory(directory, false)]);
}
configuration
};
let mut build = request(&origin);
build.world = world(&origin);
build.input = "seed.tex".into();
build.output = "seed.tfx".into();
let seed = br"\catcode123=1 \catcode125=2
\pdfoutput=1 \font\moving=cmr10
\setbox0=\vbox{\hbox{\moving ABC}}
\dump
";
let tfm_bytes = include_bytes!("../crates/texrra-tex-core/assets/cmr10.tfm");
let type1_bytes = include_bytes!("../assets/fonts/cmr10.pfb");
let map = b"cmr10 CMR10 <cmr10.pfb\n";
fs::create_dir_all(origin.join("metrics")).unwrap();
fs::create_dir_all(origin.join("fonts")).unwrap();
fs::write(origin.join("seed.tex"), seed).unwrap();
fs::write(origin.join("metrics/cmr10.tfm"), tfm_bytes).unwrap();
fs::write(origin.join("fonts/pdftex.map"), map).unwrap();
fs::write(origin.join("fonts/cmr10.pfb"), type1_bytes).unwrap();
dump_format(build, Cancellation::default()).unwrap();
fs::rename(&origin, &project).unwrap();
let format = project.join("seed.tfx");
let entry = project.join("entry.tex");
let metric = project.join("metrics/cmr10.tfm");
let outline = project.join("fonts/cmr10.pfb");
let prefix = "\\shipout\\hbox{\\copy0 \\moving DEF}\n";
let padding = format!("{}\n", "\\relax ".repeat(30)).repeat(42);
let source = |word| format!("{prefix}{padding}\\shipout\\hbox{{\\moving {word}}}\\end\n");
let configuration = world(&project);
let snapshot = ProjectSnapshot::new(ProjectDefinition {
configuration: configuration.clone(),
revision: 1,
roots: [project.clone()].into(),
files: [
(project.join("seed.tex"), Arc::from(seed.as_slice())),
(format.clone(), fs::read(&format).unwrap().into()),
(entry.clone(), source("OLD").into_bytes().into()),
(metric.clone(), Arc::from(tfm_bytes.as_slice())),
(outline.clone(), Arc::from(type1_bytes.as_slice())),
(project.join("fonts/pdftex.map"), Arc::from(map.as_slice())),
]
.into(),
directories: Default::default(),
aliases: Default::default(),
generated_roots: [project.clone()].into(),
generated: Default::default(),
})
.unwrap();
let mut job = request(&project);
job.world = configuration;
job.input = entry.clone();
job.entry = Entry::Format(format);
let compiler = NativeCompiler::default().with_entry_checkpoints(prefix.len() + 48);
let first = compiler
.compile(job.clone().with_project(snapshot.clone()))
.unwrap();
let version = file_version(&metric).unwrap().unwrap();
assert_eq!(
first.resources.trace.format_input_bindings[&origin.join("metrics/cmr10.tfm")],
metric
);
assert!(first.resources.trace.reads.contains(&version));
assert!(
first
.resources
.trace
.reads
.contains(&file_version(&outline).unwrap().unwrap())
);
let glyphs: Vec<_> = first.pages[0]
.items
.iter()
.filter_map(|item| match item {
DisplayItem::NativeGlyph { font, slot, .. } => {
assert_eq!(font.resource.version, version);
Some((font.id, *slot))
}
_ => None,
})
.collect();
assert_eq!(
glyphs.iter().map(|(_, slot)| *slot).collect::<Vec<_>>(),
b"ABCDEF"
);
assert!(glyphs.iter().all(|(id, _)| *id == glyphs[0].0));
let changed = snapshot
.with_file_edits(2, [(entry, source("NEW").into_bytes().into())].into())
.unwrap();
let revised = job.clone().with_project(changed.clone());
let next = compiler.compile(revised.clone()).unwrap();
assert!(
next.entry_checkpoint_work.reused_steps > 0,
"{:?}",
next.entry_checkpoint_work
);
let delta = next.preview_since(Some(first.revision())).unwrap();
assert_eq!(
delta
.pages
.iter()
.map(|page| page.index)
.collect::<Vec<_>>(),
[1]
);
let full = NativeCompiler::default().compile(revised).unwrap();
assert_eq!(next.pdf, full.pdf);
assert_eq!(next.pages, full.pages);
assert_eq!(next.source_map.pages, full.source_map.pages);
assert_eq!(next.source_map.sources, full.source_map.sources);
assert_eq!(
next.preview_since(None).unwrap().pages,
full.preview_since(None).unwrap().pages
);
let mut altered = tfm_bytes.to_vec();
altered[24] ^= 1;
let changed = changed
.with_file_edits(3, [(metric.clone(), altered.into())].into())
.unwrap();
let error = compiler
.compile_detailed(job.with_project(changed))
.err()
.unwrap()
.error
.to_string();
assert!(
error.contains("native format resource changed")
&& error.contains(metric.to_str().unwrap()),
"{error}"
);
assert!(Arc::ptr_eq(&next, &compiler.latest().unwrap()));
fs::remove_dir_all(root).unwrap();
}
#[test]
fn shared_format_build_load_and_cancelled_build_preserve_real_output_policy() {
let p = directory("format");