Skip to content

Commit

Permalink
feat: disable simd image resize in wasm32, bump to 0.0.12
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed Aug 28, 2024
1 parent 8489166 commit 8841bda
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
10 changes: 7 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ unicode-normalization = { version = "0.1.19", optional = true }
unicode-script = { version = "0.5.4", optional = true }
woff2-patched = { version = "0.3.0", optional = true }
png = { version = "0.17.13", optional = true }
fast_image_resize = { version = "3.0.4", optional = true, features = [
"only_u8x4",
] }
inflections = "1.1.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wit-bindgen-rt = { version = "0.24.0", optional = true }
resize = { version = "0.8.6", default-features = false, features = ["std"] }
rgb = "0.8.48"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
fast_image_resize = { version = "3.0.4", optional = true, features = [
"only_u8x4",
] }

[features]
default = ["parse", "metrics", "ras", "wit"]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fontkit-rs",
"version": "0.0.11",
"version": "0.0.12",
"description": "Toolkit used to load, match, measure, and render texts",
"main": "index.js",
"directories": {
Expand Down
68 changes: 44 additions & 24 deletions src/ras.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::num::NonZeroU32;

use ab_glyph_rasterizer::{Point as AbPoint, Rasterizer};
use fast_image_resize::{FilterType, Image, MulDiv, PixelType, ResizeAlg, Resizer};
use pathfinder_content::outline::{Contour, ContourIterFlags, Outline};
#[cfg(feature = "optimize_stroke_broken")]
use pathfinder_content::segment::{Segment, SegmentFlags, SegmentKind};
Expand Down Expand Up @@ -108,28 +105,51 @@ impl Font {
})
.ok()?;
// https://docs.rs/fast_image_resize/latest/fast_image_resize/
let mut image = Image::from_vec_u8(
NonZeroU32::new(bitmap_width).unwrap(),
NonZeroU32::new(bitmap_height).unwrap(),
bitmap,
PixelType::U8x4,
)
.unwrap();
let alpha_mul_div = MulDiv::default();
alpha_mul_div
.multiply_alpha_inplace(&mut image.view_mut())
let result;
#[cfg(target_arch = "wasm32")]
{
use rgb::FromSlice;
let mut dst = vec![0; width as usize * height as usize * 4];
let mut resizer = resize::new(
bitmap_width as usize,
bitmap_height as usize,
width as usize,
height as usize,
resize::Pixel::RGBA8,
resize::Type::Lanczos3,
)
.unwrap();
let mut resizer = Resizer::new(ResizeAlg::Convolution(FilterType::Bilinear));
let mut dst = Image::new(
NonZeroU32::new(width as u32).unwrap(),
NonZeroU32::new(height as u32).unwrap(),
PixelType::U8x4,
);
resizer.resize(&image.view(), &mut dst.view_mut()).unwrap();
alpha_mul_div
.divide_alpha_inplace(&mut dst.view_mut())
let _ = resizer.resize(bitmap.as_rgba(), dst.as_rgba_mut());
result = dst;
}
#[cfg(not(target_arch = "wasm32"))]
{
use fast_image_resize::{FilterType, Image, MulDiv, PixelType, ResizeAlg, Resizer};
use std::num::NonZeroU32;

let mut image = Image::from_vec_u8(
NonZeroU32::new(bitmap_width).unwrap(),
NonZeroU32::new(bitmap_height).unwrap(),
bitmap,
PixelType::U8x4,
)
.unwrap();
let bitmap = dst.into_vec();
let alpha_mul_div = MulDiv::default();
alpha_mul_div
.multiply_alpha_inplace(&mut image.view_mut())
.unwrap();
let mut resizer = Resizer::new(ResizeAlg::Convolution(FilterType::Bilinear));
let mut dst = Image::new(
NonZeroU32::new(width as u32).unwrap(),
NonZeroU32::new(height as u32).unwrap(),
PixelType::U8x4,
);
resizer.resize(&image.view(), &mut dst.view_mut()).unwrap();
alpha_mul_div
.divide_alpha_inplace(&mut dst.view_mut())
.unwrap();
result = dst.into_vec();
}

Some(GlyphBitmap(GlyphBitmapVariants::Raster(GlyphRasterImage {
width: width as u16,
Expand All @@ -139,7 +159,7 @@ impl Font {
ascender: a as f32 * factor,
descender: d as f32 * factor,
advanced_x,
bitmap,
bitmap: result,
})))
} else {
let (glyph, outline) = self.outline(c)?;
Expand Down

0 comments on commit 8841bda

Please sign in to comment.