BoxAlign and Flow aliases ✏

This commit is contained in:
Laurenz 2020-10-13 13:51:58 +02:00
parent 8680fcd490
commit 91e5120693
18 changed files with 150 additions and 124 deletions

View File

@ -22,7 +22,7 @@ use fontdock::FontStyle;
use crate::diag::Diag; use crate::diag::Diag;
use crate::diag::{Deco, Feedback, Pass}; use crate::diag::{Deco, Feedback, Pass};
use crate::geom::{Align, Dir, Gen, Length, Linear, Relative, Sides, Size}; use crate::geom::{BoxAlign, Flow, Gen, Length, Linear, Relative, Sides, Size};
use crate::layout::{ use crate::layout::{
Document, Expansion, LayoutNode, Pad, Pages, Par, Softness, Spacing, Stack, Text, Document, Expansion, LayoutNode, Pad, Pages, Par, Softness, Spacing, Stack, Text,
}; };
@ -122,8 +122,8 @@ impl EvalContext {
self.start_group(PageGroup { self.start_group(PageGroup {
size: self.state.page.size, size: self.state.page.size,
padding: self.state.page.margins(), padding: self.state.page.margins(),
dirs: self.state.dirs, flow: self.state.flow,
aligns: self.state.aligns, align: self.state.align,
hard, hard,
}); });
self.start_par_group(); self.start_par_group();
@ -141,9 +141,9 @@ impl EvalContext {
child: LayoutNode::dynamic(Pad { child: LayoutNode::dynamic(Pad {
padding: group.padding, padding: group.padding,
child: LayoutNode::dynamic(Stack { child: LayoutNode::dynamic(Stack {
dirs: group.dirs, flow: group.flow,
aligns: group.aligns, align: group.align,
expansion: Gen::new(Expansion::Fill, Expansion::Fill), expansion: Gen::uniform(Expansion::Fill),
children, children,
}), }),
}), }),
@ -171,8 +171,8 @@ impl EvalContext {
pub fn start_par_group(&mut self) { pub fn start_par_group(&mut self) {
let em = self.state.font.font_size(); let em = self.state.font.font_size();
self.start_group(ParGroup { self.start_group(ParGroup {
dirs: self.state.dirs, flow: self.state.flow,
aligns: self.state.aligns, align: self.state.align,
line_spacing: self.state.par.line_spacing.resolve(em), line_spacing: self.state.par.line_spacing.resolve(em),
}); });
} }
@ -185,8 +185,8 @@ impl EvalContext {
// better. // better.
let cross_expansion = Expansion::fill_if(self.groups.len() <= 1); let cross_expansion = Expansion::fill_if(self.groups.len() <= 1);
self.push(Par { self.push(Par {
dirs: group.dirs, flow: group.flow,
aligns: group.aligns, align: group.align,
cross_expansion, cross_expansion,
line_spacing: group.line_spacing, line_spacing: group.line_spacing,
children, children,
@ -242,11 +242,11 @@ impl EvalContext {
Text { Text {
text, text,
dir: self.state.dirs.cross, align: self.state.align,
dir: self.state.flow.cross,
font_size: self.state.font.font_size(), font_size: self.state.font.font_size(),
families: Rc::clone(&self.state.font.families), families: Rc::clone(&self.state.font.families),
variant, variant,
aligns: self.state.aligns,
} }
} }
} }
@ -255,8 +255,8 @@ impl EvalContext {
struct PageGroup { struct PageGroup {
size: Size, size: Size,
padding: Sides<Linear>, padding: Sides<Linear>,
dirs: Gen<Dir>, flow: Flow,
aligns: Gen<Align>, align: BoxAlign,
hard: bool, hard: bool,
} }
@ -265,8 +265,8 @@ struct ContentGroup;
/// A group for paragraphs. /// A group for paragraphs.
struct ParGroup { struct ParGroup {
dirs: Gen<Dir>, flow: Flow,
aligns: Gen<Align>, align: BoxAlign,
line_spacing: Length, line_spacing: Length,
} }
@ -370,9 +370,9 @@ impl Eval for NodeRaw {
} }
ctx.push(Stack { ctx.push(Stack {
dirs: ctx.state.dirs, flow: ctx.state.flow,
aligns: ctx.state.aligns, align: ctx.state.align,
expansion: Gen::new(Expansion::Fit, Expansion::Fit), expansion: Gen::uniform(Expansion::Fit),
children, children,
}); });

View File

@ -5,7 +5,7 @@ use std::rc::Rc;
use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight}; use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight};
use super::Scope; use super::Scope;
use crate::geom::{Align, Dir, Gen, Length, Linear, Relative, Sides, Size}; use crate::geom::{Align, BoxAlign, Dir, Flow, Length, Linear, Relative, Sides, Size};
use crate::paper::{Paper, PaperClass, PAPER_A4}; use crate::paper::{Paper, PaperClass, PAPER_A4};
/// The active evaluation state. /// The active evaluation state.
@ -20,9 +20,9 @@ pub struct State {
/// The font state. /// The font state.
pub font: FontState, pub font: FontState,
/// The active layouting directions. /// The active layouting directions.
pub dirs: Gen<Dir>, pub flow: Flow,
/// The active alignments. /// The active box alignments.
pub aligns: Gen<Align>, pub align: BoxAlign,
} }
impl Default for State { impl Default for State {
@ -32,8 +32,8 @@ impl Default for State {
page: PageState::default(), page: PageState::default(),
par: ParState::default(), par: ParState::default(),
font: FontState::default(), font: FontState::default(),
dirs: Gen::new(Dir::TTB, Dir::LTR), flow: Flow::new(Dir::TTB, Dir::LTR),
aligns: Gen::new(Align::Start, Align::Start), align: BoxAlign::new(Align::Start, Align::Start),
} }
} }
} }

View File

@ -1,5 +1,8 @@
use super::*; use super::*;
/// The alignment of a box in a container.
pub type BoxAlign = Gen<Align>;
/// Where to align something along a directed axis. /// Where to align something along a directed axis.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Align { pub enum Align {

View File

@ -1,5 +1,8 @@
use super::*; use super::*;
/// The directions along which content flows in a container.
pub type Flow = Gen<Dir>;
/// 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 {

View File

@ -14,6 +14,14 @@ impl<T> Gen<T> {
pub fn new(main: T, cross: T) -> Self { pub fn new(main: T, cross: T) -> Self {
Self { main, cross } Self { main, cross }
} }
/// Create a new instance with two equal components.
pub fn uniform(value: T) -> Self
where
T: Clone,
{
Self { main: value.clone(), cross: value }
}
} }
impl Gen<Length> { impl Gen<Length> {
@ -42,8 +50,8 @@ impl<T> Get<GenAxis> for Gen<T> {
impl<T> Switch for Gen<T> { impl<T> Switch for Gen<T> {
type Other = Spec<T>; type Other = Spec<T>;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
match dirs.main.axis() { match flow.main.axis() {
SpecAxis::Horizontal => Spec::new(self.main, self.cross), SpecAxis::Horizontal => Spec::new(self.main, self.cross),
SpecAxis::Vertical => Spec::new(self.cross, self.main), SpecAxis::Vertical => Spec::new(self.cross, self.main),
} }
@ -72,10 +80,10 @@ impl GenAxis {
impl Switch for GenAxis { impl Switch for GenAxis {
type Other = SpecAxis; type Other = SpecAxis;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
match self { match self {
Self::Main => dirs.main.axis(), Self::Main => flow.main.axis(),
Self::Cross => dirs.cross.axis(), Self::Cross => flow.cross.axis(),
} }
} }
} }

View File

@ -48,6 +48,7 @@ pub trait Switch {
/// The type of the other version. /// The type of the other version.
type Other; type Other;
/// The other version of this type based on the current directions. /// The other version of this type based on the current layouting
fn switch(self, dirs: Gen<Dir>) -> Self::Other; /// directions.
fn switch(self, flow: Flow) -> Self::Other;
} }

View File

@ -40,8 +40,8 @@ impl Get<SpecAxis> for Point {
impl Switch for Point { impl Switch for Point {
type Other = Gen<Length>; type Other = Gen<Length>;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
match dirs.main.axis() { match flow.main.axis() {
SpecAxis::Horizontal => Gen::new(self.x, self.y), SpecAxis::Horizontal => Gen::new(self.x, self.y),
SpecAxis::Vertical => Gen::new(self.y, self.x), SpecAxis::Vertical => Gen::new(self.y, self.x),
} }

View File

@ -14,12 +14,12 @@ pub struct Sides<T> {
} }
impl<T> Sides<T> { impl<T> Sides<T> {
/// Create a new box from four sizes. /// Create a new instance from the four components.
pub fn new(left: T, top: T, right: T, bottom: T) -> Self { pub fn new(left: T, top: T, right: T, bottom: T) -> Self {
Self { left, top, right, bottom } Self { left, top, right, bottom }
} }
/// Create an instance with all four components set to the same `value`. /// Create an instance with four equal components.
pub fn uniform(value: T) -> Self pub fn uniform(value: T) -> Self
where where
T: Clone, T: Clone,

View File

@ -48,8 +48,8 @@ impl Get<SpecAxis> for Size {
impl Switch for Size { impl Switch for Size {
type Other = Gen<Length>; type Other = Gen<Length>;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
match dirs.main.axis() { match flow.main.axis() {
SpecAxis::Horizontal => Gen::new(self.width, self.height), SpecAxis::Horizontal => Gen::new(self.width, self.height),
SpecAxis::Vertical => Gen::new(self.height, self.width), SpecAxis::Vertical => Gen::new(self.height, self.width),
} }

View File

@ -14,6 +14,17 @@ impl<T> Spec<T> {
pub fn new(horizontal: T, vertical: T) -> Self { pub fn new(horizontal: T, vertical: T) -> Self {
Self { horizontal, vertical } Self { horizontal, vertical }
} }
/// Create a new instance with two equal components.
pub fn uniform(value: T) -> Self
where
T: Clone,
{
Self {
horizontal: value.clone(),
vertical: value,
}
}
} }
impl Spec<Length> { impl Spec<Length> {
@ -55,8 +66,8 @@ impl<T> Get<SpecAxis> for Spec<T> {
impl<T> Switch for Spec<T> { impl<T> Switch for Spec<T> {
type Other = Gen<T>; type Other = Gen<T>;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
match dirs.main.axis() { match flow.main.axis() {
SpecAxis::Horizontal => Gen::new(self.horizontal, self.vertical), SpecAxis::Horizontal => Gen::new(self.horizontal, self.vertical),
SpecAxis::Vertical => Gen::new(self.vertical, self.horizontal), SpecAxis::Vertical => Gen::new(self.vertical, self.horizontal),
} }
@ -85,11 +96,11 @@ impl SpecAxis {
impl Switch for SpecAxis { impl Switch for SpecAxis {
type Other = GenAxis; type Other = GenAxis;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
if self == dirs.main.axis() { if self == flow.main.axis() {
GenAxis::Main GenAxis::Main
} else { } else {
debug_assert_eq!(self, dirs.cross.axis()); debug_assert_eq!(self, flow.cross.axis());
GenAxis::Cross GenAxis::Cross
} }
} }

View File

@ -128,9 +128,9 @@ pub enum Layouted {
/// Spacing that should be added to the parent. /// Spacing that should be added to the parent.
Spacing(Length), Spacing(Length),
/// A layout that should be added to and aligned in the parent. /// A layout that should be added to and aligned in the parent.
Layout(BoxLayout, Gen<Align>), Layout(BoxLayout, BoxAlign),
/// Multiple layouts. /// Multiple layouts.
Layouts(Vec<BoxLayout>, Gen<Align>), Layouts(Vec<BoxLayout>, BoxAlign),
} }
impl Layouted { impl Layouted {

View File

@ -7,15 +7,15 @@ pub struct Par {
/// ///
/// The children are placed in lines along the `cross` direction. The lines /// The children are placed in lines along the `cross` direction. The lines
/// are stacked along the `main` direction. /// are stacked along the `main` direction.
pub dirs: Gen<Dir>, pub flow: Flow,
/// How to align this paragraph in _its_ parent.
pub aligns: Gen<Align>,
/// Whether to expand the cross axis to fill the area or to fit the content. /// Whether to expand the cross axis to fill the area or to fit the content.
pub cross_expansion: Expansion, pub cross_expansion: Expansion,
/// The spacing to insert after each line. /// The spacing to insert after each line.
pub line_spacing: Length, pub line_spacing: Length,
/// The nodes to be arranged in a paragraph. /// The nodes to be arranged in a paragraph.
pub children: Vec<LayoutNode>, pub children: Vec<LayoutNode>,
/// How to align this paragraph in _its_ parent.
pub align: BoxAlign,
} }
impl Layout for Par { impl Layout for Par {
@ -24,17 +24,17 @@ impl Layout for Par {
for child in &self.children { for child in &self.children {
match child.layout(ctx, &layouter.areas) { match child.layout(ctx, &layouter.areas) {
Layouted::Spacing(spacing) => layouter.push_spacing(spacing), Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
Layouted::Layout(layout, aligns) => { Layouted::Layout(layout, align) => {
layouter.push_layout(layout, aligns.cross) layouter.push_layout(layout, align.cross)
} }
Layouted::Layouts(layouts, aligns) => { Layouted::Layouts(layouts, align) => {
for layout in layouts { for layout in layouts {
layouter.push_layout(layout, aligns.cross); layouter.push_layout(layout, align.cross);
} }
} }
} }
} }
Layouted::Layouts(layouter.finish(), self.aligns) Layouted::Layouts(layouter.finish(), self.align)
} }
} }
@ -48,7 +48,7 @@ struct ParLayouter<'a> {
par: &'a Par, par: &'a Par,
main: SpecAxis, main: SpecAxis,
cross: SpecAxis, cross: SpecAxis,
dirs: Gen<Dir>, flow: Flow,
areas: Areas, areas: Areas,
finished: Vec<BoxLayout>, finished: Vec<BoxLayout>,
lines: Vec<(Length, BoxLayout, Align)>, lines: Vec<(Length, BoxLayout, Align)>,
@ -62,9 +62,9 @@ impl<'a> ParLayouter<'a> {
fn new(par: &'a Par, areas: Areas) -> Self { fn new(par: &'a Par, areas: Areas) -> Self {
Self { Self {
par, par,
main: par.dirs.main.axis(), main: par.flow.main.axis(),
cross: par.dirs.cross.axis(), cross: par.flow.cross.axis(),
dirs: par.dirs, flow: par.flow,
areas, areas,
finished: vec![], finished: vec![],
lines: vec![], lines: vec![],
@ -105,7 +105,7 @@ impl<'a> ParLayouter<'a> {
} }
} }
let size = layout.size.switch(self.dirs); let size = layout.size.switch(self.flow);
self.run.push((self.run_size.cross, layout, align)); self.run.push((self.run_size.cross, layout, align));
self.run_size.cross += size.cross; self.run_size.cross += size.cross;
@ -119,13 +119,13 @@ impl<'a> ParLayouter<'a> {
Expansion::Fit => self.run_size.cross, Expansion::Fit => self.run_size.cross,
}); });
let mut output = BoxLayout::new(full_size.switch(self.dirs).to_size()); let mut output = BoxLayout::new(full_size.switch(self.flow).to_size());
for (before, layout, align) in std::mem::take(&mut self.run) { for (before, layout, align) in std::mem::take(&mut self.run) {
let child_cross_size = layout.size.get(self.cross); let child_cross_size = layout.size.get(self.cross);
// Position along the cross axis. // Position along the cross axis.
let cross = align.resolve(if self.dirs.cross.is_positive() { let cross = align.resolve(if self.flow.cross.is_positive() {
let after_with_self = self.run_size.cross - before; let after_with_self = self.run_size.cross - before;
before .. full_size.cross - after_with_self before .. full_size.cross - after_with_self
} else { } else {
@ -134,7 +134,7 @@ impl<'a> ParLayouter<'a> {
full_size.cross - before_with_self .. after full_size.cross - before_with_self .. after
}); });
let pos = Gen::new(Length::ZERO, cross).switch(self.dirs).to_point(); let pos = Gen::new(Length::ZERO, cross).switch(self.flow).to_point();
output.push_layout(pos, layout); output.push_layout(pos, layout);
} }
@ -151,26 +151,26 @@ impl<'a> ParLayouter<'a> {
fn finish_area(&mut self) { fn finish_area(&mut self) {
let size = self.lines_size; let size = self.lines_size;
let mut output = BoxLayout::new(size.switch(self.dirs).to_size()); let mut output = BoxLayout::new(size.switch(self.flow).to_size());
for (before, run, cross_align) in std::mem::take(&mut self.lines) { for (before, run, cross_align) in std::mem::take(&mut self.lines) {
let child_size = run.size.switch(self.dirs); let child_size = run.size.switch(self.flow);
// Position along the main axis. // Position along the main axis.
let main = if self.dirs.main.is_positive() { let main = if self.flow.main.is_positive() {
before before
} else { } else {
size.main - (before + child_size.main) size.main - (before + child_size.main)
}; };
// Align along the cross axis. // Align along the cross axis.
let cross = cross_align.resolve(if self.dirs.cross.is_positive() { let cross = cross_align.resolve(if self.flow.cross.is_positive() {
Length::ZERO .. size.cross - child_size.cross Length::ZERO .. size.cross - child_size.cross
} else { } else {
size.cross - child_size.cross .. Length::ZERO size.cross - child_size.cross .. Length::ZERO
}); });
let pos = Gen::new(main, cross).switch(self.dirs).to_point(); let pos = Gen::new(main, cross).switch(self.flow).to_point();
output.push_layout(pos, run); output.push_layout(pos, run);
} }

View File

@ -1,15 +1,15 @@
use super::*; use super::*;
/// A node that stacks and aligns its children. /// A node that stacks and align its children.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct Stack { pub struct Stack {
/// The `main` and `cross` directions of this stack. /// The `main` and `cross` directions of this stack.
/// ///
/// The children are stacked along the `main` direction. The `cross` /// The children are stacked along the `main` direction. The `cross`
/// direction is required for aligning the children. /// direction is required for aligning the children.
pub dirs: Gen<Dir>, pub flow: Flow,
/// How to align this stack in _its_ parent. /// How to align this stack in _its_ parent.
pub aligns: Gen<Align>, pub align: BoxAlign,
/// Whether to expand the axes to fill the area or to fit the content. /// Whether to expand the axes to fill the area or to fit the content.
pub expansion: Gen<Expansion>, pub expansion: Gen<Expansion>,
/// The nodes to be stacked. /// The nodes to be stacked.
@ -22,15 +22,15 @@ impl Layout for Stack {
for child in &self.children { for child in &self.children {
match child.layout(ctx, &layouter.areas) { match child.layout(ctx, &layouter.areas) {
Layouted::Spacing(spacing) => layouter.push_spacing(spacing), Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
Layouted::Layout(layout, aligns) => layouter.push_layout(layout, aligns), Layouted::Layout(layout, align) => layouter.push_layout(layout, align),
Layouted::Layouts(layouts, aligns) => { Layouted::Layouts(layouts, align) => {
for layout in layouts { for layout in layouts {
layouter.push_layout(layout, aligns); layouter.push_layout(layout, align);
} }
} }
} }
} }
Layouted::Layouts(layouter.finish(), self.aligns) Layouted::Layouts(layouter.finish(), self.align)
} }
} }
@ -43,10 +43,10 @@ impl From<Stack> for LayoutNode {
struct StackLayouter<'a> { struct StackLayouter<'a> {
stack: &'a Stack, stack: &'a Stack,
main: SpecAxis, main: SpecAxis,
dirs: Gen<Dir>, flow: Flow,
areas: Areas, areas: Areas,
finished: Vec<BoxLayout>, finished: Vec<BoxLayout>,
layouts: Vec<(Length, BoxLayout, Gen<Align>)>, layouts: Vec<(Length, BoxLayout, BoxAlign)>,
used: Gen<Length>, used: Gen<Length>,
ruler: Align, ruler: Align,
} }
@ -55,8 +55,8 @@ impl<'a> StackLayouter<'a> {
fn new(stack: &'a Stack, areas: Areas) -> Self { fn new(stack: &'a Stack, areas: Areas) -> Self {
Self { Self {
stack, stack,
main: stack.dirs.main.axis(), main: stack.flow.main.axis(),
dirs: stack.dirs, flow: stack.flow,
areas, areas,
finished: vec![], finished: vec![],
layouts: vec![], layouts: vec![],
@ -72,8 +72,8 @@ impl<'a> StackLayouter<'a> {
self.used.main += capped; self.used.main += capped;
} }
fn push_layout(&mut self, layout: BoxLayout, aligns: Gen<Align>) { fn push_layout(&mut self, layout: BoxLayout, align: BoxAlign) {
if self.ruler > aligns.main { if self.ruler > align.main {
self.finish_area(); self.finish_area();
} }
@ -87,18 +87,18 @@ impl<'a> StackLayouter<'a> {
} }
} }
let size = layout.size.switch(self.dirs); let size = layout.size.switch(self.flow);
self.layouts.push((self.used.main, layout, aligns)); self.layouts.push((self.used.main, layout, align));
*self.areas.current.rem.get_mut(self.main) -= size.main; *self.areas.current.rem.get_mut(self.main) -= size.main;
self.used.main += size.main; self.used.main += size.main;
self.used.cross = self.used.cross.max(size.cross); self.used.cross = self.used.cross.max(size.cross);
self.ruler = aligns.main; self.ruler = align.main;
} }
fn finish_area(&mut self) { fn finish_area(&mut self) {
let full_size = { let full_size = {
let full = self.areas.current.full.switch(self.dirs); let full = self.areas.current.full.switch(self.flow);
Gen::new( Gen::new(
match self.stack.expansion.main { match self.stack.expansion.main {
Expansion::Fill => full.main, Expansion::Fill => full.main,
@ -111,13 +111,13 @@ impl<'a> StackLayouter<'a> {
) )
}; };
let mut output = BoxLayout::new(full_size.switch(self.dirs).to_size()); let mut output = BoxLayout::new(full_size.switch(self.flow).to_size());
for (before, layout, aligns) in std::mem::take(&mut self.layouts) { for (before, layout, align) in std::mem::take(&mut self.layouts) {
let child_size = layout.size.switch(self.dirs); let child_size = layout.size.switch(self.flow);
// Align along the main axis. // Align along the main axis.
let main = aligns.main.resolve(if self.dirs.main.is_positive() { let main = align.main.resolve(if self.flow.main.is_positive() {
let after_with_self = self.used.main - before; let after_with_self = self.used.main - before;
before .. full_size.main - after_with_self before .. full_size.main - after_with_self
} else { } else {
@ -127,13 +127,13 @@ impl<'a> StackLayouter<'a> {
}); });
// Align along the cross axis. // Align along the cross axis.
let cross = aligns.cross.resolve(if self.dirs.cross.is_positive() { let cross = align.cross.resolve(if self.flow.cross.is_positive() {
Length::ZERO .. full_size.cross - child_size.cross Length::ZERO .. full_size.cross - child_size.cross
} else { } else {
full_size.cross - child_size.cross .. Length::ZERO full_size.cross - child_size.cross .. Length::ZERO
}); });
let pos = Gen::new(main, cross).switch(self.dirs).to_point(); let pos = Gen::new(main, cross).switch(self.flow).to_point();
output.push_layout(pos, layout); output.push_layout(pos, layout);
} }

View File

@ -11,16 +11,16 @@ use crate::shaping;
pub struct Text { pub struct Text {
/// The text. /// The text.
pub text: String, pub text: String,
/// The font size. /// How to align this text node in its parent.
pub font_size: Length, pub align: BoxAlign,
/// The text direction. /// The text direction.
pub dir: Dir, pub dir: Dir,
/// The font size.
pub font_size: Length,
/// The families used for font fallback. /// The families used for font fallback.
pub families: Rc<FallbackTree>, pub families: Rc<FallbackTree>,
/// The font variant, /// The font variant,
pub variant: FontVariant, pub variant: FontVariant,
/// How to align this text node in its parent.
pub aligns: Gen<Align>,
} }
impl Layout for Text { impl Layout for Text {
@ -30,12 +30,12 @@ impl Layout for Text {
shaping::shape( shaping::shape(
&mut loader, &mut loader,
&self.text, &self.text,
self.font_size,
self.dir, self.dir,
self.font_size,
&self.families, &self.families,
self.variant, self.variant,
), ),
self.aligns, self.align,
) )
} }
} }

View File

@ -32,13 +32,13 @@ pub fn align(mut args: Args, ctx: &mut EvalContext) -> Value {
.chain(hor.into_iter().map(|align| (Some(SpecAxis::Horizontal), align))) .chain(hor.into_iter().map(|align| (Some(SpecAxis::Horizontal), align)))
.chain(ver.into_iter().map(|align| (Some(SpecAxis::Vertical), align))); .chain(ver.into_iter().map(|align| (Some(SpecAxis::Vertical), align)));
let aligns = dedup_aligns(ctx, iter); let align = dedup_aligns(ctx, iter);
if aligns.main != ctx.state.aligns.main { if align.main != ctx.state.align.main {
ctx.end_par_group(); ctx.end_par_group();
ctx.start_par_group(); ctx.start_par_group();
} }
ctx.state.aligns = aligns; ctx.state.align = align;
if let Some(body) = body { if let Some(body) = body {
body.eval(ctx); body.eval(ctx);
@ -52,17 +52,17 @@ pub fn align(mut args: Args, ctx: &mut EvalContext) -> Value {
fn dedup_aligns( fn dedup_aligns(
ctx: &mut EvalContext, ctx: &mut EvalContext,
iter: impl Iterator<Item = (Option<SpecAxis>, Spanned<AlignArg>)>, iter: impl Iterator<Item = (Option<SpecAxis>, Spanned<AlignArg>)>,
) -> Gen<Align> { ) -> BoxAlign {
let mut aligns = ctx.state.aligns; let mut alignments = ctx.state.align;
let mut had = Gen::new(false, false); let mut had = Gen::uniform(false);
let mut had_center = false; let mut had_center = false;
for (axis, Spanned { v: align, span }) in iter { for (axis, Spanned { v: align, span }) in iter {
// 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.switch(ctx.state.dirs); let gen_axis = axis.switch(ctx.state.flow);
let gen_align = align.switch(ctx.state.dirs); let gen_align = align.switch(ctx.state.flow);
if align.axis().map_or(false, |a| a != axis) { if align.axis().map_or(false, |a| a != axis) {
ctx.diag(error!( ctx.diag(error!(
@ -72,7 +72,7 @@ fn dedup_aligns(
} else if had.get(gen_axis) { } else if had.get(gen_axis) {
ctx.diag(error!(span, "duplicate alignment for {} axis", axis)); ctx.diag(error!(span, "duplicate alignment for {} axis", axis));
} else { } else {
*aligns.get_mut(gen_axis) = gen_align; *alignments.get_mut(gen_axis) = gen_align;
*had.get_mut(gen_axis) = true; *had.get_mut(gen_axis) = true;
} }
} else { } else {
@ -85,8 +85,8 @@ fn dedup_aligns(
} 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 = Gen::new(Align::Center, Align::Center); alignments = BoxAlign::new(Align::Center, Align::Center);
had = Gen::new(true, true); had = Gen::uniform(true);
} else { } else {
had_center = true; had_center = true;
} }
@ -96,10 +96,10 @@ fn dedup_aligns(
// alignment. // alignment.
if had_center && (had.main || had.cross) { if had_center && (had.main || had.cross) {
if had.main { if had.main {
aligns.cross = Align::Center; alignments.cross = Align::Center;
had.cross = true; had.cross = true;
} else { } else {
aligns.main = Align::Center; alignments.main = Align::Center;
had.main = true; had.main = true;
} }
had_center = false; had_center = false;
@ -109,10 +109,10 @@ fn dedup_aligns(
// 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 cross axis. // we default to applying it to the cross axis.
if had_center { if had_center {
aligns.cross = Align::Center; alignments.cross = Align::Center;
} }
aligns alignments
} }
/// An alignment argument. /// An alignment argument.
@ -143,7 +143,7 @@ impl AlignArg {
impl Switch for AlignArg { impl Switch for AlignArg {
type Other = Align; type Other = Align;
fn switch(self, dirs: Gen<Dir>) -> Self::Other { fn switch(self, flow: Flow) -> Self::Other {
let get = |dir: Dir, at_positive_start| { let get = |dir: Dir, at_positive_start| {
if dir.is_positive() == at_positive_start { if dir.is_positive() == at_positive_start {
Align::Start Align::Start
@ -152,12 +152,12 @@ impl Switch for AlignArg {
} }
}; };
let dirs = dirs.switch(dirs); let flow = flow.switch(flow);
match self { match self {
Self::Left => get(dirs.horizontal, true), Self::Left => get(flow.horizontal, true),
Self::Right => get(dirs.horizontal, false), Self::Right => get(flow.horizontal, false),
Self::Top => get(dirs.vertical, true), Self::Top => get(flow.vertical, true),
Self::Bottom => get(dirs.vertical, false), Self::Bottom => get(flow.vertical, false),
Self::Center => Align::Center, Self::Center => Align::Center,
} }
} }

View File

@ -15,8 +15,8 @@ pub fn boxed(mut args: Args, ctx: &mut EvalContext) -> Value {
let height = args.get::<_, Linear>(ctx, "height"); let height = args.get::<_, Linear>(ctx, "height");
args.done(ctx); args.done(ctx);
let dirs = ctx.state.dirs; let flow = ctx.state.flow;
let aligns = ctx.state.aligns; let align = ctx.state.align;
ctx.start_content_group(); ctx.start_content_group();
body.eval(ctx); body.eval(ctx);
@ -26,14 +26,14 @@ pub fn boxed(mut args: Args, ctx: &mut EvalContext) -> Value {
width, width,
height, height,
child: LayoutNode::dynamic(Stack { child: LayoutNode::dynamic(Stack {
dirs, flow,
children, align,
aligns,
expansion: Spec::new( expansion: Spec::new(
Expansion::fill_if(width.is_some()), Expansion::fill_if(width.is_some()),
Expansion::fill_if(height.is_some()), Expansion::fill_if(height.is_some()),
) )
.switch(dirs), .switch(flow),
children,
}), }),
}); });

View File

@ -26,7 +26,7 @@ fn spacing(mut args: Args, ctx: &mut EvalContext, axis: SpecAxis) -> Value {
if let Some(linear) = spacing { if let Some(linear) = spacing {
let amount = linear.resolve(ctx.state.font.font_size()); let amount = linear.resolve(ctx.state.font.font_size());
let spacing = Spacing { amount, softness: Softness::Hard }; let spacing = Spacing { amount, softness: Softness::Hard };
if ctx.state.dirs.main.axis() == axis { if ctx.state.flow.main.axis() == axis {
ctx.end_par_group(); ctx.end_par_group();
ctx.push(spacing); ctx.push(spacing);
ctx.start_par_group(); ctx.start_par_group();

View File

@ -64,8 +64,8 @@ impl Debug for Shaped {
pub fn shape( pub fn shape(
loader: &mut FontLoader, loader: &mut FontLoader,
text: &str, text: &str,
font_size: Length,
dir: Dir, dir: Dir,
font_size: Length,
fallback: &FallbackTree, fallback: &FallbackTree,
variant: FontVariant, variant: FontVariant,
) -> BoxLayout { ) -> BoxLayout {