mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 20:43:18 -05:00
Merge pull request #10705 from dotCooCoo/fix/css-dotted-border-note
CSS Properties: evenly spaced round dots are a dotted border without a note
This commit is contained in:
2 files changed
+45
-2
No files matched your search
@@ -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();
|
||||
|
||||
+17
-2
@@ -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<Corners> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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 };
|
||||
|
||||
Reference in new issue
Block a user