Skip to content

Commit

Permalink
dev(pkg::core): render optional canvas body (#405)
Browse files Browse the repository at this point in the history
* dev: remove console_log

* dev: optional rendering body

* dev: edit warning
  • Loading branch information
Myriad-Dreamin authored Nov 11, 2023
1 parent 7ffc98a commit 7c5207d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
41 changes: 20 additions & 21 deletions packages/renderer/src/render/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TypstRenderer {
pub async fn render_page_to_canvas(
&mut self,
ses: &RenderSession,
canvas: &web_sys::CanvasRenderingContext2d,
canvas: Option<web_sys::CanvasRenderingContext2d>,
options: Option<RenderPageImageOptions>,
) -> ZResult<JsValue> {
let (fingerprint, text_content, annotation_list, ..) = self
Expand All @@ -53,7 +53,7 @@ impl TypstRenderer {
pub async fn render_page_to_canvas_internal<Feat: ExportFeature>(
&mut self,
ses: &RenderSession,
canvas: &web_sys::CanvasRenderingContext2d,
canvas: Option<web_sys::CanvasRenderingContext2d>,
options: Option<RenderPageImageOptions>,
) -> ZResult<(Fingerprint, JsValue, JsValue, Option<HashMap<String, f64>>)> {
let rect_lo_x: f32 = -1.;
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -295,7 +294,7 @@ mod tests {
let prepare = performance.now();

let (_fingerprint, res, _, perf_events) = renderer
.render_page_to_canvas_internal::<CIRenderFeature>(&session, &context, None)
.render_page_to_canvas_internal::<CIRenderFeature>(&session, Some(context), None)
.await
.unwrap();
let end = performance.now();
Expand Down
2 changes: 1 addition & 1 deletion packages/typst.ts/src/options.render.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion packages/typst.ts/src/renderer.mts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ function randstr(prefix?: string): string {
.replace('0.', prefix || '');
}

let warnOnceCanvasSet = true;

class TypstRendererDriver {
renderer: typst.TypstRenderer;
rendererJs: typeof typst;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
);
});
}

Expand Down

0 comments on commit 7c5207d

Please sign in to comment.