Retain XeTeX image lookup paths in native formats

This commit is contained in:
gloamxun committed 2026-11-28 01:06:12 +08:00
1 parent 6a118d3b60
commit c2e1e64a60
10 files changed
+208 -54

No files matched your search

+61 -10
View File
@@ -581,13 +581,14 @@ impl FileWorld {
&format!("{name}@{density}dpi"),
from,
variants,
None,
)? {
return self.read_path(path);
}
}
// A bare file may be explicitly supplied by a directory path. Never
// recursively pick an arbitrary dpiNNN/name.pk from another density.
for entry in self.paths(ResourceKind::Pk, from) {
for (entry, _) in self.paths(ResourceKind::Pk, from) {
if let SearchPath::Directory { path, suffix, .. } = entry {
let candidate = self
.config
@@ -1238,19 +1239,22 @@ impl FileWorld {
self.indexes.clear();
}
}
fn paths(&self, kind: ResourceKind, from: &Path) -> Vec<SearchPath> {
fn paths(&self, kind: ResourceKind, from: &Path) -> Vec<(SearchPath, bool)> {
// Images use the TeX namespace and have their own dependency role.
let kind = if kind == ResourceKind::PdfImage {
ResourceKind::Tex
} else {
kind
};
let mut defaults = vec![SearchPath::directory(&self.config.cwd, false)];
// Keep default job-relative paths distinct from explicitly configured
// absolute search entries, even when they name the same directory.
let mut defaults = vec![(SearchPath::directory(&self.config.cwd, false), true)];
if kind == ResourceKind::Tex && self.config.input_policy == InputPolicy::Project {
if let Some(parent) = from.parent() {
let path = self.config.cwd.join(parent);
if path != self.config.cwd {
defaults.push(SearchPath::directory(path, false));
let job_relative = path.starts_with(&self.config.cwd);
defaults.push((SearchPath::directory(path, false), job_relative));
}
}
}
@@ -1264,7 +1268,7 @@ impl FileWorld {
self.config
.texmf_roots
.iter()
.map(|r| SearchPath::directory(r.join(directory), true)),
.map(|r| (SearchPath::directory(r.join(directory), true), false)),
);
}
let mut paths = Vec::new();
@@ -1277,7 +1281,7 @@ impl FileWorld {
{
match entry {
SearchPath::Defaults => paths.extend(defaults.clone()),
e => paths.push(e.clone()),
e => paths.push((e.clone(), false)),
}
}
paths
@@ -1442,7 +1446,7 @@ impl FileWorld {
name: &str,
from: &Path,
) -> Result<Option<PathBuf>, String> {
self.resolve_variants(kind, name, from, Self::variants(kind, name, false))
self.resolve_variants(kind, name, from, Self::variants(kind, name, false), None)
}
/// Apply the TeX input scanner's default suffix alternatives. Raw host
@@ -1450,7 +1454,7 @@ impl FileWorld {
pub fn resolve_tex_input(&mut self, name: &str, from: &Path) -> Result<Option<PathBuf>, String> {
let kind = ResourceKind::Tex;
let fallback = self.config.input_policy == InputPolicy::Tex && self.tex_suffix_fallback;
self.resolve_variants(kind, name, from, Self::variants(kind, name, fallback))
self.resolve_variants(kind, name, from, Self::variants(kind, name, fallback), None)
}
fn resolve_variants(
@@ -1459,16 +1463,26 @@ impl FileWorld {
name: &str,
from: &Path,
variants: Vec<PathBuf>,
mut lookup_path: Option<&mut PathBuf>,
) -> Result<Option<PathBuf>, String> {
let mut candidates = Vec::new();
let result = (|| {
let paths = self.paths(kind, from);
let mut found = None;
if Path::new(name).is_absolute() || name.starts_with("./") || name.starts_with("../") {
let requested = Path::new(name);
if requested.is_absolute()
|| matches!(
requested.components().next(),
Some(std::path::Component::CurDir | std::path::Component::ParentDir)
)
{
for variant in &variants {
let path = self.config.cwd.join(variant);
candidates.push(path.clone());
if let Some(path) = self.probe(&path)? {
if let Some(lookup_path) = lookup_path.as_deref_mut() {
lookup_path.clone_from(variant);
}
found = Some(path);
break;
}
@@ -1477,7 +1491,7 @@ impl FileWorld {
// TeX honors search-path priority before the suffix alternatives.
// A recursive path is one entry: search all of it for .tex before
// trying the bare name, rather than flattening it into directories.
'paths: for entry in &paths {
'paths: for (entry, job_relative) in &paths {
let SearchPath::Directory {
path,
recursive,
@@ -1487,10 +1501,24 @@ impl FileWorld {
unreachable!()
};
let root = self.config.cwd.join(path);
let lookup_name = |cwd: &Path, candidate: &Path| {
if *job_relative
&& let Ok(relative) = candidate.strip_prefix(cwd)
{
return Path::new(".").join(relative);
}
if path.is_relative() && let Ok(relative) = candidate.strip_prefix(&root) {
return path.join(relative);
}
candidate.to_owned()
};
for variant in &variants {
let direct = root.join(suffix).join(variant);
candidates.push(direct.clone());
if let Some(path) = self.probe(&direct)? {
if let Some(lookup_path) = lookup_path.as_deref_mut() {
*lookup_path = lookup_name(&self.config.cwd, &direct);
}
found = Some(path);
break 'paths;
}
@@ -1545,6 +1573,9 @@ impl FileWorld {
}
candidates.push(candidate.clone());
if let Some(path) = self.probe(&candidate)? {
if let Some(lookup_path) = lookup_path.as_deref_mut() {
*lookup_path = lookup_name(&self.config.cwd, &candidate);
}
found = Some(path);
break 'paths;
}
@@ -1574,6 +1605,26 @@ impl FileWorld {
self.read_shared(kind, name, from)
.map(SnapshotFile::into_loaded)
}
/// Read a resource and return the path spelling selected by the lookup.
/// Its version records the resolved identity independently.
pub fn read_with_lookup_path(
&mut self,
kind: ResourceKind,
name: &str,
from: &Path,
) -> Result<(LoadedResource, PathBuf), String> {
let mut lookup_path = PathBuf::new();
let path = self.resolve_variants(
kind,
name,
from,
Self::variants(kind, name, false),
Some(&mut lookup_path),
)?;
let resource = self.read_shared_resolved(kind, name, from, path)?;
Ok((resource.into_loaded(), lookup_path))
}
/// Resolve and record a resource read while sharing the captured bytes.
pub fn read_shared(
&mut self,