summaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/fixed.rs12
-rw-r--r--src/layout/mod.rs197
-rw-r--r--src/layout/node.rs52
-rw-r--r--src/layout/pad.rs34
-rw-r--r--src/layout/par.rs84
-rw-r--r--src/layout/spacing.rs10
-rw-r--r--src/layout/stack.rs72
-rw-r--r--src/layout/text.rs16
8 files changed, 232 insertions, 245 deletions
diff --git a/src/layout/fixed.rs b/src/layout/fixed.rs
index d0daa2ca..2ec46df3 100644
--- a/src/layout/fixed.rs
+++ b/src/layout/fixed.rs
@@ -3,16 +3,16 @@ use crate::geom::Linear;
/// A node that can fix its child's width and height.
#[derive(Debug, Clone, PartialEq)]
-pub struct Fixed {
+pub struct NodeFixed {
/// The fixed width, if any.
pub width: Option<Linear>,
/// The fixed height, if any.
pub height: Option<Linear>,
/// The child node whose size to fix.
- pub child: LayoutNode,
+ pub child: Node,
}
-impl Layout for Fixed {
+impl Layout for NodeFixed {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
let Area { rem, full } = areas.current;
let size = Size::new(
@@ -25,8 +25,8 @@ impl Layout for Fixed {
}
}
-impl From<Fixed> for LayoutNode {
- fn from(fixed: Fixed) -> Self {
- Self::dynamic(fixed)
+impl From<NodeFixed> for Node {
+ fn from(fixed: NodeFixed) -> Self {
+ Self::any(fixed)
}
}
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index 44a2c2fa..d09566e3 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -1,4 +1,4 @@
-//! Layouting of documents.
+//! Layouting.
mod fixed;
mod node;
@@ -20,10 +20,48 @@ pub use spacing::*;
pub use stack::*;
pub use text::*;
-/// Layout a document and return the produced layouts.
-pub fn layout(document: &Document, env: SharedEnv) -> Vec<BoxLayout> {
- let mut ctx = LayoutContext { env };
- document.layout(&mut ctx)
+/// Layout a tree into a collection of frames.
+pub fn layout(tree: &Tree, env: SharedEnv) -> Vec<Frame> {
+ tree.layout(&mut LayoutContext { env })
+}
+
+/// A tree of layout nodes.
+#[derive(Debug, Clone, PartialEq)]
+pub struct Tree {
+ /// Runs of pages with the same properties.
+ pub runs: Vec<NodePages>,
+}
+
+impl Tree {
+ /// Layout the tree into a collection of frames.
+ pub fn layout(&self, ctx: &mut LayoutContext) -> Vec<Frame> {
+ self.runs.iter().flat_map(|run| run.layout(ctx)).collect()
+ }
+}
+
+/// A run of pages that all have the same properties.
+#[derive(Debug, Clone, PartialEq)]
+pub struct NodePages {
+ /// The size of each page.
+ pub size: Size,
+ /// The layout node that produces the actual pages (typically a
+ /// [`NodeStack`]).
+ pub child: Node,
+}
+
+impl NodePages {
+ /// Layout the page run.
+ pub fn layout(&self, ctx: &mut LayoutContext) -> Vec<Frame> {
+ let areas = Areas::repeat(self.size);
+ let layouted = self.child.layout(ctx, &areas);
+ layouted.frames()
+ }
+}
+
+/// Layout a node.
+pub trait Layout {
+ /// Layout the node into the given areas.
+ fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted;
}
/// The context for layouting.
@@ -33,22 +71,30 @@ pub struct LayoutContext {
pub env: SharedEnv,
}
-/// Layout a node.
-pub trait Layout {
- /// Layout the node into the given areas.
- fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted;
+/// An area into which content can be laid out.
+#[derive(Debug, Copy, Clone, PartialEq)]
+pub struct Area {
+ /// The remaining size of this area.
+ pub rem: Size,
+ /// The full size this area once had (used for relative sizing).
+ pub full: Size,
}
-/// A sequence of areas to layout into.
+impl Area {
+ /// Create a new area.
+ pub fn new(size: Size) -> Self {
+ Self { rem: size, full: size }
+ }
+}
+
+/// A collection of areas to layout into.
#[derive(Debug, Clone, PartialEq)]
pub struct Areas {
/// The current area.
pub current: Area,
- /// The backlog of followup areas.
- ///
- /// _Note_: This works stack-like and not queue-like!
+ /// A stack of followup areas (the next area is the last element).
pub backlog: Vec<Size>,
- /// The last area that is repeated when the backlog is empty.
+ /// The final area that is repeated when the backlog is empty.
pub last: Option<Size>,
}
@@ -86,136 +132,81 @@ impl Areas {
}
}
-/// The area into which content can be laid out.
-#[derive(Debug, Copy, Clone, PartialEq)]
-pub struct Area {
- /// The remaining size of this area.
- pub rem: Size,
- /// The full size this area once had (used for relative sizing).
- pub full: Size,
-}
-
-impl Area {
- /// Create a new area.
- pub fn new(size: Size) -> Self {
- Self { rem: size, full: size }
- }
-}
-
-/// How to determine a container's size along an axis.
-#[derive(Debug, Copy, Clone, Eq, PartialEq)]
-pub enum Expansion {
- /// Fit the content.
- Fit,
- /// Fill the available space.
- Fill,
-}
-
-impl Expansion {
- /// Returns `Fill` if the condition is true and `Fit` otherwise.
- pub fn fill_if(condition: bool) -> Self {
- if condition { Self::Fill } else { Self::Fit }
- }
-}
-
-/// The result of [layouting](Layout::layout) a node.
+/// The result of layouting a node.
#[derive(Debug, Clone, PartialEq)]
pub enum Layouted {
/// Spacing that should be added to the parent.
Spacing(Length),
/// A layout that should be added to and aligned in the parent.
- Layout(BoxLayout, BoxAlign),
+ Frame(Frame, ChildAlign),
/// Multiple layouts.
- Layouts(Vec<BoxLayout>, BoxAlign),
+ Frames(Vec<Frame>, ChildAlign),
}
impl Layouted {
- /// Return all layouts contained in this variant (zero, one or arbitrarily
+ /// Return all frames contained in this variant (zero, one or arbitrarily
/// many).
- pub fn into_layouts(self) -> Vec<BoxLayout> {
+ pub fn frames(self) -> Vec<Frame> {
match self {
Self::Spacing(_) => vec![],
- Self::Layout(layout, _) => vec![layout],
- Self::Layouts(layouts, _) => layouts,
+ Self::Frame(frame, _) => vec![frame],
+ Self::Frames(frames, _) => frames,
}
}
}
-/// A finished box with content at fixed positions.
+/// Whether to expand or shrink a node along an axis.
+#[derive(Debug, Copy, Clone, Eq, PartialEq)]
+pub enum Expansion {
+ /// Fit the content.
+ Fit,
+ /// Fill the available space.
+ Fill,
+}
+
+/// A finished layout with elements at fixed positions.
#[derive(Debug, Clone, PartialEq)]
-pub struct BoxLayout {
- /// The size of the box.
+pub struct Frame {
+ /// The size of the frame.
pub size: Size,
/// The elements composing this layout.
- pub elements: Vec<(Point, LayoutElement)>,
+ pub elements: Vec<(Point, Element)>,
}
-impl BoxLayout {
- /// Create a new empty collection.
+impl Frame {
+ /// Create a new, empty frame.
pub fn new(size: Size) -> Self {
Self { size, elements: vec![] }
}
/// Add an element at a position.
- pub fn push(&mut self, pos: Point, element: LayoutElement) {
+ pub fn push(&mut self, pos: Point, element: Element) {
self.elements.push((pos, element));
}
- /// Add all elements of another collection, placing them relative to the
- /// given position.
- pub fn push_layout(&mut self, pos: Point, more: Self) {
- for (subpos, element) in more.elements {
+ /// Add all elements of another frame, placing them relative to the given
+ /// position.
+ pub fn push_frame(&mut self, pos: Point, subframe: Self) {
+ for (subpos, element) in subframe.elements {
self.push(pos + subpos, element);
}
}
}
-/// A layout element, the basic building block layouts are composed of.
+/// The building block frames are composed of.
#[derive(Debug, Clone, PartialEq)]
-pub enum LayoutElement {
+pub enum Element {
/// Shaped text.
Text(Shaped),
/// An image.
- Image(ImageElement),
+ Image(Image),
}
-/// An image.
+/// An image element.
#[derive(Debug, Clone, PartialEq)]
-pub struct ImageElement {
- /// The image.
+pub struct Image {
+ /// The image resource.
pub res: ResourceId,
- /// The document size of the image.
+ /// The size of the image in the document.
pub size: Size,
}
-
-/// The top-level layout node.
-#[derive(Debug, Clone, PartialEq)]
-pub struct Document {
- /// The runs of pages with same properties.
- pub runs: Vec<Pages>,
-}
-
-impl Document {
- /// Layout the document.
- pub fn layout(&self, ctx: &mut LayoutContext) -> Vec<BoxLayout> {
- self.runs.iter().flat_map(|run| run.layout(ctx)).collect()
- }
-}
-
-/// A variable-length run of pages that all have the same properties.
-#[derive(Debug, Clone, PartialEq)]
-pub struct Pages {
- /// The size of each page.
- pub size: Size,
- /// The layout node that produces the actual pages (typically a [`Stack`]).
- pub child: LayoutNode,
-}
-
-impl Pages {
- /// Layout the page run.
- pub fn layout(&self, ctx: &mut LayoutContext) -> Vec<BoxLayout> {
- let areas = Areas::repeat(self.size);
- let layouted = self.child.layout(ctx, &areas);
- layouted.into_layouts()
- }
-}
diff --git a/src/layout/node.rs b/src/layout/node.rs
index 44767898..c945ee19 100644
--- a/src/layout/node.rs
+++ b/src/layout/node.rs
@@ -1,91 +1,89 @@
-//! Layout nodes.
-
use std::any::Any;
use std::fmt::{self, Debug, Formatter};
use super::*;
-/// A self-contained, styled layout node.
+/// A self-contained layout node.
#[derive(Clone, PartialEq)]
-pub enum LayoutNode {
- /// A spacing node.
- Spacing(Spacing),
+pub enum Node {
/// A text node.
- Text(Text),
- /// A dynamic that can implement custom layouting behaviour.
- Dyn(Dynamic),
+ Text(NodeText),
+ /// A spacing node.
+ Spacing(NodeSpacing),
+ /// A dynamic node that can implement custom layouting behaviour.
+ Any(NodeAny),
}
-impl LayoutNode {
+impl Node {
/// Create a new dynamic node.
- pub fn dynamic<T>(inner: T) -> Self
+ pub fn any<T>(any: T) -> Self
where
T: Layout + Debug + Clone + PartialEq + 'static,
{
- Self::Dyn(Dynamic::new(inner))
+ Self::Any(NodeAny::new(any))
}
}
-impl Layout for LayoutNode {
+impl Layout for Node {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
match self {
Self::Spacing(spacing) => spacing.layout(ctx, areas),
Self::Text(text) => text.layout(ctx, areas),
- Self::Dyn(dynamic) => dynamic.layout(ctx, areas),
+ Self::Any(any) => any.layout(ctx, areas),
}
}
}
-impl Debug for LayoutNode {
+impl Debug for Node {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::Spacing(spacing) => spacing.fmt(f),
Self::Text(text) => text.fmt(f),
- Self::Dyn(dynamic) => dynamic.fmt(f),
+ Self::Any(any) => any.fmt(f),
}
}
}
/// A wrapper around a dynamic layouting node.
-pub struct Dynamic(Box<dyn Bounds>);
+pub struct NodeAny(Box<dyn Bounds>);
-impl Dynamic {
+impl NodeAny {
/// Create a new instance from any node that satisifies the required bounds.
- pub fn new<T>(inner: T) -> Self
+ pub fn new<T>(any: T) -> Self
where
T: Layout + Debug + Clone + PartialEq + 'static,
{
- Self(Box::new(inner))
+ Self(Box::new(any))
}
}
-impl Layout for Dynamic {
+impl Layout for NodeAny {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
self.0.layout(ctx, areas)
}
}
-impl Clone for Dynamic {
+impl Clone for NodeAny {
fn clone(&self) -> Self {
Self(self.0.dyn_clone())
}
}
-impl PartialEq for Dynamic {
+impl PartialEq for NodeAny {
fn eq(&self, other: &Self) -> bool {
self.0.dyn_eq(other.0.as_ref())
}
}
-impl Debug for Dynamic {
+impl Debug for NodeAny {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
-impl From<Dynamic> for LayoutNode {
- fn from(dynamic: Dynamic) -> Self {
- Self::Dyn(dynamic)
+impl From<NodeAny> for Node {
+ fn from(dynamic: NodeAny) -> Self {
+ Self::Any(dynamic)
}
}
diff --git a/src/layout/pad.rs b/src/layout/pad.rs
index 00830a07..f947a7f5 100644
--- a/src/layout/pad.rs
+++ b/src/layout/pad.rs
@@ -1,26 +1,26 @@
use super::*;
use crate::geom::Linear;
-/// A node that pads its child at the sides.
+/// A node that adds padding to its child.
#[derive(Debug, Clone, PartialEq)]
-pub struct Pad {
+pub struct NodePad {
/// The amount of padding.
pub padding: Sides<Linear>,
/// The child node whose sides to pad.
- pub child: LayoutNode,
+ pub child: Node,
}
-impl Layout for Pad {
+impl Layout for NodePad {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
- let areas = shrink_areas(areas, self.padding);
+ let areas = shrink(areas, self.padding);
let mut layouted = self.child.layout(ctx, &areas);
match &mut layouted {
Layouted::Spacing(_) => {}
- Layouted::Layout(layout, _) => pad_layout(layout, self.padding),
- Layouted::Layouts(layouts, _) => {
- for layout in layouts {
- pad_layout(layout, self.padding);
+ Layouted::Frame(frame, _) => pad(frame, self.padding),
+ Layouted::Frames(frames, _) => {
+ for frame in frames {
+ pad(frame, self.padding);
}
}
}
@@ -29,14 +29,14 @@ impl Layout for Pad {
}
}
-impl From<Pad> for LayoutNode {
- fn from(pad: Pad) -> Self {
- Self::dynamic(pad)
+impl From<NodePad> for Node {
+ fn from(pad: NodePad) -> Self {
+ Self::any(pad)
}
}
/// Shrink all areas by the padding.
-fn shrink_areas(areas: &Areas, padding: Sides<Linear>) -> Areas {
+fn shrink(areas: &Areas, padding: Sides<Linear>) -> Areas {
let shrink = |size| size - padding.resolve(size).size();
Areas {
current: Area {
@@ -49,12 +49,12 @@ fn shrink_areas(areas: &Areas, padding: Sides<Linear>) -> Areas {
}
/// Enlarge the box and move all elements inwards.
-fn pad_layout(layout: &mut BoxLayout, padding: Sides<Linear>) {
- let padding = padding.resolve(layout.size);
+fn pad(frame: &mut Frame, padding: Sides<Linear>) {
+ let padding = padding.resolve(frame.size);
let origin = Point::new(padding.left, padding.top);
- layout.size += padding.size();
- for (point, _) in &mut layout.elements {
+ frame.size += padding.size();
+ for (point, _) in &mut frame.elements {
*point += origin;
}
}
diff --git a/src/layout/par.rs b/src/layout/par.rs
index 723f27cb..3a8e3969 100644
--- a/src/layout/par.rs
+++ b/src/layout/par.rs
@@ -2,69 +2,67 @@ use super::*;
/// A node that arranges its children into a paragraph.
#[derive(Debug, Clone, PartialEq)]
-pub struct Par {
+pub struct NodePar {
/// The `main` and `cross` directions of this paragraph.
///
/// The children are placed in lines along the `cross` direction. The lines
/// are stacked along the `main` direction.
- pub flow: Flow,
+ pub dirs: LayoutDirs,
/// Whether to expand the cross axis to fill the area or to fit the content.
pub cross_expansion: Expansion,
/// The spacing to insert after each line.
pub line_spacing: Length,
/// The nodes to be arranged in a paragraph.
- pub children: Vec<LayoutNode>,
+ pub children: Vec<Node>,
/// How to align this paragraph in _its_ parent.
- pub align: BoxAlign,
+ pub align: ChildAlign,
}
-impl Layout for Par {
+impl Layout for NodePar {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
let mut layouter = ParLayouter::new(self, areas.clone());
for child in &self.children {
match child.layout(ctx, &layouter.areas) {
Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
- Layouted::Layout(layout, align) => {
- layouter.push_layout(layout, align.cross)
- }
- Layouted::Layouts(layouts, align) => {
- for layout in layouts {
- layouter.push_layout(layout, align.cross);
+ Layouted::Frame(frame, align) => layouter.push_frame(frame, align.cross),
+ Layouted::Frames(frames, align) => {
+ for frame in frames {
+ layouter.push_frame(frame, align.cross);
}
}
}
}
- Layouted::Layouts(layouter.finish(), self.align)
+ Layouted::Frames(layouter.finish(), self.align)
}
}
-impl From<Par> for LayoutNode {
- fn from(par: Par) -> Self {
- Self::dynamic(par)
+impl From<NodePar> for Node {
+ fn from(par: NodePar) -> Self {
+ Self::any(par)
}
}
struct ParLayouter<'a> {
- par: &'a Par,
+ par: &'a NodePar,
main: SpecAxis,
cross: SpecAxis,
- flow: Flow,
+ dirs: LayoutDirs,
areas: Areas,
- finished: Vec<BoxLayout>,
- lines: Vec<(Length, BoxLayout, Align)>,
+ finished: Vec<Frame>,
+ lines: Vec<(Length, Frame, Align)>,
lines_size: Gen<Length>,
- run: Vec<(Length, BoxLayout, Align)>,
+ run: Vec<(Length, Frame, Align)>,
run_size: Gen<Length>,
run_ruler: Align,
}
impl<'a> ParLayouter<'a> {
- fn new(par: &'a Par, areas: Areas) -> Self {
+ fn new(par: &'a NodePar, areas: Areas) -> Self {
Self {
par,
- main: par.flow.main.axis(),
- cross: par.flow.cross.axis(),
- flow: par.flow,
+ main: par.dirs.main.axis(),
+ cross: par.dirs.cross.axis(),
+ dirs: par.dirs,
areas,
finished: vec![],
lines: vec![],
@@ -80,7 +78,7 @@ impl<'a> ParLayouter<'a> {
self.run_size.cross = (self.run_size.cross + amount).min(cross_max);
}
- fn push_layout(&mut self, layout: BoxLayout, align: Align) {
+ fn push_frame(&mut self, frame: Frame, align: Align) {
if self.run_ruler > align {
self.finish_run();
}
@@ -88,16 +86,16 @@ impl<'a> ParLayouter<'a> {
let fits = {
let mut usable = self.areas.current.rem;
*usable.get_mut(self.cross) -= self.run_size.cross;
- usable.fits(layout.size)
+ usable.fits(frame.size)
};
if !fits {
self.finish_run();
- while !self.areas.current.rem.fits(layout.size) {
+ while !self.areas.current.rem.fits(frame.size) {
if self.areas.in_full_last() {
// TODO: Diagnose once the necessary spans exist.
- let _ = warning!("cannot fit box into any area");
+ let _ = warning!("cannot fit frame into any area");
break;
} else {
self.finish_area();
@@ -105,8 +103,8 @@ impl<'a> ParLayouter<'a> {
}
}
- let size = layout.size.switch(self.flow);
- self.run.push((self.run_size.cross, layout, align));
+ let size = frame.size.switch(self.dirs);
+ self.run.push((self.run_size.cross, frame, align));
self.run_size.cross += size.cross;
self.run_size.main = self.run_size.main.max(size.main);
@@ -119,13 +117,13 @@ impl<'a> ParLayouter<'a> {
Expansion::Fit => self.run_size.cross,
});
- let mut output = BoxLayout::new(full_size.switch(self.flow).to_size());
+ let mut output = Frame::new(full_size.switch(self.dirs).to_size());
- for (before, layout, align) in std::mem::take(&mut self.run) {
- let child_cross_size = layout.size.get(self.cross);
+ for (before, frame, align) in std::mem::take(&mut self.run) {
+ let child_cross_size = frame.size.get(self.cross);
// Position along the cross axis.
- let cross = align.resolve(if self.flow.cross.is_positive() {
+ let cross = align.resolve(if self.dirs.cross.is_positive() {
let after_with_self = self.run_size.cross - before;
before .. full_size.cross - after_with_self
} else {
@@ -134,8 +132,8 @@ impl<'a> ParLayouter<'a> {
full_size.cross - before_with_self .. after
});
- let pos = Gen::new(Length::ZERO, cross).switch(self.flow).to_point();
- output.push_layout(pos, layout);
+ let pos = Gen::new(Length::ZERO, cross).switch(self.dirs).to_point();
+ output.push_frame(pos, frame);
}
self.lines.push((self.lines_size.main, output, self.run_ruler));
@@ -151,27 +149,27 @@ impl<'a> ParLayouter<'a> {
fn finish_area(&mut self) {
let size = self.lines_size;
- let mut output = BoxLayout::new(size.switch(self.flow).to_size());
+ let mut output = Frame::new(size.switch(self.dirs).to_size());
for (before, run, cross_align) in std::mem::take(&mut self.lines) {
- let child_size = run.size.switch(self.flow);
+ let child_size = run.size.switch(self.dirs);
// Position along the main axis.
- let main = if self.flow.main.is_positive() {
+ let main = if self.dirs.main.is_positive() {
before
} else {
size.main - (before + child_size.main)
};
// Align along the cross axis.
- let cross = cross_align.resolve(if self.flow.cross.is_positive() {
+ let cross = cross_align.resolve(if self.dirs.cross.is_positive() {
Length::ZERO .. size.cross - child_size.cross
} else {
size.cross - child_size.cross .. Length::ZERO
});
- let pos = Gen::new(main, cross).switch(self.flow).to_point();
- output.push_layout(pos, run);
+ let pos = Gen::new(main, cross).switch(self.dirs).to_point();
+ output.push_frame(pos, run);
}
self.finished.push(output);
@@ -180,7 +178,7 @@ impl<'a> ParLayouter<'a> {
self.lines_size = Gen::ZERO;
}
- fn finish(mut self) -> Vec<BoxLayout> {
+ fn finish(mut self) -> Vec<Frame> {
self.finish_run();
self.finish_area();
self.finished
diff --git a/src/layout/spacing.rs b/src/layout/spacing.rs
index c9a9c233..f0024fab 100644
--- a/src/layout/spacing.rs
+++ b/src/layout/spacing.rs
@@ -5,7 +5,7 @@ use crate::eval::Softness;
/// A spacing node.
#[derive(Copy, Clone, PartialEq)]
-pub struct Spacing {
+pub struct NodeSpacing {
/// The amount of spacing to insert.
pub amount: Length,
/// Defines how spacing interacts with surrounding spacing.
@@ -19,13 +19,13 @@ pub struct Spacing {
pub softness: Softness,
}
-impl Layout for Spacing {
+impl Layout for NodeSpacing {
fn layout(&self, _: &mut LayoutContext, _: &Areas) -> Layouted {
Layouted::Spacing(self.amount)
}
}
-impl Debug for Spacing {
+impl Debug for NodeSpacing {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self.softness {
Softness::Soft => write!(f, "Soft({})", self.amount),
@@ -34,8 +34,8 @@ impl Debug for Spacing {
}
}
-impl From<Spacing> for LayoutNode {
- fn from(spacing: Spacing) -> Self {
+impl From<NodeSpacing> for Node {
+ fn from(spacing: NodeSpacing) -> Self {
Self::Spacing(spacing)
}
}
diff --git a/src/layout/stack.rs b/src/layout/stack.rs
index 9d2540e9..e98be7ed 100644
--- a/src/layout/stack.rs
+++ b/src/layout/stack.rs
@@ -1,65 +1,65 @@
use super::*;
-/// A node that stacks and align its children.
+/// A node that stacks its children.
#[derive(Debug, Clone, PartialEq)]
-pub struct Stack {
+pub struct NodeStack {
/// The `main` and `cross` directions of this stack.
///
/// The children are stacked along the `main` direction. The `cross`
/// direction is required for aligning the children.
- pub flow: Flow,
+ pub dirs: LayoutDirs,
/// How to align this stack in _its_ parent.
- pub align: BoxAlign,
+ pub align: ChildAlign,
/// Whether to expand the axes to fill the area or to fit the content.
pub expansion: Gen<Expansion>,
/// The nodes to be stacked.
- pub children: Vec<LayoutNode>,
+ pub children: Vec<Node>,
}
-impl Layout for Stack {
+impl Layout for NodeStack {
fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
let mut layouter = StackLayouter::new(self, areas.clone());
for child in &self.children {
match child.layout(ctx, &layouter.areas) {
Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
- Layouted::Layout(layout, align) => layouter.push_layout(layout, align),
- Layouted::Layouts(layouts, align) => {
- for layout in layouts {
- layouter.push_layout(layout, align);
+ Layouted::Frame(frame, align) => layouter.push_frame(frame, align),
+ Layouted::Frames(frames, align) => {
+ for frame in frames {
+ layouter.push_frame(frame, align);
}
}
}
}
- Layouted::Layouts(layouter.finish(), self.align)
+ Layouted::Frames(layouter.finish(), self.align)
}
}
-impl From<Stack> for LayoutNode {
- fn from(stack: Stack) -> Self {
- Self::dynamic(stack)
+impl From<NodeStack> for Node {
+ fn from(stack: NodeStack) -> Self {
+ Self::any(stack)
}
}
struct StackLayouter<'a> {
- stack: &'a Stack,
+ stack: &'a NodeStack,
main: SpecAxis,
- flow: Flow,
+ dirs: LayoutDirs,
areas: Areas,
- finished: Vec<BoxLayout>,
- layouts: Vec<(Length, BoxLayout, BoxAlign)>,
+ finished: Vec<Frame>,
+ frames: Vec<(Length, Frame, ChildAlign)>,
used: Gen<Length>,
ruler: Align,
}
impl<'a> StackLayouter<'a> {
- fn new(stack: &'a Stack, areas: Areas) -> Self {
+ fn new(stack: &'a NodeStack, areas: Areas) -> Self {
Self {
stack,
- main: stack.flow.main.axis(),
- flow: stack.flow,
+ main: stack.dirs.main.axis(),
+ dirs: stack.dirs,
areas,
finished: vec![],
- layouts: vec![],
+ frames: vec![],
used: Gen::ZERO,
ruler: Align::Start,
}
@@ -72,23 +72,23 @@ impl<'a> StackLayouter<'a> {
self.used.main += capped;
}
- fn push_layout(&mut self, layout: BoxLayout, align: BoxAlign) {
+ fn push_frame(&mut self, frame: Frame, align: ChildAlign) {
if self.ruler > align.main {
self.finish_area();
}
- while !self.areas.current.rem.fits(layout.size) {
+ while !self.areas.current.rem.fits(frame.size) {
if self.areas.in_full_last() {
// TODO: Diagnose once the necessary spans exist.
- let _ = warning!("cannot fit box into any area");
+ let _ = warning!("cannot fit frame into any area");
break;
} else {
self.finish_area();
}
}
- let size = layout.size.switch(self.flow);
- self.layouts.push((self.used.main, layout, align));
+ let size = frame.size.switch(self.dirs);
+ self.frames.push((self.used.main, frame, align));
*self.areas.current.rem.get_mut(self.main) -= size.main;
self.used.main += size.main;
@@ -98,7 +98,7 @@ impl<'a> StackLayouter<'a> {
fn finish_area(&mut self) {
let full_size = {
- let full = self.areas.current.full.switch(self.flow);
+ let full = self.areas.current.full.switch(self.dirs);
Gen::new(
match self.stack.expansion.main {
Expansion::Fill => full.main,
@@ -111,13 +111,13 @@ impl<'a> StackLayouter<'a> {
)
};
- let mut output = BoxLayout::new(full_size.switch(self.flow).to_size());
+ let mut output = Frame::new(full_size.switch(self.dirs).to_size());
- for (before, layout, align) in std::mem::take(&mut self.layouts) {
- let child_size = layout.size.switch(self.flow);
+ for (before, frame, align) in std::mem::take(&mut self.frames) {
+ let child_size = frame.size.switch(self.dirs);
// Align along the main axis.
- let main = align.main.resolve(if self.flow.main.is_positive() {
+ let main = align.main.resolve(if self.dirs.main.is_positive() {
let after_with_self = self.used.main - before;
before .. full_size.main - after_with_self
} else {
@@ -127,14 +127,14 @@ impl<'a> StackLayouter<'a> {
});
// Align along the cross axis.
- let cross = align.cross.resolve(if self.flow.cross.is_positive() {
+ let cross = align.cross.resolve(if self.dirs.cross.is_positive() {
Length::ZERO .. full_size.cross - child_size.cross
} else {
full_size.cross - child_size.cross .. Length::ZERO
});
- let pos = Gen::new(main, cross).switch(self.flow).to_point();
- output.push_layout(pos, layout);
+ let pos = Gen::new(main, cross).switch(self.dirs).to_point();
+ output.push_frame(pos, frame);
}
self.finished.push(output);
@@ -144,7 +144,7 @@ impl<'a> StackLayouter<'a> {
self.ruler = Align::Start;
}
- fn finish(mut self) -> Vec<BoxLayout> {
+ fn finish(mut self) -> Vec<Frame> {
self.finish_area();
self.finished
}
diff --git a/src/layout/text.rs b/src/layout/text.rs
index 56b2328e..cfd83372 100644
--- a/src/layout/text.rs
+++ b/src/layout/text.rs
@@ -8,11 +8,11 @@ use crate::shaping;
/// A text node.
#[derive(Clone, PartialEq)]
-pub struct Text {
+pub struct NodeText {
/// The text.
pub text: String,
/// How to align this text node in its parent.
- pub align: BoxAlign,
+ pub align: ChildAlign,
/// The text direction.
pub dir: Dir,
/// The font size.
@@ -23,15 +23,15 @@ pub struct Text {
pub variant: FontVariant,
}
-impl Layout for Text {
+impl Layout for NodeText {
fn layout(&self, ctx: &mut LayoutContext, _: &Areas) -> Layouted {
let mut env = ctx.env.borrow_mut();
- Layouted::Layout(
+ Layouted::Frame(
shaping::shape(
- &mut env.fonts,
&self.text,
self.dir,
self.font_size,
+ &mut env.fonts,
&self.families,
self.variant,
),
@@ -40,14 +40,14 @@ impl Layout for Text {
}
}
-impl Debug for Text {
+impl Debug for NodeText {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "Text({})", self.text)
}
}
-impl From<Text> for LayoutNode {
- fn from(text: Text) -> Self {
+impl From<NodeText> for Node {
+ fn from(text: NodeText) -> Self {
Self::Text(text)
}
}