This commit is contained in:
Elena Torro
2025-10-22 15:02:54 +02:00
parent 140290cd60
commit 3bc4823e4f
4 changed files with 33 additions and 24 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 216 KiB

After

Width:  |  Height:  |  Size: 216 KiB

View File

@@ -200,47 +200,43 @@ fn draw_text(
shape: &Shape,
paragraph_builder_groups: &mut [Vec<ParagraphBuilder>],
) {
let container_height = if let crate::shapes::Type::Text(text_content) = &shape.shape_type {
text_content.size.height
} else {
shape.selrect().height()
};
let paragraph_width = shape.selrect().width();
let total_content_height =
calculate_all_paragraphs_height(paragraph_builder_groups, paragraph_width);
let text_content = shape.get_text_content();
let text_height = text_content.size.height;
let selrect_width = shape.selrect().width();
let selrect_height = shape.selrect().height();
let mut global_offset_y = match shape.vertical_align() {
VerticalAlign::Center => (container_height - total_content_height) / 2.0,
VerticalAlign::Bottom => container_height - total_content_height,
VerticalAlign::Center => (selrect_height - text_height) / 2.0,
VerticalAlign::Bottom => selrect_height - text_height,
_ => 0.0,
};
let layer_rec = SaveLayerRec::default();
canvas.save_layer(&layer_rec);
for paragraph_builder_group in paragraph_builder_groups {
for (_index, paragraph_builder_group) in paragraph_builder_groups.iter_mut().enumerate() {
let mut group_offset_y = global_offset_y;
let total_paragraphs = paragraph_builder_group.len();
for (paragraph_index, paragraph_builder) in paragraph_builder_group.iter_mut().enumerate() {
let mut paragraph = paragraph_builder.build();
paragraph.layout(paragraph_width);
paragraph.layout(selrect_width);
let _paragraph_height = paragraph.height();
// FIXME: I've kept the _paragraph_height variable to have
// a reminder in the future to keep digging why the ideographic_baseline
// works so well and not the paragraph_height. I think we should test
// this more.
if paragraph_index == 0 {
let xy = (shape.selrect().x(), shape.selrect().y() + group_offset_y);
paragraph.paint(canvas, xy);
if paragraph_index == total_paragraphs - 1 {
group_offset_y += paragraph.ideographic_baseline();
}
let xy = (shape.selrect().x(), shape.selrect().y() + global_offset_y);
paragraph.paint(canvas, xy);
for line_metrics in paragraph.get_line_metrics().iter() {
render_text_decoration(canvas, &paragraph, paragraph_builder, line_metrics, xy);
}
}
global_offset_y += group_offset_y;
global_offset_y = group_offset_y;
}
}
@@ -415,6 +411,7 @@ fn render_text_decoration(
}
}
#[allow(dead_code)]
fn calculate_total_paragraphs_height(paragraphs: &mut [ParagraphBuilder], width: f32) -> f32 {
paragraphs
.iter_mut()
@@ -426,6 +423,7 @@ fn calculate_total_paragraphs_height(paragraphs: &mut [ParagraphBuilder], width:
.sum()
}
#[allow(dead_code)]
fn calculate_all_paragraphs_height(
paragraph_groups: &mut [Vec<ParagraphBuilder>],
width: f32,

View File

@@ -684,6 +684,13 @@ impl Shape {
.get_or_init(|| self.calculate_extrect(shapes_pool, modifiers))
}
pub fn get_text_content(&self) -> &TextContent {
match &self.shape_type {
crate::shapes::Type::Text(text_content) => text_content,
_ => panic!("Shape is not of type Text"),
}
}
/// Calculates the bounding rectangle for a selrect shape's shadow, taking into account
/// stroke widths and shadow properties.
///

View File

@@ -397,7 +397,7 @@ impl TextContent {
)
});
let size = TextContentSize::new(width.ceil(), height.ceil(), width.ceil());
let size = TextContentSize::new(width.round(), height.round(), width.round());
TextContentLayoutResult(paragraph_builders, paragraphs, size)
}
@@ -414,7 +414,7 @@ impl TextContent {
.fold(0.0, |auto_height, paragraph| {
auto_height + paragraph.height()
});
let size = TextContentSize::new_with_size(width.ceil(), height.ceil());
let size = TextContentSize::new_with_size(width.round(), height.round());
TextContentLayoutResult(paragraph_builders, paragraphs, size)
}
@@ -424,14 +424,17 @@ impl TextContent {
let mut paragraph_builders = self.paragraph_builder_group_from_text(None);
let paragraphs =
self.build_paragraphs_from_paragraph_builders(&mut paragraph_builders, width);
let paragraph_height = paragraphs
let (_width, paragraph_height) = paragraphs
.iter()
.flatten()
.fold(0.0, |auto_height, paragraph| {
auto_height + paragraph.height()
.fold((0.0, 0.0), |(auto_width, auto_height), paragraph| {
(
f32::max(paragraph.max_intrinsic_width(), auto_width),
auto_height + paragraph.height()
)
});
let size = TextContentSize::new_with_size(width.ceil(), paragraph_height.ceil());
let size = TextContentSize::new_with_size(width.round(), paragraph_height.round());
TextContentLayoutResult(paragraph_builders, paragraphs, size)
}
@@ -464,6 +467,7 @@ impl TextContent {
}
pub fn update_layout(&mut self, selrect: Rect) -> TextContentSize {
println!("@@@ Updating text layout..., selrect: {:?}", selrect);
self.size.set_size(selrect.width(), selrect.height());
match self.grow_type() {