mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-17 05:35:52 +00:00
Merge main into feat/ai-save-graph-records
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use vectorcraft_doc::graph::art_bounds;
|
||||
|
||||
use super::axis::{MAX_TICK_MARKS, count_given, one_line};
|
||||
use super::data::apply_data;
|
||||
use super::generate::{children_bounds, generate};
|
||||
@@ -17,11 +19,62 @@ fn bound_param(p: &Value, key: &str, cmd: &str) -> Result<Option<Option<f64>>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The bounds a redraw of graph `n` measures a move or scale on, as kept at the last generation and as they are now:
|
||||
/// those of its art without type ([`GraphSpec::placed_art`]), or those of all its art when the graph keeps no such
|
||||
/// bounds with a width and a height, or has no art without type now.
|
||||
fn measured(n: &Node) -> Option<(Rect, Rect)> {
|
||||
let kept = n.extras().graph.as_deref()?;
|
||||
let c = n.children()?;
|
||||
let art = kept.placed_art.filter(|p| p.width() > 1e-9 && p.height() > 1e-9).zip(art_bounds(c));
|
||||
art.or_else(|| kept.placed.zip(children_bounds(c)))
|
||||
}
|
||||
|
||||
/// `r` carried by the change from bounds `from` to bounds `to`: each edge stays at the same place relative to them.
|
||||
/// `None` when `from` has no width or no height.
|
||||
fn carried(r: Rect, from: Rect, to: Rect) -> Option<Rect> {
|
||||
if from.width() <= 1e-9 || from.height() <= 1e-9 {
|
||||
return None;
|
||||
}
|
||||
let (sx, sy) = (to.width() / from.width(), to.height() / from.height());
|
||||
Some(Rect::new(to.x0 + (r.x0 - from.x0) * sx, to.y0 + (r.y0 - from.y0) * sy, to.x0 + (r.x1 - from.x0) * sx, to.y0 + (r.y1 - from.y0) * sy))
|
||||
}
|
||||
|
||||
/// The graphs holding objects `ids`, each with the bounds of its art without type and of all its art. Give them to
|
||||
/// [`keep_placed`] after an edit that changes a graph's art without moving the graph.
|
||||
pub(crate) fn graphs_holding(d: &Document, ids: &[NodeId]) -> Vec<(NodeId, Option<Rect>, Option<Rect>)> {
|
||||
let mut out: Vec<(NodeId, Option<Rect>, Option<Rect>)> = vec![];
|
||||
for id in ids {
|
||||
let Some(chain) = d.ancestry(*id) else { continue };
|
||||
let holder = chain.iter().rev().skip(1).copied().find(|a| d.node(*a).is_some_and(|n| n.extras().graph.is_some()));
|
||||
let Some(gid) = holder.filter(|g| !out.iter().any(|(o, ..)| o == g)) else { continue };
|
||||
let children = d.node(gid).and_then(Node::children);
|
||||
out.push((gid, children.and_then(|c| art_bounds(c)), children.and_then(|c| children_bounds(c))));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// After an edit that changed the art of `graphs` (from [`graphs_holding`]) without moving them, set the bounds each
|
||||
/// keeps so that its next redraw measures the same move or scale as it would have before the edit.
|
||||
pub(crate) fn keep_placed(d: &mut Document, graphs: &[(NodeId, Option<Rect>, Option<Rect>)]) {
|
||||
for &(gid, art_was, whole_was) in graphs {
|
||||
let Some(n) = d.node(gid) else { continue };
|
||||
let children = n.children();
|
||||
let (art_now, whole_now) = (children.and_then(|c| art_bounds(c)), children.and_then(|c| children_bounds(c)));
|
||||
let Some(spec) = d.node_mut(gid).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { continue };
|
||||
if let (Some(placed), Some(was), Some(now)) = (spec.placed_art, art_was, art_now) {
|
||||
spec.placed_art = carried(now, was, placed).or(spec.placed_art);
|
||||
}
|
||||
if let (Some(placed), Some(was), Some(now)) = (spec.placed, whole_was, whole_now) {
|
||||
spec.placed = carried(now, was, placed).or(spec.placed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Regenerate graph `id` from `spec` (adjusting the plot rect for any move/scale since the last
|
||||
/// generation, which is recorded in the spec's `placed` bounds).
|
||||
/// generation, which is recorded in the spec's `placed_art` bounds, or `placed` in graphs saved without them).
|
||||
pub(super) fn regenerate(d: &mut Document, id: NodeId, mut spec: GraphSpec) -> Result<()> {
|
||||
let n = d.node(id).ok_or(EngineError::NoNode(id))?;
|
||||
if let (Some(placed), Some(now)) = (n.extras().graph.as_ref().and_then(|g| g.placed), n.children().and_then(|c| children_bounds(c)))
|
||||
if let Some((placed, now)) = measured(n)
|
||||
&& placed.width() > 1e-9
|
||||
&& placed.height() > 1e-9
|
||||
&& (placed.x0 - now.x0).abs() + (placed.y0 - now.y0).abs() + (placed.x1 - now.x1).abs() + (placed.y1 - now.y1).abs() > 1e-6
|
||||
@@ -33,6 +86,7 @@ pub(super) fn regenerate(d: &mut Document, id: NodeId, mut spec: GraphSpec) -> R
|
||||
}
|
||||
let children = generate(d, &spec);
|
||||
spec.placed = children_bounds(&children);
|
||||
spec.placed_art = art_bounds(&children);
|
||||
let n = d.node_mut(id).ok_or(EngineError::NoNode(id))?;
|
||||
if let Some(ch) = n.children_mut() {
|
||||
*ch = children;
|
||||
|
||||
@@ -631,19 +631,33 @@ pub(crate) fn capture_label_styles(doc: &mut Document, ids: &[NodeId], change: &
|
||||
changed
|
||||
}
|
||||
|
||||
/// Apply `change` to the style graph `graph` keeps for its labels of kind `role` (or the default one). Returns whether
|
||||
/// the kept style changed.
|
||||
/// Apply `change` to the style graph `graph` keeps for its labels of kind `role` (or the default one). A `change` that
|
||||
/// sets the fill of legend labels marks it as set on them, so a black one stays black on dark pie wedges. Returns whether
|
||||
/// the kept style or that mark changed.
|
||||
fn store_label_style(doc: &mut Document, graph: NodeId, role: Role, change: &dyn Fn(&mut CharStyle)) -> bool {
|
||||
let Some(spec) = doc.node_mut(graph).and_then(|n| n.extras_if_any().and_then(|e| e.graph.as_deref_mut())) else { return false };
|
||||
let mut style = label_style(&spec.label_styles, role);
|
||||
// The kept fill can already be the one `change` sets (black, the default): `change` sets the fill when it gives one
|
||||
// of two different fills something else.
|
||||
let sets_fill = role == Role::Legend
|
||||
&& [Paint::None, Paint::solid(Color::BLACK)].into_iter().any(|fill| {
|
||||
let mut probe = CharStyle { fill: fill.clone(), ..style.clone() };
|
||||
change(&mut probe);
|
||||
probe.fill != fill
|
||||
});
|
||||
change(&mut style);
|
||||
let styles = &mut spec.label_styles;
|
||||
let slot = match role {
|
||||
Role::Value => &mut spec.label_styles.values,
|
||||
Role::Category => &mut spec.label_styles.categories,
|
||||
Role::Legend => &mut spec.label_styles.legend,
|
||||
Role::Value => &mut styles.values,
|
||||
Role::Category => &mut styles.categories,
|
||||
Role::Legend => &mut styles.legend,
|
||||
};
|
||||
let changed = slot.as_ref() != Some(&style);
|
||||
let mut changed = slot.as_ref() != Some(&style);
|
||||
*slot = Some(style);
|
||||
if sets_fill && !styles.legend_fill_set {
|
||||
styles.legend_fill_set = true;
|
||||
changed = true;
|
||||
}
|
||||
changed
|
||||
}
|
||||
|
||||
@@ -872,12 +886,9 @@ fn run_paint_change(was: &Node, now: &Node, fill: bool) -> Option<(CharStyle, Ve
|
||||
}
|
||||
|
||||
/// Does graph `g` pick the fill of its legend labels itself, white on a dark wedge and black on the others: a pie with
|
||||
/// Legends in Wedges whose legend labels keep no fill of their own?
|
||||
/// Legends in Wedges whose legend labels keep no fill of their own ([`GraphSpec::picks_legend_fill`])?
|
||||
pub(super) fn picks_legend_fill(g: &GraphSpec) -> bool {
|
||||
g.kind == GraphKind::Pie
|
||||
&& g.legend
|
||||
&& g.pie_legend_in_wedges
|
||||
&& !g.label_styles.legend.as_ref().is_some_and(|st| st.fill != Paint::solid(Color::BLACK))
|
||||
g.picks_legend_fill()
|
||||
}
|
||||
|
||||
/// The graph labels among `roots` and inside them, for an edit that reaches graph labels without naming them.
|
||||
|
||||
@@ -5691,6 +5691,151 @@ fn formatting_one_label_keeps_that_kind_of_label() {
|
||||
assert!(serde_json::to_value(GraphSpec::default()).unwrap().get("labelStyles").is_none());
|
||||
}
|
||||
|
||||
/// Character and paragraph edits that resize a graph's labels leave the plot where it is when the graph is drawn
|
||||
/// again, and a move or scale of the graph made after them is still followed.
|
||||
#[test]
|
||||
fn restyling_a_graphs_labels_leaves_the_plot_in_place() {
|
||||
let csv = ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6";
|
||||
let redraw = |s: &mut Session, id: NodeId| s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let rect = spec(&s, id).rect;
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("text.setStyle", &json!({"size": 14})).unwrap();
|
||||
redraw(&mut s, id);
|
||||
assert_eq!(spec(&s, id).rect, rect, "after a size change to every label");
|
||||
let c = text_id(&s, id, "Axes", "Q2");
|
||||
s.execute("text.setFormat", &json!({"ids": [c.0], "leftIndent": 20})).unwrap();
|
||||
redraw(&mut s, id);
|
||||
assert_eq!(spec(&s, id).rect, rect, "after an indent on the category labels");
|
||||
let l = text_id(&s, id, "Legend", "2025");
|
||||
s.execute("text.setStyle", &json!({"ids": [l.0], "size": 4})).unwrap();
|
||||
redraw(&mut s, id);
|
||||
assert_eq!(spec(&s, id).rect, rect, "after a smaller size on the legend labels");
|
||||
// A move and a scale of the whole graph still carry the plot with them.
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 40, "dy": -10})).unwrap();
|
||||
redraw(&mut s, id);
|
||||
let moved = spec(&s, id).rect;
|
||||
assert!((moved.x0 - (rect.x0 + 40.0)).abs() < 1e-6 && (moved.y0 - (rect.y0 - 10.0)).abs() < 1e-6, "{moved:?}");
|
||||
assert!((moved.width() - rect.width()).abs() < 1e-6 && (moved.height() - rect.height()).abs() < 1e-6, "{moved:?}");
|
||||
s.execute("object.scale", &json!({"sx": 200, "sy": 100, "origin": [moved.x0, moved.y0]})).unwrap();
|
||||
redraw(&mut s, id);
|
||||
let scaled = spec(&s, id).rect;
|
||||
assert!((scaled.width() - 2.0 * rect.width()).abs() < 1e-6 && (scaled.height() - rect.height()).abs() < 1e-6, "{scaled:?}");
|
||||
assert!((scaled.x0 - moved.x0).abs() < 1e-6 && (scaled.y0 - moved.y0).abs() < 1e-6, "{scaled:?}");
|
||||
}
|
||||
|
||||
/// Moving one label of a graph on its own leaves the plot where it is when the graph is drawn again.
|
||||
#[test]
|
||||
fn moving_one_label_leaves_the_plot_in_place() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let rect = spec(&s, id).rect;
|
||||
for (grp, text) in [("Legend", "2025"), ("Axes", "Q2")] {
|
||||
let l = text_id(&s, id, grp, text);
|
||||
s.execute("select.set", &json!({"ids": [l.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 120, "dy": 60})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6"})).unwrap();
|
||||
assert_eq!(spec(&s, id).rect, rect, "after moving {text}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Label edits leave the plot in place in graphs whose legend swatches follow the size of the labels at the next
|
||||
/// drawing: a legend across the top, a legend after a right value axis, and legend labels of several lines. A move of
|
||||
/// the graph made after them is still followed.
|
||||
#[test]
|
||||
fn restyling_the_labels_of_a_graph_whose_legend_follows_them_leaves_the_plot_in_place() {
|
||||
let csv = ",North|East|Coast,South|West,Inland|Hills|Lakes|Plains\nQ1,3,2,4\nQ2,5,4,1\nQ3,-1,6,2";
|
||||
for options in [json!({"legendAcrossTop": true}), json!({"valueAxis": "right"}), json!({"valueAxis": "both"}), json!({})] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("graph.setType", &options).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
let rect = spec(&s, id).rect;
|
||||
for size in [16, 5] {
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("text.setStyle", &json!({"size": size})).unwrap();
|
||||
for _ in 0..2 {
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
assert_eq!(spec(&s, id).rect, rect, "{options}: labels at {size} pt");
|
||||
}
|
||||
}
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 30, "dy": 20})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
let moved = spec(&s, id).rect;
|
||||
assert!((moved.x0 - (rect.x0 + 30.0)).abs() < 1e-6 && (moved.y0 - (rect.y0 + 20.0)).abs() < 1e-6, "{options}: {moved:?}");
|
||||
assert!((moved.width() - rect.width()).abs() < 1e-6 && (moved.height() - rect.height()).abs() < 1e-6, "{options}: {moved:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Outlining a graph's labels leaves the plot where it is when the graph is drawn again, and a move of the graph made
|
||||
/// before them is still followed.
|
||||
#[test]
|
||||
fn outlining_a_graphs_labels_leaves_the_plot_in_place() {
|
||||
let csv = ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6";
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let rect = spec(&s, id).rect;
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [text_id(&s, id, "Axes", "Q2").0, text_id(&s, id, "Legend", "2025").0]})).unwrap();
|
||||
s.execute("type.createOutlines", &json!({})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
assert_eq!(spec(&s, id).rect, rect, "after outlining labels");
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 30, "dy": 15})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [text_id(&s, id, "Axes", "Q1").0, text_id(&s, id, "Legend", "2024").0]})).unwrap();
|
||||
s.execute("type.createOutlines", &json!({})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": csv})).unwrap();
|
||||
let moved = spec(&s, id).rect;
|
||||
assert!((moved.x0 - (rect.x0 + 30.0)).abs() < 1e-6 && (moved.y0 - (rect.y0 + 15.0)).abs() < 1e-6, "{moved:?}");
|
||||
assert!((moved.width() - rect.width()).abs() < 1e-6 && (moved.height() - rect.height()).abs() < 1e-6, "{moved:?}");
|
||||
}
|
||||
|
||||
/// A graph whose kept bounds of its art without type have no width follows a move with the bounds of all of its art.
|
||||
#[test]
|
||||
fn a_graph_whose_art_without_type_kept_no_width_still_follows_a_move() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let rect = spec(&s, id).rect;
|
||||
let mut flat = spec(&s, id);
|
||||
let art = flat.placed_art.unwrap();
|
||||
flat.placed_art = Some(vectorcraft_geom::Rect::new(art.x0, art.y0, art.x0, art.y1));
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(id).unwrap().extras_mut().graph = Some(Box::new(flat));
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 25, "dy": 5})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6"})).unwrap();
|
||||
let moved = spec(&s, id).rect;
|
||||
assert!((moved.x0 - (rect.x0 + 25.0)).abs() < 1e-6 && (moved.y0 - (rect.y0 + 5.0)).abs() < 1e-6, "{moved:?}");
|
||||
}
|
||||
|
||||
/// A graph saved before it kept the bounds of its art without type follows a move made since with its old bounds.
|
||||
#[test]
|
||||
fn a_graph_saved_with_only_its_whole_bounds_still_follows_a_move() {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = graph(&mut s, "column");
|
||||
let rect = spec(&s, id).rect;
|
||||
let mut old = serde_json::to_value(spec(&s, id)).unwrap();
|
||||
old.as_object_mut().unwrap().remove("placedArt");
|
||||
let old: GraphSpec = serde_json::from_value(old).unwrap();
|
||||
assert_eq!(old.placed_art, None);
|
||||
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc).node_mut(id).unwrap().extras_mut().graph = Some(Box::new(old));
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 25, "dy": 5})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": ",2024,2025\nQ1,3,2\nQ2,5,4\nQ3,-1,6"})).unwrap();
|
||||
let moved = spec(&s, id).rect;
|
||||
assert!((moved.x0 - (rect.x0 + 25.0)).abs() < 1e-6 && (moved.y0 - (rect.y0 + 5.0)).abs() < 1e-6, "{moved:?}");
|
||||
assert!(spec(&s, id).placed_art.is_some(), "the next drawing keeps the bounds of the art without type");
|
||||
}
|
||||
|
||||
/// Each Axes and Legend text after the graph is drawn again, with its object rows (fill or stroke, color, opacity, and
|
||||
/// for a stroke its weight) and its object opacity.
|
||||
type RowLooks = Vec<(bool, String, f32, f64)>;
|
||||
@@ -6361,6 +6506,93 @@ fn pie_names_and_wedge_labels_keep_their_style() {
|
||||
assert_eq!(fills, ["#00ff00", "#00ff00"], "the kept fill, also on the dark wedge");
|
||||
}
|
||||
|
||||
/// On a pie with Legends in Wedges, a black fill given to the white legend label on the dark wedge is kept for every
|
||||
/// legend label: the graph draws it black on the dark wedge again, also after a native save and open.
|
||||
#[test]
|
||||
fn a_black_fill_set_on_a_wedge_legend_label_stays_black_on_dark_wedges() {
|
||||
let cases = [("paint.setFill", json!({"color": "#000000"})), ("text.setStyle", json!({"fill": "#000000"})), ("edit.colors.invert", json!({}))];
|
||||
every_case(cases, |(cmd, params)| {
|
||||
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");
|
||||
s.execute("graph.setType", &json!({"id": id.0, "pieLegend": "wedges"})).unwrap();
|
||||
let legend_fill = |s: &Session, text: &str| {
|
||||
let NodeKind::Text(t) = label_node(s, id, "Legend", text).kind else { panic!("{text} is not type") };
|
||||
t.runs[0].style.fill.color().map(|c| c.to_hex()).unwrap_or_default()
|
||||
};
|
||||
assert_eq!(legend_fill(&s, "a"), "#ffffff", "white on the black wedge");
|
||||
let mut p = params.clone();
|
||||
p["ids"] = json!([text_id(&s, id, "Legend", "a").0]);
|
||||
s.execute(cmd, &p).unwrap();
|
||||
assert!(spec(&s, id).label_styles.legend_fill_set, "{cmd}");
|
||||
s.execute("edit.undo", &json!({})).unwrap();
|
||||
assert!(!spec(&s, id).label_styles.legend_fill_set, "{cmd}: undone");
|
||||
assert_eq!(legend_fill(&s, "a"), "#ffffff", "{cmd}: white again after the undo");
|
||||
s.execute("edit.redo", &json!({})).unwrap();
|
||||
assert!(spec(&s, id).label_styles.legend_fill_set, "{cmd}: redone");
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2]]})).unwrap();
|
||||
assert_eq!((legend_fill(&s, "a"), legend_fill(&s, "b")), ("#000000".into(), "#000000".into()), "{cmd}: black on both wedges");
|
||||
let dir = vectorcraft_testkit::TempDir::new("graph-wedge-legend");
|
||||
let path = dir.join("pie.vectorcraft");
|
||||
s.execute("document.save", &json!({"path": path})).unwrap();
|
||||
s.execute("document.open", &json!({"path": path})).unwrap();
|
||||
assert!(spec(&s, id).label_styles.legend_fill_set, "{cmd}: kept through save and open");
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[2, 1]]})).unwrap();
|
||||
assert_eq!(legend_fill(&s, "a"), "#000000", "{cmd}: still black after open");
|
||||
});
|
||||
}
|
||||
|
||||
/// A size given to the legend labels of a pie with Legends in Wedges keeps the default black fill with it. A color mode
|
||||
/// change leaves that fill black, so the legend label on the dark wedge is still drawn white. A legend fill that is not
|
||||
/// black, in a file saved without the mark, is marked as set by the change.
|
||||
#[test]
|
||||
fn a_color_mode_change_keeps_the_default_wedge_legend_fill() {
|
||||
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");
|
||||
s.execute("graph.setType", &json!({"id": id.0, "pieLegend": "wedges"})).unwrap();
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("text.setStyle", &json!({"size": 11})).unwrap();
|
||||
s.execute("file.documentColorMode", &json!({"mode": "cmyk"})).unwrap();
|
||||
let styles = spec(&s, id).label_styles;
|
||||
assert!(!styles.legend_fill_set && !styles.legend_fill_is_own(), "{styles:?}");
|
||||
s.execute("graph.setData", &json!({"id": id.0, "rows": [[1, 2]]})).unwrap();
|
||||
let NodeKind::Text(t) = label_node(&s, id, "Legend", "a").kind else { panic!("a is not type") };
|
||||
assert_eq!(t.runs[0].style.fill.color().map(|c| c.to_hex()), Some("#ffffff".into()), "white on the black wedge");
|
||||
let red = Paint::solid(vectorcraft_color::Color::from_hex("#ff0000").unwrap());
|
||||
let styles = vectorcraft_doc::LabelStyles { legend: Some(vectorcraft_doc::CharStyle { fill: red, ..Default::default() }), ..Default::default() };
|
||||
assert!(!styles.legend_fill_set && styles.legend_fill_is_own(), "{styles:?}");
|
||||
s.edit("legacy", |d, _| {
|
||||
d.node_mut(id).unwrap().extras_mut().graph.as_deref_mut().unwrap().label_styles = styles;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.execute("file.documentColorMode", &json!({"mode": "rgb"})).unwrap();
|
||||
assert!(spec(&s, id).label_styles.legend_fill_set, "the converted red is still the labels' own");
|
||||
}
|
||||
|
||||
/// The default black legend fill of a graph that keeps its legend beside the marks stays the default through a color
|
||||
/// mode change, so turning on Legends in Wedges later still draws the labels on dark wedges white.
|
||||
#[test]
|
||||
fn a_color_mode_change_keeps_the_default_legend_fill_of_other_graphs() {
|
||||
// A pie made here keeps its legend beside the pies.
|
||||
for kind in ["column", "pie"] {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 800})).unwrap();
|
||||
let id = make(&mut s, kind, ",a,b\nP1,1,2");
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("text.setStyle", &json!({"size": 11})).unwrap();
|
||||
let black = Paint::solid(vectorcraft_color::Color::BLACK);
|
||||
let before = spec(&s, id).label_styles;
|
||||
assert_eq!(before.legend.as_ref().map(|st| st.fill.clone()), Some(black.clone()), "{kind}: {before:?}");
|
||||
s.execute("file.documentColorMode", &json!({"mode": "cmyk"})).unwrap();
|
||||
let after = spec(&s, id).label_styles;
|
||||
assert_eq!(after.legend.as_ref().map(|st| st.fill.clone()), Some(black), "{kind}: {after:?}");
|
||||
assert!(!after.legend_fill_set && !after.legend_fill_is_own(), "{kind}: still the default: {after:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn labels_are_told_apart_by_where_they_come_not_by_their_text() {
|
||||
let mut s = Session::new();
|
||||
@@ -6898,3 +7130,42 @@ fn a_drawn_graph_saved_as_ai_opens_as_a_graph_unless_the_save_warns() {
|
||||
}
|
||||
assert!(live.contains(&"column v7.ai".to_string()) && live.contains(&"column pdf.ai".to_string()), "{live:?}");
|
||||
}
|
||||
|
||||
/// A graph opened from a `.ai` keeps the bounds of its art without type, so moving one of its labels on its own
|
||||
/// leaves the plot where it is at the first redraw.
|
||||
#[test]
|
||||
fn a_graph_opened_from_ai_measures_moves_without_its_labels() {
|
||||
let graph = "%_Gs\n%_100 200 300 100 Gb\n%_5 0 0 14 0 1 0 0 6 44 Gy\n%_10 2 0 0 90 80 0 Gd\n\
|
||||
%_1 () Ga\n%_0 13 0 0 0 0 0 () GA\n%_2 () Ga\n%_0 14 0 0 0 0 0 () GA\n%_4 () Ga\n%_0 14 0 0 0 0 0 () GA\n\
|
||||
%_2 2 1 1 Gz\n%_() Gc (East) Gc (Q1) Gc 5 Gc GC\n\
|
||||
u\nu\n0.6 0 1 0 k\n110 100 m\n110 150 L\n130 150 L\n130 100 L\nf\nU\n%_2 1 0 0 Go\n\
|
||||
u\n0 0 0 1 K\n100 200 m\n100 100 L\n300 100 L\nS\nU\n\
|
||||
0 To\n1 0 0 1 100 220 0 Tp\nTP\n0 Tr\n0 0 0 1 k\n/_Helvetica 12 Tf\n(Sales) Tx\nTO\nU\n%_GS\n";
|
||||
let text = format!(
|
||||
"%!PS-Adobe-3.0\n%%Creator: a script\n%%BoundingBox: 0 0 400 400\n%AI5_FileFormat 3\n%AI3_Cropmarks: 0 0 400 400\n%%EndComments\n\
|
||||
%%BeginProlog\n%%EndProlog\n%%BeginSetup\n%%EndSetup\n%AI5_BeginLayer\n1 1 1 1 0 0 0 79 128 255 Lb\n(Graph) Ln\n0 A\n{graph}LB\n\
|
||||
%AI5_EndLayer--\n%%PageTrailer\n%%Trailer\n%%EOF\n"
|
||||
);
|
||||
let dir = vectorcraft_testkit::temp_dir("graph-ai-label");
|
||||
let path = dir.join("graph.ai");
|
||||
std::fs::write(&path, text).unwrap();
|
||||
let mut s = Session::new();
|
||||
s.execute("document.open", &json!({"path": path})).unwrap();
|
||||
let (mut graphs, mut labels) = (vec![], vec![]);
|
||||
s.doc().unwrap().doc.walk(|n| {
|
||||
if n.extras().graph.is_some() {
|
||||
graphs.push(n.id);
|
||||
}
|
||||
if matches!(&n.kind, NodeKind::Text(t) if t.plain_text() == "Sales") {
|
||||
labels.push(n.id);
|
||||
}
|
||||
});
|
||||
assert_eq!((graphs.len(), labels.len()), (1, 1));
|
||||
let (id, label) = (graphs[0], labels[0]);
|
||||
let before = spec(&s, id);
|
||||
assert!(before.placed_art.is_some());
|
||||
s.execute("select.set", &json!({"ids": [label.0]})).unwrap();
|
||||
s.execute("object.move", &json!({"dx": 80, "dy": -40})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": id.0, "csv": ",East\nQ1,8\nQ2,2"})).unwrap();
|
||||
assert_eq!(spec(&s, id).rect, before.rect);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user