Skip to content

Commit 3a14b38

Browse files
AyeTbkKeavon
authored andcommitted
Add a document method that returns a layer's bounding box (#273)
* Add a document method that returns a layer's bounding box * Moved `use` to the top of the file * Add layer local bounding box method
1 parent 67131a2 commit 3a14b38

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

core/document/src/document.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ impl Document {
227227
Ok(())
228228
}
229229

230+
pub fn layer_axis_aligned_bounding_box(&self, path: &[LayerId]) -> Result<[DVec2; 2], DocumentError> {
231+
let layer = self.layer(path)?;
232+
Ok(layer.bounding_box(self.root.transform * layer.transform, layer.style))
233+
}
234+
235+
pub fn layer_local_bounding_box(&self, path: &[LayerId]) -> Result<[DVec2; 2], DocumentError> {
236+
let layer = self.layer(path)?;
237+
Ok(layer.bounding_box(layer.transform, layer.style))
238+
}
239+
230240
fn mark_as_dirty(&mut self, path: &[LayerId]) -> Result<(), DocumentError> {
231241
let mut root = &mut self.root;
232242
root.cache_dirty = true;

core/document/src/layers/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub use ellipse::Ellipse;
66
pub mod line;
77
use glam::{DMat2, DVec2};
88
use kurbo::BezPath;
9+
use kurbo::Shape as KurboShape;
910
pub use line::Line;
1011

1112
pub mod rect;
@@ -100,6 +101,12 @@ impl LayerDataTypes {
100101
}
101102
}
102103
}
104+
105+
pub fn bounding_box(&self, transform: glam::DAffine2, style: style::PathStyle) -> [DVec2; 2] {
106+
let bez_path = self.to_kurbo_path(transform, style);
107+
let bbox = bez_path.bounding_box();
108+
[DVec2::new(bbox.x0, bbox.y0), DVec2::new(bbox.x1, bbox.y1)]
109+
}
103110
}
104111

105112
#[derive(Serialize, Deserialize)]
@@ -168,6 +175,10 @@ impl Layer {
168175
self.data.to_kurbo_path(self.transform, self.style)
169176
}
170177

178+
pub fn bounding_box(&self, transform: glam::DAffine2, style: style::PathStyle) -> [DVec2; 2] {
179+
self.data.bounding_box(transform, style)
180+
}
181+
171182
pub fn as_folder_mut(&mut self) -> Result<&mut Folder, DocumentError> {
172183
match &mut self.data {
173184
LayerDataTypes::Folder(f) => Ok(f),

0 commit comments

Comments
 (0)