mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
View > Pixel Aspect Ratio and its Correction toggle only stored a value: the canvas transform (ViewXform) had no notion of pixel aspect, so the image was always drawn with square pixels. ViewXform now carries the displayed pixel aspect ratio and stretches document x by it before rotation and flip. Drawing, pointer-to-document mapping, pan and zoom-about deltas, rulers, grid, Fit/Fill on Screen, scrollbars, the Navigator's view box and paste-in-place visibility all go through it. Correction is on by default, as in Photoshop. A corrected view draws through the CPU canvas path, like a flipped one, since the GPU shader assumes square pixels. The document's pixels are unchanged. Co-authored-by: Fabio Lussi <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Brandon Thomas <[email protected]>
This commit is contained in:
33 files changed
+321
-73
No files matched your search
@@ -75,7 +75,7 @@ fn screen(h: &H, x: f64, y: f64) -> Pos2 {
|
||||
let idx = app.session.active_index().unwrap_or(0);
|
||||
let v = &app.ui.views[idx];
|
||||
let rect = photocraft_ui_egui::rulers::content_rect(app, app.last_canvas_rect);
|
||||
ViewXform { rect, zoom: v.zoom, center: v.center, flip: app.ui.view.flip_horizontal, rotation: v.rotation }.to_screen(x as f32, y as f32)
|
||||
ViewXform { rect, zoom: v.zoom, center: v.center, flip: app.ui.view.flip_horizontal, rotation: v.rotation, aspect: 1.0 }.to_screen(x as f32, y as f32)
|
||||
}
|
||||
|
||||
fn stats(name: &str, mut v: Vec<f64>) {
|
||||
|
||||
@@ -86,7 +86,7 @@ fn screen(h: &H, x: f64, y: f64) -> Pos2 {
|
||||
let idx = app.session.active_index().unwrap_or(0);
|
||||
let v = &app.ui.views[idx];
|
||||
let rect = photocraft_ui_egui::rulers::content_rect(app, app.last_canvas_rect);
|
||||
ViewXform { rect, zoom: v.zoom, center: v.center, flip: app.ui.view.flip_horizontal, rotation: v.rotation }.to_screen(x as f32, y as f32)
|
||||
ViewXform { rect, zoom: v.zoom, center: v.center, flip: app.ui.view.flip_horizontal, rotation: v.rotation, aspect: 1.0 }.to_screen(x as f32, y as f32)
|
||||
}
|
||||
|
||||
fn select(h: &mut H, id: LayerId) {
|
||||
|
||||
@@ -731,7 +731,7 @@ mod tests {
|
||||
let idx = app.session.active_index().unwrap();
|
||||
assert!(app.ui.views[idx].fit_pending);
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
crate::canvas::fit_view(&mut app.ui.views[idx], doc, vec2(100.0, 100.0), 1.0);
|
||||
crate::canvas::fit_view(&mut app.ui.views[idx], doc, vec2(100.0, 100.0), 1.0, 1.0);
|
||||
assert_eq!(app.ui.views[idx].zoom, 0.75);
|
||||
assert_eq!(app.ui.views[idx].center, [40.0, 30.0]);
|
||||
assert!(!app.ui.views[idx].fit_pending);
|
||||
|
||||
@@ -40,6 +40,7 @@ fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(x, y)
|
||||
}
|
||||
|
||||
@@ -180,7 +180,14 @@ mod tests {
|
||||
app.run("layer.new.artboard", json!({"rect": [0, 0, 40, 50]})).unwrap();
|
||||
app.run("layer.new.artboard", json!({"rect": [60, 10, 40, 30]})).unwrap();
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, egui::vec2(100.0, 50.0)), zoom: 1.0, center: [50.0, 25.0], flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform {
|
||||
rect: Rect::from_min_size(Pos2::ZERO, egui::vec2(100.0, 50.0)),
|
||||
zoom: 1.0,
|
||||
center: [50.0, 25.0],
|
||||
flip: false,
|
||||
rotation: 0.0,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let rects = pasteboard_rects(&xf, &doc);
|
||||
let area: f32 = rects.iter().map(|r| r.area()).sum();
|
||||
// 100×50 canvas − 40×50 − 40×30 boards.
|
||||
|
||||
@@ -688,6 +688,10 @@ pub struct ViewXform {
|
||||
/// Camera rotation in degrees around [`Self::center`]. Clockwise (Y-down) is positive; applied
|
||||
/// before flip.
|
||||
pub rotation: f32,
|
||||
/// View › Pixel Aspect Ratio Correction: a document pixel is drawn this many times wider than
|
||||
/// tall (1 = square). Applied in document space, before rotation, so a rotated view still
|
||||
/// shows the stretched image turned as a whole.
|
||||
pub aspect: f32,
|
||||
}
|
||||
|
||||
impl ViewXform {
|
||||
@@ -700,6 +704,7 @@ impl ViewXform {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -712,16 +717,27 @@ impl ViewXform {
|
||||
}
|
||||
}
|
||||
|
||||
/// Unflip then unrotate a screen-space vector (no zoom). Hand pan and zoom-about use this.
|
||||
/// Document pixel width over height on screen: [`Self::aspect`], or 1 when it is unusable.
|
||||
fn sx(self) -> f32 {
|
||||
if self.aspect.is_finite() && self.aspect > 0.0 { self.aspect } else { 1.0 }
|
||||
}
|
||||
|
||||
/// Screen points per document pixel along the document's x axis.
|
||||
pub fn zoom_x(&self) -> f32 {
|
||||
self.zoom * self.sx()
|
||||
}
|
||||
|
||||
/// Unflip, unrotate and unstretch a screen-space vector (no zoom). Hand pan and zoom-about
|
||||
/// use this.
|
||||
pub fn unmap_vec(self, d: Vec2) -> Vec2 {
|
||||
let d = if self.flip { vec2(-d.x, d.y) } else { d };
|
||||
let (c, s) = self.sincos();
|
||||
vec2(d.x * c + d.y * s, -d.x * s + d.y * c)
|
||||
vec2((d.x * c + d.y * s) / self.sx(), -d.x * s + d.y * c)
|
||||
}
|
||||
|
||||
pub fn to_screen(&self, x: f32, y: f32) -> Pos2 {
|
||||
let (c, s) = self.sincos();
|
||||
let dx = x - self.center[0];
|
||||
let dx = (x - self.center[0]) * self.sx();
|
||||
let dy = y - self.center[1];
|
||||
let rx = dx * c - dy * s;
|
||||
let ry = dx * s + dy * c;
|
||||
@@ -760,10 +776,12 @@ pub fn doc_point_at(app: &PhotocraftApp, pos: Pos2) -> Option<[f64; 2]> {
|
||||
}
|
||||
|
||||
/// Fit the document into `area` (egui points) for a display with `ppp` physical pixels per
|
||||
/// point; `View::zoom` is stored in physical pixels per document pixel.
|
||||
pub fn fit_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32) {
|
||||
/// point; `View::zoom` is stored in physical pixels per document pixel. `aspect` is the
|
||||
/// displayed pixel aspect ratio ([`ViewXform::aspect`]): the image is that much wider on screen.
|
||||
pub fn fit_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32, aspect: f32) {
|
||||
let (w, h) = (doc.size.width as f32, doc.size.height as f32);
|
||||
let points = ((area.x - 40.0) / w).min((area.y - 40.0) / h).min(1.0);
|
||||
let shown_w = w * if aspect.is_finite() && aspect > 0.0 { aspect } else { 1.0 };
|
||||
let points = ((area.x - 40.0) / shown_w).min((area.y - 40.0) / h).min(1.0);
|
||||
let zoom = crate::zoom_levels::clamp(points * ppp, [doc.size.width, doc.size.height]);
|
||||
view.zoom = zoom;
|
||||
view.center = [w / 2.0, h / 2.0];
|
||||
@@ -773,9 +791,10 @@ pub fn fit_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32) {
|
||||
|
||||
/// Centre the document and zoom until it fills the canvas. One axis can extend beyond the
|
||||
/// viewport, matching the Hand tool's Fill Screen action.
|
||||
pub fn fill_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32) {
|
||||
pub fn fill_view(view: &mut View, doc: &Document, area: Vec2, ppp: f32, aspect: f32) {
|
||||
let (w, h) = (doc.size.width as f32, doc.size.height as f32);
|
||||
let points = (area.x / w).max(area.y / h);
|
||||
let shown_w = w * if aspect.is_finite() && aspect > 0.0 { aspect } else { 1.0 };
|
||||
let points = (area.x / shown_w).max(area.y / h);
|
||||
let zoom = crate::zoom_levels::clamp(points * ppp, [doc.size.width, doc.size.height]);
|
||||
view.zoom = zoom;
|
||||
view.center = [w / 2.0, h / 2.0];
|
||||
@@ -2333,20 +2352,22 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
|
||||
}
|
||||
view.doc_size = size;
|
||||
}
|
||||
// View › Pixel Aspect Ratio Correction: document pixels are shown this much wider.
|
||||
let aspect = app.ui.view.display_aspect();
|
||||
if view.fit_pending && rect.width() > 50.0 {
|
||||
fit_view(&mut view, &doc, rect.size(), ppp);
|
||||
fit_view(&mut view, &doc, rect.size(), ppp, aspect);
|
||||
}
|
||||
if view.fill_pending && rect.width() > 50.0 {
|
||||
fill_view(&mut view, &doc, rect.size(), ppp);
|
||||
fill_view(&mut view, &doc, rect.size(), ppp, aspect);
|
||||
}
|
||||
// Preferences › Tools › Overscroll off: clamp before anything is drawn (scrollbars.rs).
|
||||
if !app.session.prefs().tools.overscroll && crate::scrollbars::clamp_view(&mut view, rect.size(), ppp) {
|
||||
if !app.session.prefs().tools.overscroll && crate::scrollbars::clamp_view(&mut view, rect.size() / vec2(aspect, 1.0), ppp) {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
let flip = app.ui.view.flip_horizontal;
|
||||
// Canvas geometry (ViewXform, ViewParams, painter rects) is in egui points.
|
||||
let point_zoom = view.zoom / ppp;
|
||||
let xf = ViewXform { rect, zoom: point_zoom, center: view.center, flip, rotation: view.rotation };
|
||||
let xf = ViewXform { rect, zoom: point_zoom, center: view.center, flip, rotation: view.rotation, aspect };
|
||||
let pixel_grid = app.ui.view.shows(app.ui.view.show.pixel_grid);
|
||||
let response = ui.allocate_rect(rect, Sense::click_and_drag());
|
||||
let painter = ui.painter_at(rect);
|
||||
@@ -2368,9 +2389,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
|
||||
// Live adjustment previews on big documents use a downsampled proxy (see proxy.rs), unless
|
||||
// Preferences › Performance › Low Resolution Previews is off.
|
||||
let mut on_gpu = false;
|
||||
// A flipped view draws through the CPU path (the GPU canvas shader has no mirroring).
|
||||
// Rotation stays on the GPU: it is a view uniform, not a CPU blit.
|
||||
let gpu_ok = crate::gpu_canvas::gpu_view_ok(flip, view.rotation);
|
||||
// A flipped or pixel-aspect-corrected view draws through the CPU path (the GPU canvas shader
|
||||
// has no mirroring or non-square pixels). Rotation stays on the GPU: it is a view uniform.
|
||||
let gpu_ok = crate::gpu_canvas::gpu_view_ok(flip, view.rotation, aspect);
|
||||
if app.gpu.is_some()
|
||||
&& gpu_ok
|
||||
&& let Some((k, key, preview_size)) =
|
||||
@@ -3189,7 +3210,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
|
||||
let t0 = crate::gpu_canvas::now_ms();
|
||||
let before = view.center;
|
||||
let over_bars = app.ui.view.shows_scrollbars()
|
||||
&& crate::scrollbars::show(ui, rect, &mut view, flip, app.session.prefs().tools.overscroll, egui::Id::new(("pc-canvas", idx)));
|
||||
&& crate::scrollbars::show(ui, rect, &mut view, flip, aspect, app.session.prefs().tools.overscroll, egui::Id::new(("pc-canvas", idx)));
|
||||
ctx.data_mut(|d| d.insert_temp(bars_id, over_bars));
|
||||
if view.center != before {
|
||||
ctx.request_repaint();
|
||||
@@ -3761,8 +3782,9 @@ fn draw_drag_preview(app: &mut PhotocraftApp, painter: &egui::Painter, xf: &View
|
||||
}
|
||||
// The layers themselves follow the pointer (`move_ui`); the arrow only when they can't.
|
||||
Tool::Move if !crate::move_ui::showing(app) => {
|
||||
let off = vec2(((last[0] - d.start[0]) as f32) * xf.zoom, ((last[1] - d.start[1]) as f32) * xf.zoom);
|
||||
painter.arrow(xf.to_screen(d.start[0] as f32, d.start[1] as f32), off, Stroke::new(2.0, crate::theme::Tokens::get(painter.ctx()).accent));
|
||||
let from = xf.to_screen(d.start[0] as f32, d.start[1] as f32);
|
||||
let off = xf.to_screen(last[0] as f32, last[1] as f32) - from;
|
||||
painter.arrow(from, off, Stroke::new(2.0, crate::theme::Tokens::get(painter.ctx()).accent));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -5127,7 +5149,8 @@ mod tests {
|
||||
app.run("cloneSource.set", json!({"source": [16,16]})).unwrap();
|
||||
app.ui.tool = Tool::CloneStamp;
|
||||
app.session.tools.brush.size = 20.0;
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(100.0, 100.0)), zoom: 1.0, center: [32.0, 32.0], flip: false, rotation: 0.0 };
|
||||
let xf =
|
||||
ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(100.0, 100.0)), zoom: 1.0, center: [32.0, 32.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
let revision = app.session.active().unwrap().revision;
|
||||
let mut output = ctx.run_ui(Default::default(), |ui| {
|
||||
let ctx = ui.ctx();
|
||||
@@ -5280,7 +5303,14 @@ mod tests {
|
||||
#[test]
|
||||
fn view_transform_roundtrip() {
|
||||
for flip in [false, true] {
|
||||
let xf = ViewXform { rect: Rect::from_min_size(pos2(100.0, 50.0), vec2(800.0, 600.0)), zoom: 2.5, center: [320.0, 240.0], flip, rotation: 0.0 };
|
||||
let xf = ViewXform {
|
||||
rect: Rect::from_min_size(pos2(100.0, 50.0), vec2(800.0, 600.0)),
|
||||
zoom: 2.5,
|
||||
center: [320.0, 240.0],
|
||||
flip,
|
||||
rotation: 0.0,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let s = xf.to_screen(10.0, 20.0);
|
||||
let d = xf.to_doc(s);
|
||||
assert!((d[0] - 10.0).abs() < 1e-3 && (d[1] - 20.0).abs() < 1e-3);
|
||||
@@ -5674,7 +5704,7 @@ mod tests {
|
||||
let ctx = ui.ctx();
|
||||
let rect = Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0));
|
||||
let painter = ctx.layer_painter(egui::LayerId::background()).with_clip_rect(rect);
|
||||
let xf = ViewXform { rect, zoom, center: [60.0, 30.0], flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform { rect, zoom, center: [60.0, 30.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
draw_drag_preview(app, &painter, &xf);
|
||||
});
|
||||
let egui::FullOutput { mut textures_delta, shapes, .. } = out;
|
||||
@@ -5931,7 +5961,7 @@ mod tests {
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
let mut view = View { fill_pending: true, ..View::default() };
|
||||
|
||||
fill_view(&mut view, &doc, vec2(600.0, 600.0), 1.0);
|
||||
fill_view(&mut view, &doc, vec2(600.0, 600.0), 1.0, 1.0);
|
||||
|
||||
assert_eq!(view.zoom, 3.0, "the short edge fills the available height");
|
||||
assert_eq!(view.center, [200.0, 100.0]);
|
||||
@@ -5939,15 +5969,15 @@ mod tests {
|
||||
|
||||
// A scaled display stores the physical factor: the same point zoom is 2× the device px.
|
||||
let mut view = View { fill_pending: true, ..View::default() };
|
||||
fill_view(&mut view, &doc, vec2(600.0, 600.0), 2.0);
|
||||
fill_view(&mut view, &doc, vec2(600.0, 600.0), 2.0, 1.0);
|
||||
assert_eq!(view.zoom, 6.0);
|
||||
|
||||
// Fit never magnifies past the 1 point-per-pixel cap; on a scaled display the cap is 2.
|
||||
let mut view = View { fit_pending: true, ..View::default() };
|
||||
fit_view(&mut view, &doc, vec2(600.0, 600.0), 1.0);
|
||||
fit_view(&mut view, &doc, vec2(600.0, 600.0), 1.0, 1.0);
|
||||
assert_eq!(view.zoom, 1.0);
|
||||
let mut view = View { fit_pending: true, ..View::default() };
|
||||
fit_view(&mut view, &doc, vec2(600.0, 600.0), 2.0);
|
||||
fit_view(&mut view, &doc, vec2(600.0, 600.0), 2.0, 1.0);
|
||||
assert_eq!(view.zoom, 2.0);
|
||||
}
|
||||
|
||||
@@ -5977,12 +6007,12 @@ mod tests {
|
||||
let p = pos2(600.0, 200.0);
|
||||
for ppp in [1.0f32, 2.0] {
|
||||
// `View::zoom` is physical; the transform works in points.
|
||||
let xf = ViewXform { rect, zoom: 1.0 / ppp, center: [400.0, 300.0], flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform { rect, zoom: 1.0 / ppp, center: [400.0, 300.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
let doc = xf.to_doc(p);
|
||||
// Off: the document point under the pointer stays under it.
|
||||
let mut view = View { zoom: 1.0, center: [400.0, 300.0], ..Default::default() };
|
||||
zoom_about(&mut view, &xf, p, 2.0, false, ppp);
|
||||
let after = ViewXform { rect, zoom: 2.0 / ppp, center: view.center, flip: false, rotation: 0.0 };
|
||||
let after = ViewXform { rect, zoom: 2.0 / ppp, center: view.center, flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
let s = after.to_screen(doc[0] as f32, doc[1] as f32);
|
||||
assert!((s - p).length() < 0.5, "ppp {ppp}: {s:?} vs {p:?}");
|
||||
// On: the clicked point is the view centre.
|
||||
@@ -6025,7 +6055,8 @@ mod transform_controls_tests {
|
||||
app.ui.tool = Tool::Move;
|
||||
app.ui.tool_options.move_show_transform = true;
|
||||
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0 };
|
||||
let xf =
|
||||
ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
let r = transform_controls_rect(&app, &xf).expect("shape layers have transform bounds");
|
||||
assert!(transform_controls_hit(r, r.right_bottom()));
|
||||
|
||||
@@ -6068,7 +6099,8 @@ mod transform_controls_tests {
|
||||
// The Move tool's transform controls.
|
||||
app.ui.tool = Tool::Move;
|
||||
app.ui.tool_options.move_show_transform = true;
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0 };
|
||||
let xf =
|
||||
ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
let r = transform_controls_rect(&app, &xf).unwrap();
|
||||
assert!(!begin_transform_controls_at(&mut app, &ctx, &xf, r.right_bottom()));
|
||||
assert!(app.ui.transform.is_none());
|
||||
|
||||
@@ -55,7 +55,7 @@ pub fn moves_image(app: &PhotocraftApp) -> bool {
|
||||
/// goes from `from` to `to` degrees (zoom and flip unchanged). `center` itself when anything isn't
|
||||
/// finite.
|
||||
pub fn pivot_center(center: [f32; 2], c: [f64; 2], from: f32, to: f32, flip: bool) -> [f32; 2] {
|
||||
let a = ViewXform { rect: Rect::ZERO, zoom: 1.0, center, flip, rotation: from };
|
||||
let a = ViewXform { rect: Rect::ZERO, zoom: 1.0, center, flip, rotation: from, aspect: 1.0 };
|
||||
// c's offset from the view centre on screen (at zoom 1), then the same offset in the new view.
|
||||
let off = a.to_screen(c[0] as f32, c[1] as f32).to_vec2();
|
||||
let u = ViewXform { rotation: to, ..a }.unmap_vec(off);
|
||||
@@ -202,7 +202,7 @@ pub fn press_view(app: &PhotocraftApp) -> Option<PressView> {
|
||||
/// `to` shows at the screen position where `from` shows `p` (zoom and flip are the same). `p`
|
||||
/// itself when anything isn't finite.
|
||||
fn remap(p: [f64; 2], from: PressView, to: PressView, flip: bool) -> [f64; 2] {
|
||||
let at = |v: PressView| ViewXform { rect: Rect::ZERO, zoom: 1.0, center: v.center, flip, rotation: v.rotation };
|
||||
let at = |v: PressView| ViewXform { rect: Rect::ZERO, zoom: 1.0, center: v.center, flip, rotation: v.rotation, aspect: 1.0 };
|
||||
let q = at(to).to_doc(at(from).to_screen(p[0] as f32, p[1] as f32));
|
||||
if q.iter().all(|v| v.is_finite()) { q } else { p }
|
||||
}
|
||||
@@ -251,7 +251,7 @@ pub fn offset_image(app: &mut PhotocraftApp, rect: [f64; 4], press: Option<Press
|
||||
/// A screen vector `s` (points, at the current zoom ignored) as a document vector of the same
|
||||
/// length: undoes the view's flip and rotation.
|
||||
pub fn screen_to_doc_vec(app: &PhotocraftApp, s: [f64; 2]) -> [f64; 2] {
|
||||
let xf = ViewXform { rect: Rect::ZERO, zoom: 1.0, center: [0.0, 0.0], flip: app.ui.view.flip_horizontal, rotation: view_rotation(app) };
|
||||
let xf = ViewXform { rect: Rect::ZERO, zoom: 1.0, center: [0.0, 0.0], flip: app.ui.view.flip_horizontal, rotation: view_rotation(app), aspect: 1.0 };
|
||||
let u = xf.unmap_vec(vec2(s[0] as f32, s[1] as f32));
|
||||
[f64::from(u.x), f64::from(u.y)]
|
||||
}
|
||||
@@ -761,7 +761,7 @@ mod tests {
|
||||
for (from, to) in [(0.0, 30.0), (45.0, -90.0), (170.0, -170.0)] {
|
||||
let center = [100.0f32, 50.0];
|
||||
let c = [70.0, 20.0];
|
||||
let a = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)), zoom: 2.0, center, flip, rotation: from };
|
||||
let a = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0)), zoom: 2.0, center, flip, rotation: from, aspect: 1.0 };
|
||||
let n = pivot_center(center, c, from, to, flip);
|
||||
let b = ViewXform { center: n, rotation: to, ..a };
|
||||
let (p, q) = (a.to_screen(70.0, 20.0), b.to_screen(70.0, 20.0));
|
||||
|
||||
@@ -47,9 +47,10 @@ const VIEW_UNIFORM_SIZE: u64 = 128;
|
||||
const VIEW_FLOATS: usize = 32;
|
||||
const TILE_UNIFORM_SIZE: u64 = 16;
|
||||
|
||||
/// Whether this view can use the GPU canvas. Flip is still a CPU blit; rotation is a shader uniform.
|
||||
pub fn gpu_view_ok(flip: bool, _rotation: f32) -> bool {
|
||||
!flip
|
||||
/// Whether this view can use the GPU canvas. Flip and non-square pixel aspect correction are
|
||||
/// still CPU blits (the shader's filtering assumes square pixels); rotation is a shader uniform.
|
||||
pub fn gpu_view_ok(flip: bool, _rotation: f32, aspect: f32) -> bool {
|
||||
!flip && (aspect - 1.0).abs() < 1e-6
|
||||
}
|
||||
|
||||
/// Parameters for drawing one document view. Positions are in egui points.
|
||||
@@ -2032,9 +2033,10 @@ mod tests {
|
||||
assert_eq!(VIEW_FLOATS, 32);
|
||||
assert_eq!(VIEW_UNIFORM_SIZE, 128);
|
||||
assert_eq!(VIEW_FLOATS * 4, VIEW_UNIFORM_SIZE as usize);
|
||||
assert!(gpu_view_ok(false, 0.0));
|
||||
assert!(gpu_view_ok(false, 45.0_f32.to_radians()));
|
||||
assert!(!gpu_view_ok(true, 45.0_f32.to_radians()));
|
||||
assert!(gpu_view_ok(false, 0.0, 1.0));
|
||||
assert!(gpu_view_ok(false, 45.0_f32.to_radians(), 1.0));
|
||||
assert!(!gpu_view_ok(true, 45.0_f32.to_radians(), 1.0));
|
||||
assert!(!gpu_view_ok(false, 0.0, 2.0), "pixel aspect correction draws on the CPU");
|
||||
// Identity packing: rotation 0 matches the unrotated origin + scale map.
|
||||
let origin = [10.0, 20.0];
|
||||
let (scale, center) = (2.0, [40.0, 8.0]);
|
||||
|
||||
@@ -46,6 +46,7 @@ fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(x, y)
|
||||
}
|
||||
|
||||
@@ -153,7 +153,14 @@ fn harness() -> egui_kittest::Harness<'static, PhotocraftApp> {
|
||||
fn mouse(h: &mut egui_kittest::Harness<'static, PhotocraftApp>, kind: &str, x: f32, y: f32, mods: Modifiers) {
|
||||
let app = h.state();
|
||||
let v = &app.ui.views[0];
|
||||
let xf = ViewXform { rect: crate::rulers::content_rect(app, app.last_canvas_rect), zoom: v.zoom, center: v.center, flip: false, rotation: v.rotation };
|
||||
let xf = ViewXform {
|
||||
rect: crate::rulers::content_rect(app, app.last_canvas_rect),
|
||||
zoom: v.zoom,
|
||||
center: v.center,
|
||||
flip: false,
|
||||
rotation: v.rotation,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let pos = xf.to_screen(x, y);
|
||||
h.event(Event::ModifiersChanged(mods));
|
||||
h.event(Event::PointerMoved(pos));
|
||||
@@ -265,7 +272,14 @@ fn real_canvas_backspace_and_right_click_retract_polygonal_points() {
|
||||
|
||||
let state = h.state();
|
||||
let view = &state.ui.views[0];
|
||||
let xf = ViewXform { rect: crate::rulers::content_rect(state, state.last_canvas_rect), zoom: view.zoom, center: view.center, flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform {
|
||||
rect: crate::rulers::content_rect(state, state.last_canvas_rect),
|
||||
zoom: view.zoom,
|
||||
center: view.center,
|
||||
flip: false,
|
||||
rotation: 0.0,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let pos = xf.to_screen(155.0, 100.0);
|
||||
h.event(Event::PointerMoved(pos));
|
||||
h.event(Event::PointerButton { pos, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::ALT });
|
||||
|
||||
@@ -1989,6 +1989,9 @@ mod input_tests;
|
||||
#[cfg(test)]
|
||||
mod pencil_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod pixel_aspect_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod transform_undo_tests;
|
||||
|
||||
|
||||
@@ -316,6 +316,7 @@ fn screen(h: &Harness<'static, PhotocraftApp>, x: f64, y: f64) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(x as f32, y as f32)
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ fn view_xform(h: &Harness<'static, PhotocraftApp>) -> ViewXform {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -439,7 +439,7 @@ pub(crate) fn invoke_unguarded(app: &mut PhotocraftApp, ctx: &egui::Context, id:
|
||||
&& app.session.active_index().zip(app.session.clipboard.as_ref()).is_some_and(|(i, clip)| {
|
||||
let v = &app.ui.views[i];
|
||||
let pz = app.point_zoom();
|
||||
let (hw, hh) = (app.last_canvas_rect.width() / 2.0 / pz, app.last_canvas_rect.height() / 2.0 / pz);
|
||||
let (hw, hh) = (app.last_canvas_rect.width() / 2.0 / pz / app.ui.view.display_aspect(), app.last_canvas_rect.height() / 2.0 / pz);
|
||||
let r =
|
||||
photocraft_geom::Rect::new((v.center[0] - hw) as i32, (v.center[1] - hh) as i32, (v.center[0] + hw) as i32, (v.center[1] + hh) as i32);
|
||||
!clip.bounds.intersect(&r).is_empty()
|
||||
|
||||
@@ -57,7 +57,7 @@ fn harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
|
||||
|
||||
fn xf(app: &PhotocraftApp) -> ViewXform {
|
||||
let v = &app.ui.views[0];
|
||||
ViewXform { rect: app.last_canvas_rect, zoom: v.zoom / app.ppp, center: v.center, flip: false, rotation: 0.0 }
|
||||
ViewXform { rect: app.last_canvas_rect, zoom: v.zoom / app.ppp, center: v.center, flip: false, rotation: 0.0, aspect: 1.0 }
|
||||
}
|
||||
|
||||
/// The screen point of document point `p`.
|
||||
|
||||
@@ -1668,7 +1668,8 @@ fn navigator(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
let v = app.ui.views[idx].clone();
|
||||
let canvas = app.last_canvas_rect;
|
||||
let point_zoom = (v.zoom / app.canvas_ppp()).max(1e-6);
|
||||
let vw = canvas.width() / point_zoom * s;
|
||||
// Pixel aspect correction shows fewer document columns across the same canvas width.
|
||||
let vw = canvas.width() / point_zoom / app.ui.view.display_aspect() * s;
|
||||
let vh = canvas.height() / point_zoom * s;
|
||||
let c = pos2(rect.min.x + v.center[0] * s, rect.min.y + v.center[1] * s);
|
||||
let vr = Rect::from_center_size(c, vec2(vw, vh)).intersect(frame.shrink(1.0));
|
||||
|
||||
@@ -114,7 +114,7 @@ fn ctrl_alt_drag_resizes_the_pencil_without_painting() {
|
||||
#[test]
|
||||
fn square_cursor_sits_on_the_pixel_grid() {
|
||||
let rect = Rect::from_min_size(Pos2::new(0.0, 0.0), vec2(800.0, 600.0));
|
||||
let xf = ViewXform { rect, zoom: 8.0, center: [50.0, 37.5], flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform { rect, zoom: 8.0, center: [50.0, 37.5], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
// 1 px at 800 %: the 8-point square of the pixel under the pointer.
|
||||
let r = pencil_cursor_rect(&xf, [10.3, 5.7], 1.0, 1.0);
|
||||
assert_eq!(r, Rect::from_two_pos(xf.to_screen(10.0, 5.0), xf.to_screen(11.0, 6.0)));
|
||||
@@ -126,7 +126,14 @@ fn square_cursor_sits_on_the_pixel_grid() {
|
||||
assert_eq!(r, Rect::from_two_pos(xf.to_screen(9.0, 4.0), xf.to_screen(12.0, 7.0)));
|
||||
// At 2× and at an odd zoom the edges land on physical pixels.
|
||||
for (zoom, ppp) in [(3.3, 2.0), (0.5, 2.0), (1.0, 1.0), (13.7, 1.5)] {
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::new(0.3, 0.7), vec2(800.0, 600.0)), zoom, center: [50.2, 37.9], flip: true, rotation: 0.0 };
|
||||
let xf = ViewXform {
|
||||
rect: Rect::from_min_size(Pos2::new(0.3, 0.7), vec2(800.0, 600.0)),
|
||||
zoom,
|
||||
center: [50.2, 37.9],
|
||||
flip: true,
|
||||
rotation: 0.0,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let r = pencil_cursor_rect(&xf, [20.4, 30.6], 5.0, ppp);
|
||||
for v in [r.min.x, r.min.y, r.max.x, r.max.y] {
|
||||
assert!((v * ppp - (v * ppp).round()).abs() < 1e-3, "zoom {zoom} ppp {ppp}: {v}");
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
//! View › Pixel Aspect Ratio (#2594): with correction on, the canvas shows each document pixel
|
||||
//! that many times wider, and the pointer still lands on the pixel drawn under it. The document's
|
||||
//! pixels never change.
|
||||
|
||||
use egui::{Modifiers, PointerButton, Pos2, Rect, vec2};
|
||||
use egui_kittest::Harness;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::ViewXform;
|
||||
use crate::state::Tool;
|
||||
|
||||
fn harness() -> Harness<'static, PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 100, "background": "transparent"})).unwrap();
|
||||
app.run("tools.setColors", json!({"foreground": "#000000"})).unwrap();
|
||||
app.ui.tool = Tool::Pencil;
|
||||
crate::paint_mouse::sync_tool_brush(&mut app);
|
||||
app.run("tools.setBrush", json!({"brush": {"size": 1, "smoothing": {"amount": 0.0}}})).unwrap();
|
||||
app.sync_views();
|
||||
let mut h = Harness::builder().with_size(vec2(1200.0, 800.0)).with_step_dt(1.0 / 60.0).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
fn menu(h: &mut Harness<'static, PhotocraftApp>, id: &str, params: serde_json::Value) {
|
||||
let ctx = h.ctx.clone();
|
||||
crate::menus::invoke(h.state_mut(), &ctx, id, params).unwrap();
|
||||
h.run_steps(3);
|
||||
}
|
||||
|
||||
/// The image's screen rectangle, as the canvas draws it.
|
||||
fn image_rect(h: &Harness<'static, PhotocraftApp>) -> Rect {
|
||||
let app = h.state();
|
||||
let doc = &app.session.active().unwrap().doc;
|
||||
ViewXform::active(app).unwrap().doc_rect(doc.bounds())
|
||||
}
|
||||
|
||||
fn click(h: &mut Harness<'static, PhotocraftApp>, pos: Pos2) {
|
||||
h.event(egui::Event::PointerMoved(pos));
|
||||
h.run_steps(1);
|
||||
for pressed in [true, false] {
|
||||
h.event(egui::Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
|
||||
h.run_steps(2);
|
||||
}
|
||||
}
|
||||
|
||||
fn alpha(h: &Harness<'static, PhotocraftApp>, x: i32, y: i32) -> f32 {
|
||||
let st = h.state().session.active().unwrap();
|
||||
st.doc.layer(st.active_layer.unwrap()).unwrap().surface().unwrap().rgba(x, y)[3]
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_mapping_stretches_document_x_and_inverts_exactly() {
|
||||
let rect = Rect::from_min_size(Pos2::new(10.0, 20.0), vec2(800.0, 600.0));
|
||||
let square = ViewXform { rect, zoom: 1.5, center: [100.0, 50.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
let wide = ViewXform { aspect: 2.0, ..square };
|
||||
let r = wide.doc_rect(photocraft_geom::Rect::new(0, 0, 200, 100));
|
||||
assert!((r.width() - 600.0).abs() < 1e-3 && (r.height() - 150.0).abs() < 1e-3, "{r:?}");
|
||||
assert_eq!(r.center(), square.doc_rect(photocraft_geom::Rect::new(0, 0, 200, 100)).center());
|
||||
assert!((wide.zoom_x() - 3.0).abs() < 1e-6);
|
||||
for flip in [false, true] {
|
||||
for rotation in [0.0, 30.0, -90.0] {
|
||||
for aspect in [0.91, 1.0, 2.0] {
|
||||
let xf = ViewXform { flip, rotation, aspect, ..square };
|
||||
let p = xf.to_screen(37.0, 81.0);
|
||||
let d = xf.to_doc(p);
|
||||
assert!((d[0] - 37.0).abs() < 1e-3 && (d[1] - 81.0).abs() < 1e-3, "{flip} {rotation} {aspect}: {d:?}");
|
||||
// Pan and zoom-about deltas agree with the point mapping.
|
||||
let q = xf.to_screen(40.0, 85.0);
|
||||
let u = xf.unmap_vec(q - p) / xf.zoom;
|
||||
assert!((u.x - 3.0).abs() < 1e-3 && (u.y - 4.0).abs() < 1e-3, "{flip} {rotation} {aspect}: {u:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
// A hostile ratio draws square instead of collapsing the image.
|
||||
for aspect in [0.0, -1.0, f32::NAN, f32::INFINITY] {
|
||||
let xf = ViewXform { aspect, ..square };
|
||||
assert_eq!(xf.to_screen(5.0, 5.0), square.to_screen(5.0, 5.0));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anamorphic_ratio_draws_the_canvas_twice_as_wide_and_correction_off_restores_it() {
|
||||
let mut h = harness();
|
||||
let square = image_rect(&h);
|
||||
assert!((square.width() / square.height() - 2.0).abs() < 0.02, "{square:?}");
|
||||
menu(&mut h, "view.pixelAspectRatio.anamorphic2To1", json!({}));
|
||||
assert!(h.state().ui.view.pixel_aspect_correction, "correction is on by default, as in Photoshop");
|
||||
let wide = image_rect(&h);
|
||||
assert!((wide.width() / wide.height() - 4.0).abs() < 0.02, "{wide:?}");
|
||||
assert!((wide.height() - square.height()).abs() < 0.5, "only the width changes: {square:?} {wide:?}");
|
||||
// The document itself keeps its pixels.
|
||||
let doc = &h.state().session.active().unwrap().doc;
|
||||
assert_eq!([doc.size.width, doc.size.height], [200, 100]);
|
||||
// View › Pixel Aspect Ratio Correction off: square pixels again.
|
||||
menu(&mut h, "view.pixelAspectRatioCorrection", json!({}));
|
||||
assert!(!h.state().ui.view.pixel_aspect_correction);
|
||||
let off = image_rect(&h);
|
||||
assert!((off.width() / off.height() - 2.0).abs() < 0.02, "{off:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_paints_the_pixel_drawn_under_the_pointer() {
|
||||
let mut h = harness();
|
||||
menu(&mut h, "view.pixelAspectRatio.anamorphic2To1", json!({}));
|
||||
let xf = ViewXform::active(h.state()).unwrap();
|
||||
let at = xf.to_screen(120.5, 30.5);
|
||||
click(&mut h, at);
|
||||
assert_eq!(alpha(&h, 120, 30), 1.0, "painted under the pointer");
|
||||
// A square mapping of the same screen point is a different pixel, left blank.
|
||||
let square = ViewXform { aspect: 1.0, ..xf }.to_doc(at);
|
||||
let (sx, sy) = (square[0].floor() as i32, square[1].floor() as i32);
|
||||
assert_ne!((sx, sy), (120, 30));
|
||||
assert_eq!(alpha(&h, sx, sy), 0.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fit_and_fill_on_screen_use_the_stretched_width() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 1000, "height": 500})).unwrap();
|
||||
let doc = app.session.active().unwrap().doc.clone();
|
||||
// Fit keeps 20 points of margin per side: 1000 points across for 1000 px wide, 2000 shown.
|
||||
let area = vec2(1040.0, 800.0);
|
||||
let mut view = crate::state::View::default();
|
||||
crate::canvas::fit_view(&mut view, &doc, area, 1.0, 1.0);
|
||||
assert_eq!(view.zoom, 1.0);
|
||||
crate::canvas::fit_view(&mut view, &doc, area, 1.0, 2.0);
|
||||
assert_eq!(view.zoom, 0.5);
|
||||
assert_eq!(view.center, [500.0, 250.0]);
|
||||
// Fill: the stretched image is wider than tall, so the height fills.
|
||||
crate::canvas::fill_view(&mut view, &doc, vec2(1000.0, 1000.0), 1.0, 2.0);
|
||||
assert_eq!(view.zoom, 2.0);
|
||||
}
|
||||
@@ -49,6 +49,7 @@ fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(x, y)
|
||||
}
|
||||
|
||||
@@ -316,7 +316,7 @@ mod tests {
|
||||
for center in [[0.0, 0.0], [320.0, 240.0], [10.0, -4.0]] {
|
||||
for flip in [false, true] {
|
||||
let rect = Rect::from_min_size(pos2(100.0, 50.0), vec2(800.0, 600.0));
|
||||
let a = ViewXform { rect, zoom, center, flip, rotation: 0.0 };
|
||||
let a = ViewXform { rect, zoom, center, flip, rotation: 0.0, aspect: 1.0 };
|
||||
let sx = if flip { -1.0 } else { 1.0 };
|
||||
let want = rect.center() + vec2((10.0 - center[0]) * zoom * sx, (20.0 - center[1]) * zoom);
|
||||
let got = a.to_screen(10.0, 20.0);
|
||||
@@ -331,21 +331,21 @@ mod tests {
|
||||
let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(400.0, 300.0));
|
||||
for rot in [15.0, 90.0, -45.0, 180.0] {
|
||||
for flip in [false, true] {
|
||||
let xf = ViewXform { rect, zoom: 2.0, center: [80.0, 40.0], flip, rotation: rot };
|
||||
let xf = ViewXform { rect, zoom: 2.0, center: [80.0, 40.0], flip, rotation: rot, aspect: 1.0 };
|
||||
let s = xf.to_screen(10.0, 20.0);
|
||||
let d = xf.to_doc(s);
|
||||
assert!((d[0] - 10.0).abs() < 1e-3 && (d[1] - 20.0).abs() < 1e-3, "rot={rot} flip={flip} {d:?}");
|
||||
}
|
||||
}
|
||||
// 90°: a point on +X in document space lands on +Y on screen (Y-down = clockwise).
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: false, rotation: 90.0 };
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: false, rotation: 90.0, aspect: 1.0 };
|
||||
let s = xf.to_screen(1.0, 0.0);
|
||||
let c = rect.center();
|
||||
assert!((s.x - c.x).abs() < 1e-3 && (s.y - (c.y + 1.0)).abs() < 1e-3, "{s:?} vs {c:?}");
|
||||
// Flip after rotate: 0° +X goes left.
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: true, rotation: 0.0 };
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: true, rotation: 0.0, aspect: 1.0 };
|
||||
assert!(xf.to_screen(1.0, 0.0).x < rect.center().x);
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: true, rotation: 90.0 };
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [0.0, 0.0], flip: true, rotation: 90.0, aspect: 1.0 };
|
||||
let s = xf.to_screen(1.0, 0.0);
|
||||
assert!((s.x - c.x).abs() < 1e-3 && (s.y - (c.y + 1.0)).abs() < 1e-3, "flip then rotate {s:?}");
|
||||
}
|
||||
@@ -509,7 +509,8 @@ mod tests {
|
||||
let b = st.doc.selection.as_ref().and_then(|s| photocraft_compose::bounds::content_bounds(s).into()).expect("selection");
|
||||
// Axis-aligned in the file: the outline is drawn rotated via ViewXform.
|
||||
assert!(b.width() >= 40 && b.height() >= 10, "{b:?}");
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), zoom: 1.0, center: [100.0, 50.0], flip: false, rotation: 30.0 };
|
||||
let xf =
|
||||
ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0)), zoom: 1.0, center: [100.0, 50.0], flip: false, rotation: 30.0, aspect: 1.0 };
|
||||
let a = xf.to_screen(10.0, 20.0);
|
||||
let c = xf.to_screen(50.0, 20.0);
|
||||
let v = c - a;
|
||||
@@ -521,8 +522,8 @@ mod tests {
|
||||
#[test]
|
||||
fn gpu_path_stays_on_with_rotation() {
|
||||
// Flip is the only CPU-blit fallback; rotation is a shader uniform.
|
||||
assert!(crate::gpu_canvas::gpu_view_ok(false, 45.0));
|
||||
assert!(!crate::gpu_canvas::gpu_view_ok(true, 45.0));
|
||||
assert!(crate::gpu_canvas::gpu_view_ok(false, 0.0));
|
||||
assert!(crate::gpu_canvas::gpu_view_ok(false, 45.0, 1.0));
|
||||
assert!(!crate::gpu_canvas::gpu_view_ok(true, 45.0, 1.0));
|
||||
assert!(crate::gpu_canvas::gpu_view_ok(false, 0.0, 1.0));
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ pub fn draw_grid(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, d
|
||||
}
|
||||
let st = Stroke::new(1.0, base.gamma_multiply(alpha));
|
||||
let [step_x, step_y] = steps;
|
||||
if step_x * (xf.zoom as f64) >= 6.0 {
|
||||
if step_x * (xf.zoom_x() as f64) >= 6.0 {
|
||||
let mut x = step_x;
|
||||
while x < w {
|
||||
styled_line(painter, xf.to_screen(x as f32, 0.0), xf.to_screen(x as f32, h as f32), st, g.grid_style);
|
||||
@@ -246,7 +246,9 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &
|
||||
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 px_per_unit = unit.to_px(1.0, dpi, extent, ppi).max(1e-9);
|
||||
let step = nice_step(60.0 / (xf.zoom as f64 * px_per_unit).max(1e-6), whole);
|
||||
// The top ruler runs along document x, which pixel aspect correction stretches.
|
||||
let zoom = if vertical { xf.zoom } else { xf.zoom_x() };
|
||||
let step = nice_step(60.0 / (zoom as f64 * px_per_unit).max(1e-6), whole);
|
||||
let (u0, u1) = if vertical {
|
||||
ruler_range(xf, left.left_top(), left.left_bottom(), 1, px_per_unit)
|
||||
} else {
|
||||
@@ -471,7 +473,7 @@ mod tests {
|
||||
let rect = Rect::from_min_size(pos2(0.0, 0.0), vec2(800.0, 600.0));
|
||||
let top = Rect::from_min_size(rect.min, vec2(rect.width(), RULER));
|
||||
for flip in [false, true] {
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [400.0, 300.0], flip, rotation: 0.0 };
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [400.0, 300.0], flip, rotation: 0.0, aspect: 1.0 };
|
||||
let (u0, u1) = ruler_range(&xf, top.left_top(), top.right_top(), 0, 1.0);
|
||||
assert!(u0 <= u1, "flip={flip}: u0 ({u0}) must be <= u1 ({u1})");
|
||||
assert!(u1 - u0 > 0.0, "flip={flip}: range is empty");
|
||||
|
||||
@@ -124,9 +124,10 @@ pub fn bar_rects(rect: Rect, horizontal: bool, vertical: bool) -> (Option<Rect>,
|
||||
|
||||
/// Show the scrollbars of the canvas `rect` and apply their drags and page clicks to `view`.
|
||||
/// `flip` is View › Flip Horizontal (the bar then runs the other way through the image);
|
||||
/// `overscroll` is Preferences › Tools › Overscroll.
|
||||
/// `aspect` is the displayed pixel aspect ratio (a document pixel is that many points wider per
|
||||
/// point of height); `overscroll` is Preferences › Tools › Overscroll.
|
||||
/// Returns true when the pointer is over a bar (the canvas then leaves the pointer alone).
|
||||
pub fn show(ui: &Ui, rect: Rect, view: &mut View, flip: bool, overscroll: bool, key: Id) -> bool {
|
||||
pub fn show(ui: &Ui, rect: Rect, view: &mut View, flip: bool, aspect: f32, overscroll: bool, key: Id) -> bool {
|
||||
let [w, h] = view.doc_size;
|
||||
// `Span` works in egui points; `View::zoom` is device pixels per document pixel.
|
||||
let ppp = ui.ctx().pixels_per_point();
|
||||
@@ -134,7 +135,8 @@ pub fn show(ui: &Ui, rect: Rect, view: &mut View, flip: bool, overscroll: bool,
|
||||
// Horizontal axis in "screen order": with the view flipped, run the image backwards.
|
||||
let hx = |c: f32| if flip { w as f32 - c } else { c };
|
||||
let with_extent = |s: Span| (s, s.extent(overscroll));
|
||||
let ex = Span::of(w as f32, hx(view.center[0]), rect.width(), zoom).map(with_extent);
|
||||
let aspect = if aspect.is_finite() && aspect > 0.0 { aspect } else { 1.0 };
|
||||
let ex = Span::of(w as f32, hx(view.center[0]), rect.width() / aspect, zoom).map(with_extent);
|
||||
let ey = Span::of(h as f32, view.center[1], rect.height(), zoom).map(with_extent);
|
||||
if ex.is_none() && ey.is_none() {
|
||||
return false;
|
||||
|
||||
@@ -355,7 +355,7 @@ mod tests {
|
||||
let xf = {
|
||||
let st = h.state();
|
||||
let v = st.ui.views[st.session.active_index().unwrap()].clone();
|
||||
crate::canvas::ViewXform { rect: st.last_canvas_rect, zoom: v.zoom, center: v.center, flip: false, rotation: v.rotation }
|
||||
crate::canvas::ViewXform { rect: st.last_canvas_rect, zoom: v.zoom, center: v.center, flip: false, rotation: v.rotation, aspect: 1.0 }
|
||||
};
|
||||
let from = xf.to_screen(end[0] as f32, end[1] as f32);
|
||||
h.event(Event::PointerMoved(target));
|
||||
|
||||
@@ -43,6 +43,7 @@ fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(x, y)
|
||||
}
|
||||
|
||||
@@ -169,7 +169,8 @@ fn overlays_follow_document_view_transforms_in_every_theme() {
|
||||
app.run("paint.setSymmetry", json!({"mode":"dual"})).unwrap();
|
||||
let ctx = Context::default();
|
||||
crate::theme::apply(&ctx, theme);
|
||||
let xf = ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(500.0, 400.0)), zoom: 1.7, center: [50.0, 40.0], rotation: 37.0, flip: true };
|
||||
let xf =
|
||||
ViewXform { rect: Rect::from_min_size(Pos2::ZERO, vec2(500.0, 400.0)), zoom: 1.7, center: [50.0, 40.0], rotation: 37.0, flip: true, aspect: 1.0 };
|
||||
let mut out = ctx.run_ui(RawInput { screen_rect: Some(xf.rect), ..Default::default() }, |ui| {
|
||||
let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Middle, egui::Id::new("symmetry")));
|
||||
draw(&app, &painter, &xf);
|
||||
|
||||
@@ -3239,7 +3239,14 @@ mod tests {
|
||||
assert!(q[0][0] < -10.0 && q[2][1] > 74.0, "the box reaches past the canvas: {q:?}");
|
||||
let tol = 1e-3;
|
||||
for (rotation, flip) in [(0.0, false), (30.0, false), (-75.0, true)] {
|
||||
let xf = ViewXform { rect: egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(400.0, 400.0)), zoom: 2.0, center: [32.0, 32.0], flip, rotation };
|
||||
let xf = ViewXform {
|
||||
rect: egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(400.0, 400.0)),
|
||||
zoom: 2.0,
|
||||
center: [32.0, 32.0],
|
||||
flip,
|
||||
rotation,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let meshes = preview_meshes(&app, &xf);
|
||||
let verts: Vec<[f64; 2]> = meshes.iter().flat_map(|m| m.vertices.iter().map(|v| xf.to_doc(v.pos))).collect();
|
||||
assert!(!verts.is_empty(), "the preview still draws (rotation {rotation})");
|
||||
@@ -3254,8 +3261,14 @@ mod tests {
|
||||
// Inside the canvas the mesh is drawn unchanged (24×24 grid).
|
||||
cancel(&mut app);
|
||||
begin(&mut app, &gpu_ctx()).unwrap();
|
||||
let xf =
|
||||
ViewXform { rect: egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(200.0, 200.0)), zoom: 1.0, center: [32.0, 32.0], flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform {
|
||||
rect: egui::Rect::from_min_size(egui::Pos2::ZERO, vec2(200.0, 200.0)),
|
||||
zoom: 1.0,
|
||||
center: [32.0, 32.0],
|
||||
flip: false,
|
||||
rotation: 0.0,
|
||||
aspect: 1.0,
|
||||
};
|
||||
let meshes = preview_meshes(&app, &xf);
|
||||
assert_eq!(meshes.iter().map(|m| m.vertices.len()).sum::<usize>(), 25 * 25);
|
||||
}
|
||||
@@ -3330,6 +3343,7 @@ mod tests {
|
||||
center: [3000.0, 2000.0],
|
||||
flip: false,
|
||||
rotation: 0.0,
|
||||
aspect: 1.0,
|
||||
};
|
||||
draw_overlay(&app, &painter, &xf);
|
||||
});
|
||||
|
||||
@@ -57,6 +57,7 @@ fn screen(h: &Harness<'static, PhotocraftApp>, p: [f64; 2]) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(p[0] as f32, p[1] as f32)
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ fn xf(app: &PhotocraftApp) -> ViewXform {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1168,7 +1168,7 @@ mod tests {
|
||||
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
|
||||
let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 400.0));
|
||||
let painter = ui.ctx().layer_painter(egui::LayerId::background()).with_clip_rect(rect);
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0 };
|
||||
let xf = ViewXform { rect, zoom: 1.0, center: [100.0, 100.0], flip: false, rotation: 0.0, aspect: 1.0 };
|
||||
draw_overlay(app, &painter, &xf, &doc);
|
||||
});
|
||||
out.textures_delta.clear();
|
||||
|
||||
@@ -111,7 +111,8 @@ impl Default for ViewOptions {
|
||||
snap_to: SnapTo::default(),
|
||||
flip_horizontal: false,
|
||||
pixel_aspect: "square".into(),
|
||||
pixel_aspect_correction: false,
|
||||
// Photoshop shows non-square pixels corrected until the user turns it off.
|
||||
pixel_aspect_correction: true,
|
||||
pattern_preview: false,
|
||||
pixel_art_preview: false,
|
||||
arrange: "tabs".into(),
|
||||
@@ -134,6 +135,13 @@ impl ViewOptions {
|
||||
pub fn hides_tabs(&self) -> bool {
|
||||
self.screen_mode != "standard"
|
||||
}
|
||||
/// Horizontal display scale of a document pixel: the pixel aspect ratio while View › Pixel
|
||||
/// Aspect Ratio Correction is on, else 1 (square). The document's pixels never change.
|
||||
pub fn display_aspect(&self) -> f32 {
|
||||
let r = pixel_aspect_ratio(&self.pixel_aspect);
|
||||
// `ui.set` can store any string; only the dialog's 0.1–10 range is drawn.
|
||||
if self.pixel_aspect_correction && r.is_finite() { r.clamp(0.1, 10.0) } else { 1.0 }
|
||||
}
|
||||
/// Photoshop's full screen modes show no scroll bars.
|
||||
pub fn shows_scrollbars(&self) -> bool {
|
||||
self.screen_mode == "standard"
|
||||
|
||||
@@ -69,6 +69,7 @@ fn screen(h: &Harness, x: f32, y: f32) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(x, y)
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ fn screen(h: &H, p: [f64; 2]) -> Pos2 {
|
||||
let app = h.state();
|
||||
let v = &app.ui.views[0];
|
||||
let rect = photocraft_ui_egui::rulers::content_rect(app, app.last_canvas_rect);
|
||||
ViewXform { rect, zoom: v.zoom, center: v.center, flip: false, rotation: v.rotation }.to_screen(p[0] as f32, p[1] as f32)
|
||||
ViewXform { rect, zoom: v.zoom, center: v.center, flip: false, rotation: v.rotation, aspect: 1.0 }.to_screen(p[0] as f32, p[1] as f32)
|
||||
}
|
||||
|
||||
/// A point where `id` (or, for a group, one of its layers) is the topmost layer with pixels.
|
||||
|
||||
@@ -73,6 +73,7 @@ fn screen(h: &Harness, x: f32, y: f32) -> Pos2 {
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
}
|
||||
.to_screen(x, y)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user