1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::ops::Mul;
use crate::{One, Scale};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct Points;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct Pixels;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub struct Scaled;
#[derive(Debug, Clone, Copy)]
pub struct DisplayScale<T> {
pub(crate) total: Scale<T, Scaled, Pixels>,
pub(crate) dpi: Scale<T, Points, Pixels>,
pub(crate) additional: Scale<T, Scaled, Points>,
}
impl<T: Mul<T, Output = T> + Copy> DisplayScale<T> {
pub fn dpi_scale(&self) -> Scale<T, Points, Pixels> {
self.dpi
}
pub fn additional_scale(&self) -> Scale<T, Scaled, Points> {
self.additional
}
pub fn total_scale(&self) -> Scale<T, Scaled, Pixels> {
self.total
}
pub fn set_additional_scale(&mut self, scale: Scale<T, Scaled, Points>) {
self.additional = scale;
self.total = total_scale(self.dpi, self.additional);
}
pub fn set_dpi_scale(&mut self, scale: Scale<T, Points, Pixels>) {
self.dpi = scale;
self.total = total_scale(self.dpi, self.additional);
}
}
impl<T: Mul<T, Output = T> + Copy> DisplayScale<T> {
pub fn new(
dpi: Scale<T, Points, Pixels>,
additional_scaling: Scale<T, Scaled, Points>,
) -> Self {
Self {
dpi,
additional: additional_scaling,
total: total_scale(dpi, additional_scaling),
}
}
}
fn total_scale<T: Mul<T, Output = T> + Copy>(
dpi: Scale<T, Points, Pixels>,
additional_scaling: Scale<T, Scaled, Points>,
) -> Scale<T, Scaled, Pixels> {
Scale::new(dpi.get() * additional_scaling.get())
}
impl<T> One for DisplayScale<T>
where
T: num_traits::One + Mul<T, Output = T> + Copy,
{
fn one() -> Self {
Self::new(Scale::one(), Scale::one())
}
}
pub trait Displayable<T> {
type Pixels: Displayable<T>;
type Points: Displayable<T>;
type Scaled: Displayable<T>;
fn to_pixels(&self, scale: &DisplayScale<T>) -> Self::Pixels;
fn to_points(&self, scale: &DisplayScale<T>) -> Self::Points;
fn to_scaled(&self, scale: &DisplayScale<T>) -> Self::Scaled;
}