summaryrefslogtreecommitdiff
path: root/src/library/font.rs
blob: 21fb2d13a19211484fce2c4b4859692c114e1a1a (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
use fontdock::{FontStretch, FontStyle, FontWeight};

use crate::eval::StringLike;
use crate::geom::Linear;
use crate::prelude::*;

/// `font`: Configure the font.
///
/// # Positional arguments
/// - The font size (optional, length or relative to previous font size).
/// - A font family fallback list (optional, identifiers or strings).
///
/// # Keyword arguments
/// - `style`
///     - `normal`
///     - `italic`
///     - `oblique`
///
/// - `weight`
///     - `thin` or `hairline` (`100`)
///     - `extralight`         (`200`)
///     - `light`              (`300`)
///     - `regular`            (`400`)
///     - `medium`             (`500`)
///     - `semibold`           (`600`)
///     - `bold`               (`700`)
///     - `extrabold`          (`800`)
///     - `black`              (`900`)
///     - any integer from the range `100` - `900` (inclusive)
///
/// - `stretch`
///     - `ultra-condensed`
///     - `extra-condensed`
///     - `condensed`
///     - `semi-condensed`
///     - `normal`
///     - `semi-expanded`
///     - `expanded`
///     - `extra-expanded`
///     - `ultra-expanded`
///
/// - Any other keyword argument whose value is a dictionary of strings defines
///   a fallback class, for example:
///   ```typst
///   [font: serif = ("Source Serif Pro", "Noto Serif")]
///   ```
///   This class can be used in the fallback list or other fallback classes as
///   long as the resulting fallback tree is acylic.
///   ```typst
///   [font: "My Serif", serif]
///   ```
pub async fn font(mut args: Args, ctx: &mut LayoutContext) -> Value {
    let mut text = ctx.state.text.clone();
    let mut needs_flattening = false;

    let body = args.find::<SynTree>();

    if let Some(linear) = args.find::<Linear>() {
        if linear.rel == 0.0 {
            text.font_size.base = linear.abs;
            text.font_size.scale = Linear::rel(1.0);
        } else {
            text.font_size.scale = linear;
        }
    }

    let list: Vec<_> = args.find_all::<StringLike>().map(|s| s.to_lowercase()).collect();
    if !list.is_empty() {
        text.fallback.list = list;
        needs_flattening = true;
    }

    if let Some(style) = args.get::<_, FontStyle>(ctx, "style") {
        text.variant.style = style;
    }

    if let Some(weight) = args.get::<_, FontWeight>(ctx, "weight") {
        text.variant.weight = weight;
    }

    if let Some(stretch) = args.get::<_, FontStretch>(ctx, "stretch") {
        text.variant.stretch = stretch;
    }

    for (class, dict) in args.find_all_str::<Spanned<ValueDict>>() {
        let fallback = Args(dict)
            .find_all::<StringLike>()
            .map(|s| s.to_lowercase())
            .collect();

        text.fallback.update_class_list(class, fallback);
        needs_flattening = true;
    }

    args.done(ctx);

    if needs_flattening {
        text.fallback.flatten();
    }

    Value::Commands(match body {
        Some(tree) => vec![
            SetTextState(text),
            LayoutSyntaxTree(tree),
            SetTextState(ctx.state.text.clone()),
        ],
        None => vec![SetTextState(text)],
    })
}