mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Copy CSS: write no border-radius for an unequally scaled rounded rectangle (CodeRabbit review on layer_css_cmds.rs:144)
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 <[email protected]>
This commit is contained in:
1 parent
13e7b0fe0e
commit
0f6524ffbd
2 files changed
+34
-4
No files matched your search
@@ -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::<Vec<_>>();
|
||||
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<String> {
|
||||
@@ -136,9 +148,10 @@ 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.
|
||||
// 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));
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in new issue
Block a user