| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use smithay::utils::{Physical, Raw, Size}; |
| 7 | |
| 8 | const MIN_SCALE: i32 = 1; |
| 9 | const MAX_SCALE: i32 = 4; |
| 10 | const STEPS: i32 = 4; |
| 11 | const MIN_LOGICAL_AREA: i32 = 800 * 480; |
| 12 | |
| 13 | const MOBILE_TARGET_DPI: f64 = 135.; |
| 14 | const LARGE_TARGET_DPI: f64 = 110.; |
| 15 | const LARGE_MIN_SIZE_INCHES: f64 = 20.; |
| 16 | |
| 17 | |
| 18 | pub fn guess_monitor_scale(size_mm: Size<i32, Raw>, resolution: Size<i32, Physical>) -> f64 { |
| 19 | if size_mm.w == 0 || size_mm.h == 0 { |
| 20 | return 1.; |
| 21 | } |
| 22 | |
| 23 | let diag_inches = f64::from(size_mm.w * size_mm.w + size_mm.h * size_mm.h).sqrt() / 25.4; |
| 24 | |
| 25 | let target_dpi = if diag_inches < LARGE_MIN_SIZE_INCHES { |
| 26 | MOBILE_TARGET_DPI |
| 27 | } else { |
| 28 | LARGE_TARGET_DPI |
| 29 | }; |
| 30 | |
| 31 | let physical_dpi = |
| 32 | f64::from(resolution.w * resolution.w + resolution.h * resolution.h).sqrt() / diag_inches; |
| 33 | let perfect_scale = physical_dpi / target_dpi; |
| 34 | |
| 35 | supported_scales(resolution) |
| 36 | .map(|scale| (scale, (scale - perfect_scale).abs())) |
| 37 | .min_by(|a, b| a.1.partial_cmp(&b.1).unwrap()) |
| 38 | .map_or(1., |(scale, _)| scale) |
| 39 | } |
| 40 | |
| 41 | pub fn supported_scales(resolution: Size<i32, Physical>) -> impl Iterator<Item = f64> { |
| 42 | (MIN_SCALE * STEPS..=MAX_SCALE * STEPS) |
| 43 | .map(|x| f64::from(x) / f64::from(STEPS)) |
| 44 | .filter(move |scale| is_valid_for_resolution(resolution, *scale)) |
| 45 | } |
| 46 | |
| 47 | fn is_valid_for_resolution(resolution: Size<i32, Physical>, scale: f64) -> bool { |
| 48 | let logical = resolution.to_f64().to_logical(scale).to_i32_round::<i32>(); |
| 49 | logical.w * logical.h >= MIN_LOGICAL_AREA |
| 50 | } |
| 51 | |
| 52 | |
| 53 | pub fn closest_representable_scale(scale: f64) -> f64 { |
| 54 | |
| 55 | const FRACTIONAL_SCALE_DENOM: f64 = 120.; |
| 56 | |
| 57 | (scale * FRACTIONAL_SCALE_DENOM).round() / FRACTIONAL_SCALE_DENOM |
| 58 | } |
| 59 | |
| 60 | #[cfg(test)] |
| 61 | mod tests { |
| 62 | use insta::assert_snapshot; |
| 63 | |
| 64 | use super::*; |
| 65 | |
| 66 | fn check(size_mm: (i32, i32), resolution: (i32, i32)) -> f64 { |
| 67 | guess_monitor_scale(Size::from(size_mm), Size::from(resolution)) |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn test_guess_monitor_scale() { |
| 72 | |
| 73 | assert_snapshot!(check((65, 129), (720, 1440)), @"1.5"); |
| 74 | |
| 75 | assert_snapshot!(check((68, 144), (1080, 2280)), @"2.5"); |
| 76 | |
| 77 | assert_snapshot!(check((64, 142), (1080, 2400)), @"2.5"); |
| 78 | |
| 79 | assert_snapshot!(check((286, 179), (2560, 1600)), @"1.75"); |
| 80 | |
| 81 | assert_snapshot!(check((303, 202), (2400, 1600)), @"1.5"); |
| 82 | |
| 83 | assert_snapshot!(check((290, 180), (3840, 2400)), @"2.5"); |
| 84 | |
| 85 | assert_snapshot!(check((300, 190), (3840, 2400)), @"2.5"); |
| 86 | |
| 87 | assert_snapshot!(check((509, 286), (1920, 1080)), @"1"); |
| 88 | |
| 89 | assert_snapshot!(check((509, 286), (3840, 2160)), @"1.75"); |
| 90 | |
| 91 | assert_snapshot!(check((598, 336), (3840, 2160)), @"1.5"); |
| 92 | |
| 93 | assert_snapshot!(check((708, 398), (3840, 2160)), @"1.25"); |
| 94 | |
| 95 | assert_snapshot!(check((554, 312), (3840, 2160)), @"1.5"); |
| 96 | |
| 97 | assert_snapshot!(check((522, 294), (3840, 2160)), @"1.75"); |
| 98 | |
| 99 | assert_snapshot!(check((340, 210), (2560, 1600)), @"1.5"); |
| 100 | |
| 101 | assert_snapshot!(check((700, 390), (2560, 1440)), @"1"); |
| 102 | |
| 103 | assert_snapshot!(check((260, 170), (2736, 1824)), @"2"); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn guess_monitor_scale_unknown_size() { |
| 108 | assert_eq!(check((0, 0), (1920, 1080)), 1.); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_round_scale() { |
| 113 | assert_snapshot!(closest_representable_scale(1.3), @"1.3"); |
| 114 | assert_snapshot!(closest_representable_scale(1.31), @"1.3083333333333333"); |
| 115 | assert_snapshot!(closest_representable_scale(1.32), @"1.3166666666666667"); |
| 116 | assert_snapshot!(closest_representable_scale(1.33), @"1.3333333333333333"); |
| 117 | assert_snapshot!(closest_representable_scale(1.34), @"1.3416666666666666"); |
| 118 | assert_snapshot!(closest_representable_scale(1.35), @"1.35"); |
| 119 | } |
| 120 | } |