mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Copy CSS: write border-radius: 50% for a live ellipse (CodeRabbit review on layer_css_cmds.rs:147)
A solid live ellipse got the plain rectangle rule, so pasting it showed a rectangle. It now writes `border-radius: 50%;` where a rounded rectangle writes its radii, before the fill. The module doc lists it, and lists the geometry of other shapes (live polygons, stars and lines, custom paths) as not written. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
d4622f78f2
commit
1bdba8264d
2 files changed
+26
-9
No files matched your search
@@ -5,7 +5,7 @@
|
||||
//! gets a `_` in front). What is written, in this order:
|
||||
//! - a type layer: `font-size`, `font-family`, `color`, `line-height` and `text-align`, from its
|
||||
//! first run and first paragraph;
|
||||
//! - a live rounded rectangle: `border-radius`;
|
||||
//! - a live rounded rectangle: `border-radius`; a live ellipse: `border-radius: 50%`;
|
||||
//! - a shape or fill layer with a solid colour: `background-color`; any other layer (pixels,
|
||||
//! gradient or pattern fills, smart objects): `background-image`, an image named after it;
|
||||
//! - `opacity` when it isn't 100 %;
|
||||
@@ -13,8 +13,9 @@
|
||||
//! - `z-index`, its place in the stack counted from the bottom layer as 1.
|
||||
//!
|
||||
//! Not written: strokes (`border`), layer effects (`box-shadow`, `text-shadow`), gradients as
|
||||
//! CSS gradients, blend modes (`mix-blend-mode`), fill opacity, and the styles of later runs and
|
||||
//! paragraphs. Groups and adjustment layers have no rule.
|
||||
//! 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.
|
||||
|
||||
use photocraft_doc::text::{TextAlign, TextShape};
|
||||
use photocraft_doc::{Document, Fill, Layer, LayerContent, LayerId, LiveShape};
|
||||
@@ -135,12 +136,15 @@ pub fn rule(doc: &Document, z: usize, l: &Layer) -> Option<String> {
|
||||
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));
|
||||
// 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) => {
|
||||
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));
|
||||
}
|
||||
Some(LiveShape::Ellipse { .. }) => props.push(("border-radius", "50%".into())),
|
||||
_ => {}
|
||||
}
|
||||
match &sh.fill {
|
||||
Some(Fill::Solid(c)) => props.push(("background-color", rgb(c))),
|
||||
|
||||
@@ -171,6 +171,19 @@ fn a_rounded_rectangle_writes_its_border_radius() {
|
||||
assert!(!css.contains("border-radius") && css.contains(" 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]
|
||||
fn an_ellipse_writes_a_half_border_radius() {
|
||||
let mut s = session("Box");
|
||||
let id = s.execute("shape.create", json!({"kind": "ellipse", "rect": [40, 30, 120, 80], "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: 50%;\n background-color: rgb(255, 0, 0);\n position: absolute;\n"), "{css}");
|
||||
let star = s.execute("shape.create", json!({"kind": "star", "rect": [40, 30, 120, 80], "fill": "#ff0000"})).unwrap()["layer"].as_u64().unwrap();
|
||||
let css = s.execute(ID, json!({"layer": star})).unwrap()["css"].as_str().unwrap().to_string();
|
||||
assert!(!css.contains("border-radius") && css.contains(" {\n background-color: rgb(255, 0, 0);\n"), "{css}");
|
||||
}
|
||||
|
||||
/// `css` without its double-quoted strings (escapes inside them skipped).
|
||||
fn outside_strings(css: &str) -> String {
|
||||
let (mut out, mut quoted, mut escaped) = (String::new(), false, false);
|
||||
|
||||
Reference in new issue
Block a user