diff options
| author | Laurenz Mädje <laurmaedje@gmail.com> | 2019-06-02 18:58:37 +0200 |
|---|---|---|
| committer | Laurenz Mädje <laurmaedje@gmail.com> | 2019-06-02 19:15:45 +0200 |
| commit | 3e9f42661ed19464f1a33c279b315dae8d1e6e83 (patch) | |
| tree | c45356f99080a8ba451a88ecd1eb7bf4b53889d6 /src/layout | |
| parent | 221934df4b586250b0063282ef8885c475dec7a7 (diff) | |
Move to top-left default positioning 📐
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/boxed.rs | 2 | ||||
| -rw-r--r-- | src/layout/size.rs | 6 | ||||
| -rw-r--r-- | src/layout/text.rs | 32 |
3 files changed, 18 insertions, 22 deletions
diff --git a/src/layout/boxed.rs b/src/layout/boxed.rs index cfdf51f2..c240db0b 100644 --- a/src/layout/boxed.rs +++ b/src/layout/boxed.rs @@ -22,7 +22,7 @@ impl<'a, 'p> BoxLayouter<'a, 'p> { /// Add a sublayout. pub fn add_layout_absolute(&mut self, position: Position, layout: Layout) { - self.actions.push(TextAction::MoveNewline(position)); + self.actions.push(TextAction::MoveAbsolute(position)); self.actions.extend(layout.actions); } } diff --git a/src/layout/size.rs b/src/layout/size.rs index c4686966..92a4c113 100644 --- a/src/layout/size.rs +++ b/src/layout/size.rs @@ -15,6 +15,12 @@ pub struct Position { pub y: Size, } +impl Position { + /// Create a zeroed position. + #[inline] + pub fn zero() -> Position { Position { x: Size::zero(), y: Size::zero() } } +} + /// Size of a box in 2-dimensional space. #[derive(Debug, Copy, Clone, PartialEq, Default)] pub struct Extent { diff --git a/src/layout/text.rs b/src/layout/text.rs index 0f105531..11a1e38c 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -145,8 +145,9 @@ impl<'a, 'p> TextFinisher<'a, 'p> { let mut units = Vec::new(); mem::swap(&mut self.layouter.units, &mut units); - // Move to the top-left corner of the layout space. - self.move_start(); + // Move from the origin one line below because the y-component of the origin is the + // baseline. + self.move_newline(1.0); for unit in units { match unit { @@ -206,29 +207,18 @@ 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(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. fn move_newline(&mut self, factor: f32) { - if self.active_font != std::usize::MAX { - let vertical = Size::from_points(self.layouter.ctx.text_style.font_size) - * self.layouter.ctx.text_style.line_spacing - * factor; + let vertical = Size::from_points(self.layouter.ctx.text_style.font_size) + * self.layouter.ctx.text_style.line_spacing + * factor; - self.actions.push(TextAction::MoveNewline(Position { - x: Size::zero(), - y: -vertical - })); + self.actions.push(TextAction::MoveNewline(Position { + x: Size::zero(), + y: vertical + })); - self.current_width = Size::zero(); - } + self.current_width = Size::zero(); } /// Output a text action containing the buffered text and reset the buffer. |
