mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Merge pull request #9994 from dotCooCoo/fix/selection-text-runs-paints-count
Select: deselecting type counts each run before taking it out of the paints
This commit is contained in:
3 files changed
+43
-4
No files matched your search
@@ -865,11 +865,11 @@ pub use plugin_call::{Location, ParamsPlace, key_location, location_in, location
|
||||
pub use select::SIMILAR;
|
||||
pub(crate) use vectorcraft_doc::range::parse_range;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use paint::PAINTS_READ;
|
||||
/// What the fill and stroke proxies read from the selected objects, kept with the selection
|
||||
/// ([`crate::doc_state::SelectionRecord`]).
|
||||
pub(crate) use paint::SelectionPaints;
|
||||
#[cfg(test)]
|
||||
pub(crate) use paint::{PAINTS_READ, WEIGHTS_READ};
|
||||
|
||||
/// A date param in Unix seconds that defaults to the clock: `None` when absent (now, see
|
||||
/// [`clock_date`]), `Some(None)` for null (no date), else `Some(Some(seconds))`.
|
||||
|
||||
@@ -398,21 +398,26 @@ pub(crate) fn painted<'a>(n: &'a Node, top: bool, stroke: bool, out: &mut Vec<&'
|
||||
thread_local! {
|
||||
/// Tests: how many fills [`SelectionPaints`] read or took out on this thread.
|
||||
pub(crate) static PAINTS_READ: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
/// Tests: how many stroke weights [`SelectionPaints`] read or took out on this thread.
|
||||
pub(crate) static WEIGHTS_READ: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
|
||||
}
|
||||
|
||||
/// The objects that reading objects `ids` meets, one item each ([`SelectionPaints::reads_no_more_than`]):
|
||||
/// each id, and the contents of a group, a layer, a Live Paint group or a container inside a group,
|
||||
/// as [`painted`] expands them. An id with no object and a ruler guide count once each.
|
||||
/// as [`painted`] expands them. An id with no object and a ruler guide count once each. A type
|
||||
/// object counts once for each of its runs, since the stroke weights are read run by run.
|
||||
struct Reads<'a> {
|
||||
doc: &'a Document,
|
||||
ids: std::slice::Iter<'a, NodeId>,
|
||||
/// The contents not counted yet of the objects met, innermost last.
|
||||
left: Vec<std::slice::Iter<'a, std::sync::Arc<Node>>>,
|
||||
/// The runs not counted yet of the type object met last.
|
||||
runs: usize,
|
||||
}
|
||||
|
||||
impl<'a> Reads<'a> {
|
||||
fn new(doc: &'a Document, ids: &'a [NodeId]) -> Self {
|
||||
Self { doc, ids: ids.iter(), left: vec![] }
|
||||
Self { doc, ids: ids.iter(), left: vec![], runs: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,6 +425,10 @@ impl Iterator for Reads<'_> {
|
||||
type Item = ();
|
||||
|
||||
fn next(&mut self) -> Option<()> {
|
||||
if self.runs > 0 {
|
||||
self.runs -= 1;
|
||||
return Some(());
|
||||
}
|
||||
loop {
|
||||
let (n, top) = match self.left.last_mut() {
|
||||
Some(contents) => match contents.next() {
|
||||
@@ -447,6 +456,9 @@ impl Iterator for Reads<'_> {
|
||||
if let Some(contents) = n.children().filter(|_| expand) {
|
||||
self.left.push(contents.iter());
|
||||
}
|
||||
if let NodeKind::Text(t) = &n.kind {
|
||||
self.runs = t.runs.len().saturating_sub(1);
|
||||
}
|
||||
return Some(());
|
||||
}
|
||||
}
|
||||
@@ -618,6 +630,8 @@ impl SelectionPaints {
|
||||
NodeKind::Text(t) => t.runs.iter().map(|r| r.style.stroke_layer().width).collect(),
|
||||
_ => n.appearance.stroke().map(|s| s.width).into_iter().collect(),
|
||||
};
|
||||
#[cfg(test)]
|
||||
WEIGHTS_READ.with(|c| c.set(c.get() + widths.len()));
|
||||
for w in widths {
|
||||
if add {
|
||||
self.weight.add(w, |a, b| a == b);
|
||||
|
||||
@@ -788,3 +788,28 @@ fn deselecting_a_small_group_beside_a_large_one_takes_out_only_its_objects() {
|
||||
let fills = read() - before;
|
||||
assert!(fills <= 100, "read or took out {fills} fills");
|
||||
}
|
||||
|
||||
/// Taking a type object of 10,000 runs out of a selection that keeps two squares reads the two
|
||||
/// squares' stroke weights again instead of taking the 10,000 run weights out of them.
|
||||
#[test]
|
||||
fn deselecting_type_of_many_runs_reads_the_objects_left() {
|
||||
let mut s = session(3, |d, k| match k {
|
||||
0 => {
|
||||
let style = vectorcraft_doc::CharStyle::default();
|
||||
let mut t = vectorcraft_doc::TextObject::point(vectorcraft_geom::Point::new(10.0, 40.0), "x", style.clone());
|
||||
t.runs = (0..10_000).map(|_| vectorcraft_doc::TextRun::new("x", style.clone())).collect();
|
||||
Node::new(d.alloc_id(), NodeKind::Text(Box::new(t)))
|
||||
}
|
||||
_ => square(d, k),
|
||||
});
|
||||
let ids = layer(&s, 0);
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
s.execute("select.add", &json!({"ids": ids.iter().map(|id| id.0).collect::<Vec<_>>()})).unwrap();
|
||||
assert!(selected(&s, &ids));
|
||||
let read = || crate::cmd::WEIGHTS_READ.with(std::cell::Cell::get);
|
||||
let before = read();
|
||||
s.execute("select.toggle", &json!({"id": ids[0].0})).unwrap();
|
||||
assert!(selected(&s, &ids[1..]));
|
||||
let weights = read() - before;
|
||||
assert!(weights <= 100, "read or took out {weights} stroke weights");
|
||||
}
|
||||
Reference in new issue
Block a user