| 1 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 2 | pub struct EmbedBounds { |
| 3 | pub x: i32, |
| 4 | pub y: i32, |
| 5 | pub width: i32, |
| 6 | pub height: i32, |
| 7 | } |
| 8 | |
| 9 | impl EmbedBounds { |
| 10 | pub fn from_logical(x: f32, y: f32, width: f32, height: f32, scale: f32) -> Self { |
| 11 | let physical = |value: f32| (value * scale).round() as i32; |
| 12 | EmbedBounds { |
| 13 | x: physical(x), |
| 14 | y: physical(y), |
| 15 | width: physical(width), |
| 16 | height: physical(height), |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub fn has_area(self) -> bool { |
| 21 | self.width > 0 && self.height > 0 |
| 22 | } |
| 23 | |
| 24 | fn right(self) -> i32 { |
| 25 | self.x + self.width |
| 26 | } |
| 27 | |
| 28 | fn bottom(self) -> i32 { |
| 29 | self.y + self.height |
| 30 | } |
| 31 | |
| 32 | fn within(self, outer: EmbedBounds) -> Option<EmbedBounds> { |
| 33 | let left = self.x.max(outer.x); |
| 34 | let top = self.y.max(outer.y); |
| 35 | let right = self.right().min(outer.right()); |
| 36 | let bottom = self.bottom().min(outer.bottom()); |
| 37 | (right > left && bottom > top).then_some(EmbedBounds { |
| 38 | x: left, |
| 39 | y: top, |
| 40 | width: right - left, |
| 41 | height: bottom - top, |
| 42 | }) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// Cutouts clipped to the content rectangle, relative to its top-left corner. |
| 47 | pub fn local_cutouts(content: EmbedBounds, cutouts: &[EmbedBounds]) -> Vec<EmbedBounds> { |
| 48 | cutouts |
| 49 | .iter() |
| 50 | .filter_map(|cutout| cutout.within(content)) |
| 51 | .map(|clipped| EmbedBounds { |
| 52 | x: clipped.x - content.x, |
| 53 | y: clipped.y - content.y, |
| 54 | ..clipped |
| 55 | }) |
| 56 | .collect() |
| 57 | } |
| 58 | |
| 59 | #[cfg(test)] |
| 60 | mod tests { |
| 61 | use super::{EmbedBounds, local_cutouts}; |
| 62 | |
| 63 | fn bounds(x: i32, y: i32, width: i32, height: i32) -> EmbedBounds { |
| 64 | EmbedBounds { |
| 65 | x, |
| 66 | y, |
| 67 | width, |
| 68 | height, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn logical_bounds_scale_to_physical_pixels() { |
| 74 | assert_eq!( |
| 75 | EmbedBounds::from_logical(10., 20.5, 100., 50., 1.5), |
| 76 | bounds(15, 31, 150, 75) |
| 77 | ); |
| 78 | assert_eq!( |
| 79 | EmbedBounds::from_logical(10., 20., 100., 50., 1.), |
| 80 | bounds(10, 20, 100, 50) |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn a_cutout_inside_the_content_becomes_content_relative() { |
| 86 | let content = bounds(100, 50, 800, 600); |
| 87 | assert_eq!( |
| 88 | local_cutouts(content, &[bounds(500, 400, 200, 100)]), |
| 89 | vec![bounds(400, 350, 200, 100)] |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn a_cutout_sticking_out_is_clipped_to_the_content() { |
| 95 | let content = bounds(100, 50, 800, 600); |
| 96 | assert_eq!( |
| 97 | local_cutouts(content, &[bounds(850, 600, 200, 200)]), |
| 98 | vec![bounds(750, 550, 50, 50)] |
| 99 | ); |
| 100 | assert_eq!( |
| 101 | local_cutouts(content, &[bounds(0, 0, 150, 100)]), |
| 102 | vec![bounds(0, 0, 50, 50)] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn cutouts_outside_or_empty_are_dropped() { |
| 108 | let content = bounds(100, 50, 800, 600); |
| 109 | assert!(local_cutouts(content, &[bounds(0, 0, 50, 50)]).is_empty()); |
| 110 | assert!(local_cutouts(content, &[bounds(100, 650, 100, 20)]).is_empty()); |
| 111 | assert!(local_cutouts(content, &[bounds(200, 200, 0, 40)]).is_empty()); |
| 112 | } |
| 113 | |
| 114 | #[test] |
| 115 | fn scaled_cutouts_line_up_with_scaled_content() { |
| 116 | let scale = 1.25; |
| 117 | let content = EmbedBounds::from_logical(240., 80., 600., 400., scale); |
| 118 | let toast = EmbedBounds::from_logical(660., 380., 180., 100., scale); |
| 119 | assert_eq!( |
| 120 | local_cutouts(content, &[toast]), |
| 121 | vec![bounds(525, 375, 225, 125)] |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | #[test] |
| 126 | fn has_area_needs_both_sides() { |
| 127 | assert!(bounds(0, 0, 1, 1).has_area()); |
| 128 | assert!(!bounds(0, 0, 0, 10).has_area()); |
| 129 | assert!(!bounds(0, 0, 10, 0).has_area()); |
| 130 | } |
| 131 | } |