Skip to content

Commit

Permalink
Panic hardening of ab-glyph (#64)
Browse files Browse the repository at this point in the history
* Panic hardening of ab-glyph
  • Loading branch information
PolyMeilex authored Jul 2, 2024
1 parent af39ca8 commit 83b5673
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## [Unreleased]
- Panic hardening of ab-glyph (#64)

## 0.10.0
- Bump sctk to 0.19
Expand Down
12 changes: 9 additions & 3 deletions src/title/ab_glyph_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,26 @@ impl AbGlyphTitleText {
let top = bounds.min.y as u32;
outline.draw(|x, y, c| {
// `ab_glyph` may return values greater than 1.0, but they are defined to be
// same as 1.0. For our purposes, we need to contrain this value.
// same as 1.0. For our purposes, we need to constrain this value.
let c = c.min(1.0);

// offset the index by 1, so it is in the center of the pixmap
let p_idx = (top + y + 1) * width + (left + x + 1);
let old_alpha_u8 = pixels[p_idx as usize].alpha();
let Some(pixel) = pixels.get_mut(p_idx as usize) else {
log::warn!("Ignoring out of range pixel (pixel id: {p_idx}");
return;
};

let old_alpha_u8 = pixel.alpha();

let new_alpha = c + (old_alpha_u8 as f32 / 255.0);
if let Some(px) = PremultipliedColorU8::from_rgba(
(self.color.red() * new_alpha * 255.0) as _,
(self.color.green() * new_alpha * 255.0) as _,
(self.color.blue() * new_alpha * 255.0) as _,
(new_alpha * 255.0) as _,
) {
pixels[p_idx as usize] = px;
*pixel = px;
}
})
}
Expand Down

0 comments on commit 83b5673

Please sign in to comment.