diff options
Diffstat (limited to 'src/layout/mod.rs')
| -rw-r--r-- | src/layout/mod.rs | 116 |
1 files changed, 75 insertions, 41 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 { |
