From 0f6524ffbd4e50b6b2698210de02cd122e066542 Mon Sep 17 00:00:00 2001 From: Hector Villarreal Date: Sun, 1 Nov 2026 01:06:16 -0600 Subject: [PATCH] Copy CSS: write no border-radius for an unequally scaled rounded rectangle (CodeRabbit review on layer_css_cmds.rs:144) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Scaled 200 % across and 100 % down, a live rounded rectangle keeps one radius per corner (r times the smaller scale), while its path, which is what renders, has r·sx by r·sy elliptical corners. The rule wrote the single circular radius. It now writes border-radius only when the path's anchors are the ones the live radii draw (in any order, so a flipped shape still writes its corners); otherwise none, as the module doc says. Co-Authored-By: Claude Opus 5.5 --- crates/engine/src/cmd/layer_css_cmds.rs | 21 +++++++++++++++---- crates/engine/src/cmd/layer_css_cmds/tests.rs | 17 +++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) 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]