rpic-core 0.11.0

Core engine for rpic: lexer, parser, geometry, and SVG/PNG/PDF backends for the pic graphics language.
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
//! Colour-name validation. rpic passes a resolved colour string straight to
//! the SVG backend; this module decides whether that string is a colour any
//! SVG renderer will understand, so an unknown name (a typo, a mis-cased
//! keyword) can be flagged with a warning instead of silently producing blank
//! ink. It does **not** map or alter colours — only classifies them.
//!
//! Accepted forms: `#rgb` / `#rgba` / `#rrggbb` / `#rrggbbaa` hex, the CSS
//! functional notations (`rgb()`/`rgba()`/`hsl()`/`hsla()`/`color()`), the
//! CSS Color Module Level 4 named colours (case-insensitive), the special
//! keywords `none`/`transparent`/`currentColor`, and the dvips/xcolor named
//! colours (`Dandelion`, `Goldenrod`, …) that the dpic corpus carries.

/// The 148 CSS Color Module Level 4 extended colour keywords, lowercase.
static CSS_NAMED: &[&str] = &[
    "aliceblue",
    "antiquewhite",
    "aqua",
    "aquamarine",
    "azure",
    "beige",
    "bisque",
    "black",
    "blanchedalmond",
    "blue",
    "blueviolet",
    "brown",
    "burlywood",
    "cadetblue",
    "chartreuse",
    "chocolate",
    "coral",
    "cornflowerblue",
    "cornsilk",
    "crimson",
    "cyan",
    "darkblue",
    "darkcyan",
    "darkgoldenrod",
    "darkgray",
    "darkgreen",
    "darkgrey",
    "darkkhaki",
    "darkmagenta",
    "darkolivegreen",
    "darkorange",
    "darkorchid",
    "darkred",
    "darksalmon",
    "darkseagreen",
    "darkslateblue",
    "darkslategray",
    "darkslategrey",
    "darkturquoise",
    "darkviolet",
    "deeppink",
    "deepskyblue",
    "dimgray",
    "dimgrey",
    "dodgerblue",
    "firebrick",
    "floralwhite",
    "forestgreen",
    "fuchsia",
    "gainsboro",
    "ghostwhite",
    "gold",
    "goldenrod",
    "gray",
    "green",
    "greenyellow",
    "grey",
    "honeydew",
    "hotpink",
    "indianred",
    "indigo",
    "ivory",
    "khaki",
    "lavender",
    "lavenderblush",
    "lawngreen",
    "lemonchiffon",
    "lightblue",
    "lightcoral",
    "lightcyan",
    "lightgoldenrodyellow",
    "lightgray",
    "lightgreen",
    "lightgrey",
    "lightpink",
    "lightsalmon",
    "lightseagreen",
    "lightskyblue",
    "lightslategray",
    "lightslategrey",
    "lightsteelblue",
    "lightyellow",
    "lime",
    "limegreen",
    "linen",
    "magenta",
    "maroon",
    "mediumaquamarine",
    "mediumblue",
    "mediumorchid",
    "mediumpurple",
    "mediumseagreen",
    "mediumslateblue",
    "mediumspringgreen",
    "mediumturquoise",
    "mediumvioletred",
    "midnightblue",
    "mintcream",
    "mistyrose",
    "moccasin",
    "navajowhite",
    "navy",
    "oldlace",
    "olive",
    "olivedrab",
    "orange",
    "orangered",
    "orchid",
    "palegoldenrod",
    "palegreen",
    "paleturquoise",
    "palevioletred",
    "papayawhip",
    "peachpuff",
    "peru",
    "pink",
    "plum",
    "powderblue",
    "purple",
    "rebeccapurple",
    "red",
    "rosybrown",
    "royalblue",
    "saddlebrown",
    "salmon",
    "sandybrown",
    "seagreen",
    "seashell",
    "sienna",
    "silver",
    "skyblue",
    "slateblue",
    "slategray",
    "slategrey",
    "snow",
    "springgreen",
    "steelblue",
    "tan",
    "teal",
    "thistle",
    "tomato",
    "turquoise",
    "violet",
    "wheat",
    "white",
    "whitesmoke",
    "yellow",
    "yellowgreen",
];

/// The 68 dvips/xcolor named colours (as written — CamelCase). The dpic corpus
/// and circuit_macros figures use these (`shaded "Dandelion"`). Kept
/// case-sensitively: they are distinct keywords, not CSS names.
static XCOLOR_NAMED: &[&str] = &[
    "Apricot",
    "Aquamarine",
    "Bittersweet",
    "Black",
    "Blue",
    "BlueGreen",
    "BlueViolet",
    "BrickRed",
    "Brown",
    "BurntOrange",
    "CadetBlue",
    "CarnationPink",
    "Cerulean",
    "CornflowerBlue",
    "Cyan",
    "Dandelion",
    "DarkOrchid",
    "Emerald",
    "ForestGreen",
    "Fuchsia",
    "Goldenrod",
    "Gray",
    "Green",
    "GreenYellow",
    "JungleGreen",
    "Lavender",
    "LimeGreen",
    "Magenta",
    "Mahogany",
    "Maroon",
    "Melon",
    "MidnightBlue",
    "Mulberry",
    "NavyBlue",
    "OliveGreen",
    "Orange",
    "OrangeRed",
    "Orchid",
    "Peach",
    "Periwinkle",
    "PineGreen",
    "Plum",
    "ProcessBlue",
    "Purple",
    "RawSienna",
    "Red",
    "RedOrange",
    "RedViolet",
    "Rhodamine",
    "RoyalBlue",
    "RoyalPurple",
    "RubineRed",
    "Salmon",
    "SeaGreen",
    "Sepia",
    "SkyBlue",
    "SpringGreen",
    "Tan",
    "TealBlue",
    "Thistle",
    "Turquoise",
    "Violet",
    "VioletRed",
    "White",
    "WildStrawberry",
    "Yellow",
    "YellowGreen",
    "YellowOrange",
];

/// The dvips names browsers can't render, mapped to their RGB — derived from
/// `dvipsnam.def`'s cmyk values (channel = 1 − min(1, c + k)); `Dandelion`
/// checks out against man19.pic's own comment (`1, 0.71, 0.16` → `#ffb529`).
/// Deliberately excludes the xcolor names that are *also* CSS keywords
/// (`Goldenrod`, `Plum`, …): those render natively, case-insensitively, and
/// remapping them to the (different!) dvips values would change figures that
/// already display correctly. Sorted for binary search.
static XCOLOR_HEX: &[(&str, &str)] = &[
    ("Apricot", "#ffad7a"),
    ("Bittersweet", "#c20300"),
    ("BlueGreen", "#26ffab"),
    ("BrickRed", "#b80000"),
    ("BurntOrange", "#ff7d00"),
    ("CarnationPink", "#ff5eff"),
    ("Cerulean", "#0fe3ff"),
    ("Dandelion", "#ffb529"),
    ("Emerald", "#00ff80"),
    ("JungleGreen", "#03ff7a"),
    ("Mahogany", "#a60000"),
    ("Melon", "#ff8a80"),
    ("Mulberry", "#a314fa"),
    ("NavyBlue", "#0f75ff"),
    ("OliveGreen", "#009900"),
    ("Peach", "#ff804d"),
    ("Periwinkle", "#6e73ff"),
    ("PineGreen", "#00bf29"),
    ("ProcessBlue", "#0affff"),
    ("RawSienna", "#8c0000"),
    ("RedOrange", "#ff3b21"),
    ("RedViolet", "#9600a8"),
    ("Rhodamine", "#ff2eff"),
    ("RoyalPurple", "#4019ff"),
    ("RubineRed", "#ff00de"),
    ("Sepia", "#4d0000"),
    ("TealBlue", "#1ffaa3"),
    ("VioletRed", "#ff30ff"),
    ("WildStrawberry", "#ff0a9c"),
    ("YellowOrange", "#ff9400"),
];

/// The hex for a dvips/xcolor name **no browser understands** (`Dandelion` →
/// `#ffb529`), or `None` for everything else — including the xcolor names that
/// are also CSS keywords, which must stay untouched.
pub fn xcolor_hex(name: &str) -> Option<&'static str> {
    XCOLOR_HEX
        .binary_search_by_key(&name, |(n, _)| n)
        .ok()
        .map(|i| XCOLOR_HEX[i].1)
}

/// Is `s` a colour an SVG renderer will understand? Used to warn on an unknown
/// colour name; a `false` result never blocks rendering, it only flags.
pub fn is_valid_color(s: &str) -> bool {
    let t = s.trim();
    if t.is_empty() {
        return false;
    }
    if is_hex(t) || is_functional(t) {
        return true;
    }
    // `none`/`transparent`/`currentColor` are valid SVG paint keywords.
    if t.eq_ignore_ascii_case("none")
        || t.eq_ignore_ascii_case("transparent")
        || t.eq_ignore_ascii_case("currentcolor")
    {
        return true;
    }
    let lower = t.to_ascii_lowercase();
    CSS_NAMED.binary_search(&lower.as_str()).is_ok() || XCOLOR_NAMED.contains(&t)
}

/// Nearest known colour name to `s` (edit distance ≤ 2) for a "did you mean"
/// hint. Tries the CSS keywords (case-insensitively) then the dvips/xcolor
/// names (case-sensitive CamelCase) — so `"Dandelio"` suggests `Dandelion`.
pub fn suggest(s: &str) -> Option<&'static str> {
    crate::diagnostic::closest(&s.to_ascii_lowercase(), CSS_NAMED)
        .or_else(|| crate::diagnostic::closest(s, XCOLOR_NAMED))
}

/// `#` followed by exactly 3, 4, 6, or 8 hex digits.
fn is_hex(s: &str) -> bool {
    let Some(hex) = s.strip_prefix('#') else {
        return false;
    };
    matches!(hex.len(), 3 | 4 | 6 | 8) && hex.bytes().all(|b| b.is_ascii_hexdigit())
}

/// A CSS functional colour: `rgb(`/`rgba(`/`hsl(`/`hsla(`/`color(` … `)`. The
/// components aren't validated — rpic's own `rgb()` literal already resolves to
/// hex, so this only accepts strings a user passed through verbatim.
fn is_functional(s: &str) -> bool {
    let lower = s.to_ascii_lowercase();
    ["rgb(", "rgba(", "hsl(", "hsla(", "color("]
        .iter()
        .any(|p| lower.starts_with(p))
        && lower.ends_with(')')
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn css_list_is_sorted_for_binary_search() {
        let mut sorted = CSS_NAMED.to_vec();
        sorted.sort_unstable();
        assert_eq!(CSS_NAMED, sorted.as_slice(), "CSS_NAMED must stay sorted");
    }

    #[test]
    fn xcolor_hex_table_is_sorted_and_consistent() {
        let mut sorted = XCOLOR_HEX.to_vec();
        sorted.sort_unstable_by_key(|(n, _)| *n);
        assert_eq!(XCOLOR_HEX, sorted.as_slice(), "XCOLOR_HEX must stay sorted");
        for (name, hex) in XCOLOR_HEX {
            // every mapped name is a recognised xcolor name …
            assert!(XCOLOR_NAMED.contains(name), "{name} not in XCOLOR_NAMED");
            // … that is NOT also a CSS keyword (those must stay unmapped) …
            assert!(
                CSS_NAMED
                    .binary_search(&name.to_ascii_lowercase().as_str())
                    .is_err(),
                "{name} is a CSS keyword and must not be remapped"
            );
            // … and maps to well-formed hex
            assert!(is_hex(hex), "bad hex for {name}: {hex}");
        }
    }

    #[test]
    fn every_valid_xcolor_name_actually_renders() {
        // `is_valid_color` accepts every XCOLOR_NAMED entry (so it doesn't
        // warn), so each MUST render to real paint — either it's also a CSS
        // keyword the browser knows, or we remap it to hex. A future name that
        // is neither would validate yet paint nothing (the silent-blank-ink
        // failure this module exists to prevent).
        for name in XCOLOR_NAMED {
            let is_css = CSS_NAMED
                .binary_search(&name.to_ascii_lowercase().as_str())
                .is_ok();
            assert!(
                is_css || xcolor_hex(name).is_some(),
                "{name} is a valid xcolor name but neither a CSS keyword nor remapped to hex — it would render blank"
            );
        }
    }

    #[test]
    fn xcolor_hex_maps_non_css_names_only() {
        // dvipsnam.def: Dandelion = cmyk(0,.29,.84,0) -> rgb(1,.71,.16)
        assert_eq!(xcolor_hex("Dandelion"), Some("#ffb529"));
        // Goldenrod IS a CSS keyword — browsers render it; stays untouched
        assert_eq!(xcolor_hex("Goldenrod"), None);
        // case matters: the CSS name `dandelion` doesn't exist, and the
        // xcolor spelling is CamelCase — lowercase input is not remapped
        assert_eq!(xcolor_hex("dandelion"), None);
        assert_eq!(xcolor_hex("notacolor"), None);
    }

    #[test]
    fn suggest_covers_css_and_xcolor() {
        assert_eq!(suggest("crimsom"), Some("crimson")); // CSS typo
        assert_eq!(suggest("Dandelio"), Some("Dandelion")); // xcolor typo (#291)
        assert_eq!(suggest("zzzzzzzz"), None); // nothing close
    }

    #[test]
    fn accepts_valid_forms() {
        for c in [
            "red",
            "Red",
            "REBECCAPURPLE",
            "cornflowerblue",
            "#1b5e20",
            "#abc",
            "#12345678",
            "#abcd",
            "rgb(1,2,3)",
            "rgba(1,2,3,0.5)",
            "hsl(120, 50%, 50%)",
            "none",
            "transparent",
            "currentColor",
            "Dandelion",
            "Goldenrod",
        ] {
            assert!(is_valid_color(c), "should be valid: {c}");
        }
    }

    #[test]
    fn rejects_invalid_forms() {
        for c in [
            "notacolor",
            "crimsom",
            "#12g456",
            "#ab",
            "#abcde",
            "0xff0000",
            "dandelion",
            "",
            "rgb(1,2,3",
        ] {
            assert!(!is_valid_color(c), "should be invalid: {c}");
        }
    }
}