Text grow width/height

This commit is contained in:
alonso.torres 2025-05-06 17:23:46 +02:00 committed by Alonso Torres
parent 6b300d516b
commit 568af52ebc
13 changed files with 381 additions and 106 deletions

View file

@ -356,6 +356,7 @@ impl RenderState {
s.canvas().concat(&matrix);
});
let text_content = text_content.new_bounds(shape.selrect());
let paragraphs = text_content.get_skia_paragraphs(&self.fonts.font_collection());
shadows::render_text_drop_shadows(self, &shape, &paragraphs, antialias);

View file

@ -711,9 +711,9 @@ impl Shape {
}
pub fn clear_text(&mut self) {
match self.shape_type {
Type::Text(_) => {
let new_text_content = TextContent::new(self.selrect);
match &self.shape_type {
Type::Text(old_text_content) => {
let new_text_content = TextContent::new(self.selrect, old_text_content.grow_type());
self.shape_type = Type::Text(new_text_content);
}
_ => {}

View file

@ -12,19 +12,57 @@ use super::FontFamily;
use crate::utils::uuid_from_u32;
use crate::Uuid;
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum GrowType {
Fixed,
AutoWidth,
AutoHeight,
}
impl GrowType {
pub fn from(grow_type: u8) -> Self {
match grow_type {
0 => Self::Fixed,
1 => Self::AutoWidth,
2 => Self::AutoHeight,
_ => unreachable!(),
}
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct TextContent {
paragraphs: Vec<Paragraph>,
bounds: Rect,
grow_type: GrowType,
}
pub fn set_paragraphs_width(width: f32, paragraphs: &mut Vec<Vec<skia::textlayout::Paragraph>>) {
for group in paragraphs {
for paragraph in group {
paragraph.layout(width)
}
}
}
impl TextContent {
pub fn new(bounds: Rect) -> Self {
pub fn new(bounds: Rect, grow_type: GrowType) -> Self {
let mut res = Self::default();
res.bounds = bounds;
res.grow_type = grow_type;
res
}
pub fn new_bounds(&self, bounds: Rect) -> Self {
let paragraphs = self.paragraphs.clone();
let grow_type = self.grow_type;
Self {
paragraphs,
bounds,
grow_type,
}
}
pub fn set_xywh(&mut self, x: f32, y: f32, w: f32, h: f32) {
self.bounds = Rect::from_xywh(x, y, w, h);
}
@ -97,22 +135,25 @@ impl TextContent {
paragraph_group
}
pub fn collect_paragraphs(
&self,
mut paragraphs: Vec<Vec<skia::textlayout::Paragraph>>,
) -> Vec<Vec<skia::textlayout::Paragraph>> {
if self.grow_type() == GrowType::AutoWidth {
set_paragraphs_width(f32::MAX, &mut paragraphs);
let max_width = auto_width(&paragraphs).ceil();
set_paragraphs_width(max_width, &mut paragraphs);
} else {
set_paragraphs_width(self.width(), &mut paragraphs);
}
paragraphs
}
pub fn get_skia_paragraphs(
&self,
fonts: &FontCollection,
) -> Vec<Vec<skia::textlayout::Paragraph>> {
self.to_paragraphs(fonts)
.into_iter()
.map(|group| {
group
.into_iter()
.map(|mut paragraph| {
paragraph.layout(self.width());
paragraph
})
.collect()
})
.collect()
self.collect_paragraphs(self.to_paragraphs(fonts))
}
pub fn get_skia_stroke_paragraphs(
@ -120,18 +161,15 @@ impl TextContent {
fonts: &FontCollection,
paints: &Vec<Paint>,
) -> Vec<Vec<skia::textlayout::Paragraph>> {
self.to_stroke_paragraphs(fonts, paints)
.into_iter()
.map(|group| {
group
.into_iter()
.map(|mut paragraph| {
paragraph.layout(self.width());
paragraph
})
.collect()
})
.collect()
self.collect_paragraphs(self.to_stroke_paragraphs(fonts, paints))
}
pub fn grow_type(&self) -> GrowType {
self.grow_type
}
pub fn set_grow_type(&mut self, grow_type: GrowType) {
self.grow_type = grow_type;
}
}
@ -140,6 +178,7 @@ impl Default for TextContent {
Self {
paragraphs: vec![],
bounds: Rect::default(),
grow_type: GrowType::Fixed,
}
}
}
@ -495,3 +534,16 @@ impl From<&Vec<u8>> for RawTextData {
Self { paragraph }
}
}
pub fn auto_width(paragraphs: &Vec<Vec<skia::textlayout::Paragraph>>) -> f32 {
paragraphs.iter().flatten().fold(0.0, |auto_width, p| {
f32::max(p.max_intrinsic_width(), auto_width)
})
}
pub fn auto_height(paragraphs: &Vec<Vec<skia::textlayout::Paragraph>>) -> f32 {
paragraphs
.iter()
.flatten()
.fold(0.0, |auto_height, p| auto_height + p.height())
}

View file

@ -1,7 +1,8 @@
use crate::mem;
use crate::shapes::RawTextData;
use crate::with_current_shape;
use crate::shapes::{auto_height, auto_width, GrowType, RawTextData, Type};
use crate::STATE;
use crate::{with_current_shape, with_state};
#[no_mangle]
pub extern "C" fn clear_shape_text() {
@ -22,3 +23,48 @@ pub extern "C" fn set_shape_text_content() {
mem::free_bytes();
}
#[no_mangle]
pub extern "C" fn set_shape_grow_type(grow_type: u8) {
with_current_shape!(state, |shape: &mut Shape| {
if let Type::Text(text_content) = &mut shape.shape_type {
text_content.set_grow_type(GrowType::from(grow_type));
}
});
}
#[no_mangle]
pub extern "C" fn get_text_dimensions() -> *mut u8 {
let font_col;
with_state!(state, {
font_col = state.render_state.fonts.font_collection();
});
let mut width = 0.01;
let mut height = 0.01;
with_current_shape!(state, |shape: &mut Shape| {
width = shape.selrect.width();
height = shape.selrect.height();
if let Type::Text(content) = &shape.shape_type {
match content.grow_type() {
GrowType::AutoWidth => {
let paragraphs = content.get_skia_paragraphs(font_col);
width = auto_width(&paragraphs);
height = auto_height(&paragraphs);
}
GrowType::AutoHeight => {
let paragraphs = content.get_skia_paragraphs(font_col);
height = auto_height(&paragraphs);
}
_ => {}
}
}
});
let mut bytes = Vec::<u8>::with_capacity(8);
bytes.resize(8, 0);
bytes[0..4].clone_from_slice(&width.to_le_bytes());
bytes[4..8].clone_from_slice(&height.to_le_bytes());
mem::write_bytes(bytes)
}