PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Save for Web: slices never overwrite each other, and overlapping slices keep their HTML cells (#908, #909) (#929)
Brandon Thomas5bfadbadd0fe
diff --git a/crates/engine/src/web_cmds.rs b/crates/engine/src/web_cmds.rsindex b52fd3a1d..19e2997aa 100644--- a/crates/engine/src/web_cmds.rs+++ b/crates/engine/src/web_cmds.rs@@ -659,7 +659,7 @@ fn html_table(title: &str, w: u32, h: u32, cells: &[(Rect, String, String)], spafor (r, attrs, body) in cells {let (c0, c1, r0, r1) = (col(r.x0), col(r.x1), row(r.y0), row(r.y1));if (r0..r1).any(|j| (c0..c1).any(|i| taken[j * nc + i])) {- continue; // overlapped by an earlier slice+ continue; // overlapped (the caller cuts slices into visible pieces, so this can't happen)}for j in r0..r1 {for i in c0..c1 {@@ -750,35 +750,69 @@ fn save_for_web(s: &mut Session, p: &Value) -> Result<Value> {let mut files = Vec::new();let mut cells: Vec<(Rect, String, String)> = Vec::new();let mut total = 0usize;+ // Output names are unique (case-insensitively, for macOS and Windows file systems): two+ // slices named alike, or names that sanitize alike, must not overwrite each other. The HTML+ // page's spacer image is reserved.+ let mut used: std::collections::HashSet<String> = std::collections::HashSet::new();+ if html {+ used.insert("spacer.gif".into());+ }+ let mut unique = |stem: &str, ext: &str| {+ let mut name = format!("{stem}.{ext}");+ let mut k = 2u32;+ while !used.insert(name.to_lowercase()) {+ name = format!("{stem}_{k}.{ext}");+ k = k.saturating_add(1);+ }+ name+ };+ // Stacking order: later stored slices are on top (auto slices never overlap anything).+ let z = |r: &ResolvedSlice| r.id.and_then(|id| doc.slices.list.iter().position(|sl| sl.id == id));for r in &chosen {let rect = scale_rect(r.rect, sx, sy).intersect(&wdoc.bounds());if rect.is_empty() {continue;}+ // In the HTML table a slice shows only where no slice above covers it; that visible part+ // is cut into rectangles, each its own cell (and image), as Photoshop's subslices.+ let pieces = if html {+ let above: Vec<Rect> = chosen.iter().filter(|o| z(o) > z(r)).map(|o| scale_rect(o.rect, sx, sy).intersect(&wdoc.bounds())).collect();+ slices::auto_slices(rect, &above)+ } else {+ vec![rect]+ };+ let split = pieces.len() > 1;let stored = r.id.and_then(|id| doc.slices.get(id));- if r.kind == SliceKind::NoImage {- let text = stored.map(|s| if s.cell_text_is_html { s.cell_text.clone() } else { html_escape(&s.cell_text) }).unwrap_or_default();- let bg = stored.and_then(|s| s.background).map(|c| format!(" bgcolor=\"#{:02X}{:02X}{:02X}\"", c[1], c[2], c[3])).unwrap_or_default();- cells.push((rect, bg, text));- continue;- }- let o = optimize(&buf.px, bw, rect, &st, icc, xmp.as_deref(), dpi, false)?;- let name = format!("{}.{}", slice_file(&r.name), o.ext);- let rel = if images.is_empty() { name.clone() } else { format!("{images}/{name}") };- let out = join(&dir, &rel);- write_file(&out, &o.bytes)?;- total += o.bytes.len();- files.push(out);- let alt = stored.map_or(String::new(), |s| html_escape(&s.alt));- let img = format!("\n\t\t\t<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{alt}\">", html_escape(&rel), rect.width(), rect.height());- let body = match stored.filter(|s| !s.url.is_empty()) {- Some(s) => {- let target = if s.target.is_empty() { String::new() } else { format!(" target=\"{}\"", html_escape(&s.target)) };- format!("\n\t\t\t<a href=\"{}\"{target}>{img}</a>", html_escape(&s.url))+ for (k, piece) in pieces.into_iter().enumerate() {+ if r.kind == SliceKind::NoImage {+ let text = if k > 0 {+ String::new()+ } else {+ stored.map(|s| if s.cell_text_is_html { s.cell_text.clone() } else { html_escape(&s.cell_text) }).unwrap_or_default()+ };+ let bg = stored.and_then(|s| s.background).map(|c| format!(" bgcolor=\"#{:02X}{:02X}{:02X}\"", c[1], c[2], c[3])).unwrap_or_default();+ cells.push((piece, bg, text));+ continue;}- None => img,- };- cells.push((rect, String::new(), body));+ let o = optimize(&buf.px, bw, piece, &st, icc, xmp.as_deref(), dpi, false)?;+ let stem = if split { format!("{}_{:02}", slice_file(&r.name), k + 1) } else { slice_file(&r.name) };+ let name = unique(&stem, o.ext);+ let rel = if images.is_empty() { name.clone() } else { format!("{images}/{name}") };+ let out = join(&dir, &rel);+ write_file(&out, &o.bytes)?;+ total += o.bytes.len();+ files.push(out);+ let alt = stored.map_or(String::new(), |s| html_escape(&s.alt));+ let img = format!("\n\t\t\t<img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"{alt}\">", html_escape(&rel), piece.width(), piece.height());+ let body = match stored.filter(|s| !s.url.is_empty()) {+ Some(s) => {+ let target = if s.target.is_empty() { String::new() } else { format!(" target=\"{}\"", html_escape(&s.target)) };+ format!("\n\t\t\t<a href=\"{}\"{target}>{img}</a>", html_escape(&s.url))+ }+ None => img,+ };+ cells.push((piece, String::new(), body));+ }}let mut html_path = None;if html {diff --git a/crates/engine/src/web_cmds/tests.rs b/crates/engine/src/web_cmds/tests.rsindex c04cd4977..7dcc92af0 100644--- a/crates/engine/src/web_cmds/tests.rs+++ b/crates/engine/src/web_cmds/tests.rs@@ -266,3 +266,48 @@ fn jpeg_preview_at_odd_sizes() {}}}++#[test]+fn slices_with_the_same_name_get_distinct_files() {+ // #908: two slices named `tile` (and names that sanitize alike) used to overwrite each other.+ let dir = tmp("dupes");+ let mut s = session(8);+ s.execute("slice.new", json!({"rect": [0, 0, 10, 10], "name": "tile"})).unwrap();+ s.execute("slice.new", json!({"rect": [20, 0, 10, 10], "name": "tile"})).unwrap();+ s.execute("slice.new", json!({"rect": [40, 0, 10, 10], "name": "a/b"})).unwrap();+ s.execute("slice.new", json!({"rect": [0, 20, 10, 10], "name": "a?b"})).unwrap();+ s.execute("slice.new", json!({"rect": [20, 20, 10, 10], "name": "spacer"})).unwrap();+ let r = s.execute("file.export.saveForWebLegacy", json!({"format": "gif", "dir": dir, "slices": "user", "html": true})).unwrap();+ let mut files: Vec<String> = r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().rsplit('/').next().unwrap().to_string()).collect();+ files.sort();+ assert_eq!(files, ["a_b.gif", "a_b_2.gif", "spacer_2.gif", "tile.gif", "tile_2.gif"]);+ let html = std::fs::read_to_string(r["html"].as_str().unwrap()).unwrap();+ assert!(html.contains("images/tile.gif") && html.contains("images/tile_2.gif"));+}++#[test]+fn overlapping_slices_keep_the_later_slice_in_html() {+ // #909: the later slice is on top; the earlier one shows only where it isn't covered.+ let dir = tmp("overlap");+ let mut s = session(8);+ s.execute("slice.new", json!({"rect": [0, 0, 40, 30], "name": "under", "url": "https://example.org/under"})).unwrap();+ s.execute("slice.new", json!({"rect": [20, 15, 40, 30], "name": "over", "url": "https://example.org/over"})).unwrap();+ let r = s.execute("file.export.saveForWebLegacy", json!({"format": "png24", "dir": dir, "html": true})).unwrap();+ let html = std::fs::read_to_string(r["html"].as_str().unwrap()).unwrap();+ assert!(html.contains("https://example.org/over"), "the later slice's cell is in the table");+ assert!(html.contains("https://example.org/under"));+ let files: Vec<String> = r["files"].as_array().unwrap().iter().map(|v| v.as_str().unwrap().to_string()).collect();+ let over = files.iter().find(|f| f.ends_with("images/over.png")).unwrap();+ assert_eq!(decode(over).dimensions(), (40, 30));+ // The visible pieces of every image tile the canvas exactly once.+ let area: u64 = files+ .iter()+ .filter(|f| !f.ends_with("spacer.gif"))+ .map(|f| {+ let (w, h) = decode(f).dimensions();+ u64::from(w) * u64::from(h)+ })+ .sum();+ assert_eq!(area, 64 * 48);+ assert!(files.iter().any(|f| f.ends_with("images/under_01.png")), "the covered slice is cut into pieces: {files:?}");+}