Skip to content

Commit

Permalink
fix: calculate bbox of text elements which have zero advance (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Nov 27, 2024
1 parent 7671112 commit 97958d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
16 changes: 12 additions & 4 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;

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

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::new(Scalar(0.), accender - upem),
hi: Point::new(Scalar(0.), accender),
}))
} 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
14 changes: 9 additions & 5 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())
.map(|new_rect| new_rect.intersect(&viewport).is_intersected())
.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
8 changes: 8 additions & 0 deletions crates/reflexo/src/vector/ir/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,18 @@ impl Rect {
}
}

/// Returns whether the rectangle has no area.
pub fn is_empty(&self) -> bool {
self.lo.x >= self.hi.x || self.lo.y >= self.hi.y
}

/// Returns whether the rectangle is not well constructed.
///
/// Note: This is not the same as `is_empty`.
pub fn is_intersected(&self) -> bool {
self.lo.x <= self.hi.x || self.lo.y <= self.hi.y
}

pub fn intersect(&self, other: &Self) -> Self {
Self {
lo: self.lo.max(&other.lo),
Expand Down

0 comments on commit 97958d6

Please sign in to comment.