Skip to content

Commit

Permalink
dev: remove some hacking in new rust version
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Sep 29, 2024
1 parent b63e512 commit 6f5358c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 43 deletions.
8 changes: 5 additions & 3 deletions crates/conversion/vec2dom/src/dom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ impl DomPage {
self.realized_canvas = Some(b.render_page(module, &data)?);
}

let should_load_resource = self.realized_canvas.as_ref().unwrap().prepare().is_some();
let ppp = b.pixel_per_pt;
let ts = tiny_skia::Transform::from_scale(ppp, ppp);
let should_load_resource = self.realized_canvas.as_ref().unwrap().prepare(ts).is_some();

#[cfg(feature = "debug_repaint_canvas")]
web_sys::console::log_1(
Expand Down Expand Up @@ -536,7 +538,7 @@ impl DomPage {
{
break 'render_canvas;
}
// #[cfg(feature = "debug_repaint_canvas")]
#[cfg(feature = "debug_repaint_canvas")]
web_sys::console::log_1(
&format!(
"canvas state changed, render all: {} {}",
Expand Down Expand Up @@ -589,7 +591,7 @@ impl DomPage {
break 'clip_canvas;
};

// #[cfg(feature = "debug_repaint_canvas")]
#[cfg(feature = "debug_repaint_canvas")]
web_sys::console::log_1(
&format!("canvas partial render: {idx} {damage_rect:?}").into(),
);
Expand Down
68 changes: 28 additions & 40 deletions crates/conversion/vec2dom/src/incr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use reflexo::error::prelude::*;
use reflexo::hash::Fingerprint;
use reflexo::vector::ir::{FontItem, FontRef, LayoutRegionNode, Module, Page, VecItem};
use reflexo_typst2vec::incr::{IncrDocClient, IncrDocServer};
use reflexo_vec2canvas::{CanvasElem, CanvasOp};
use reflexo_vec2canvas::CanvasOp;
use wasm_bindgen::prelude::*;
use web_sys::{wasm_bindgen::JsCast, Element, HtmlElement};

Expand Down Expand Up @@ -122,12 +122,22 @@ impl IncrDomDocClient {
}
}

// rust produces false positive on repaint
fn repaint_false_positive(
pub fn repaint(
&mut self,
page_num: u32,
x: f32,
y: f32,
w: f32,
h: f32,
stage: u8,
) -> ZResult<Option<Arc<CanvasElem>>> {
) -> ZResult<JsValue> {
// todo: overflow
let viewport = Some(tiny_skia::Rect::from_xywh(x, y, w, h).unwrap());
#[cfg(feature = "debug_recalc_stage")]
web_sys::console::log_1(
&format!("repaint page:{page_num} stage:{stage} {viewport:?}").into(),
);

let kern = self.client.clone().unwrap();
let kern_lock = kern.lock().unwrap();

Expand All @@ -145,44 +155,21 @@ impl IncrDomDocClient {
match stage {
STAGE_SVG => {
page.repaint_svg(&mut ctx)?;
Ok(None)
}
STAGE_SEMANTICS => {
page.repaint_semantics(&mut ctx)?;
Ok(None)
}
STAGE_PREPARE_CANVAS => Ok(page.prepare_canvas(&mut ctx)?),
STAGE_CANVAS => Ok(None),
_ => todo!(),
}
}

pub fn repaint(
&mut self,
page_num: u32,
x: f32,
y: f32,
w: f32,
h: f32,
stage: u8,
) -> ZResult<JsValue> {
// todo: overflow
let viewport = Some(tiny_skia::Rect::from_xywh(x, y, w, h).unwrap());
#[cfg(feature = "debug_recalc_stage")]
web_sys::console::log_1(
&format!("repaint page:{page_num} stage:{stage} {viewport:?}").into(),
);

let elem = self.repaint_false_positive(page_num, stage)?;

match stage {
STAGE_SVG => Ok(()),
STAGE_SEMANTICS => Ok(()),
STAGE_PREPARE_CANVAS => {
if let Some(elem) = elem {
if let Some(elem) = page.prepare_canvas(&mut ctx)? {
// explicit drop ctx to avoid async promise cature these variables
drop(ctx);
#[cfg(feature = "debug_repaint_canvas")]
web_sys::console::log_1(&format!("canvas state prepare: {}", page_num).into());
if let Some(fut) = elem.prepare() {
web_sys::console::log_1(&format!("canvas state prepare: {page_num}").into());
let ppp = self.canvas_backend.pixel_per_pt;
let ts = tiny_skia::Transform::from_scale(ppp, ppp);
if let Some(fut) = elem.prepare(ts) {
#[allow(dropping_references)]
drop(self);
return Ok(wasm_bindgen_futures::future_to_promise(async move {
fut.await;

Expand All @@ -194,13 +181,15 @@ impl IncrDomDocClient {
&format!("canvas state prepare done: {}", page_num).into(),
);
}

Ok(())
}
STAGE_CANVAS => {
// explicit drop ctx to avoid async promise cature these variables
drop(ctx);
let ppp = self.canvas_backend.pixel_per_pt;
let page = &mut self.doc_view[page_num as usize];
let fut = page.repaint_canvas(viewport, ppp)?;
#[allow(dropping_references)]
drop(self);
return Ok(wasm_bindgen_futures::future_to_promise(async move {
fut.await;

Expand All @@ -209,8 +198,7 @@ impl IncrDomDocClient {
.into());
}
_ => todo!(),
}?;

}
Ok(JsValue::UNDEFINED)
}
}
Expand Down

0 comments on commit 6f5358c

Please sign in to comment.