fix(view): match actual size metrics to display axes and backing pixels

This commit is contained in:
Terra854 committed 2026-12-02 07:11:51 +08:00
1 parent 91855750a9
commit 40ea294a22
2 files changed
+69 -12

No files matched your search

@@ -92,6 +92,13 @@ fn display_ppi(ctx: &egui::Context) -> Option<f64> {
fn windows_monitor_width(device: &str) -> Option<f64> {
use winsafe::{HKEY, RegistryValue, co};
// Without a current orientation, a rotated desktop width cannot be paired with an EDID axis safely.
let mut mode = winsafe::DEVMODE::default();
if !winsafe::EnumDisplaySettingsEx(Some(device), winsafe::GmidxEnum::Enum(co::ENUM_SETTINGS::CURRENT), &mut mode, co::EDS::default()).ok()?
|| !mode.dmFields.has(co::DM::DISPLAYORIENTATION)
{
return None;
}
let mut result = None;
for monitor in winsafe::EnumDisplayDevices(Some(device), Some(co::EDD::GET_DEVICE_INTERFACE_NAME)).take(64) {
let monitor = monitor.ok()?;
@@ -104,7 +111,7 @@ fn windows_monitor_width(device: &str) -> Option<f64> {
_ => windows_edid_value(&key, "EDID").ok()?,
};
let RegistryValue::Binary(edid) = value else { return None };
let width = edid_width(&edid)?;
let width = oriented_width(edid_size(&edid)?, mode.dmDisplayOrientation().raw())?;
// Mirrored outputs must agree on physical size before one scale can describe both.
if result.is_some_and(|previous: f64| (previous - width).abs() > 0.5) {
return None;
@@ -146,7 +153,7 @@ pub(super) fn monitor_registry_path(interface: &str) -> Option<String> {
}
#[cfg(any(test, target_os = "windows"))]
pub(super) fn edid_width(edid: &[u8]) -> Option<f64> {
pub(super) fn edid_size(edid: &[u8]) -> Option<[f64; 2]> {
let base = edid.get(..128)?;
if base.get(..8)? != [0, 255, 255, 255, 255, 255, 255, 0] || base.iter().fold(0u8, |sum, value| sum.wrapping_add(*value)) != 0 {
return None;
@@ -159,12 +166,31 @@ pub(super) fn edid_width(edid: &[u8]) -> Option<f64> {
let width = u16::from(*timing.get(12)?) | (u16::from(*timing.get(14)? & 0xf0) << 4);
let height = u16::from(*timing.get(13)?) | (u16::from(*timing.get(14)? & 0x0f) << 8);
if width > 0 && height > 0 {
return Some(f64::from(width));
return Some([f64::from(width), f64::from(height)]);
}
}
let width = *base.get(21)?;
let height = *base.get(22)?;
(width > 0 && height > 0).then_some(f64::from(width) * 10.0)
(width > 0 && height > 0).then_some([f64::from(width) * 10.0, f64::from(height) * 10.0])
}
/// EDID describes the unrotated panel; desktop horizontal pixels follow the current display orientation.
#[cfg(any(test, target_os = "windows"))]
pub(super) fn oriented_width(size: [f64; 2], quarter_turns: u32) -> Option<f64> {
if !size.iter().all(|value| value.is_finite() && *value > 0.0) {
return None;
}
match quarter_turns {
0 | 2 => Some(size[0]),
1 | 3 => Some(size[1]),
_ => None,
}
}
/// Missing backing-mode metrics must fall back, rather than substitute a logical display width.
#[cfg(any(test, target_os = "macos"))]
pub(super) fn backing_mode_ppi(width: Option<u64>, millimeters: f64) -> Option<f64> {
physical_ppi(width? as f64, millimeters)
}
#[cfg(target_os = "macos")]
@@ -182,7 +208,7 @@ fn display_ppi(ctx: &egui::Context) -> Option<f64> {
let height = (f64::from(window.bottom()) * factor).min(bounds.origin.y + bounds.size.height) - (f64::from(window.top()) * factor).max(bounds.origin.y);
let area = width.max(0.0) * height.max(0.0);
if area > best.0 {
best = (area, physical_ppi(display.pixels_wide() as f64, display.screen_size().width));
best = (area, backing_mode_ppi(display.display_mode().map(|mode| mode.pixel_width()), display.screen_size().width));
}
}
best.1
@@ -1,4 +1,4 @@
use super::actual_size::{apply, edid_width, monitor_registry_path, physical_ppi};
use super::actual_size::{apply, backing_mode_ppi, edid_size, monitor_registry_path, oriented_width, physical_ppi};
use crate::{PhotocraftApp, Services};
use photocraft_engine::Session;
use serde_json::json;
@@ -103,19 +103,50 @@ fn actual_size_reads_validated_edid_physical_dimensions() {
edid[67] = 30;
edid[68] = 0x11;
edid[127] = 0u8.wrapping_sub(edid.iter().copied().fold(0u8, u8::wrapping_add));
assert_eq!(edid_width(&edid), Some(509.0));
assert_eq!(physical_ppi(1920.0, edid_width(&edid).unwrap()), Some(95.0));
assert_eq!(edid_size(&edid), Some([509.0, 286.0]));
assert_eq!(physical_ppi(1920.0, oriented_width(edid_size(&edid).unwrap(), 0).unwrap()), Some(95.0));
for length in 0..128 {
assert_eq!(edid_width(&edid[..length]), None);
assert_eq!(edid_size(&edid[..length]), None);
}
edid[54] = 0;
edid[127] = edid[127].wrapping_add(1);
assert_eq!(edid_width(&edid), Some(510.0), "centimetres when detailed dimensions are absent");
assert_eq!(edid_size(&edid), Some([510.0, 290.0]), "centimetres when detailed dimensions are absent");
edid[21] = 0;
edid[127] = edid[127].wrapping_add(51);
assert_eq!(edid_width(&edid), None, "an aspect ratio has no physical width");
assert_eq!(edid_size(&edid), None, "an aspect ratio has no physical width");
edid[1] = 0;
assert_eq!(edid_width(&edid), None, "corrupt headers are unavailable");
assert_eq!(edid_size(&edid), None, "corrupt headers are unavailable");
}
#[test]
fn actual_size_matches_the_edid_axis_to_all_four_windows_orientations() {
let size = [600.0, 337.0];
for (orientation, pixels, millimeters) in [(0, 1920.0, 600.0), (1, 1080.0, 337.0), (2, 1920.0, 600.0), (3, 1080.0, 337.0)] {
let width = oriented_width(size, orientation).unwrap();
assert_eq!(width, millimeters);
assert_eq!(physical_ppi(pixels, width), Some(81.0));
}
assert_eq!(oriented_width(size, 4), None);
for invalid in [[0.0, 337.0], [600.0, f64::NAN], [f64::INFINITY, 337.0]] {
assert_eq!(oriented_width(invalid, 1), None);
}
}
#[test]
fn actual_size_uses_backing_mode_pixels_and_falls_back_without_a_usable_mode() {
let mut app = PhotocraftApp::new(Session::new(), Services::default());
app.run("file.new", json!({"width": 640, "height": 480})).unwrap();
let physical = backing_mode_ppi(Some(3024), 344.0);
assert_eq!(physical, Some(223.0), "backing width rather than a 1512-point logical mode");
assert_eq!(physical_ppi(1512.0, 344.0), Some(111.0));
assert_eq!(apply(&mut app, physical, &json!({})).unwrap()["fallback"], false);
for mode in [None, Some(0)] {
let missing = backing_mode_ppi(mode, 344.0);
assert_eq!(missing, None);
let fallback = apply(&mut app, missing, &json!({})).unwrap();
assert_eq!(fallback["fallback"], true);
assert!(fallback["status"].as_str().unwrap().contains("Screen Resolution"));
}
}
#[test]