Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Artboards: paint pasteboard pieces as quads so a rotated view keeps the holes (#5712)

Higgs32584c8f7cfaae6ff
diff --git a/crates/ui-egui/src/artboard_ui.rs b/crates/ui-egui/src/artboard_ui.rs
index da97fc7d4..63939fa9f 100644
--- a/crates/ui-egui/src/artboard_ui.rs
+++ b/crates/ui-egui/src/artboard_ui.rs
@@ -10,13 +10,15 @@ use crate::PhotocraftApp;
use crate::canvas::ViewXform;
use crate::theme::Tokens;
-/// Screen rects covering the document image minus every artboard (vertical slabs between the
+/// Screen quads covering the document image minus every artboard (vertical slabs between the
/// boards' x edges, split around the boards they cross), and minus where visible layers outside
/// the boards draw: Photoshop shows those over the pasteboard, so a board added next to earlier
/// work must not paint over it (#5712). The Background and any layer that covers the whole
/// canvas (or may draw anywhere, such as a fill layer) are backdrop, not loose work: they keep
-/// the pasteboard.
-pub fn pasteboard_rects(xf: &ViewXform, doc: &Document) -> Vec<Rect> {
+/// the pasteboard. Each piece is returned as its four screen corners rather than a screen
+/// bounding box, so in a rotated view the pieces keep their shape and do not cover the boards
+/// or the loose layers they were cut around.
+pub fn pasteboard_quads(xf: &ViewXform, doc: &Document) -> Vec<[Pos2; 4]> {
let canvas = doc.bounds();
let background = doc.background().map(|b| b.id);
let loose: Vec<photocraft_geom::Rect> = doc
@@ -41,17 +43,23 @@ pub fn pasteboard_rects(xf: &ViewXform, doc: &Document) -> Vec<Rect> {
let mut y = canvas.y0;
for (a, b) in spans {
if a > y {
- out.extend(subtract(photocraft_geom::Rect::new(x0, y, x1, a), &loose).into_iter().map(|r| xf.doc_rect(r)));
+ out.extend(subtract(photocraft_geom::Rect::new(x0, y, x1, a), &loose).into_iter().map(|r| quad(xf, r)));
}
y = y.max(b);
}
if y < canvas.y1 {
- out.extend(subtract(photocraft_geom::Rect::new(x0, y, x1, canvas.y1), &loose).into_iter().map(|r| xf.doc_rect(r)));
+ out.extend(subtract(photocraft_geom::Rect::new(x0, y, x1, canvas.y1), &loose).into_iter().map(|r| quad(xf, r)));
}
}
out
}
+/// The corners of a document rect on screen, in order (a parallelogram when the view is rotated).
+fn quad(xf: &ViewXform, r: photocraft_geom::Rect) -> [Pos2; 4] {
+ let (x0, y0, x1, y1) = (r.x0 as f32, r.y0 as f32, r.x1 as f32, r.y1 as f32);
+ [xf.to_screen(x0, y0), xf.to_screen(x1, y0), xf.to_screen(x1, y1), xf.to_screen(x0, y1)]
+}
+
fn is_artboard(l: &Layer) -> bool {
matches!(&l.content, photocraft_doc::LayerContent::Group(g) if g.artboard.is_some())
}
@@ -228,11 +236,11 @@ mod tests {
rotation: 0.0,
aspect: 1.0,
};
- let rects = pasteboard_rects(&xf, &doc);
- let area: f32 = rects.iter().map(|r| r.area()).sum();
+ let quads = pasteboard_quads(&xf, &doc);
+ let area: f32 = quads.iter().map(quad_area).sum();
// 100×50 canvas − 40×50 − 40×30 boards.
- assert!((area - (5000.0 - 2000.0 - 1200.0)).abs() < 0.5, "{rects:?}");
- assert!(rects.iter().all(|r| !r.intersects(Rect::from_min_max(Pos2::new(0.5, 0.5), Pos2::new(39.5, 49.5)))));
+ assert!((area - (5000.0 - 2000.0 - 1200.0)).abs() < 0.5, "{quads:?}");
+ assert!(quads.iter().all(|q| !Rect::from_points(q).intersects(Rect::from_min_max(Pos2::new(0.5, 0.5), Pos2::new(39.5, 49.5)))));
}
#[test]
@@ -252,11 +260,11 @@ mod tests {
rotation: 0.0,
aspect: 1.0,
};
- let rects = pasteboard_rects(&xf, &doc);
- let area: f32 = rects.iter().map(|r| r.area()).sum();
+ let quads = pasteboard_quads(&xf, &doc);
+ let area: f32 = quads.iter().map(quad_area).sum();
// 100×50 canvas − the 40×50 board − the 20×20 loose pixels.
- assert!((area - (5000.0 - 2000.0 - 400.0)).abs() < 0.5, "{rects:?}");
- assert!(rects.iter().all(|r| !r.intersects(Rect::from_min_max(Pos2::new(60.5, 10.5), Pos2::new(79.5, 29.5)))));
+ assert!((area - (5000.0 - 2000.0 - 400.0)).abs() < 0.5, "{quads:?}");
+ assert!(quads.iter().all(|q| !Rect::from_points(q).intersects(Rect::from_min_max(Pos2::new(60.5, 10.5), Pos2::new(79.5, 29.5)))));
}
#[test]
@@ -278,10 +286,51 @@ mod tests {
rotation: 0.0,
aspect: 1.0,
};
- let area: f32 = pasteboard_rects(&xf, &doc).iter().map(|r| r.area()).sum();
+ let area: f32 = pasteboard_quads(&xf, &doc).iter().map(quad_area).sum();
assert!((area - (5000.0 - 2000.0)).abs() < 0.5);
}
+ fn quad_area(q: &[Pos2; 4]) -> f32 {
+ (0..4).map(|i| q[i].x * q[(i + 1) % 4].y - q[(i + 1) % 4].x * q[i].y).sum::<f32>().abs() / 2.0
+ }
+
+ /// Whether `p` lies strictly inside the convex quad `q` (either winding).
+ fn inside(q: &[Pos2; 4], p: Pos2) -> bool {
+ let side = |i: usize| (q[(i + 1) % 4] - q[i]).x * (p - q[i]).y - (q[(i + 1) % 4] - q[i]).y * (p - q[i]).x;
+ let s: Vec<f32> = (0..4).map(side).collect();
+ s.iter().all(|&v| v > 0.0) || s.iter().all(|&v| v < 0.0)
+ }
+
+ #[test]
+ fn rotated_view_keeps_the_holes_around_loose_layers() {
+ // A loose layer in the middle of the pasteboard, seen at 45°: the strips around it must
+ // not cover it (their screen bounding boxes would).
+ let (mut app, _) = app();
+ app.run("layer.new.artboard", json!({"rect": [0, 0, 20, 50]})).unwrap();
+ let mut doc = (*app.session.active().unwrap().doc).clone();
+ let mut px = photocraft_doc::Layer::raster("Work", doc.pixel_format());
+ px.surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(50, 15, 70, 35), &[1.0, 0.0, 0.0, 1.0]);
+ doc.layers.insert(0, px);
+ let xf = ViewXform {
+ rect: Rect::from_min_size(Pos2::ZERO, egui::vec2(200.0, 200.0)),
+ zoom: 1.0,
+ center: [50.0, 25.0],
+ flip: true,
+ rotation: 45.0,
+ aspect: 1.0,
+ };
+ let quads = pasteboard_quads(&xf, &doc);
+ let area: f32 = quads.iter().map(quad_area).sum();
+ // Rotation keeps areas: 100×50 canvas − the 20×50 board − the 20×20 loose pixels.
+ assert!((area - (5000.0 - 1000.0 - 400.0)).abs() < 0.5, "{quads:?}");
+ for (x, y) in [(60.0, 25.0), (51.0, 16.0), (69.0, 34.0), (10.0, 25.0)] {
+ let p = xf.to_screen(x, y);
+ assert!(quads.iter().all(|q| !inside(q, p)), "({x}, {y}) is covered");
+ }
+ // Pasteboard still shows next to the hole.
+ assert!(quads.iter().any(|q| inside(q, xf.to_screen(80.0, 25.0))));
+ }
+
#[test]
fn subtract_splits_around_holes() {
let r = photocraft_geom::Rect::new(0, 0, 10, 10);
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 91644b5f7..538fb08ac 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3174,21 +3174,29 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
let t = crate::theme::Tokens::get(&ctx);
let pasteboard = crate::prefs_ui::pasteboard_color(app);
let dot_tex = dots(&ctx, &t);
- for r in crate::artboard_ui::pasteboard_rects(&xf, &doc) {
- let r = r.intersect(rect);
- if !r.is_positive() {
+ // Each piece is painted as its own quad (the painter clips to the canvas), so a
+ // rotated view does not paint a piece's bounding box over the boards or loose work.
+ let fill = |q: &[Pos2; 4], tex: Option<egui::TextureId>, color: Color32| {
+ let mut mesh = tex.map_or_else(egui::Mesh::default, egui::Mesh::with_texture);
+ for &p in q {
+ // Same phase as the dots around the document.
+ let uv = if tex.is_some() { ((p - rect.min) / 22.0).to_pos2() } else { egui::epaint::WHITE_UV };
+ mesh.vertices.push(egui::epaint::Vertex { pos: p, uv, color });
+ }
+ mesh.add_triangle(0, 1, 2);
+ mesh.add_triangle(0, 2, 3);
+ painter.add(mesh);
+ };
+ for q in crate::artboard_ui::pasteboard_quads(&xf, &doc) {
+ if !Rect::from_points(&q).intersects(rect) {
continue;
}
match pasteboard {
- Some(c) => {
- painter.rect_filled(r, 0.0, c);
- }
+ Some(c) => fill(&q, None, c),
None => {
- painter.rect_filled(r, 0.0, t.canvas);
+ fill(&q, None, t.canvas);
if let Some(id) = dot_tex {
- // Same phase as the dots around the document.
- let uv = Rect::from_min_max(((r.min - rect.min) / 22.0).to_pos2(), ((r.max - rect.min) / 22.0).to_pos2());
- painter.image(id, r, uv, Color32::WHITE);
+ fill(&q, Some(id), Color32::WHITE);
}
}
}