PhotoCraft: An open-source, photo editor written by scratch in pure Rust
feat(text): lay out area type inside a closed path
PanPDX4fda152dbd38
diff --git a/crates/doc/src/text.rs b/crates/doc/src/text.rsindex 6f7836fa4..070ab1132 100644--- a/crates/doc/src/text.rs+++ b/crates/doc/src/text.rs@@ -209,7 +209,8 @@ pub struct ParagraphRun {}/// Point text grows from its anchor (text-space origin = first baseline at the anchor);-/// paragraph (box) text wraps inside a rectangle; path text runs along a curve.+/// paragraph (box) text wraps inside a rectangle; path text runs along a curve; area text+/// wraps inside a closed path.#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]pub enum TextShape {#[default]@@ -221,6 +222,10 @@ pub enum TextShape {/// curve. `start` is the arc-length offset (px) of the text origin along the path; `flip`/// puts the text on the other side of the path.Path { path: super::vector::Subpath, start: f32, flip: bool },+ /// Area type: paragraph text that wraps inside the closed subpaths of `path` (text-space+ /// pixels, before the layer transform), combined with their path operations and fill rule.+ /// Text that doesn't fit is hidden, as in a text box.+ Area { path: super::vector::Path },}#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]diff --git a/crates/engine/src/type_extra_cmds.rs b/crates/engine/src/type_extra_cmds.rsindex efa2d791c..569af27c9 100644--- a/crates/engine/src/type_extra_cmds.rs+++ b/crates/engine/src/type_extra_cmds.rs@@ -44,7 +44,7 @@ fn has_point_text(s: &Session) -> std::result::Result<(), String> {match active_text(s)?.shape {TextShape::Point => Ok(()),TextShape::Box { .. } => Err("the type layer is already paragraph text".into()),- TextShape::Path { .. } => Err("detach the type layer from its path first".into()),+ TextShape::Path { .. } | TextShape::Area { .. } => Err("release the type layer from its path first".into()),}}@@ -53,6 +53,7 @@ fn has_box_text(s: &Session) -> std::result::Result<(), String> {TextShape::Box { .. } => Ok(()),TextShape::Point => Err("the type layer is already point text".into()),TextShape::Path { .. } => Err("the type layer is path text".into()),+ TextShape::Area { .. } => Err("the type layer is area type; release it from its path first".into()),}}diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rsindex e2bd223b3..0a09cbc08 100644--- a/crates/text/src/layout.rs+++ b/crates/text/src/layout.rs@@ -22,7 +22,7 @@ use parley::{PositionedLayoutItem, StyleProperty,};use photocraft_doc::TextLayer;-use photocraft_doc::text::{Caps, CharStyle, Kerning, Orientation, TextAlign, TextDirection, TextShape};+use photocraft_doc::text::{Caps, CharStyle, Kerning, Orientation, ParagraphStyle, TextAlign, TextDirection, TextShape};use skrifa::raw::types::Tag;use crate::fonts::FontDb;@@ -502,9 +502,18 @@ impl Layouter {let text = &t.text;let (box_rect, is_box) = match t.shape {TextShape::Box { x, y, width, height } => ((x, y, width, height), true),- // Path text is laid out as one point-text line, then bent onto the path.- TextShape::Point | TextShape::Path { .. } => ((0.0, 0.0, 0.0, 0.0), false),+ // Path text is laid out as one point-text line, then bent onto the path. Area text+ // places its own lines (`area` below).+ TextShape::Point | TextShape::Path { .. } | TextShape::Area { .. } => ((0.0, 0.0, 0.0, 0.0), false),};+ // Area type: the shape its lines wrap inside. A path that encloses nothing (only from a+ // hostile file; `type.fillPath` refuses one) lays out as point text.+ let area = match &t.shape {+ TextShape::Area { path } => crate::area::AreaShape::new(path, vertical),+ _ => None,+ };+ // Box and area text wrap, align within their lines and hide what doesn't fit.+ let wraps = is_box || area.is_some();let mut prev_baseline: Option<f32> = None;let mut pending_space = 0.0f32;let mut stop = false;@@ -640,35 +649,66 @@ impl Layouter {// Box extent along the lines: width, or height for vertical type (columns).let (line_origin, line_len) = if vertical { (box_rect.1, box_rect.3) } else { (box_rect.0, box_rect.2) };let avail = if is_box { Some((line_len - indent_start - indent_end).max(1.0)) } else { None };- let break_lines = |layout: &mut Layout<RunBrush>| {+ // Area type: plan rows with the first style's metrics, and the first row no higher+ // than one leading (plus paragraph spacing) below the previous paragraph's last line.+ let area_style = out.styles[style_at(prange.start)].clone();+ let guess = AreaMetrics {+ ascent: if vertical { first_px * 0.5 } else { first_px * 0.8 },+ descent: if vertical { first_px * 0.5 } else { first_px * 0.2 },+ leading: area_style.leading_pt.map_or(ps.auto_leading * first_px, |l| l * k),+ };+ let space_before = if prev_baseline.is_some() { ps.space_before_pt * k } else { 0.0 };+ let area_from = |m: AreaMetrics| match (&area, prev_baseline) {+ (Some(_), Some(b)) => b + m.leading + pending_space + space_before,+ (Some(a), None) => a.bounds[1] + m.ascent,+ (None, _) => 0.0,+ };+ let break_lines = |layout: &mut Layout<RunBrush>| -> (Vec<Slot>, bool) {if first_indent != 0.0 {layout.set_text_indent(first_indent, IndentOptions::default());}- layout.break_all_lines(avail);+ let Some(a) = &area else {+ layout.break_all_lines(avail);+ return (Vec::new(), false);+ };+ let mut r = break_area(layout, a, area_from(guess), guess, (indent_start, indent_end), first_px);+ // Re-plan once with the real metrics if a line is taller than the guess.+ let real = area_metrics(layout, &out.styles, &ps, k, &area_style, vertical);+ if real.ascent > guess.ascent + 0.5 || real.descent > guess.descent + 0.5 || real.leading > guess.leading + 0.5 {+ let m = AreaMetrics {+ ascent: real.ascent.max(guess.ascent),+ descent: real.descent.max(guess.descent),+ leading: real.leading.max(guess.leading),+ };+ r = break_area(layout, a, area_from(m), m, (indent_start, indent_end), first_px);+ }+ r};// Paragraph text: a tab is never wider than a space plus one tab interval, so breaking// lines at that width keeps the expanded tabs inside the box, but can wrap early.// Break again with the tab widths that layout gives until they agree; if they don't// settle, keep the safe layout.- let wide = if is_box { tab_px } else { 0.0 };+ let wide = if wraps { tab_px } else { 0.0 };let mut layout = build(&mut self.lcx, &mut fonts.fcx, &vec![wide; tabs.len()]);- break_lines(&mut layout);- if is_box && !tabs.is_empty() {+ let (mut area_slots, mut area_full) = break_lines(&mut layout);+ if wraps && !tabs.is_empty() {let first = tab_widths(&layout, &tabs, &out.styles, indent_start, first_indent, tab_px);let mut want: Vec<f32> = first.iter().map(|t| t.width).collect();for _ in 0..4 {let spacing: Vec<f32> = first.iter().zip(&want).map(|(t, w)| w / t.scale - (t.advance - tab_px)).collect();let mut next = build(&mut self.lcx, &mut fonts.fcx, &spacing);- break_lines(&mut next);+ let (next_slots, next_full) = break_lines(&mut next);let got: Vec<f32> = tab_widths(&next, &tabs, &out.styles, indent_start, first_indent, tab_px).iter().map(|t| t.width).collect();if got.iter().zip(&want).all(|(a, b)| (a - b).abs() < 0.01) {layout = next;+ area_slots = next_slots;+ area_full = next_full;break;}want = got;}}- let alignment = if is_box {+ let alignment = if wraps {match ps.align {TextAlign::Left => Alignment::Left,TextAlign::Center => Alignment::Center,@@ -678,7 +718,7 @@ impl Layouter {} else {Alignment::Left};- layout.align(alignment, AlignmentOptions { align_when_overflowing: !is_box });+ layout.align(alignment, AlignmentOptions { align_when_overflowing: !wraps });// Stack lines.if prev_baseline.is_some() {@@ -686,6 +726,12 @@ impl Layouter {}let nlines = layout.len();for (li, line) in layout.lines().enumerate() {+ // Area type: the slot the breaker gave this line.+ let slot = area_slots.get(li).copied();+ if area.is_some() && slot.is_none() {+ stop = true;+ break;+ }let m = *line.metrics();// Largest leading among the glyph runs of the line.let mut leading = 0.0f32;@@ -716,14 +762,15 @@ impl Layouter {} else {(first_px * 0.8, first_px * 0.2)};- let baseline = match prev_baseline {+ let baseline = match (slot, prev_baseline) {+ (Some(s), _) => s.baseline,// The first column's right edge touches the box's right edge.- None if is_box && vertical => -(box_rect.0 + box_rect.2) + col_half,+ (None, None) if is_box && vertical => -(box_rect.0 + box_rect.2) + col_half,// Photoshop's "first baseline: ascent": the top of the tallest ascender// (height of 'd') touches the box top, not the font's hhea ascent.- None if is_box => box_rect.1 + first_ascent(&line).unwrap_or(ascent),- None => 0.0,- Some(b) => b + leading + pending_space,+ (None, None) if is_box => box_rect.1 + first_ascent(&line).unwrap_or(ascent),+ (None, None) => 0.0,+ (None, Some(b)) => b + leading + pending_space,};pending_space = 0.0;// Photoshop keeps a horizontal line whose baseline is inside the box and lets@@ -847,7 +894,7 @@ impl Layouter {cursors.push((run.index(), g2s, 0, base));}// Parley aligned box lines without the kerning.- let kern_align = if is_box {+ let kern_align = if wraps {match ps.align {TextAlign::Center => line_kern / 2.0,TextAlign::Right => line_kern,@@ -856,9 +903,13 @@ impl Layouter {} else {0.0};- let dx = if is_box {- let base = line_origin + indent_start - kern_align;- let slack = avail.unwrap_or(0.0) - adv - line_kern;+ // Box and area lines start at their box or span edge; the line's own width+ // (`line_avail`) takes the justify-last-line slack.+ let line_avail = slot.map(|s| s.width).or(avail);+ let dx = if wraps {+ let origin = slot.map_or(line_origin, |s| s.x);+ let base = origin + indent_start - kern_align;+ let slack = line_avail.unwrap_or(0.0) - adv - line_kern;base + if last_line {match ps.align {TextAlign::JustifyCenter => slack * 0.5 - m.offset,@@ -876,7 +927,7 @@ impl Layouter {};target - m.offset};- let justify_all = is_box && last_line && ps.align == TextAlign::JustifyAll;+ let justify_all = wraps && last_line && ps.align == TextAlign::JustifyAll;let line_index = out.lines.len();let lr = line.text_range();let g0 = out.glyphs.len();@@ -996,7 +1047,7 @@ impl Layouter {extra -= squeeze_punctuation(text, &mut out.clusters[c0..], &mut out.glyphs[g0..]);}if justify_all {- let slack = avail.unwrap_or(0.0) - (adv + extra);+ let slack = line_avail.unwrap_or(0.0) - (adv + extra);let n = out.clusters.len() - c0;if n > 1 && slack > 0.0 {let step = slack / (n - 1) as f32;@@ -1037,6 +1088,10 @@ impl Layouter {paragraph: pi,});}+ if area_full {+ // The shape is full: later paragraphs are hidden too.+ stop = true;+ }pending_space += ps.space_after_pt * k;}out@@ -1048,6 +1103,147 @@ fn next_tab_stop(pos: f32, interval: f32) -> f32 {if interval > 0.0 && pos.is_finite() { ((pos + 1e-3) / interval).floor() * interval + interval } else { pos }}+/// Where one line of area type goes: its baseline, the left edge of its span, and the width+/// it may fill (the span less the paragraph's indents).+#[derive(Clone, Copy, Debug, PartialEq)]+struct Slot {+ baseline: f32,+ x: f32,+ width: f32,+}++/// Line metrics an area paragraph is planned with.+#[derive(Clone, Copy, Debug)]+struct AreaMetrics {+ ascent: f32,+ descent: f32,+ leading: f32,+}++/// Most spans one area paragraph skips because the word that would start there doesn't fit (each+/// skip breaks the paragraph again).+const MAX_AREA_SKIPS: usize = 64;++/// Breaks one paragraph of area type a line at a time, each line as wide as the next span the+/// shape leaves (rows from `from` down, spans left to right). Returns each line's slot and+/// whether text was left over because the shape ran out (hidden, like box overflow).+///+/// A span too narrow for the word that would start it (the narrow top of an ellipse) is left+/// empty and the word goes to the next span, as long as the word fits across the shape's full+/// width somewhere; a word wider than the whole shape overflows its span, as in a text box.+fn break_area(+ layout: &mut Layout<RunBrush>,+ area: &crate::area::AreaShape,+ from: f32,+ m: AreaMetrics,+ indents: (f32, f32),+ min_width: f32,+) -> (Vec<Slot>, bool) {+ let widest = area.bounds[2] - area.bounds[0] - indents.0 - indents.1;+ // Spans left empty, by baseline and left edge (exact: every pass computes them the same way).+ let mut skip: Vec<(f32, f32)> = Vec::new();+ loop {+ let (slots, overflow) = break_area_pass(layout, area, from, m, indents, min_width, &skip);+ // Skipping only makes progress when rows move down.+ if m.leading < 1.0 || skip.len() >= MAX_AREA_SKIPS {+ return (slots, overflow);+ }+ let narrow = layout.lines().zip(&slots).find_map(|(line, s)| {+ let lm = line.metrics();+ let content = lm.advance - lm.trailing_whitespace;+ (content > s.width + 0.5 && content <= widest).then_some((s.baseline, s.x))+ });+ match narrow {+ Some(span) => skip.push(span),+ None => return (slots, overflow),+ }+ }+}++/// One pass of [`break_area`], leaving the spans in `skip` empty.+fn break_area_pass(+ layout: &mut Layout<RunBrush>,+ area: &crate::area::AreaShape,+ from: f32,+ m: AreaMetrics,+ indents: (f32, f32),+ min_width: f32,+ skip: &[(f32, f32)],+) -> (Vec<Slot>, bool) {+ let mut slots = Vec::new();+ let mut br = layout.break_lines();+ // Per-line widths below are never wider than this, as parley requires.+ br.state_mut().set_layout_max_advance(f32::INFINITY);+ let mut baseline = from;+ // The current row's spans, rightmost first so `pop` takes them left to right.+ let mut row: Vec<(f32, f32)> = Vec::new();+ let mut overflow = false;+ while !br.is_done() {+ if row.is_empty() {+ match area.next_row(baseline, m.ascent, m.descent, min_width) {+ Some((b, mut spans)) => {+ baseline = b;+ spans.reverse();+ row = spans;+ }+ None => {+ overflow = true;+ break;+ }+ }+ }+ let Some((x0, x1)) = row.pop() else { break };+ if !skip.contains(&(baseline, x0)) {+ let width = (x1 - x0 - indents.0 - indents.1).max(1.0);+ br.state_mut().set_line_max_advance(width);+ if br.break_next().is_none() {+ break;+ }+ // Parley aligns each line within the width it was broken at.+ br.set_prior_line_width(width);+ slots.push(Slot { baseline, x: x0, width });+ }+ if row.is_empty() {+ baseline += m.leading;+ }+ }+ br.finish();+ (slots, overflow)+}++/// Largest ascent, descent and leading over the lines of a broken paragraph, computed the same+/// way the line loop below does, so an area paragraph can be re-planned with its real metrics.+fn area_metrics(layout: &Layout<RunBrush>, styles: &[CharStyle], ps: &ParagraphStyle, k: f32, fallback: &CharStyle, vertical: bool) -> AreaMetrics {+ let first_px = fallback.size_pt * k;+ let mut out = AreaMetrics { ascent: 0.0, descent: 0.0, leading: 0.0 };+ for line in layout.lines() {+ let m = *line.metrics();+ let mut leading = 0.0f32;+ let mut half = 0.0f32;+ for item in line.items() {+ if let PositionedLayoutItem::GlyphRun(gr) = item {+ let px = gr.run().font_size();+ half = half.max(px * 0.5);+ let lp = styles.get(gr.style().brush.0 as usize).and_then(|st| st.leading_pt);+ leading = leading.max(lp.map_or(ps.auto_leading * px, |l| l * k));+ }+ }+ if leading == 0.0 {+ leading = fallback.leading_pt.map_or(ps.auto_leading * first_px, |l| l * k);+ }+ let (a, d) = if vertical {+ let h = if half > 0.0 { half } else { first_px * 0.5 };+ (h, h)+ } else if m.ascent > 0.0 || m.descent > 0.0 {+ (m.ascent, m.descent)+ } else {+ (first_px * 0.8, first_px * 0.2)+ };+ out = AreaMetrics { ascent: out.ascent.max(a), descent: out.descent.max(d), leading: out.leading.max(leading) };+ }+ out+}+/// A tab as line breaking saw it: its shaped advance (before horizontal scale), the horizontal/// scale, and the width that takes it to its tab stop on its line.struct TabWidth {diff --git a/crates/text/src/psd.rs b/crates/text/src/psd.rsindex 0092fcf92..4c8c966ac 100644--- a/crates/text/src/psd.rs+++ b/crates/text/src/psd.rs@@ -248,7 +248,7 @@ pub fn apply_txt2_frame(layer: &mut TextLayer, tysh: &[u8], txt2: &E) {let p = arr_f(frame.get("0"));if p.len() == 2 && p.iter().all(|v| v.is_finite()) { Affine::translate(p[0], p[1]) } else { Affine::IDENTITY }}- TextShape::Box { .. } => Affine::IDENTITY,+ TextShape::Box { .. } | TextShape::Area { .. } => Affine::IDENTITY,};let to_text = matrix.mul(&at);if to_text == Affine::IDENTITY || to_text.determinant().abs() <= 1e-9 {@@ -261,7 +261,7 @@ pub fn apply_txt2_frame(layer: &mut TextLayer, tysh: &[u8], txt2: &E) {// again would move it twice.let origin = match layer.shape {TextShape::Point | TextShape::Path { .. } => Point::new(0.0, 0.0),- TextShape::Box { .. } => {+ TextShape::Box { .. } | TextShape::Area { .. } => {let b = engine_data(&t.text).map(|e| {arr_f(@@ -1188,10 +1188,16 @@ pub fn build_engine_data(layer: &TextLayer, template: Option<E>, dpi: f32) -> Eif dict.get("UseFractionalGlyphWidths").is_none() {dict.set("UseFractionalGlyphWidths", E::Bool(true));}- let (shape_type, box_bounds) = match layer.shape {+ let (shape_type, box_bounds) = match &layer.shape {// PSD path type is not written yet: the layer saves as point text.TextShape::Point | TextShape::Path { .. } => (0, None),- TextShape::Box { x, y, width, height } => (1, Some([x, y, x + width, y + height])),+ TextShape::Box { x, y, width, height } => (1, Some([*x, *y, x + width, y + height])),+ // PSD area type is not written yet: the layer saves as paragraph text over the path's+ // bounds, so Photoshop opens it with the same text wrapping in a rectangle.+ TextShape::Area { path } => match path.control_bounds() {+ Some((x0, y0, x1, y1)) => (1, Some([x0 as f32, y0 as f32, x1 as f32, y1 as f32])),+ None => (0, None),+ },};let mut photoshop = E::Dict(vec![("ShapeType".into(), E::Int(shape_type))]);match box_bounds {@@ -1303,9 +1309,14 @@ pub fn build_tysh(layer: &TextLayer, dpi: f32, ink: Option<[f32; 4]>) -> Vec<u8>},),);- let [l, t, r, b] = match (&layer.shape, ink) {- (&TextShape::Box { x, y, width, height }, _) => [x, y, x + width, y + height],- (_, Some(i)) => i,+ let area = match &layer.shape {+ TextShape::Area { path } => path.control_bounds().map(|(x0, y0, x1, y1)| [x0 as f32, y0 as f32, x1 as f32, y1 as f32]),+ _ => None,+ };+ let [l, t, r, b] = match (&layer.shape, area, ink) {+ (&TextShape::Box { x, y, width, height }, _, _) => [x, y, x + width, y + height],+ (_, Some(a), _) => a,+ (_, _, Some(i)) => i,_ => [0.0; 4],};let rect = |cls: &str| {diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rsindex 13de665cc..03f37f7f0 100644--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@ -2008,3 +2008,126 @@ fn hyphenless_postscript_names_split_trailing_styles() {assert_eq!((r.family.as_str(), r.weight, r.italic), (family, weight, italic), "{ps}");}}++/// Closed polygon in text space.+fn area_poly(pts: &[(f64, f64)]) -> photocraft_doc::vector::Path {+ photocraft_doc::vector::Path::new(vec![photocraft_doc::vector::Subpath::polygon(pts)])+}++/// An ellipse from four kappa curves inside `[x, y, w, h]`.+fn area_ellipse(x: f64, y: f64, w: f64, h: f64) -> photocraft_doc::vector::Path {+ use photocraft_doc::vector::{Knot, Subpath};+ use photocraft_geom::Point;+ let (rx, ry, cx, cy) = (w / 2.0, h / 2.0, x + w / 2.0, y + h / 2.0);+ let (kx, ky) = (0.552_284_75 * rx, 0.552_284_75 * ry);+ let p = Point::new;+ let knots = vec![+ Knot::smooth(p(cx, y), p(cx - kx, y), p(cx + kx, y)),+ Knot::smooth(p(x + w, cy), p(x + w, cy - ky), p(x + w, cy + ky)),+ Knot::smooth(p(cx, y + h), p(cx + kx, y + h), p(cx - kx, y + h)),+ Knot::smooth(p(x, cy), p(x, cy + ky), p(x, cy - ky)),+ ];+ photocraft_doc::vector::Path::new(vec![Subpath { closed: true, knots, op: Default::default() }])+}++const BUBBLE_TEXT: &str = "Well, that went better than expected. Now where did the cat hide the keys this time?";++/// Every laid-out line sits inside a span of the shape across its band.+fn assert_lines_inside(l: &crate::TextLayout, path: &photocraft_doc::vector::Path) {+ let a = crate::area::AreaShape::new(path, false).unwrap();+ for line in &l.lines {+ let spans = a.spans(line.baseline - line.ascent, line.baseline + line.descent, 0.0);+ assert!(spans.iter().any(|&(s0, s1)| line.x0 >= s0 - 0.5 && line.x1 <= s1 + 0.5), "{line:?} not inside {spans:?}");+ }+}++#[test]+fn area_text_stays_inside_an_ellipse() {+ let mut e = TextEngine::new();+ let mut t = point(BUBBLE_TEXT, 18.0);+ let path = area_ellipse(0.0, 0.0, 260.0, 160.0);+ t.shape = TextShape::Area { path: path.clone() };+ let l = e.layout(&t, 72.0);+ assert!(l.lines.len() >= 4, "{}", l.lines.len());+ assert_lines_inside(&l, &path);+ // The top of an ellipse is too narrow for a line at its very edge: the first line starts lower.+ assert!(l.lines[0].baseline - l.lines[0].ascent > 1.0, "{:?}", l.lines[0]);+ // Lines go down the shape in text order.+ for w in l.lines.windows(2) {+ assert!(w[1].baseline >= w[0].baseline && w[1].range.start >= w[0].range.end, "{w:?}");+ }+ // Middle lines are wider than the first (the ellipse is widest in the middle).+ let width = |i: usize| l.lines[i].x1 - l.lines[i].x0;+ assert!(width(l.lines.len() / 2) > width(0), "{l:?}");+}++#[test]+fn area_overflow_is_hidden() {+ let mut e = TextEngine::new();+ let mut t = point(&BUBBLE_TEXT.repeat(10), 14.0);+ let path = area_ellipse(0.0, 0.0, 200.0, 80.0);+ t.shape = TextShape::Area { path: path.clone() };+ let l = e.layout(&t, 72.0);+ assert!(!l.lines.is_empty());+ assert!(l.lines.last().unwrap().range.end < t.text.len(), "the rest is hidden");+ assert_lines_inside(&l, &path);+}++#[test]+fn area_lines_center_in_their_span() {+ let mut e = TextEngine::new();+ let mut t = point(BUBBLE_TEXT, 12.0);+ t.paragraphs = vec![ParagraphRun { len: t.text.len(), style: ParagraphStyle { align: TextAlign::Center, ..Default::default() } }];+ // A diamond: every row's span is centred on x = 100.+ t.shape = TextShape::Area { path: area_poly(&[(100.0, 0.0), (200.0, 100.0), (100.0, 200.0), (0.0, 100.0)]) };+ let l = e.layout(&t, 72.0);+ assert!(l.lines.len() >= 3);+ for line in &l.lines {+ assert!(((line.x0 + line.x1) / 2.0 - 100.0).abs() < 1.5, "{line:?}");+ }+}++#[test]+fn area_rows_split_around_a_hole() {+ use photocraft_doc::vector::{PathOp, Subpath};+ let mut e = TextEngine::new();+ let mut t = point(&BUBBLE_TEXT.repeat(6), 12.0);+ let path = photocraft_doc::vector::Path::new(vec![+ Subpath::polygon(&[(0.0, 0.0), (300.0, 0.0), (300.0, 300.0), (0.0, 300.0)]),+ Subpath::polygon(&[(120.0, 100.0), (180.0, 100.0), (180.0, 200.0), (120.0, 200.0)]).with_op(PathOp::Subtract),+ ]);+ t.shape = TextShape::Area { path: path.clone() };+ let l = e.layout(&t, 72.0);+ assert_lines_inside(&l, &path);+ // Some baseline carries two lines: one left of the hole, one right of it, left first.+ let w = l.lines.windows(2).find(|w| (w[0].baseline - w[1].baseline).abs() < 1e-3).expect("a row split around the hole");+ let (left, right) = (&w[0], &w[1]);+ assert!(left.x1 <= 120.5 && right.x0 >= 179.5, "{left:?} {right:?}");+}++#[test]+fn vertical_area_text_stays_inside() {+ use photocraft_doc::text::Orientation;+ let mut e = TextEngine::new();+ let mut t = point(BUBBLE_TEXT, 12.0);+ t.orientation = Orientation::Vertical;+ t.shape = TextShape::Area { path: area_poly(&[(0.0, 0.0), (120.0, 0.0), (120.0, 200.0), (0.0, 200.0)]) };+ let l = e.layout(&t, 72.0);+ assert!(l.lines.len() >= 2, "several columns");+ for g in &l.glyphs {+ assert!(g.x > -1.0 && g.x < 121.0 && g.y > -1.0 && g.y < 201.0, "{g:?}");+ }+}++#[test]+fn area_without_room_lays_out_nothing_and_a_bad_path_lays_out_as_point_text() {+ let mut e = TextEngine::new();+ let mut t = point("Hello", 40.0);+ // 10 px tall: no 40 pt line fits.+ t.shape = TextShape::Area { path: area_poly(&[(0.0, 0.0), (300.0, 0.0), (300.0, 10.0), (0.0, 10.0)]) };+ assert!(e.layout(&t, 72.0).lines.is_empty());+ // An open path encloses nothing (only reachable from a hostile file): point text, no panic.+ t.shape = TextShape::Area { path: photocraft_doc::vector::Path::new(vec![photocraft_doc::vector::Subpath::polyline(&[(0.0, 0.0), (9.0, 9.0)])]) };+ let point_layout = e.layout(&point("Hello", 40.0), 72.0);+ assert_eq!(e.layout(&t, 72.0).lines.len(), point_layout.lines.len());+}diff --git a/crates/ui-egui/src/type_transform.rs b/crates/ui-egui/src/type_transform.rsindex 0c1b4837e..890e4b968 100644--- a/crates/ui-egui/src/type_transform.rs+++ b/crates/ui-egui/src/type_transform.rs@@ -92,8 +92,13 @@ pub(crate) fn frame(app: &mut PhotocraftApp) -> Option<TransformSession> {if !aff.m.iter().all(|v| v.is_finite()) || aff.inverse().is_none() {return None;}- let rect = match shape {- TextShape::Box { x, y, width, height } => [x, y, x + width, y + height].map(f64::from),+ let area = match &shape {+ TextShape::Area { path } => path.control_bounds(),+ _ => None,+ };+ let rect = match (shape, area) {+ (TextShape::Box { x, y, width, height }, _) => [x, y, x + width, y + height].map(f64::from),+ (_, Some((x0, y0, x1, y1))) => [x0, y0, x1, y1],_ => {let mut r = layout.bounds().unwrap_or([0.0, -size * layout.px_per_pt, 1.0, 0.0]).map(f64::from);// Empty lines still have a caret and can be moved/scaled, without inventing glyphs.