diff --git a/crates/engine/src/cmd/layer_css_cmds.rs b/crates/engine/src/cmd/layer_css_cmds.rs index 309825e094..fbb5db9770 100644 --- a/crates/engine/src/cmd/layer_css_cmds.rs +++ b/crates/engine/src/cmd/layer_css_cmds.rs @@ -14,11 +14,12 @@ //! //! Not written: strokes (`border`), layer effects (`box-shadow`, `text-shadow`), gradients as //! CSS gradients, blend modes (`mix-blend-mode`), fill opacity, the styles of later runs and -//! paragraphs, and the geometry of other shapes (live polygons, stars and lines, and custom -//! paths), which get the rule of their box. Groups and adjustment layers have no rule. +//! paragraphs, the corners of a rounded rectangle scaled unequally (elliptical, while it keeps +//! one radius per corner), and the geometry of other shapes (live polygons, stars and lines, and +//! custom paths), which get the rule of their box. Groups and adjustment layers have no rule. use photocraft_doc::text::{TextAlign, TextShape}; -use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId, LiveShape}; +use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId, LiveShape, Path}; use photocraft_geom::Point; use serde_json::{Value, json}; @@ -88,6 +89,17 @@ fn rgb(c: &photocraft_color::Color) -> String { format!("rgb({r}, {g}, {b})") } +/// Whether `path` is the outline `live` draws: every anchor of one is an anchor of the other, +/// within a hundredth of a pixel, in any order or direction (a flipped shape runs the other way). +/// A live rectangle scaled unequally keeps one radius per corner, but its path keeps the corners +/// it was drawn with, now elliptical, so the two differ. +fn draws(live: &LiveShape, path: &Path) -> bool { + let anchors = |p: &Path| p.subpaths.iter().flat_map(|s| s.knots.iter().map(|k| k.anchor)).collect::>(); + let (have, want) = (anchors(path), anchors(&photocraft_vector::shapes::live_path(live))); + let among = |p: &Point, q: &[Point]| q.iter().any(|q| (p.x - q.x).abs() < 0.01 && (p.y - q.y).abs() < 0.01); + have.iter().all(|p| among(p, &want)) && want.iter().all(|p| among(p, &have)) +} + /// The rule for one layer, `z` its place in the stack from the bottom (1-based); `None` for a /// layer CSS can't describe (groups, adjustments) or one with nothing to place. pub fn rule(doc: &Document, z: usize, l: &Layer) -> Option { @@ -136,9 +148,10 @@ pub fn rule(doc: &Document, z: usize, l: &Layer) -> Option { LayerContent::Shape(sh) => { // A live rounded rectangle's corners, before the fill as Photoshop writes them: one // value when all four are equal, else top-left, top-right, bottom-right, bottom-left. + // Scaled unequally, its corners are elliptical though it keeps one radius each: none. // A live ellipse fills its box, which half the box's size on each axis rounds to. match &sh.live { - Some(LiveShape::Rect { radii, .. }) if radii.iter().any(|&r| r > 0.0) => { + Some(live @ LiveShape::Rect { radii, .. }) if radii.iter().any(|&r| r > 0.0) && draws(live, &sh.path) => { let px = |r: f64| format!("{}px", number(r.max(0.0))); let all = if radii.iter().all(|&r| r == radii[0]) { px(radii[0]) } else { radii.map(px).join(" ") }; props.push(("border-radius", all)); diff --git a/crates/engine/src/cmd/layer_css_cmds/tests.rs b/crates/engine/src/cmd/layer_css_cmds/tests.rs index 818f6057d5..375e4bae4c 100644 --- a/crates/engine/src/cmd/layer_css_cmds/tests.rs +++ b/crates/engine/src/cmd/layer_css_cmds/tests.rs @@ -176,6 +176,23 @@ fn a_rounded_rectangle_writes_its_border_radius() { assert!(css.contains(" border-radius: 24px;\n background-color: rgb(255, 0, 0);\n"), "{css}"); } +/// Scaled 200 % across and 100 % down, a rounded rectangle's corners are 24 × 12 px ellipses, but +/// it keeps one radius per corner (12): no `border-radius` rather than a circular one. A flipped +/// one, whose outline runs the other way, keeps its circular corners and writes them. +#[test] +fn an_unequally_scaled_rounded_rectangle_writes_no_border_radius() { + let mut s = session("Box"); + let create = json!({"kind": "roundedRect", "rect": [40, 30, 120, 80], "radius": 12, "fill": "#ff0000"}); + let id = s.execute("shape.create", create.clone()).unwrap()["layer"].as_u64().unwrap(); + s.execute("shape.edit", json!({"layer": id, "transform": [2, 0, 0, 1, 0, 0]})).unwrap(); + let css = s.execute(ID, json!({"layer": id})).unwrap()["css"].as_str().unwrap().to_string(); + assert!(!css.contains("border-radius") && css.contains(" {\n background-color: rgb(255, 0, 0);\n"), "{css}"); + let flipped = s.execute("shape.create", create).unwrap()["layer"].as_u64().unwrap(); + s.execute("shape.edit", json!({"layer": flipped, "transform": [-1, 0, 0, 1, 400, 0]})).unwrap(); + let css = s.execute(ID, json!({"layer": flipped})).unwrap()["css"].as_str().unwrap().to_string(); + assert!(css.contains(" {\n border-radius: 12px;\n background-color: rgb(255, 0, 0);\n"), "{css}"); +} + /// A live ellipse writes `border-radius: 50%;` where a rounded rectangle writes its radii, so the /// rule draws an ellipse, not a rectangle; other live shapes (a star here) write no geometry. #[test]