Skip to content

Commit

Permalink
Fix rare overflow panic
Browse files Browse the repository at this point in the history
  • Loading branch information
laurmaedje committed Oct 2, 2024
1 parent deee2c0 commit 8dd0078
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ The SIL Open Font License Version 1.1 applies to:
States and/or other countries.
(https://github.com/adobe-fonts/source-sans)

* LibertinusSerif-Regular.otf
Copyright © 2012-2024 The Libertinus Project Authors,
with Reserved Font Name "Linux Libertine", "Biolinum", "STIX Fonts".

-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
Expand Down
Binary file added fonts/LibertinusSerif-Regular.otf
Binary file not shown.
24 changes: 13 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,11 @@ impl Canvas {
}

/// Add to a value in the accumulation buffer.
fn add(&mut self, index: usize, delta: f32) {
if let Some(a) = self.a.get_mut(index) {
*a += delta;
fn add(&mut self, linestart: usize, x: i32, delta: f32) {
if let Ok(x) = usize::try_from(x) {
if let Some(a) = self.a.get_mut(linestart + x) {
*a += delta;
}
}
}

Expand Down Expand Up @@ -292,27 +294,27 @@ impl Canvas {
let x1i = x1ceil as i32;
if x1i <= x0i + 1 {
let xmf = 0.5 * (x + xnext) - x0floor;
self.add(linestart + x0i as usize, d - d * xmf);
self.add(linestart + (x0i + 1) as usize, d * xmf);
self.add(linestart, x0i, d - d * xmf);
self.add(linestart, x0i + 1, d * xmf);
} else {
let s = (x1 - x0).recip();
let x0f = x0 - x0floor;
let a0 = 0.5 * s * (1.0 - x0f) * (1.0 - x0f);
let x1f = x1 - x1ceil + 1.0;
let am = 0.5 * s * x1f * x1f;
self.add(linestart + x0i as usize, d * a0);
self.add(linestart, x0i, d * a0);
if x1i == x0i + 2 {
self.add(linestart + (x0i + 1) as usize, d * (1.0 - a0 - am));
self.add(linestart, x0i + 1, d * (1.0 - a0 - am));
} else {
let a1 = s * (1.5 - x0f);
self.add(linestart + (x0i + 1) as usize, d * (a1 - a0));
self.add(linestart, x0i + 1, d * (a1 - a0));
for xi in x0i + 2..x1i - 1 {
self.add(linestart + xi as usize, d * s);
self.add(linestart, xi, d * s);
}
let a2 = a1 + (x1i - x0i - 3) as f32 * s;
self.add(linestart + (x1i - 1) as usize, d * (1.0 - a2 - am));
self.add(linestart, x1i - 1, d * (1.0 - a2 - am));
}
self.add(linestart + x1i as usize, d * am);
self.add(linestart, x1i, d * am);
}
x = xnext;
}
Expand Down
Binary file added tests/(.ppm
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/raster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ttf_parser::{Face, GlyphId};
const ROBOTO: &[u8] = include_bytes!("../fonts/Roboto-Regular.ttf");
const SOURCE_SANS: &[u8] = include_bytes!("../fonts/SourceSans3-Regular.otf");
const IBM_PLEX: &[u8] = include_bytes!("../fonts/IBMPlexSans-Bold.ttf");
const LIBERTINUS: &[u8] = include_bytes!("../fonts/LibertinusSerif-Regular.otf");

#[test]
fn test_load_all() {
Expand All @@ -21,6 +22,7 @@ fn test_rasterize() {
ok &= raster_letter(ROBOTO, 'A', 0.0, 0.0, 100.0);
ok &= raster_letter(SOURCE_SANS, 'g', 0.0, 0.0, 400.0);
ok &= raster_letter(IBM_PLEX, 'l', 138.48, 95.84, 80.0);
ok &= raster_letter(LIBERTINUS, '(', 114.09056, 34.47, 22.0);
if !ok {
panic!();
}
Expand Down

0 comments on commit 8dd0078

Please sign in to comment.