spritebox 0.4.0

Zero-dependency, safe Rust pixel art as text: sprites, UI panels, text, themes, screens, animations, tilemaps and pixel fonts, rendered to PNG and atlases
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! Pixel fonts: `font NAME height=H` and a `glyph` for each character.

use std::collections::HashMap;

use super::lex::{Node, Token};
use super::{Header, Parsed, Parser, duplicate};
use crate::font::{Font, Glyph};
use crate::{Error, ErrorCode, Position};

impl<'a> Parser<'a, '_> {
    pub(super) fn font(&mut self, node: &Node<'a>) -> Parsed<()> {
        let mut header = Header::new(node, "font")?;
        if let Some(extra) = header.args.first() {
            return Err(extra.error(
                ErrorCode::Syntax,
                format!(
                    "Unexpected `{}`; write the font's metrics as `height=7`",
                    extra.text
                ),
            ));
        }
        let max = self.limits.max_sprite_side;
        let height_token = header.option("height").ok_or_else(|| {
            header
                .name
                .end_error("A font needs `height=H`, the rows of its glyphs".to_owned())
        })?;
        let height = number(height_token, 1, max, "Height")?;
        let baseline = match header.option("baseline") {
            Some(t) => number(t, 0, height, "The baseline")?,
            None => height,
        };
        let line_height = match header.option("line_height") {
            Some(t) => number(t, 1, max, "The line height")?,
            None => height + 1,
        };
        let spacing = match header.option("spacing") {
            Some(t) => number(t, 0, max, "Spacing")?,
            None => 1,
        };
        let space = match header.option("space") {
            Some(t) => Some(number(t, 0, max, "The space's advance")?),
            None => None,
        };
        let covers = match header.option("covers") {
            Some(t) => Some((coverage(t)?, t)),
            None => None,
        };
        let name = header.name;
        let lint = header.option("lint");
        header.finish(
            "font",
            "`height=`, `baseline=`, `line_height=`, `spacing=`, `space=`, `covers=` or `lint=`",
        )?;
        self.lint_option(crate::lint::Kind::Font, name, lint)?;

        let mut glyphs: Vec<Glyph> = Vec::new();
        let mut seen: HashMap<char, Position> = HashMap::new();
        let mut complete = true;
        let mut kerns = Vec::new();
        let mut kern_names = std::collections::HashSet::new();
        let mut glyph_entries = 0usize;
        for child in &node.children {
            if child.keyword() == "kern" {
                let tokens = &child.line.tokens;
                if kerns.len() >= self.limits.max_kerning_pairs {
                    return Err(tokens[0]
                        .error(ErrorCode::LimitExceeded, "More than the kerning pair limit"));
                }
                if tokens.len() != 3 || !child.children.is_empty() {
                    return Err(tokens[0].error(ErrorCode::Syntax, "Expected `kern AV -1`"));
                }
                let parts: Vec<_> = tokens[1].text.split(',').collect();
                let pair: Vec<char> = if parts.len() == 2 {
                    parts
                        .iter()
                        .map(|p| {
                            character(Token {
                                text: p,
                                pos: tokens[1].pos,
                            })
                        })
                        .collect::<Parsed<Vec<_>>>()?
                } else {
                    tokens[1].text.chars().collect()
                };
                if pair.len() != 2 {
                    return Err(tokens[1].error(
                        ErrorCode::Syntax,
                        "Expected two characters, or U+0041,U+0056",
                    ));
                }
                let adjustment = tokens[2]
                    .text
                    .parse::<i32>()
                    .ok()
                    .filter(|v| v.unsigned_abs() <= max)
                    .ok_or_else(|| {
                        tokens[2].error(
                            ErrorCode::InvalidArgument,
                            "Kerning exceeds the font limits",
                        )
                    })?;
                if !kern_names.insert((pair[0], pair[1])) {
                    return Err(tokens[1].error(ErrorCode::DuplicateName, "Duplicate kerning pair"));
                }
                kerns.push((
                    crate::KernPair {
                        left: pair[0],
                        right: pair[1],
                        adjustment,
                    },
                    tokens[1],
                ));
                continue;
            }
            if glyph_entries >= self.limits.max_glyphs {
                self.errors.push(child.line.first().error(
                    ErrorCode::LimitExceeded,
                    format!("More than the limit of {} glyphs", self.limits.max_glyphs),
                ));
                complete = false;
                break;
            }
            glyph_entries += 1;
            match self.glyph(child, height, spacing) {
                Ok((glyph, token)) => {
                    if let Some(&first) = seen.get(&glyph.ch) {
                        self.errors.push(duplicate(token, "Glyph", first));
                        complete = false;
                    } else {
                        seen.insert(glyph.ch, token.pos);
                        glyphs.push(glyph);
                    }
                }
                Err(e) => {
                    self.errors.push(e);
                    complete = false;
                }
            }
        }
        if !glyphs.iter().any(|g| g.ch == ' ') {
            glyphs.push(Glyph {
                ch: ' ',
                width: 0,
                advance: space.unwrap_or(height.div_ceil(2)),
                pixels: Vec::new(),
            });
        } else if let Some(space) = space {
            self.errors.push(Error::at(
                name.pos,
                ErrorCode::DuplicateName,
                format!(
                    "Font `{}` has a glyph for the space, so `space={space}` is not needed",
                    name.text
                ),
            ));
            complete = false;
        }
        if let Some((required, token)) = covers {
            let missing: Vec<char> = required
                .into_iter()
                .filter(|c| !glyphs.iter().any(|g| g.ch == *c))
                .collect();
            if !missing.is_empty() {
                let listed: Vec<String> = missing
                    .iter()
                    .map(|&c| format!("`{c}` (U+{:04X})", c as u32))
                    .collect();
                self.errors.push(token.error(
                    ErrorCode::UnknownName,
                    format!(
                        "Font `{}` has no glyph for {} of the characters that `covers={}` requires: {}",
                        name.text,
                        missing.len(),
                        token.text,
                        listed.join(", ")
                    ),
                ));
                complete = false;
            }
        }
        if complete {
            for (pair, token) in &kerns {
                let left = glyphs.iter().find(|g| g.ch == pair.left);
                if left.is_none() || !glyphs.iter().any(|g| g.ch == pair.right) {
                    return Err(token.error(
                        ErrorCode::UnknownName,
                        "A kerning pair names an unknown glyph",
                    ));
                }
                if pair.adjustment < -(left.unwrap().advance as i32) {
                    return Err(token.error(
                        ErrorCode::InvalidArgument,
                        "Kerning cannot move the pen before the preceding glyph",
                    ));
                }
            }
            kerns.sort_by_key(|(k, _)| (k.left, k.right));
            self.fonts.push((
                name,
                Font {
                    name: name.text.to_owned(),
                    height,
                    baseline,
                    line_height,
                    glyphs,
                    kerning: kerns.into_iter().map(|(k, _)| k).collect(),
                },
            ));
        } else {
            self.failed_fonts.push(name);
        }
        Ok(())
    }

    /// `glyph CHAR [advance=N]` and its rows of `#` and `.`.
    fn glyph(&mut self, node: &Node<'a>, height: u32, spacing: u32) -> Parsed<(Glyph, Token<'a>)> {
        let tokens = &node.line.tokens;
        if tokens[0].text != "glyph" {
            return Err(tokens[0].error(
                ErrorCode::Syntax,
                format!("Unknown font entry `{}`; expected `glyph`", tokens[0].text),
            ));
        }
        let token = *tokens.get(1).ok_or_else(|| {
            tokens[0]
                .end_error("Expected the glyph's character, such as `A` or `U+0041`".to_owned())
        })?;
        let ch = character(token)?;
        let mut advance = None;
        for &extra in &tokens[2..] {
            match extra.text.strip_prefix("advance=") {
                Some(value) if advance.is_none() => {
                    let value_token = Token {
                        text: value,
                        pos: extra.at("advance=".len()),
                    };
                    advance = Some(number(
                        value_token,
                        0,
                        self.limits.max_sprite_side,
                        "Advance",
                    )?);
                }
                _ => {
                    return Err(extra.error(
                        ErrorCode::Syntax,
                        format!("Unexpected `{}`; expected `advance=N`", extra.text),
                    ));
                }
            }
        }
        let what = format!("glyph `{}`", token.text);
        let mut width = None;
        let mut pixels = Vec::new();
        for child in &node.children {
            let row = child.line.first();
            if child.line.tokens.len() > 1 || !child.children.is_empty() {
                return Err(row.error(
                    ErrorCode::Syntax,
                    "A row of a glyph is one word of `#` and `.`",
                ));
            }
            if let Some((x, c)) = row
                .text
                .chars()
                .enumerate()
                .find(|&(_, c)| c != '#' && c != '.')
            {
                return Err(Error::at(
                    row.at(x),
                    ErrorCode::Syntax,
                    format!("A glyph's pixels are `#` (ink) or `.` (none), not `{c}`"),
                ));
            }
            let count = row.text.chars().count() as u32;
            match width {
                None => width = Some(count),
                Some(w) if w != count => {
                    let pos = if count < w {
                        row.end()
                    } else {
                        row.at(w as usize)
                    };
                    return Err(Error::at(
                        pos,
                        ErrorCode::SizeMismatch,
                        format!("Row has {count} pixels; the first row of {what} has {w}"),
                    ));
                }
                Some(_) => {}
            }
            pixels.extend(row.text.chars().map(|c| c == '#'));
        }
        let rows = node.children.len() as u32;
        if rows != 0 && rows != height {
            let pos = match node.children.get(height as usize) {
                Some(extra) => extra.line.first().pos,
                None => token.end(),
            };
            return Err(Error::at(
                pos,
                ErrorCode::SizeMismatch,
                format!(
                    "{} has {rows} rows; the font's height is {height}",
                    capitalize(&what)
                ),
            ));
        }
        if width.is_some_and(|w| w > self.limits.max_sprite_side) {
            return Err(token.error(
                ErrorCode::LimitExceeded,
                format!(
                    "{} is wider than the limit of {}",
                    capitalize(&what),
                    self.limits.max_sprite_side
                ),
            ));
        }
        let width = width.unwrap_or(0);
        Ok((
            Glyph {
                ch,
                width,
                advance: advance.unwrap_or(if width == 0 { spacing } else { width + spacing }),
                pixels,
            },
            token,
        ))
    }

    pub(super) fn resolve_fonts(&mut self) -> Vec<Font> {
        let mut out: Vec<Font> = Vec::new();
        let mut seen: HashMap<&str, Position> = HashMap::new();
        for name in &self.failed_fonts {
            seen.entry(name.text).or_insert(name.pos);
        }
        let mut names: Vec<(Token, Font)> = std::mem::take(&mut self.fonts);
        names.sort_by_key(|(t, _)| t.pos);
        for (token, font) in names {
            if let Some(&first) = seen.get(token.text) {
                if first != token.pos {
                    self.errors.push(duplicate(token, "Font", first));
                }
                continue;
            }
            seen.insert(token.text, token.pos);
            out.push(font);
        }
        out
    }
}

/// The characters of the sets named in `covers=`: `ascii` (printable ASCII, U+0020 to
/// U+007E), `latin1` (U+00A0 to U+00FF) and `spanish` (the letters and marks that
/// Spanish adds to ASCII), separated by commas.
fn coverage(token: Token) -> Parsed<Vec<char>> {
    let mut chars: Vec<char> = Vec::new();
    for set in token.text.split(',') {
        let more: Vec<char> = match set {
            "ascii" => (' '..='~').collect(),
            "latin1" => ('\u{a0}'..='\u{ff}').collect(),
            "spanish" => "áéíóúüñÁÉÍÓÚÜÑ¿¡".chars().collect(),
            _ => {
                return Err(token.error(
                    ErrorCode::Syntax,
                    format!(
                        "Unknown character set `{set}`; expected `ascii`, `latin1` or `spanish`"
                    ),
                ));
            }
        };
        for c in more {
            if !chars.contains(&c) {
                chars.push(c);
            }
        }
    }
    Ok(chars)
}

/// A glyph's character: itself, or `U+` and 4 to 6 hexadecimal digits.
fn character(token: Token) -> Parsed<char> {
    let mut chars = token.text.chars();
    if let (Some(c), None) = (chars.next(), chars.next()) {
        return Ok(c);
    }
    let invalid = || {
        token.error(
            ErrorCode::Syntax,
            format!(
                "Expected one character or `U+` and its code, such as `U+003B` for `;`, not `{}`",
                token.text
            ),
        )
    };
    let hex = token.text.strip_prefix("U+").ok_or_else(invalid)?;
    if !(4..=6).contains(&hex.len()) || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
        return Err(invalid());
    }
    u32::from_str_radix(hex, 16)
        .ok()
        .and_then(char::from_u32)
        .ok_or_else(|| {
            token.error(
                ErrorCode::InvalidArgument,
                format!("{} is not a character", token.text),
            )
        })
}

fn number(token: Token, min: u32, max: u32, what: &str) -> Parsed<u32> {
    if token.text.is_empty() || !token.text.bytes().all(|b| b.is_ascii_digit()) {
        return Err(token.error(
            ErrorCode::Syntax,
            format!("Expected a whole number, not `{}`", token.text),
        ));
    }
    match token.text.parse::<u64>() {
        Ok(v) if (min as u64..=max as u64).contains(&v) => Ok(v as u32),
        _ => Err(token.error(
            ErrorCode::InvalidArgument,
            format!("{what} must be {min}..={max}, not {}", token.text),
        )),
    }
}

fn capitalize(text: &str) -> String {
    let mut chars = text.chars();
    match chars.next() {
        Some(c) => c.to_uppercase().chain(chars).collect(),
        None => String::new(),
    }
}