Fix crash when printing some text
This commit is contained in:
parent
4f3d66f6a0
commit
d69557daf6
2 changed files with 32 additions and 10 deletions
|
|
@ -20,12 +20,24 @@ impl Vec2 {
|
||||||
Self { x, y }
|
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 {
|
pub fn from_u32(x: u32, y: u32) -> Self {
|
||||||
let x: i32 = x.try_into().expect("x too large");
|
let x: i32 = x.try_into().expect("x too large");
|
||||||
let y: i32 = y.try_into().expect("y too large");
|
let y: i32 = y.try_into().expect("y too large");
|
||||||
Self::new(x, y)
|
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) {
|
pub fn to_u32(self) -> (u32, u32) {
|
||||||
let x: u32 = self.x.try_into().expect("x too small");
|
let x: u32 = self.x.try_into().expect("x too small");
|
||||||
let y: u32 = self.y.try_into().expect("y too small");
|
let y: u32 = self.y.try_into().expect("y too small");
|
||||||
|
|
|
||||||
|
|
@ -40,25 +40,35 @@ impl<'a> View<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, pos: Vec2) -> Option<Srgba> {
|
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)?;
|
let pixel = self.buffer.get_pixel_checked(x, y)?;
|
||||||
Some(color::from_image_color(*pixel))
|
Some(color::from_image_color(*pixel))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set(&mut self, pos: Vec2, color: Srgba) {
|
pub fn set(&mut self, pos: Vec2, color: Srgba) {
|
||||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
let Some((x, y)) = self.pos_to_buffer_pos(pos).to_u32_checked() else {
|
||||||
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
let below = color::from_image_color(*pixel);
|
let below = color::from_image_color(*pixel);
|
||||||
*pixel = color::to_image_color(color.atop(below));
|
*pixel = color::to_image_color(color.atop(below));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn replace(&mut self, pos: Vec2, color: Srgba) {
|
pub fn replace(&mut self, pos: Vec2, color: Srgba) {
|
||||||
let (x, y) = self.pos_to_buffer_pos(pos).to_u32();
|
let Some((x, y)) = self.pos_to_buffer_pos(pos).to_u32_checked() else {
|
||||||
if let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) {
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(pixel) = self.buffer.get_pixel_mut_checked(x, y) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
*pixel = color::to_image_color(color);
|
*pixel = color::to_image_color(color);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// More complicated drawing primitives
|
// More complicated drawing primitives
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue