From 740dd3019f97cf8bae05ff37c0802a112377d583 Mon Sep 17 00:00:00 2001 From: Valentin Klein Date: Sun, 11 Oct 2026 19:27:41 +0200 Subject: [PATCH] Rulers: stay level and fully ticked on a rotated view; guides turn with the image (#3233) (#3282) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Rulers: stay level and fully ticked on a rotated view; guides turn with the image (#3233, #3277) The rulers measured along the rotated document axes, so at most angles the ticks were squeezed into a few pixels (none at 90°) and the rest of the ruler was empty. They now measure with the view minus its rotation: same zoom, flip and pixel aspect, same document point under the canvas centre. Guides were drawn as screen-level lines through the screen position of (pos, 0), so on a rotated view they no longer marked their document coordinate while hit-testing still used it. They are now drawn along the document line across the clip rect. Co-Authored-By: Claude Opus 5.5 * Rulers: anchor a rotated view's rulers at the document origin (#3233) Photoshop 2026 keeps the rulers level at zoom scale on a rotated view, with 0 at the screen position of the document origin (where the rotated image's top-left corner lands), not at the unrotated frame through the canvas centre. ruler_xform now re-centres the level mapping so the origin projects where the rotated view draws it; tick spacing still follows zoom, flip and pixel aspect. Co-Authored-By: Claude Opus 5.5 * Rulers: label distances before the origin without a minus sign (#3233) Photoshop's rulers print the distance from the origin, so ticks left of / above it read "1000 500 0 500 1000", not "-1000 -500 0". The label is now the absolute value (extracted into `ruler_label`), for every view. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/rulers.rs | 137 ++++++++++++++++++++++++++++++----- 1 file changed, 120 insertions(+), 17 deletions(-) diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs index f90b61b0c1e..5f16c1a86b6 100644 --- a/crates/ui-egui/src/rulers.rs +++ b/crates/ui-egui/src/rulers.rs @@ -143,6 +143,22 @@ pub fn draw_grid(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, d } } +/// Screen end points of a guide across `clip`: the document line x = `pos` (vertical) or +/// y = `pos`, spanning every document point the clip rect shows. Guides belong to the document, +/// so on a rotated view they turn with the image (#3277); unrotated this is the clip's full +/// height (or width). +fn guide_segment(xf: &ViewXform, clip: Rect, vertical: bool, pos: f64) -> [Pos2; 2] { + let axis = usize::from(vertical); + let (mut lo, mut hi) = (f64::INFINITY, f64::NEG_INFINITY); + for c in [clip.left_top(), clip.right_top(), clip.right_bottom(), clip.left_bottom()] { + let d = xf.to_doc(c)[axis]; + lo = lo.min(d); + hi = hi.max(d); + } + let (pos, lo, hi) = (pos as f32, lo as f32, hi as f32); + if vertical { [xf.to_screen(pos, lo), xf.to_screen(pos, hi)] } else { [xf.to_screen(lo, pos), xf.to_screen(hi, pos)] } +} + pub(crate) fn parse_guide_color(value: &str) -> Option { let value = value.strip_prefix('#')?; if value.len() != 6 { @@ -163,13 +179,8 @@ pub fn draw_guides(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, let (guide, style) = (preview.as_ref().map(|(_, color)| *color).unwrap_or_else(|| pref_color(&g.guide_color, Color32::from_rgb(74, 255, 255))), g.guide_style); let line = |vertical: bool, pos: f64, color: Color32| { - if vertical { - let x = xf.to_screen(pos as f32, 0.0).x; - styled_line(painter, pos2(x, clip.top()), pos2(x, clip.bottom()), Stroke::new(1.0, color), style); - } else { - let y = xf.to_screen(0.0, pos as f32).y; - styled_line(painter, pos2(clip.left(), y), pos2(clip.right(), y), Stroke::new(1.0, color), style); - } + let [a, b] = guide_segment(xf, clip, vertical, pos); + styled_line(painter, a, b, Stroke::new(1.0, color), style); }; if app.ui.extras.guides || preview.is_some() { for (vertical, list) in [(true, &guides.vertical), (false, &guides.horizontal)] { @@ -226,15 +237,50 @@ pub fn finish_drag(app: &mut PhotocraftApp, d: GuideDrag) { } } +/// The mapping the rulers measure with. Rulers are screen chrome that stays level while +/// View › Rotate View turns the canvas, so they use the view without its rotation: the same zoom, +/// flip and pixel aspect (ticks never stretch), anchored so that 0 sits at the screen position of +/// the document origin as the rotated view draws it, as in Photoshop. Tilted guides then meet the +/// rulers near their numbers. Measuring along the rotated axes instead squeezed the ticks into a +/// few pixels (or none at 90°) and left the rest of the ruler empty (#3233). +fn ruler_xform(xf: &ViewXform) -> ViewXform { + let level = ViewXform { rotation: 0.0, ..*xf }; + // The level view is an axis-aligned scale, so moving the origin by `shift` on screen moves + // every point by it: re-centre on the document point that sat `shift` before the centre. + let shift = xf.to_screen(0.0, 0.0) - level.to_screen(0.0, 0.0); + let center = level.to_doc(xf.rect.center() - shift); + if !(center[0].is_finite() && center[1].is_finite()) { + return level; + } + ViewXform { center: [center[0] as f32, center[1] as f32], ..level } +} + /// Visible ruler range in ruler units, normalised to `(min, max)` so the tick loop runs for -/// flipped or rotated views too (#1814). Shared by the renderer and its tests. +/// flipped views too (#1814). `xf` is the canvas mapping; rotation is ignored (see +/// [`ruler_xform`]). Shared by the renderer and its tests. fn ruler_range(xf: &ViewXform, a: Pos2, b: Pos2, axis: usize, px_per_unit: f64) -> (f64, f64) { - let d0 = xf.to_doc(a)[axis]; - let d1 = xf.to_doc(b)[axis]; + let rxf = ruler_xform(xf); + let d0 = rxf.to_doc(a)[axis]; + let d1 = rxf.to_doc(b)[axis]; let (u0, u1) = (d0 / px_per_unit, d1 / px_per_unit); (u0.min(u1), u0.max(u1)) } +/// Screen coordinate along the ruler (x for the top ruler, y for the left one) of the value `u` +/// in ruler units. The inverse of [`ruler_range`]'s mapping. +fn ruler_pos(xf: &ViewXform, vertical: bool, u: f64, px_per_unit: f64) -> f32 { + let px = (u * px_per_unit) as f32; + let rxf = ruler_xform(xf); + if vertical { rxf.to_screen(0.0, px).y } else { rxf.to_screen(px, 0.0).x } +} + +/// The number a ruler prints at the tick `v` (in ruler units). Like Photoshop, rulers show the +/// distance from the origin, so values left of / above it read without a minus sign. +fn ruler_label(v: f64, step: f64, whole: bool) -> String { + let v = v.abs(); + if step >= 1.0 || whole { format!("{}", v.round() as i64) } else { crate::widgets::fmt_num2(v) } +} + /// Rulers along the top and left of `full` (the canvas rect before `content_rect`), with the /// pointer position marked; dragging out of a ruler creates a guide. pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &ViewXform) { @@ -257,7 +303,6 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: & let (dpi, size) = app.session.active().map_or((72.0, [1.0, 1.0]), |d| (d.doc.resolution_dpi.max(1.0) as f64, [d.doc.size.width as f64, d.doc.size.height as f64])); let whole = unit == photocraft_engine::prefs::Unit::Pixels; - let label = |v: f64, step: f64| -> String { if step >= 1.0 || whole { format!("{}", v.round() as i64) } else { crate::widgets::fmt_num2(v) } }; for (vertical, extent) in [(false, size[0]), (true, size[1])] { let p = p.with_clip_rect(if vertical { left } else { top }); let px_per_unit = unit.to_px(1.0, dpi, extent, ppi).max(1e-9); @@ -271,20 +316,17 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: & }; let mut v = (u0 / step).floor() * step; while v <= u1 { - let at = |u: f64| -> f32 { - let px = (u * px_per_unit) as f32; - if vertical { xf.to_screen(0.0, px).y } else { xf.to_screen(px, 0.0).x } - }; + let at = |u: f64| -> f32 { ruler_pos(xf, vertical, u, px_per_unit) }; let c = at(v); if vertical { p.line_segment([pos2(left.right() - RULER, c), pos2(left.right(), c)], tick); // Labels stacked, as in Photoshop. - for (i, ch) in label(v, step).chars().enumerate() { + for (i, ch) in ruler_label(v, step, whole).chars().enumerate() { p.text(pos2(left.left() + 3.0, c + 2.0 + i as f32 * 8.5), Align2::LEFT_TOP, ch, font.clone(), t.text_dim); } } else { p.line_segment([pos2(c, top.bottom() - RULER), pos2(c, top.bottom())], tick); - p.text(pos2(c + 2.0, top.top() + 1.0), Align2::LEFT_TOP, label(v, step), font.clone(), t.text_dim); + p.text(pos2(c + 2.0, top.top() + 1.0), Align2::LEFT_TOP, ruler_label(v, step, whole), font.clone(), t.text_dim); } for k in 1..10 { let m = at(v + step * k as f64 / 10.0); @@ -550,6 +592,67 @@ mod tests { } } + /// #3233: a rotated view keeps the rulers level and fully ticked at zoom scale. The top ruler + /// spans the same number of document pixels as the unrotated view, the ticks map back onto + /// its ends, and both rulers read 0 at the screen position of the (rotated) document origin, + /// as Photoshop does. + #[test] + fn rotated_view_keeps_full_rulers() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let (a, b) = (rect.left_top(), rect.right_top()); + for rotation in [0.0f32, 30.0, 45.0, 90.0, -119.0, 180.0] { + for flip in [false, true] { + let xf = ViewXform { rect, zoom: 0.5, center: [1000.0, 700.0], flip, rotation, aspect: 1.0 }; + let (u0, u1) = ruler_range(&xf, a, b, 0, 1.0); + assert!((u1 - u0 - 1600.0).abs() < 1e-2, "rotation {rotation}: top ruler covers {u0}..{u1}"); + let (first, last) = if flip { (u1, u0) } else { (u0, u1) }; + assert!((ruler_pos(&xf, false, first, 1.0) - a.x).abs() < 1e-2, "rotation {rotation}: first tick"); + assert!((ruler_pos(&xf, false, last, 1.0) - b.x).abs() < 1e-2, "rotation {rotation}: last tick"); + // 0 at the projected origin, 500 document px = 500 × zoom screen px further on. + let origin = xf.to_screen(0.0, 0.0); + assert!((ruler_pos(&xf, false, 0.0, 1.0) - origin.x).abs() < 1e-2, "rotation {rotation}: top ruler 0"); + assert!((ruler_pos(&xf, true, 0.0, 1.0) - origin.y).abs() < 1e-2, "rotation {rotation}: left ruler 0"); + let dir = if flip { -1.0 } else { 1.0 }; + assert!((ruler_pos(&xf, false, 500.0, 1.0) - origin.x - dir * 250.0).abs() < 1e-2, "rotation {rotation}: top scale"); + assert!((ruler_pos(&xf, true, 500.0, 1.0) - origin.y - 250.0).abs() < 1e-2, "rotation {rotation}: left scale"); + } + } + // Distances left of / above the origin read without a minus sign, as in Photoshop. + let xf = ViewXform { rect, zoom: 0.5, center: [0.0, 0.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let (u0, u1) = ruler_range(&xf, a, b, 0, 1.0); + assert!(u0 < -500.0 && u1 > 500.0, "the view spans the origin: {u0}..{u1}"); + for (step, whole) in [(100.0, true), (100.0, false), (0.25, false)] { + let mut v = (u0 / step).floor() * step; + while v <= u1 { + let s = ruler_label(v, step, whole); + assert!(!s.contains('-'), "tick {v} (step {step}) is labelled {s:?}"); + v += step; + } + } + assert_eq!(ruler_label(-500.0, 100.0, true), "500"); + assert_eq!(ruler_label(-0.25, 0.25, false), "0.25"); + } + + /// #3277: guides belong to the document, so a rotated view draws them along the document + /// line; unrotated they still span the whole clip rect. + #[test] + fn guides_turn_with_a_rotated_view() { + let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0)); + let level = ViewXform { rect, zoom: 0.5, center: [1000.0, 700.0], flip: false, rotation: 0.0, aspect: 1.0 }; + let [a, b] = guide_segment(&level, rect, true, 500.0); + assert!((a.x - b.x).abs() < 1e-3 && (a.y - rect.top()).abs() < 1e-3 && (b.y - rect.bottom()).abs() < 1e-3, "{a:?} {b:?}"); + for rotation in [30.0f32, 90.0, -119.0] { + let xf = ViewXform { rotation, ..level }; + for (vertical, pos) in [(true, 500.0), (false, 900.0)] { + for end in guide_segment(&xf, rect, vertical, pos) { + let d = xf.to_doc(end); + let on_line = if vertical { d[0] } else { d[1] }; + assert!((on_line - pos).abs() < 1e-2, "rotation {rotation}: {end:?} maps to {d:?}, off the guide at {pos}"); + } + } + } + } + #[test] fn ruler_range_is_normalised_when_flipped() { // #1814: with View > Flip Horizontal, `to_doc` negates x, so the raw