summaryrefslogtreecommitdiff
path: root/src/library/mod.rs
blob: 333ba02ada9897ea7ecb4653b97f2001cde89cfe (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! The standard library.

use toddle::query::{FontWeight, FontStyle};

use crate::func::prelude::*;
use crate::style::{Paper, PaperClass};
use self::maps::{ExtentMap, PaddingMap, AxisKey};

pub mod maps;

pub_use_mod!(align);
pub_use_mod!(boxed);
pub_use_mod!(direction);


/// Create a scope with all standard functions.
pub fn std() -> Scope {
    let mut std = Scope::new();

    // Font setup
    std.add::<FontFamilyFunc>("font.family");
    std.add::<FontStyleFunc>("font.style");
    std.add::<FontWeightFunc>("font.weight");
    std.add::<FontSizeFunc>("font.size");

    // Layout
    std.add::<AlignFunc>("align");
    std.add::<DirectionFunc>("direction");
    std.add_with_metadata::<ContentSpacingFunc>("par.spacing", ContentKind::Paragraph);
    std.add_with_metadata::<ContentSpacingFunc>("word.spacing", ContentKind::Word);
    std.add_with_metadata::<ContentSpacingFunc>("line.spacing", ContentKind::Line);
    std.add::<BoxFunc>("box");

    // Spacing
    std.add::<LineBreakFunc>("n");
    std.add::<LineBreakFunc>("line.break");
    std.add::<ParBreakFunc>("par.break");
    std.add::<PageBreakFunc>("page.break");
    std.add_with_metadata::<SpacingFunc>("spacing", None);
    std.add_with_metadata::<SpacingFunc>("h", Some(Horizontal));
    std.add_with_metadata::<SpacingFunc>("v", Some(Vertical));

    // Page setup
    std.add::<PageSizeFunc>("page.size");
    std.add::<PageMarginsFunc>("page.margins");

    std
}

// -------------------------------------------------------------------------- //
// Font setup

function! {
    /// `font.family`: Set the font family.
    #[derive(Debug, PartialEq)]
    pub struct FontFamilyFunc {
        body: Option<SyntaxTree>,
        list: Vec<String>,
    }

    parse(args, body, ctx) {
        FontFamilyFunc {
            body: parse!(optional: body, ctx),
            list: {
                args.pos().map(|arg| match arg.v {
                    Expression::Str(s) |
                    Expression::Ident(Ident(s)) => Ok(s.to_lowercase()),
                    _ => error!("expected identifier or string"),
                }).collect::<LayoutResult<Vec<_>>>()?
            }
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.text.clone();
        style.fallback.list = self.list.clone();
        styled(&self.body, &ctx, style)
    }
}

function! {
    /// `font.style`: Set the font style (normal / italic).
    #[derive(Debug, PartialEq)]
    pub struct FontStyleFunc {
        body: Option<SyntaxTree>,
        style: FontStyle,
    }

    parse(args, body, ctx) {
        FontStyleFunc {
            body: parse!(optional: body, ctx),
            style: {
                let s = args.get_pos::<String>()?;
                match FontStyle::from_str(&s) {
                    Some(style) => style,
                    None => error!("invalid font style: `{}`", s),
                }
            }
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.text.clone();
        style.variant.style = self.style;
        styled(&self.body, &ctx, style)
    }
}

function! {
    /// `font.weight`: Set text with a given weight.
    #[derive(Debug, PartialEq)]
    pub struct FontWeightFunc {
        body: Option<SyntaxTree>,
        weight: FontWeight,
    }

    parse(args, body, ctx) {
        FontWeightFunc {
            body: parse!(optional: body, ctx),
            weight: match args.get_pos::<Expression>()? {
                Expression::Num(weight) => {
                    let weight = weight.round() as i16;
                    FontWeight(
                        if weight < 100 { 100 }
                        else if weight <= 900 { weight }
                        else { 900 }
                    )
                }
                Expression::Ident(Ident(s)) => {
                    match FontWeight::from_str(&s) {
                        Some(weight) => weight,
                        None => error!("invalid font weight: `{}`", s),
                    }
                }
                _ => error!("expected identifier or number"),
            },
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.text.clone();
        style.variant.style.toggle();
        styled(&self.body, &ctx, style)
    }
}

function! {
    /// `font.size`: Sets the font size.
    #[derive(Debug, PartialEq)]
    pub struct FontSizeFunc {
        body: Option<SyntaxTree>,
        size: ScaleSize,
    }

    parse(args, body, ctx) {
        FontSizeFunc {
            body: parse!(optional: body, ctx),
            size: args.get_pos::<ScaleSize>()?,
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.text.clone();
        match self.size {
            ScaleSize::Absolute(size) => {
                style.base_font_size = size;
                style.font_scale = 1.0;
            }
            ScaleSize::Scaled(scale) => style.font_scale = scale,
        }
        styled(&self.body, &ctx, style)
    }
}

// -------------------------------------------------------------------------- //
// Layout

function! {
    /// `word.spacing`, `line.spacing`, `par.spacing`: The spacing between
    /// words, lines or paragraphs as a multiple of the font size.
    #[derive(Debug, PartialEq)]
    pub struct ContentSpacingFunc {
        body: Option<SyntaxTree>,
        content: ContentKind,
        spacing: f32,
    }

    type Meta = ContentKind;

    parse(args, body, ctx, meta) {
        ContentSpacingFunc {
            body: parse!(optional: body, ctx),
            content: meta,
            spacing: args.get_pos::<f64>()? as f32,
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.text.clone();
        match self.content {
            ContentKind::Word => style.word_spacing_scale = self.spacing,
            ContentKind::Line => style.line_spacing_scale = self.spacing,
            ContentKind::Paragraph => style.paragraph_spacing_scale = self.spacing,
        }
        styled(&self.body, &ctx, style)
    }
}

/// The different kinds of content that can be spaced.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ContentKind {
    Word,
    Line,
    Paragraph,
}

// -------------------------------------------------------------------------- //
// Spacing

function! {
    /// `line.break`, `n`: Ends the current line.
    #[derive(Debug, Default, PartialEq)]
    pub struct LineBreakFunc;

    parse(default)
    layout() { vec![FinishLine] }
}

function! {
    /// `par.break`: Ends the current paragraph.
    ///
    /// self has the same effect as two subsequent newlines.
    #[derive(Debug, Default, PartialEq)]
    pub struct ParBreakFunc;

    parse(default)
    layout() { vec![BreakParagraph] }
}

function! {
    /// `page.break`: Ends the current page.
    #[derive(Debug, Default, PartialEq)]
    pub struct PageBreakFunc;

    parse(default)
    layout() { vec![BreakPage] }
}

function! {
    /// `spacing`, `h`, `v`: Adds spacing along an axis.
    #[derive(Debug, PartialEq)]
    pub struct SpacingFunc {
        axis: AxisKey,
        spacing: FSize,
    }

    type Meta = Option<SpecificAxis>;

    parse(args, body, _, meta) {
        parse!(forbidden: body);

        if let Some(axis) = meta {
            SpacingFunc {
                axis: AxisKey::Specific(axis),
                spacing: FSize::from_expr(args.get_pos::<Spanned<Expression>>()?)?,
            }
        } else if let Some(arg) = args.get_key_next() {
            let axis = AxisKey::from_ident(&arg.v.key)
                .map_err(|_| error!(@unexpected_argument))?;

            let spacing = FSize::from_expr(arg.v.value)?;
            SpacingFunc { axis, spacing }
        } else {
            error!("expected axis and spacing")
        }
    }

    layout(self, ctx) {
        let axis = self.axis.to_generic(ctx.axes);
        let spacing = self.spacing.scaled(ctx.style.text.font_size());
        vec![SpacingFunc(spacing, SpacingKind::Hard, axis)]
    }
}

// -------------------------------------------------------------------------- //
// Page setup

function! {
    /// `page.size`: Set the size of pages.
    #[derive(Debug, PartialEq)]
    pub enum PageSizeFunc {
        Paper(Paper, bool),
        Custom(ExtentMap<PSize>),
    }

    parse(args, body) {
        parse!(forbidden: body);

        if let Some(name) = args.get_pos_opt::<Ident>()? {
            let flip = args.get_key_opt::<bool>("flip")?
                .unwrap_or(false);
            PageSizeFunc::Paper(Paper::from_name(name.as_str())?, flip)
        } else {
            PageSizeFunc::Custom(ExtentMap::new(&mut args, true)?)
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.page;

        match self {
            PageSizeFunc::Paper(paper, flip) => {
                style.class = paper.class;
                style.dimensions = paper.dimensions;
                if *flip {
                    style.dimensions.swap();
                }
            }

            PageSizeFunc::Custom(map) => {
                style.class = PaperClass::Custom;

                let map = map.dedup(ctx.axes)?;
                let dims = &mut style.dimensions;
                map.with(Horizontal, |&psize| dims.x = psize.scaled(dims.x));
                map.with(Vertical, |&psize| dims.y = psize.scaled(dims.y));
            }
        }

        vec![SetPageStyle(style)]
    }
}

function! {
    /// `page.margins`: Sets the page margins.
    #[derive(Debug, PartialEq)]
    pub struct PageMarginsFunc {
        map: PaddingMap,
    }

    parse(args, body) {
        parse!(forbidden: body);
        PageMarginsFunc {
            map: PaddingMap::new(&mut args)?,
        }
    }

    layout(self, ctx) {
        let mut style = ctx.style.page;
        self.map.apply(ctx.axes, &mut style.margins)?;
        vec![SetPageStyle(style)]
    }
}

// -------------------------------------------------------------------------- //
// Helpers

/// Layout the body with the style or update the style if there is no body.
fn styled<'a>(
    body: &'a Option<SyntaxTree>,
    ctx: &LayoutContext,
    style: TextStyle
) -> Commands<'a> {
    match &body {
        Some(body) => vec![
            SetTextStyle(style),
            LayoutTree(body),
            SetTextStyle(ctx.style.text.clone()),
        ],
        None => vec![SetTextStyle(style)]
    }
}