mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
291 lines
13 KiB
Rust
291 lines
13 KiB
Rust
//! Image Trace objects' View: how a live trace draws on screen.
|
||
//!
|
||
//! An Image Trace object is a group whose `trace` holds `{preset, params, view, made}` (see
|
||
//! `imageTrace.make`). The view is a viewing aid only: exports, printing and Expand always use the
|
||
//! tracing result, whatever it is set to. `made` names the objects the trace made ([`TraceMade`]).
|
||
|
||
use crate::NodeId;
|
||
use crate::node::Node;
|
||
|
||
/// The key of an Image Trace object's `trace` settings that names the objects its trace made.
|
||
pub const TRACE_MADE: &str = "made";
|
||
|
||
/// The objects the trace of an Image Trace object made, by id: the ranges `[first, last]` of their
|
||
/// ids, ascending and apart. A trace gives out its ids one after the other, so a trace of any size
|
||
/// is a few ranges.
|
||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||
pub struct TraceMade(Vec<(u64, u64)>);
|
||
|
||
impl TraceMade {
|
||
/// The objects `ids` name.
|
||
pub fn of(ids: impl IntoIterator<Item = NodeId>) -> Self {
|
||
let mut ids: Vec<u64> = ids.into_iter().map(|i| i.0).collect();
|
||
ids.sort_unstable();
|
||
ids.dedup();
|
||
let mut ranges: Vec<(u64, u64)> = vec![];
|
||
for id in ids {
|
||
match ranges.last_mut() {
|
||
Some((_, last)) if last.checked_add(1) == Some(id) => *last = id,
|
||
_ => ranges.push((id, id)),
|
||
}
|
||
}
|
||
Self(ranges)
|
||
}
|
||
|
||
/// Whether the trace made the object `id`.
|
||
pub fn contains(&self, id: NodeId) -> bool {
|
||
let i = self.0.partition_point(|(_, last)| *last < id.0);
|
||
self.0.get(i).is_some_and(|(first, _)| *first <= id.0)
|
||
}
|
||
|
||
/// The ranges as the `trace` settings save them: `[[first, last], ...]`.
|
||
pub fn to_json(&self) -> serde_json::Value {
|
||
serde_json::Value::Array(self.0.iter().map(|(a, b)| serde_json::json!([a, b])).collect())
|
||
}
|
||
|
||
/// The ranges `v` saves, or `None` when it is not a list of `[first, last]` pairs of whole
|
||
/// numbers, ascending and apart.
|
||
pub fn from_json(v: &serde_json::Value) -> Option<Self> {
|
||
let mut ranges: Vec<(u64, u64)> = vec![];
|
||
for r in v.as_array()? {
|
||
let [a, b] = r.as_array()?.as_slice() else { return None };
|
||
let (a, b) = (a.as_u64()?, b.as_u64()?);
|
||
if a > b || ranges.last().is_some_and(|(_, last)| *last >= a) {
|
||
return None;
|
||
}
|
||
ranges.push((a, b));
|
||
}
|
||
Some(Self(ranges))
|
||
}
|
||
}
|
||
|
||
/// Image Trace › View.
|
||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||
pub enum TraceView {
|
||
/// The traced shapes as they are filled.
|
||
#[default]
|
||
Result,
|
||
/// The traced shapes, with their outlines over them.
|
||
ResultWithOutlines,
|
||
/// Only the traced shapes' outlines.
|
||
Outlines,
|
||
/// The source image, with the traced shapes' outlines over it.
|
||
OutlinesWithSource,
|
||
/// Only the source image.
|
||
Source,
|
||
/// The source image on the part of the image left of a divider across it, and the traced
|
||
/// shapes everywhere else ([`TraceView::split_of`]).
|
||
Split,
|
||
}
|
||
|
||
/// Where the Split view's divider is when the `trace` settings name no valid position: across the
|
||
/// middle of the source image.
|
||
pub const DEFAULT_TRACE_SPLIT: f64 = 0.5;
|
||
|
||
impl TraceView {
|
||
/// Panel order.
|
||
pub const ALL: [Self; 6] = [Self::Result, Self::ResultWithOutlines, Self::Outlines, Self::OutlinesWithSource, Self::Source, Self::Split];
|
||
|
||
/// The id used in command params and in the saved `trace` object.
|
||
pub fn id(self) -> &'static str {
|
||
match self {
|
||
Self::Result => "tracingResult",
|
||
Self::ResultWithOutlines => "tracingResultWithOutlines",
|
||
Self::Outlines => "outlines",
|
||
Self::OutlinesWithSource => "outlinesWithSourceImage",
|
||
Self::Source => "sourceImage",
|
||
Self::Split => "split",
|
||
}
|
||
}
|
||
|
||
/// The view an id names (any case).
|
||
pub fn from_id(id: &str) -> Option<Self> {
|
||
Self::ALL.into_iter().find(|v| v.id().eq_ignore_ascii_case(id))
|
||
}
|
||
|
||
/// The view an Image Trace object's `trace` settings ask for (the tracing result when they
|
||
/// name none, or an unknown one).
|
||
pub fn of(trace: &serde_json::Value) -> Self {
|
||
trace.get("view").and_then(serde_json::Value::as_str).and_then(Self::from_id).unwrap_or_default()
|
||
}
|
||
|
||
/// The Split view's divider in `trace` settings: the fraction of the source image's width, from
|
||
/// its left edge in its own pixels, that shows the source image ([`DEFAULT_TRACE_SPLIT`] when
|
||
/// `split` is missing, not a number or outside 0 to 1).
|
||
pub fn split_of(trace: &serde_json::Value) -> f64 {
|
||
trace.get("split").and_then(serde_json::Value::as_f64).filter(|f| (0.0..=1.0).contains(f)).unwrap_or(DEFAULT_TRACE_SPLIT)
|
||
}
|
||
|
||
/// Whether the source image shows (in the Split view, on one side of the divider).
|
||
pub fn shows_source(self) -> bool {
|
||
matches!(self, Self::OutlinesWithSource | Self::Source | Self::Split)
|
||
}
|
||
|
||
/// Whether the traced shapes show filled (in the Split view, on one side of the divider).
|
||
pub fn shows_result(self) -> bool {
|
||
matches!(self, Self::Result | Self::ResultWithOutlines | Self::Split)
|
||
}
|
||
|
||
/// Whether the traced shapes' outlines show.
|
||
pub fn shows_outlines(self) -> bool {
|
||
matches!(self, Self::ResultWithOutlines | Self::Outlines | Self::OutlinesWithSource)
|
||
}
|
||
}
|
||
|
||
impl Node {
|
||
/// The View of an Image Trace object (the tracing result for anything else, or an unknown id).
|
||
pub fn trace_view(&self) -> TraceView {
|
||
self.extras().trace.as_deref().map(TraceView::of).unwrap_or_default()
|
||
}
|
||
|
||
/// The Split view's divider of an Image Trace object ([`TraceView::split_of`];
|
||
/// [`DEFAULT_TRACE_SPLIT`] for anything else).
|
||
pub fn trace_split(&self) -> f64 {
|
||
self.extras().trace.as_deref().map_or(DEFAULT_TRACE_SPLIT, TraceView::split_of)
|
||
}
|
||
|
||
/// The objects the trace of this Image Trace object made ([`TRACE_MADE`]), or `None` when its
|
||
/// `trace` settings name none, as in a tracing saved before they did, or name them in any other
|
||
/// form than [`TraceMade::to_json`]'s.
|
||
pub fn trace_made(&self) -> Option<TraceMade> {
|
||
self.extras().trace.as_deref().and_then(|t| t.get(TRACE_MADE)).and_then(TraceMade::from_json)
|
||
}
|
||
|
||
/// Records `made` as the objects the trace of this Image Trace object made, when its `trace`
|
||
/// settings are an object.
|
||
pub fn set_trace_made(&mut self, made: &TraceMade) {
|
||
if let Some(t) = self.extras_if_any().and_then(|e| e.trace.as_deref_mut()).and_then(serde_json::Value::as_object_mut) {
|
||
t.insert(TRACE_MADE.into(), made.to_json());
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn ids_round_trip_and_unknown_views_show_the_result() {
|
||
assert_eq!(TraceView::Source.id(), "sourceImage");
|
||
assert_eq!(TraceView::from_id("outlinesWithSourceImage"), Some(TraceView::OutlinesWithSource));
|
||
assert_eq!(TraceView::from_id("TRACINGRESULT"), Some(TraceView::Result), "case does not matter");
|
||
assert_eq!(TraceView::from_id("tracing"), None);
|
||
let mut g = Node::group(crate::NodeId(1), vec![]);
|
||
assert_eq!(g.trace_view(), TraceView::Result);
|
||
g.extras_mut().trace = Some(Box::new(serde_json::json!({"view": "outlines"})));
|
||
assert_eq!(g.trace_view(), TraceView::Outlines);
|
||
g.extras_mut().trace = Some(Box::new(serde_json::json!({"view": "sepia"})));
|
||
assert_eq!(g.trace_view(), TraceView::Result);
|
||
}
|
||
|
||
#[test]
|
||
fn trace_made_keeps_ranges_and_reads_only_well_formed_ones() {
|
||
let ids = [7, 3, 4, 5, 9, 3, u64::MAX].map(crate::NodeId);
|
||
let made = TraceMade::of(ids);
|
||
assert_eq!(made.to_json(), serde_json::json!([[3, 5], [7, 7], [9, 9], [u64::MAX, u64::MAX]]));
|
||
for id in 0..12 {
|
||
assert_eq!(made.contains(crate::NodeId(id)), ids.contains(&crate::NodeId(id)), "{id}");
|
||
}
|
||
assert!(made.contains(crate::NodeId(u64::MAX)));
|
||
assert_eq!(TraceMade::from_json(&made.to_json()), Some(made.clone()));
|
||
assert_eq!(TraceMade::from_json(&serde_json::json!([])), Some(TraceMade::default()));
|
||
for bad in [
|
||
serde_json::json!({}),
|
||
serde_json::json!([[5, 3]]),
|
||
serde_json::json!([[1, 4], [4, 6]]),
|
||
serde_json::json!([[6, 7], [1, 2]]),
|
||
serde_json::json!([[1]]),
|
||
serde_json::json!([[1, 2, 3]]),
|
||
serde_json::json!([[-1, 2]]),
|
||
serde_json::json!([[1.5, 2]]),
|
||
serde_json::json!(["1", "2"]),
|
||
] {
|
||
assert_eq!(TraceMade::from_json(&bad), None, "{bad}");
|
||
}
|
||
let mut g = Node::group(crate::NodeId(1), vec![]);
|
||
g.set_trace_made(&made);
|
||
assert_eq!(g.trace_made(), None, "a group with no trace settings records nothing");
|
||
g.extras_mut().trace = Some(Box::new(serde_json::json!({"preset": "Default"})));
|
||
assert_eq!(g.trace_made(), None);
|
||
g.set_trace_made(&made);
|
||
assert_eq!(g.trace_made(), Some(made));
|
||
}
|
||
|
||
#[test]
|
||
fn the_split_divider_reads_a_fraction_and_falls_back_to_the_middle() {
|
||
let mut g = Node::group(crate::NodeId(1), vec![]);
|
||
assert_eq!(g.trace_split(), DEFAULT_TRACE_SPLIT);
|
||
assert_eq!(TraceView::from_id("Split"), Some(TraceView::Split));
|
||
assert!(TraceView::Split.shows_source() && TraceView::Split.shows_result() && !TraceView::Split.shows_outlines());
|
||
for (split, want) in [
|
||
(serde_json::json!(0.25), 0.25),
|
||
(serde_json::json!(0), 0.0),
|
||
(serde_json::json!(1), 1.0),
|
||
(serde_json::json!(-0.1), DEFAULT_TRACE_SPLIT),
|
||
(serde_json::json!(1.5), DEFAULT_TRACE_SPLIT),
|
||
(serde_json::json!("0.3"), DEFAULT_TRACE_SPLIT),
|
||
(serde_json::Value::Null, DEFAULT_TRACE_SPLIT),
|
||
] {
|
||
g.extras_mut().trace = Some(Box::new(serde_json::json!({"view": "split", "split": split})));
|
||
assert_eq!(g.trace_split(), want, "{split}");
|
||
}
|
||
}
|
||
#[test]
|
||
fn live_trace_result_bounds_exclude_the_retained_bitmap_at_any_angle() {
|
||
use crate::{Appearance, Document, ImageObject, NodeId, NodeKind};
|
||
use std::sync::Arc;
|
||
use vectorcraft_geom::{Affine, PathData, Point, Rect, SubPath};
|
||
let mut source = Node::new(
|
||
NodeId(2),
|
||
NodeKind::Image(Box::new(ImageObject {
|
||
key: "synthetic".into(),
|
||
width: 200,
|
||
height: 200,
|
||
xf: Affine::IDENTITY,
|
||
link: None,
|
||
placement: Default::default(),
|
||
pixels: None,
|
||
})),
|
||
);
|
||
source.visible = false;
|
||
let art = Node::path(
|
||
NodeId(3),
|
||
PathData::single(SubPath::polyline(
|
||
&[Point::new(20.0, 30.0), Point::new(80.0, 30.0), Point::new(80.0, 90.0), Point::new(20.0, 90.0)],
|
||
true,
|
||
)),
|
||
Appearance::basic(vectorcraft_color::Paint::solid(vectorcraft_color::Color::BLACK), vectorcraft_color::Paint::None, 0.0),
|
||
);
|
||
let mut group = Node::group(NodeId(1), vec![Arc::new(source), Arc::new(art.clone())]);
|
||
group.extras_mut().trace = Some(Box::new(serde_json::json!({})));
|
||
let doc = Document::new(300.0, 300.0);
|
||
for view in TraceView::ALL {
|
||
group.extras_mut().trace = Some(Box::new(serde_json::json!({"view":view.id()})));
|
||
let want = if view.shows_source() { Rect::new(0.0, 0.0, 200.0, 200.0) } else { Rect::new(20.0, 30.0, 80.0, 90.0) };
|
||
assert_eq!(group.geometric_bounds(), Some(want));
|
||
assert_eq!(group.visual_bounds(), Some(want));
|
||
if !view.shows_source() {
|
||
for angle in [0.0_f64, 30.0, 90.0] {
|
||
let xf = Affine::rotate(angle.to_radians());
|
||
for visual in [false, true] {
|
||
assert_eq!(group.bounds_in(&doc.symbol_boxes(), xf, visual), art.bounds_in(&doc.symbol_boxes(), xf, visual));
|
||
}
|
||
}
|
||
}
|
||
}
|
||
// The Split view measures the whole image wherever the divider is, so the selection box
|
||
// stays put while the divider moves.
|
||
for split in [0.0, 0.01, 1.0] {
|
||
let want = Rect::new(0.0, 0.0, 200.0, 200.0);
|
||
group.extras_mut().trace = Some(Box::new(serde_json::json!({"view": "split", "split": split})));
|
||
assert_eq!((group.geometric_bounds(), group.visual_bounds()), (Some(want), Some(want)), "split {split}");
|
||
}
|
||
group.extras_mut().trace = None;
|
||
assert_eq!(
|
||
group.geometric_bounds(),
|
||
Some(Rect::new(0.0, 0.0, 200.0, 200.0)),
|
||
"ordinary hidden group members retain their existing bounds policy"
|
||
);
|
||
}
|
||
}
|