Rename ChildAlign to LayoutAligns ✏

This commit is contained in:
Laurenz 2021-03-12 14:12:30 +01:00
parent ffcb8cd97a
commit 584a43277d
11 changed files with 62 additions and 60 deletions

View File

@ -5,7 +5,7 @@ use fontdock::FontStyle;
use super::*; use super::*;
use crate::diag::{Diag, DiagSet}; use crate::diag::{Diag, DiagSet};
use crate::geom::{ChildAlign, Dir, Gen, LayoutDirs, Length, Linear, Sides, Size}; use crate::geom::{Dir, Gen, LayoutAligns, LayoutDirs, Length, Linear, Sides, Size};
use crate::layout::{ use crate::layout::{
Node, NodePad, NodePages, NodePar, NodeSpacing, NodeStack, NodeText, Tree, Node, NodePad, NodePages, NodePar, NodeSpacing, NodeStack, NodeText, Tree,
}; };
@ -108,7 +108,7 @@ impl<'a> ExecContext<'a> {
/// Execute a template and return the result as a stack node. /// Execute a template and return the result as a stack node.
pub fn exec(&mut self, template: &ValueTemplate) -> Node { pub fn exec(&mut self, template: &ValueTemplate) -> Node {
let dirs = self.state.dirs; let dirs = self.state.dirs;
let align = self.state.align; let aligns = self.state.aligns;
self.start_group(ContentGroup); self.start_group(ContentGroup);
self.start_par_group(); self.start_par_group();
@ -116,7 +116,7 @@ impl<'a> ExecContext<'a> {
self.end_par_group(); self.end_par_group();
let children = self.end_group::<ContentGroup>().1; let children = self.end_group::<ContentGroup>().1;
NodeStack { dirs, align, children }.into() NodeStack { dirs, aligns, children }.into()
} }
/// Start a page group based on the active page state. /// Start a page group based on the active page state.
@ -130,7 +130,7 @@ impl<'a> ExecContext<'a> {
size: self.state.page.size, size: self.state.page.size,
padding: self.state.page.margins(), padding: self.state.page.margins(),
dirs: self.state.dirs, dirs: self.state.dirs,
align: self.state.align, aligns: self.state.aligns,
softness, softness,
}); });
self.start_par_group(); self.start_par_group();
@ -156,7 +156,7 @@ impl<'a> ExecContext<'a> {
padding: group.padding, padding: group.padding,
child: NodeStack { child: NodeStack {
dirs: group.dirs, dirs: group.dirs,
align: group.align, aligns: group.aligns,
children, children,
} }
.into(), .into(),
@ -172,7 +172,7 @@ impl<'a> ExecContext<'a> {
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, dirs: self.state.dirs,
align: self.state.align, aligns: self.state.aligns,
line_spacing: self.state.par.line_spacing.resolve(em), line_spacing: self.state.par.line_spacing.resolve(em),
}); });
} }
@ -183,7 +183,7 @@ impl<'a> ExecContext<'a> {
if !children.is_empty() { if !children.is_empty() {
self.push(NodePar { self.push(NodePar {
dirs: group.dirs, dirs: group.dirs,
align: group.align, aligns: group.aligns,
line_spacing: group.line_spacing, line_spacing: group.line_spacing,
children, children,
}); });
@ -275,7 +275,7 @@ impl<'a> ExecContext<'a> {
NodeText { NodeText {
text, text,
align: self.state.align, aligns: self.state.aligns,
dir: self.state.dirs.cross, dir: self.state.dirs.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),
@ -296,10 +296,10 @@ pub enum Softness {
/// A group for a page run. /// A group for a page run.
#[derive(Debug)] #[derive(Debug)]
struct PageGroup { struct PageGroup {
dirs: LayoutDirs,
aligns: LayoutAligns,
size: Size, size: Size,
padding: Sides<Linear>, padding: Sides<Linear>,
dirs: LayoutDirs,
align: ChildAlign,
softness: Softness, softness: Softness,
} }
@ -311,6 +311,6 @@ struct ContentGroup;
#[derive(Debug)] #[derive(Debug)]
struct ParGroup { struct ParGroup {
dirs: LayoutDirs, dirs: LayoutDirs,
align: ChildAlign, aligns: LayoutAligns,
line_spacing: Length, line_spacing: Length,
} }

View File

@ -126,7 +126,7 @@ impl Exec for NodeRaw {
height: None, height: None,
child: NodeStack { child: NodeStack {
dirs: ctx.state.dirs, dirs: ctx.state.dirs,
align: ctx.state.align, aligns: ctx.state.aligns,
children, children,
} }
.into(), .into(),

View File

@ -3,33 +3,33 @@ use std::rc::Rc;
use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight}; use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight};
use crate::geom::{ use crate::geom::{
Align, ChildAlign, Dir, LayoutDirs, Length, Linear, Relative, Sides, Size, Align, Dir, LayoutAligns, LayoutDirs, Length, Linear, Relative, Sides, Size,
}; };
use crate::paper::{Paper, PaperClass, PAPER_A4}; use crate::paper::{Paper, PaperClass, PAPER_A4};
/// The evaluation state. /// The evaluation state.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct State { pub struct State {
/// The current directions along which layouts are placed in their parents.
pub dirs: LayoutDirs,
/// The current alignments of layouts in their parents.
pub aligns: LayoutAligns,
/// The current page settings. /// The current page settings.
pub page: PageState, pub page: PageState,
/// The current paragraph settings. /// The current paragraph settings.
pub par: ParState, pub par: ParState,
/// The current font settings. /// The current font settings.
pub font: FontState, pub font: FontState,
/// The current layouting directions.
pub dirs: LayoutDirs,
/// The current alignments of an item in its parent.
pub align: ChildAlign,
} }
impl Default for State { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self { Self {
dirs: LayoutDirs::new(Dir::TTB, Dir::LTR),
aligns: LayoutAligns::new(Align::Start, Align::Start),
page: PageState::default(), page: PageState::default(),
par: ParState::default(), par: ParState::default(),
font: FontState::default(), font: FontState::default(),
dirs: LayoutDirs::new(Dir::TTB, Dir::LTR),
align: ChildAlign::new(Align::Start, Align::Start),
} }
} }
} }

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
/// The alignment of a child in a container. /// The alignments of a layout in its parent.
pub type ChildAlign = Gen<Align>; pub type LayoutAligns = 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)]

View File

@ -1,6 +1,6 @@
use super::*; use super::*;
/// The directions along which nodes are layouted. /// The directions along which layouts are placed in their parent.
pub type LayoutDirs = Gen<Dir>; pub type LayoutDirs = Gen<Dir>;
/// The four directions into which content can be laid out. /// The four directions into which content can be laid out.

View File

@ -159,9 +159,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.
Frame(Frame, ChildAlign), Frame(Frame, LayoutAligns),
/// Multiple layouts. /// Multiple layouts.
Frames(Vec<Frame>, ChildAlign), Frames(Vec<Frame>, LayoutAligns),
} }
impl Layouted { impl Layouted {

View File

@ -8,12 +8,12 @@ pub struct NodePar {
/// 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: LayoutDirs, pub dirs: LayoutDirs,
/// How to align this paragraph in its parent.
pub aligns: LayoutAligns,
/// 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<Node>, pub children: Vec<Node>,
/// How to align this paragraph in _its_ parent.
pub align: ChildAlign,
} }
impl Layout for NodePar { impl Layout for NodePar {
@ -22,15 +22,17 @@ impl Layout for NodePar {
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::Frame(frame, align) => layouter.push_frame(frame, align.cross), Layouted::Frame(frame, aligns) => {
Layouted::Frames(frames, align) => { layouter.push_frame(frame, aligns.cross)
}
Layouted::Frames(frames, aligns) => {
for frame in frames { for frame in frames {
layouter.push_frame(frame, align.cross); layouter.push_frame(frame, aligns.cross);
} }
} }
} }
} }
Layouted::Frames(layouter.finish(), self.align) Layouted::Frames(layouter.finish(), self.aligns)
} }
} }

View File

@ -8,8 +8,8 @@ pub struct NodeStack {
/// 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: LayoutDirs, pub dirs: LayoutDirs,
/// How to align this stack in _its_ parent. /// How to align this stack in its parent.
pub align: ChildAlign, pub aligns: LayoutAligns,
/// The nodes to be stacked. /// The nodes to be stacked.
pub children: Vec<Node>, pub children: Vec<Node>,
} }
@ -20,15 +20,15 @@ impl Layout for NodeStack {
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::Frame(frame, align) => layouter.push_frame(frame, align), Layouted::Frame(frame, aligns) => layouter.push_frame(frame, aligns),
Layouted::Frames(frames, align) => { Layouted::Frames(frames, aligns) => {
for frame in frames { for frame in frames {
layouter.push_frame(frame, align); layouter.push_frame(frame, aligns);
} }
} }
} }
} }
Layouted::Frames(layouter.finish(), self.align) Layouted::Frames(layouter.finish(), self.aligns)
} }
} }
@ -43,7 +43,7 @@ struct StackLayouter {
dirs: LayoutDirs, dirs: LayoutDirs,
areas: Areas, areas: Areas,
finished: Vec<Frame>, finished: Vec<Frame>,
frames: Vec<(Length, Frame, ChildAlign)>, frames: Vec<(Length, Frame, LayoutAligns)>,
used: Gen<Length>, used: Gen<Length>,
ruler: Align, ruler: Align,
} }
@ -68,8 +68,8 @@ impl StackLayouter {
self.used.main += capped; self.used.main += capped;
} }
fn push_frame(&mut self, frame: Frame, align: ChildAlign) { fn push_frame(&mut self, frame: Frame, aligns: LayoutAligns) {
if self.ruler > align.main { if self.ruler > aligns.main {
self.finish_area(); self.finish_area();
} }
@ -83,12 +83,12 @@ impl StackLayouter {
} }
let size = frame.size.switch(self.dirs); let size = frame.size.switch(self.dirs);
self.frames.push((self.used.main, frame, align)); self.frames.push((self.used.main, frame, aligns));
*self.areas.current.get_mut(self.main) -= size.main; *self.areas.current.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 = align.main; self.ruler = aligns.main;
} }
fn finish_area(&mut self) { fn finish_area(&mut self) {
@ -103,11 +103,11 @@ impl StackLayouter {
let mut output = Frame::new(full_size.switch(self.dirs).to_size()); let mut output = Frame::new(full_size.switch(self.dirs).to_size());
for (before, frame, align) in std::mem::take(&mut self.frames) { for (before, frame, aligns) in std::mem::take(&mut self.frames) {
let child_size = frame.size.switch(self.dirs); let child_size = frame.size.switch(self.dirs);
// Align along the main axis. // Align along the main axis.
let main = align.main.resolve(if self.dirs.main.is_positive() { let main = aligns.main.resolve(if self.dirs.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 {
@ -117,7 +117,7 @@ impl StackLayouter {
}); });
// Align along the cross axis. // Align along the cross axis.
let cross = align.cross.resolve(if self.dirs.cross.is_positive() { let cross = aligns.cross.resolve(if self.dirs.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

View File

@ -9,12 +9,12 @@ use crate::shaping;
/// A text node. /// A text node.
#[derive(Clone, PartialEq)] #[derive(Clone, PartialEq)]
pub struct NodeText { pub struct NodeText {
/// The text.
pub text: String,
/// How to align this text node in its parent.
pub align: ChildAlign,
/// The text direction. /// The text direction.
pub dir: Dir, pub dir: Dir,
/// How to align this text node in its parent.
pub aligns: LayoutAligns,
/// The text.
pub text: String,
/// The font size. /// The font size.
pub font_size: Length, pub font_size: Length,
/// The families used for font fallback. /// The families used for font fallback.
@ -34,7 +34,7 @@ impl Layout for NodeText {
&self.families, &self.families,
self.variant, self.variant,
), ),
self.align, self.aligns,
) )
} }
} }

View File

@ -54,7 +54,7 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
} 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 {
*ctx.state.align.get_mut(gen_axis) = gen_align; *ctx.state.aligns.get_mut(gen_axis) = gen_align;
*had.get_mut(gen_axis) = true; *had.get_mut(gen_axis) = true;
} }
} else { } else {
@ -67,8 +67,8 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
} 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.
ctx.state.align.main = Align::Center; ctx.state.aligns.main = Align::Center;
ctx.state.align.cross = Align::Center; ctx.state.aligns.cross = Align::Center;
had = Gen::uniform(true); had = Gen::uniform(true);
} else { } else {
had_center = true; had_center = true;
@ -79,10 +79,10 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
// `center` alignment. // `center` alignment.
if had_center && (had.main || had.cross) { if had_center && (had.main || had.cross) {
if had.main { if had.main {
ctx.state.align.cross = Align::Center; ctx.state.aligns.cross = Align::Center;
had.cross = true; had.cross = true;
} else { } else {
ctx.state.align.main = Align::Center; ctx.state.aligns.main = Align::Center;
had.main = true; had.main = true;
} }
had_center = false; had_center = false;
@ -92,10 +92,10 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
// If `had_center` wasn't flushed by now, it's the only argument and then we // If `had_center` wasn't flushed by now, it's the only argument and then we
// default to applying it to the cross axis. // default to applying it to the cross axis.
if had_center { if had_center {
ctx.state.align.cross = Align::Center; ctx.state.aligns.cross = Align::Center;
} }
if ctx.state.align.main != snapshot.align.main { if ctx.state.aligns.main != snapshot.aligns.main {
ctx.end_par_group(); ctx.end_par_group();
ctx.start_par_group(); ctx.start_par_group();
} }

View File

@ -25,7 +25,7 @@ pub fn image(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
dimensions, dimensions,
width, width,
height, height,
align: ctx.state.align, aligns: ctx.state.aligns,
}); });
} else { } else {
ctx.diag(error!(path.span, "failed to load image")); ctx.diag(error!(path.span, "failed to load image"));
@ -37,6 +37,8 @@ pub fn image(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
/// An image node. /// An image node.
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
struct NodeImage { struct NodeImage {
/// How to align this image node in its parent.
aligns: LayoutAligns,
/// The resource id of the image file. /// The resource id of the image file.
res: ResourceId, res: ResourceId,
/// The pixel dimensions of the image. /// The pixel dimensions of the image.
@ -45,8 +47,6 @@ struct NodeImage {
width: Option<Linear>, width: Option<Linear>,
/// The fixed height, if any. /// The fixed height, if any.
height: Option<Linear>, height: Option<Linear>,
/// How to align this image node in its parent.
align: ChildAlign,
} }
impl Layout for NodeImage { impl Layout for NodeImage {
@ -81,7 +81,7 @@ impl Layout for NodeImage {
let mut frame = Frame::new(size); let mut frame = Frame::new(size);
frame.push(Point::ZERO, Element::Image(Image { res: self.res, size })); frame.push(Point::ZERO, Element::Image(Image { res: self.res, size }));
Layouted::Frame(frame, self.align) Layouted::Frame(frame, self.aligns)
} }
} }