From 7c5207d2d36faf106e6a160c4ea559c2b3042ee4 Mon Sep 17 00:00:00 2001 From: Myriad-Dreamin <35292584+Myriad-Dreamin@users.noreply.github.com> Date: Sat, 11 Nov 2023 12:31:02 +0800 Subject: [PATCH] dev(pkg::core): render optional canvas body (#405) * dev: remove console_log * dev: optional rendering body * dev: edit warning --- packages/renderer/src/render/canvas.rs | 41 ++++++++++++------------ packages/typst.ts/src/options.render.mts | 2 +- packages/typst.ts/src/renderer.mts | 11 ++++++- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/packages/renderer/src/render/canvas.rs b/packages/renderer/src/render/canvas.rs index fb859457..131789ad 100644 --- a/packages/renderer/src/render/canvas.rs +++ b/packages/renderer/src/render/canvas.rs @@ -29,7 +29,7 @@ impl TypstRenderer { pub async fn render_page_to_canvas( &mut self, ses: &RenderSession, - canvas: &web_sys::CanvasRenderingContext2d, + canvas: Option, options: Option, ) -> ZResult { let (fingerprint, text_content, annotation_list, ..) = self @@ -53,7 +53,7 @@ impl TypstRenderer { pub async fn render_page_to_canvas_internal( &mut self, ses: &RenderSession, - canvas: &web_sys::CanvasRenderingContext2d, + canvas: Option, options: Option, ) -> ZResult<(Fingerprint, JsValue, JsValue, Option>)> { let rect_lo_x: f32 = -1.; @@ -69,16 +69,13 @@ impl TypstRenderer { let mut client = ses.canvas_kern.lock().unwrap(); client.set_pixel_per_pt(ses.pixel_per_pt.unwrap_or(3.)); client.set_fill(ses.background_color.as_deref().unwrap_or("ffffff").into()); - // console_log!( - // "background_color: {:?}", - // ses.background_color.as_deref().unwrap_or("ffffff") - // ); let data_selection = options .as_ref() .and_then(|o| o.data_selection) .unwrap_or(u32::MAX); + let should_render_body = (data_selection & (1 << 0)) != 0; let mut tc = ((data_selection & (1 << 1)) != 0).then(TextContent::default); let mut annotations = ((data_selection & (1 << 2)) != 0).then(AnnotationList::default); @@ -124,20 +121,22 @@ impl TypstRenderer { page_off = None; } - let cached = options - .and_then(|o| o.cache_key) - .map(|c| c == fingerprint.as_svg_id("c")) - .unwrap_or(false); - - console_log!("cached: {}", cached); - - if !cached { - if let Some(page_off) = page_off { - client - .render_page_in_window(&mut kern, canvas, page_off, rect) - .await?; - } else { - client.render_in_window(&mut kern, canvas, rect).await; + if should_render_body { + let cached = options + .and_then(|o| o.cache_key) + .map(|c| c == fingerprint.as_svg_id("c")) + .unwrap_or(false); + + let canvas = &canvas.ok_or_else(|| error_once!("Renderer.MissingCanvasForBody"))?; + + if !cached { + if let Some(page_off) = page_off { + client + .render_page_in_window(&mut kern, canvas, page_off, rect) + .await?; + } else { + client.render_in_window(&mut kern, canvas, rect).await; + } } } @@ -295,7 +294,7 @@ mod tests { let prepare = performance.now(); let (_fingerprint, res, _, perf_events) = renderer - .render_page_to_canvas_internal::(&session, &context, None) + .render_page_to_canvas_internal::(&session, Some(context), None) .await .unwrap(); let end = performance.now(); diff --git a/packages/typst.ts/src/options.render.mts b/packages/typst.ts/src/options.render.mts index 49293e93..1c5776f5 100644 --- a/packages/typst.ts/src/options.render.mts +++ b/packages/typst.ts/src/options.render.mts @@ -115,7 +115,7 @@ export interface ManipulateDataOptions { * @property {number} page_off - The page offset to render. */ export class RenderCanvasOptions { - canvas: CanvasRenderingContext2D; + canvas?: CanvasRenderingContext2D; /** * The page offset to render. diff --git a/packages/typst.ts/src/renderer.mts b/packages/typst.ts/src/renderer.mts index 665d130c..38b776f0 100644 --- a/packages/typst.ts/src/renderer.mts +++ b/packages/typst.ts/src/renderer.mts @@ -507,6 +507,8 @@ function randstr(prefix?: string): string { .replace('0.', prefix || ''); } +let warnOnceCanvasSet = true; + class TypstRendererDriver { renderer: typst.TypstRenderer; rendererJs: typeof typst; @@ -558,6 +560,9 @@ class TypstRendererDriver { let encoded = 0; if (options.dataSelection.body) { encoded |= 1 << 0; + } else if (options.canvas && warnOnceCanvasSet) { + warnOnceCanvasSet = false; + console.warn('dataSelection.body is not set but providing canvas for body'); } if (options.dataSelection.text) { encoded |= 1 << 1; @@ -567,7 +572,11 @@ class TypstRendererDriver { } rustOptions.data_selection = encoded; } - return this.renderer.render_page_to_canvas(sessionRef[kObject], options.canvas, rustOptions); + return this.renderer.render_page_to_canvas( + sessionRef[kObject], + options.canvas || undefined, + rustOptions, + ); }); }