mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 14:03:16 -05:00
Tests: cover the theme picker's masks and blends, and pie legend contrast past the wedge limit
- recolor.pickTheme with an enabled clipping opacity mask: white mask art over one of three squares lets only that square's color through, black mask art hides all three, and inside a curved clip group the masked art shows only where the mask and the clipping path both let it. - recolor.pickTheme with a blend: a marquee over a middle step returns the colors of both end objects, also when a clip group around the blend hides the end objects. - Pie legend labels past the wedge measurement limit take the contrast most of their series' wedges show (2 of 3 dark gives dark, 1 of 3 gives light). The limit is read through a thread-local in tests, as the Extrude & Bevel face limit is; the shipped limit is unchanged.
This commit is contained in:
1 parent
4002b2b66c
commit
b7db671455
4 files changed
+150
-4
No files matched your search
@@ -47,8 +47,6 @@ pub(crate) use edit::{edit_graph_art, graphs_holding, keep_placed};
|
||||
pub use generate::calculated_axes;
|
||||
#[cfg(test)]
|
||||
use generate::generate;
|
||||
#[cfg(test)]
|
||||
use series::default_series_color;
|
||||
pub(crate) use series::{
|
||||
LabelTextChange, RowWrites, capture_label_anti_alias, capture_label_changes, capture_label_kind_rows, capture_label_object_opacity,
|
||||
capture_label_paragraphs, capture_label_rows, capture_label_styles, capture_label_styles_given, capture_label_text, capture_line_changes,
|
||||
@@ -57,6 +55,8 @@ pub(crate) use series::{
|
||||
is_graph_line, is_series_group, label_object_opacity_differs, label_rows, labels_in, paint_targets, plain_labels, series_group_marks,
|
||||
series_group_marks_where, series_groups_among, series_marks_in, series_object_opacity_differs, shown_paint, wedge_shades,
|
||||
};
|
||||
#[cfg(test)]
|
||||
use series::{WEDGE_BUDGET, default_series_color, legend_on_dark};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -1113,7 +1113,7 @@ fn redrawn_on_dark(n: &Node, g: &GraphSpec) -> Option<HashMap<NodeId, bool>> {
|
||||
/// on, which lies inside its wedge: the wedge holding it of the series the label names (the nearest one when none holds
|
||||
/// it), or of any series when the label names none. When the wedges of the series a label names are all dark or all
|
||||
/// light, the label's place does not matter.
|
||||
fn legend_on_dark(n: &Node, g: &GraphSpec) -> HashMap<NodeId, bool> {
|
||||
pub(super) fn legend_on_dark(n: &Node, g: &GraphSpec) -> HashMap<NodeId, bool> {
|
||||
wedge_contrast(&legend_labels(n), &pie_wedges(n), g)
|
||||
}
|
||||
|
||||
@@ -1134,6 +1134,28 @@ fn label_anchor(text: &vectorcraft_doc::TextObject, name: &str, g: &GraphSpec, l
|
||||
text.xf * vectorcraft_geom::Point::new(0.0, middle - ls * 0.35)
|
||||
}
|
||||
|
||||
/// Most wedges [`wedge_contrast`] measures for one pie's legend labels together.
|
||||
const WEDGE_MEASUREMENTS: usize = 1 << 20;
|
||||
|
||||
#[cfg(test)]
|
||||
thread_local! {
|
||||
/// The limit [`wedge_budget`] gives on this thread: [`WEDGE_MEASUREMENTS`] unless a test sets a lower one, so that a
|
||||
/// test of the limit uses a small pie.
|
||||
pub(super) static WEDGE_BUDGET: std::cell::Cell<usize> = const { std::cell::Cell::new(WEDGE_MEASUREMENTS) };
|
||||
}
|
||||
|
||||
/// Most wedges [`wedge_contrast`] measures: [`WEDGE_MEASUREMENTS`].
|
||||
#[cfg(not(test))]
|
||||
fn wedge_budget() -> usize {
|
||||
WEDGE_MEASUREMENTS
|
||||
}
|
||||
|
||||
/// Most wedges [`wedge_contrast`] measures: [`WEDGE_MEASUREMENTS`], or the lower limit a test set in [`WEDGE_BUDGET`].
|
||||
#[cfg(test)]
|
||||
fn wedge_budget() -> usize {
|
||||
WEDGE_BUDGET.with(std::cell::Cell::get)
|
||||
}
|
||||
|
||||
/// [`legend_on_dark`] for legend labels `legend` of a pie of spec `g` with wedges `wedges`.
|
||||
fn wedge_contrast(legend: &[&Node], wedges: &[Wedge], g: &GraphSpec) -> HashMap<NodeId, bool> {
|
||||
use vectorcraft_geom::kurbo::ParamCurveNearest;
|
||||
@@ -1164,7 +1186,7 @@ fn wedge_contrast(legend: &[&Node], wedges: &[Wedge], g: &GraphSpec) -> HashMap<
|
||||
let ls = label_size(&g.label_styles, Role::Legend);
|
||||
// The search for the nearest wedge looks at this many wedges in all; past it, a label takes the contrast most of the
|
||||
// wedges of its series show.
|
||||
let mut budget: usize = 1 << 20;
|
||||
let mut budget = wedge_budget();
|
||||
for t in legend {
|
||||
let NodeKind::Text(text) = &t.kind else { continue };
|
||||
let id = t.id;
|
||||
|
||||
@@ -10285,6 +10285,60 @@ fn a_color_mode_change_keeps_the_default_legend_fill_of_other_graphs() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Past the limit on wedge measurements, a legend label of a pie with Legends in Wedges takes the contrast most of the
|
||||
/// wedges of its series show: dark when 2 of its 3 wedges are dark, light when 1 of its 3 is.
|
||||
#[test]
|
||||
fn past_the_wedge_limit_a_legend_label_takes_the_contrast_most_of_its_wedges_show() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = make(&mut s, "pie", ",a,b\nP1,1,2\nP2,1,2\nP3,1,2");
|
||||
s.execute("graph.setType", &json!({"id": id.0, "pieLegend": "wedges"})).unwrap();
|
||||
// The first two wedges of series a are dark and the third light; only the first wedge of series b is dark.
|
||||
let wedge_ids = |s: &Session, index: u32| -> Vec<NodeId> {
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let mut ids = vec![];
|
||||
series(&n, index).walk(&mut |m| {
|
||||
if matches!(m.kind, NodeKind::Path { .. }) {
|
||||
ids.push(m.id);
|
||||
}
|
||||
});
|
||||
ids
|
||||
};
|
||||
let (a, b) = (wedge_ids(&s, 0), wedge_ids(&s, 1));
|
||||
assert_eq!((a.len(), b.len()), (3, 3), "one wedge of each series in each of the 3 pies");
|
||||
s.edit("shade", |d, _| {
|
||||
for (ids, darks) in [(&a, 2), (&b, 1)] {
|
||||
for (i, w) in ids.iter().enumerate() {
|
||||
let color = if i < darks { vectorcraft_color::Color::BLACK } else { vectorcraft_color::Color::WHITE };
|
||||
d.node_mut(*w).unwrap().appearance.set_fill(Paint::solid(color));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
let n = s.doc().unwrap().doc.node(id).unwrap().clone();
|
||||
let g = spec(&s, id);
|
||||
let label = |text: &str| -> Vec<NodeId> {
|
||||
group(&n, "Legend")
|
||||
.children()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.filter(|c| matches!(&c.kind, NodeKind::Text(t) if t.plain_text() == text))
|
||||
.map(|c| c.id)
|
||||
.collect()
|
||||
};
|
||||
let (label_a, label_b) = (label("a"), label("b"));
|
||||
assert!(!label_a.is_empty() && !label_b.is_empty(), "the legend labels a and b");
|
||||
let measured = super::legend_on_dark(&n, &g);
|
||||
super::WEDGE_BUDGET.with(|b| b.set(0));
|
||||
let past = super::legend_on_dark(&n, &g);
|
||||
super::WEDGE_BUDGET.with(|b| b.set(1 << 20));
|
||||
let on_dark = |map: &std::collections::HashMap<NodeId, bool>, ids: &[NodeId]| -> Vec<bool> { ids.iter().map(|i| map[i]).collect() };
|
||||
assert!(on_dark(&measured, &label_a).contains(&false), "measured: a label of a sits on its light wedge: {measured:?}");
|
||||
assert!(on_dark(&past, &label_a).iter().all(|d| *d), "past the limit: 2 of 3 dark gives dark: {past:?}");
|
||||
assert!(on_dark(&past, &label_b).iter().all(|d| !*d), "past the limit: 1 of 3 dark gives light: {past:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn labels_are_told_apart_by_where_they_come_not_by_their_text() {
|
||||
let mut s = Session::new();
|
||||
|
||||
@@ -1831,6 +1831,76 @@ fn theme_picker_leaves_out_art_a_curved_clipping_path_hides() {
|
||||
assert_eq!(pick_theme(&mut s, all), ["rgb 255 0 0"], "the corner square is hidden");
|
||||
}
|
||||
|
||||
/// A filled rectangle without a stroke.
|
||||
fn filled(s: &mut Session, [x, y, w, h]: [f64; 4], fill: &str) -> NodeId {
|
||||
let id = NodeId(run(s, "shape.rectangle", json!({"x": x, "y": y, "width": w, "height": h}))["id"].as_u64().unwrap());
|
||||
run(s, "paint.setFill", json!({"ids": [id.0], "color": fill}));
|
||||
run(s, "paint.setStroke", json!({"ids": [id.0], "none": true}));
|
||||
id
|
||||
}
|
||||
|
||||
/// An enabled opacity mask that clips shows the art only where its mask art is lighter than black: white mask art over
|
||||
/// the red square of three lets red alone through, and black mask art hides all three. Inside a clip group, the masked
|
||||
/// art shows only where the mask and the clipping path both let it.
|
||||
#[test]
|
||||
fn theme_picker_reads_only_what_a_clipping_opacity_mask_shows() {
|
||||
let all = json!({"picks": [{"rect": [-10, -10, 120, 110]}]});
|
||||
// A red, a blue and a green square under one mask.
|
||||
let masked = |[red, blue, green]: [[f64; 4]; 3], mask_art: [f64; 4], mask_fill: &str| {
|
||||
let mut s = session();
|
||||
let red = filled(&mut s, red, "#ff0000");
|
||||
let blue = filled(&mut s, blue, "#0000ff");
|
||||
let green = filled(&mut s, green, "#00ff00");
|
||||
let mask = filled(&mut s, mask_art, mask_fill);
|
||||
run(&mut s, "select.set", json!({"ids": [red.0, blue.0, green.0, mask.0]}));
|
||||
let g = NodeId(run(&mut s, "transparency.makeOpacityMask", json!({"clip": true}))["id"].as_u64().unwrap());
|
||||
(s, g)
|
||||
};
|
||||
// Red runs from x 10 to 25, blue from 40 to 55 and green from 70 to 85; the mask art covers red alone.
|
||||
let in_a_row = [[10.0, 10.0, 15.0, 15.0], [40.0, 10.0, 15.0, 15.0], [70.0, 10.0, 15.0, 15.0]];
|
||||
let over_red = [5.0, 5.0, 25.0, 25.0];
|
||||
let (mut s, _) = masked(in_a_row, over_red, "#ffffff");
|
||||
assert_eq!(pick_theme(&mut s, all.clone()), ["rgb 255 0 0"], "white mask art over the red square");
|
||||
let (mut s, _) = masked(in_a_row, over_red, "#000000");
|
||||
assert!(pick_theme(&mut s, all.clone()).is_empty(), "black mask art hides every square");
|
||||
// Red sits in the top left corner of a 100 × 100 ellipse's box, outside the ellipse; blue and green are inside it.
|
||||
// The mask art covers red and blue, and the ellipse clips the masked group.
|
||||
let around_an_ellipse = [[0.0, 0.0, 8.0, 8.0], [40.0, 40.0, 15.0, 15.0], [70.0, 40.0, 15.0, 15.0]];
|
||||
let (mut s, g) = masked(around_an_ellipse, [-5.0, -5.0, 65.0, 65.0], "#ffffff");
|
||||
let e = NodeId(run(&mut s, "shape.ellipse", json!({"x": 0, "y": 0, "width": 100, "height": 100}))["id"].as_u64().unwrap());
|
||||
run(&mut s, "select.set", json!({"ids": [g.0, e.0]}));
|
||||
run(&mut s, "object.clippingMask.make", json!({}));
|
||||
assert_eq!(pick_theme(&mut s, all), ["rgb 0 0 255"], "only blue shows through both");
|
||||
}
|
||||
|
||||
/// A blend a marquee touches is read whole: a marquee over a middle step alone takes the colors of both end objects,
|
||||
/// also when a clip group around the blend hides the end objects.
|
||||
#[test]
|
||||
fn theme_picker_reads_a_blend_whole() {
|
||||
let mut s = session();
|
||||
let circle = |s: &mut Session, x: f64, fill: &str| {
|
||||
let id = NodeId(run(s, "shape.ellipse", json!({"x": x, "y": 10, "width": 20, "height": 20}))["id"].as_u64().unwrap());
|
||||
run(s, "paint.setFill", json!({"ids": [id.0], "color": fill}));
|
||||
run(s, "paint.setStroke", json!({"ids": [id.0], "none": true}));
|
||||
id
|
||||
};
|
||||
// The end objects are centered on x 20 and 260; the 3 steps between them on x 80, 140 and 200.
|
||||
let (red, blue) = (circle(&mut s, 10.0, "#ff0000"), circle(&mut s, 250.0, "#0000ff"));
|
||||
run(&mut s, "select.set", json!({"ids": [red.0, blue.0]}));
|
||||
let blend = NodeId(run(&mut s, "object.blend.make", json!({"steps": 3}))["id"].as_u64().unwrap());
|
||||
let middle = json!({"picks": [{"rect": [135, 15, 145, 25]}]});
|
||||
let mut got = pick_theme(&mut s, middle.clone());
|
||||
got.sort();
|
||||
assert_eq!(got, ["rgb 0 0 255", "rgb 255 0 0"], "the middle step gives both end colors");
|
||||
// A clipping path over the middle step alone.
|
||||
let clip = filled(&mut s, [120.0, 5.0, 40.0, 30.0], "#00ff00");
|
||||
run(&mut s, "select.set", json!({"ids": [blend.0, clip.0]}));
|
||||
run(&mut s, "object.clippingMask.make", json!({}));
|
||||
let mut got = pick_theme(&mut s, middle);
|
||||
got.sort();
|
||||
assert_eq!(got, ["rgb 0 0 255", "rgb 255 0 0"], "the clip group around the blend leaves the end objects in");
|
||||
}
|
||||
|
||||
/// In isolation mode a click and a marquee read only the isolated art.
|
||||
#[test]
|
||||
fn theme_picker_reads_only_the_isolated_art() {
|
||||
|
||||
Reference in new issue
Block a user