mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 08:13:22 +00:00
Merge pull request #7620 from dotCooCoo/fix/live-paint-info-every-row
MCP: livePaint.info lists every appearance row of a face or edge
This commit is contained in:
@@ -135,7 +135,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
"Live Paint Info",
|
||||
[],
|
||||
None,
|
||||
"{group, id?: object id (default: the selection)} → {faces: [{id, fill, bounds}], edges: [{id, stroke, width}], sources, gapOptions: {detect, size: \"small\"|\"medium\"|\"large\"|pt, points, color: #rrggbb} (as livePaint.gapOptions sets them), gaps: [[x0,y0,x1,y1]…] (as livePaint.gapOptions returns them)}",
|
||||
"{group?|id?: Live Paint group id (one required; id is read when group is left out)} → {faces: [{id, fill (top fill), bounds, appearance: [row…]}], edges: [{id, stroke (top stroke), width (top stroke's), appearance: [row…]}], where a row is {index (in the part's fill and stroke stack, bottom first), kind: \"fill\"|\"stroke\", paint, opacity: 0..1, visible, width?: pt (strokes only), drawn (the part is shown with opacity above 0, and the row is shown, has paint and opacity above 0, a stroke also width above 0)}, sources, gapOptions: {detect, size: \"small\"|\"medium\"|\"large\"|pt, points, color: #rrggbb} (as livePaint.gapOptions sets them), gaps: [[x0,y0,x1,y1]…] (as livePaint.gapOptions returns them)}",
|
||||
has_doc,
|
||||
lp_info
|
||||
),
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use super::{
|
||||
Appearance, Arc, Beside, Document, EDGE_NAME, EngineError, FACE_NAME, GapOptions, GapSize, LIVE_PAINT_NAME, Node, NodeId, PathData, Point, Rect,
|
||||
Result, SOURCES_NAME, Selection, Session, SubPath, Value, b, bad, build_children, edge_near, face_at, finite, ids_json, is_live_paint, json,
|
||||
number_or, object_param, paint_from, po, point_param, repaint_from, replace_roots, root_ids, source_nodes, source_shapes, switch_or,
|
||||
Appearance, AppearanceItem, Arc, Beside, Document, EDGE_NAME, EngineError, FACE_NAME, GapOptions, GapSize, LIVE_PAINT_NAME, Node, NodeId,
|
||||
PathData, Point, Rect, Result, SOURCES_NAME, Selection, Session, SubPath, Value, b, bad, build_children, edge_near, face_at, finite, ids_json,
|
||||
is_live_paint, json, number_or, object_param, paint_from, po, point_param, repaint_from, replace_roots, root_ids, source_nodes, source_shapes,
|
||||
switch_or,
|
||||
};
|
||||
|
||||
/// `options` as stored on a Live Paint group: none for the defaults.
|
||||
@@ -524,6 +525,37 @@ pub(super) fn lp_stroke_edge(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
Ok(json!({ "group": g.0, "edge": edge.0 }))
|
||||
}
|
||||
|
||||
/// Every fill and stroke row of face or edge `n`, bottom first, as `livePaint.info` lists them. A
|
||||
/// row is `drawn` when the part is shown with an opacity above 0 and the row is shown, has paint
|
||||
/// and has an opacity above 0 (a stroke also a weight above 0): the rule of [`draws`], row by row.
|
||||
fn appearance_rows(n: &Node) -> Vec<Value> {
|
||||
let part_shown = n.visible && n.opacity > 0.0;
|
||||
n.appearance
|
||||
.items
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, item)| {
|
||||
let shown = part_shown && item.visible() && !item.paint().is_none() && item.opacity() > 0.0;
|
||||
let mut row = json!({
|
||||
"index": i,
|
||||
"kind": item.kind_name(),
|
||||
"paint": item.paint().label(),
|
||||
"opacity": item.opacity(),
|
||||
"visible": item.visible(),
|
||||
});
|
||||
let drawn = match item {
|
||||
AppearanceItem::Fill(_) => shown,
|
||||
AppearanceItem::Stroke(st) => {
|
||||
row["width"] = json!(st.width);
|
||||
shown && st.width > 0.0
|
||||
}
|
||||
};
|
||||
row["drawn"] = json!(drawn);
|
||||
row
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub(super) fn lp_info(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let g = match object_param(p, "group", "livePaint.info")? {
|
||||
Some(g) => g,
|
||||
@@ -535,12 +567,24 @@ pub(super) fn lp_info(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
.iter()
|
||||
.map(|f| {
|
||||
let bb = f.geometric_bounds().unwrap_or(Rect::ZERO);
|
||||
json!({ "id": f.id.0, "fill": f.appearance.fill_paint().label(), "bounds": [bb.x0, bb.y0, bb.x1, bb.y1] })
|
||||
json!({
|
||||
"id": f.id.0,
|
||||
"fill": f.appearance.fill_paint().label(),
|
||||
"bounds": [bb.x0, bb.y0, bb.x1, bb.y1],
|
||||
"appearance": appearance_rows(f),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let edges: Vec<Value> = b::edges(n)
|
||||
.iter()
|
||||
.map(|e| json!({ "id": e.id.0, "stroke": e.appearance.stroke_paint().label(), "width": e.appearance.stroke_width() }))
|
||||
.map(|e| {
|
||||
json!({
|
||||
"id": e.id.0,
|
||||
"stroke": e.appearance.stroke_paint().label(),
|
||||
"width": e.appearance.stroke_width(),
|
||||
"appearance": appearance_rows(e),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let sources = b::sources(n).and_then(|g| g.children()).map(|c| c.len()).unwrap_or(0);
|
||||
let options = n.gap_options();
|
||||
|
||||
@@ -3018,6 +3018,76 @@ fn live_paint_expand_keeps_the_faces_and_edges_that_draw() {
|
||||
assert_eq!(rows(&n, lower_stroke), rows(&before, lower_stroke));
|
||||
}
|
||||
|
||||
/// `livePaint.info` lists every fill and stroke row of each face and edge, bottom first, and says
|
||||
/// which rows draw by Expand's rule, while `fill`, `stroke` and `width` stay the top rows.
|
||||
#[test]
|
||||
fn live_paint_info_lists_every_appearance_row() {
|
||||
use vectorcraft_doc::{FillLayer, StrokeLayer};
|
||||
let fill = |p: Paint| AppearanceItem::Fill(FillLayer::new(p));
|
||||
let stroke = |p: Paint, w: f64| AppearanceItem::Stroke(StrokeLayer::new(p, w));
|
||||
let red = || Paint::solid(Color::rgb(1.0, 0.0, 0.0));
|
||||
let blue = || Paint::solid(Color::rgb(0.0, 0.0, 1.0));
|
||||
let green = || Paint::solid(Color::rgb(0.0, 1.0, 0.0));
|
||||
let hidden = |mut i: AppearanceItem| {
|
||||
match &mut i {
|
||||
AppearanceItem::Fill(f) => f.visible = false,
|
||||
AppearanceItem::Stroke(st) => st.visible = false,
|
||||
}
|
||||
i
|
||||
};
|
||||
let faded = |mut i: AppearanceItem, o: f32| {
|
||||
match &mut i {
|
||||
AppearanceItem::Fill(f) => f.opacity = o,
|
||||
AppearanceItem::Stroke(st) => st.opacity = o,
|
||||
}
|
||||
i
|
||||
};
|
||||
let mut s = session();
|
||||
let g = live(&mut s);
|
||||
// A drawn lower fill and a half-opaque stroke under an unpainted top fill.
|
||||
let lower = set_live_part(&mut s, g, Some((25.0, 50.0)), None, vec![fill(red()), faded(stroke(blue(), 2.0), 0.5), fill(Paint::None)], 1.0);
|
||||
let quiet = set_live_part(&mut s, g, Some((125.0, 50.0)), None, vec![hidden(fill(red())), faded(fill(red()), 0.0)], 1.0);
|
||||
let bare = set_live_part(&mut s, g, Some((75.0, 50.0)), None, vec![], 1.0);
|
||||
// Two strokes with a fill between them.
|
||||
let two = set_live_part(&mut s, g, None, Some((0.0, 50.0)), vec![stroke(red(), 4.0), fill(green()), stroke(blue(), 2.0)], 1.0);
|
||||
let thin = set_live_part(&mut s, g, None, Some((100.0, 50.0)), vec![stroke(red(), 0.0)], 1.0);
|
||||
let clear_edge = set_live_part(&mut s, g, None, Some((75.0, 0.0)), vec![stroke(red(), 2.0)], 0.0);
|
||||
let hidden_edge = set_live_part(&mut s, g, None, Some((150.0, 50.0)), vec![stroke(red(), 2.0)], 1.0);
|
||||
s.edit("t", |d, _| {
|
||||
d.node_mut(hidden_edge).unwrap().visible = false;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let parts = [lower, quiet, bare, two, thin, clear_edge, hidden_edge];
|
||||
assert_eq!(parts.iter().collect::<std::collections::HashSet<_>>().len(), parts.len());
|
||||
|
||||
let info = s.execute("livePaint.info", &json!({"group": g.0})).unwrap();
|
||||
let part = |key: &str, id: NodeId| info[key].as_array().unwrap().iter().find(|p| p["id"] == id.0).unwrap().clone();
|
||||
let fill_row = |i: usize, p: Paint, o: f64, v: bool, drawn: bool| json!({"index": i, "kind": "fill", "paint": p.label(), "opacity": o, "visible": v, "drawn": drawn});
|
||||
let stroke_row = |i: usize, p: Paint, o: f64, v: bool, w: f64, drawn: bool| json!({"index": i, "kind": "stroke", "paint": p.label(), "opacity": o, "visible": v, "width": w, "drawn": drawn});
|
||||
|
||||
let f = part("faces", lower);
|
||||
assert_eq!(f["fill"], json!(Paint::None.label()), "fill stays the top fill");
|
||||
assert!(f["bounds"].is_array());
|
||||
assert_eq!(
|
||||
f["appearance"],
|
||||
json!([fill_row(0, red(), 1.0, true, true), stroke_row(1, blue(), 0.5, true, 2.0, true), fill_row(2, Paint::None, 1.0, true, false)])
|
||||
);
|
||||
assert_eq!(part("faces", quiet)["appearance"], json!([fill_row(0, red(), 1.0, false, false), fill_row(1, red(), 0.0, true, false)]));
|
||||
assert_eq!(part("faces", bare)["appearance"], json!([]));
|
||||
|
||||
let e = part("edges", two);
|
||||
assert_eq!((e["stroke"].clone(), e["width"].clone()), (json!(blue().label()), json!(2.0)), "stroke and width stay the top stroke's");
|
||||
assert_eq!(
|
||||
e["appearance"],
|
||||
json!([stroke_row(0, red(), 1.0, true, 4.0, true), fill_row(1, green(), 1.0, true, true), stroke_row(2, blue(), 1.0, true, 2.0, true)])
|
||||
);
|
||||
assert_eq!(part("edges", thin)["appearance"], json!([stroke_row(0, red(), 1.0, true, 0.0, false)]));
|
||||
for id in [clear_edge, hidden_edge] {
|
||||
assert_eq!(part("edges", id)["appearance"], json!([stroke_row(0, red(), 1.0, true, 2.0, false)]), "a part that is not shown draws no row");
|
||||
}
|
||||
}
|
||||
|
||||
/// Merge carries a face's whole appearance when one of its rows draws, and carries nothing from a
|
||||
/// face that draws nothing.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user