Repository: plotters-rs/plotters (language: rust)

Issue:
Support for wasm32-wasi target
I ran into an issue while trying to compile plotters to WebAssembly.

Currently **any** `wasm32-*-*` target (e.g. `wasm32-unknown-unknown`, `wasm32-wasi, `wasm32-...`) is considered to be running inside the browsers and uses `wasm-bindgen` as a dependency.

But this is not always the case. In my case I'm trying to run it on the backend inside an application compiled to `wasm32-wasi` on [Wasmtime](https://github.com/bytecodealliance/wasmtime) and [Wasmer](https://github.com/wasmerio/wasmer). The issue is that `wasm-bindgen` doesn't work on this platforms because they are not browsers and the required wasm-bindgen imports are not satisfied.

Would plotters still work if we just treated `wasm32-wasi` as a non-wasm backend?


Discussion / hints:

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index f675967a..b90197c2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -23,22 +23,22 @@ version = "^0.3.*"
 optional = true
 default_features = false
 
-[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
+[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))'.dependencies]
 ttf-parser = { version = "0.8.2", optional = true }
 lazy_static = { version = "1.4.0", optional = true }
 pathfinder_geometry = { version = "0.5.1", optional = true }
 font-kit = { version = "0.7.0", optional = true }
 
-[target.'cfg(not(target_arch = "wasm32"))'.dependencies.image]
+[target.'cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))'.dependencies.image]
 version = "0.23.4"
 optional = true
 default-features = false
 features = ["jpeg", "png", "bmp"]
 
-[target.'cfg(target_arch = "wasm32")'.dependencies.wasm-bindgen]
+[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies.wasm-bindgen]
 version = "0.2.62"
 
-[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
+[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies.web-sys]
 version = "0.3.39"
 features = [
          "Document",
@@ -101,7 +101,7 @@ serde_json = "1.0.57"
 serde = "1.0.115"
 serde_derive = "1.0.115"
 
-[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
+[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dev-dependencies]
 wasm-bindgen-test = "0.3.12"
 
 [[bench]]
diff --git a/src/element/image.rs b/src/element/image.rs
index dbddb19f..affe9c14 100644
--- a/src/element/image.rs
+++ b/src/element/image.rs
@@ -1,4 +1,7 @@
-#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    feature = "image"
+))]
 use image::{DynamicImage, GenericImageView};
 
 use super::{Drawable, PointCollection};
@@ -6,7 +9,10 @@ use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
 
 use plotters_bitmap::bitmap_pixel::{PixelFormat, RGBPixel};
 
-#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    feature = "image"
+))]
 use plotters_bitmap::bitmap_pixel::BGRXPixel;
 
 use plotters_bitmap::BitMapBackend;
@@ -164,7 +170,10 @@ impl<'a, Coord, P: PixelFormat> BitMapElement<'a, Coord, P> {
     }
 }
 
-#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    feature = "image"
+))]
 impl<'a, Coord> From<(Coord, DynamicImage)> for BitMapElement<'a, Coord, RGBPixel> {
     fn from((pos, image): (Coord, DynamicImage)) -> Self {
         let (w, h) = image.dimensions();
@@ -178,7 +187,10 @@ impl<'a, Coord> From<(Coord, DynamicImage)> for BitMapElement<'a, Coord, RGBPixe
     }
 }
 
-#[cfg(all(not(target_arch = "wasm32"), feature = "image"))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    feature = "image"
+))]
 impl<'a, Coord> From<(Coord, DynamicImage)> for BitMapElement<'a, Coord, BGRXPixel> {
     fn from((pos, image): (Coord, DynamicImage)) -> Self {
         let (w, h) = image.dimensions();
diff --git a/src/style/font/mod.rs b/src/style/font/mod.rs
index 99c3ca62..7441ac15 100644
--- a/src/style/font/mod.rs
+++ b/src/style/font/mod.rs
@@ -6,19 +6,31 @@
 ///
 /// Thus we need different mechanism for the font implementation
 
-#[cfg(all(not(target_arch = "wasm32"), feature = "ttf"))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    feature = "ttf"
+))]
 mod ttf;
-#[cfg(all(not(target_arch = "wasm32"), feature = "ttf"))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    feature = "ttf"
+))]
 use ttf::FontDataInternal;
 
-#[cfg(all(not(target_arch = "wasm32"), not(feature = "ttf")))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    not(feature = "ttf")
+))]
 mod naive;
-#[cfg(all(not(target_arch = "wasm32"), not(feature = "ttf")))]
+#[cfg(all(
+    not(all(target_arch = "wasm32", not(target_os = "wasi"))),
+    not(feature = "ttf")
+))]
 use naive::FontDataInternal;
 
-#[cfg(target_arch = "wasm32")]
+#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
 mod web;
-#[cfg(target_arch = "wasm32")]
+#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
 use web::FontDataInternal;
 
 mod font_desc;
diff --git a/src/style/size.rs b/src/style/size.rs
index 35f2d57f..254df8d4 100644
--- a/src/style/size.rs
+++ b/src/style/size.rs
@@ -51,7 +51,6 @@ impl SizeDesc for f32 {
     }
 }
 
-
 impl SizeDesc for f64 {
     fn in_pixels<D: HasDimension>(&self, _parent: &D) -> i32 {
         *self as i32


Test patch: