From 107450ee5ca8e7e0bc03cf6ce9f59268aa20e9f6 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 11 Mar 2019 22:15:34 +0100 Subject: =?UTF-8?q?Restructure=20typeset=20crate=20=E2=9C=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/engine.rs | 59 ++++++++++++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) (limited to 'src/engine.rs') diff --git a/src/engine.rs b/src/engine.rs index f7d820e8..34f98766 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -1,53 +1,26 @@ //! Core typesetting engine. +use std::error; use std::fmt; -use crate::parsing::{SyntaxTree, Node}; +use crate::syntax::{SyntaxTree, Node}; use crate::doc::{Document, Style, Page, Text, TextCommand}; use crate::font::Font; -/// A type that can be typesetted into a document. -pub trait Typeset { - /// Generate a document from self. - fn typeset(self) -> TypeResult; -} - -impl Typeset for SyntaxTree<'_> { - fn typeset(self) -> TypeResult { - Engine::new(self).typeset() - } -} - -/// Result type used for parsing. -type TypeResult = std::result::Result; - -/// Errors occuring in the process of typesetting. -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct TypesetError { - message: String, -} - -impl fmt::Display for TypesetError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(&self.message) - } -} - - -/// Transforms an abstract syntax tree into a document. +/// The core typesetting engine, transforming an abstract syntax tree into a document. #[derive(Debug, Clone)] -struct Engine<'s> { +pub struct Engine<'s> { tree: SyntaxTree<'s>, } impl<'s> Engine<'s> { /// Create a new generator from a syntax tree. - fn new(tree: SyntaxTree<'s>) -> Engine<'s> { + pub fn new(tree: SyntaxTree<'s>) -> Engine<'s> { Engine { tree } } /// Generate the abstract document. - fn typeset(&mut self) -> TypeResult { + pub fn typeset(&mut self) -> TypeResult { let style = Style::default(); // Load font defined by style @@ -68,8 +41,7 @@ impl<'s> Engine<'s> { } let page = Page { - width: style.paper_size[0], - height: style.paper_size[1], + size: style.paper_size, text: vec![Text { commands: vec![ TextCommand::Move(style.margins[0], style.paper_size[1] - style.margins[1]), @@ -85,3 +57,20 @@ impl<'s> Engine<'s> { }) } } + +/// Result type used for parsing. +type TypeResult = std::result::Result; + +/// The error type for typesetting. +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct TypesetError { + message: String, +} + +impl error::Error for TypesetError {} + +impl fmt::Display for TypesetError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&self.message) + } +} -- cgit v1.2.3