Merge pull request #7816 from dotCooCoo/fix/bucket-strokeedge-and-livepaint-info-act-on

Live Paint: the Bucket replaces every fill or stroke row of a face or edge
This commit is contained in:
BFlat
2026-11-15 16:40:12 +07:00
committed by GitHub
5 changed files with 196 additions and 50 deletions
+2 -2
View File
@@ -117,7 +117,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Live Paint Bucket",
[],
None,
"{group?: id | ids?|id?: [id…] (paths to make live first, as livePaint.make does), point: [x,y], points?: [[x,y]…] (a drag polyline starting at point; 1 to 4096 finite canvas points; fills every crossed face of the same group), continuation?: [[[x,y]…]…] (further path chunks of 1 to 4096 finite canvas points each; each starts at the preceding chunk's last point; requires points), scope?: \"one\" (default: the face alone) | \"connected\" (a double-click: also each face reached across edges that are hidden or draw no stroke, as a flood from the face; a stroked edge, a hidden face or a Gap Detection gap stops it) | \"samePaint\" (a triple-click: also every face of the group whose fill is match; \"connected\" and \"samePaint\" are refused with points), match?: paint (as the document stores it, the `fill` of a face's appearance; scope samePaint only; default: the face's fill), color?|swatch?|gradient?|none? (default: current fill; a color in #rrggbbaa or #rgba is refused), tint?: 0..100, keepModel?: bool (both as paint.setFill)} fill the face containing point, skipping hidden faces; when every face it reaches already has the paint, nothing changes (no undo step, and the recent colors stay as they are) → {group, face, count (faces reached)}",
"{group?: id | ids?|id?: [id…] (paths to make live first, as livePaint.make does), point: [x,y], points?: [[x,y]…] (a drag polyline starting at point; 1 to 4096 finite canvas points; fills every crossed face of the same group), continuation?: [[[x,y]…]…] (further path chunks of 1 to 4096 finite canvas points each; each starts at the preceding chunk's last point; requires points), scope?: \"one\" (default: the face alone) | \"connected\" (a double-click: also each face reached across edges that are hidden or draw no stroke, as a flood from the face; a stroked edge, a hidden face or a Gap Detection gap stops it) | \"samePaint\" (a triple-click: also every face of the group whose fill is match; \"connected\" and \"samePaint\" are refused with points), match?: paint (as the document stores it, the `fill` of a face's appearance; scope samePaint only; default: the face's fill), color?|swatch?|gradient?|none? (default: current fill; a color in #rrggbbaa or #rgba is refused), tint?: 0..100, keepModel?: bool (both as paint.setFill)} fill the face containing point, skipping hidden faces: each face reached has its fills replaced by one fill with the paint, shown, fully opaque and blending normally, in the place of its topmost fill (none leaves one fill of none), and its strokes and its own opacity stay as they are; when every face it reaches already has only that one fill, nothing changes (no undo step, and the recent colors stay as they are) → {group, face, count (faces reached)}",
has_doc,
lp_fill
),
@@ -126,7 +126,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Live Paint Bucket (Stroke)",
[],
None,
"{group: id, point: [x,y], color?|swatch?|none? (default: current stroke), width?: pt (default: current weight), tolerance?: pt (4), scope?: \"one\" (default: the edge alone) | \"connected\" (a double-click: also each edge joined to it, end to end, through edges that draw with stroke paint match) | \"samePaint\" (a triple-click: also every edge of the group that draws with stroke paint match), match?: paint (as the document stores it; scope connected or samePaint only; default: the stroke paint the edge draws with; an edge draws with the paint of its topmost stroke that is shown, with paint, above 0 pt and not fully transparent, and with none when the edge is fully transparent or has no such stroke), tint?: 0..100, keepModel?: bool (both as paint.setFill)} paint the edge nearest point, skipping hidden edges: each edge reached takes the paint and width in its topmost stroke that is shown, or in a new stroke above the others when it has none; when every edge it reaches already has the paint and the width there, nothing changes (no undo step, and the recent colors stay as they are) → {group, edge, count (edges reached)}",
"{group: id, point: [x,y], color?|swatch?|none? (default: current stroke), width?: pt (default: current weight), tolerance?: pt (4), scope?: \"one\" (default: the edge alone) | \"connected\" (a double-click: also each edge joined to it, end to end, through edges that draw with stroke paint match) | \"samePaint\" (a triple-click: also every edge of the group that draws with stroke paint match), match?: paint (as the document stores it; scope connected or samePaint only; default: the stroke paint the edge draws with; an edge draws with the paint of its topmost stroke that is shown, with paint, above 0 pt and not fully transparent, and with none when the edge is fully transparent or has no such stroke), tint?: 0..100, keepModel?: bool (both as paint.setFill)} paint the edge nearest point, skipping hidden edges: each edge reached has its strokes replaced by one stroke with the paint and width, shown, fully opaque and blending normally, in the place of its topmost stroke and with the dashes, caps and arrowheads of its topmost shown stroke (a new stroke above the fills when it has none; none leaves one stroke of none), and its fills stay as they are; when every edge it reaches already has only that one stroke (at any width for none), nothing changes (no undo step, and the recent colors stay as they are) → {group, edge, count (edges reached)}",
has_doc,
lp_stroke_edge
),
+79 -39
View File
@@ -525,16 +525,17 @@ pub(super) fn lp_fill(s: &mut Session, p: &Value) -> Result<Value> {
Some(gid) => {
let (fid, targets) = targets_in(&s.doc()?.doc, gid)?;
let count = targets.len();
// Faces that already have the paint stay as they are: when every face has it, nothing
// changes and no undo step is recorded.
// Faces that already have only the fill the bucket would leave ([`bucket_keeps`]) stay as
// they are: when every face does, nothing changes and no undo step is recorded.
let d = &s.doc()?.doc;
let unpainted: Vec<NodeId> = targets.into_iter().filter(|t| d.node(*t).is_none_or(|f| f.appearance.fill_paint() != paint)).collect();
let unpainted: Vec<NodeId> =
targets.into_iter().filter(|t| d.node(*t).is_none_or(|f| !bucket_keeps(&f.appearance, true, &paint, 0.0))).collect();
if unpainted.is_empty() {
return Ok(json!({ "group": gid.0, "face": fid.0, "count": count }));
}
s.edit("Live Paint Bucket", |d, _| {
for target in unpainted {
d.node_mut(target).ok_or(EngineError::NoNode(target))?.appearance.set_fill(paint.clone());
bucket_paint(&mut d.node_mut(target).ok_or(EngineError::NoNode(target))?.appearance, true, &paint, 0.0);
}
Ok((gid, fid, count))
})?
@@ -544,7 +545,7 @@ pub(super) fn lp_fill(s: &mut Session, p: &Value) -> Result<Value> {
let (fid, targets) = targets_in(d, gid)?;
let count = targets.len();
for target in targets {
d.node_mut(target).ok_or(EngineError::NoNode(target))?.appearance.set_fill(paint.clone());
bucket_paint(&mut d.node_mut(target).ok_or(EngineError::NoNode(target))?.appearance, true, &paint, 0.0);
}
Ok((gid, fid, count))
})?,
@@ -580,18 +581,10 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result<Value> {
.collect(),
};
let count = targets.len();
// An edge whose shown stroke already has the paint and the width (or that has no shown stroke
// and gets none) stays as it is; when every edge does, nothing changes and no undo step is
// recorded.
let changed: Vec<NodeId> = targets
.into_iter()
.filter(|t| {
d.node(*t).is_none_or(|e| match shown_stroke(e) {
Some(st) => !(st.paint == paint && st.width == width),
None => !paint.is_none(),
})
})
.collect();
// An edge that already has only the stroke the bucket would leave ([`bucket_keeps`]) stays
// as it is; when every edge does, nothing changes and no undo step is recorded.
let changed: Vec<NodeId> =
targets.into_iter().filter(|t| d.node(*t).is_none_or(|e| !bucket_keeps(&e.appearance, false, &paint, width))).collect();
(eid, count, changed)
};
if changed.is_empty() {
@@ -599,14 +592,7 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result<Value> {
}
s.edit("Live Paint Bucket", |d, _| {
for target in &changed {
let e = d.node_mut(*target).ok_or(EngineError::NoNode(*target))?;
match shown_stroke_mut(e) {
Some(st) => {
st.paint = paint.clone();
st.width = width;
}
None => e.appearance.items.push(super::AppearanceItem::Stroke(super::StrokeLayer::new(paint.clone(), width))),
}
bucket_paint(&mut d.node_mut(*target).ok_or(EngineError::NoNode(*target))?.appearance, false, &paint, width);
}
Ok(())
})?;
@@ -614,22 +600,76 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result<Value> {
Ok(json!({ "group": g.0, "edge": eid.0, "count": count }))
}
/// The stroke `livePaint.strokeEdge` paints on Live Paint edge `e`: its topmost stroke that is
/// shown. None when it has no stroke or every stroke is hidden; the command then adds a stroke
/// above the others.
fn shown_stroke(e: &Node) -> Option<&super::StrokeLayer> {
e.appearance.items.iter().rev().find_map(|i| match i {
super::AppearanceItem::Stroke(s) if s.visible => Some(s),
_ => None,
})
/// Whether the Live Paint Bucket leaves appearance `a` as it is when it paints its fills (`fill`)
/// or strokes with `paint` (a stroke at `width`): it has exactly one row of that kind, shown, fully
/// opaque, blending normally and with the paint (a stroke also with the width, unless the paint is
/// none), or it has no row of that kind and the paint is none.
fn bucket_keeps(a: &Appearance, fill: bool, paint: &super::Paint, width: f64) -> bool {
let mut rows = a.items.iter().filter(|i| i.is_fill() == fill);
match (rows.next(), rows.next()) {
(None, _) => paint.is_none(),
(Some(row), None) => {
row.visible()
&& row.opacity() == 1.0
&& row.blend() == vectorcraft_color::BlendMode::Normal
&& row.paint() == paint
&& match row {
AppearanceItem::Fill(_) => true,
AppearanceItem::Stroke(st) => paint.is_none() || st.width == width,
}
}
(Some(_), Some(_)) => false,
}
}
/// [`shown_stroke`], to change.
fn shown_stroke_mut(e: &mut Node) -> Option<&mut super::StrokeLayer> {
e.appearance.items.iter_mut().rev().find_map(|i| match i {
super::AppearanceItem::Stroke(s) if s.visible => Some(s),
_ => None,
})
/// Paint the fills (`fill`) or strokes of appearance `a` with `paint` (a stroke at `width`), as the
/// Live Paint Bucket does: every row of that kind is replaced by one row, shown, fully opaque and
/// blending normally, in the place of the topmost row of that kind. The new row keeps the other
/// settings (a stroke's dashes, caps and arrowheads, a row's effects) of the topmost row of that
/// kind that is shown, or of the topmost one when every row is hidden. Rows of the other kind stay
/// as they are. With no row of that kind, a fill is added below the strokes and a stroke above the
/// fills.
fn bucket_paint(a: &mut Appearance, fill: bool, paint: &super::Paint, width: f64) {
let of_kind = |i: &AppearanceItem| i.is_fill() == fill;
let Some(top) = a.items.iter().rposition(of_kind) else {
if fill {
a.set_fill(paint.clone());
} else {
a.items.push(AppearanceItem::Stroke(super::StrokeLayer::new(paint.clone(), width)));
}
return;
};
let base = a.items.iter().rposition(|i| of_kind(i) && i.visible()).unwrap_or(top);
let Some(mut row) = a.items.get(base).cloned() else { return };
match &mut row {
AppearanceItem::Fill(f) => {
f.paint = paint.clone();
f.opacity = 1.0;
f.blend = vectorcraft_color::BlendMode::Normal;
f.visible = true;
}
AppearanceItem::Stroke(st) => {
st.paint = paint.clone();
st.width = width;
st.opacity = 1.0;
st.blend = vectorcraft_color::BlendMode::Normal;
st.visible = true;
}
}
let mut row = Some(row);
a.items = std::mem::take(&mut a.items)
.into_iter()
.enumerate()
.filter_map(|(i, item)| {
if i == top {
row.take()
} else if of_kind(&item) {
None
} else {
Some(item)
}
})
.collect();
}
/// Every fill and stroke row of face or edge `n`, bottom first, as `livePaint.info` lists them. A