PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(text): area type breaks at the span width divided by horizontal scale
PanPDX27ad078860a2
diff --git a/crates/text/src/layout.rs b/crates/text/src/layout.rsindex f43209f83..f0256986c 100644--- a/crates/text/src/layout.rs+++ b/crates/text/src/layout.rs@@ -658,9 +658,9 @@ impl Layouter {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 };// Horizontal scale is applied after shaping, so paragraph text breaks at the box- // width divided by it (#5565). Mixed scales break at the largest, so no line- // overflows the box.- let break_scale = if is_box {+ // (or area span) width divided by it (#5565). Mixed scales break at the largest, so+ // no line overflows the box.+ let break_scale = if wraps {let si0 = style_at(prange.start);(0..out.styles.len()).filter(|&ri| {@@ -698,7 +698,7 @@ impl Layouter {// out taller than the row it was planned in and no second plan is needed.layout.break_all_lines(None);let m = area_metrics(layout, &out.styles, &ps, k, first_style, vertical);- break_area(layout, a, area_from(m), m, (indent_start, indent_end), first_px, &area_budget)+ break_area(layout, a, area_from(m), m, (indent_start, indent_end), first_px, break_scale, &area_budget)};// 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.@@ -1198,6 +1198,10 @@ struct Need {/// later span holds overflows its span, as in a text box. Parley doesn't report a line's/// trailing whitespace while breaking, so overflowing lines are found after a pass and the/// paragraph broken again; `budget` (shared by the paragraph's calls) bounds the passes.+///+/// Horizontal scale applies after shaping, so each line breaks at its span width divided by+/// `scale`; widths compared with spans are the scaled ones.+#[allow(clippy::too_many_arguments)]fn break_area(layout: &mut Layout<RunBrush>,area: &crate::area::AreaShape,@@ -1205,23 +1209,24 @@ fn break_area(m: LineMetrics,indents: (f32, f32),min_width: f32,+ scale: f32,budget: &std::cell::Cell<usize>,) -> (Vec<Slot>, bool) {let inset = indents.0 + indents.1;let mut rows = AreaRows { area, m, min_width, next: from, rows: Vec::new(), done: false };let mut needs: Vec<Need> = Vec::new();loop {- let (slots, overflow) = break_area_pass(layout, &mut rows, inset, &needs);+ let (slots, overflow) = break_area_pass(layout, &mut rows, inset, scale, &needs);budget.set(budget.get().saturating_sub(1));if budget.get() == 0 {return (slots, overflow);}- // Each line's text start and width without its trailing whitespace.+ // Each line's text start and scaled width without its trailing whitespace.let lines: Vec<(usize, f32)> = layout.lines().map(|l| {let lm = l.metrics();- (l.text_range().start, lm.advance - lm.trailing_whitespace)+ (l.text_range().start, (lm.advance - lm.trailing_whitespace) * scale)}).collect();// A line that now starts elsewhere (an earlier line changed) no longer needs its span.@@ -1246,7 +1251,7 @@ fn break_area(/// One pass of [`break_area`]: each line in the next span, skipping spans too narrow for a/// line in `needs` when a later span holds it.-fn break_area_pass(layout: &mut Layout<RunBrush>, rows: &mut AreaRows<'_>, inset: f32, needs: &[Need]) -> (Vec<Slot>, bool) {+fn break_area_pass(layout: &mut Layout<RunBrush>, rows: &mut AreaRows<'_>, inset: f32, scale: f32, needs: &[Need]) -> (Vec<Slot>, bool) {#[cfg(test)]AREA_PASSES.with(|c| c.set(c.get() + 1));let mut slots: Vec<Slot> = Vec::new();@@ -1275,12 +1280,12 @@ fn break_area_pass(layout: &mut Layout<RunBrush>, rows: &mut AreaRows<'_>, inset(row, span) = (r, j);continue;}- br.state_mut().set_line_max_advance(width);+ br.state_mut().set_line_max_advance(width / scale);if br.break_next().is_none() {break;}// Parley aligns each line within the width it was broken at.- br.set_prior_line_width(width);+ br.set_prior_line_width(width / scale);slots.push(Slot { baseline, x: x0, width, row, span });span += 1;}diff --git a/crates/text/src/tests.rs b/crates/text/src/tests.rsindex ef0703720..5672f3728 100644--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@ -2125,6 +2125,27 @@ fn area_lines_center_in_their_span() {}}+/// Horizontal scale applies after shaping, so area lines break at the span width divided by it:+/// widened text stays inside the shape and centred lines stay centred (#5565).+#[test]+fn horizontally_scaled_area_text_stays_inside_its_spans() {+ let mut e = TextEngine::new();+ let style = CharStyle { font_family: "Inter".into(), size_pt: 12.0, horizontal_scale: 1.5, ..Default::default() };+ let mut t = styled(BUBBLE_TEXT, style);+ let path = area_ellipse(0.0, 0.0, 240.0, 200.0);+ t.shape = TextShape::Area { path: path.clone() };+ let l = e.layout(&t, 72.0);+ assert!(l.lines.len() >= 3, "{:?}", l.lines);+ assert_lines_inside(&l, &path);+ t.paragraphs = vec![ParagraphRun { len: t.text.len(), style: ParagraphStyle { align: TextAlign::Center, ..Default::default() } }];+ 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() >= 2, "{:?}", l.lines);+ 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};