dpi.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. use serde::{Deserialize, Serialize};
  5. pub trait Pixel: Copy + Into<f64> {
  6. fn from_f64(f: f64) -> Self;
  7. fn cast<P: Pixel>(self) -> P {
  8. P::from_f64(self.into())
  9. }
  10. }
  11. impl Pixel for u8 {
  12. fn from_f64(f: f64) -> Self {
  13. f.round() as u8
  14. }
  15. }
  16. impl Pixel for u16 {
  17. fn from_f64(f: f64) -> Self {
  18. f.round() as u16
  19. }
  20. }
  21. impl Pixel for u32 {
  22. fn from_f64(f: f64) -> Self {
  23. f.round() as u32
  24. }
  25. }
  26. impl Pixel for i8 {
  27. fn from_f64(f: f64) -> Self {
  28. f.round() as i8
  29. }
  30. }
  31. impl Pixel for i16 {
  32. fn from_f64(f: f64) -> Self {
  33. f.round() as i16
  34. }
  35. }
  36. impl Pixel for i32 {
  37. fn from_f64(f: f64) -> Self {
  38. f.round() as i32
  39. }
  40. }
  41. impl Pixel for f32 {
  42. fn from_f64(f: f64) -> Self {
  43. f as f32
  44. }
  45. }
  46. impl Pixel for f64 {
  47. fn from_f64(f: f64) -> Self {
  48. f
  49. }
  50. }
  51. /// Checks that the scale factor is a normal positive `f64`.
  52. ///
  53. /// All functions that take a scale factor assert that this will return `true`. If you're sourcing scale factors from
  54. /// anywhere other than tao, it's recommended to validate them using this function before passing them to tao;
  55. /// otherwise, you risk panics.
  56. #[inline]
  57. pub fn validate_scale_factor(scale_factor: f64) -> bool {
  58. scale_factor.is_sign_positive() && scale_factor.is_normal()
  59. }
  60. /// A position represented in logical pixels.
  61. ///
  62. /// The position is stored as floats, so please be careful. Casting floats to integers truncates the
  63. /// fractional part, which can cause noticeable issues. To help with that, an `Into<(i32, i32)>`
  64. /// implementation is provided which does the rounding for you.
  65. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Serialize, Deserialize)]
  66. pub struct LogicalPosition<P> {
  67. pub x: P,
  68. pub y: P,
  69. }
  70. impl<P> LogicalPosition<P> {
  71. #[inline]
  72. pub const fn new(x: P, y: P) -> Self {
  73. LogicalPosition { x, y }
  74. }
  75. }
  76. impl<P: Pixel> LogicalPosition<P> {
  77. #[inline]
  78. pub fn from_physical<T: Into<PhysicalPosition<X>>, X: Pixel>(
  79. physical: T,
  80. scale_factor: f64,
  81. ) -> Self {
  82. physical.into().to_logical(scale_factor)
  83. }
  84. #[inline]
  85. pub fn to_physical<X: Pixel>(&self, scale_factor: f64) -> PhysicalPosition<X> {
  86. assert!(validate_scale_factor(scale_factor));
  87. let x = self.x.into() * scale_factor;
  88. let y = self.y.into() * scale_factor;
  89. PhysicalPosition::new(x, y).cast()
  90. }
  91. #[inline]
  92. pub fn cast<X: Pixel>(&self) -> LogicalPosition<X> {
  93. LogicalPosition {
  94. x: self.x.cast(),
  95. y: self.y.cast(),
  96. }
  97. }
  98. }
  99. impl<P: Pixel, X: Pixel> From<(X, X)> for LogicalPosition<P> {
  100. fn from((x, y): (X, X)) -> LogicalPosition<P> {
  101. LogicalPosition::new(x.cast(), y.cast())
  102. }
  103. }
  104. impl<P: Pixel, X: Pixel> From<LogicalPosition<P>> for (X, X) {
  105. fn from(pos: LogicalPosition<P>) -> Self {
  106. (pos.x.cast(), pos.y.cast())
  107. }
  108. }
  109. impl<P: Pixel, X: Pixel> From<[X; 2]> for LogicalPosition<P> {
  110. fn from([x, y]: [X; 2]) -> LogicalPosition<P> {
  111. LogicalPosition::new(x.cast(), y.cast())
  112. }
  113. }
  114. impl<P: Pixel, X: Pixel> From<LogicalPosition<P>> for [X; 2] {
  115. fn from(pos: LogicalPosition<P>) -> Self {
  116. [pos.x.cast(), pos.y.cast()]
  117. }
  118. }
  119. /// A position represented in physical pixels.
  120. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Serialize, Deserialize)]
  121. pub struct PhysicalPosition<P> {
  122. pub x: P,
  123. pub y: P,
  124. }
  125. impl<P> PhysicalPosition<P> {
  126. #[inline]
  127. pub const fn new(x: P, y: P) -> Self {
  128. PhysicalPosition { x, y }
  129. }
  130. }
  131. impl<P: Pixel> PhysicalPosition<P> {
  132. #[inline]
  133. pub fn from_logical<T: Into<LogicalPosition<X>>, X: Pixel>(
  134. logical: T,
  135. scale_factor: f64,
  136. ) -> Self {
  137. logical.into().to_physical(scale_factor)
  138. }
  139. #[inline]
  140. pub fn to_logical<X: Pixel>(&self, scale_factor: f64) -> LogicalPosition<X> {
  141. assert!(validate_scale_factor(scale_factor));
  142. let x = self.x.into() / scale_factor;
  143. let y = self.y.into() / scale_factor;
  144. LogicalPosition::new(x, y).cast()
  145. }
  146. #[inline]
  147. pub fn cast<X: Pixel>(&self) -> PhysicalPosition<X> {
  148. PhysicalPosition {
  149. x: self.x.cast(),
  150. y: self.y.cast(),
  151. }
  152. }
  153. }
  154. impl<P: Pixel, X: Pixel> From<(X, X)> for PhysicalPosition<P> {
  155. fn from((x, y): (X, X)) -> PhysicalPosition<P> {
  156. PhysicalPosition::new(x.cast(), y.cast())
  157. }
  158. }
  159. impl<P: Pixel, X: Pixel> From<PhysicalPosition<P>> for (X, X) {
  160. fn from(pos: PhysicalPosition<P>) -> Self {
  161. (pos.x.cast(), pos.y.cast())
  162. }
  163. }
  164. impl<P: Pixel, X: Pixel> From<[X; 2]> for PhysicalPosition<P> {
  165. fn from([x, y]: [X; 2]) -> PhysicalPosition<P> {
  166. PhysicalPosition::new(x.cast(), y.cast())
  167. }
  168. }
  169. impl<P: Pixel, X: Pixel> From<PhysicalPosition<P>> for [X; 2] {
  170. fn from(pos: PhysicalPosition<P>) -> Self {
  171. [pos.x.cast(), pos.y.cast()]
  172. }
  173. }
  174. /// A size represented in logical pixels.
  175. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Serialize, Deserialize)]
  176. pub struct LogicalSize<P> {
  177. pub width: P,
  178. pub height: P,
  179. }
  180. impl<P> LogicalSize<P> {
  181. #[inline]
  182. pub const fn new(width: P, height: P) -> Self {
  183. LogicalSize { width, height }
  184. }
  185. }
  186. impl<P: Pixel> LogicalSize<P> {
  187. #[inline]
  188. pub fn from_physical<T: Into<PhysicalSize<X>>, X: Pixel>(physical: T, scale_factor: f64) -> Self {
  189. physical.into().to_logical(scale_factor)
  190. }
  191. #[inline]
  192. pub fn to_physical<X: Pixel>(&self, scale_factor: f64) -> PhysicalSize<X> {
  193. assert!(validate_scale_factor(scale_factor));
  194. let width = self.width.into() * scale_factor;
  195. let height = self.height.into() * scale_factor;
  196. PhysicalSize::new(width, height).cast()
  197. }
  198. #[inline]
  199. pub fn cast<X: Pixel>(&self) -> LogicalSize<X> {
  200. LogicalSize {
  201. width: self.width.cast(),
  202. height: self.height.cast(),
  203. }
  204. }
  205. }
  206. impl<P: Pixel, X: Pixel> From<(X, X)> for LogicalSize<P> {
  207. fn from((x, y): (X, X)) -> LogicalSize<P> {
  208. LogicalSize::new(x.cast(), y.cast())
  209. }
  210. }
  211. impl<P: Pixel, X: Pixel> From<LogicalSize<P>> for (X, X) {
  212. fn from(size: LogicalSize<P>) -> Self {
  213. (size.width.cast(), size.height.cast())
  214. }
  215. }
  216. impl<P: Pixel, X: Pixel> From<[X; 2]> for LogicalSize<P> {
  217. fn from([x, y]: [X; 2]) -> LogicalSize<P> {
  218. LogicalSize::new(x.cast(), y.cast())
  219. }
  220. }
  221. impl<P: Pixel, X: Pixel> From<LogicalSize<P>> for [X; 2] {
  222. fn from(size: LogicalSize<P>) -> Self {
  223. [size.width.cast(), size.height.cast()]
  224. }
  225. }
  226. /// A size represented in physical pixels.
  227. #[derive(Debug, Copy, Clone, Eq, PartialEq, Default, Hash, Serialize, Deserialize)]
  228. pub struct PhysicalSize<P> {
  229. pub width: P,
  230. pub height: P,
  231. }
  232. impl<P> PhysicalSize<P> {
  233. #[inline]
  234. pub const fn new(width: P, height: P) -> Self {
  235. PhysicalSize { width, height }
  236. }
  237. }
  238. impl<P: Pixel> PhysicalSize<P> {
  239. #[inline]
  240. pub fn from_logical<T: Into<LogicalSize<X>>, X: Pixel>(logical: T, scale_factor: f64) -> Self {
  241. logical.into().to_physical(scale_factor)
  242. }
  243. #[inline]
  244. pub fn to_logical<X: Pixel>(&self, scale_factor: f64) -> LogicalSize<X> {
  245. assert!(validate_scale_factor(scale_factor));
  246. let width = self.width.into() / scale_factor;
  247. let height = self.height.into() / scale_factor;
  248. LogicalSize::new(width, height).cast()
  249. }
  250. #[inline]
  251. pub fn cast<X: Pixel>(&self) -> PhysicalSize<X> {
  252. PhysicalSize {
  253. width: self.width.cast(),
  254. height: self.height.cast(),
  255. }
  256. }
  257. }
  258. impl<P: Pixel, X: Pixel> From<(X, X)> for PhysicalSize<P> {
  259. fn from((x, y): (X, X)) -> PhysicalSize<P> {
  260. PhysicalSize::new(x.cast(), y.cast())
  261. }
  262. }
  263. impl<P: Pixel, X: Pixel> From<PhysicalSize<P>> for (X, X) {
  264. fn from(size: PhysicalSize<P>) -> Self {
  265. (size.width.cast(), size.height.cast())
  266. }
  267. }
  268. impl<P: Pixel, X: Pixel> From<[X; 2]> for PhysicalSize<P> {
  269. fn from([x, y]: [X; 2]) -> PhysicalSize<P> {
  270. PhysicalSize::new(x.cast(), y.cast())
  271. }
  272. }
  273. impl<P: Pixel, X: Pixel> From<PhysicalSize<P>> for [X; 2] {
  274. fn from(size: PhysicalSize<P>) -> Self {
  275. [size.width.cast(), size.height.cast()]
  276. }
  277. }
  278. /// A size that's either physical or logical.
  279. #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
  280. #[serde(tag = "type", content = "data")]
  281. pub enum Size {
  282. Physical(PhysicalSize<u32>),
  283. Logical(LogicalSize<f64>),
  284. }
  285. impl Size {
  286. pub fn new<S: Into<Size>>(size: S) -> Size {
  287. size.into()
  288. }
  289. pub fn to_logical<P: Pixel>(&self, scale_factor: f64) -> LogicalSize<P> {
  290. match *self {
  291. Size::Physical(size) => size.to_logical(scale_factor),
  292. Size::Logical(size) => size.cast(),
  293. }
  294. }
  295. pub fn to_physical<P: Pixel>(&self, scale_factor: f64) -> PhysicalSize<P> {
  296. match *self {
  297. Size::Physical(size) => size.cast(),
  298. Size::Logical(size) => size.to_physical(scale_factor),
  299. }
  300. }
  301. }
  302. impl<P: Pixel> From<PhysicalSize<P>> for Size {
  303. #[inline]
  304. fn from(size: PhysicalSize<P>) -> Size {
  305. Size::Physical(size.cast())
  306. }
  307. }
  308. impl<P: Pixel> From<LogicalSize<P>> for Size {
  309. #[inline]
  310. fn from(size: LogicalSize<P>) -> Size {
  311. Size::Logical(size.cast())
  312. }
  313. }
  314. /// A position that's either physical or logical.
  315. #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
  316. #[serde(tag = "type", content = "data")]
  317. pub enum Position {
  318. Physical(PhysicalPosition<i32>),
  319. Logical(LogicalPosition<f64>),
  320. }
  321. impl Position {
  322. pub fn new<S: Into<Position>>(position: S) -> Position {
  323. position.into()
  324. }
  325. pub fn to_logical<P: Pixel>(&self, scale_factor: f64) -> LogicalPosition<P> {
  326. match *self {
  327. Position::Physical(position) => position.to_logical(scale_factor),
  328. Position::Logical(position) => position.cast(),
  329. }
  330. }
  331. pub fn to_physical<P: Pixel>(&self, scale_factor: f64) -> PhysicalPosition<P> {
  332. match *self {
  333. Position::Physical(position) => position.cast(),
  334. Position::Logical(position) => position.to_physical(scale_factor),
  335. }
  336. }
  337. }
  338. impl<P: Pixel> From<PhysicalPosition<P>> for Position {
  339. #[inline]
  340. fn from(position: PhysicalPosition<P>) -> Position {
  341. Position::Physical(position.cast())
  342. }
  343. }
  344. impl<P: Pixel> From<LogicalPosition<P>> for Position {
  345. #[inline]
  346. fn from(position: LogicalPosition<P>) -> Position {
  347. Position::Logical(position.cast())
  348. }
  349. }