Fix crash when printing some text

This commit is contained in:
Joscha 2024-03-17 17:35:44 +01:00
parent 4f3d66f6a0
commit d69557daf6
2 changed files with 32 additions and 10 deletions

View file

@ -20,12 +20,24 @@ impl Vec2 {
Self { x, y }
}
pub fn from_u32_checked(x: u32, y: u32) -> Option<Self> {
let x: i32 = x.try_into().ok()?;
let y: i32 = y.try_into().ok()?;
Some(Self::new(x, y))
}
pub fn from_u32(x: u32, y: u32) -> Self {
let x: i32 = x.try_into().expect("x too large");
let y: i32 = y.try_into().expect("y too large");
Self::new(x, y)
}
pub fn to_u32_checked(self) -> Option<(u32, u32)> {
let x: u32 = self.x.try_into().ok()?;
let y: u32 = self.y.try_into().ok()?;
Some((x, y))
}
pub fn to_u32(self) -> (u32, u32) {
let x: u32 = self.x.try_into().expect("x too small");
let y: u32 = self.y.try_into().expect("y too small");

View file

@ -40,24 +40,34 @@ impl<'a> View<'a> {
}
pub fn get(&self, pos: Vec2) -> Option<Srgba> {
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
let (x, y) = self.pos_to_buffer_pos(pos).to_u32_checked()?;
let pixel = self.buffer.get_pixel_checked(x, y)?;
Some(color::from_image_color(*pixel))
}
pub fn set(&mut self, pos: Vec2, color: Srgba) {
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
let below = color::from_image_color(*pixel);
*pixel = color::to_image_color(color.atop(below));
}
let Some((x, y)) = self.pos_to_buffer_pos(pos).to_u32_checked() else {
return;
};
let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) else {
return;
};
let below = color::from_image_color(*pixel);
*pixel = color::to_image_color(color.atop(below));
}
pub fn replace(&mut self, pos: Vec2, color: Srgba) {
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
*pixel = color::to_image_color(color);
}
let Some((x, y)) = self.pos_to_buffer_pos(pos).to_u32_checked() else {
return;
};
let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) else {
return;
};
*pixel = color::to_image_color(color);
}
// More complicated drawing primitives