summaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-08-17 12:49:48 +0200
committerLaurenz <laurmaedje@gmail.com>2021-08-17 12:49:48 +0200
commitc53d98a22f367a9eecfb45d1b22f1be5c6cf908d (patch)
treeac319fa1b53d659bc021f5ebfd85b868f982a922 /src/layout
parent9a798ce6f6e734a02764473891632c071fed41ee (diff)
More logical ordering and naming
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/frame.rs102
1 files changed, 50 insertions, 52 deletions
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<Self::Item> {
- 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<Frame>),
}
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<Rc<Self>> {
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<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()?;
+ 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<Glyph>,
}
-/// 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<u8> {
@@ -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 {