PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge upstream/main into area-type
PanPDX6ed536ce3431
diff --cc crates/text/src/layout.rsindex f62c57fe6,ae1bd552a..f43209f83--- a/crates/text/src/layout.rs+++ b/crates/text/src/layout.rs@@@ -649,30 -648,29 +657,48 @@@ 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 };+ // 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 {+ let si0 = style_at(prange.start);+ (0..out.styles.len())+ .filter(|&ri| {+ let rs = run_starts.get(ri).copied().unwrap_or(0);+ let end = rs.saturating_add(runs.get(ri).map_or(0, |r| r.len));+ ri == si0 || (rs.max(prange.start) < end.min(content_end))+ })+ .filter_map(|ri| out.styles.get(ri))+ .map(effective_horizontal_scale)+ .fold(0.0f32, f32::max)+ } else {+ 1.0+ };+ let break_scale = if break_scale > 0.0 { break_scale } else { 1.0 };- let break_lines = |layout: &mut Layout<RunBrush>| {+ // Area type: the first row goes no higher than one leading (plus paragraph spacing)+ // below the previous paragraph's last line.+ let space_before = if prev_baseline.is_some() { ps.space_before_pt * k } else { 0.0 };+ let area_from = |m: LineMetrics| 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,+ };+ // Line-breaking passes left for this paragraph's area type (see `break_area`).+ let area_budget = std::cell::Cell::new(MAX_AREA_PASSES);+ let break_lines = |layout: &mut Layout<RunBrush>| -> (Vec<Slot>, bool) {if first_indent != 0.0 {- layout.set_text_indent(first_indent, IndentOptions::default());+ layout.set_text_indent(first_indent / break_scale, IndentOptions::default());}- layout.break_all_lines(avail.map(|a| a / break_scale));+ let Some(a) = &area else {- layout.break_all_lines(avail);++ layout.break_all_lines(avail.map(|a| a / break_scale));+ return (Vec::new(), false);+ };+ // Plan every row with the paragraph's real line metrics (its tallest font and+ // largest leading, measured on the unwrapped paragraph), so no wrapped line comes+ // 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)};// 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.@@@ -866,13 -888,12 +898,16 @@@} else {0.0};- let dx = if is_box {- let base = line_origin + indent_start - kern_align;- let slack = avail.unwrap_or(0.0) - adv * break_scale - 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;++ let slack = line_avail.unwrap_or(0.0) - adv * break_scale - line_kern;+ // Parley aligned the unscaled line in `avail / break_scale`: scale its+ // indent and alignment offset back to the box.+ let scaled_offset = m.offset * (break_scale - 1.0);base + if last_line {match ps.align {TextAlign::JustifyCenter => slack * 0.5 - m.offset,diff --cc crates/text/src/psd.rsindex c3eb714c2,8c502c75a..72c46096e--- a/crates/text/src/psd.rs+++ b/crates/text/src/psd.rs@@@ -1373,27 -1365,19 +1376,40 @@@ mod nonfinite_testsassert_eq!(char_style(None, &d, &[], 1.0).size_pt, 24.0);}+ #[test]+ fn area_path_that_is_not_finite_saves_as_point_text() {+ let path = photocraft_doc::vector::Path::new(vec![photocraft_doc::vector::Subpath::polygon(&[(0.0, 0.0), (f64::NAN, 0.0), (10.0, f64::INFINITY)])]);+ let layer = TextLayer {+ text: "x".into(),+ runs: vec![TextRun { len: 1, style: CharStyle { size_pt: 12.0, ..Default::default() } }],+ shape: TextShape::Area { path },+ ..Default::default()+ };+ let out = String::from_utf8_lossy(&crate::engine_data::write(&build_engine_data(&layer, None, 72.0))).into_owned();+ assert!(out.contains("/PointBase") && !out.contains("/BoxBounds"), "{out}");+ assert!(!out.to_ascii_lowercase().contains("nan") && !out.to_ascii_lowercase().contains("inf"), "{out}");+ let t = parse_tysh(&build_tysh(&layer, 72.0, None)).unwrap();+ for key in ["bounds", "boundingBox"] {+ let Some(D::Descriptor(d)) = t.text.get(key) else { panic!("no {key}") };+ for side in ["Left", "Top ", "Rght", "Btom"] {+ assert!(matches!(d.get(side), Some(D::UnitFloat { value, .. }) if value.is_finite()), "{key} {side}: {:?}", d.get(side));+ }+ }+ }++ #[test]+ fn warp_values_beyond_f32_clamp_to_the_warp_range() {+ let warp = Descriptor::new("warp")+ .with("warpStyle", enumv("warpStyle", "warpArc"))+ .with("warpValue", D::Double(1e39))+ .with("warpPerspective", D::Double(-1e39))+ .with("warpPerspectiveOther", D::Double(250.0))+ .with("warpRotate", enumv("Ornt", "Hrzn"));+ let tysh = write_tysh(&TySh { transform: Affine::IDENTITY, text: Descriptor::new("TxLr"), warp: Some(warp), bounds: [0.0; 4] });+ let w = text_layer_from_tysh(&tysh, 72.0).and_then(|l| l.warp).unwrap();+ assert_eq!((w.value, w.horizontal_distortion, w.vertical_distortion), (100.0, -100.0, 100.0));+ }+#[test]fn infinite_dpi_does_not_write_a_zero_size() {let layer =diff --cc crates/text/src/tests.rsindex 8c0c88e58,894ecf4e4..ef0703720--- a/crates/text/src/tests.rs+++ b/crates/text/src/tests.rs@@@ -2009,234 -2047,36 +2047,268 @@@ fn hyphenless_postscript_names_split_tr}}+/// 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());+}++#[test]+fn area_word_wider_than_every_span_overflows_instead_of_hiding_the_text() {+ use photocraft_doc::vector::Subpath;+ let mut e = TextEngine::new();+ // Two 40 px columns 120 px apart: their bounding box is 200 px wide, but no span is wider+ // than 40 px, so no row can ever hold the long word.+ let mut t = point("Wonderfulness is a long word", 12.0);+ let path = photocraft_doc::vector::Path::new(vec![+ Subpath::polygon(&[(0.0, 0.0), (40.0, 0.0), (40.0, 200.0), (0.0, 200.0)]),+ Subpath::polygon(&[(160.0, 0.0), (200.0, 0.0), (200.0, 200.0), (160.0, 200.0)]),+ ]);+ t.shape = TextShape::Area { path };+ let l = e.layout(&t, 72.0);+ let first = l.lines.first().expect("the text is shown");+ // The word overflows its 40 px span, as in a text box, on the first row.+ assert!(first.x1 - first.x0 > 40.0 && first.x1 - first.x0 < 200.0, "{first:?}");+ assert!(first.x0 >= -0.5, "{first:?}");+ // Nothing is hidden.+ assert_eq!(l.lines.last().map(|line| line.range.end), Some(t.text.len()), "{:?}", l.lines);+}++#[test]+fn single_size_area_paragraph_breaks_in_one_pass() {+ let mut e = TextEngine::new();+ let mut t = point(BUBBLE_TEXT, 12.0);+ t.shape = TextShape::Area { path: area_poly(&[(0.0, 0.0), (300.0, 0.0), (300.0, 300.0), (0.0, 300.0)]) };+ crate::layout::AREA_PASSES.with(|c| c.set(0));+ let l = e.layout(&t, 72.0);+ assert!(l.lines.len() >= 2);+ // Rows are planned with the paragraph's real metrics: no re-plan and no skipped spans.+ assert_eq!(crate::layout::AREA_PASSES.with(|c| c.get()), 1);+}++#[test]+fn area_word_skips_a_narrow_span_for_a_wider_one_on_the_same_row() {+ let mut e = TextEngine::new();+ let mut t = point("Wonderful things happen in speech bubbles every single day.", 12.0);+ // A U: a 24 px left arm and a wide right arm above y = 150, joined below it.+ let path = area_poly(&[(0.0, 0.0), (24.0, 0.0), (24.0, 150.0), (80.0, 150.0), (80.0, 0.0), (300.0, 0.0), (300.0, 200.0), (0.0, 200.0)]);+ t.shape = TextShape::Area { path: path.clone() };+ let l = e.layout(&t, 72.0);+ assert_lines_inside(&l, &path);+ let first = l.lines.first().expect("the text is shown");+ // "Wonderful" is wider than the left arm: it starts the text in the right arm, on the first+ // row, and nothing is put in the left arm's span on that row.+ assert_eq!(first.range.start, 0);+ assert!(first.x0 >= 79.5, "{first:?}");+ assert!(first.baseline < 30.0, "first row: {first:?}");+ assert_eq!(l.lines.iter().filter(|line| (line.baseline - first.baseline).abs() < 1e-3).count(), 1, "{:?}", l.lines);+ assert_eq!(l.lines.last().map(|line| line.range.end), Some(t.text.len()), "nothing hidden");+}++#[test]+fn area_stacks_paragraphs_with_leading_and_space_before() {+ let mut e = TextEngine::new();+ let text = "First paragraph of the bubble text.\nSecond paragraph goes right after it.";+ let mut t = point(text, 12.0);+ let style = ParagraphStyle { space_before_pt: 6.0, ..Default::default() };+ let leading = style.auto_leading * 12.0;+ t.paragraphs = vec![ParagraphRun { len: text.len(), style }];+ let path = area_poly(&[(0.0, 0.0), (120.0, 0.0), (120.0, 300.0), (0.0, 300.0)]);+ t.shape = TextShape::Area { path: path.clone() };+ let l = e.layout(&t, 72.0);+ assert_lines_inside(&l, &path);+ let (p0, p1): (Vec<_>, Vec<_>) = l.lines.iter().partition(|line| line.paragraph == 0);+ assert!(p0.len() >= 2 && p1.len() >= 2, "both paragraphs wrap: {:?}", l.lines);+ assert!(p1.iter().all(|line| line.paragraph == 1));+ // The second paragraph starts right after the break, one leading plus space before lower.+ let second = text.find('\n').unwrap() + 1;+ assert_eq!(p1[0].range.start, second);+ let gap = p1[0].baseline - p0.last().unwrap().baseline;+ assert!((gap - (leading + 6.0)).abs() < 1.0, "gap {gap}, want {}", leading + 6.0);+ // Lines within a paragraph are one leading apart.+ for w in p0.windows(2) {+ assert!((w[1].baseline - w[0].baseline - leading).abs() < 1.0, "{w:?}");+ }+ assert_eq!(l.lines.last().map(|line| line.range.end), Some(text.len()));+}++#[test]+fn area_justify_fills_each_span_but_the_last_line() {+ let mut e = TextEngine::new();+ let mut t = point(&BUBBLE_TEXT.repeat(2), 12.0);+ t.paragraphs = vec![ParagraphRun { len: t.text.len(), style: ParagraphStyle { align: TextAlign::JustifyLeft, ..Default::default() } }];+ 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);+ let a = crate::area::AreaShape::new(&path, false).unwrap();+ let span_of = |line: &crate::LineInfo| {+ a.spans(line.baseline - line.ascent, line.baseline + line.descent, 0.0)+ .into_iter()+ .find(|&(s0, s1)| line.x0 >= s0 - 0.5 && line.x1 <= s1 + 0.5)+ .unwrap()+ };+ let (last, rest) = l.lines.split_last().unwrap();+ for line in rest {+ // Justified: the glyphs run from one edge of the line's span to the other.+ let (s0, s1) = span_of(line);+ let ink = l.glyphs.iter().filter(|g| (g.y - line.baseline).abs() < 1e-3).map(|g| g.x).fold(f32::MIN, f32::max);+ assert!((line.x0 - s0).abs() < 1.0, "{line:?} in {s0}..{s1}");+ assert!(ink > s1 - 12.0, "last glyph at {ink}, span ends {s1}: {line:?}");+ }+ // The last line is set flush left, not stretched.+ let (s0, s1) = span_of(last);+ assert!((last.x0 - s0).abs() < 1.0 && last.x1 < s1 - 12.0, "{last:?} in {s0}..{s1}");+}+++ /// A word wider than its paragraph box wraps at the box edge instead of running past it (#6694);+ /// a normal break opportunity still wins, and a box that fits the word lays out exactly as before.+ #[test]+ fn a_word_wider_than_the_paragraph_box_breaks_at_the_box_edge() {+ let mut e = TextEngine::new();+ let word = "BLOQUEADO?!";+ let alone = e.layout(&point(word, 12.0), 72.0);+ assert_eq!(alone.lines.len(), 1, "{:?}", alone.lines);+ let word_width = width(&alone);+ let narrow = 30.0;+ assert!(word_width > narrow * 2.0, "{word_width}");++ let mut t = point(word, 12.0);+ t.shape = TextShape::Box { x: 0.0, y: 0.0, width: narrow, height: 200.0 };+ let l = e.layout(&t, 72.0);+ assert!(l.lines.len() >= 2, "{:?}", l.lines);+ for line in &l.lines {+ assert!(line.x1 <= narrow + 0.5, "{line:?}");+ }+ let joined: String = l.lines.iter().map(|l| &word[l.range.clone()]).collect();+ assert_eq!(joined, word);++ t.shape = TextShape::Box { x: 0.0, y: 0.0, width: word_width + 20.0, height: 200.0 };+ let fits = e.layout(&t, 72.0);+ assert_eq!(fits.lines.len(), 1, "{:?}", fits.lines);+ assert!((width(&fits) - word_width).abs() < 0.01, "{} vs {word_width}", width(&fits));++ let mut spaced = point("ab cd", 12.0);+ spaced.shape = TextShape::Box { x: 0.0, y: 0.0, width: narrow, height: 200.0 };+ let l = e.layout(&spaced, 72.0);+ assert_eq!(l.lines.len(), 2, "{:?}", l.lines);+ assert_eq!(l.lines[1].range.start, 3, "{:?}", l.lines);+ }diff --cc docs/scorecard.mdindex 6f8b38734,3b36e7c61..45d6ce478--- a/docs/scorecard.md+++ b/docs/scorecard.md@@@ -6,13 -6,13 +6,13 @@@ Generated by `cargo xtask scorecard` fr| Area | Done | Partial | Missing | Key numbers ||---|---:|---:|---:|---|- | [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 17 not run, 5 not measurable yet (baseline 2026-10-05) |+ | [Performance](#performance) | 3 | 22 | 24 | 58 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 19 not run, 5 not measurable yet (baseline 2026-10-05) || [File compatibility](#file-compatibility) | 7 | 14 | 6 | io corpus: oracle ≥ 148/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 249/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 188/256, round trip ≥ 258/256 || [Tools](#tools) | 33 | 11 | 0 | |- | [Workspace and UI](#workspace-and-ui) | 13 | 6 | 4 | settings that do nothing: 26 of 166 (target 0) |+ | [Workspace and UI](#workspace-and-ui) | 13 | 6 | 4 | settings that do nothing: 26 of 168 (target 0) |-| [Type](#type) | 4 | 3 | 2 | |+| [Type](#type) | 4 | 4 | 2 | || [Automation](#automation) | 5 | 2 | 4 | 631 of 631 menu items live (100.0%) |- | [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 29 of 29 crates; panic_hunt present |+ | [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 30 of 30 crates; panic_hunt present || [Distribution](#distribution) | 5 | 4 | 2 | || [Open bugs](#open-bugs) | 29 | 2 | 1 | |