Skip to content

Commit

Permalink
fix: calculate bbox of text elements which have zero advance
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Nov 27, 2024
1 parent 7671112 commit c3d5025
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 13 additions & 5 deletions crates/conversion/vec2canvas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,20 @@ impl<'m, 't, Feat: ExportFeature> RenderVm<'m> for CanvasRenderTask<'m, 't, Feat
let upem = Scalar(font.units_per_em.0);
let accender = Scalar(font.ascender.0) * upem;

let w = text.width();
// todo: glyphs like macron has zero width... why?
let w = text.width().max(Scalar(1.));

CanvasBBox::Static(Box::new(Rect {
lo: Point::new(Scalar(0.), accender - upem),
hi: Point::new(w * upem / text.shape.size, accender),
}))
if text.shape.size.0 == 0. {
CanvasBBox::Static(Box::new(Rect {
lo: Point::default(),
hi: Point::default(),
}))
} else {
CanvasBBox::Static(Box::new(Rect {
lo: Point::new(Scalar(0.), accender - upem),
hi: Point::new(w * upem / text.shape.size, accender),
}))
}
};
for style in &text.shape.styles {
if let ir::PathStyle::Fill(fill) = style {
Expand Down
12 changes: 8 additions & 4 deletions crates/conversion/vec2dom/src/svg_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,17 @@ impl TypstElem {
}

let bbox = self.canvas.as_ref().unwrap().bbox_at(ts);
// web_sys::console::log_2(
// &"bbox".into(),
// &format!("{:?} -> {:?} & {:?}", self.f.as_svg_id("g"), bbox,
// viewport).into(), );
let should_visible = bbox
.map(|new_rect| !new_rect.intersect(&viewport).is_empty())
.unwrap_or(true);
// web_sys::console::log_2(
// &"bbox".into(),
// &format!(
// "{:?} -> ({bbox:?} & {viewport:?}) = {should_visible}",
// self.f.as_svg_id("g")
// )
// .into(),
// );

if should_visible != self.is_svg_visible {
let (x, y) = (&self.stub, &self.g);
Expand Down

0 comments on commit c3d5025

Please sign in to comment.