Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #843

Claudec97b2790d36e
diff --cc crates/engine/src/export.rs
index 6a4edbebc,edc4760cf..ad1d7e560
--- a/crates/engine/src/export.rs
+++ b/crates/engine/src/export.rs
@@@ -2072,74 -2010,27 +2086,95 @@@ mod tests
assert!(ExportOptions::from_json(&serde_json::json!({"watermark": ""})).watermark.is_none());
}
+ /// Horizontal and vertical margins move a text watermark separately; settings saved with one
+ /// `inset` (before the two margins) read as both, and either new key overrides it.
+ #[test]
+ fn watermark_insets_are_horizontal_and_vertical() {
+ // gaps between the drawn text and the right and bottom edges of a 400 × 300 image
+ let gaps = |wm: &Watermark| {
+ let mut img = Rgba8::new(400, 300);
+ draw_watermark(&mut img, wm);
+ let lit: Vec<(usize, usize)> = (0..300).flat_map(|y| (0..400).map(move |x| (x, y))).filter(|&(x, y)| img.get(x, y)[0] > 128).collect();
+ let right = lit.iter().map(|p| p.0).max().unwrap();
+ let bottom = lit.iter().map(|p| p.1).max().unwrap();
+ (399 - right as i64, 299 - bottom as i64)
+ };
+ let base = Watermark { text: "LC".into(), size: 0.1, opacity: 1.0, shadow: false, inset_x: 0.0, inset_y: 0.0, ..Default::default() };
+ let (r0, b0) = gaps(&base);
+ // 0.2 and 0.1 of the 300 px short edge
+ let (r1, b1) = gaps(&Watermark { inset_x: 0.2, ..base.clone() });
+ assert!((r1 - r0 - 60).abs() <= 1 && (b1 - b0).abs() <= 1, "horizontal: {r0},{b0} → {r1},{b1}");
+ let (r2, b2) = gaps(&Watermark { inset_y: 0.1, ..base.clone() });
+ assert!((r2 - r0).abs() <= 1 && (b2 - b0 - 30).abs() <= 1, "vertical: {r0},{b0} → {r2},{b2}");
+ // the old single margin
+ let wm = |w: serde_json::Value| ExportOptions::from_json(&json!({"watermark": w})).watermark.unwrap();
+ let old = wm(json!({"text": "LC", "inset": 0.1}));
+ assert_eq!((old.inset_x, old.inset_y), (0.1, 0.1));
+ let mixed = wm(json!({"text": "LC", "inset": 0.1, "insetY": 0.3}));
+ assert_eq!((mixed.inset_x, mixed.inset_y), (0.1, 0.3));
+ assert!(ExportOptions::from_params(&json!({"watermark": {"text": "LC", "inset": 0.1}})).is_ok());
+ // saved and read back as the two margins
+ let o = ExportOptions { watermark: Some(Watermark { inset_x: 0.05, inset_y: 0.15, ..base }), ..Default::default() };
+ let saved = o.to_json();
+ assert!(saved["watermark"].get("inset").is_none(), "{saved}");
+ assert_eq!(ExportOptions::from_json(&saved).watermark.map(|w| (w.inset_x, w.inset_y)), Some((0.05, 0.15)));
+ }
+
+ /// A turned text watermark is placed by its turned box: taller than wide at 90° and 270°, the
+ /// same corner and insets, and an unknown angle draws it unturned.
+ #[test]
+ fn watermark_rotation_turns_the_text_in_its_corner() {
+ let lit = |wm: &Watermark| {
+ let mut img = Rgba8::new(400, 300);
+ draw_watermark(&mut img, wm);
+ let on: Vec<(usize, usize)> = (0..300).flat_map(|y| (0..400).map(move |x| (x, y))).filter(|&(x, y)| img.get(x, y)[0] > 128).collect();
+ let (x0, x1) = (on.iter().map(|p| p.0).min().unwrap(), on.iter().map(|p| p.0).max().unwrap());
+ let (y0, y1) = (on.iter().map(|p| p.1).min().unwrap(), on.iter().map(|p| p.1).max().unwrap());
+ (x0, x1, y0, y1, img)
+ };
+ let base = Watermark { text: "LightCraft".into(), size: 0.08, opacity: 1.0, shadow: false, inset_x: 0.0, inset_y: 0.0, ..Default::default() };
+ let (a0, a1, b0, b1, flat) = lit(&base);
+ assert!(a1 - a0 > 2 * (b1 - b0), "unturned text is wide");
+ for rotation in [90, 270] {
+ let (x0, x1, y0, y1, _) = lit(&Watermark { rotation, ..base.clone() });
+ assert!(y1 - y0 > 2 * (x1 - x0), "{rotation}°: tall");
+ assert!(x1 >= 390 && y1 >= 290, "{rotation}°: still in the bottom-right corner ({x1}, {y1})");
+ // 0.1 × 300 px further from the right edge
+ let (_, moved, ..) = lit(&Watermark { rotation, inset_x: 0.1, ..base.clone() });
+ assert!((x1 as i64 - moved as i64 - 30).abs() <= 1, "{rotation}°: {x1} → {moved}");
+ }
+ let (x0, x1, y0, y1, _) = lit(&Watermark { rotation: 180, ..base.clone() });
+ assert!(((x1 - x0) as i64 - (a1 - a0) as i64).abs() <= 2 && ((y1 - y0) as i64 - (b1 - b0) as i64).abs() <= 2, "180°: same size");
+ assert!(x1 >= 390 && y1 >= 290, "180°: same corner");
+ let (.., odd) = lit(&Watermark { rotation: 45, ..base.clone() });
+ assert_eq!(odd.data, flat.data, "an angle that isn't a quarter turn draws unturned");
+ // saved and read back; older settings have none
+ let o = ExportOptions { watermark: Some(Watermark { rotation: 270, ..base }), ..Default::default() };
+ assert_eq!(ExportOptions::from_json(&o.to_json()).watermark.map(|w| w.rotation), Some(270));
+ assert_eq!(ExportOptions::from_json(&json!({"watermark": {"text": "x"}})).watermark.map(|w| w.rotation), Some(0));
+ }
+
+ /// A preview keeps the export's 6 px minimum text size as a share of the photo: a watermark
+ /// clamped to 6 px in a 150 px export covers the same share of a 600 px preview.
+ #[test]
+ fn watermark_preview_scales_the_minimum_text_size() {
+ let wm = Watermark { text: "LightCraft".into(), size: 0.01, opacity: 1.0, shadow: false, ..Default::default() };
+ let rows = |img: &Rgba8| (0..img.height).filter(|&y| (0..img.width).any(|x| img.get(x, y)[0] > 64)).count() as f32;
+ let mut export = Rgba8::new(200, 150);
+ draw_watermark(&mut export, &wm);
+ let mut preview = Rgba8::new(800, 600);
+ draw_watermark_preview(&mut preview, &wm, 150);
+ let mut unscaled = Rgba8::new(800, 600);
+ draw_watermark(&mut unscaled, &wm);
+ let (export_share, preview_share) = (rows(&export) / 150.0, rows(&preview) / 600.0);
+ assert!((preview_share - export_share).abs() < 0.02, "export {export_share}, preview {preview_share}");
+ assert!(rows(&unscaled) < rows(&preview) / 2.0, "the unscaled 6 px floor would draw it too small");
+ // drawn at the export's own size, the preview is the export
+ let mut same = Rgba8::new(200, 150);
+ draw_watermark_preview(&mut same, &wm, 150);
+ assert_eq!(same.data, export.data);
+ }
+
fn vertical_japanese() -> Watermark {
let options =
ExportOptions::from_json(&json!({"watermark": {"text": "日本語", "vertical": true, "size": 0.1, "shadow": false, "opacity": 1.0}}));
diff --cc docs/parity-checklist.md
index 8a2e2ef72,569aca39d..cf1132e84
--- a/docs/parity-checklist.md
+++ b/docs/parity-checklist.md
@@@ -391,7 -391,7 +391,7 @@@ differences and missing bindings are i
| LR-EXP-HDR | HDR output | P2 | 🟡 | `cmd:app.export`, `crates/ui-egui/src/panels/dialogs.rs` | HDR output checkbox in the export dialog: gain map JPEG, PQ AVIF, float TIFF; no HDR presets yet |
| LR-EXP-SHARPEN | Output sharpening | P1 | ✅ | `cmd:app.export` (`sharpen`, `sharpenAmount`) | |
| LR-EXP-METADATA | Metadata policy | P1 | ✅ | `cmd:app.export` (`metadata`, `removeLocation`) | |
- | LR-EXP-WATERMARK | Watermark | P1 | ✅ | `cmd:app.export` (`watermark`: text, or `{text, vertical, size, opacity, anchor, insetX, insetY, inset, rotation, color, shadow, image, imageWidth}`), `crates/engine/src/export.rs` (`Watermark`, `WATERMARK_PARAMS`) | text (`size` = height as a fraction of the short edge, 0.005..0.5, default 0.035; `color` sRGB `[r,g,b]`; `shadow`; Japanese vertical glyph forms and origins, tested by `biz_ud_vertical_watermarks_place_punctuation_at_the_top_right`; extended grapheme cells keep decomposed dakuten/handakuten and Latin accents together, tested by `vertical_watermarks_keep_combining_marks_in_one_cell` and `upright_watermark_cell_retains_multiple_glyphs` (see `docs/vertical-watermarks.md`)) or a graphic with transparency (`image` path, `imageWidth` = fraction of the photo's width, 0.01..1, converted to the output colour space); `anchor` (nine positions), `insetX` / `insetY` (margins from the side and from the top/bottom edges, each a fraction of the short edge, 0..0.4; the Export dialog's Horizontal and Vertical inset sliders, as in Lightroom Classic; the older single `inset` still reads as both; tested by `watermark_insets_are_horizontal_and_vertical`), `rotation` (0, 90, 180 or 270 degrees clockwise, the Export dialog's Rotate row, as Lightroom Classic's rotate buttons; sized before it is turned, placed and inset by the turned box; tested by `watermark_rotation_turns_the_text_in_its_corner` and `graphic_watermark_is_placed_scaled_and_blended`), `opacity` 0..1. Unknown keys and out-of-range values are errors (issue #183) |
-| LR-EXP-WATERMARK | Watermark | P1 | ✅ | `cmd:app.export` (`watermark`: text, or `{text, vertical, size, opacity, anchor, inset, color, shadow, image, imageWidth}`), `crates/engine/src/export.rs` (`Watermark`, `WATERMARK_PARAMS`) | text (`size` = height as a fraction of the short edge, 0.005..0.5, default 0.035; `color` sRGB `[r,g,b]`; `shadow`; Japanese vertical glyph forms and origins, tested by `biz_ud_vertical_watermarks_place_punctuation_at_the_top_right`; extended grapheme cells keep decomposed dakuten/handakuten and Latin accents together, tested by `vertical_watermarks_keep_combining_marks_in_one_cell` and `upright_watermark_cell_retains_multiple_glyphs` (see `docs/vertical-watermarks.md`)) or a graphic with transparency (`image` path, `imageWidth` = fraction of the photo's width, 0.01..1, converted to the output colour space); `anchor`, `inset` (fraction of the short edge, 0..0.4), `opacity` 0..1. Unknown keys and out-of-range values are errors (issue #183). The Export dialog previews it on the first exported photo's thumbnail (`draw_watermark_preview` scales the 6 px minimum text size to the preview so it matches the file), tested by `watermark_preview_scales_the_minimum_text_size` and `export_dialog_previews_the_watermark` |
++| LR-EXP-WATERMARK | Watermark | P1 | ✅ | `cmd:app.export` (`watermark`: text, or `{text, vertical, size, opacity, anchor, insetX, insetY, inset, rotation, color, shadow, image, imageWidth}`), `crates/engine/src/export.rs` (`Watermark`, `WATERMARK_PARAMS`) | text (`size` = height as a fraction of the short edge, 0.005..0.5, default 0.035; `color` sRGB `[r,g,b]`; `shadow`; Japanese vertical glyph forms and origins, tested by `biz_ud_vertical_watermarks_place_punctuation_at_the_top_right`; extended grapheme cells keep decomposed dakuten/handakuten and Latin accents together, tested by `vertical_watermarks_keep_combining_marks_in_one_cell` and `upright_watermark_cell_retains_multiple_glyphs` (see `docs/vertical-watermarks.md`)) or a graphic with transparency (`image` path, `imageWidth` = fraction of the photo's width, 0.01..1, converted to the output colour space); `anchor` (nine positions), `insetX` / `insetY` (margins from the side and from the top/bottom edges, each a fraction of the short edge, 0..0.4; the Export dialog's Horizontal and Vertical inset sliders, as in Lightroom Classic; the older single `inset` still reads as both; tested by `watermark_insets_are_horizontal_and_vertical`), `rotation` (0, 90, 180 or 270 degrees clockwise, the Export dialog's Rotate row, as Lightroom Classic's rotate buttons; sized before it is turned, placed and inset by the turned box; tested by `watermark_rotation_turns_the_text_in_its_corner` and `graphic_watermark_is_placed_scaled_and_blended`), `opacity` 0..1. Unknown keys and out-of-range values are errors (issue #183). The Export dialog previews it on the first exported photo's thumbnail (`draw_watermark_preview` scales the 6 px minimum text size to the preview so it matches the file), tested by `watermark_preview_scales_the_minimum_text_size` and `export_dialog_previews_the_watermark` |
| LR-EXP-NAMING | File naming | P1 | ✅ | `cmd:app.export` (`naming`, `startNumber`), `crates/engine/src/rename.rs` (`expand_tokens`) | one template language for export, Rename Photos and import renaming: `{name}`, `{num}` (number at the end of the name), `{seq}` / `{seq:N}`, `{date}` / `{date:%Y-%m-%d}`, `{folder}`, `{camera}`, `{lens}`, `{iso}`, `{rating}`, `{title}`, `{creator}`, `{ext}`; free-form template rather than a list of named schemes |
| LR-EXP-LOCATION | Destination folder | P0 | ✅ | `cmd:app.export` (`dir`, `subfolder`, `conflict`: unique / overwrite / skip), `cmd:export.checkTarget`, `crates/ui-egui/src/panels/dialogs.rs` (Choose… folder picker), `crates/engine/src/originals.rs`, `crates/catalog/src/safe_file.rs` | folder picker on the desktop; existing files get `-2`, `-3`… by default; the policy covers a file and its sidecars together; a catalogued original (or its sidecar) is never written over, whatever the policy or an exact `path` (export, `ui.render`, MCP `render_photo`, merge `previewPath`, `lightcraft-cli render`); files are written to a temp file and renamed into place (issue #93; not synced file by file — exports can be made again, issue #134) |
| LR-EXP-PREVIOUS | Export with previous settings | P0 | ✅ | `cmd:app.exportPrevious`, `cmd:dialog.export` | last options persist in prefs.json; dialog prefilled; no named export presets yet |