diff --git a/crates/conversion/vec2canvas/Cargo.toml b/crates/conversion/vec2canvas/Cargo.toml index 120acb97..de99ab5d 100644 --- a/crates/conversion/vec2canvas/Cargo.toml +++ b/crates/conversion/vec2canvas/Cargo.toml @@ -37,6 +37,7 @@ web-sys = { workspace = true, features = [ "Window", "Document", "TextMetrics", + "DedicatedWorkerGlobalScope", ] } [features] diff --git a/crates/conversion/vec2canvas/src/incr.rs b/crates/conversion/vec2canvas/src/incr.rs index 2f21afe5..1a1fb735 100644 --- a/crates/conversion/vec2canvas/src/incr.rs +++ b/crates/conversion/vec2canvas/src/incr.rs @@ -58,12 +58,6 @@ 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; } diff --git a/crates/conversion/vec2canvas/src/ops.rs b/crates/conversion/vec2canvas/src/ops.rs index c1ac29ac..ffac621f 100644 --- a/crates/conversion/vec2canvas/src/ops.rs +++ b/crates/conversion/vec2canvas/src/ops.rs @@ -345,11 +345,6 @@ impl CanvasImageElem { } async fn draw_image(ts: sk::Transform, canvas: &dyn CanvasDevice, image_data: &ImageItem) { - // ImageBitmap - // wasm_bindgen_futures::JsFuture::from(img_load_promise) - // .await - // .unwrap(); - if !set_transform(canvas, ts) { return; } diff --git a/packages/renderer/Cargo.toml b/packages/renderer/Cargo.toml index f5e928a9..e2b2aebf 100644 --- a/packages/renderer/Cargo.toml +++ b/packages/renderer/Cargo.toml @@ -62,7 +62,7 @@ vergen.workspace = true [features] default = ["full"] -full = ["serde_json", "render_full", "worker", "web_test"] +full = ["serde_json", "render_full", "web_test"] web_test = ["typst-ts-test-common/web_artifacts"] web_verbose = ["web_test"] # for test debug @@ -90,7 +90,6 @@ worker = [ "web-sys/Worker", "web-sys/WorkerOptions", "web-sys/WorkerType", - "web-sys/DedicatedWorkerGlobalScope", ] web = ["render_canvas", "render_svg", "render_dom", "worker"] diff --git a/packages/renderer/package.json b/packages/renderer/package.json index e533d8cb..5c1532d2 100644 --- a/packages/renderer/package.json +++ b/packages/renderer/package.json @@ -28,7 +28,7 @@ "publish:lib": "npm publish --access public || exit 0", "test:chrome:verbose": "wasm-pack test --chrome --headless --release --features web_verbose", "test:firefox:verbose": "wasm-pack test --firefox --headless --release --features web_verbose", - "test:chrome": "cross-env WASM_BINDGEN_TEST_NO_ORIGIN_ISOLATION=1 wasm-pack test --chrome --release", + "test:chrome": "cross-env WASM_BINDGEN_TEST_NO_ORIGIN_ISOLATION=1 wasm-pack test --chrome --headless --release", "test:firefox": "cross-env WASM_BINDGEN_TEST_NO_ORIGIN_ISOLATION=1 wasm-pack test --firefox --headless --release" } } \ No newline at end of file diff --git a/packages/renderer/src/render/canvas.rs b/packages/renderer/src/render/canvas.rs index 25f48da5..14ca2942 100644 --- a/packages/renderer/src/render/canvas.rs +++ b/packages/renderer/src/render/canvas.rs @@ -165,10 +165,9 @@ mod tests { use std::{ collections::HashMap, - sync::{Arc, Mutex, OnceLock}, + sync::{Mutex, OnceLock}, }; - use js_sys::Uint8Array; use reflexo_vec2canvas::ExportFeature; use send_wrapper::SendWrapper; use serde::{Deserialize, Serialize}; @@ -177,16 +176,14 @@ mod tests { use wasm_bindgen::JsCast; use wasm_bindgen_test::*; - use crate::{ - session::CreateSessionOptions, - worker::{create_worker, WorkerCore}, - TypstRenderer, - }; + #[cfg(feature = "worker")] + use crate::worker::{create_worker, WorkerCore}; + use crate::{session::CreateSessionOptions, TypstRenderer}; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); const SHOW_RESULT: bool = true; - const IN_WORKER: bool = true; + const IN_WORKER: bool = false; fn hash_bytes>(bytes: T) -> String { format!("sha256:{}", hex::encode(sha2::Sha256::digest(bytes))) @@ -218,7 +215,8 @@ mod tests { static RENDERER: Mutex>>> = Mutex::new(OnceLock::new()); - static WORKER: Mutex>>>> = + #[cfg(feature = "worker")] + static WORKER: Mutex>>>> = Mutex::new(OnceLock::new()); type PerfMap = Option>; @@ -281,11 +279,16 @@ mod tests { (text_content, perf_events) } + #[cfg(feature = "worker")] async fn render_in_worker_thread( artifact: &[u8], format: &str, canvas: &web_sys::HtmlCanvasElement, ) -> (String, PerfMap) { + use std::sync::Arc; + + use js_sys::Uint8Array; + let repo = "http://localhost:20810/base/node_modules/@myriaddreamin/typst-ts-renderer"; let renderer_wrapper = format!("{repo}/pkg/typst_ts_renderer.mjs"); let renderer_wasm = format!("{repo}/pkg/typst_ts_renderer_bg.wasm"); @@ -324,7 +327,7 @@ renderer_wrapper.as_str() let opts = web_sys::WorkerOptions::new(); opts.set_type(web_sys::WorkerType::Module); - let worker = web_sys::Worker::new_with_options(worker_url, &opts).unwrap(); + let worker = web_sys::Worker::new_with_options(&worker_url, &opts).unwrap(); SendWrapper::new(Mutex::new(create_worker(worker))) }); @@ -349,7 +352,7 @@ renderer_wrapper.as_str() let prepare = performance.now(); let (_fingerprint, res, perf_events) = renderer - .render_page_to_canvas(&session, Some(canvas), None) + .render_page_to_canvas(Arc::new(session), Some(canvas), None) .await .unwrap(); let end = performance.now(); @@ -385,11 +388,15 @@ renderer_wrapper.as_str() let (time_used, perf_events, data_content_hash, ..) = { let start = performance.now(); + #[cfg(feature = "worker")] let (text_content, perf_events) = if IN_WORKER { render_in_worker_thread(artifact, format, &canvas).await } else { render_in_main_thread(artifact, format, &canvas).await }; + #[cfg(not(feature = "worker"))] + let (text_content, perf_events) = + render_in_main_thread(artifact, format, &canvas).await; let end = performance.now();