Skip to content

Commit

Permalink
Fix a regression related to whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation committed Oct 1, 2024
1 parent 8959a81 commit 6321c8c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/rust/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,29 @@ impl<T: BuildPath> LyonPathBuilder<T> {
self.add_offset_x(find_kerning(facetables, prev_glyph, cur_glyph) as f32);
}

if font.is_color_glyph(cur_glyph) {
let mut painter = LyonPathBuilderForPaint::new(self, &font);
let fg_color = RgbaColor::new(0, 0, 0, 255);
let res = font.paint_color_glyph(cur_glyph, 0, fg_color, &mut painter);
res.ok_or_else(|| savvy::Error::new(&format!("Failed to outline char '{c}'")))?;
} else {
let res = font.outline_glyph(cur_glyph, self);
res.ok_or_else(|| savvy::Error::new(&format!("Failed to outline char '{c}'")))?;
// outline the glyph except when it's a whitespace
if !c.is_whitespace() {
if font.is_color_glyph(cur_glyph) {
let mut painter = LyonPathBuilderForPaint::new(self, &font);
let fg_color = RgbaColor::new(0, 0, 0, 255);
let res = font.paint_color_glyph(cur_glyph, 0, fg_color, &mut painter);
res.ok_or_else(|| {
let nm = font.glyph_name(cur_glyph).unwrap_or("unknown");
savvy::Error::new(&format!(
"Failed to outline char '{c}' (Glyph ID {}: {})",
cur_glyph.0, nm
))
})?;
} else {
let res = font.outline_glyph(cur_glyph, self);
res.ok_or_else(|| {
let nm = font.glyph_name(cur_glyph).unwrap_or("unknown");
savvy::Error::new(&format!(
"Failed to outline char '{c}' (Glyph ID {}: {})",
cur_glyph.0, nm
))
})?;
}
}

if let Some(ha) = font.glyph_hor_advance(cur_glyph) {
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/_snaps/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@
2 0 0.800 1 0
3 0.800 0.800 1 0

# handle white spaces

Code
string2path("A A", "./font/test.ttf")
Output
# A tibble: 8 x 4
x y glyph_id path_id
<dbl> <dbl> <int> <int>
1 0 0 1 1
2 0.800 0.800 1 1
3 0 0.800 1 1
4 0 0 1 1
5 1.60 0 3 2
6 2.40 0.800 3 2
7 1.60 0.800 3 2
8 1.60 0 3 2

# the data extracted from installed font are as expected

Code
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ test_that("the data extracted from font file are as expected", {
expect_snapshot(string2fill("A", "./font/test.ttf"))
})

test_that("handle white spaces", {
expect_snapshot(string2path("A A", "./font/test.ttf"))
})


test_that("the data extracted from installed font are as expected", {
skip_if_not(isTRUE("Arial" %in% dump_fontdb()$family))

Expand Down

0 comments on commit 6321c8c

Please sign in to comment.