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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Lint: warnings for art that is valid but does not follow pixel-art practice.

mod lines;
mod palette;
mod screens;
mod sets;
mod shading;

use std::cmp::Ordering;
use std::collections::{BinaryHeap, HashMap, HashSet};
use std::fmt;
use std::str::FromStr;

use crate::{Document, Error, ErrorCode, Limits, Position, Sprite};

/// Stable lint categories. Match on the code rather than the message.
///
/// [`LintCode::as_str`] gives the name used in `lint=allow(...)`, `lint allow` and the
/// command-line tool's diagnostics, which is stable too.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum LintCode {
    /// A lone pixel on a flat area, close in lightness to the color around it, which
    /// reads as noise; or an opaque pixel that touches nothing at all.
    OrphanPixel,
    /// Steps along a silhouette's edge whose lengths jump, such as `1, 1, 3, 1, 1`,
    /// where a clean line keeps one length or changes it gradually.
    Jaggy,
    /// An outline two pixels thick at an L corner, where one diagonal step would do.
    DoubleOutline,
    /// A lone translucent pixel in an otherwise opaque sprite: a leftover of smoothing.
    StrayAlpha,
    /// Two keys of a palette, both used, whose colors are too close to tell apart.
    NearDuplicateColor,
    /// A ramp whose hue does not change from end to end: shades that only change in
    /// brightness look dull.
    FlatRamp,
    /// Neighbouring keys of a ramp too close in lightness to tell apart.
    LowContrast,
    /// Shading that darkens toward the edges on every side, with no direction of
    /// light.
    PillowShading,
    /// A line of a shade one pixel thick between a lighter and a darker one: shading
    /// stacked in parallel lines.
    Banding,
    /// A sprite drawn at 2×, 3× or 4× among sprites drawn at 1×.
    Mixel,
    /// More colors than a sprite of its size reads well with.
    TooManyColors,
    /// A sprite of a set (an animation's frames, a group) of another size than most.
    InconsistentSize,
    /// A sprite of a set with no outline, or another, when most have one; or with
    /// one when most have none.
    InconsistentOutline,
    /// A sprite of a set drawn with a palette that is not a copy of the one most use.
    InconsistentPalette,
    /// A sprite of a set drawn at another pixel scale than most.
    InconsistentScale,
    /// A palette key that nothing uses.
    UnusedKey,
    /// An item falls partly outside its screen.
    ScreenBounds,
    /// Opaque pixels from multiple screen items overlap. Opt-in.
    ScreenOverlap,
    /// An item is not aligned to its screen's declared grid.
    OffGrid,
}

impl LintCode {
    /// Every code, in order.
    pub const ALL: [LintCode; 19] = [
        LintCode::OrphanPixel,
        LintCode::Jaggy,
        LintCode::DoubleOutline,
        LintCode::StrayAlpha,
        LintCode::PillowShading,
        LintCode::Banding,
        LintCode::Mixel,
        LintCode::TooManyColors,
        LintCode::InconsistentSize,
        LintCode::InconsistentOutline,
        LintCode::InconsistentPalette,
        LintCode::InconsistentScale,
        LintCode::NearDuplicateColor,
        LintCode::FlatRamp,
        LintCode::LowContrast,
        LintCode::UnusedKey,
        LintCode::ScreenBounds,
        LintCode::ScreenOverlap,
        LintCode::OffGrid,
    ];

    /// The code's stable name in `snake_case`, such as `"orphan_pixel"`.
    pub fn as_str(self) -> &'static str {
        match self {
            LintCode::OrphanPixel => "orphan_pixel",
            LintCode::Jaggy => "jaggy",
            LintCode::DoubleOutline => "double_outline",
            LintCode::StrayAlpha => "stray_alpha",
            LintCode::PillowShading => "pillow_shading",
            LintCode::Banding => "banding",
            LintCode::Mixel => "mixel",
            LintCode::TooManyColors => "too_many_colors",
            LintCode::InconsistentSize => "inconsistent_size",
            LintCode::InconsistentOutline => "inconsistent_outline",
            LintCode::InconsistentPalette => "inconsistent_palette",
            LintCode::InconsistentScale => "inconsistent_scale",
            LintCode::NearDuplicateColor => "near_duplicate_color",
            LintCode::FlatRamp => "flat_ramp",
            LintCode::LowContrast => "low_contrast",
            LintCode::UnusedKey => "unused_key",
            LintCode::ScreenBounds => "screen_bounds",
            LintCode::ScreenOverlap => "screen_overlap",
            LintCode::OffGrid => "off_grid",
        }
    }
}

impl fmt::Display for LintCode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for LintCode {
    type Err = Error;
    fn from_str(name: &str) -> Result<Self, Error> {
        LintCode::ALL
            .into_iter()
            .find(|c| c.as_str() == name)
            .ok_or_else(|| {
                let names: Vec<&str> = LintCode::ALL.iter().map(|c| c.as_str()).collect();
                Error::new(
                    ErrorCode::Syntax,
                    format!(
                        "Unknown lint `{name}`; expected one of {}",
                        names.join(", ")
                    ),
                )
            })
    }
}

/// A lint warning: a stable code, a message and, when it is about a place in the
/// text, its position.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Warning {
    pub code: LintCode,
    pub message: String,
    /// Where in the text the warning is: the pixel's character in its row, or else
    /// the definition's name.
    pub position: Option<Position>,
    /// The imported file that `position` is in, as in [`Error::file`].
    pub file: Option<String>,
    /// The sprite the warning is about, if any.
    pub sprite: Option<String>,
    /// The pixels of that sprite the warning is about, if any.
    pub pixels: Vec<(u32, u32)>,
}

impl fmt::Display for Warning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(file) = &self.file {
            write!(f, "{file}, ")?;
        }
        match self.position {
            Some(p) => write!(f, "line {}, column {}: {}", p.line, p.column, self.message),
            None => f.write_str(&self.message),
        }
    }
}

/// What [`Document::lint`] found: the first warnings, and how many of each code there
/// are in all.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Lint {
    /// The warnings, ordered by file, then position, then code: all of them, or the
    /// first [`Limits::max_warnings`].
    pub warnings: Vec<Warning>,
    /// Each code found, in the order of [`LintCode::ALL`], with how many warnings it
    /// has, those beyond the limit included.
    pub counts: Vec<(LintCode, usize)>,
}

impl Lint {
    /// How many warnings there are, those beyond the limit included.
    pub fn total(&self) -> usize {
        self.counts.iter().map(|&(_, n)| n).sum()
    }
    /// How many warnings of `code` there are, those beyond the limit included.
    pub fn count(&self, code: LintCode) -> usize {
        self.counts
            .iter()
            .find(|&&(c, _)| c == code)
            .map_or(0, |&(_, n)| n)
    }
    /// How many warnings were left out of [`Lint::warnings`] by the limit.
    pub fn omitted(&self) -> usize {
        self.total() - self.warnings.len()
    }
    /// Whether there are no warnings at all.
    pub fn is_clean(&self) -> bool {
        self.counts.is_empty()
    }
}

/// What [`Document::lint`] checks.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LintOptions {
    /// Check opaque screen item overlaps. Default false.
    pub screen_overlap: bool,
    /// Minimum text/background OKLab lightness difference, in ten-thousandths.
    /// Default 2500 (0.25).
    pub min_text_contrast: u32,
    /// Codes that are not reported, besides those the text allows.
    pub allow: Vec<LintCode>,
    /// The largest difference in lightness, 0..=255, between a lone pixel and the
    /// color around it for `orphan_pixel` to report it. A lone pixel that stands out
    /// more, such as an eye or a glint, is taken as deliberate. Default 32.
    pub orphan_contrast: u8,
    /// How near, in percent of the sprite's shorter side, the light must sit to the
    /// middle of the fill for `pillow_shading`. Default 10.
    pub pillow_offset: u32,
    /// The shortest line of one shade that `banding` reports, in pixels. Default 4.
    pub banding_length: usize,
    /// The most colors for `too_many_colors` in a sprite under 32×32. Default 8.
    pub max_colors_small: usize,
    /// The most colors for `too_many_colors` in any sprite. Default 16.
    pub max_colors: usize,
    /// The smallest OKLab distance, in ten-thousandths, between two used keys of a
    /// palette before `near_duplicate_color` reports them. Default 300 (0.03).
    pub min_color_distance: u32,
    /// The smallest step of OKLab lightness, in ten-thousandths, between neighbouring
    /// keys of a ramp before `low_contrast` reports them. Default 600 (0.06).
    pub min_ramp_step: u32,
}

impl Default for LintOptions {
    fn default() -> Self {
        Self {
            screen_overlap: false,
            min_text_contrast: 2500,
            allow: Vec::new(),
            orphan_contrast: 32,
            pillow_offset: 10,
            banding_length: 4,
            max_colors_small: 8,
            max_colors: 16,
            min_color_distance: 300,
            min_ramp_step: 600,
        }
    }
}

/// The kinds of definitions that `lint=` can be given to.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) enum Kind {
    Palette,
    Sprite,
    Animation,
    Tileset,
    Tilemap,
    Font,
    Group,
    States,
    Icons,
    Screen,
}

/// Where definitions are in the text, and what lint they allow: what a [`Document`]
/// keeps of its text for [`Document::lint`].
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct Sources {
    /// The name of each file of the parse; the first, the text itself, is empty.
    pub files: Vec<String>,
    /// The codes that `lint allow` allows in each file.
    pub file_allows: Vec<Vec<LintCode>>,
    pub defs: Vec<Def>,
    /// Palettes and the keys that sprites used from them midway through chains, such
    /// as `q` in `s:swap(q):outline(k):swap(p)`.
    pub palette_uses: std::collections::BTreeSet<(String, char)>,
    /// Sprites that belong together.
    pub sets: Vec<Set>,
}

/// Sprites that belong together: an animation's frames, a group's sprites, or a
/// tileset's tiles, each by the name of its sprite in the document, at the place
/// that names it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Set {
    pub kind: Kind,
    pub name: String,
    pub members: Vec<Member>,
}

/// A sprite of a set: its name in the document, the place that names it, and the
/// sprites it is made from, whose `lint=` reaches it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Member {
    pub name: String,
    pub pos: Position,
    pub sources: Vec<String>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Def {
    pub kind: Kind,
    pub name: String,
    pub pos: Position,
    pub allow: Vec<LintCode>,
    /// What it is built from: the sprites of a sprite, the palette a palette copies.
    pub deps: Vec<(Kind, String)>,
    pub detail: Detail,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) enum Detail {
    Other,
    /// A sprite with a size: its layers as written, bottom first.
    Drawn(Vec<DrawnLayer>),
    /// A texture, and the keys of its ramp.
    Texture(Vec<char>),
    /// A palette: the palette it copies, and the keys it writes, at their positions.
    Palette {
        from: Option<String>,
        keys: Vec<(char, Position)>,
        /// Its own ramps.
        ramps: Vec<RampSource>,
    },
}

/// A ramp as a palette writes it: its name, the name's position, and its keys at
/// their positions.
pub(crate) type RampSource = (String, Position, Vec<(char, Position)>);

/// A layer as written in a sprite: rows, or a sprite it is made from.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct DrawnLayer {
    /// How many of the sprite's layers it makes: one for rows, as many as its sprite
    /// has for a sprite.
    pub layers: usize,
    /// The first character of each row, for rows.
    pub rows: Option<Vec<Position>>,
}

/// The definitions by kind and name, the first of each.
type Index<'s> = HashMap<(Kind, &'s str), &'s Def>;

impl Sources {
    fn index(&self) -> Index<'_> {
        let mut index = HashMap::new();
        for def in &self.defs {
            index.entry((def.kind, def.name.as_str())).or_insert(def);
        }
        index
    }

    /// Whether the text allows `code` for the definition: by its own `lint=` or its
    /// file's `lint allow`, or by the `lint=` of the sprites it is built from. A
    /// file's `lint allow` stays in that file, even for sprites built from its own.
    fn allows(&self, index: &Index, kind: Kind, name: &str, code: LintCode) -> bool {
        self.allows_where(index, kind, name, code, true)
    }

    /// [`Sources::allows`], with the file's `lint allow` only when `file`.
    fn allows_where(
        &self,
        index: &Index,
        kind: Kind,
        name: &str,
        code: LintCode,
        file: bool,
    ) -> bool {
        if let Some(def) = index.get(&(kind, name)).filter(|_| file) {
            let file = self.file_allows.get(def.pos.file as usize);
            if file.is_some_and(|f| f.contains(&code)) {
                return true;
            }
        }
        let mut seen = HashSet::new();
        let mut stack = vec![(kind, name)];
        while let Some((kind, name)) = stack.pop() {
            if !seen.insert((kind, name)) {
                continue;
            }
            let Some(def) = index.get(&(kind, name)) else {
                continue;
            };
            if def.allow.contains(&code) {
                return true;
            }
            stack.extend(def.deps.iter().map(|(kind, name)| (*kind, name.as_str())))
        }
        false
    }
}

/// Collects warnings: drops those allowed, counts the rest, and keeps only the first
/// `limit` in order, so that memory stays bounded however many there are.
pub(super) struct Sink<'s> {
    sources: &'s Sources,
    index: Index<'s>,
    options: &'s LintOptions,
    limit: usize,
    /// Whether each (kind, code, name) is allowed, once worked out.
    allowed: HashMap<(Kind, LintCode), HashMap<String, bool>>,
    counts: [usize; LintCode::ALL.len()],
    /// The first warnings so far, with the last of them on top.
    kept: BinaryHeap<Kept>,
    found: usize,
}

/// A kept warning, ordered by file, position and code, then by when it was found.
struct Kept {
    key: (bool, Option<Position>, LintCode, usize),
    warning: Warning,
}

impl PartialEq for Kept {
    fn eq(&self, other: &Self) -> bool {
        self.key == other.key
    }
}
impl Eq for Kept {}
impl PartialOrd for Kept {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for Kept {
    fn cmp(&self, other: &Self) -> Ordering {
        self.key.cmp(&other.key)
    }
}

/// A warning about to be reported, built only if it is kept.
pub(super) struct Found<'f> {
    code: LintCode,
    /// The definition whose `lint=` can allow it.
    kind: Kind,
    name: &'f str,
    position: Option<Position>,
    sprite: Option<&'f str>,
    pixels: Vec<(u32, u32)>,
}

impl<'s> Sink<'s> {
    fn new(sources: &'s Sources, options: &'s LintOptions, limit: usize) -> Self {
        Self {
            sources,
            index: sources.index(),
            options,
            limit,
            allowed: HashMap::new(),
            counts: [0; LintCode::ALL.len()],
            kept: BinaryHeap::new(),
            found: 0,
        }
    }

    /// Whether `code` is allowed for the definition: by the options or the text.
    pub(super) fn allowed(&mut self, code: LintCode, kind: Kind, name: &str) -> bool {
        if self.options.allow.contains(&code) {
            return true;
        }
        let known = self
            .allowed
            .get(&(kind, code))
            .and_then(|names| names.get(name))
            .copied();
        known.unwrap_or_else(|| {
            let allowed = self.sources.allows(&self.index, kind, name, code);
            self.allowed
                .entry((kind, code))
                .or_default()
                .insert(name.to_owned(), allowed);
            allowed
        })
    }

    /// Whether `code` is allowed for a sprite by the options or by `lint=` on it or
    /// on what it is made from; not by its file's `lint allow`, which stays in that
    /// file.
    pub(super) fn allowed_by_definition(&self, code: LintCode, kind: Kind, name: &str) -> bool {
        self.options.allow.contains(&code)
            || self
                .sources
                .allows_where(&self.index, kind, name, code, false)
    }

    pub(super) fn push(&mut self, found: Found, message: impl FnOnce() -> String) {
        let code = found.code;
        if self.allowed(code, found.kind, found.name) {
            return;
        }
        if let Some(i) = LintCode::ALL.iter().position(|&c| c == code) {
            self.counts[i] += 1;
        }
        let key = (found.position.is_none(), found.position, code, self.found);
        self.found += 1;
        let full = self.kept.len() >= self.limit;
        if full && self.kept.peek().is_none_or(|last| key >= last.key) {
            return;
        }
        let file = found
            .position
            .filter(|p| p.file > 0)
            .and_then(|p| self.sources.files.get(p.file as usize).cloned());
        self.kept.push(Kept {
            key,
            warning: Warning {
                code,
                message: message(),
                position: found.position,
                file,
                sprite: found.sprite.map(str::to_owned),
                pixels: found.pixels,
            },
        });
        if self.kept.len() > self.limit {
            self.kept.pop();
        }
    }

    fn finish(self) -> Lint {
        Lint {
            warnings: self
                .kept
                .into_sorted_vec()
                .into_iter()
                .map(|k| k.warning)
                .collect(),
            counts: LintCode::ALL
                .into_iter()
                .zip(self.counts)
                .filter(|&(_, n)| n > 0)
                .collect(),
        }
    }
}

/// Runs the checks; see [`Document::lint`].
pub(crate) fn run(document: &Document, options: &LintOptions, limits: &Limits) -> Lint {
    let sources = &document.sources;
    let mut sink = Sink::new(sources, options, limits.max_warnings);
    let index = sources.index();
    // Each drawn sprite's pixel scale, for `mixel`.
    let mut scales = Vec::new();
    for sprite in &document.sprites {
        let Some(def) = index.get(&(Kind::Sprite, sprite.name.as_str())) else {
            continue;
        };
        if let Detail::Drawn(layers) = &def.detail {
            let canvas = lines::Canvas::new(sprite);
            let report = lines::Report {
                sprite,
                def,
                layers,
            };
            lines::check(&canvas, &report, options, &mut sink);
            shading::check(&canvas, &report, options, &mut sink);
            scales.push((shading::scale(&canvas), report));
        }
    }
    shading::mixels(&scales, &mut sink);
    sets::check(document, &index, &mut sink);
    palette::check(document, &index, options, &mut sink);
    screens::check(document, options, limits, &mut sink);
    sink.finish()
}

/// The character in the text that draws pixel `(x, y)`, when the layer seen there is
/// written as rows; `None` when it comes from another sprite.
pub(super) fn pixel_position(
    sprite: &Sprite,
    layers: &[DrawnLayer],
    x: u32,
    y: u32,
) -> Option<Position> {
    let seen = sprite.top_layer(x, y)?;
    let mut first = 0;
    for layer in layers {
        if seen < first + layer.layers {
            let start = layer.rows.as_ref()?.get(y as usize)?;
            return Some(Position::in_file(
                start.file,
                start.line,
                start.column + x as usize,
            ));
        }
        first += layer.layers;
    }
    None
}