mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Rename ChildAlign to LayoutAligns ✏
This commit is contained in:
parent
ffcb8cd97a
commit
584a43277d
@ -5,7 +5,7 @@ use fontdock::FontStyle;
|
||||
|
||||
use super::*;
|
||||
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::{
|
||||
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.
|
||||
pub fn exec(&mut self, template: &ValueTemplate) -> Node {
|
||||
let dirs = self.state.dirs;
|
||||
let align = self.state.align;
|
||||
let aligns = self.state.aligns;
|
||||
|
||||
self.start_group(ContentGroup);
|
||||
self.start_par_group();
|
||||
@ -116,7 +116,7 @@ impl<'a> ExecContext<'a> {
|
||||
self.end_par_group();
|
||||
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.
|
||||
@ -130,7 +130,7 @@ impl<'a> ExecContext<'a> {
|
||||
size: self.state.page.size,
|
||||
padding: self.state.page.margins(),
|
||||
dirs: self.state.dirs,
|
||||
align: self.state.align,
|
||||
aligns: self.state.aligns,
|
||||
softness,
|
||||
});
|
||||
self.start_par_group();
|
||||
@ -156,7 +156,7 @@ impl<'a> ExecContext<'a> {
|
||||
padding: group.padding,
|
||||
child: NodeStack {
|
||||
dirs: group.dirs,
|
||||
align: group.align,
|
||||
aligns: group.aligns,
|
||||
children,
|
||||
}
|
||||
.into(),
|
||||
@ -172,7 +172,7 @@ impl<'a> ExecContext<'a> {
|
||||
let em = self.state.font.font_size();
|
||||
self.start_group(ParGroup {
|
||||
dirs: self.state.dirs,
|
||||
align: self.state.align,
|
||||
aligns: self.state.aligns,
|
||||
line_spacing: self.state.par.line_spacing.resolve(em),
|
||||
});
|
||||
}
|
||||
@ -183,7 +183,7 @@ impl<'a> ExecContext<'a> {
|
||||
if !children.is_empty() {
|
||||
self.push(NodePar {
|
||||
dirs: group.dirs,
|
||||
align: group.align,
|
||||
aligns: group.aligns,
|
||||
line_spacing: group.line_spacing,
|
||||
children,
|
||||
});
|
||||
@ -275,7 +275,7 @@ impl<'a> ExecContext<'a> {
|
||||
|
||||
NodeText {
|
||||
text,
|
||||
align: self.state.align,
|
||||
aligns: self.state.aligns,
|
||||
dir: self.state.dirs.cross,
|
||||
font_size: self.state.font.font_size(),
|
||||
families: Rc::clone(&self.state.font.families),
|
||||
@ -296,10 +296,10 @@ pub enum Softness {
|
||||
/// A group for a page run.
|
||||
#[derive(Debug)]
|
||||
struct PageGroup {
|
||||
dirs: LayoutDirs,
|
||||
aligns: LayoutAligns,
|
||||
size: Size,
|
||||
padding: Sides<Linear>,
|
||||
dirs: LayoutDirs,
|
||||
align: ChildAlign,
|
||||
softness: Softness,
|
||||
}
|
||||
|
||||
@ -311,6 +311,6 @@ struct ContentGroup;
|
||||
#[derive(Debug)]
|
||||
struct ParGroup {
|
||||
dirs: LayoutDirs,
|
||||
align: ChildAlign,
|
||||
aligns: LayoutAligns,
|
||||
line_spacing: Length,
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ impl Exec for NodeRaw {
|
||||
height: None,
|
||||
child: NodeStack {
|
||||
dirs: ctx.state.dirs,
|
||||
align: ctx.state.align,
|
||||
aligns: ctx.state.aligns,
|
||||
children,
|
||||
}
|
||||
.into(),
|
||||
|
@ -3,33 +3,33 @@ use std::rc::Rc;
|
||||
use fontdock::{fallback, FallbackTree, FontStretch, FontStyle, FontVariant, FontWeight};
|
||||
|
||||
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};
|
||||
|
||||
/// The evaluation state.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
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.
|
||||
pub page: PageState,
|
||||
/// The current paragraph settings.
|
||||
pub par: ParState,
|
||||
/// The current font settings.
|
||||
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 {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dirs: LayoutDirs::new(Dir::TTB, Dir::LTR),
|
||||
aligns: LayoutAligns::new(Align::Start, Align::Start),
|
||||
page: PageState::default(),
|
||||
par: ParState::default(),
|
||||
font: FontState::default(),
|
||||
dirs: LayoutDirs::new(Dir::TTB, Dir::LTR),
|
||||
align: ChildAlign::new(Align::Start, Align::Start),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
/// The alignment of a child in a container.
|
||||
pub type ChildAlign = Gen<Align>;
|
||||
/// The alignments of a layout in its parent.
|
||||
pub type LayoutAligns = Gen<Align>;
|
||||
|
||||
/// Where to align something along a directed axis.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
|
||||
|
@ -1,6 +1,6 @@
|
||||
use super::*;
|
||||
|
||||
/// The directions along which nodes are layouted.
|
||||
/// The directions along which layouts are placed in their parent.
|
||||
pub type LayoutDirs = Gen<Dir>;
|
||||
|
||||
/// The four directions into which content can be laid out.
|
||||
|
@ -159,9 +159,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.
|
||||
Frame(Frame, ChildAlign),
|
||||
Frame(Frame, LayoutAligns),
|
||||
/// Multiple layouts.
|
||||
Frames(Vec<Frame>, ChildAlign),
|
||||
Frames(Vec<Frame>, LayoutAligns),
|
||||
}
|
||||
|
||||
impl Layouted {
|
||||
|
@ -8,12 +8,12 @@ pub struct NodePar {
|
||||
/// The children are placed in lines along the `cross` direction. The lines
|
||||
/// are stacked along the `main` direction.
|
||||
pub dirs: LayoutDirs,
|
||||
/// How to align this paragraph in its parent.
|
||||
pub aligns: LayoutAligns,
|
||||
/// The spacing to insert after each line.
|
||||
pub line_spacing: Length,
|
||||
/// The nodes to be arranged in a paragraph.
|
||||
pub children: Vec<Node>,
|
||||
/// How to align this paragraph in _its_ parent.
|
||||
pub align: ChildAlign,
|
||||
}
|
||||
|
||||
impl Layout for NodePar {
|
||||
@ -22,15 +22,17 @@ impl Layout for NodePar {
|
||||
for child in &self.children {
|
||||
match child.layout(ctx, &layouter.areas) {
|
||||
Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
|
||||
Layouted::Frame(frame, align) => layouter.push_frame(frame, align.cross),
|
||||
Layouted::Frames(frames, align) => {
|
||||
Layouted::Frame(frame, aligns) => {
|
||||
layouter.push_frame(frame, aligns.cross)
|
||||
}
|
||||
Layouted::Frames(frames, aligns) => {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,8 +8,8 @@ pub struct NodeStack {
|
||||
/// The children are stacked along the `main` direction. The `cross`
|
||||
/// direction is required for aligning the children.
|
||||
pub dirs: LayoutDirs,
|
||||
/// How to align this stack in _its_ parent.
|
||||
pub align: ChildAlign,
|
||||
/// How to align this stack in its parent.
|
||||
pub aligns: LayoutAligns,
|
||||
/// The nodes to be stacked.
|
||||
pub children: Vec<Node>,
|
||||
}
|
||||
@ -20,15 +20,15 @@ impl Layout for NodeStack {
|
||||
for child in &self.children {
|
||||
match child.layout(ctx, &layouter.areas) {
|
||||
Layouted::Spacing(spacing) => layouter.push_spacing(spacing),
|
||||
Layouted::Frame(frame, align) => layouter.push_frame(frame, align),
|
||||
Layouted::Frames(frames, align) => {
|
||||
Layouted::Frame(frame, aligns) => layouter.push_frame(frame, aligns),
|
||||
Layouted::Frames(frames, aligns) => {
|
||||
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,
|
||||
areas: Areas,
|
||||
finished: Vec<Frame>,
|
||||
frames: Vec<(Length, Frame, ChildAlign)>,
|
||||
frames: Vec<(Length, Frame, LayoutAligns)>,
|
||||
used: Gen<Length>,
|
||||
ruler: Align,
|
||||
}
|
||||
@ -68,8 +68,8 @@ impl StackLayouter {
|
||||
self.used.main += capped;
|
||||
}
|
||||
|
||||
fn push_frame(&mut self, frame: Frame, align: ChildAlign) {
|
||||
if self.ruler > align.main {
|
||||
fn push_frame(&mut self, frame: Frame, aligns: LayoutAligns) {
|
||||
if self.ruler > aligns.main {
|
||||
self.finish_area();
|
||||
}
|
||||
|
||||
@ -83,12 +83,12 @@ impl StackLayouter {
|
||||
}
|
||||
|
||||
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.used.main += size.main;
|
||||
self.used.cross = self.used.cross.max(size.cross);
|
||||
self.ruler = align.main;
|
||||
self.ruler = aligns.main;
|
||||
}
|
||||
|
||||
fn finish_area(&mut self) {
|
||||
@ -103,11 +103,11 @@ impl StackLayouter {
|
||||
|
||||
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);
|
||||
|
||||
// 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;
|
||||
before .. full_size.main - after_with_self
|
||||
} else {
|
||||
@ -117,7 +117,7 @@ impl StackLayouter {
|
||||
});
|
||||
|
||||
// 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
|
||||
} else {
|
||||
full_size.cross - child_size.cross .. Length::ZERO
|
||||
|
@ -9,12 +9,12 @@ use crate::shaping;
|
||||
/// A text node.
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub struct NodeText {
|
||||
/// The text.
|
||||
pub text: String,
|
||||
/// How to align this text node in its parent.
|
||||
pub align: ChildAlign,
|
||||
/// The text direction.
|
||||
pub dir: Dir,
|
||||
/// How to align this text node in its parent.
|
||||
pub aligns: LayoutAligns,
|
||||
/// The text.
|
||||
pub text: String,
|
||||
/// The font size.
|
||||
pub font_size: Length,
|
||||
/// The families used for font fallback.
|
||||
@ -34,7 +34,7 @@ impl Layout for NodeText {
|
||||
&self.families,
|
||||
self.variant,
|
||||
),
|
||||
self.align,
|
||||
self.aligns,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||
} else if had.get(gen_axis) {
|
||||
ctx.diag(error!(span, "duplicate alignment for {} axis", axis));
|
||||
} 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;
|
||||
}
|
||||
} else {
|
||||
@ -67,8 +67,8 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||
} else if had_center {
|
||||
// Both this and the previous one are unspecified `center`
|
||||
// alignments. Both axes should be centered.
|
||||
ctx.state.align.main = Align::Center;
|
||||
ctx.state.align.cross = Align::Center;
|
||||
ctx.state.aligns.main = Align::Center;
|
||||
ctx.state.aligns.cross = Align::Center;
|
||||
had = Gen::uniform(true);
|
||||
} else {
|
||||
had_center = true;
|
||||
@ -79,10 +79,10 @@ pub fn align(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||
// `center` alignment.
|
||||
if had_center && (had.main || had.cross) {
|
||||
if had.main {
|
||||
ctx.state.align.cross = Align::Center;
|
||||
ctx.state.aligns.cross = Align::Center;
|
||||
had.cross = true;
|
||||
} else {
|
||||
ctx.state.align.main = Align::Center;
|
||||
ctx.state.aligns.main = Align::Center;
|
||||
had.main = true;
|
||||
}
|
||||
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
|
||||
// default to applying it to the cross axis.
|
||||
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.start_par_group();
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ pub fn image(ctx: &mut EvalContext, args: &mut ValueArgs) -> Value {
|
||||
dimensions,
|
||||
width,
|
||||
height,
|
||||
align: ctx.state.align,
|
||||
aligns: ctx.state.aligns,
|
||||
});
|
||||
} else {
|
||||
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.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
struct NodeImage {
|
||||
/// How to align this image node in its parent.
|
||||
aligns: LayoutAligns,
|
||||
/// The resource id of the image file.
|
||||
res: ResourceId,
|
||||
/// The pixel dimensions of the image.
|
||||
@ -45,8 +47,6 @@ struct NodeImage {
|
||||
width: Option<Linear>,
|
||||
/// The fixed height, if any.
|
||||
height: Option<Linear>,
|
||||
/// How to align this image node in its parent.
|
||||
align: ChildAlign,
|
||||
}
|
||||
|
||||
impl Layout for NodeImage {
|
||||
@ -81,7 +81,7 @@ impl Layout for NodeImage {
|
||||
let mut frame = Frame::new(size);
|
||||
frame.push(Point::ZERO, Element::Image(Image { res: self.res, size }));
|
||||
|
||||
Layouted::Frame(frame, self.align)
|
||||
Layouted::Frame(frame, self.aligns)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user