mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Copy CSS: write a live rounded rectangle's border-radius (FredApps review)
A shape layer that is still a live rectangle with rounded corners now gets `border-radius` before its fill, as Photoshop writes it: one value when all four corners are equal, else top-left, top-right, bottom-right, bottom-left, in px. Sharp rectangles write none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
fc49818c58
commit
38337c8c6d
2 files changed
+35
-5
No files matched your search
@@ -8,7 +8,7 @@
|
||||
//! stack as `z-index`, counted from the bottom layer as 1.
|
||||
|
||||
use photocraft_doc::text::{TextAlign, TextShape};
|
||||
use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId};
|
||||
use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId, LiveShape};
|
||||
use photocraft_geom::Point;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -123,10 +123,21 @@ pub fn rule(doc: &Document, z: usize, l: &Layer) -> Option<String> {
|
||||
_ => sized = false,
|
||||
}
|
||||
}
|
||||
LayerContent::Shape(sh) => match &sh.fill {
|
||||
Some(Fill::Solid(c)) => props.push(("background-color", rgb(c))),
|
||||
_ => props.push(("background-image", image(l))),
|
||||
},
|
||||
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.
|
||||
if let Some(LiveShape::Rect { radii, .. }) = &sh.live
|
||||
&& radii.iter().any(|&r| r > 0.0)
|
||||
{
|
||||
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));
|
||||
}
|
||||
match &sh.fill {
|
||||
Some(Fill::Solid(c)) => props.push(("background-color", rgb(c))),
|
||||
_ => props.push(("background-image", image(l))),
|
||||
}
|
||||
}
|
||||
LayerContent::Fill(f) => {
|
||||
// A fill layer without a shape covers the canvas.
|
||||
if bounds.is_empty() {
|
||||
|
||||
@@ -151,3 +151,22 @@ fn bad_params_and_layers_without_css_are_errors() {
|
||||
let mut empty = Session::new();
|
||||
assert!(empty.execute(ID, json!({})).is_err(), "no document");
|
||||
}
|
||||
|
||||
/// A live rounded rectangle gives its corners as Photoshop writes them, before the fill: one
|
||||
/// value when all four are equal, else top-left, top-right, bottom-right, bottom-left; a sharp
|
||||
/// one writes none.
|
||||
#[test]
|
||||
fn a_rounded_rectangle_writes_its_border_radius() {
|
||||
let mut s = session("Box");
|
||||
let id = s.execute("shape.create", json!({"kind": "roundedRect", "rect": [40, 30, 120, 80], "radius": 12, "fill": "#ff0000"})).unwrap()["layer"]
|
||||
.as_u64()
|
||||
.unwrap();
|
||||
let css = s.execute(ID, json!({"layer": id})).unwrap()["css"].as_str().unwrap().to_string();
|
||||
assert!(css.contains(" {\n border-radius: 12px;\n background-color: rgb(255, 0, 0);\n position: absolute;\n"), "{css}");
|
||||
s.execute("shape.edit", json!({"layer": id, "radii": [20, 0, 10.5, 5]})).unwrap();
|
||||
let css = s.execute(ID, json!({"layer": id})).unwrap()["css"].as_str().unwrap().to_string();
|
||||
assert!(css.contains(" border-radius: 20px 0px 10.5px 5px;\n background-color: rgb(255, 0, 0);\n"), "{css}");
|
||||
s.execute("shape.edit", json!({"layer": id, "radii": [0, 0, 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(" background-color: rgb(255, 0, 0);\n"), "{css}");
|
||||
}
|
||||
Reference in new issue
Block a user