diff options
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/actions.rs | 26 | ||||
| -rw-r--r-- | src/layout/flex.rs | 13 | ||||
| -rw-r--r-- | src/layout/mod.rs | 45 | ||||
| -rw-r--r-- | src/layout/stacked.rs | 12 | ||||
| -rw-r--r-- | src/layout/text.rs | 10 |
5 files changed, 62 insertions, 44 deletions
diff --git a/src/layout/actions.rs b/src/layout/actions.rs index 069c3f7c..3eb4fb7f 100644 --- a/src/layout/actions.rs +++ b/src/layout/actions.rs @@ -3,11 +3,10 @@ use std::fmt::{self, Display, Formatter}; use std::io::{self, Write}; -use crate::size::Size2D; use super::Layout; +use crate::size::Size2D; use LayoutAction::*; - /// A layouting action. #[derive(Clone)] pub enum LayoutAction { @@ -30,8 +29,14 @@ impl LayoutAction { MoveAbsolute(s) => write!(f, "m {:.4} {:.4}", s.x.to_pt(), s.y.to_pt()), SetFont(i, s) => write!(f, "f {} {}", i, s), WriteText(s) => write!(f, "w {}", s), - DebugBox(p, s) => write!(f, "b {} {} {} {}", - p.x.to_pt(), p.y.to_pt(), s.x.to_pt(), s.y.to_pt()) + DebugBox(p, s) => write!( + f, + "b {} {} {} {}", + p.x.to_pt(), + p.y.to_pt(), + s.x.to_pt(), + s.y.to_pt() + ), } } } @@ -81,7 +86,7 @@ impl LayoutActionList { SetFont(index, size) if (index, size) != self.active_font => { self.next_font = Some((index, size)); - }, + } _ => { if let Some(target) = self.next_pos.take() { @@ -92,19 +97,21 @@ impl LayoutActionList { } self.actions.push(action); - }, + } } } /// Add a series of actions. - pub fn extend<I>(&mut self, actions: I) where I: IntoIterator<Item=LayoutAction> { + pub fn extend<I>(&mut self, actions: I) + where I: IntoIterator<Item = LayoutAction> { for action in actions.into_iter() { self.add(action); } } /// Add all actions from a box layout at a position. A move to the position - /// is generated and all moves inside the box layout are translated as necessary. + /// is generated and all moves inside the box layout are translated as + /// necessary. pub fn add_box(&mut self, position: Size2D, layout: Layout) { if let Some(target) = self.next_pos.take() { self.actions.push(MoveAbsolute(target)); @@ -114,7 +121,8 @@ impl LayoutActionList { self.origin = position; if layout.debug_render { - self.actions.push(LayoutAction::DebugBox(position, layout.dimensions)); + self.actions + .push(LayoutAction::DebugBox(position, layout.dimensions)); } self.extend(layout.actions); diff --git a/src/layout/flex.rs b/src/layout/flex.rs index 704281d3..ab1f066e 100644 --- a/src/layout/flex.rs +++ b/src/layout/flex.rs @@ -1,6 +1,5 @@ use super::*; - /// Finishes a flex layout by justifying the positions of the individual boxes. #[derive(Debug)] pub struct FlexLayouter { @@ -32,7 +31,8 @@ enum FlexUnit { /// A content unit to be arranged flexibly. Boxed(Layout), /// A unit which acts as glue between two [`FlexUnit::Boxed`] units and - /// is only present if there was no flow break in between the two surrounding boxes. + /// is only present if there was no flow break in between the two + /// surrounding boxes. Glue(Layout), } @@ -107,7 +107,9 @@ impl FlexLayouter { /// Layout the box. fn boxed(&mut self, boxed: Layout) -> LayoutResult<()> { - let last_glue_x = self.last_glue.as_ref() + let last_glue_x = self + .last_glue + .as_ref() .map(|g| g.dimensions.x) .unwrap_or(Size::zero()); @@ -157,7 +159,7 @@ impl FlexLayouter { // Right align everything by shifting it right by the // amount of space left to the right of the line. cursor + remaining_space - }, + } }; self.actions.add_box(position, layout); @@ -173,7 +175,8 @@ impl FlexLayouter { self.dimensions.y += self.line_metrics.y; - // Reset the cursor the left and move down by the line and the inter-line spacing. + // Reset the cursor the left and move down by the line and the inter-line + // spacing. self.cursor.x = self.ctx.space.padding.left; self.cursor.y += self.line_metrics.y + self.ctx.flex_spacing; diff --git a/src/layout/mod.rs b/src/layout/mod.rs index c8d10141..14aa6417 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -4,24 +4,23 @@ use std::borrow::Cow; use std::io::{self, Write}; use std::mem; -use toddle::query::{SharedFontLoader, FontClass}; +use toddle::query::{FontClass, SharedFontLoader}; use toddle::Error as FontError; use crate::func::Command; use crate::size::{Size, Size2D, SizeBox}; -use crate::syntax::{SyntaxTree, Node, FuncCall}; use crate::style::TextStyle; +use crate::syntax::{FuncCall, Node, SyntaxTree}; -mod text; -mod stacked; -mod flex; mod actions; +mod flex; +mod stacked; +mod text; pub use actions::{LayoutAction, LayoutActionList}; +pub use flex::{FlexContext, FlexLayouter}; +pub use stacked::{StackContext, StackLayouter}; pub use text::{layout_text, TextContext}; -pub use flex::{FlexLayouter, FlexContext}; -pub use stacked::{StackLayouter, StackContext}; - /// A box layout has a fixed width and height and composes of actions. #[derive(Debug, Clone)] @@ -37,7 +36,12 @@ pub struct Layout { impl Layout { /// Serialize this layout into an output buffer. pub fn serialize<W: Write>(&self, f: &mut W) -> io::Result<()> { - writeln!(f, "{:.4} {:.4}", self.dimensions.x.to_pt(), self.dimensions.y.to_pt())?; + writeln!( + f, + "{:.4} {:.4}", + self.dimensions.x.to_pt(), + self.dimensions.y.to_pt() + )?; for action in &self.actions { action.serialize(f)?; writeln!(f)?; @@ -55,9 +59,7 @@ pub struct MultiLayout { impl MultiLayout { /// Create an empty multibox layout. pub fn new() -> MultiLayout { - MultiLayout { - layouts: vec![], - } + MultiLayout { layouts: vec![] } } /// Extract a single sublayout and panic if this layout does not have @@ -158,7 +160,7 @@ impl<'a, 'p> Layouter<'a, 'p> { }, flex_spacing: (ctx.style.line_spacing - 1.0) * Size::pt(ctx.style.font_size), }), - style: Cow::Borrowed(ctx.style) + style: Cow::Borrowed(ctx.style), } } @@ -175,7 +177,7 @@ impl<'a, 'p> Layouter<'a, 'p> { if !self.flex_layouter.is_empty() { self.layout_text(" ", true)?; } - }, + } // Finish the current flex layout and add it to the box layouter. Node::Newline => { @@ -186,7 +188,7 @@ impl<'a, 'p> Layouter<'a, 'p> { let size = Size::pt(self.style.font_size) * (self.style.line_spacing * self.style.paragraph_spacing - 1.0); self.stack_layouter.add_space(size)?; - }, + } // Toggle the text styles. Node::ToggleItalics => self.style.to_mut().toggle_class(FontClass::Italic), @@ -208,16 +210,19 @@ impl<'a, 'p> Layouter<'a, 'p> { } Ok(MultiLayout { - layouts: vec![self.stack_layouter.finish()] + layouts: vec![self.stack_layouter.finish()], }) } /// Layout a piece of text into a box. fn layout_text(&mut self, text: &str, glue: bool) -> LayoutResult<()> { - let boxed = layout_text(text, TextContext { - loader: &self.ctx.loader, - style: &self.style, - })?; + let boxed = layout_text( + text, + TextContext { + loader: &self.ctx.loader, + style: &self.style, + }, + )?; if glue { self.flex_layouter.add_glue(boxed); diff --git a/src/layout/stacked.rs b/src/layout/stacked.rs index 312681ac..5ca32970 100644 --- a/src/layout/stacked.rs +++ b/src/layout/stacked.rs @@ -1,6 +1,5 @@ use super::*; - /// Layouts boxes block-style. #[derive(Debug)] pub struct StackLayouter { @@ -29,10 +28,13 @@ impl StackLayouter { Alignment::Right => Size2D::with_x(space.usable().x), }, usable: space.usable(), - cursor: Size2D::new(match ctx.space.alignment { - Alignment::Left => space.padding.left, - Alignment::Right => space.dimensions.x - space.padding.right, - }, space.padding.top), + cursor: Size2D::new( + match ctx.space.alignment { + Alignment::Left => space.padding.left, + Alignment::Right => space.dimensions.x - space.padding.right, + }, + space.padding.top, + ), } } diff --git a/src/layout/text.rs b/src/layout/text.rs index cf258029..27b65d56 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -1,9 +1,8 @@ use toddle::query::{FontQuery, SharedFontLoader}; -use toddle::tables::{Header, CharMap, HorizontalMetrics}; +use toddle::tables::{CharMap, Header, HorizontalMetrics}; -use crate::size::{Size, Size2D}; use super::*; - +use crate::size::{Size, Size2D}; /// The context for text layouting. #[derive(Copy, Clone)] @@ -53,7 +52,8 @@ pub fn layout_text(text: &str, ctx: TextContext) -> LayoutResult<Layout> { let font_unit_to_size = |x| Size::pt(font_unit_ratio * x); // Add the char width to the total box width. - let glyph = font.read_table::<CharMap>()? + let glyph = font + .read_table::<CharMap>()? .get(character) .expect("layout text: font should have char"); @@ -61,7 +61,7 @@ pub fn layout_text(text: &str, ctx: TextContext) -> LayoutResult<Layout> { font.read_table::<HorizontalMetrics>()? .get(glyph) .expect("layout text: font should have glyph") - .advance_width as f32 + .advance_width as f32, ); let char_width = glyph_width * ctx.style.font_size; |
