summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorLaurenz Mädje <laurmaedje@gmail.com>2019-06-02 18:01:22 +0200
committerLaurenz Mädje <laurmaedje@gmail.com>2019-06-02 18:01:22 +0200
commit221934df4b586250b0063282ef8885c475dec7a7 (patch)
treeea91eb6fe6fbad71377f2dbf1c23a3a808dbf970 /src/lib.rs
parentc4eb4ee36261be8832b2649cc075edee188be5c7 (diff)
Add margins with basic box layouter 📖
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/lib.rs b/src/lib.rs
index dd0785f7..c56cbba1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -48,7 +48,7 @@ use std::fmt::{self, Debug, Formatter};
use crate::doc::Document;
use crate::font::{Font, FontLoader, FontProvider};
use crate::func::Scope;
-use crate::layout::{layout, Layout, LayoutContext, LayoutDimensions};
+use crate::layout::{layout, Layout, Layouter, LayoutContext, BoxLayouter, Extent, Position};
use crate::layout::{PageStyle, TextStyle, LayoutResult, LayoutError};
use crate::parsing::{parse, ParseContext, ParseResult, ParseError};
use crate::syntax::SyntaxTree;
@@ -115,21 +115,34 @@ impl<'p> Typesetter<'p> {
}
/// Layout a syntax tree and return the layout and the referenced font list.
- #[inline]
pub fn layout(&self, tree: &SyntaxTree) -> LayoutResult<(Layout, Vec<Font>)> {
let loader = FontLoader::new(&self.font_providers);
+ // Prepare the layouting context.
let page = &self.page_style;
- let ctx = LayoutContext {
+ let mut ctx = LayoutContext {
loader: &loader,
text_style: self.text_style.clone(),
- max_extent: LayoutDimensions {
+ max_extent: Extent {
width: page.width - page.margin_left - page.margin_right,
height: page.height - page.margin_top - page.margin_bottom,
},
};
- let layout = layout(&tree, &ctx)?;
+ // Layout the content of the page (without margins).
+ let content = layout(&tree, &ctx)?;
+
+ // Adjust the context for adding the margins.
+ ctx.max_extent = Extent {
+ width: page.width,
+ height: page.height,
+ };
+
+ // Add the margins.
+ let mut box_layouter = BoxLayouter::new(&ctx);
+ let start = Position { x: page.margin_left, y: page.margin_top };
+ box_layouter.add_layout_absolute(start, content);
+ let layout = box_layouter.finish()?;
Ok((layout, loader.into_fonts()))
}