mirror of
https://github.com/storytold/figcraft.git
synced 2026-10-26 11:26:50 -06:00
Merge pull request #1282 from Silvertree2010/fix/h1985-distribute-spacing-measures-the
Measure distribute span to the group's outermost edge
This commit is contained in:
@@ -206,8 +206,9 @@ fn distribute(s: &mut Session, p: &Value, horizontal: bool) -> Result<Value> {
|
||||
let mut items: Vec<(NodeId, Rect)> = ids.iter().filter_map(|i| doc.world_bounds(*i).map(|b| (*i, b))).collect();
|
||||
items.sort_by(|a, b| if horizontal { a.1.x0.total_cmp(&b.1.x0) } else { a.1.y0.total_cmp(&b.1.y0) });
|
||||
let total: f64 = items.iter().map(|(_, b)| if horizontal { b.width() } else { b.height() }).sum();
|
||||
let (Some(first), Some(last)) = (items.first(), items.last()) else { return ok() };
|
||||
let span = if horizontal { last.1.x1 - first.1.x0 } else { last.1.y1 - first.1.y0 };
|
||||
let Some(first) = items.first() else { return ok() };
|
||||
let end = items.iter().map(|(_, b)| if horizontal { b.x1 } else { b.y1 }).fold(f64::MIN, f64::max);
|
||||
let span = if horizontal { end - first.1.x0 } else { end - first.1.y0 };
|
||||
let gap = num(p, "spacing").unwrap_or((span - total) / (items.len().saturating_sub(1).max(1)) as f64);
|
||||
let mut pos = if horizontal { first.1.x0 } else { first.1.y0 };
|
||||
for (i, b) in items.clone() {
|
||||
|
||||
@@ -275,6 +275,20 @@ fn align_distribute_zorder() {
|
||||
assert_eq!(s.doc().unwrap().node(page).unwrap().children[0], c);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn distribute_spans_the_outermost_edge() {
|
||||
let mut s = sess();
|
||||
let a = id(&s.execute("shape.rectangle", &json!({"x": 0, "y": 0, "width": 10, "height": 10})).unwrap());
|
||||
let b = id(&s.execute("shape.rectangle", &json!({"x": 150, "y": 150, "width": 500, "height": 500})).unwrap());
|
||||
let c = id(&s.execute("shape.rectangle", &json!({"x": 160, "y": 160, "width": 10, "height": 10})).unwrap());
|
||||
s.execute("distribute.horizontal", &json!({"ids": [a, b, c]})).unwrap();
|
||||
let xs: Vec<f64> = [a, b, c].iter().map(|i| s.doc().unwrap().world_bounds(*i).unwrap().x0).collect();
|
||||
assert_eq!(xs, [0.0, 75.0, 640.0]);
|
||||
s.execute("distribute.vertical", &json!({"ids": [a, b, c]})).unwrap();
|
||||
let ys: Vec<f64> = [a, b, c].iter().map(|i| s.doc().unwrap().world_bounds(*i).unwrap().y0).collect();
|
||||
assert_eq!(ys, [0.0, 75.0, 640.0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_layer_alone_on_the_page_has_nothing_to_align_to() {
|
||||
use crate::cmd::arrange::can_align;
|
||||
|
||||
Reference in New Issue
Block a user