Embed project images in route-D previews and PDFs
This commit is contained in:
1 parent
ae69eba64d
commit
0f8aff7290
8 files changed
+407
-18
No files matched your search
+51
-6
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
collections::hash_map::DefaultHasher,
|
||||
collections::{HashMap, hash_map::DefaultHasher},
|
||||
env,
|
||||
fmt::Write as _,
|
||||
fs,
|
||||
@@ -809,9 +809,22 @@ fn store_route_d_pdf(key: (String, u64), pdf: &[u8]) {
|
||||
}
|
||||
}
|
||||
|
||||
fn route_d_cache_key(id: &str, source: &str) -> (String, u64) {
|
||||
fn route_d_cache_key(
|
||||
id: &str,
|
||||
source: &str,
|
||||
images: &HashMap<String, route_d::RasterImage>,
|
||||
) -> (String, u64) {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
source.hash(&mut hasher);
|
||||
let mut names: Vec<&String> = images.keys().collect();
|
||||
names.sort();
|
||||
for name in names {
|
||||
name.hash(&mut hasher);
|
||||
let image = &images[name];
|
||||
image.width.hash(&mut hasher);
|
||||
image.height.hash(&mut hasher);
|
||||
image.rgb.hash(&mut hasher);
|
||||
}
|
||||
(id.to_string(), hasher.finish())
|
||||
}
|
||||
|
||||
@@ -878,6 +891,36 @@ fn expand_project_source(source: &str, store: &projects::ProjectStore, id: &str)
|
||||
expand_project_inputs(&expanded, store, id)
|
||||
}
|
||||
|
||||
/// Decode every `\includegraphics` file a project source references.
|
||||
fn load_project_images(
|
||||
source: &str,
|
||||
store: &projects::ProjectStore,
|
||||
id: &str,
|
||||
) -> HashMap<String, route_d::RasterImage> {
|
||||
let mut images = HashMap::new();
|
||||
for path in route_d::image_paths(source) {
|
||||
let Ok(bytes) = store
|
||||
.read_file(id, &path)
|
||||
.or_else(|_| store.read_file(id, &format!("{path}.png")))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let Ok(decoded) = image::load_from_memory(&bytes) else {
|
||||
continue;
|
||||
};
|
||||
let rgb = decoded.to_rgb8();
|
||||
images.insert(
|
||||
path,
|
||||
route_d::RasterImage {
|
||||
width: rgb.width(),
|
||||
height: rgb.height(),
|
||||
rgb: rgb.into_raw(),
|
||||
},
|
||||
);
|
||||
}
|
||||
images
|
||||
}
|
||||
|
||||
fn expand_project_inputs(source: &str, store: &projects::ProjectStore, id: &str) -> String {
|
||||
let read = |path: &str| {
|
||||
let bytes = store
|
||||
@@ -981,11 +1024,12 @@ fn handle_project_request(
|
||||
};
|
||||
if uses_route_d(url) {
|
||||
let expanded = expand_project_source(&source, &store, id);
|
||||
let key = route_d_cache_key(id, &expanded);
|
||||
let images = load_project_images(&expanded, &store, id);
|
||||
let key = route_d_cache_key(id, &expanded, &images);
|
||||
if let Some(json) = route_d_cached_preview(&key) {
|
||||
return respond_json(request, 200, &preview_without_changes(&json));
|
||||
}
|
||||
return match route_d::project_preview_json(id, &expanded) {
|
||||
return match route_d::project_preview_json_with_images(id, &expanded, &images) {
|
||||
Ok(json) => {
|
||||
store_route_d_preview(key, json.clone());
|
||||
respond_json(request, 200, &json)
|
||||
@@ -1010,11 +1054,12 @@ fn handle_project_request(
|
||||
};
|
||||
if uses_route_d(url) {
|
||||
let expanded = expand_project_source(&source, &store, id);
|
||||
let key = route_d_cache_key(id, &expanded);
|
||||
let images = load_project_images(&expanded, &store, id);
|
||||
let key = route_d_cache_key(id, &expanded, &images);
|
||||
if let Some(pdf) = route_d_cached_pdf(&key) {
|
||||
return respond_pdf_with_cache(request, pdf, "hit");
|
||||
}
|
||||
return match route_d::pdf_bytes(&expanded) {
|
||||
return match route_d::pdf_bytes_with_images(&expanded, &images) {
|
||||
Ok(pdf) => {
|
||||
store_route_d_pdf(key, &pdf);
|
||||
respond_pdf_with_cache(request, pdf, "miss")
|
||||
|
||||
Reference in new issue
Block a user