Resolve graphics through graphicspath directories

This commit is contained in:
gloamxun committed 2026-10-24 23:35:14 +08:00
1 parent 46f86c771e
commit 9aacb91184
6 files changed
+118 -8

No files matched your search

+59
View File
@@ -185,6 +185,8 @@ struct PageGeometry {
conditionals: Vec<ConditionalDefinition>,
/// Decoded images keyed by `\includegraphics` path.
images: HashMap<String, RasterImage>,
/// Directories searched for `\includegraphics` paths.
graphics_paths: Vec<String>,
}
/// A `\newtheorem` declaration: display name, shared counter and section scope.
@@ -962,6 +964,12 @@ fn strip_page_geometry(source: &str) -> (String, PageGeometry) {
}
continue;
}
if trimmed.starts_with("\\graphicspath") {
geometry
.graphics_paths
.extend(graphics_path_groups(trimmed));
continue;
}
if let Some(value) = braced_value(trimmed, "\\title") {
geometry.title = Some(value);
continue;
@@ -2152,6 +2160,47 @@ fn parse_image_length(value: &str) -> Option<ImageLength> {
None
}
/// Directories declared by `\graphicspath{{figures/}{plots/}}`.
pub fn graphics_paths(source: &str) -> Vec<String> {
let mut paths = Vec::new();
for line in source.lines() {
let trimmed = line.trim();
if trimmed.starts_with("\\graphicspath") {
paths.extend(graphics_path_groups(trimmed));
}
}
paths
}
fn graphics_path_groups(line: &str) -> Vec<String> {
let Some((argument, _)) = braced_group(line.trim_start_matches("\\graphicspath").trim_start())
else {
return Vec::new();
};
let mut groups = Vec::new();
let mut rest = argument;
loop {
rest = rest.trim_start();
if rest.is_empty() {
break;
}
let Some((path, consumed)) = braced_group(rest) else {
break;
};
let path = path.trim();
if path.is_empty() {
break;
}
let mut path = path.to_string();
if !path.ends_with('/') {
path.push('/');
}
groups.push(path);
rest = &rest[consumed..];
}
groups
}
/// Every distinct `\includegraphics` path in a source file.
pub fn image_paths(source: &str) -> Vec<String> {
let mut paths: Vec<String> = Vec::new();
@@ -4998,6 +5047,16 @@ mod source_tests {
}
}
#[test]
fn graphics_paths_are_read_from_the_preamble() {
let (body, geometry) = geometry_and_body(
"\\graphicspath{{figures/}{plots}}\n\\begin{figure}[t]\n\\includegraphics{x}\n\\end{figure}",
);
assert_eq!(geometry.graphics_paths, vec!["figures/", "plots/"]);
assert!(!body.contains("graphicspath"));
assert_eq!(graphics_paths("\\graphicspath{{a/}}"), vec!["a/"]);
}
#[test]
fn figure_graphics_reserve_a_placeholder() {
let source = "\\begin{figure}[b]\n\\includegraphics[width=200pt]{plot.pdf}\\caption{Plot}\n\\end{figure}";