From 7b51b005eb759473b97a73d58192e490df89f2e6 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 00:15:40 -0800 Subject: [PATCH] CSS Properties: evenly spaced round dots are a dotted border without a note A stroke of evenly spaced round dots (round cap, a zero-length dash, one gap length, not fitted to corners) is written as `border: ... dotted`, but the rule also carried the "arrowheads, width profiles, brushes or dashes fitted to corners" note, and with rasterize on it got a picture as well. Such a stroke no longer raises that note. Projecting caps, dots fitted to corners, dots mixed with dashes and unevenly spaced dots keep it. A gradient border is written with `border-image`, which replaces the border's style, so its dots or dashes are lost. A dotted or dashed gradient border now carries the note "dashes or dots on a gradient border". --- crates/engine/src/tests/css.rs | 28 ++++++++++++++++++++++++++++ crates/svg/src/css.rs | 19 +++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/tests/css.rs b/crates/engine/src/tests/css.rs index 39fb6a5bc2..a6bb174d28 100644 --- a/crates/engine/src/tests/css.rs +++ b/crates/engine/src/tests/css.rs @@ -43,6 +43,34 @@ fn a_rounded_rectangle_gives_background_border_and_radius() { assert!(s.execute("css.selection", &json!({"units": "furlong"})).is_err(), "unknown units are refused"); } +#[test] +fn round_dots_are_a_dotted_border_without_a_note() { + let mut s = session(); + run(&mut s, "shape.rectangle", json!({"x": 10, "y": 20, "width": 120, "height": 60})); + run(&mut s, "paint.setStroke", json!({"color": "#000000"})); + run(&mut s, "stroke.set", json!({"cap": "round", "dash": [0, 4]})); + let r = run(&mut s, "css.selection", json!({})); + assert!(css(&r).contains("border: 1px dotted #000000;"), "{}", css(&r)); + assert!(r["rules"][0].get("unsupported").is_none(), "{r}"); + // Squares, dots fitted to corners, dots mixed with dashes and unevenly spaced dots are not what + // `dotted` draws. + let others = [json!({"cap": "projecting"}), json!({"alignDashes": true}), json!({"dash": [0, 4, 6, 4]}), json!({"dash": [0, 4, 0, 40]})]; + for p in others { + run(&mut s, "stroke.set", json!({"cap": "round", "dash": [0, 4], "alignDashes": false})); + run(&mut s, "stroke.set", p.clone()); + let r = run(&mut s, "css.selection", json!({})); + assert!(r["rules"][0].get("unsupported").is_some(), "{p}: {r}"); + } + // A gradient border image draws the border whole, without its dots or dashes. + run(&mut s, "paint.setStroke", json!({"gradient": {"kind": "linear"}})); + for dash in [json!([0, 4]), json!([4, 2])] { + run(&mut s, "stroke.set", json!({"cap": "round", "dash": dash, "alignDashes": false})); + let r = run(&mut s, "css.selection", json!({})); + assert!(css(&r).contains("border-image: linear-gradient("), "{}", css(&r)); + assert_eq!(r["rules"][0]["unsupported"], json!("dashes or dots on a gradient border"), "{dash}"); + } +} + #[test] fn type_gives_font_properties_and_gradients_css_gradients() { let mut s = session(); diff --git a/crates/svg/src/css.rs b/crates/svg/src/css.rs index 76069d802a..d37715f9b9 100644 --- a/crates/svg/src/css.rs +++ b/crates/svg/src/css.rs @@ -539,8 +539,11 @@ impl Rules<'_> { /// A stroke as a border: weight, style (dashed; dotted for round dots) and colour. fn border(&mut self, s: &StrokeLayer, bounds: Rect, art: &mut Art) { - // Aligned strokes still draw as borders; arrowheads, profiles, brushes and fitted dashes don't. - if !vectorcraft_effects::stroke::is_plain(&StrokeLayer { align: StrokeAlign::Center, ..s.clone() }) { + // Aligned strokes still draw as borders, and evenly spaced round dots as `dotted`; + // arrowheads, profiles, brushes and fitted dashes don't. + let dots = s.dash.as_ref().is_some_and(|d| d.is_dashed() && s.cap == LineCap::Round && !d.align_corners && even_dots(&d.pattern)); + let dash = if dots { None } else { s.dash.clone() }; + if !vectorcraft_effects::stroke::is_plain(&StrokeLayer { align: StrokeAlign::Center, dash, ..s.clone() }) { art.unsupported("arrowheads, width profiles, brushes or dashes fitted to corners"); } let style = match &s.dash { @@ -553,6 +556,11 @@ impl Rules<'_> { Paint::Solid { color, .. } => art.props.push(("border", format!("{width} {style} {}", color_css(color, s.opacity)))), Paint::Gradient(g) => match self.gradient(g, s.paint_bounds(bounds), s.opacity) { Some(css) => { + // A border image takes the place of the border's style + // (https://www.w3.org/TR/css-backgrounds-3/#border-images). + if style != "solid" { + art.unsupported("dashes or dots on a gradient border"); + } art.props.push(("border", format!("{width} {style}"))); art.props.push(("border-image", format!("{css} 1"))); } @@ -803,6 +811,13 @@ fn corners(path: &PathData, live: Option<&LiveShape>) -> Option { } } +/// Whether `pattern` (dash, gap, dash, gap…) draws evenly spaced dots: every dash has zero length +/// and every gap is the same. An odd-length pattern repeats with dashes and gaps swapped, so its +/// dashes can't all be zero unless the whole pattern is. +fn even_dots(pattern: &[f64]) -> bool { + pattern.len().is_multiple_of(2) && pattern.chunks(2).all(|c| c.first() == Some(&0.0) && c.get(1) == pattern.get(1)) +} + /// One closed subpath of four straight sides along the axes. fn is_upright_rectangle(path: &PathData) -> bool { let ([sp], Some(b)) = (path.subpaths.as_slice(), path.bounds()) else { return false };