diff options
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/mod.rs | 116 | ||||
| -rw-r--r-- | src/layout/size.rs | 22 |
2 files changed, 87 insertions, 51 deletions
diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 7bc62bd3..f2ca3029 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -1,73 +1,73 @@ -//! Layouting engine. +//! The layouting engine. -use crate::doc::Document; +use crate::doc::{Document, Page, TextAction}; use crate::font::{Font, FontLoader, FontFamily, FontError}; use crate::syntax::SyntaxTree; mod size; + pub use size::Size; -/// Layout a syntax tree given a context. +/// Layout a syntax tree in a given context. #[allow(unused_variables)] pub fn layout(tree: &SyntaxTree, ctx: &LayoutContext) -> LayoutResult<Layout> { - Ok(Layout {}) + Ok(Layout { + extent: LayoutDimensions { width: Size::zero(), height: Size::zero() }, + actions: vec![], + }) } /// A collection of layouted content. -pub struct Layout {} +#[derive(Debug, Clone)] +pub struct Layout { + /// The extent of this layout into all directions. + extent: LayoutDimensions, + /// Actions composing this layout. + actions: Vec<TextAction>, +} impl Layout { /// Convert this layout into a document given the list of fonts referenced by it. pub fn into_document(self, fonts: Vec<Font>) -> Document { Document { - pages: vec![], + pages: vec![Page { + width: self.extent.width, + height: self.extent.height, + actions: self.actions, + }], fonts, } } } +/// Types supporting some kind of layouting. +pub trait Layouter { + /// Finishing the current layouting process and return a layout. + fn finish(self) -> LayoutResult<Layout>; +} + /// The context for layouting. +#[derive(Debug, Clone)] pub struct LayoutContext<'a, 'p> { + /// Loads fonts matching queries. pub loader: &'a FontLoader<'p>, + /// The spacial constraints to layout in. + pub max_extent: LayoutDimensions, + /// Base style to set text with. + pub text_style: TextStyle, } -/// Default styles for pages. -#[derive(Debug, Clone, PartialEq)] -pub struct PageStyle { - /// The width of the paper. +#[derive(Debug, Clone)] +pub struct LayoutDimensions { + /// Horizontal extent. pub width: Size, - /// The height of the paper. + /// Vertical extent. pub height: Size, - - /// The left margin of the paper. - pub margin_left: Size, - /// The top margin of the paper. - pub margin_top: Size, - /// The right margin of the paper. - pub margin_right: Size, - /// The bottom margin of the paper. - pub margin_bottom: Size, -} - -impl Default for PageStyle { - fn default() -> PageStyle { - PageStyle { - // A4 paper. - width: Size::from_mm(210.0), - height: Size::from_mm(297.0), - - // Margins. A bit more on top and bottom. - margin_left: Size::from_cm(3.0), - margin_top: Size::from_cm(3.0), - margin_right: Size::from_cm(3.0), - margin_bottom: Size::from_cm(3.0), - } - } } -/// Default styles for texts. -#[derive(Debug, Clone, PartialEq)] +/// Default styles for text. +#[derive(Debug, Clone)] pub struct TextStyle { /// A fallback list of font families to use. pub font_families: Vec<FontFamily>, @@ -75,7 +75,7 @@ pub struct TextStyle { pub font_size: f32, /// The line spacing (as a multiple of the font size). pub line_spacing: f32, - /// The spacing for paragraphs (as a multiple of the line spacing). + /// The paragraphs spacing (as a multiple of the line spacing). pub paragraph_spacing: f32, } @@ -92,10 +92,44 @@ impl Default for TextStyle { } } +/// Default styles for pages. +#[derive(Debug, Clone)] +pub struct PageStyle { + /// The width of the page. + pub width: Size, + /// The height of the page. + pub height: Size, + + /// The amount of white space on the left side. + pub margin_left: Size, + /// The amount of white space on the top side. + pub margin_top: Size, + /// The amount of white space on the right side. + pub margin_right: Size, + /// The amount of white space on the bottom side. + pub margin_bottom: Size, +} + +impl Default for PageStyle { + fn default() -> PageStyle { + PageStyle { + // A4 paper. + width: Size::from_mm(210.0), + height: Size::from_mm(297.0), + + // All the same margins. + margin_left: Size::from_cm(3.0), + margin_top: Size::from_cm(3.0), + margin_right: Size::from_cm(3.0), + margin_bottom: Size::from_cm(3.0), + } + } +} + /// The error type for layouting. pub enum LayoutError { /// There was no suitable font. - MissingFont, + NoSuitableFont, /// An error occured while gathering font data. Font(FontError), } @@ -106,7 +140,7 @@ pub type LayoutResult<T> = Result<T, LayoutError>; error_type! { err: LayoutError, show: f => match err { - LayoutError::MissingFont => write!(f, "missing font"), + LayoutError::NoSuitableFont => write!(f, "no suitable font"), LayoutError::Font(err) => write!(f, "font error: {}", err), }, source: match err { diff --git a/src/layout/size.rs b/src/layout/size.rs index bf79a3c4..d0b557d7 100644 --- a/src/layout/size.rs +++ b/src/layout/size.rs @@ -1,10 +1,12 @@ +//! A general spacing type. + use std::cmp::Ordering; use std::fmt::{self, Display, Debug, Formatter}; use std::iter::Sum; use std::ops::*; -/// A general size (unit of length) type. +/// A general spacing type. #[derive(Copy, Clone, PartialEq, Default)] pub struct Size { /// The size in typographic points (1/72 inches). @@ -12,39 +14,39 @@ pub struct Size { } impl Size { - /// Create an zeroed size. + /// Create a zeroed size. #[inline] pub fn zero() -> Size { Size { points: 0.0 } } - /// Create a size from a number of points. + /// Create a size from an amount of points. #[inline] pub fn from_points(points: f32) -> Size { Size { points } } - /// Create a size from a number of inches. + /// Create a size from an amount of inches. #[inline] pub fn from_inches(inches: f32) -> Size { Size { points: 72.0 * inches } } - /// Create a size from a number of millimeters. + /// Create a size from an amount of millimeters. #[inline] pub fn from_mm(mm: f32) -> Size { Size { points: 2.83465 * mm } } - /// Create a size from a number of centimeters. + /// Create a size from an amount of centimeters. #[inline] pub fn from_cm(cm: f32) -> Size { Size { points: 28.3465 * cm } } - /// Create a size from a number of points. + /// Convert this size into points. #[inline] pub fn to_points(&self) -> f32 { self.points } - /// Create a size from a number of inches. + /// Convert this size into inches. #[inline] pub fn to_inches(&self) -> f32 { self.points * 0.0138889 } - /// Create a size from a number of millimeters. + /// Convert this size into millimeters. #[inline] pub fn to_mm(&self) -> f32 { self.points * 0.352778 } - /// Create a size from a number of centimeters. + /// Convert this size into centimeters. #[inline] pub fn to_cm(&self) -> f32 { self.points * 0.0352778 } } |
