mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
The dialogs converted points and picas at a fixed 72 pt/in, while the rulers, Info, layer properties and the shape dialog use Preferences > Units & Rulers > Point/Pica Size. With Traditional (72.27) a document showed different point sizes in the rulers and in Image Size. The dialog now stores the preference's points per inch when it opens (__ptPerIn, 72 when missing) and uses it for points, picas (points / 12) and typed units in the Width/Height fields. Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
b404d50581
commit
8226139806
1 file changed
+73
-16
@@ -71,6 +71,7 @@ pub fn reset(f: &mut Map<String, Value>) {
|
||||
pub fn open(app: &mut PhotocraftApp, command: &str) -> Option<u64> {
|
||||
let unit = pref_unit(app.session.prefs().units_and_rulers.rulers);
|
||||
let resample = pref_resample(app.session.prefs().general.image_interpolation);
|
||||
let pt_per_in = app.session.prefs().units_and_rulers.point_size.per_inch();
|
||||
let d = &app.session.active()?.doc;
|
||||
let (w, h, res) = (d.size.width as f64, d.size.height as f64, d.resolution_dpi as f64);
|
||||
let label = photocraft_engine::commands::find(command).map(|c| c.label).unwrap_or("Image Size…");
|
||||
@@ -82,6 +83,7 @@ pub fn open(app: &mut PhotocraftApp, command: &str) -> Option<u64> {
|
||||
f.insert("__origH".into(), json!(h));
|
||||
f.insert("__origRes".into(), json!(res));
|
||||
f.insert("__unit".into(), json!(unit));
|
||||
f.insert("__ptPerIn".into(), json!(pt_per_in));
|
||||
f.insert("__bytesPerPixel".into(), json!(d.layers.first().and_then(|l| l.surface()).map_or(4, |s| s.format().bytes_per_pixel())));
|
||||
f.insert("width".into(), json!(w));
|
||||
f.insert("height".into(), json!(h));
|
||||
@@ -102,27 +104,33 @@ fn num(f: &Map<String, Value>, k: &str) -> f64 {
|
||||
f.get(k).and_then(Value::as_f64).unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// Pixels → display unit.
|
||||
fn to_unit(px: f64, unit: &str, orig: f64, res: f64) -> f64 {
|
||||
/// Points per inch from Preferences › Units & Rulers › Point/Pica Size, stored when the dialog
|
||||
/// opens: 72 (PostScript) when missing or not a positive finite number.
|
||||
fn pt_per_in(f: &Map<String, Value>) -> f64 {
|
||||
f.get("__ptPerIn").and_then(Value::as_f64).filter(|v| v.is_finite() && *v > 0.0).unwrap_or(72.0)
|
||||
}
|
||||
|
||||
/// Pixels → display unit; `ppi` is points per inch (a pica is 12 points).
|
||||
fn to_unit(px: f64, unit: &str, orig: f64, res: f64, ppi: f64) -> f64 {
|
||||
match unit {
|
||||
"percent" => px / orig.max(1.0) * 100.0,
|
||||
"in" => px / res.max(1.0),
|
||||
"cm" => px / res.max(1.0) * 2.54,
|
||||
"mm" => px / res.max(1.0) * 25.4,
|
||||
"pt" => px / res.max(1.0) * 72.0,
|
||||
"pica" => px / res.max(1.0) * 6.0,
|
||||
"pt" => px / res.max(1.0) * ppi,
|
||||
"pica" => px / res.max(1.0) * ppi / 12.0,
|
||||
_ => px,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_unit(v: f64, unit: &str, orig: f64, res: f64) -> f64 {
|
||||
fn from_unit(v: f64, unit: &str, orig: f64, res: f64, ppi: f64) -> f64 {
|
||||
match unit {
|
||||
"percent" => v * orig / 100.0,
|
||||
"in" => v * res,
|
||||
"cm" => v / 2.54 * res,
|
||||
"mm" => v / 25.4 * res,
|
||||
"pt" => v / 72.0 * res,
|
||||
"pica" => v / 6.0 * res,
|
||||
"pt" => v / ppi * res,
|
||||
"pica" => v / ppi * 12.0 * res,
|
||||
_ => v,
|
||||
}
|
||||
}
|
||||
@@ -165,18 +173,19 @@ fn dim_field(ui: &mut egui::Ui, f: &mut Map<String, Value>, key: &str, orig_key:
|
||||
let res = f.get("resolution").and_then(Value::as_f64).unwrap_or(num(f, "__origRes"));
|
||||
let orig = num(f, orig_key);
|
||||
let px = num(f, key);
|
||||
let mut v = to_unit(px, &unit, orig, res) as f32;
|
||||
let ppi = pt_per_in(f);
|
||||
let mut v = to_unit(px, &unit, orig, res, ppi) as f32;
|
||||
// The engine's 300000 px limit in this unit (never below 30000), so a large document's size
|
||||
// isn't clamped when the field is drawn: egui clamps the shown value to the range, and the
|
||||
// field reports that as an edit (#4162).
|
||||
let lim = if unit == "px" { 300000.0 } else { to_unit(300000.0, &unit, orig, res).max(30000.0) as f32 };
|
||||
let lim = if unit == "px" { 300000.0 } else { to_unit(300000.0, &unit, orig, res, ppi).max(30000.0) as f32 };
|
||||
let range = -lim..=lim;
|
||||
// Each unit keeps its own precision: whole pixels, 3 places for inches, 2 for cm/mm/picas (#2434).
|
||||
// A typed unit (`2in` while in pixels) converts at the dialog's resolution.
|
||||
let units = crate::widgets::FieldUnits { unit: crate::widgets::unit_of_key(&unit), dpi: res, ppi: 72.0 };
|
||||
let units = crate::widgets::FieldUnits { unit: crate::widgets::unit_of_key(&unit), dpi: res, ppi };
|
||||
let changed = crate::widgets::value_field_units(ui, &mut v, range, 90.0, crate::widgets::unit_decimals(&unit), units).changed();
|
||||
if changed {
|
||||
let px = from_unit(v as f64, &unit, orig, res);
|
||||
let px = from_unit(v as f64, &unit, orig, res, ppi);
|
||||
f.insert(key.into(), json!(if unit == "px" { px.round() } else { px }));
|
||||
}
|
||||
let _ = id;
|
||||
@@ -442,15 +451,36 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn unit_conversions_round_trip() {
|
||||
for unit in ["px", "percent", "in", "cm", "mm", "pt", "pica"] {
|
||||
let v = to_unit(1500.0, unit, 3000.0, 300.0);
|
||||
assert!((from_unit(v, unit, 3000.0, 300.0) - 1500.0).abs() < 1e-9, "{unit}");
|
||||
for ppi in [72.0, 72.27] {
|
||||
for unit in ["px", "percent", "in", "cm", "mm", "pt", "pica"] {
|
||||
let v = to_unit(1500.0, unit, 3000.0, 300.0, ppi);
|
||||
assert!((from_unit(v, unit, 3000.0, 300.0, ppi) - 1500.0).abs() < 1e-9, "{unit} at {ppi} pt/in");
|
||||
}
|
||||
}
|
||||
assert_eq!(to_unit(1500.0, "percent", 3000.0, 300.0), 50.0);
|
||||
assert_eq!(to_unit(600.0, "in", 3000.0, 300.0), 2.0);
|
||||
assert_eq!(to_unit(1500.0, "percent", 3000.0, 300.0, 72.0), 50.0);
|
||||
assert_eq!(to_unit(600.0, "in", 3000.0, 300.0, 72.0), 2.0);
|
||||
assert_eq!(human_bytes(6016.0 * 6016.0 * 4.0), "138.1M");
|
||||
}
|
||||
|
||||
/// #5756: points and picas use the Point/Pica Size preference (Traditional is 72.27 pt/in),
|
||||
/// and a dialog without the field (or with a bad one) falls back to PostScript's 72.
|
||||
#[test]
|
||||
fn points_and_picas_use_the_points_per_inch() {
|
||||
assert!((to_unit(72.0, "pt", 72.0, 72.0, 72.27) - 72.27).abs() < 1e-9);
|
||||
assert!((to_unit(72.0, "pica", 72.0, 72.0, 72.27) - 72.27 / 12.0).abs() < 1e-9);
|
||||
assert_eq!(to_unit(72.0, "pt", 72.0, 72.0, 72.0), 72.0);
|
||||
assert_eq!(to_unit(72.0, "pica", 72.0, 72.0, 72.0), 6.0);
|
||||
assert!((from_unit(72.27, "pt", 72.0, 72.0, 72.27) - 72.0).abs() < 1e-9);
|
||||
let mut f = Map::new();
|
||||
assert_eq!(pt_per_in(&f), 72.0);
|
||||
for bad in [json!(0.0), json!(-3.0), json!("x")] {
|
||||
f.insert("__ptPerIn".into(), bad);
|
||||
assert_eq!(pt_per_in(&f), 72.0);
|
||||
}
|
||||
f.insert("__ptPerIn".into(), json!(72.27));
|
||||
assert_eq!(pt_per_in(&f), 72.27);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dialog_maps_to_engine_command() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
@@ -547,6 +577,33 @@ mod tests {
|
||||
(h, id)
|
||||
}
|
||||
|
||||
/// #5756: points and picas follow Preferences › Units & Rulers › Point/Pica Size. With
|
||||
/// Traditional (72.27 pt/in) and Resample off, 100 px typed as 72.27 pt wide is 100 ppi.
|
||||
#[test]
|
||||
fn traditional_point_size_drives_points_and_picas() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.session.execute("file.new", json!({"width": 100, "height": 50, "resolution": 72})).unwrap();
|
||||
app.session.execute("prefs.set", json!({"path": "unitsAndRulers.pointSize", "value": "traditional"})).unwrap();
|
||||
for command in ["image.imageSize", "image.canvasSize"] {
|
||||
let id = open(&mut app, command).unwrap();
|
||||
assert_eq!(app.ui.dialog_mut(id).unwrap().fields.get("__ptPerIn"), Some(&json!(72.27)), "{command}");
|
||||
app.ui.close_dialog(id);
|
||||
}
|
||||
let mut h = egui_kittest::Harness::builder()
|
||||
.with_size(egui::vec2(1400.0, 900.0))
|
||||
.build_ui_state(|ui, app: &mut PhotocraftApp| crate::dialogs::show(app, ui.ctx()), app);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
let id = open(h.state_mut(), "image.imageSize").unwrap();
|
||||
let d = h.state_mut().ui.dialog_mut(id).unwrap();
|
||||
d.fields.insert("resample".into(), json!("none"));
|
||||
d.fields.insert("__unit".into(), json!("pt"));
|
||||
h.run_steps(3);
|
||||
type_width(&mut h, "72.27");
|
||||
let f = fields(&h);
|
||||
assert_eq!((num(&f, "width"), num(&f, "height")), (100.0, 50.0), "{f:?}");
|
||||
assert!((num(&f, "resolution") - 100.0).abs() < 1e-2, "{f:?}");
|
||||
}
|
||||
|
||||
/// Types `text` into the Width field and leaves it with Tab.
|
||||
fn type_width(h: &mut egui_kittest::Harness<'static, PhotocraftApp>, text: &str) {
|
||||
use egui::accesskit::Role;
|
||||
|
||||
Reference in new issue
Block a user