Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: archive web worker draft #590

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/conversion/vec2canvas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ web-sys = { workspace = true, features = [
"CanvasRenderingContext2d",
"Path2d",
"Blob",
"HtmlImageElement",
"ImageBitmap",
"HtmlCanvasElement",
"Url",
"ImageData",
Expand All @@ -36,6 +36,8 @@ web-sys = { workspace = true, features = [
"BlobPropertyBag",
"Window",
"Document",
"TextMetrics",
"DedicatedWorkerGlobalScope",
] }

[features]
Expand Down
220 changes: 219 additions & 1 deletion crates/conversion/vec2canvas/src/device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use web_sys::{ImageData, OffscreenCanvas};
use web_sys::{ImageBitmap, ImageData, OffscreenCanvas, Path2d};

pub trait CanvasDevice {
#[doc = "The `restore()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore)"]
fn restore(&self);
#[doc = "The `save()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save)"]
fn save(&self);

#[doc = "The `setTransform()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform)"]
Expand Down Expand Up @@ -31,10 +40,75 @@ pub trait CanvasDevice {
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage)"]
fn draw_image_with_offscreen_canvas(&self, image: &OffscreenCanvas, dx: f64, dy: f64);

#[doc = "The `clip()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip)"]
fn clip_with_path_2d(&self, path: &Path2d);
#[doc = "Setter for the `strokeStyle` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeStyle)"]
fn set_stroke_style(&self, value: &::wasm_bindgen::JsValue);
#[doc = "The `stroke()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/stroke)"]
fn stroke_with_path(&self, path: &Path2d);
#[doc = "The `drawImage()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage)"]
fn draw_image_with_image_bitmap_and_dw_and_dh(
&self,
image: &ImageBitmap,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
);
#[doc = "The `drawImage()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvasRenderingContext2D/drawImage)"]
fn draw_image_with_offscreen_canvas_and_dw_and_dh(
&self,
image: &OffscreenCanvas,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
);

#[doc = "Setter for the `lineWidth` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth)"]
fn set_line_width(&self, value: f64);
#[doc = "Setter for the `lineCap` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap)"]
fn set_line_cap(&self, value: &str);
#[doc = "Setter for the `lineJoin` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin)"]
fn set_line_join(&self, value: &str);
#[doc = "Setter for the `miterLimit` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/miterLimit)"]
fn set_miter_limit(&self, value: f64);
#[doc = "Setter for the `lineDashOffset` field of this object."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset)"]
fn set_line_dash_offset(&self, value: f64);
#[doc = "The `setLineDash()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setLineDash)"]
fn set_line_dash(&self, segments: &::wasm_bindgen::JsValue);
#[doc = "The `fill()` method."]
#[doc = ""]
#[doc = "[MDN Documentation](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fill)"]
fn fill_with_path_2d(&self, path: &Path2d);
}

impl CanvasDevice for web_sys::CanvasRenderingContext2d {
fn set_transform(&self, a: f64, b: f64, c: f64, d: f64, e: f64, f: f64) {
// .map_err(map_err("CanvasRenderTask.SetTransform"))
self.set_transform(a, b, c, d, e, f).unwrap();
}

Expand Down Expand Up @@ -62,6 +136,78 @@ impl CanvasDevice for web_sys::CanvasRenderingContext2d {
self.draw_image_with_offscreen_canvas(image, dx, dy)
.unwrap();
}

fn clip_with_path_2d(&self, path: &Path2d) {
self.clip_with_path_2d(path);
}

fn set_stroke_style(&self, value: &::wasm_bindgen::JsValue) {
self.set_stroke_style(value);
}

fn stroke_with_path(&self, path: &Path2d) {
self.stroke_with_path(path);
}

fn draw_image_with_image_bitmap_and_dw_and_dh(
&self,
image: &ImageBitmap,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) {
self.draw_image_with_image_bitmap_and_dw_and_dh(image, dx, dy, dw, dh)
.unwrap();
}

fn draw_image_with_offscreen_canvas_and_dw_and_dh(
&self,
image: &OffscreenCanvas,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) {
self.draw_image_with_offscreen_canvas_and_dw_and_dh(image, dx, dy, dw, dh)
.unwrap();
}

fn set_line_width(&self, value: f64) {
self.set_line_width(value);
}

fn set_line_cap(&self, value: &str) {
self.set_line_cap(value);
}

fn set_line_join(&self, value: &str) {
self.set_line_join(value);
}

fn set_miter_limit(&self, value: f64) {
self.set_miter_limit(value);
}

fn set_line_dash_offset(&self, value: f64) {
self.set_line_dash_offset(value);
}

fn restore(&self) {
self.restore();
}

fn save(&self) {
self.save();
}

fn set_line_dash(&self, segments: &::wasm_bindgen::JsValue) {
self.set_line_dash(segments).unwrap();
}

fn fill_with_path_2d(&self, path: &Path2d) {
self.fill_with_path_2d(path);
}
}

impl CanvasDevice for web_sys::OffscreenCanvasRenderingContext2d {
Expand Down Expand Up @@ -93,4 +239,76 @@ impl CanvasDevice for web_sys::OffscreenCanvasRenderingContext2d {
self.draw_image_with_offscreen_canvas(image, dx, dy)
.unwrap();
}

fn clip_with_path_2d(&self, path: &Path2d) {
self.clip_with_path_2d(path);
}

fn set_stroke_style(&self, value: &::wasm_bindgen::JsValue) {
self.set_stroke_style(value);
}

fn stroke_with_path(&self, path: &Path2d) {
self.stroke_with_path(path);
}

fn draw_image_with_image_bitmap_and_dw_and_dh(
&self,
image: &ImageBitmap,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) {
self.draw_image_with_image_bitmap_and_dw_and_dh(image, dx, dy, dw, dh)
.unwrap();
}

fn draw_image_with_offscreen_canvas_and_dw_and_dh(
&self,
image: &OffscreenCanvas,
dx: f64,
dy: f64,
dw: f64,
dh: f64,
) {
self.draw_image_with_offscreen_canvas_and_dw_and_dh(image, dx, dy, dw, dh)
.unwrap();
}

fn set_line_width(&self, value: f64) {
self.set_line_width(value);
}

fn set_line_cap(&self, value: &str) {
self.set_line_cap(value);
}

fn set_line_join(&self, value: &str) {
self.set_line_join(value);
}

fn set_miter_limit(&self, value: f64) {
self.set_miter_limit(value);
}

fn set_line_dash_offset(&self, value: f64) {
self.set_line_dash_offset(value);
}

fn restore(&self) {
self.restore();
}

fn save(&self) {
self.save();
}

fn set_line_dash(&self, segments: &::wasm_bindgen::JsValue) {
self.set_line_dash(segments).unwrap();
}

fn fill_with_path_2d(&self, path: &Path2d) {
self.fill_with_path_2d(path);
}
}
17 changes: 3 additions & 14 deletions crates/conversion/vec2canvas/src/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use reflexo::{
},
};

use crate::{set_transform, CanvasOp, CanvasPage, CanvasTask, DefaultExportFeature};
use crate::{set_transform, CanvasDevice, CanvasOp, CanvasPage, CanvasTask, DefaultExportFeature};

/// Incremental pass from vector to canvas
pub struct IncrVec2CanvasPass {
Expand Down Expand Up @@ -58,22 +58,11 @@ impl IncrVec2CanvasPass {
})
.collect();

let ts = sk::Transform::from_scale(self.pixel_per_pt, self.pixel_per_pt);
for page in pages.iter() {
page.elem.prepare(ts);
}

web_sys::console::log_1(&"interpret_changes".into());
self.pages = pages;
}

/// Flushes a page to the canvas with the given transform.
pub async fn flush_page(
&mut self,
idx: usize,
canvas: &web_sys::CanvasRenderingContext2d,
ts: sk::Transform,
) {
pub async fn flush_page(&mut self, idx: usize, canvas: &dyn CanvasDevice, ts: sk::Transform) {
let pg = &self.pages[idx];

if !set_transform(canvas, ts) {
Expand Down Expand Up @@ -125,7 +114,7 @@ impl IncrCanvasDocClient {
pub async fn render_page_in_window(
&mut self,
kern: &mut IncrDocClient,
canvas: &web_sys::CanvasRenderingContext2d,
canvas: &dyn CanvasDevice,
idx: usize,
_rect: Rect,
) -> ZResult<()> {
Expand Down
Loading
Loading