Skip to content

Commit

Permalink
feat: build without worker
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Oct 2, 2024
1 parent ec95b0e commit 2c79cb1
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 25 deletions.
1 change: 1 addition & 0 deletions crates/conversion/vec2canvas/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ web-sys = { workspace = true, features = [
"Window",
"Document",
"TextMetrics",
"DedicatedWorkerGlobalScope",
] }

[features]
Expand Down
6 changes: 0 additions & 6 deletions crates/conversion/vec2canvas/src/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 0 additions & 5 deletions crates/conversion/vec2canvas/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion packages/renderer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
29 changes: 18 additions & 11 deletions packages/renderer/src/render/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<T: AsRef<[u8]>>(bytes: T) -> String {
format!("sha256:{}", hex::encode(sha2::Sha256::digest(bytes)))
Expand Down Expand Up @@ -218,7 +215,8 @@ mod tests {
static RENDERER: Mutex<OnceLock<SendWrapper<Mutex<TypstRenderer>>>> =
Mutex::new(OnceLock::new());

static WORKER: Mutex<OnceLock<SendWrapper<Mutex<Arc<WorkerCore>>>>> =
#[cfg(feature = "worker")]
static WORKER: Mutex<OnceLock<SendWrapper<Mutex<std::sync::Arc<WorkerCore>>>>> =
Mutex::new(OnceLock::new());

type PerfMap = Option<HashMap<String, f64>>;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)))
});
Expand All @@ -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();
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 2c79cb1

Please sign in to comment.