@@ 94,6 94,9 @@ where
/// Chooses the best spot in the storage to put a rect of the given dimensions, or returns `None` if it can't fit.
fn find_new_rect(&mut self, width: f32, height: f32) -> Option<Rect> {
// Ohai an easy case
+ if width < 0.0 || height < 0.0 {
+ return None;
+ }
if width >= self.storage_dims || height >= self.storage_dims {
return None;
}
@@ 180,8 183,19 @@ mod tests {
fn write_bytes_to(&mut self, _rect: Rect, _bytes: &[u8]) -> Result<(), ()> {
todo!()
}
- fn resize(&mut self, _new_w: f32, _new_h: f32) {
- todo!()
+ fn resize(&mut self, new_w: f32, new_h: f32) {
+ assert!(new_w == new_h);
+ let new_w = (new_w + 1.0) as usize;
+ let new_h = (new_h + 1.0) as usize;
+ let mut new_contents = vec![0; new_w * new_h];
+ // slow but simple
+ for y in 0..self.size {
+ for x in 0..self.size {
+ new_contents[x + y * new_w] = self.contents[x + y * self.size];
+ }
+ }
+ self.contents = new_contents;
+ self.size = new_w;
}
}
@@ 230,9 244,98 @@ mod tests {
h: 10.0
}
);
+
+ // Do we recognize we're out of space?
let r4 = g.find_new_rect(10.0, 10.0);
assert!(r4.is_none());
+ // Can we still insert little things?
+ let r5 = g.find_new_rect(1.0, 1.0).unwrap();
+ assert_eq!(
+ r5,
+ Rect {
+ x: 12.0,
+ y: 0.0,
+ w: 1.0,
+ h: 1.0
+ }
+ );
+
+ // Do we now wrap around to the next row?
+ let r6 = g.find_new_rect(8.0, 3.0).unwrap();
+ assert_eq!(
+ r6,
+ Rect {
+ x: 0.0,
+ y: 10.0,
+ w: 8.0,
+ h: 3.0
+ }
+ );
+ }
+
+ #[test]
+ fn rect_nonsense_1() {
+ let s = St::new(16.0);
+ let mut g = Glif::new(s, 16.0);
+ assert!(g.find_new_rect(17.0, 17.0).is_none());
+ }
+
+ #[test]
+ fn rect_nonsense_2() {
+ let s = St::new(16.0);
+ let mut g = Glif::new(s, 16.0);
+ assert!(g.find_new_rect(-7.0, 3.0).is_none());
+ }
+
+ #[test]
+ fn rect_making_1() {
+ let s = St::new(16.0);
+ let mut g = Glif::new(s, 16.0);
+ let r1 = g.make_new_rect(1.0, 1.0);
+ assert_eq!(
+ r1,
+ Rect {
+ x: 0.0,
+ y: 0.0,
+ w: 1.0,
+ h: 1.0
+ }
+ );
+
+ let r2 = g.make_new_rect(1.0, 1.0);
+ assert_eq!(
+ r2,
+ Rect {
+ x: 1.0,
+ y: 0.0,
+ w: 1.0,
+ h: 1.0
+ }
+ );
+ let r3 = g.make_new_rect(10.0, 10.0);
+ assert_eq!(
+ r3,
+ Rect {
+ x: 2.0,
+ y: 0.0,
+ w: 10.0,
+ h: 10.0
+ }
+ );
+
+ // Now, do we resize the storage and select a new spot?
+ let r4 = g.make_new_rect(10.0, 10.0);
+ assert_eq!(
+ r4,
+ Rect {
+ x: 0.0,
+ y: 10.0,
+ w: 10.0,
+ h: 10.0
+ }
+ );
+
let r5 = g.find_new_rect(1.0, 1.0).unwrap();
assert_eq!(
r5,
@@ 244,11 347,4 @@ mod tests {
}
);
}
-
- #[test]
- fn rect_nonsense_1() {
- let s = St::new(16.0);
- let mut g = Glif::new(s, 16.0);
- assert!(g.find_new_rect(17.0, 17.0).is_none());
- }
}