summaryrefslogtreecommitdiff
path: root/src/layout
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/layout
parentc4eb4ee36261be8832b2649cc075edee188be5c7 (diff)
Add margins with basic box layouter 📖
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/boxed.rs37
-rw-r--r--src/layout/mod.rs17
-rw-r--r--src/layout/size.rs18
-rw-r--r--src/layout/text.rs15
4 files changed, 70 insertions, 17 deletions
diff --git a/src/layout/boxed.rs b/src/layout/boxed.rs
new file mode 100644
index 00000000..cfdf51f2
--- /dev/null
+++ b/src/layout/boxed.rs
@@ -0,0 +1,37 @@
+//! Layouting of layout boxes.
+
+use crate::doc::TextAction;
+use super::{Layouter, Layout, LayoutContext, LayoutResult, Position};
+
+
+/// Layouts sublayouts within the constraints of a layouting context.
+#[derive(Debug)]
+pub struct BoxLayouter<'a, 'p> {
+ ctx: &'a LayoutContext<'a, 'p>,
+ actions: Vec<TextAction>,
+}
+
+impl<'a, 'p> BoxLayouter<'a, 'p> {
+ /// Create a new box layouter.
+ pub fn new(ctx: &'a LayoutContext<'a, 'p>) -> BoxLayouter<'a, 'p> {
+ BoxLayouter {
+ ctx,
+ actions: vec![],
+ }
+ }
+
+ /// Add a sublayout.
+ pub fn add_layout_absolute(&mut self, position: Position, layout: Layout) {
+ self.actions.push(TextAction::MoveNewline(position));
+ self.actions.extend(layout.actions);
+ }
+}
+
+impl Layouter for BoxLayouter<'_, '_> {
+ fn finish(self) -> LayoutResult<Layout> {
+ Ok(Layout {
+ extent: self.ctx.max_extent.clone(),
+ actions: self.actions
+ })
+ }
+}
diff --git a/src/layout/mod.rs b/src/layout/mod.rs
index edcd63a7..ab27314d 100644
--- a/src/layout/mod.rs
+++ b/src/layout/mod.rs
@@ -6,9 +6,11 @@ use crate::syntax::{SyntaxTree, Node};
mod size;
mod text;
+mod boxed;
-pub use size::Size;
+pub use size::{Size, Position, Extent};
pub use text::TextLayouter;
+pub use boxed::BoxLayouter;
/// Layout a syntax tree in a given context.
@@ -44,7 +46,7 @@ pub fn layout(tree: &SyntaxTree, ctx: &LayoutContext) -> LayoutResult<Layout> {
#[derive(Debug, Clone)]
pub struct Layout {
/// The extent of this layout into all directions.
- extent: LayoutDimensions,
+ extent: Extent,
/// Actions composing this layout.
actions: Vec<TextAction>,
}
@@ -75,20 +77,11 @@ pub struct LayoutContext<'a, 'p> {
/// Loads fonts matching queries.
pub loader: &'a FontLoader<'p>,
/// The spacial constraints to layout in.
- pub max_extent: LayoutDimensions,
+ pub max_extent: Extent,
/// Base style to set text with.
pub text_style: TextStyle,
}
-/// A space to layout in.
-#[derive(Debug, Clone)]
-pub struct LayoutDimensions {
- /// Horizontal extent.
- pub width: Size,
- /// Vertical extent.
- pub height: Size,
-}
-
/// Default styles for text.
#[derive(Debug, Clone)]
pub struct TextStyle {
diff --git a/src/layout/size.rs b/src/layout/size.rs
index d0b557d7..c4686966 100644
--- a/src/layout/size.rs
+++ b/src/layout/size.rs
@@ -6,6 +6,24 @@ use std::iter::Sum;
use std::ops::*;
+/// A position in 2-dimensional space.
+#[derive(Debug, Copy, Clone, PartialEq, Default)]
+pub struct Position {
+ /// The horizontal coordinate.
+ pub x: Size,
+ /// The vertical coordinate.
+ pub y: Size,
+}
+
+/// Size of a box in 2-dimensional space.
+#[derive(Debug, Copy, Clone, PartialEq, Default)]
+pub struct Extent {
+ /// The horizontal extent.
+ pub width: Size,
+ /// The vertical extent.
+ pub height: Size,
+}
+
/// A general spacing type.
#[derive(Copy, Clone, PartialEq, Default)]
pub struct Size {
diff --git a/src/layout/text.rs b/src/layout/text.rs
index 15da8373..0f105531 100644
--- a/src/layout/text.rs
+++ b/src/layout/text.rs
@@ -7,7 +7,7 @@ use smallvec::SmallVec;
use crate::doc::TextAction;
use crate::font::{Font, FontQuery};
-use super::{Layouter, Layout, LayoutError, LayoutContext, Size, LayoutResult};
+use super::{Layouter, Layout, LayoutError, LayoutContext, LayoutResult, Size, Position};
/// Layouts text within the constraints of a layouting context.
@@ -208,10 +208,11 @@ impl<'a, 'p> TextFinisher<'a, 'p> {
/// Move to the top-left corner of the layout space.
fn move_start(&mut self) {
- self.actions.push(TextAction::MoveNewline(
- Size::zero(), self.layouter.ctx.max_extent.height
+ self.actions.push(TextAction::MoveNewline(Position {
+ x: Size::zero(),
+ y: self.layouter.ctx.max_extent.height
- Size::from_points(self.layouter.ctx.text_style.font_size)
- ));
+ }));
}
/// Move to the next line. A factor of 1.0 uses the default line spacing.
@@ -221,7 +222,11 @@ impl<'a, 'p> TextFinisher<'a, 'p> {
* self.layouter.ctx.text_style.line_spacing
* factor;
- self.actions.push(TextAction::MoveNewline(Size::zero(), -vertical));
+ self.actions.push(TextAction::MoveNewline(Position {
+ x: Size::zero(),
+ y: -vertical
+ }));
+
self.current_width = Size::zero();
}
}