From c53d98a22f367a9eecfb45d1b22f1be5c6cf908d Mon Sep 17 00:00:00 2001 From: Laurenz Date: Tue, 17 Aug 2021 12:49:48 +0200 Subject: More logical ordering and naming --- src/layout/frame.rs | 102 ++++++++++++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 52 deletions(-) (limited to 'src/layout') diff --git a/src/layout/frame.rs b/src/layout/frame.rs index 6d5cc2f3..2e3b838e 100644 --- a/src/layout/frame.rs +++ b/src/layout/frame.rs @@ -19,37 +19,12 @@ pub struct Frame { children: Vec<(Point, Child)>, } -/// An iterator over all elements in a frame, alongside with their positions. -#[derive(Debug, Clone)] -pub struct ElementIter<'a> { - stack: Vec<(usize, Point, &'a Frame)>, -} - -impl<'a> Iterator for ElementIter<'a> { - type Item = (Point, &'a Element); - - /// Get the next element, if any. - fn next(&mut self) -> Option { - let (cursor, offset, frame) = self.stack.last_mut()?; - match frame.children.get(*cursor) { - Some((pos, Child::Frame(f))) => { - let new_offset = *offset + *pos; - self.stack.push((0, new_offset, f.as_ref())); - self.next() - } - Some((pos, Child::Element(e))) => { - *cursor += 1; - Some((*offset + *pos, e)) - } - None => { - self.stack.pop(); - if let Some((cursor, _, _)) = self.stack.last_mut() { - *cursor += 1; - } - self.next() - } - } - } +/// A frame can contain multiple children: elements or other frames, complete +/// with their children. +#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] +enum Child { + Element(Element), + Frame(Rc), } impl Frame { @@ -86,23 +61,46 @@ impl Frame { } } - /// Wraps the frame with constraints. + /// Wrap the frame with constraints. pub fn constrain(self, constraints: Constraints) -> Constrained> { Constrained { item: Rc::new(self), constraints } } - /// Returns an iterator over all elements in the frame and its children. - pub fn elements(&self) -> ElementIter { - ElementIter { stack: vec![(0, Point::zero(), self)] } + /// An iterator over all elements in the frame and its children. + pub fn elements(&self) -> Elements { + Elements { stack: vec![(0, Point::zero(), self)] } } } -/// A frame can contain multiple children: elements or other frames, complete -/// with their children. -#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] -enum Child { - Element(Element), - Frame(Rc), +/// 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 { + let (cursor, offset, frame) = self.stack.last_mut()?; + match frame.children.get(*cursor) { + Some((pos, Child::Frame(f))) => { + let new_offset = *offset + *pos; + self.stack.push((0, new_offset, f.as_ref())); + self.next() + } + Some((pos, Child::Element(e))) => { + *cursor += 1; + Some((*offset + *pos, e)) + } + None => { + self.stack.pop(); + if let Some((cursor, _, _)) = self.stack.last_mut() { + *cursor += 1; + } + self.next() + } + } + } } /// The building block frames are composed of. @@ -130,17 +128,6 @@ pub struct Text { pub glyphs: Vec, } -/// A glyph in a run of shaped text. -#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] -pub struct Glyph { - /// The glyph's index in the face. - pub id: u16, - /// The advance width of the glyph. - pub x_advance: Length, - /// The horizontal offset of the glyph. - pub x_offset: Length, -} - impl Text { /// Encode the glyph ids into a big-endian byte buffer. pub fn encode_glyphs_be(&self) -> Vec { @@ -153,6 +140,17 @@ impl Text { } } +/// A glyph in a run of shaped text. +#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)] +pub struct Glyph { + /// The glyph's index in the face. + pub id: u16, + /// The advance width of the glyph. + pub x_advance: Length, + /// The horizontal offset of the glyph. + pub x_offset: Length, +} + /// A geometric shape. #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub enum Geometry { -- cgit v1.2.3