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

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

@ -5,7 +5,7 @@ use std::rc::Rc;
use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight};
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};
/// The active evaluation state.
@ -20,9 +20,9 @@ pub struct State {
/// The font state.
pub font: FontState,
/// The active layouting directions.
pub dirs: Gen<Dir>,
/// The active alignments.
pub aligns: Gen<Align>,
pub flow: Flow,
/// The active box alignments.
pub align: BoxAlign,
}
impl Default for State {
@ -32,8 +32,8 @@ impl Default for State {
page: PageState::default(),
par: ParState::default(),
font: FontState::default(),
dirs: Gen::new(Dir::TTB, Dir::LTR),
aligns: Gen::new(Align::Start, Align::Start),
flow: Flow::new(Dir::TTB, Dir::LTR),
align: BoxAlign::new(Align::Start, Align::Start),
}
}
}

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

@ -1,5 +1,8 @@
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.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Dir {

@ -14,6 +14,14 @@ impl<T> Gen<T> {
pub fn new(main: T, cross: T) -> Self {
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> {
@ -42,8 +50,8 @@ impl<T> Get<GenAxis> for Gen<T> {
impl<T> Switch for Gen<T> {
type Other = Spec<T>;
fn switch(self, dirs: Gen<Dir>) -> Self::Other {
match dirs.main.axis() {
fn switch(self, flow: Flow) -> Self::Other {
match flow.main.axis() {
SpecAxis::Horizontal => Spec::new(self.main, self.cross),
SpecAxis::Vertical => Spec::new(self.cross, self.main),
}
@ -72,10 +80,10 @@ impl GenAxis {
impl Switch for GenAxis {
type Other = SpecAxis;
fn switch(self, dirs: Gen<Dir>) -> Self::Other {
fn switch(self, flow: Flow) -> Self::Other {
match self {
Self::Main => dirs.main.axis(),
Self::Cross => dirs.cross.axis(),
Self::Main => flow.main.axis(),
Self::Cross => flow.cross.axis(),
}
}
}

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

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

@ -14,12 +14,12 @@ pub struct 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 {
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
where
T: Clone,

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

@ -14,6 +14,17 @@ impl<T> Spec<T> {
pub fn new(horizontal: T, vertical: T) -> Self {
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> {
@ -55,8 +66,8 @@ impl<T> Get<SpecAxis> for Spec<T> {
impl<T> Switch for Spec<T> {
type Other = Gen<T>;
fn switch(self, dirs: Gen<Dir>) -> Self::Other {
match dirs.main.axis() {
fn switch(self, flow: Flow) -> Self::Other {
match flow.main.axis() {
SpecAxis::Horizontal => Gen::new(self.horizontal, self.vertical),
SpecAxis::Vertical => Gen::new(self.vertical, self.horizontal),
}
@ -85,11 +96,11 @@ impl SpecAxis {
impl Switch for SpecAxis {
type Other = GenAxis;
fn switch(self, dirs: Gen<Dir>) -> Self::Other {
if self == dirs.main.axis() {
fn switch(self, flow: Flow) -> Self::Other {
if self == flow.main.axis() {
GenAxis::Main
} else {
debug_assert_eq!(self, dirs.cross.axis());
debug_assert_eq!(self, flow.cross.axis());
GenAxis::Cross
}
}

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

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

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

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

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

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

@ -26,7 +26,7 @@ fn spacing(mut args: Args, ctx: &mut EvalContext, axis: SpecAxis) -> Value {
if let Some(linear) = spacing {
let amount = linear.resolve(ctx.state.font.font_size());
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.push(spacing);
ctx.start_par_group();

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