const STANDARD_DPI: f32 = 96.0;
const PLAUSIBLE_DPI: core::ops::RangeInclusive<f32> = 24.0..=1536.0;
pub(super) fn scale_factor(resources: Option<&str>) -> f32 {
resources
.and_then(xft_dpi)
.map_or(1.0, |dpi| dpi / STANDARD_DPI)
}
fn xft_dpi(resources: &str) -> Option<f32> {
let value = resources
.lines()
.filter_map(|line| line.trim_start().strip_prefix("Xft.dpi"))
.filter_map(|rest| rest.trim_start().strip_prefix(':'))
.next_back()?;
let dpi: f32 = value.trim().parse().ok()?;
PLAUSIBLE_DPI.contains(&dpi).then_some(dpi)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_dpi_is_read_from_its_line() {
assert_eq!(xft_dpi("Xft.dpi:\t96"), Some(96.0));
assert_eq!(xft_dpi("Xft.dpi: 144\n"), Some(144.0));
assert_eq!(xft_dpi("Xft.dpi:192"), Some(192.0));
assert_eq!(xft_dpi(" Xft.dpi : 120 "), Some(120.0));
assert_eq!(xft_dpi("Xft.dpi:\t110.5"), Some(110.5));
}
#[test]
fn the_dpi_is_found_among_other_resources() {
let resources = "\
Xcursor.size:\t24
Xcursor.theme:\tAdwaita
Xft.antialias:\t1
Xft.dpi:\t168
Xft.hinting:\t1
Xft.rgba:\trgb
";
assert_eq!(xft_dpi(resources), Some(168.0));
assert_eq!(scale_factor(Some(resources)), 1.75);
}
#[test]
fn the_later_of_two_lines_wins() {
assert_eq!(xft_dpi("Xft.dpi: 96\nXft.dpi: 192\n"), Some(192.0));
assert_eq!(xft_dpi("Xft.dpi: 96\nXft.dpi: big\n"), None);
}
#[test]
fn other_names_are_not_the_dpi() {
for resources in [
"",
"Xft.dpix: 192",
"Xft.dpi.other: 192",
"MyXft.dpi: 192",
"! Xft.dpi: 192",
"*dpi: 192",
"Xft*dpi: 192",
"xft.dpi: 192",
"Xft.dpi 192",
"Xft.antialias: 1\nXcursor.size: 24",
] {
assert_eq!(xft_dpi(resources), None, "{resources:?}");
}
}
#[test]
fn a_value_that_is_not_a_plausible_density_is_ignored() {
for value in [
"", "abc", "96dpi", "0", "-96", "23.9", "1537", "1e30", "inf", "NaN", "96 96",
] {
let resources = format!("Xft.dpi: {value}");
assert_eq!(xft_dpi(&resources), None, "{value:?}");
assert_eq!(scale_factor(Some(&resources)), 1.0, "{value:?}");
}
assert_eq!(xft_dpi("Xft.dpi: 24"), Some(24.0));
assert_eq!(xft_dpi("Xft.dpi: 1536"), Some(1536.0));
}
#[test]
fn the_scale_factor_is_the_dpi_over_96() {
assert_eq!(scale_factor(Some("Xft.dpi: 96")), 1.0);
assert_eq!(scale_factor(Some("Xft.dpi: 144")), 1.5);
assert_eq!(scale_factor(Some("Xft.dpi: 192")), 2.0);
assert_eq!(scale_factor(Some("Xft.dpi: 72")), 0.75);
}
#[test]
fn without_resources_or_a_dpi_the_scale_factor_is_one() {
assert_eq!(scale_factor(None), 1.0);
assert_eq!(scale_factor(Some("")), 1.0);
assert_eq!(scale_factor(Some("Xcursor.size: 24")), 1.0);
}
}