diff options
| author | Laurenz <laurmaedje@gmail.com> | 2021-11-23 22:04:08 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2021-11-23 22:04:08 +0100 |
| commit | 8a88f71cb11565c1a78bd57f02a8df17cb2bf7a0 (patch) | |
| tree | 8802c1ff48e2be118e3872d25bd2f2c1f7a21b4a /src/frame.rs | |
| parent | c77c5a0f0ae6560a03a85e847006c29de9c7ae62 (diff) | |
Transformations
Diffstat (limited to 'src/frame.rs')
| -rw-r--r-- | src/frame.rs | 64 |
1 files changed, 27 insertions, 37 deletions
diff --git a/src/frame.rs b/src/frame.rs index 7862e005..5b1c36ce 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -6,7 +6,7 @@ use std::rc::Rc; use serde::{Deserialize, Serialize}; use crate::font::FaceId; -use crate::geom::{Em, Length, Paint, Path, Point, Size}; +use crate::geom::{Em, Length, Paint, Path, Point, Size, Transform}; use crate::image::ImageId; /// A finished layout with elements at fixed positions. @@ -40,8 +40,7 @@ impl Frame { /// Add a group element. pub fn push_frame(&mut self, pos: Point, frame: Rc<Self>) { - self.elements - .push((pos, Element::Group(Group { frame, clips: false }))) + self.elements.push((pos, Element::Group(Group::new(frame)))); } /// Add all elements of another frame, placing them relative to the given @@ -62,11 +61,6 @@ impl Frame { *point += offset; } } - - /// An iterator over all non-frame elements in this and nested frames. - pub fn elements(&self) -> Elements { - Elements { stack: vec![(0, Point::zero(), self)] } - } } impl Debug for Frame { @@ -87,35 +81,6 @@ impl Debug for Frame { } } -/// An iterator over all elements in a frame, alongside with their positions. -pub struct Elements<'a> { - stack: Vec<(usize, Point, &'a Frame)>, -} - -impl<'a> Iterator for Elements<'a> { - type Item = (Point, &'a Element); - - fn next(&mut self) -> Option<Self::Item> { - let (cursor, offset, frame) = self.stack.last_mut()?; - if let Some((pos, e)) = frame.elements.get(*cursor) { - if let Element::Group(g) = e { - let new_offset = *offset + *pos; - self.stack.push((0, new_offset, g.frame.as_ref())); - self.next() - } else { - *cursor += 1; - Some((*offset + *pos, e)) - } - } else { - self.stack.pop(); - if let Some((cursor, _, _)) = self.stack.last_mut() { - *cursor += 1; - } - self.next() - } - } -} - /// The building block frames are composed of. #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum Element { @@ -136,10 +101,35 @@ pub enum Element { pub struct Group { /// The group's frame. pub frame: Rc<Frame>, + /// A transformation to apply to the group. + pub transform: Transform, /// Whether the frame should be a clipping boundary. pub clips: bool, } +impl Group { + /// Create a new group with default settings. + pub fn new(frame: Rc<Frame>) -> Self { + Self { + frame, + transform: Transform::identity(), + clips: false, + } + } + + /// Set the group's transform. + pub fn transform(mut self, transform: Transform) -> Self { + self.transform = transform; + self + } + + /// Set whether the group should be a clipping boundary. + pub fn clips(mut self, clips: bool) -> Self { + self.clips = clips; + self + } +} + /// A run of shaped text. #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct Text { |
