Rename secondary/primary to main/cross ✏

This commit is contained in:
Laurenz 2020-10-06 13:13:18 +02:00
parent 5a7a32a9ba
commit c6a6870978
10 changed files with 403 additions and 360 deletions

View File

@ -4,7 +4,7 @@ use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, Font
use super::Scope; use super::Scope;
use crate::geom::{Insets, Linear, Size}; use crate::geom::{Insets, Linear, Size};
use crate::layout::{Dir, GenAlign, LayoutAlign, LayoutSystem, Sides}; use crate::layout::{Dir, Gen2, GenAlign, Sides};
use crate::length::Length; use crate::length::Length;
use crate::paper::{Paper, PaperClass, PAPER_A4}; use crate::paper::{Paper, PaperClass, PAPER_A4};
@ -17,10 +17,10 @@ pub struct State {
pub text: TextState, pub text: TextState,
/// The page state. /// The page state.
pub page: PageState, pub page: PageState,
/// The active layouting system. /// The active layouting directions.
pub sys: LayoutSystem, pub dirs: Gen2<Dir>,
/// The active alignments. /// The active alignments.
pub align: LayoutAlign, pub aligns: Gen2<GenAlign>,
} }
impl Default for State { impl Default for State {
@ -29,8 +29,8 @@ impl Default for State {
scope: crate::library::_std(), scope: crate::library::_std(),
text: TextState::default(), text: TextState::default(),
page: PageState::default(), page: PageState::default(),
sys: LayoutSystem::new(Dir::LTR, Dir::TTB), dirs: Gen2::new(Dir::TTB, Dir::LTR),
align: LayoutAlign::new(GenAlign::Start, GenAlign::Start), aligns: Gen2::new(GenAlign::Start, GenAlign::Start),
} }
} }
} }

View File

@ -6,7 +6,7 @@ pub use kurbo::*;
use std::fmt::{self, Debug, Formatter}; use std::fmt::{self, Debug, Formatter};
use std::ops::*; use std::ops::*;
use crate::layout::primitive::{Dir, GenAlign, LayoutAlign, LayoutSystem, SpecAxis}; use crate::layout::primitive::{Dir, Gen2, GenAlign, Side, SpecAxis};
/// Additional methods for [sizes]. /// Additional methods for [sizes].
/// ///
@ -18,26 +18,28 @@ pub trait SizeExt {
/// Borrow the component for the specified axis mutably. /// Borrow the component for the specified axis mutably.
fn get_mut(&mut self, axis: SpecAxis) -> &mut f64; fn get_mut(&mut self, axis: SpecAxis) -> &mut f64;
/// Returns the generalized version of a `Size` based on the layouting /// Returns the generalized version of a `Size` based on the current
/// system, that is: /// directions.
/// - `x` describes the primary axis instead of the horizontal one. ///
/// - `y` describes the secondary axis instead of the vertical one. /// In the generalized version:
fn generalized(self, sys: LayoutSystem) -> Self; /// - `x` describes the cross axis instead of the horizontal one.
/// - `y` describes the main axis instead of the vertical one.
fn generalized(self, dirs: Gen2<Dir>) -> Self;
/// Returns the specialized version of this generalized Size2D (inverse to /// Returns the specialized version of this generalized `Size` (inverse to
/// `generalized`). /// `generalized`).
fn specialized(self, sys: LayoutSystem) -> Self; fn specialized(self, dirs: Gen2<Dir>) -> Self;
/// Whether the given size fits into this one, that is, both coordinate /// Whether the given size fits into this one, that is, both coordinate
/// values are smaller or equal. /// values are smaller or equal.
fn fits(self, other: Self) -> bool; fn fits(self, other: Self) -> bool;
/// The anchor position for an object to be aligned according to `align` in /// The anchor position for an object to be aligned in a container with this
/// a container with this size. /// size and the given directions.
/// ///
/// This assumes the size to be generalized such that `width` corresponds to /// This assumes the size to be generalized such that `width` corresponds to
/// the primary and `height` to the secondary axis. /// the cross and `height` to the main axis.
fn anchor(self, align: LayoutAlign, sys: LayoutSystem) -> Point; fn anchor(self, dirs: Gen2<Dir>, aligns: Gen2<GenAlign>) -> Point;
} }
impl SizeExt for Size { impl SizeExt for Size {
@ -55,25 +57,25 @@ impl SizeExt for Size {
} }
} }
fn generalized(self, sys: LayoutSystem) -> Self { fn generalized(self, dirs: Gen2<Dir>) -> Self {
match sys.primary.axis() { match dirs.main.axis() {
SpecAxis::Horizontal => self, SpecAxis::Horizontal => Self::new(self.height, self.width),
SpecAxis::Vertical => Self::new(self.height, self.width), SpecAxis::Vertical => self,
} }
} }
fn specialized(self, sys: LayoutSystem) -> Self { fn specialized(self, dirs: Gen2<Dir>) -> Self {
// Even though generalized is its own inverse, we still have this second // Even though generalized is its own inverse, we still have this second
// function, for clarity at the call-site. // function, for clarity at the call-site.
self.generalized(sys) self.generalized(dirs)
} }
fn fits(self, other: Self) -> bool { fn fits(self, other: Self) -> bool {
self.width >= other.width && self.height >= other.height self.width >= other.width && self.height >= other.height
} }
fn anchor(self, align: LayoutAlign, sys: LayoutSystem) -> Point { fn anchor(self, dirs: Gen2<Dir>, aligns: Gen2<GenAlign>) -> Point {
fn anchor(length: f64, align: GenAlign, dir: Dir) -> f64 { fn anchor(length: f64, dir: Dir, align: GenAlign) -> f64 {
match (dir.is_positive(), align) { match (dir.is_positive(), align) {
(true, GenAlign::Start) | (false, GenAlign::End) => 0.0, (true, GenAlign::Start) | (false, GenAlign::End) => 0.0,
(_, GenAlign::Center) => length / 2.0, (_, GenAlign::Center) => length / 2.0,
@ -82,8 +84,8 @@ impl SizeExt for Size {
} }
Point::new( Point::new(
anchor(self.width, align.primary, sys.primary), anchor(self.width, dirs.cross, aligns.cross),
anchor(self.height, align.secondary, sys.secondary), anchor(self.height, dirs.main, aligns.main),
) )
} }
} }
@ -92,34 +94,29 @@ impl SizeExt for Size {
/// ///
/// [rectangles]: ../../kurbo/struct.Rect.html /// [rectangles]: ../../kurbo/struct.Rect.html
pub trait RectExt { pub trait RectExt {
/// Return the side identified by direction and alignment. /// Return the value for the given side.
/// fn get(self, side: Side) -> f64;
/// Center alignment is treated the same as origin alignment.
fn get(&mut self, dir: Dir, align: GenAlign) -> f64;
/// Get a mutable reference to the side identified by direction and /// Borrow the value for the given side mutably.
/// alignment. fn get_mut(&mut self, side: Side) -> &mut f64;
///
/// Center alignment is treated the same as origin alignment.
fn get_mut(&mut self, dir: Dir, align: GenAlign) -> &mut f64;
} }
impl RectExt for Rect { impl RectExt for Rect {
fn get(&mut self, dir: Dir, align: GenAlign) -> f64 { fn get(self, side: Side) -> f64 {
match if align == GenAlign::End { dir.inv() } else { dir } { match side {
Dir::LTR => self.x0, Side::Left => self.x0,
Dir::TTB => self.y0, Side::Top => self.y0,
Dir::RTL => self.x1, Side::Right => self.x1,
Dir::BTT => self.y1, Side::Bottom => self.y1,
} }
} }
fn get_mut(&mut self, dir: Dir, align: GenAlign) -> &mut f64 { fn get_mut(&mut self, side: Side) -> &mut f64 {
match if align == GenAlign::End { dir.inv() } else { dir } { match side {
Dir::LTR => &mut self.x0, Side::Left => &mut self.x0,
Dir::TTB => &mut self.y0, Side::Top => &mut self.y0,
Dir::RTL => &mut self.x1, Side::Right => &mut self.x1,
Dir::BTT => &mut self.y1, Side::Bottom => &mut self.y1,
} }
} }
} }

View File

@ -1,9 +1,8 @@
//! Arranging boxes into lines. //! Arranging boxes into lines.
//! //!
//! Along the primary axis, the boxes are laid out next to each other as long as //! The boxes are laid out along the cross axis as long as they fit into a line.
//! they fit into a line. When necessary, a line break is inserted and the new //! When necessary, a line break is inserted and the new line is offset along
//! line is offset along the secondary axis by the height of the previous line //! the main axis by the height of the previous line plus extra line spacing.
//! plus extra line spacing.
//! //!
//! Internally, the line layouter uses a stack layouter to stack the finished //! Internally, the line layouter uses a stack layouter to stack the finished
//! lines on top of each. //! lines on top of each.
@ -23,8 +22,8 @@ pub struct LineLayouter {
/// The context for line layouting. /// The context for line layouting.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LineContext { pub struct LineContext {
/// The initial layouting system, which can be updated through `set_sys`. /// The layout directions.
pub sys: LayoutSystem, pub dirs: Gen2<Dir>,
/// The spaces to layout into. /// The spaces to layout into.
pub spaces: Vec<LayoutSpace>, pub spaces: Vec<LayoutSpace>,
/// Whether to spill over into copies of the last space or finish layouting /// Whether to spill over into copies of the last space or finish layouting
@ -40,7 +39,7 @@ impl LineLayouter {
Self { Self {
stack: StackLayouter::new(StackContext { stack: StackLayouter::new(StackContext {
spaces: ctx.spaces.clone(), spaces: ctx.spaces.clone(),
sys: ctx.sys, dirs: ctx.dirs,
repeat: ctx.repeat, repeat: ctx.repeat,
}), }),
ctx, ctx,
@ -49,26 +48,24 @@ impl LineLayouter {
} }
/// Add a layout. /// Add a layout.
pub fn add(&mut self, layout: BoxLayout, align: LayoutAlign) { pub fn add(&mut self, layout: BoxLayout, aligns: Gen2<GenAlign>) {
let sys = self.ctx.sys; if let Some(prev) = self.run.aligns {
if aligns.main != prev.main {
if let Some(prev) = self.run.align {
if align.secondary != prev.secondary {
// TODO: Issue warning for non-fitting alignment in // TODO: Issue warning for non-fitting alignment in
// non-repeating context. // non-repeating context.
let fitting = self.stack.is_fitting_alignment(align); let fitting = self.stack.is_fitting_alignment(aligns);
if !fitting && self.ctx.repeat { if !fitting && self.ctx.repeat {
self.finish_space(true); self.finish_space(true);
} else { } else {
self.finish_line(); self.finish_line();
} }
} else if align.primary < prev.primary { } else if aligns.cross < prev.cross {
self.finish_line(); self.finish_line();
} else if align.primary > prev.primary { } else if aligns.cross > prev.cross {
let mut rest_run = LineRun::new(); let mut rest_run = LineRun::new();
let usable = self.stack.usable().get(sys.primary.axis()); let usable = self.stack.usable().get(self.ctx.dirs.cross.axis());
rest_run.usable = Some(match align.primary { rest_run.usable = Some(match aligns.cross {
GenAlign::Start => unreachable!("start > x"), GenAlign::Start => unreachable!("start > x"),
GenAlign::Center => usable - 2.0 * self.run.size.width, GenAlign::Center => usable - 2.0 * self.run.size.width,
GenAlign::End => usable - self.run.size.width, GenAlign::End => usable - self.run.size.width,
@ -84,10 +81,10 @@ impl LineLayouter {
} }
if let LastSpacing::Soft(spacing, _) = self.run.last_spacing { if let LastSpacing::Soft(spacing, _) = self.run.last_spacing {
self.add_primary_spacing(spacing, SpacingKind::Hard); self.add_cross_spacing(spacing, SpacingKind::Hard);
} }
let size = layout.size.generalized(sys); let size = layout.size.generalized(self.ctx.dirs);
if !self.usable().fits(size) { if !self.usable().fits(size) {
if !self.line_is_empty() { if !self.line_is_empty() {
@ -100,7 +97,7 @@ impl LineLayouter {
} }
} }
self.run.align = Some(align); self.run.aligns = Some(aligns);
self.run.layouts.push((self.run.size.width, layout)); self.run.layouts.push((self.run.size.width, layout));
self.run.size.width += size.width; self.run.size.width += size.width;
@ -114,19 +111,25 @@ impl LineLayouter {
/// needed. /// needed.
fn usable(&self) -> Size { fn usable(&self) -> Size {
// The base is the usable space of the stack layouter. // The base is the usable space of the stack layouter.
let mut usable = self.stack.usable().generalized(self.ctx.sys); let mut usable = self.stack.usable().generalized(self.ctx.dirs);
// If there was another run already, override the stack's size. // If there was another run already, override the stack's size.
if let Some(primary) = self.run.usable { if let Some(cross) = self.run.usable {
usable.width = primary; usable.width = cross;
} }
usable.width -= self.run.size.width; usable.width -= self.run.size.width;
usable usable
} }
/// Finish the line and add spacing to the underlying stack.
pub fn add_main_spacing(&mut self, spacing: f64, kind: SpacingKind) {
self.finish_line_if_not_empty();
self.stack.add_spacing(spacing, kind)
}
/// Add spacing to the line. /// Add spacing to the line.
pub fn add_primary_spacing(&mut self, mut spacing: f64, kind: SpacingKind) { pub fn add_cross_spacing(&mut self, mut spacing: f64, kind: SpacingKind) {
match kind { match kind {
SpacingKind::Hard => { SpacingKind::Hard => {
spacing = spacing.min(self.usable().width); spacing = spacing.min(self.usable().width);
@ -150,19 +153,6 @@ impl LineLayouter {
} }
} }
/// Finish the line and add spacing to the underlying stack.
pub fn add_secondary_spacing(&mut self, spacing: f64, kind: SpacingKind) {
self.finish_line_if_not_empty();
self.stack.add_spacing(spacing, kind)
}
/// Update the layouting system.
pub fn set_sys(&mut self, sys: LayoutSystem) {
self.finish_line_if_not_empty();
self.ctx.sys = sys;
self.stack.set_sys(sys)
}
/// Update the layouting spaces. /// Update the layouting spaces.
/// ///
/// If `replace_empty` is true, the current space is replaced if there are /// If `replace_empty` is true, the current space is replaced if there are
@ -181,7 +171,7 @@ impl LineLayouter {
/// it will fit into this layouter's underlying stack. /// it will fit into this layouter's underlying stack.
pub fn remaining(&self) -> Vec<LayoutSpace> { pub fn remaining(&self) -> Vec<LayoutSpace> {
let mut spaces = self.stack.remaining(); let mut spaces = self.stack.remaining();
*spaces[0].size.get_mut(self.ctx.sys.secondary.axis()) -= self.run.size.height; *spaces[0].size.get_mut(self.ctx.dirs.main.axis()) -= self.run.size.height;
spaces spaces
} }
@ -206,17 +196,17 @@ impl LineLayouter {
/// Finish the active line and start a new one. /// Finish the active line and start a new one.
pub fn finish_line(&mut self) { pub fn finish_line(&mut self) {
let mut layout = BoxLayout::new(self.run.size.specialized(self.ctx.sys)); let mut layout = BoxLayout::new(self.run.size.specialized(self.ctx.dirs));
let align = self.run.align.unwrap_or_default(); let aligns = self.run.aligns.unwrap_or_default();
let layouts = std::mem::take(&mut self.run.layouts); let layouts = std::mem::take(&mut self.run.layouts);
for (offset, child) in layouts { for (offset, child) in layouts {
let x = match self.ctx.sys.primary.is_positive() { let x = match self.ctx.dirs.cross.is_positive() {
true => offset, true => offset,
false => { false => {
self.run.size.width self.run.size.width
- offset - offset
- child.size.get(self.ctx.sys.primary.axis()) - child.size.get(self.ctx.dirs.cross.axis())
} }
}; };
@ -224,7 +214,7 @@ impl LineLayouter {
layout.push_layout(pos, child); layout.push_layout(pos, child);
} }
self.stack.add(layout, align); self.stack.add(layout, aligns);
self.run = LineRun::new(); self.run = LineRun::new();
self.stack.add_spacing(self.ctx.line_spacing, SpacingKind::LINE); self.stack.add_spacing(self.ctx.line_spacing, SpacingKind::LINE);
@ -249,7 +239,7 @@ struct LineRun {
/// When a new run is created the alignment is yet to be determined and /// When a new run is created the alignment is yet to be determined and
/// `None` as such. Once a layout is added, its alignment decides the /// `None` as such. Once a layout is added, its alignment decides the
/// alignment for the whole run. /// alignment for the whole run.
align: Option<LayoutAlign>, aligns: Option<Gen2<GenAlign>>,
/// The amount of space left by another run on the same line or `None` if /// The amount of space left by another run on the same line or `None` if
/// this is the only run so far. /// this is the only run so far.
usable: Option<f64>, usable: Option<f64>,
@ -263,7 +253,7 @@ impl LineRun {
Self { Self {
layouts: vec![], layouts: vec![],
size: Size::ZERO, size: Size::ZERO,
align: None, aligns: None,
usable: None, usable: None,
last_spacing: LastSpacing::Hard, last_spacing: LastSpacing::Hard,
} }

View File

@ -11,11 +11,10 @@ pub use primitive::*;
pub use stack::*; pub use stack::*;
pub use tree::*; pub use tree::*;
use crate::geom::{Insets, Point, Rect, RectExt, Size, SizeExt};
use crate::diag::Diag; use crate::diag::Diag;
use crate::eval::{PageState, State, TextState}; use crate::eval::{PageState, State, TextState};
use crate::font::SharedFontLoader; use crate::font::SharedFontLoader;
use crate::geom::{Insets, Point, Rect, RectExt, Size, SizeExt};
use crate::shaping::Shaped; use crate::shaping::Shaped;
use crate::syntax::{Deco, Spanned, SynTree}; use crate::syntax::{Deco, Spanned, SynTree};
use crate::{Feedback, Pass}; use crate::{Feedback, Pass};
@ -29,7 +28,7 @@ pub async fn layout(
let space = LayoutSpace { let space = LayoutSpace {
size: state.page.size, size: state.page.size,
insets: state.page.insets(), insets: state.page.insets(),
expansion: LayoutExpansion::new(true, true), expansion: Spec2::new(true, true),
}; };
let constraints = LayoutConstraints { let constraints = LayoutConstraints {
@ -134,7 +133,7 @@ pub struct LayoutSpace {
pub insets: Insets, pub insets: Insets,
/// Whether to expand the size of the resulting layout to the full size of /// Whether to expand the size of the resulting layout to the full size of
/// this space or to shrink it to fit the content. /// this space or to shrink it to fit the content.
pub expansion: LayoutExpansion, pub expansion: Spec2<bool>,
} }
impl LayoutSpace { impl LayoutSpace {
@ -154,7 +153,7 @@ impl LayoutSpace {
Self { Self {
size: self.usable(), size: self.usable(),
insets: Insets::ZERO, insets: Insets::ZERO,
expansion: LayoutExpansion::new(false, false), expansion: Spec2::new(false, false),
} }
} }
} }
@ -172,8 +171,8 @@ pub enum Command {
LayoutSyntaxTree(SynTree), LayoutSyntaxTree(SynTree),
/// Add a finished layout. /// Add a finished layout.
Add(BoxLayout, LayoutAlign), Add(BoxLayout, Gen2<GenAlign>),
/// Add spacing of the given kind along the primary or secondary axis. The /// Add spacing of the given kind along the given axis. The
/// kind defines how the spacing interacts with surrounding spacing. /// kind defines how the spacing interacts with surrounding spacing.
AddSpacing(f64, SpacingKind, GenAxis), AddSpacing(f64, SpacingKind, GenAxis),
@ -187,11 +186,8 @@ pub enum Command {
SetTextState(TextState), SetTextState(TextState),
/// Update the page style. /// Update the page style.
SetPageState(PageState), SetPageState(PageState),
/// Update the layouting system along which future boxes will be laid
/// out. This ends the current line.
SetSystem(LayoutSystem),
/// Update the alignment for future boxes added to this layouting process. /// Update the alignment for future boxes added to this layouting process.
SetAlignment(LayoutAlign), SetAlignment(Gen2<GenAlign>),
} }
/// Defines how spacing interacts with surrounding spacing. /// Defines how spacing interacts with surrounding spacing.

View File

@ -2,30 +2,6 @@
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
/// Specifies the directions into which content is laid out.
///
/// The primary component defines into which direction text and lines flow and the
/// secondary into which paragraphs and pages grow.
pub type LayoutSystem = Gen2<Dir>;
impl Default for LayoutSystem {
fn default() -> Self {
Self::new(Dir::LTR, Dir::TTB)
}
}
/// Specifies where to align a layout in a parent container.
pub type LayoutAlign = Gen2<GenAlign>;
impl Default for LayoutAlign {
fn default() -> Self {
Self::new(GenAlign::Start, GenAlign::Start)
}
}
/// Whether to expand a layout to an area's full size or shrink it to fit its content.
pub type LayoutExpansion = Spec2<bool>;
/// The four directions into which content can be laid out. /// The four directions into which content can be laid out.
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Dir { pub enum Dir {
@ -66,6 +42,23 @@ impl Dir {
if self.is_positive() { 1.0 } else { -1.0 } if self.is_positive() { 1.0 } else { -1.0 }
} }
/// The side of this direction the alignment identifies.
///
/// `Center` alignment is treated the same as `Start` alignment.
pub fn side(self, align: GenAlign) -> Side {
let start = match self {
Self::LTR => Side::Left,
Self::RTL => Side::Right,
Self::TTB => Side::Top,
Self::BTT => Side::Bottom,
};
match align {
GenAlign::Start | GenAlign::Center => start,
GenAlign::End => start.inv(),
}
}
/// The inverse direction. /// The inverse direction.
pub fn inv(self) -> Self { pub fn inv(self) -> Self {
match self { match self {
@ -75,18 +68,6 @@ impl Dir {
Self::BTT => Self::TTB, Self::BTT => Self::TTB,
} }
} }
/// The side of this direction the alignment identifies.
///
/// `Center` alignment is treated the same as `Start` alignment.
pub fn side(self, align: GenAlign) -> Side {
match if align == GenAlign::End { self.inv() } else { self } {
Self::LTR => Side::Left,
Self::RTL => Side::Right,
Self::TTB => Side::Top,
Self::BTT => Side::Bottom,
}
}
} }
impl Display for Dir { impl Display for Dir {
@ -100,27 +81,159 @@ impl Display for Dir {
} }
} }
/// Convert a type into its generic representation.
///
/// The generic representation deals with main and cross axes while the specific
/// representation deals with horizontal and vertical axes.
///
/// See also [`ToSpec`] for the inverse conversion.
///
/// [`ToSpec`]: trait.ToSpec.html
pub trait ToGen {
/// The generic version of this type.
type Output;
/// The generic version of this type based on the current directions.
fn to_gen(self, dirs: Gen2<Dir>) -> Self::Output;
}
/// Convert a type into its specific representation.
///
/// The specific representation deals with horizontal and vertical axes while
/// the generic representation deals with main and cross axes.
///
/// See also [`ToGen`] for the inverse conversion.
///
/// [`ToGen`]: trait.ToGen.html
pub trait ToSpec {
/// The specific version of this type.
type Output;
/// The specific version of this type based on the current directions.
fn to_spec(self, dirs: Gen2<Dir>) -> Self::Output;
}
/// A generic container with two components for the two generic axes.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Gen2<T> {
/// The main component.
pub main: T,
/// The cross component.
pub cross: T,
}
impl<T> Gen2<T> {
/// Create a new instance from the two components.
pub fn new(main: T, cross: T) -> Self {
Self { main, cross }
}
/// Return the component for the specified generic axis.
pub fn get(self, axis: GenAxis) -> T {
match axis {
GenAxis::Main => self.main,
GenAxis::Cross => self.cross,
}
}
/// Borrow the component for the specified generic axis mutably.
pub fn get_mut(&mut self, axis: GenAxis) -> &mut T {
match axis {
GenAxis::Main => &mut self.main,
GenAxis::Cross => &mut self.cross,
}
}
}
impl<T> ToSpec for Gen2<T> {
type Output = Spec2<T>;
fn to_spec(self, dirs: Gen2<Dir>) -> Self::Output {
match dirs.main.axis() {
SpecAxis::Horizontal => Spec2::new(self.main, self.cross),
SpecAxis::Vertical => Spec2::new(self.cross, self.main),
}
}
}
/// A generic container with two components for the two specific axes.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Spec2<T> {
/// The horizontal component.
pub horizontal: T,
/// The vertical component.
pub vertical: T,
}
impl<T> Spec2<T> {
/// Create a new instance from the two components.
pub fn new(horizontal: T, vertical: T) -> Self {
Self { horizontal, vertical }
}
/// Return the component for the given specific axis.
pub fn get(self, axis: SpecAxis) -> T {
match axis {
SpecAxis::Horizontal => self.horizontal,
SpecAxis::Vertical => self.vertical,
}
}
/// Borrow the component for the given specific axis mutably.
pub fn get_mut(&mut self, axis: SpecAxis) -> &mut T {
match axis {
SpecAxis::Horizontal => &mut self.horizontal,
SpecAxis::Vertical => &mut self.vertical,
}
}
}
impl<T> ToGen for Spec2<T> {
type Output = Gen2<T>;
fn to_gen(self, dirs: Gen2<Dir>) -> Self::Output {
match dirs.main.axis() {
SpecAxis::Horizontal => Gen2::new(self.horizontal, self.vertical),
SpecAxis::Vertical => Gen2::new(self.vertical, self.horizontal),
}
}
}
/// The two generic layouting axes. /// The two generic layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum GenAxis { pub enum GenAxis {
/// The primary layouting direction into which text and lines flow. /// The axis pages and paragraphs are set along.
Primary, Main,
/// The secondary layouting direction into which paragraphs grow. /// The axis words and lines are set along.
Secondary, Cross,
} }
impl GenAxis { impl GenAxis {
/// The specific version of this axis in the given layout system. /// The other axis.
pub fn to_spec(self, sys: LayoutSystem) -> SpecAxis { pub fn other(self) -> Self {
sys.get(self).axis() match self {
Self::Main => Self::Cross,
Self::Cross => Self::Main,
}
}
}
impl ToSpec for GenAxis {
type Output = SpecAxis;
fn to_spec(self, dirs: Gen2<Dir>) -> Self::Output {
match self {
Self::Main => dirs.main.axis(),
Self::Cross => dirs.cross.axis(),
}
} }
} }
impl Display for GenAxis { impl Display for GenAxis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self { f.pad(match self {
Self::Primary => "primary", Self::Main => "main",
Self::Secondary => "secondary", Self::Cross => "cross",
}) })
} }
} }
@ -128,19 +241,31 @@ impl Display for GenAxis {
/// The two specific layouting axes. /// The two specific layouting axes.
#[derive(Debug, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpecAxis { pub enum SpecAxis {
/// The horizontal layouting axis.
Horizontal,
/// The vertical layouting axis. /// The vertical layouting axis.
Vertical, Vertical,
/// The horizontal layouting axis.
Horizontal,
} }
impl SpecAxis { impl SpecAxis {
/// The generic version of this axis in the given layout system. /// The other axis.
pub fn to_gen(self, sys: LayoutSystem) -> GenAxis { pub fn other(self) -> Self {
if self == sys.primary.axis() { match self {
GenAxis::Primary Self::Horizontal => Self::Vertical,
Self::Vertical => Self::Horizontal,
}
}
}
impl ToGen for SpecAxis {
type Output = GenAxis;
fn to_gen(self, dirs: Gen2<Dir>) -> Self::Output {
if self == dirs.main.axis() {
GenAxis::Main
} else { } else {
GenAxis::Secondary debug_assert_eq!(self, dirs.cross.axis());
GenAxis::Cross
} }
} }
} }
@ -148,8 +273,8 @@ impl SpecAxis {
impl Display for SpecAxis { impl Display for SpecAxis {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self { f.pad(match self {
Self::Horizontal => "horizontal",
Self::Vertical => "vertical", Self::Vertical => "vertical",
Self::Horizontal => "horizontal",
}) })
} }
} }
@ -173,6 +298,12 @@ impl GenAlign {
} }
} }
impl Default for GenAlign {
fn default() -> Self {
Self::Start
}
}
impl Display for GenAlign { impl Display for GenAlign {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.pad(match self { f.pad(match self {
@ -207,21 +338,36 @@ impl SpecAlign {
} }
} }
/// The generic version of this alignment in the given layout system. /// The inverse alignment.
pub fn to_gen(self, sys: LayoutSystem) -> GenAlign { pub fn inv(self) -> Self {
let get = |spec: SpecAxis, positive: GenAlign| { match self {
if sys.get(spec.to_gen(sys)).is_positive() { Self::Left => Self::Right,
positive Self::Right => Self::Left,
Self::Top => Self::Bottom,
Self::Bottom => Self::Top,
Self::Center => Self::Center,
}
}
}
impl ToGen for SpecAlign {
type Output = GenAlign;
fn to_gen(self, dirs: Gen2<Dir>) -> Self::Output {
let dirs = dirs.to_spec(dirs);
let get = |dir: Dir, at_positive_start| {
if dir.is_positive() == at_positive_start {
GenAlign::Start
} else { } else {
positive.inv() GenAlign::End
} }
}; };
match self { match self {
Self::Left => get(SpecAxis::Horizontal, GenAlign::Start), Self::Left => get(dirs.horizontal, true),
Self::Right => get(SpecAxis::Horizontal, GenAlign::End), Self::Right => get(dirs.horizontal, false),
Self::Top => get(SpecAxis::Vertical, GenAlign::Start), Self::Top => get(dirs.vertical, true),
Self::Bottom => get(SpecAxis::Vertical, GenAlign::End), Self::Bottom => get(dirs.vertical, false),
Self::Center => GenAlign::Center, Self::Center => GenAlign::Center,
} }
} }
@ -239,91 +385,6 @@ impl Display for SpecAlign {
} }
} }
/// A side of a container.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Side {
Left,
Top,
Right,
Bottom,
}
impl Side {
/// The opposite side.
pub fn inv(self) -> Self {
match self {
Self::Left => Self::Right,
Self::Top => Self::Bottom,
Self::Right => Self::Left,
Self::Bottom => Self::Top,
}
}
}
/// A generic container with two components for the two generic axes.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Gen2<T> {
/// The primary component.
pub primary: T,
/// The secondary component.
pub secondary: T,
}
impl<T> Gen2<T> {
/// Create a new instance from the two components.
pub fn new(primary: T, secondary: T) -> Self {
Self { primary, secondary }
}
/// Return the component for the specified generic axis.
pub fn get(self, axis: GenAxis) -> T {
match axis {
GenAxis::Primary => self.primary,
GenAxis::Secondary => self.secondary,
}
}
/// Borrow the component for the specified generic axis mutably.
pub fn get_mut(&mut self, axis: GenAxis) -> &mut T {
match axis {
GenAxis::Primary => &mut self.primary,
GenAxis::Secondary => &mut self.secondary,
}
}
}
/// A generic container with two components for the two specific axes.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Spec2<T> {
/// The horizontal component.
pub horizontal: T,
/// The vertical component.
pub vertical: T,
}
impl<T> Spec2<T> {
/// Create a new instance from the two components.
pub fn new(horizontal: T, vertical: T) -> Self {
Self { horizontal, vertical }
}
/// Return the component for the given specific axis.
pub fn get(self, axis: SpecAxis) -> T {
match axis {
SpecAxis::Horizontal => self.horizontal,
SpecAxis::Vertical => self.vertical,
}
}
/// Borrow the component for the given specific axis mutably.
pub fn get_mut(&mut self, axis: SpecAxis) -> &mut T {
match axis {
SpecAxis::Horizontal => &mut self.horizontal,
SpecAxis::Vertical => &mut self.vertical,
}
}
}
/// A generic container with left, top, right and bottom components. /// A generic container with left, top, right and bottom components.
#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)] #[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub struct Sides<T> { pub struct Sides<T> {
@ -360,8 +421,8 @@ impl<T> Sides<T> {
pub fn get(self, side: Side) -> T { pub fn get(self, side: Side) -> T {
match side { match side {
Side::Left => self.left, Side::Left => self.left,
Side::Right => self.right,
Side::Top => self.top, Side::Top => self.top,
Side::Right => self.right,
Side::Bottom => self.bottom, Side::Bottom => self.bottom,
} }
} }
@ -370,9 +431,30 @@ impl<T> Sides<T> {
pub fn get_mut(&mut self, side: Side) -> &mut T { pub fn get_mut(&mut self, side: Side) -> &mut T {
match side { match side {
Side::Left => &mut self.left, Side::Left => &mut self.left,
Side::Right => &mut self.right,
Side::Top => &mut self.top, Side::Top => &mut self.top,
Side::Right => &mut self.right,
Side::Bottom => &mut self.bottom, Side::Bottom => &mut self.bottom,
} }
} }
} }
/// A side of a container.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Side {
Left,
Top,
Right,
Bottom,
}
impl Side {
/// The opposite side.
pub fn inv(self) -> Self {
match self {
Self::Left => Self::Right,
Self::Top => Self::Bottom,
Self::Right => Self::Left,
Self::Bottom => Self::Top,
}
}
}

View File

@ -1,4 +1,4 @@
//! Arranging boxes into a stack along the secondary axis. //! Arranging boxes into a stack along the main axis.
//! //!
//! Individual layouts can be aligned at `Start`, `Center` or `End` along both //! Individual layouts can be aligned at `Start`, `Center` or `End` along both
//! axes. These alignments are with respect to the size of the finished layout //! axes. These alignments are with respect to the size of the finished layout
@ -34,8 +34,8 @@ pub struct StackLayouter {
/// The context for stack layouting. /// The context for stack layouting.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StackContext { pub struct StackContext {
/// The initial layouting system, which can be updated through `set_sys`. /// The layouting directions.
pub sys: LayoutSystem, pub dirs: Gen2<Dir>,
/// The spaces to layout into. /// The spaces to layout into.
pub spaces: Vec<LayoutSpace>, pub spaces: Vec<LayoutSpace>,
/// Whether to spill over into copies of the last space or finish layouting /// Whether to spill over into copies of the last space or finish layouting
@ -55,15 +55,15 @@ impl StackLayouter {
} }
/// Add a layout to the stack. /// Add a layout to the stack.
pub fn add(&mut self, layout: BoxLayout, align: LayoutAlign) { pub fn add(&mut self, layout: BoxLayout, aligns: Gen2<GenAlign>) {
// If the alignment cannot be fitted in this space, finish it. // If the alignment cannot be fitted in this space, finish it.
// TODO: Issue warning for non-fitting alignment in non-repeating // TODO: Issue warning for non-fitting alignment in non-repeating
// context. // context.
if !self.update_rulers(align) && self.ctx.repeat { if !self.update_rulers(aligns) && self.ctx.repeat {
self.finish_space(true); self.finish_space(true);
} }
// Now, we add a possibly cached soft space. If the secondary alignment // Now, we add a possibly cached soft space. If the main alignment
// changed before, a possibly cached space would have already been // changed before, a possibly cached space would have already been
// discarded. // discarded.
if let LastSpacing::Soft(spacing, _) = self.space.last_spacing { if let LastSpacing::Soft(spacing, _) = self.space.last_spacing {
@ -76,11 +76,11 @@ impl StackLayouter {
} }
// Change the usable space and size of the space. // Change the usable space and size of the space.
self.update_metrics(layout.size.generalized(self.ctx.sys)); self.update_metrics(layout.size.generalized(self.ctx.dirs));
// Add the box to the vector and remember that spacings are allowed // Add the box to the vector and remember that spacings are allowed
// again. // again.
self.space.layouts.push((self.ctx.sys, align, layout)); self.space.layouts.push((self.ctx.dirs, aligns, layout));
self.space.last_spacing = LastSpacing::None; self.space.last_spacing = LastSpacing::None;
} }
@ -90,15 +90,15 @@ impl StackLayouter {
// A hard space is simply an empty box. // A hard space is simply an empty box.
SpacingKind::Hard => { SpacingKind::Hard => {
// Reduce the spacing such that it definitely fits. // Reduce the spacing such that it definitely fits.
let axis = self.ctx.sys.secondary.axis(); let axis = self.ctx.dirs.main.axis();
spacing = spacing.min(self.space.usable.get(axis)); spacing = spacing.min(self.space.usable.get(axis));
let size = Size::new(0.0, spacing); let size = Size::new(0.0, spacing);
self.update_metrics(size); self.update_metrics(size);
self.space.layouts.push(( self.space.layouts.push((
self.ctx.sys, self.ctx.dirs,
LayoutAlign::default(), Gen2::default(),
BoxLayout::new(size.specialized(self.ctx.sys)), BoxLayout::new(size.specialized(self.ctx.dirs)),
)); ));
self.space.last_spacing = LastSpacing::Hard; self.space.last_spacing = LastSpacing::Hard;
@ -121,37 +121,34 @@ impl StackLayouter {
} }
fn update_metrics(&mut self, added: Size) { fn update_metrics(&mut self, added: Size) {
let sys = self.ctx.sys; let mut size = self.space.size.generalized(self.ctx.dirs);
let mut extra = self.space.extra.generalized(self.ctx.dirs);
let mut size = self.space.size.generalized(sys);
let mut extra = self.space.extra.generalized(sys);
size.width += (added.width - extra.width).max(0.0); size.width += (added.width - extra.width).max(0.0);
size.height += (added.height - extra.height).max(0.0); size.height += (added.height - extra.height).max(0.0);
extra.width = extra.width.max(added.width); extra.width = extra.width.max(added.width);
extra.height = (extra.height - added.height).max(0.0); extra.height = (extra.height - added.height).max(0.0);
self.space.size = size.specialized(sys); self.space.size = size.specialized(self.ctx.dirs);
self.space.extra = extra.specialized(sys); self.space.extra = extra.specialized(self.ctx.dirs);
*self.space.usable.get_mut(sys.secondary.axis()) -= added.height; *self.space.usable.get_mut(self.ctx.dirs.main.axis()) -= added.height;
} }
/// Returns true if a space break is necessary. /// Returns true if a space break is necessary.
fn update_rulers(&mut self, align: LayoutAlign) -> bool { fn update_rulers(&mut self, aligns: Gen2<GenAlign>) -> bool {
let allowed = self.is_fitting_alignment(align); let allowed = self.is_fitting_alignment(aligns);
if allowed { if allowed {
let side = self.ctx.sys.secondary.side(GenAlign::Start); let side = self.ctx.dirs.main.side(GenAlign::Start);
*self.space.rulers.get_mut(side) = align.secondary; *self.space.rulers.get_mut(side) = aligns.main;
} }
allowed allowed
} }
/// Whether a layout with the given alignment can still be layouted into the /// Whether a layout with the given alignment can still be layouted into the
/// active space or a space break is necessary. /// active space or a space break is necessary.
pub(crate) fn is_fitting_alignment(&self, align: LayoutAlign) -> bool { pub(crate) fn is_fitting_alignment(&self, aligns: Gen2<GenAlign>) -> bool {
self.is_fitting_axis(self.ctx.sys.primary, align.primary) self.is_fitting_axis(self.ctx.dirs.main, aligns.main)
&& self.is_fitting_axis(self.ctx.sys.secondary, align.secondary) && self.is_fitting_axis(self.ctx.dirs.cross, aligns.cross)
} }
fn is_fitting_axis(&self, dir: Dir, align: GenAlign) -> bool { fn is_fitting_axis(&self, dir: Dir, align: GenAlign) -> bool {
@ -159,16 +156,6 @@ impl StackLayouter {
&& align <= self.space.rulers.get(dir.side(GenAlign::End)).inv() && align <= self.space.rulers.get(dir.side(GenAlign::End)).inv()
} }
/// Update the layouting system.
pub fn set_sys(&mut self, sys: LayoutSystem) {
// Forget the spacing because it is not relevant anymore.
if sys.secondary != self.ctx.sys.secondary {
self.space.last_spacing = LastSpacing::Hard;
}
self.ctx.sys = sys;
}
/// Update the layouting spaces. /// Update the layouting spaces.
/// ///
/// If `replace_empty` is true, the current space is replaced if there are /// If `replace_empty` is true, the current space is replaced if there are
@ -200,12 +187,10 @@ impl StackLayouter {
/// The remaining inner spaces. If something is laid out into these spaces, /// The remaining inner spaces. If something is laid out into these spaces,
/// it will fit into this stack. /// it will fit into this stack.
pub fn remaining(&self) -> Vec<LayoutSpace> { pub fn remaining(&self) -> Vec<LayoutSpace> {
let size = self.usable();
let mut spaces = vec![LayoutSpace { let mut spaces = vec![LayoutSpace {
size, size: self.usable(),
insets: Insets::ZERO, insets: Insets::ZERO,
expansion: LayoutExpansion::new(false, false), expansion: Spec2::new(false, false),
}]; }];
for space in &self.ctx.spaces[self.next_space() ..] { for space in &self.ctx.spaces[self.next_space() ..] {
@ -219,7 +204,7 @@ impl StackLayouter {
pub fn usable(&self) -> Size { pub fn usable(&self) -> Size {
self.space.usable self.space.usable
- Size::new(0.0, self.space.last_spacing.soft_or_zero()) - Size::new(0.0, self.space.last_spacing.soft_or_zero())
.specialized(self.ctx.sys) .specialized(self.ctx.dirs)
} }
/// Whether the current layout space is empty. /// Whether the current layout space is empty.
@ -274,7 +259,7 @@ impl StackLayouter {
y1: start.y + self.space.size.height, y1: start.y + self.space.size.height,
}; };
for (sys, _, layout) in &self.space.layouts { for &(dirs, _, ref layout) in &self.space.layouts {
// First, we store the bounds calculated so far (which were reduced // First, we store the bounds calculated so far (which were reduced
// by the predecessors of this layout) as the initial bounding box // by the predecessors of this layout) as the initial bounding box
// of this layout. // of this layout.
@ -283,41 +268,42 @@ impl StackLayouter {
// Then, we reduce the bounding box for the following layouts. This // Then, we reduce the bounding box for the following layouts. This
// layout uses up space from the origin to the end. Thus, it reduces // layout uses up space from the origin to the end. Thus, it reduces
// the usable space for following layouts at its origin by its // the usable space for following layouts at its origin by its
// extent along the secondary axis. // main-axis extent.
*bound.get_mut(sys.secondary, GenAlign::Start) += *bound.get_mut(dirs.main.side(GenAlign::Start)) +=
sys.secondary.factor() * layout.size.get(sys.secondary.axis()); dirs.main.factor() * layout.size.get(dirs.main.axis());
} }
// ------------------------------------------------------------------ // // ------------------------------------------------------------------ //
// Step 3: Backward pass. Reduce the bounding boxes from the previous // Step 3: Backward pass. Reduce the bounding boxes from the previous
// layouts by what is taken by the following ones. // layouts by what is taken by the following ones.
// The `x` field stores the maximal primary extent in one axis-aligned // The `x` field stores the maximal cross-axis extent in one
// run, while the `y` fields stores the accumulated secondary extent. // axis-aligned run, while the `y` fields stores the accumulated
// main-axis extent.
let mut extent = Size::ZERO; let mut extent = Size::ZERO;
let mut rotation = SpecAxis::Vertical; let mut rotation = SpecAxis::Vertical;
for (bound, entry) in bounds.iter_mut().zip(&self.space.layouts).rev() { for (bound, entry) in bounds.iter_mut().zip(&self.space.layouts).rev() {
let (sys, _, layout) = entry; let &(dirs, _, ref layout) = entry;
// When the axes are rotated, the maximal primary size (`extent.x`) // When the axes are rotated, the maximal cross-axis size
// dictates how much secondary extent the whole run had. This value // (`extent.x`) dictates how much main-axis extent the whole run
// is thus stored in `extent.y`. The primary extent is reset for // had. This value is thus stored in `extent.y`. The cross-axis
// this new axis-aligned run. // extent is reset for this new axis-aligned run.
if rotation != sys.secondary.axis() { if rotation != dirs.main.axis() {
extent.height = extent.width; extent.height = extent.width;
extent.width = 0.0; extent.width = 0.0;
rotation = sys.secondary.axis(); rotation = dirs.main.axis();
} }
// We reduce the bounding box of this layout at its end by the // We reduce the bounding box of this layout at its end by the
// accumulated secondary extent of all layouts we have seen so far, // accumulated main-axis extent of all layouts we have seen so far,
// which are the layouts after this one since we iterate reversed. // which are the layouts after this one since we iterate reversed.
*bound.get_mut(sys.secondary, GenAlign::End) -= *bound.get_mut(dirs.main.side(GenAlign::End)) -=
sys.secondary.factor() * extent.height; dirs.main.factor() * extent.height;
// Then, we add this layout's secondary extent to the accumulator. // Then, we add this layout's main-axis extent to the accumulator.
let size = layout.size.generalized(*sys); let size = layout.size.generalized(dirs);
extent.width = extent.width.max(size.width); extent.width = extent.width.max(size.width);
extent.height += size.height; extent.height += size.height;
} }
@ -329,14 +315,14 @@ impl StackLayouter {
let mut layout = BoxLayout::new(size); let mut layout = BoxLayout::new(size);
let layouts = std::mem::take(&mut self.space.layouts); let layouts = std::mem::take(&mut self.space.layouts);
for ((sys, align, child), bound) in layouts.into_iter().zip(bounds) { for ((dirs, aligns, child), bound) in layouts.into_iter().zip(bounds) {
let size = child.size.specialized(sys); let size = child.size.specialized(dirs);
// The space in which this layout is aligned is given by the // The space in which this layout is aligned is given by the
// distances between the borders of its bounding box. // distances between the borders of its bounding box.
let usable = bound.size().generalized(sys); let usable = bound.size().generalized(dirs);
let local = usable.anchor(align, sys) - size.anchor(align, sys); let local = usable.anchor(dirs, aligns) - size.anchor(dirs, aligns);
let pos = bound.origin() + local.to_size().specialized(sys).to_vec2(); let pos = bound.origin() + local.to_size().specialized(dirs).to_vec2();
layout.push_layout(pos, child); layout.push_layout(pos, child);
} }
@ -359,7 +345,7 @@ impl StackLayouter {
} }
} }
/// A layout space composed of subspaces which can have different systems and /// A layout space composed of subspaces which can have different directions and
/// alignments. /// alignments.
struct Space { struct Space {
/// The index of this space in `ctx.spaces`. /// The index of this space in `ctx.spaces`.
@ -367,7 +353,7 @@ struct Space {
/// Whether to include a layout for this space even if it would be empty. /// Whether to include a layout for this space even if it would be empty.
hard: bool, hard: bool,
/// The so-far accumulated layouts. /// The so-far accumulated layouts.
layouts: Vec<(LayoutSystem, LayoutAlign, BoxLayout)>, layouts: Vec<(Gen2<Dir>, Gen2<GenAlign>, BoxLayout)>,
/// The specialized size of this space. /// The specialized size of this space.
size: Size, size: Size,
/// The specialized remaining space. /// The specialized remaining space.

View File

@ -26,7 +26,7 @@ impl<'a> TreeLayouter<'a> {
fn new(ctx: &'a mut LayoutContext) -> Self { fn new(ctx: &'a mut LayoutContext) -> Self {
let layouter = LineLayouter::new(LineContext { let layouter = LineLayouter::new(LineContext {
spaces: ctx.constraints.spaces.clone(), spaces: ctx.constraints.spaces.clone(),
sys: ctx.state.sys, dirs: ctx.state.dirs,
repeat: ctx.constraints.repeat, repeat: ctx.constraints.repeat,
line_spacing: ctx.state.text.line_spacing(), line_spacing: ctx.state.text.line_spacing(),
}); });
@ -89,14 +89,12 @@ impl<'a> TreeLayouter<'a> {
fn layout_space(&mut self) { fn layout_space(&mut self) {
self.layouter self.layouter
.add_primary_spacing(self.ctx.state.text.word_spacing(), SpacingKind::WORD); .add_cross_spacing(self.ctx.state.text.word_spacing(), SpacingKind::WORD);
} }
fn layout_parbreak(&mut self) { fn layout_parbreak(&mut self) {
self.layouter.add_secondary_spacing( self.layouter
self.ctx.state.text.par_spacing(), .add_main_spacing(self.ctx.state.text.par_spacing(), SpacingKind::PARAGRAPH);
SpacingKind::PARAGRAPH,
);
} }
async fn layout_text(&mut self, text: &str) { async fn layout_text(&mut self, text: &str) {
@ -117,14 +115,14 @@ impl<'a> TreeLayouter<'a> {
let boxed = shaping::shape( let boxed = shaping::shape(
text, text,
self.ctx.state.text.font_size(), self.ctx.state.text.font_size(),
self.ctx.state.sys.primary, self.ctx.state.dirs.cross,
&mut self.ctx.loader.borrow_mut(), &mut self.ctx.loader.borrow_mut(),
&self.ctx.state.text.fallback, &self.ctx.state.text.fallback,
variant, variant,
) )
.await; .await;
self.layouter.add(boxed, self.ctx.state.align); self.layouter.add(boxed, self.ctx.state.aligns);
} }
async fn layout_heading(&mut self, heading: &NodeHeading) { async fn layout_heading(&mut self, heading: &NodeHeading) {
@ -187,10 +185,10 @@ impl<'a> TreeLayouter<'a> {
match command { match command {
LayoutSyntaxTree(tree) => self.layout_tree(&tree).await, LayoutSyntaxTree(tree) => self.layout_tree(&tree).await,
Add(layout, align) => self.layouter.add(layout, align), Add(layout, aligns) => self.layouter.add(layout, aligns),
AddSpacing(space, kind, axis) => match axis { AddSpacing(space, kind, axis) => match axis {
GenAxis::Primary => self.layouter.add_primary_spacing(space, kind), GenAxis::Main => self.layouter.add_main_spacing(space, kind),
GenAxis::Secondary => self.layouter.add_secondary_spacing(space, kind), GenAxis::Cross => self.layouter.add_cross_spacing(space, kind),
}, },
BreakLine => self.layouter.finish_line(), BreakLine => self.layouter.finish_line(),
@ -219,7 +217,7 @@ impl<'a> TreeLayouter<'a> {
let space = LayoutSpace { let space = LayoutSpace {
size: style.size, size: style.size,
insets: style.insets(), insets: style.insets(),
expansion: LayoutExpansion::new(true, true), expansion: Spec2::new(true, true),
}; };
self.constraints.base = space.usable(); self.constraints.base = space.usable();
self.layouter.set_spaces(vec![space], true); self.layouter.set_spaces(vec![space], true);
@ -230,12 +228,7 @@ impl<'a> TreeLayouter<'a> {
)); ));
} }
} }
SetAlignment(aligns) => self.ctx.state.aligns = aligns,
SetAlignment(align) => self.ctx.state.align = align,
SetSystem(sys) => {
self.layouter.set_sys(sys);
self.ctx.state.sys = sys;
}
} }
} }
} }

View File

@ -35,7 +35,7 @@ pub async fn align(mut args: Args, ctx: &mut LayoutContext) -> Value {
Some(tree) => vec![ Some(tree) => vec![
SetAlignment(aligns), SetAlignment(aligns),
LayoutSyntaxTree(tree), LayoutSyntaxTree(tree),
SetAlignment(ctx.state.align), SetAlignment(ctx.state.aligns),
], ],
None => vec![SetAlignment(aligns)], None => vec![SetAlignment(aligns)],
}) })
@ -45,8 +45,8 @@ pub async fn align(mut args: Args, ctx: &mut LayoutContext) -> Value {
fn dedup_aligns( fn dedup_aligns(
ctx: &mut LayoutContext, ctx: &mut LayoutContext,
iter: impl Iterator<Item = (Option<SpecAxis>, Spanned<SpecAlign>)>, iter: impl Iterator<Item = (Option<SpecAxis>, Spanned<SpecAlign>)>,
) -> LayoutAlign { ) -> Gen2<GenAlign> {
let mut aligns = ctx.state.align; let mut aligns = ctx.state.aligns;
let mut had = Gen2::new(false, false); let mut had = Gen2::new(false, false);
let mut had_center = false; let mut had_center = false;
@ -54,8 +54,8 @@ fn dedup_aligns(
// Check whether we know which axis this alignment belongs to. // Check whether we know which axis this alignment belongs to.
if let Some(axis) = axis { if let Some(axis) = axis {
// We know the axis. // We know the axis.
let gen_axis = axis.to_gen(ctx.state.sys); let gen_axis = axis.to_gen(ctx.state.dirs);
let gen_align = align.to_gen(ctx.state.sys); let gen_align = align.to_gen(ctx.state.dirs);
if align.axis().map_or(false, |a| a != axis) { if align.axis().map_or(false, |a| a != axis) {
ctx.diag(error!( ctx.diag(error!(
@ -73,14 +73,13 @@ fn dedup_aligns(
// positional argument. // positional argument.
debug_assert_eq!(align, SpecAlign::Center); debug_assert_eq!(align, SpecAlign::Center);
if had.primary && had.secondary { if had.main && had.cross {
ctx.diag(error!(span, "duplicate alignment")); ctx.diag(error!(span, "duplicate alignment"));
} else if had_center { } else if had_center {
// Both this and the previous one are unspecified `center` // Both this and the previous one are unspecified `center`
// alignments. Both axes should be centered. // alignments. Both axes should be centered.
aligns = LayoutAlign::new(GenAlign::Center, GenAlign::Center); aligns = Gen2::new(GenAlign::Center, GenAlign::Center);
had.primary = true; had = Gen2::new(true, true);
had.secondary = true;
} else { } else {
had_center = true; had_center = true;
} }
@ -88,22 +87,22 @@ fn dedup_aligns(
// If we we know one alignment, we can handle the unspecified `center` // If we we know one alignment, we can handle the unspecified `center`
// alignment. // alignment.
if had_center && (had.primary || had.secondary) { if had_center && (had.main || had.cross) {
if had.primary { if had.main {
aligns.secondary = GenAlign::Center; aligns.cross = GenAlign::Center;
had.secondary = true; had.cross = true;
} else { } else {
aligns.primary = GenAlign::Center; aligns.main = GenAlign::Center;
had.primary = true; had.main = true;
} }
had_center = false; had_center = false;
} }
} }
// If center has not been flushed by now, it is the only argument and then // If center has not been flushed by now, it is the only argument and then
// we default to applying it to the primary axis. // we default to applying it to the cross axis.
if had_center { if had_center {
aligns.primary = GenAlign::Center; aligns.cross = GenAlign::Center;
} }
aligns aligns

View File

@ -12,7 +12,7 @@ pub async fn boxed(mut args: Args, ctx: &mut LayoutContext) -> Value {
let height = args.get::<_, Linear>(ctx, "height"); let height = args.get::<_, Linear>(ctx, "height");
args.done(ctx); args.done(ctx);
let align = ctx.state.align; let aligns = ctx.state.aligns;
let constraints = &mut ctx.constraints; let constraints = &mut ctx.constraints;
constraints.base = constraints.spaces[0].size; constraints.base = constraints.spaces[0].size;
constraints.spaces.truncate(1); constraints.spaces.truncate(1);
@ -35,5 +35,5 @@ pub async fn boxed(mut args: Args, ctx: &mut LayoutContext) -> Value {
let layouted = layout_tree(&body, ctx).await; let layouted = layout_tree(&body, ctx).await;
let layout = layouted.into_iter().next().unwrap(); let layout = layouted.into_iter().next().unwrap();
Value::Commands(vec![Add(layout, align)]) Value::Commands(vec![Add(layout, aligns)])
} }

View File

@ -24,7 +24,7 @@ fn spacing(mut args: Args, ctx: &mut LayoutContext, axis: SpecAxis) -> Value {
Value::Commands(if let Some(spacing) = spacing { Value::Commands(if let Some(spacing) = spacing {
let spacing = spacing.eval(ctx.state.text.font_size()); let spacing = spacing.eval(ctx.state.text.font_size());
let axis = axis.to_gen(ctx.state.sys); let axis = axis.to_gen(ctx.state.dirs);
vec![AddSpacing(spacing, SpacingKind::Hard, axis)] vec![AddSpacing(spacing, SpacingKind::Hard, axis)]
} else { } else {
vec![] vec![]