summaryrefslogtreecommitdiff
path: root/library/src/compute/create.rs
blob: cb693a1c3aee81803b9679c3cb8a34d8314532ef (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
use std::str::FromStr;

use typst::model::Regex;

use crate::prelude::*;

/// Convert a value to an integer.
///
/// # Parameters
/// - value: ToInt (positional, required)
///   The value that should be converted to an integer.
///
/// # Tags
/// - create
#[func]
pub fn int(args: &mut Args) -> SourceResult<Value> {
    Ok(Value::Int(args.expect::<ToInt>("value")?.0))
}

/// A value that can be cast to an integer.
struct ToInt(i64);

castable! {
    ToInt,
    v: bool => Self(v as i64),
    v: i64 => Self(v),
    v: f64 => Self(v as i64),
    v: EcoString => Self(v.parse().map_err(|_| "not a valid integer")?),
}

/// Convert a value to a float.
///
/// # Parameters
/// - value: ToFloat (positional, required)
///   The value that should be converted to a float.
///
/// # Tags
/// - create
#[func]
pub fn float(args: &mut Args) -> SourceResult<Value> {
    Ok(Value::Float(args.expect::<ToFloat>("value")?.0))
}

/// A value that can be cast to a float.
struct ToFloat(f64);

castable! {
    ToFloat,
    v: bool => Self(v as i64 as f64),
    v: i64 => Self(v as f64),
    v: f64 => Self(v),
    v: EcoString => Self(v.parse().map_err(|_| "not a valid float")?),
}

/// Create a grayscale color.
///
/// # Parameters
/// - gray: Component (positional, required)
///   The gray component.
///
/// # Tags
/// - create
#[func]
pub fn luma(args: &mut Args) -> SourceResult<Value> {
    let Component(luma) = args.expect("gray component")?;
    Ok(Value::Color(LumaColor::new(luma).into()))
}

/// Create an RGB(A) color.
///
/// # Parameters
/// - hex: EcoString (positional)
///   The color in hexademical notation.
///
///   Accepts three, four, six or eight hexadecimal digits and optionally
///   a leading hashtag.
///
///   If this string is given, the individual components should not be given.
///
///   # Example
///   ```
///   #let color = rgb("#239dad")
///   #text(16pt, color)[*Typst*]
///   ```
///
/// - red: Component (positional)
///   The red component.
///
/// - green: Component (positional)
///   The green component.
///
/// - blue: Component (positional)
///   The blue component.
///
/// - alpha: Component (positional)
///   The alpha component.
///
/// # Tags
/// - create
#[func]
pub fn rgb(args: &mut Args) -> SourceResult<Value> {
    Ok(Value::Color(if let Some(string) = args.find::<Spanned<EcoString>>()? {
        match RgbaColor::from_str(&string.v) {
            Ok(color) => color.into(),
            Err(msg) => bail!(string.span, msg),
        }
    } else {
        let Component(r) = args.expect("red component")?;
        let Component(g) = args.expect("green component")?;
        let Component(b) = args.expect("blue component")?;
        let Component(a) = args.eat()?.unwrap_or(Component(255));
        RgbaColor::new(r, g, b, a).into()
    }))
}

/// An integer or ratio component.
struct Component(u8);

castable! {
    Component,
    v: i64 => match v {
        0 ..= 255 => Self(v as u8),
        _ => Err("must be between 0 and 255")?,
    },
    v: Ratio => if (0.0 ..= 1.0).contains(&v.get()) {
        Self((v.get() * 255.0).round() as u8)
    } else {
        Err("must be between 0% and 100%")?
    },
}

/// Create a CMYK color.
///
/// # Parameters
/// - cyan: RatioComponent (positional, required)
///   The cyan component.
///
/// - magenta: RatioComponent (positional, required)
///   The magenta component.
///
/// - yellow: RatioComponent (positional, required)
///   The yellow component.
///
/// - key: RatioComponent (positional, required)
///   The key component.
///
/// # Tags
/// - create
#[func]
pub fn cmyk(args: &mut Args) -> SourceResult<Value> {
    let RatioComponent(c) = args.expect("cyan component")?;
    let RatioComponent(m) = args.expect("magenta component")?;
    let RatioComponent(y) = args.expect("yellow component")?;
    let RatioComponent(k) = args.expect("key component")?;
    Ok(Value::Color(CmykColor::new(c, m, y, k).into()))
}

/// A component that must be a ratio.
struct RatioComponent(u8);

castable! {
    RatioComponent,
    v: Ratio => if (0.0 ..= 1.0).contains(&v.get()) {
        Self((v.get() * 255.0).round() as u8)
    } else {
        Err("must be between 0% and 100%")?
    },
}

/// Convert a value to a string.
///
/// # Parameters
/// - value: ToStr (positional, required)
///   The value that should be converted to a string.
///
/// # Tags
/// - create
#[func]
pub fn str(args: &mut Args) -> SourceResult<Value> {
    Ok(Value::Str(args.expect::<ToStr>("value")?.0))
}

/// A value that can be cast to a string.
struct ToStr(Str);

castable! {
    ToStr,
    v: i64 => Self(format_str!("{}", v)),
    v: f64 => Self(format_str!("{}", v)),
    v: Label => Self(v.0.into()),
    v: Str => Self(v),
}

/// Create a label from a string.
///
/// # Parameters
/// - name: EcoString (positional, required)
///   The name of the label.
///
/// # Tags
/// - create
#[func]
pub fn label(args: &mut Args) -> SourceResult<Value> {
    Ok(Value::Label(Label(args.expect("string")?)))
}

/// Create a regular expression from a string.
///
/// # Parameters
/// - regex: EcoString (positional, required)
///   The regular expression.
///
/// # Tags
/// - create
#[func]
pub fn regex(args: &mut Args) -> SourceResult<Value> {
    let Spanned { v, span } = args.expect::<Spanned<EcoString>>("regular expression")?;
    Ok(Regex::new(&v).at(span)?.into())
}

/// Create an array consisting of a sequence of numbers.
///
/// # Parameters
/// - start: i64 (positional)
///   The start of the range (inclusive).
///
/// - end: i64 (positional, required)
///   The end of the range (exclusive).
///
/// - step: i64 (named)
///   The distance between the generated numbers.
///
/// # Tags
/// - create
#[func]
pub fn range(args: &mut Args) -> SourceResult<Value> {
    let first = args.expect::<i64>("end")?;
    let (start, end) = match args.eat::<i64>()? {
        Some(second) => (first, second),
        None => (0, first),
    };

    let step: i64 = match args.named("step")? {
        Some(Spanned { v: 0, span }) => bail!(span, "step must not be zero"),
        Some(Spanned { v, .. }) => v,
        None => 1,
    };

    let mut x = start;
    let mut seq = vec![];

    while x.cmp(&end) == 0.cmp(&step) {
        seq.push(Value::Int(x));
        x += step;
    }

    Ok(Value::Array(Array::from_vec(seq)))
}