mirror of
https://github.com/penpot/penpot.git
synced 2025-12-12 06:24:17 +01:00
♻️ Move auto_width and auto_height to their own textlayout module
This commit is contained in:
@@ -7,6 +7,7 @@ mod performance;
|
||||
mod render;
|
||||
mod shapes;
|
||||
mod state;
|
||||
mod textlayout;
|
||||
mod tiles;
|
||||
mod utils;
|
||||
mod uuid;
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
pub mod common;
|
||||
|
||||
mod constraints;
|
||||
mod flex_layout;
|
||||
|
||||
pub mod common;
|
||||
pub mod grid_layout;
|
||||
|
||||
use crate::math::{self as math, bools, identitish, Bounds, Matrix, Point};
|
||||
use common::GetBounds;
|
||||
|
||||
use crate::math::bools;
|
||||
use crate::math::{self as math, identitish, Bounds, Matrix, Point};
|
||||
|
||||
use crate::shapes::{
|
||||
auto_height, ConstraintH, ConstraintV, Frame, Group, GrowType, Layout, Modifier, Shape,
|
||||
StructureEntry, TransformEntry, Type,
|
||||
ConstraintH, ConstraintV, Frame, Group, GrowType, Layout, Modifier, Shape, StructureEntry,
|
||||
TransformEntry, Type,
|
||||
};
|
||||
use crate::state::ShapesPool;
|
||||
use crate::state::State;
|
||||
use crate::state::{ShapesPool, State};
|
||||
use crate::textlayout::auto_height;
|
||||
use crate::uuid::Uuid;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
|
||||
@@ -13,6 +13,7 @@ use std::collections::HashSet;
|
||||
|
||||
use super::FontFamily;
|
||||
use crate::shapes::{self, merge_fills, set_paint_fill, Stroke, StrokeKind};
|
||||
use crate::textlayout::{auto_height, auto_width};
|
||||
use crate::utils::{get_fallback_fonts, get_font_collection, uuid_from_u32};
|
||||
use crate::wasm::fills::parse_fills_from_bytes;
|
||||
use crate::Uuid;
|
||||
@@ -42,30 +43,6 @@ pub struct TextContent {
|
||||
pub grow_type: GrowType,
|
||||
}
|
||||
|
||||
pub fn build_paragraphs_with_width(
|
||||
paragraphs: &mut [Vec<ParagraphBuilder>],
|
||||
width: f32,
|
||||
) -> Vec<Vec<skia_safe::textlayout::Paragraph>> {
|
||||
paragraphs
|
||||
.iter_mut()
|
||||
.map(|builders| {
|
||||
builders
|
||||
.iter_mut()
|
||||
.map(|builder| {
|
||||
let mut paragraph = builder.build();
|
||||
// For auto-width, always layout with infinite width first to get intrinsic width
|
||||
paragraph.layout(f32::MAX);
|
||||
let intrinsic_width = paragraph.max_intrinsic_width().ceil();
|
||||
// Use the larger of the requested width or intrinsic width to prevent line breaks
|
||||
let final_width = f32::max(width, intrinsic_width);
|
||||
paragraph.layout(final_width);
|
||||
paragraph
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
impl TextContent {
|
||||
pub fn new(bounds: Rect, grow_type: GrowType) -> Self {
|
||||
Self {
|
||||
@@ -742,34 +719,6 @@ impl From<&Vec<u8>> for RawTextData {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_built_paragraphs(
|
||||
paragraphs: &mut [Vec<ParagraphBuilder>],
|
||||
width: f32,
|
||||
) -> Vec<Vec<skia_safe::textlayout::Paragraph>> {
|
||||
build_paragraphs_with_width(paragraphs, 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 auto_height(paragraphs: &mut [Vec<ParagraphBuilder>], width: f32) -> f32 {
|
||||
paragraphs.iter_mut().fold(0.0, |auto_height, p| {
|
||||
p.iter_mut().fold(auto_height, |auto_height, paragraph| {
|
||||
let mut paragraph = paragraph.build();
|
||||
paragraph.layout(width);
|
||||
auto_height + paragraph.height()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn get_text_stroke_paints_with_shadows(
|
||||
stroke: &Stroke,
|
||||
blur: Option<&ImageFilter>,
|
||||
|
||||
53
render-wasm/src/textlayout.rs
Normal file
53
render-wasm/src/textlayout.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
use skia_safe::textlayout::ParagraphBuilder;
|
||||
|
||||
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 auto_height(paragraphs: &mut [Vec<ParagraphBuilder>], width: f32) -> f32 {
|
||||
paragraphs.iter_mut().fold(0.0, |auto_height, p| {
|
||||
p.iter_mut().fold(auto_height, |auto_height, paragraph| {
|
||||
let mut paragraph = paragraph.build();
|
||||
paragraph.layout(width);
|
||||
auto_height + paragraph.height()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
pub fn build_paragraphs_with_width(
|
||||
paragraphs: &mut [Vec<ParagraphBuilder>],
|
||||
width: f32,
|
||||
) -> Vec<Vec<skia_safe::textlayout::Paragraph>> {
|
||||
paragraphs
|
||||
.iter_mut()
|
||||
.map(|builders| {
|
||||
builders
|
||||
.iter_mut()
|
||||
.map(|builder| {
|
||||
let mut paragraph = builder.build();
|
||||
// For auto-width, always layout with infinite width first to get intrinsic width
|
||||
paragraph.layout(f32::MAX);
|
||||
let intrinsic_width = paragraph.max_intrinsic_width().ceil();
|
||||
// Use the larger of the requested width or intrinsic width to prevent line breaks
|
||||
let final_width = f32::max(width, intrinsic_width);
|
||||
paragraph.layout(final_width);
|
||||
paragraph
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_built_paragraphs(
|
||||
paragraphs: &mut [Vec<ParagraphBuilder>],
|
||||
width: f32,
|
||||
) -> Vec<Vec<skia_safe::textlayout::Paragraph>> {
|
||||
build_paragraphs_with_width(paragraphs, width)
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
use crate::mem;
|
||||
use crate::shapes::{auto_height, build_paragraphs_with_width, GrowType, RawTextData, Type};
|
||||
|
||||
use crate::STATE;
|
||||
use crate::{with_current_shape, with_current_shape_mut};
|
||||
use crate::shapes::{GrowType, RawTextData, Type};
|
||||
use crate::textlayout::{auto_height, build_paragraphs_with_width};
|
||||
use crate::{with_current_shape, with_current_shape_mut, STATE};
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn clear_shape_text() {
|
||||
|
||||
Reference in New Issue
Block a user