diff options
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/actions.rs | 10 | ||||
| -rw-r--r-- | src/layout/line.rs | 21 | ||||
| -rw-r--r-- | src/layout/mod.rs | 14 | ||||
| -rw-r--r-- | src/layout/model.rs | 4 | ||||
| -rw-r--r-- | src/layout/stack.rs | 18 | ||||
| -rw-r--r-- | src/layout/text.rs | 17 |
6 files changed, 41 insertions, 43 deletions
diff --git a/src/layout/actions.rs b/src/layout/actions.rs index 7806932e..89c10285 100644 --- a/src/layout/actions.rs +++ b/src/layout/actions.rs @@ -4,7 +4,7 @@ use std::fmt::{self, Debug, Formatter}; use serde::ser::{Serialize, Serializer, SerializeTuple}; use fontdock::FaceId; -use crate::length::{Length, Size}; +use crate::geom::Size; use super::Layout; use self::LayoutAction::*; @@ -15,7 +15,7 @@ pub enum LayoutAction { /// Move to an absolute position. MoveAbsolute(Size), /// Set the font given the index from the font loader and font size. - SetFont(FaceId, Length), + SetFont(FaceId, f64), /// Write text at the current position. WriteText(String), /// Visualize a box for debugging purposes. @@ -82,9 +82,9 @@ impl Debug for LayoutAction { pub struct LayoutActions { origin: Size, actions: Vec<LayoutAction>, - active_font: (FaceId, Length), + active_font: (FaceId, f64), next_pos: Option<Size>, - next_font: Option<(FaceId, Length)>, + next_font: Option<(FaceId, f64)>, } impl LayoutActions { @@ -93,7 +93,7 @@ impl LayoutActions { LayoutActions { actions: vec![], origin: Size::ZERO, - active_font: (FaceId::MAX, Length::ZERO), + active_font: (FaceId::MAX, 0.0), next_pos: None, next_font: None, } diff --git a/src/layout/line.rs b/src/layout/line.rs index 1bb36204..0ef58878 100644 --- a/src/layout/line.rs +++ b/src/layout/line.rs @@ -39,7 +39,7 @@ pub struct LineContext { /// extent of the layout. pub debug: bool, /// The line spacing. - pub line_spacing: Length, + pub line_spacing: f64, } /// A line run is a sequence of boxes with the same alignment that are arranged @@ -48,9 +48,8 @@ pub struct LineContext { #[derive(Debug)] struct LineRun { /// The so-far accumulated layouts in the line. - layouts: Vec<(Length, Layout)>, - /// The width (primary length) and maximal height (secondary length) of the - /// line. + layouts: Vec<(f64, Layout)>, + /// The width and maximal height of the line. size: Size, /// The alignment of all layouts in the line. /// @@ -60,7 +59,7 @@ struct LineRun { alignment: Option<LayoutAlignment>, /// If another line run with different alignment already took up some space /// of the line, this run has less space and how much is stored here. - usable: Option<Length>, + usable: Option<f64>, /// A possibly cached soft spacing or spacing state. last_spacing: LastSpacing, } @@ -104,7 +103,7 @@ impl LineLayouter { let usable = self.stack.usable().primary(axes); rest_run.usable = Some(match layout.alignment.primary { Alignment::Origin => unreachable!("origin > x"), - Alignment::Center => usable - 2 * self.run.size.x, + Alignment::Center => usable - 2.0 * self.run.size.x, Alignment::End => usable - self.run.size.x, }); @@ -138,7 +137,7 @@ impl LineLayouter { self.run.layouts.push((self.run.size.x, layout)); self.run.size.x += size.x; - self.run.size.y.max_eq(size.y); + self.run.size.y = self.run.size.y.max(size.y); self.run.last_spacing = LastSpacing::None; } @@ -170,11 +169,11 @@ impl LineLayouter { } /// Add spacing along the primary axis to the line. - pub fn add_primary_spacing(&mut self, mut spacing: Length, kind: SpacingKind) { + pub fn add_primary_spacing(&mut self, mut spacing: f64, kind: SpacingKind) { match kind { // A hard space is simply an empty box. SpacingKind::Hard => { - spacing.min_eq(self.usable().x); + spacing = spacing.min(self.usable().x); self.run.size.x += spacing; self.run.last_spacing = LastSpacing::Hard; } @@ -196,7 +195,7 @@ impl LineLayouter { } /// Finish the line and add secondary spacing to the underlying stack. - pub fn add_secondary_spacing(&mut self, spacing: Length, kind: SpacingKind) { + pub fn add_secondary_spacing(&mut self, spacing: f64, kind: SpacingKind) { self.finish_line_if_not_empty(); self.stack.add_spacing(spacing, kind) } @@ -218,7 +217,7 @@ impl LineLayouter { } /// Update the line spacing. - pub fn set_line_spacing(&mut self, line_spacing: Length) { + pub fn set_line_spacing(&mut self, line_spacing: f64) { self.ctx.line_spacing = line_spacing; } diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 8bcceda6..a6af0f82 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -5,7 +5,7 @@ use smallvec::SmallVec; use serde::Serialize; use fontdock::FaceId; -use crate::length::{Length, Size, Margins}; +use crate::geom::{Size, Margins}; use self::prelude::*; pub mod line; @@ -219,8 +219,8 @@ impl Direction { /// /// - `1` if the direction is positive. /// - `-1` if the direction is negative. - pub fn factor(self) -> i32 { - if self.is_positive() { 1 } else { -1 } + pub fn factor(self) -> f64 { + if self.is_positive() { 1.0 } else { -1.0 } } /// The inverse axis. @@ -368,17 +368,17 @@ enum LastSpacing { /// The last item was hard spacing. Hard, /// The last item was soft spacing with the given width and level. - Soft(Length, u32), + Soft(f64, u32), /// The last item was not spacing. None, } impl LastSpacing { - /// The length of the soft space if this is a soft space or zero otherwise. - fn soft_or_zero(self) -> Length { + /// The width of the soft space if this is a soft space or zero otherwise. + fn soft_or_zero(self) -> f64 { match self { LastSpacing::Soft(space, _) => space, - _ => Length::ZERO, + _ => 0.0, } } } diff --git a/src/layout/model.rs b/src/layout/model.rs index 3fb594d5..c78c733e 100644 --- a/src/layout/model.rs +++ b/src/layout/model.rs @@ -9,7 +9,7 @@ use smallvec::smallvec; use crate::{Pass, Feedback}; use crate::SharedFontLoader; use crate::style::{LayoutStyle, PageStyle, TextStyle}; -use crate::length::{Length, Size}; +use crate::geom::Size; use crate::syntax::{Model, SyntaxModel, Node, Decoration}; use crate::syntax::span::{Span, Spanned}; use super::line::{LineLayouter, LineContext}; @@ -74,7 +74,7 @@ pub enum Command<'a> { /// Add spacing of given [kind](super::SpacingKind) along the primary or /// secondary axis. The spacing kind defines how the spacing interacts with /// surrounding spacing. - AddSpacing(Length, SpacingKind, GenericAxis), + AddSpacing(f64, SpacingKind, GenericAxis), /// Start a new line. BreakLine, diff --git a/src/layout/stack.rs b/src/layout/stack.rs index 20d99fa6..2dd67ea9 100644 --- a/src/layout/stack.rs +++ b/src/layout/stack.rs @@ -22,7 +22,7 @@ //! sentence in the second box. use smallvec::smallvec; -use crate::length::Value4; +use crate::geom::Value4; use super::*; /// Performs the stack layouting. @@ -128,12 +128,12 @@ impl StackLayouter { } /// Add secondary spacing to the stack. - pub fn add_spacing(&mut self, mut spacing: Length, kind: SpacingKind) { + pub fn add_spacing(&mut self, mut spacing: f64, kind: SpacingKind) { match kind { // A hard space is simply an empty box. SpacingKind::Hard => { // Reduce the spacing such that it definitely fits. - spacing.min_eq(self.space.usable.secondary(self.ctx.axes)); + spacing = spacing.min(self.space.usable.secondary(self.ctx.axes)); let dimensions = Size::with_y(spacing); self.update_metrics(dimensions); @@ -170,11 +170,11 @@ impl StackLayouter { let mut size = self.space.size.generalized(axes); let mut extra = self.space.extra.generalized(axes); - size.x += (dimensions.x - extra.x).max(Length::ZERO); - size.y += (dimensions.y - extra.y).max(Length::ZERO); + size.x += (dimensions.x - extra.x).max(0.0); + size.y += (dimensions.y - extra.y).max(0.0); - extra.x.max_eq(dimensions.x); - extra.y = (extra.y - dimensions.y).max(Length::ZERO); + extra.x = extra.x.max(dimensions.x); + extra.y = (extra.y - dimensions.y).max(0.0); self.space.size = size.specialized(axes); self.space.extra = extra.specialized(axes); @@ -348,7 +348,7 @@ impl StackLayouter { // is reset for this new axis-aligned run. if rotation != axes.secondary.axis() { extent.y = extent.x; - extent.x = Length::ZERO; + extent.x = 0.0; rotation = axes.secondary.axis(); } @@ -360,7 +360,7 @@ impl StackLayouter { // Then, we add this layout's secondary extent to the accumulator. let size = layout.dimensions.generalized(*axes); - extent.x.max_eq(size.x); + extent.x = extent.x.max(size.x); extent.y += size.y; } diff --git a/src/layout/text.rs b/src/layout/text.rs index 22616667..30995be0 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -6,7 +6,7 @@ use fontdock::{FaceId, FaceQuery, FontStyle}; use crate::font::SharedFontLoader; -use crate::length::{Length, Size}; +use crate::geom::Size; use crate::style::TextStyle; use super::*; @@ -18,7 +18,7 @@ struct TextLayouter<'a> { actions: LayoutActions, buffer: String, active_font: FaceId, - width: Length, + width: f64, } /// The context for text layouting. @@ -51,7 +51,7 @@ impl<'a> TextLayouter<'a> { actions: LayoutActions::new(), buffer: String::new(), active_font: FaceId::MAX, - width: Length::ZERO, + width: 0.0, } } @@ -107,7 +107,7 @@ impl<'a> TextLayouter<'a> { /// Select the best font for a character and return its index along with /// the width of the char in the font. - async fn select_font(&mut self, c: char) -> Option<(FaceId, Length)> { + async fn select_font(&mut self, c: char) -> Option<(FaceId, f64)> { let mut loader = self.ctx.loader.borrow_mut(); let mut variant = self.ctx.style.variant; @@ -132,14 +132,13 @@ impl<'a> TextLayouter<'a> { if let Some((id, face)) = loader.query(query).await { // Determine the width of the char. - let units_per_em = face.units_per_em().unwrap_or(1000); - let ratio = 1.0 / (units_per_em as f64); - let to_length = |x| Length::pt(ratio * x as f64); + let units_per_em = face.units_per_em().unwrap_or(1000) as f64; + let ratio = 1.0 / units_per_em; + let to_raw = |x| ratio * x as f64; let glyph = face.glyph_index(c)?; let glyph_width = face.glyph_hor_advance(glyph)?; - let char_width = to_length(glyph_width) - * self.ctx.style.font_size().to_pt(); + let char_width = to_raw(glyph_width) * self.ctx.style.font_size(); Some((id, char_width)) } else { |
