diff --git a/render-wasm/src/shapes.rs b/render-wasm/src/shapes.rs index d1d5ed2c0f..86fcfdc2d2 100644 --- a/render-wasm/src/shapes.rs +++ b/render-wasm/src/shapes.rs @@ -648,20 +648,26 @@ impl Shape { // TODO: Maybe store this inside the shape pub fn bounds(&self) -> Bounds { + let (x, y, mut width, mut height) = ( + self.selrect.x(), + self.selrect.y(), + self.selrect.width(), + self.selrect.height(), + ); + + if let Type::Text(text_content) = &self.shape_type { + width = text_content.size.width; + height = text_content.size.height + } + let mut bounds = Bounds::new( - Point::new(self.selrect.x(), self.selrect.y()), - Point::new(self.selrect.x() + self.selrect.width(), self.selrect.y()), - Point::new( - self.selrect.x() + self.selrect.width(), - self.selrect.y() + self.selrect.height(), - ), - Point::new(self.selrect.x(), self.selrect.y() + self.selrect.height()), + Point::new(x, y), + Point::new(x + width, y), + Point::new(x + width, y + height), + Point::new(x, y + height), ); // Apply this transformation only when self.transform - // is not the identity matrix because if it is, - // the result of applying this transformations would be - // the same identity matrix. if !self.transform.is_identity() { let mut matrix = self.transform; let center = self.center(); @@ -823,10 +829,10 @@ impl Shape { shapes_pool: &ShapesPool, modifiers: &HashMap, ) -> math::Rect { - let mut shape = self.transformed(modifiers.get(&self.id)); + let shape = self.transformed(modifiers.get(&self.id)); let max_stroke = Stroke::max_bounds_width(shape.strokes.iter(), shape.is_open()); - let mut rect = match &mut shape.shape_type { + let mut rect = match &shape.shape_type { Type::Path(_) | Type::Bool(_) => { if let Some(path) = shape.get_skia_path() { return path diff --git a/render-wasm/src/shapes/text.rs b/render-wasm/src/shapes/text.rs index 5303057acc..de90b68448 100644 --- a/render-wasm/src/shapes/text.rs +++ b/render-wasm/src/shapes/text.rs @@ -1,7 +1,6 @@ -use crate::{ - math::{Matrix, Rect}, - render::{default_font, DEFAULT_EMOJI_FONT}, -}; +use crate::render::{default_font, DEFAULT_EMOJI_FONT}; + +use crate::math::{Matrix, Point, Rect}; use core::f32; use macros::ToJs; @@ -15,7 +14,6 @@ use skia_safe::{ use std::collections::HashSet; use super::FontFamily; -use crate::math::Point; use crate::shapes::{self, merge_fills}; use crate::utils::{get_fallback_fonts, get_font_collection}; use crate::Uuid; @@ -397,8 +395,21 @@ impl TextContent { self.size.copy_finite_size(result.2); } + pub fn get_height(&self, width: f32) -> f32 { + let mut paragraph_builders = self.paragraph_builder_group_from_text(None); + let paragraphs = + self.build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width); + paragraphs + .iter() + .flatten() + .fold(0.0, |auto_height, paragraph| { + auto_height + paragraph.height() + }) + } + pub fn update_layout(&mut self, selrect: Rect) -> TextContentSize { - self.size.set_size(selrect.width(), selrect.height()); + let height = self.get_height(selrect.width()); + self.size.set_size(selrect.width(), height); match self.grow_type() { GrowType::AutoHeight => { diff --git a/render-wasm/src/wasm/text.rs b/render-wasm/src/wasm/text.rs index 923c96d1f5..5b0612a361 100644 --- a/render-wasm/src/wasm/text.rs +++ b/render-wasm/src/wasm/text.rs @@ -317,10 +317,9 @@ pub extern "C" fn set_shape_grow_type(grow_type: u8) { #[no_mangle] pub extern "C" fn get_text_dimensions() -> *mut u8 { let mut ptr = std::ptr::null_mut(); - println!("@@@ get_text_dimensions called"); with_current_shape_mut!(state, |shape: &mut Shape| { - shape.invalidate_extrect(); if let Type::Text(content) = &mut shape.shape_type { + // FIXME: can we use content.size here? let text_content_size = content.update_layout(shape.selrect); let mut bytes = vec![0; 12];