🐛 Fix text width calculation

This commit is contained in:
Elena Torro
2025-07-25 11:33:06 +02:00
committed by Belén Albeza
parent bd15ef4618
commit 0e20bb6271
2 changed files with 36 additions and 17 deletions

View File

@@ -148,7 +148,7 @@ impl TextContent {
) -> Vec<Vec<ParagraphBuilder>> {
if self.grow_type() == GrowType::AutoWidth {
set_paragraphs_width(f32::MAX, &mut paragraphs);
let max_width = auto_width(&mut paragraphs).ceil();
let max_width = auto_width(&mut paragraphs, self.width()).ceil();
set_paragraphs_width(max_width, &mut paragraphs);
} else {
set_paragraphs_width(self.width(), &mut paragraphs);
@@ -638,24 +638,43 @@ impl From<&Vec<u8>> for RawTextData {
}
}
pub fn auto_width(paragraphs: &mut [Vec<ParagraphBuilder>]) -> f32 {
paragraphs.iter_mut().fold(0.0, |auto_width, p| {
p.iter_mut().fold(auto_width, |auto_width, paragraph| {
let mut paragraph = paragraph.build();
paragraph.layout(f32::MAX);
f32::max(paragraph.max_intrinsic_width(), auto_width)
pub fn get_built_paragraphs(
paragraphs: &mut [Vec<ParagraphBuilder>],
width: f32,
) -> Vec<Vec<skia_safe::textlayout::Paragraph>> {
paragraphs
.iter_mut()
.map(|builders| {
builders
.iter_mut()
.map(|builder_handle| {
let mut paragraph = builder_handle.build();
paragraph.layout(width);
paragraph
})
.collect()
})
})
.collect()
}
pub fn max_width(paragraphs: &mut [Vec<ParagraphBuilder>]) -> f32 {
paragraphs.iter_mut().fold(0.0, |max_width, p| {
p.iter_mut().fold(max_width, |max_width, paragraph| {
let mut paragraph = paragraph.build();
paragraph.layout(f32::MAX);
f32::max(paragraph.max_width(), max_width)
pub fn auto_width(paragraphs: &mut [Vec<ParagraphBuilder>], width: f32) -> f32 {
let built_paragraphs = get_built_paragraphs(paragraphs, width);
built_paragraphs
.iter()
.flatten()
.fold(0.0, |auto_width, p| {
f32::max(p.max_intrinsic_width(), auto_width)
})
})
}
pub fn max_width(paragraphs: &mut [Vec<ParagraphBuilder>], width: f32) -> f32 {
let built_paragraphs = get_built_paragraphs(paragraphs, width);
built_paragraphs
.iter()
.flatten()
.fold(0.0, |max_width, p| f32::max(p.max_width(), max_width))
}
pub fn auto_height(paragraphs: &mut [Vec<ParagraphBuilder>], width: f32) -> f32 {

View File

@@ -45,14 +45,14 @@ pub extern "C" fn get_text_dimensions() -> *mut u8 {
if let Type::Text(content) = &shape.shape_type {
let mut paragraphs = content.get_skia_paragraphs();
m_width = max_width(&mut paragraphs);
m_width = max_width(&mut paragraphs, width);
match content.grow_type() {
GrowType::AutoHeight => {
height = auto_height(&mut paragraphs, width).ceil();
}
GrowType::AutoWidth => {
width = auto_width(&mut paragraphs).ceil();
width = auto_width(&mut paragraphs, width).ceil();
height = auto_height(&mut paragraphs, width).ceil();
}
GrowType::Fixed => {}