summaryrefslogtreecommitdiff
path: root/src/library/align.rs
blob: 35d925eb8c987866290aebb1ff4df5c91080ccce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use super::*;

/// `align`: Configure the alignment along the layouting axes.
///
/// # Positional parameters
/// - Alignments: variadic, of type `alignment`.
/// - Body: optional, of type `template`.
///
/// # Named parameters
/// - Horizontal alignment: `horizontal`, of type `alignment`.
/// - Vertical alignment: `vertical`, of type `alignment`.
///
/// # Return value
/// A template that changes the alignment along the layouting axes. The effect
/// is scoped to the body if present.
///
/// # Relevant types and constants
/// - Type `alignment`
///   - `start`
///   - `center`
///   - `end`
///   - `left`
///   - `right`
///   - `top`
///   - `bottom`
pub fn align(ctx: &mut EvalContext, args: &mut FuncArgs) -> Value {
    let first = args.eat::<AlignValue>(ctx);
    let second = args.eat::<AlignValue>(ctx);
    let mut horizontal = args.eat_named::<AlignValue>(ctx, "horizontal");
    let mut vertical = args.eat_named::<AlignValue>(ctx, "vertical");
    let body = args.eat::<TemplateValue>(ctx);

    for value in first.into_iter().chain(second) {
        match value.axis() {
            Some(SpecAxis::Horizontal) | None if horizontal.is_none() => {
                horizontal = Some(value);
            }
            Some(SpecAxis::Vertical) | None if vertical.is_none() => {
                vertical = Some(value);
            }
            _ => {}
        }
    }

    Value::template("align", move |ctx| {
        let snapshot = ctx.state.clone();

        if let Some(horizontal) = horizontal {
            ctx.state.aligns.cross = horizontal.to_align(ctx.state.lang.dir);
        }

        if let Some(vertical) = vertical {
            ctx.state.aligns.main = vertical.to_align(Dir::TTB);
            if ctx.state.aligns.main != snapshot.aligns.main {
                ctx.parbreak();
            }
        }

        if let Some(body) = &body {
            body.exec(ctx);
            ctx.state = snapshot;
        }
    })
}

/// An alignment specifier passed to `align`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub(super) enum AlignValue {
    Start,
    Center,
    End,
    Left,
    Right,
    Top,
    Bottom,
}

impl AlignValue {
    fn axis(self) -> Option<SpecAxis> {
        match self {
            Self::Start => None,
            Self::Center => None,
            Self::End => None,
            Self::Left => Some(SpecAxis::Horizontal),
            Self::Right => Some(SpecAxis::Horizontal),
            Self::Top => Some(SpecAxis::Vertical),
            Self::Bottom => Some(SpecAxis::Vertical),
        }
    }

    fn to_align(self, dir: Dir) -> Align {
        let side = |is_at_positive_start| {
            if dir.is_positive() == is_at_positive_start {
                Align::Start
            } else {
                Align::End
            }
        };

        match self {
            Self::Start => Align::Start,
            Self::Center => Align::Center,
            Self::End => Align::End,
            Self::Left => side(true),
            Self::Right => side(false),
            Self::Top => side(true),
            Self::Bottom => side(false),
        }
    }
}

impl Display for AlignValue {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.pad(match self {
            Self::Start => "start",
            Self::Center => "center",
            Self::End => "end",
            Self::Left => "left",
            Self::Right => "right",
            Self::Top => "top",
            Self::Bottom => "bottom",
        })
    }
}

value! {
    AlignValue: "alignment",
}