summaryrefslogtreecommitdiff
path: root/src/layout
diff options
context:
space:
mode:
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/document.rs37
-rw-r--r--src/layout/graphics.rs62
-rw-r--r--src/layout/mod.rs36
-rw-r--r--src/layout/node.rs12
-rw-r--r--src/layout/pad.rs12
5 files changed, 44 insertions, 115 deletions
diff --git a/src/layout/document.rs b/src/layout/document.rs
deleted file mode 100644
index 112457d6..00000000
--- a/src/layout/document.rs
+++ /dev/null
@@ -1,37 +0,0 @@
-use super::*;
-
-/// 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> {
- let mut layouts = vec![];
- for run in &self.runs {
- layouts.extend(run.layout(ctx));
- }
- layouts
- }
-}
-
-/// A variable-length run of pages that all have the same properties.
-#[derive(Debug, Clone, PartialEq)]
-pub struct Pages {
- /// The size of the pages.
- 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/graphics.rs b/src/layout/graphics.rs
deleted file mode 100644
index 1fa05605..00000000
--- a/src/layout/graphics.rs
+++ /dev/null
@@ -1,62 +0,0 @@
-use std::fmt::{self, Debug, Formatter};
-
-use super::*;
-
-/// An image node.
-#[derive(Clone, PartialEq)]
-pub struct Image {
- /// The image.
- pub buf: RgbaImage,
- /// The fixed width, if any.
- pub width: Option<Linear>,
- /// The fixed height, if any.
- pub height: Option<Linear>,
- /// How to align this image node in its parent.
- pub align: BoxAlign,
-}
-
-impl Layout for Image {
- fn layout(&self, _: &mut LayoutContext, areas: &Areas) -> Layouted {
- let Area { rem, full } = areas.current;
- let (pixel_width, pixel_height) = self.buf.dimensions();
- let pixel_ratio = (pixel_width as f64) / (pixel_height as f64);
-
- let width = self.width.map(|w| w.resolve(full.width));
- let height = self.height.map(|w| w.resolve(full.height));
-
- let size = match (width, height) {
- (Some(width), Some(height)) => Size::new(width, height),
- (Some(width), None) => Size::new(width, width / pixel_ratio),
- (None, Some(height)) => Size::new(height * pixel_ratio, height),
- (None, None) => {
- let ratio = rem.width / rem.height;
- if ratio < pixel_ratio {
- Size::new(rem.width, rem.width / pixel_ratio)
- } else {
- // TODO: Fix issue with line spacing.
- Size::new(rem.height * pixel_ratio, rem.height)
- }
- }
- };
-
- let mut boxed = BoxLayout::new(size);
- boxed.push(
- Point::ZERO,
- LayoutElement::Image(ImageElement { buf: self.buf.clone(), size }),
- );
-
- Layouted::Layout(boxed, self.align)
- }
-}
-
-impl Debug for Image {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- f.pad("Image")
- }
-}
-
-impl From<Image> for LayoutNode {
- fn from(image: Image) -> Self {
- Self::dynamic(image)
- }
-}
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index 2d4553b4..57add044 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -1,8 +1,6 @@
//! Layouting of documents.
-mod document;
mod fixed;
-mod graphics;
mod node;
mod pad;
mod par;
@@ -16,9 +14,7 @@ use crate::font::SharedFontLoader;
use crate::geom::*;
use crate::shaping::Shaped;
-pub use document::*;
pub use fixed::*;
-pub use graphics::*;
pub use node::*;
pub use pad::*;
pub use par::*;
@@ -193,3 +189,35 @@ pub struct ImageElement {
/// The document size of the image.
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 35e742a5..f01ded56 100644
--- a/src/layout/node.rs
+++ b/src/layout/node.rs
@@ -74,12 +74,6 @@ impl Debug for Dynamic {
}
}
-impl From<Dynamic> for LayoutNode {
- fn from(dynamic: Dynamic) -> Self {
- Self::Dyn(dynamic)
- }
-}
-
impl Clone for Dynamic {
fn clone(&self) -> Self {
Self(self.0.dyn_clone())
@@ -92,6 +86,12 @@ impl PartialEq for Dynamic {
}
}
+impl From<Dynamic> for LayoutNode {
+ fn from(dynamic: Dynamic) -> Self {
+ Self::Dyn(dynamic)
+ }
+}
+
/// A dynamic node, which can implement custom layouting behaviour.
///
/// This trait just combines the requirements for types to qualify as dynamic
diff --git a/src/layout/pad.rs b/src/layout/pad.rs
index 09cf016b..00830a07 100644
--- a/src/layout/pad.rs
+++ b/src/layout/pad.rs
@@ -29,6 +29,12 @@ impl Layout for Pad {
}
}
+impl From<Pad> for LayoutNode {
+ fn from(pad: Pad) -> Self {
+ Self::dynamic(pad)
+ }
+}
+
/// Shrink all areas by the padding.
fn shrink_areas(areas: &Areas, padding: Sides<Linear>) -> Areas {
let shrink = |size| size - padding.resolve(size).size();
@@ -52,9 +58,3 @@ fn pad_layout(layout: &mut BoxLayout, padding: Sides<Linear>) {
*point += origin;
}
}
-
-impl From<Pad> for LayoutNode {
- fn from(pad: Pad) -> Self {
- Self::dynamic(pad)
- }
-}