Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix build errors
  • Loading branch information
elrnv committed Apr 12, 2020
commit 2e23f0e390307ac337b40f27ee5af265aceb3306
3 changes: 3 additions & 0 deletions piet-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ png = { version = "0.16.1", optional = true }

[target.'cfg(target_arch="wasm32")'.dependencies]
piet-web = { version = "0.0.12", path = "../piet-web" }
web-sys = { version = "0.3.36", features = ["console", "Window", "CanvasGradient", "CanvasRenderingContext2d", "CanvasWindingRule", "Document", "Element", "HtmlCanvasElement", "ImageBitmap", "ImageData", "TextMetrics"] }
wasm-bindgen = "0.2.59"

50 changes: 41 additions & 9 deletions piet-common/src/web_back.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
//! Support for piet Web back-end.

use piet::{ErrorKind, ImageFormat};
#[doc(hidden)]
pub use piet_web::*;

use std::fmt;
use std::marker::PhantomData;

use wasm_bindgen::JsCast;

pub type Piet<'a> = WebRenderContext<'a>;

/// The associated brush type for this backend.
Expand Down Expand Up @@ -44,8 +51,8 @@ pub struct Device;

/// A struct provides a `RenderContext` and then can have its bitmap extracted.
pub struct BitmapTarget<'a> {
canvas: HtmlCanvasElement,
context: CanvasRenderingContext2d,
canvas: web_sys::HtmlCanvasElement,
context: web_sys::CanvasRenderingContext2d,
phantom: PhantomData<&'a ()>,
}

Expand All @@ -67,10 +74,11 @@ impl Device {
.create_element("canvas")
.unwrap()
.dyn_into::<web_sys::HtmlCanvasElement>()
.unwrap()
.unwrap();
let context = canvas
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2d>()
.unwrap();

Expand All @@ -89,22 +97,46 @@ impl Device {
impl<'a> BitmapTarget<'a> {
/// Get a piet `RenderContext` for the bitmap.
pub fn render_context(&mut self) -> WebRenderContext {
WebRenderContext::new(self.ctx.clone(), web_sys::window().unwrap())
WebRenderContext::new(self.context.clone(), web_sys::window().unwrap())
}

/// Get raw RGBA pixels from the bitmap.
pub fn into_raw_pixels(mut self, fmt: ImageFormat) -> Result<Vec<u8>, piet::Error> {
pub fn into_raw_pixels(self, fmt: ImageFormat) -> Result<Vec<u8>, piet::Error> {
// TODO: This code is just a snippet. A thorough review and testing should be done before
// this is used. It is here for compatibility with druid.

if fmt != ImageFormat::RgbaPremul {
return Err(piet::new_eror(ErrorKind::NotSupported));
return Err(piet::new_error(ErrorKind::NotSupported));
}

let mut raw_data = vec![0; width * height * 4];
let img_data = self.context.get_image_data()
.map_err(Into::<Box<dyn std::error::Error>>::into)?;
let width = self.canvas.width() as usize;
let height = self.canvas.height() as usize;

let img_data = self
.context
.get_image_data(0.0, 0.0, width as f64, height as f64)
.map_err(|jsv| piet::new_error(ErrorKind::BackendError(Box::new(JsError::new(jsv)))))?;

// ImageDate is in RGBA order. This should be the same as expected on the output.
Ok(img_data.data().0)
}
}

#[derive(Clone, Debug)]
struct JsError {
jsv: wasm_bindgen::JsValue,
}

impl JsError {
fn new(jsv: wasm_bindgen::JsValue) -> Self {
JsError { jsv }
}
}

impl fmt::Display for JsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.jsv)
}
}

impl std::error::Error for JsError {}