Skip to content

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

Image Size: type keeps its pixel size when the resolution changes (#7124)

David Mann617b75597c7d
diff --git a/crates/engine/src/canvas_geom.rs b/crates/engine/src/canvas_geom.rs
index 0722c6bd1..ca8c6b30a 100644
--- a/crates/engine/src/canvas_geom.rs
+++ b/crates/engine/src/canvas_geom.rs
@@ -71,6 +71,40 @@ fn retag_text(dpi: f32, t: &mut TextLayer) {
t.psd_raw = Some(Arc::new(photocraft_text::psd::build_tysh(t, dpi, layout.bounds())));
}
+/// Sets the document resolution keeping every type layer's size in pixels, as Photoshop does (its
+/// point size changes instead). Text space is in pixels at `pt x dpi / 72`, so it grows by
+/// new / old: box and path scale with it, and the layer transform undoes that.
+pub(crate) fn set_resolution(doc: &mut Document, dpi: f32) {
+ let old = std::mem::replace(&mut doc.resolution_dpi, dpi);
+ if !(old.is_finite() && old > 0.0 && dpi.is_finite() && dpi > 0.0) || old == dpi {
+ return;
+ }
+ let k = f64::from(dpi) / f64::from(old);
+ let grow = Affine::scale(k);
+ crate::image_cmds::for_each_layer(&mut doc.layers, &mut |l| {
+ let LayerContent::Text(t) = &mut l.content else { return };
+ t.transform = t.transform.mul(&Affine::scale(1.0 / k));
+ match &mut t.shape {
+ photocraft_doc::text::TextShape::Box { x, y, width, height } => {
+ for v in [x, y, width, height] {
+ *v = grown(*v, k);
+ }
+ }
+ photocraft_doc::text::TextShape::Path { path, start, .. } => {
+ path.knots.iter_mut().for_each(|kn| *kn = kn.transform(&grow));
+ *start = grown(*start, k);
+ }
+ photocraft_doc::text::TextShape::Point => {}
+ }
+ retag_text(dpi, t);
+ });
+}
+
+/// `v x k` kept finite (a huge box from a file must not become infinite).
+fn grown(v: f32, k: f64) -> f32 {
+ (f64::from(v) * k).clamp(-f64::from(f32::MAX), f64::from(f32::MAX)) as f32
+}
+
/// Applies `a` to one layer's (and its children's) non-pixel geometry. With `content` false only
/// the parts Free Transform (`transform_layer`) doesn't move are touched: unlinked vector masks,
/// gradient fill angles, effect reference points and artboards.
diff --git a/crates/engine/src/image_cmds.rs b/crates/engine/src/image_cmds.rs
index 3e870aa68..72e757ae5 100644
--- a/crates/engine/src/image_cmds.rs
+++ b/crates/engine/src/image_cmds.rs
@@ -212,7 +212,8 @@ fn image_size(s: &mut Session, p: &Value) -> Result<Value> {
}
s.edit("Image Size", |doc, _| {
if let Some(r) = dpi {
- doc.resolution_dpi = r.clamp(1.0, 10_000.0);
+ // Type keeps its pixel size; resampling below then scales it with the pixels.
+ crate::canvas_geom::set_resolution(doc, r.clamp(1.0, 10_000.0));
}
let filter = match resample {
Method::None => return Ok(()),
@@ -1000,6 +1001,28 @@ mod tests {
assert_eq!(r, json!({"width": 40, "height": 20}));
}
+ /// Type keeps its pixel size across a resolution change: resampled 2x at twice the ppi it is
+ /// twice as tall (not 4x), with Resample off it stays, and a later re-render doesn't jump.
+ #[test]
+ fn image_size_with_a_new_resolution_scales_type_with_the_pixels_only() {
+ for (params, k) in [(json!({"width": 200, "height": 200, "resolution": 144}), 2), (json!({"resolution": 144, "resample": "none"}), 1)] {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 100, "height": 100})).unwrap();
+ s.execute("type.create", json!({"x": 10, "y": 50, "text": "Hi", "size": 12})).unwrap();
+ let height = |s: &Session| doc(s).layers.last().unwrap().surface().unwrap().content_bounds().height();
+ let h0 = height(&s);
+ assert!(h0 > 4, "{h0}");
+ s.execute("image.imageSize", params.clone()).unwrap();
+ assert!(height(&s).abs_diff(h0 * k) <= 2, "{params}: {h0} -> {}", height(&s));
+ let d = doc(&s);
+ let LayerContent::Text(t) = &d.layers.last().unwrap().content else { panic!("type layer") };
+ let mut t = t.clone();
+ crate::type_cmds::refresh(d, &mut t);
+ let after = t.cache.unwrap().content_bounds().height();
+ assert!(after.abs_diff(h0 * k) <= 2, "{params}: re-rendered {h0} -> {after}");
+ }
+ }
+
#[test]
fn image_size_reports_the_size_it_applied() {
// #1815: every path reports the document's size afterwards, at every depth.