From 6672f8f7dfcb38bbda3ec92bdf95341c05e9a782 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Sun, 4 Oct 2020 18:18:55 +0200 Subject: =?UTF-8?q?Remove=20Typesetter=20in=20favor=20of=20typeset=20funct?= =?UTF-8?q?ion=20=F0=9F=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/export/pdf.rs | 3 +- src/layout/line.rs | 1 - src/layout/mod.rs | 43 +++++++++++++++++++++----- src/layout/tree.rs | 6 ++-- src/lib.rs | 85 +++++++++------------------------------------------- src/library/boxed.rs | 2 +- src/prelude.rs | 2 +- src/shaping.rs | 3 +- 8 files changed, 58 insertions(+), 87 deletions(-) (limited to 'src') diff --git a/src/export/pdf.rs b/src/export/pdf.rs index 1bed2188..d4d1adf2 100644 --- a/src/export/pdf.rs +++ b/src/export/pdf.rs @@ -14,8 +14,7 @@ use tide::{PdfWriter, Rect, Ref, Trailer, Version}; use ttf_parser::{name_id, GlyphId}; use crate::font::FontLoader; -use crate::layout::elements::LayoutElement; -use crate::layout::BoxLayout; +use crate::layout::{BoxLayout, LayoutElement}; use crate::length::Length; /// Export a list of layouts into a _PDF_ document. diff --git a/src/layout/line.rs b/src/layout/line.rs index 86531f30..c190c152 100644 --- a/src/layout/line.rs +++ b/src/layout/line.rs @@ -8,7 +8,6 @@ //! Internally, the line layouter uses a stack layouter to stack the finished //! lines on top of each. -use super::stack::{StackContext, StackLayouter}; use super::*; /// Performs the line layouting. diff --git a/src/layout/mod.rs b/src/layout/mod.rs index c9abb165..8156f596 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -1,13 +1,17 @@ //! Layouting of syntax trees into box layouts. -pub mod elements; -pub mod line; pub mod primitive; -pub mod stack; + +mod elements; +mod line; +mod stack; mod tree; +pub use elements::*; +pub use line::*; pub use primitive::*; -pub use tree::layout_tree as layout; +pub use stack::*; +pub use tree::*; use crate::geom::{Insets, Point, Rect, RectExt, Sides, Size, SizeExt}; @@ -15,8 +19,33 @@ use crate::eval::Scope; use crate::font::SharedFontLoader; use crate::style::{LayoutStyle, PageStyle, TextStyle}; use crate::syntax::SynTree; - -use elements::LayoutElements; +use crate::Pass; + +/// Layout a syntax tree and return the produced layout. +pub async fn layout( + tree: &SynTree, + style: &LayoutStyle, + scope: &Scope, + loader: SharedFontLoader, +) -> Pass { + let space = LayoutSpace { + size: style.page.size, + insets: style.page.insets(), + expansion: LayoutExpansion::new(true, true), + }; + tree::layout_tree(&tree, LayoutContext { + loader, + scope, + style, + base: space.usable(), + spaces: vec![space], + repeat: true, + sys: LayoutSystem::new(Dir::LTR, Dir::TTB), + align: LayoutAlign::new(GenAlign::Start, GenAlign::Start), + root: true, + }) + .await +} /// A collection of layouts. pub type MultiLayout = Vec; @@ -36,7 +65,7 @@ pub struct BoxLayout { #[derive(Debug, Clone)] pub struct LayoutContext<'a> { /// The font loader to query fonts from when typesetting text. - pub loader: &'a SharedFontLoader, + pub loader: SharedFontLoader, /// The function scope. pub scope: &'a Scope, /// The style for pages and text. diff --git a/src/layout/tree.rs b/src/layout/tree.rs index dfc1c464..df174544 100644 --- a/src/layout/tree.rs +++ b/src/layout/tree.rs @@ -1,6 +1,7 @@ //! Layouting of syntax trees. -use super::line::{LineContext, LineLayouter}; +use std::rc::Rc; + use super::*; use crate::shaping; use crate::style::LayoutStyle; @@ -9,7 +10,7 @@ use crate::syntax::{ }; use crate::{DynFuture, Feedback, Pass}; -/// Layout a syntax tree into a collection of boxes. +/// Layout a syntax tree in a given context. pub async fn layout_tree(tree: &SynTree, ctx: LayoutContext<'_>) -> Pass { let mut layouter = TreeLayouter::new(ctx); layouter.layout_tree(tree).await; @@ -159,6 +160,7 @@ impl<'a> TreeLayouter<'a> { style: &self.style, spaces: self.layouter.remaining(), root: false, + loader: Rc::clone(&self.ctx.loader), ..self.ctx }; diff --git a/src/lib.rs b/src/lib.rs index 43868cd4..c4a14c2e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,78 +46,21 @@ use std::pin::Pin; use crate::diagnostic::Diagnostic; use crate::eval::{Scope, Value}; use crate::font::SharedFontLoader; -use crate::layout::{ - layout, Commands, Dir, GenAlign, LayoutAlign, LayoutContext, LayoutExpansion, - LayoutSpace, LayoutSystem, MultiLayout, -}; -use crate::style::{LayoutStyle, PageStyle, TextStyle}; -use crate::syntax::{Decoration, Offset, Pos, SpanVec, SynTree}; - -/// Transforms source code into typesetted layouts. -/// -/// A typesetter can be configured through various methods. -pub struct Typesetter { - /// The font loader shared by all typesetting processes. +use crate::layout::{Commands, MultiLayout}; +use crate::style::LayoutStyle; +use crate::syntax::{Decoration, Offset, Pos, SpanVec}; + +/// Process source code directly into a collection of layouts. +pub async fn typeset( + src: &str, + style: &LayoutStyle, + scope: &Scope, loader: SharedFontLoader, - /// A scope that contains the standard library function definitions. - std: Scope, - /// The base layouting style. - style: LayoutStyle, -} - -impl Typesetter { - /// Create a new typesetter. - pub fn new(loader: SharedFontLoader) -> Self { - Self { - loader, - std: crate::library::_std(), - style: LayoutStyle::default(), - } - } - - /// Set the base text style. - pub fn set_text_style(&mut self, style: TextStyle) { - self.style.text = style; - } - - /// Set the base page style. - pub fn set_page_style(&mut self, style: PageStyle) { - self.style.page = style; - } - - /// Parse source code into a syntax tree. - pub fn parse(&self, src: &str) -> Pass { - parse::parse(src) - } - - /// Layout a syntax tree and return the produced layout. - pub async fn layout(&self, tree: &SynTree) -> Pass { - let space = LayoutSpace { - size: self.style.page.size, - insets: self.style.page.insets(), - expansion: LayoutExpansion::new(true, true), - }; - layout(&tree, LayoutContext { - loader: &self.loader, - scope: &self.std, - style: &self.style, - base: space.usable(), - spaces: vec![space], - repeat: true, - sys: LayoutSystem::new(Dir::LTR, Dir::TTB), - align: LayoutAlign::new(GenAlign::Start, GenAlign::Start), - root: true, - }) - .await - } - - /// Process source code directly into a collection of layouts. - pub async fn typeset(&self, src: &str) -> Pass { - let parsed = self.parse(src); - let layouted = self.layout(&parsed.output).await; - let feedback = Feedback::merge(parsed.feedback, layouted.feedback); - Pass::new(layouted.output, feedback) - } +) -> Pass { + let parsed = parse::parse(src); + let layouted = layout::layout(&parsed.output, style, scope, loader).await; + let feedback = Feedback::merge(parsed.feedback, layouted.feedback); + Pass::new(layouted.output, feedback) } /// A dynamic future type which allows recursive invocation of async functions diff --git a/src/library/boxed.rs b/src/library/boxed.rs index b36a151b..fe0272bf 100644 --- a/src/library/boxed.rs +++ b/src/library/boxed.rs @@ -33,7 +33,7 @@ pub async fn boxed( ctx.spaces[0].expansion.vertical = true; } - let layouted = layout(&content, ctx).await; + let layouted = layout_tree(&content, ctx).await; let layout = layouted.output.into_iter().next().unwrap(); f.extend(layouted.feedback); diff --git a/src/prelude.rs b/src/prelude.rs index 899ced7d..84c5859b 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -4,7 +4,7 @@ pub use crate::eval::{Dict, DictValue, Value}; pub use crate::layout::primitive::*; #[doc(no_inline)] -pub use crate::layout::{layout, Command, Commands, LayoutContext}; +pub use crate::layout::{layout_tree, Command, Commands, LayoutContext}; #[doc(no_inline)] pub use crate::syntax::{Span, Spanned, SynTree}; pub use crate::{Feedback, Pass}; diff --git a/src/shaping.rs b/src/shaping.rs index 01739db3..d9e6e9fd 100644 --- a/src/shaping.rs +++ b/src/shaping.rs @@ -9,8 +9,7 @@ use ttf_parser::GlyphId; use crate::font::FontLoader; use crate::geom::{Point, Size}; -use crate::layout::elements::{LayoutElement, LayoutElements, Shaped}; -use crate::layout::{BoxLayout, Dir, LayoutAlign}; +use crate::layout::{BoxLayout, Dir, LayoutAlign, LayoutElement, LayoutElements, Shaped}; use crate::style::TextStyle; /// Shape text into a box. -- cgit v1.2.3