diff --git a/crates/engine-core/src/cmd/type_convert_paragraph_box_tests.rs b/crates/engine-core/src/cmd/type_convert_paragraph_box_tests.rs new file mode 100644 index 0000000000..199b5c884c --- /dev/null +++ b/crates/engine-core/src/cmd/type_convert_paragraph_box_tests.rs @@ -0,0 +1,61 @@ +use photocraft_doc::{LayerContent, LayerId, TextLayer, text::TextShape}; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::Session; + +fn text_and_ink(s: &Session, id: LayerId) -> (&TextLayer, Rect) { + let layer = s.active().unwrap().doc.layer(id).unwrap(); + let LayerContent::Text(text) = &layer.content else { panic!("expected text") }; + (text, layer.surface().unwrap().content_bounds()) +} + +fn convert(text: &str, size: u32) -> (f32, f64) { + let mut s = crate::session(); + s.execute("file.new", json!({"width": 640, "height": 480, "resolution": 72})).unwrap(); + let result = s.execute("type.create", json!({"x": 100, "y": 200, "text": text, "font": "Arial", "size": size})).unwrap(); + let id = LayerId(result["layer"].as_u64().unwrap()); + let (point, before_ink) = text_and_ink(&s, id); + let before = super::layout_at(72.0, point); + let before_baseline = point.transform.m[5] + f64::from(before.lines[0].baseline); + let [x0, top, x1, bottom] = before.bounds().unwrap(); + let expected_width = (x1 - x0).max(1.0) + (size as f32 * 0.5).max(4.0); + + s.execute("type.convertToParagraphText", json!({})).unwrap(); + let (paragraph, after_ink) = text_and_ink(&s, id); + let TextShape::Box { width, height, .. } = paragraph.shape else { panic!("expected paragraph text") }; + let after = super::layout_at(72.0, paragraph); + assert_eq!(paragraph.text, text); + assert_eq!(after.lines.len(), before.lines.len()); + assert!(!after.overflows(text), "the new box must keep every line"); + assert!((width - expected_width).abs() < 1e-4); + if before.lines.len() == 1 { + let expected_height = (bottom - top).max(size as f32 * 1.2).max(after.lines[0].baseline); + assert!((height - expected_height).abs() < 1e-4, "expected {expected_height}, got {height}"); + } + let after_baseline = paragraph.transform.m[5] + f64::from(after.lines[0].baseline); + assert!((after_baseline - before_baseline).abs() < 1e-4); + assert_eq!(after_ink, before_ink); + assert_eq!(s.active().unwrap().history.undo_label(), Some("Convert to Paragraph Text")); + (height, paragraph.transform.m[5]) +} + +#[test] +fn converting_48_point_arial_uses_its_line_height() { + let (height, y) = convert("Hello World", 48); + let arial_available = photocraft_text::shared().lock().unwrap_or_else(|e| e.into_inner()).has_family("Arial"); + if arial_available { + assert!((height - 58.0).abs() <= 1.0, "height {height}"); + assert!((y - 165.63).abs() <= 1.0, "top {y}"); + } else { + assert!(height >= 57.0, "the leading fits even with the fallback font: {height}"); + } +} + +#[test] +fn smaller_text_gets_a_shorter_box_and_multiline_text_still_fits() { + let (single, _) = convert("Hello World", 48); + let (small, _) = convert("Hello World", 24); + let (multiline, _) = convert("Hello World\nThird?\nA final line", 48); + assert!(small < single && single < multiline, "{small}, {single}, {multiline}"); +} diff --git a/crates/engine-core/src/cmd/type_extra_cmds.rs b/crates/engine-core/src/cmd/type_extra_cmds.rs index 52a034321e..8163044bae 100644 --- a/crates/engine-core/src/cmd/type_extra_cmds.rs +++ b/crates/engine-core/src/cmd/type_extra_cmds.rs @@ -189,17 +189,37 @@ fn to_paragraph(s: &mut Session, p: &Value) -> Result { let [x0, y0, x1, y1] = l.bounds().unwrap_or([0.0, -t.size_pt, t.size_pt, 0.0]); // A little slack so no line re-wraps; the box keeps the text where it was. let width = (x1 - x0).max(1.0) + indents + (size_px * 0.5).max(4.0); - let height = (y1 - y0).max(1.0) + size_px * 0.5; + let text_height = (y1 - y0).max(1.0); + let probe_height = text_height + size_px * 0.5; let x = match ps.align { TextAlign::Center | TextAlign::JustifyCenter => -width / 2.0, TextAlign::Right | TextAlign::JustifyRight => -width, _ => 0.0, }; let mut probe = t.clone(); - probe.shape = TextShape::Box { x, y: y0, width, height }; + probe.shape = TextShape::Box { x, y: y0, width, height: probe_height }; // Box text puts the first baseline at the box top plus the ascender height: measure it and // shift the box so that baseline stays at the point-text anchor (y = 0). - let b0 = layout_at(dpi, &probe).lines.first().map_or(0.0, |ln| ln.baseline); + let boxed = layout_at(dpi, &probe); + let b0 = boxed.lines.first().map_or(0.0, |ln| ln.baseline); + let mut height = text_height; + if t.text.is_empty() || l.lines.is_empty() { + height = probe_height; + } else if l.lines.len() == 1 { + // A single line still gets its leading. Across multiple lines the bounds already + // account for the laid-out baseline spacing. + let leading = l + .glyphs + .iter() + .map(|glyph| { + let style = &l.styles[glyph.style as usize]; + style.leading_pt.map_or(ps.auto_leading * l.faces[glyph.face as usize].size_px, |points| points * l.px_per_pt) + }) + .fold(0.0, f32::max); + height = height.max(leading); + } + // The box uses ascender placement; keep every baseline that fit the original probe. + height = height.max(boxed.lines.last().map_or(0.0, |ln| ln.baseline - y0)); let y = y0 - b0; t.shape = TextShape::Box { x: 0.0, y: 0.0, width, height }; t.transform = then_translate(&t.transform, f64::from(x), f64::from(y)); @@ -1167,6 +1187,10 @@ pub fn specs() -> Vec { all } +#[cfg(test)] +#[path = "type_convert_paragraph_box_tests.rs"] +mod type_convert_paragraph_box_tests; + #[cfg(test)] #[path = "type_extra_cmds/tests.rs"] mod tests;