ferravex_render/
icons.rs

1//! Procedural vector icons: a shared visual vocabulary that replaces words.
2//!
3//! Every icon is drawn from anti-aliased [`crate::shapes`] primitives inside a
4//! square box, so it stays crisp at any size from small HUD glyphs (12 px) to
5//! large menu art (128 px) and batches with ordinary immediate drawing. Icons
6//! are two-tone: the main `color` paints the silhouette and a darker `detail`
7//! tone paints inner marks (a coin's rim, a clock's hands), so they read on
8//! both light and dark surfaces without needing a cut-out background.
9//!
10//! Coordinates are the current camera's logical units. Nothing allocates.
11//! Non-finite input or a non-positive size draws nothing.
12//!
13//! ```
14//! use ferravex_render::{Color, vec2, icons::{self, Icon, IconStyle}};
15//! icons::draw(Icon::Heart, vec2(24., 24.), 24., Color::from_hex(0xe5484d));
16//! icons::draw_styled(Icon::Coin, vec2(60., 24.), 24.,
17//!     IconStyle::new(Color::from_hex(0xf7b553)).shadow(Color::new(0., 0., 0., 0.4)));
18//! icons::badge(Icon::Pause, vec2(100., 24.), 32., Color::from_hex(0x1f2a3d),
19//!     Color::from_hex(0xeef2f5));
20//! assert_eq!(Icon::from_name("heart"), Some(Icon::Heart));
21//! assert!(Icon::ALL.iter().all(|i| Icon::from_name(i.name()) == Some(*i)));
22//! ```
23use crate::shapes::{self, Cap, Join, Stroke};
24use crate::{Color, Rect, Vec2, vec2};
25use std::f32::consts::{FRAC_PI_2, PI, TAU};
26
27macro_rules! icons {
28    ($($(#[$doc:meta])* $variant:ident => $name:literal,)*) => {
29        /// A named pictogram. See [`Icon::ALL`] for the complete set.
30        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
31        pub enum Icon {
32            $($(#[$doc])* $variant,)*
33        }
34        impl Icon {
35            /// Every icon, in declaration order (useful for specimen sheets).
36            pub const ALL: &'static [Icon] = &[$(Icon::$variant,)*];
37            /// Stable lowercase identifier, such as `"heart"`.
38            pub const fn name(self) -> &'static str {
39                match self {
40                    $(Icon::$variant => $name,)*
41                }
42            }
43            /// The icon whose [`Icon::name`] equals `name`.
44            pub fn from_name(name: &str) -> Option<Icon> {
45                match name {
46                    $($name => Some(Icon::$variant),)*
47                    _ => None,
48                }
49            }
50        }
51    };
52}
53
54icons! {
55    // Interface and menus.
56    /// Right-pointing triangle: start, resume, run.
57    Play => "play",
58    /// Two bars: pause.
59    Pause => "pause",
60    /// Circular arrow: restart, retry.
61    Restart => "restart",
62    /// Counter-clockwise hook arrow: undo, take back.
63    Undo => "undo",
64    /// Tick mark: done, correct, accepted.
65    Check => "check",
66    /// Diagonal cross: wrong, close, rejected.
67    Cross => "cross",
68    Plus => "plus",
69    Minus => "minus",
70    ArrowUp => "arrow_up",
71    ArrowDown => "arrow_down",
72    ArrowLeft => "arrow_left",
73    ArrowRight => "arrow_right",
74    /// Question mark: help, unknown.
75    Question => "question",
76    /// Lowercase i: information, details.
77    Info => "info",
78    /// Exclamation in a triangle: warning, danger.
79    Alert => "alert",
80    /// Speaker with waves.
81    SoundOn => "sound_on",
82    /// Speaker with a cross.
83    SoundOff => "sound_off",
84    /// Cog: settings, machinery.
85    Gear => "gear",
86    /// Game controller: controls, input.
87    Gamepad => "gamepad",
88    /// Mouse arrow cursor.
89    Pointer => "pointer",
90    /// Raised hand / tap gesture.
91    Hand => "hand",
92    Eye => "eye",
93    /// Eye with a slash: hidden, unseen.
94    EyeOff => "eye_off",
95    Lock => "lock",
96    Unlock => "unlock",
97    Key => "key",
98    Home => "home",
99    Trophy => "trophy",
100    Crown => "crown",
101    Flag => "flag",
102    /// Concentric rings: aim, goal.
103    Target => "target",
104    /// Map marker teardrop.
105    Pin => "pin",
106    /// Folded map.
107    Map => "map",
108    /// Magnifying glass: search, inspect.
109    Search => "search",
110    Bell => "bell",
111    // Stats and resources.
112    Star => "star",
113    Heart => "heart",
114    Skull => "skull",
115    Clock => "clock",
116    Hourglass => "hourglass",
117    Calendar => "calendar",
118    /// Lightning bolt: energy, power, speed.
119    Bolt => "bolt",
120    Battery => "battery",
121    Shield => "shield",
122    Sword => "sword",
123    /// Round bomb with a fuse; also a cannonball.
124    Bomb => "bomb",
125    Flame => "flame",
126    /// Water droplet.
127    Drop => "drop",
128    Snowflake => "snowflake",
129    Sun => "sun",
130    Moon => "moon",
131    Cloud => "cloud",
132    Rain => "rain",
133    Wind => "wind",
134    /// Two water waves.
135    Wave => "wave",
136    Leaf => "leaf",
137    /// Seedling with two leaves.
138    Sprout => "sprout",
139    Tree => "tree",
140    Mountain => "mountain",
141    Coin => "coin",
142    /// Faceted gemstone: treasure, crystal, amber.
143    Gem => "gem",
144    /// Tied sack: inventory, goods.
145    Bag => "bag",
146    /// Wooden crate: cargo, supplies, parcels.
147    Crate => "crate",
148    Scroll => "scroll",
149    Book => "book",
150    /// Light bulb: idea, light, research.
151    Bulb => "bulb",
152    Hammer => "hammer",
153    Wrench => "wrench",
154    Pickaxe => "pickaxe",
155    Anchor => "anchor",
156    /// Sailing ship.
157    Ship => "ship",
158    Rocket => "rocket",
159    /// Ringed planet.
160    Planet => "planet",
161    Train => "train",
162    Person => "person",
163    People => "people",
164    Paw => "paw",
165    Bone => "bone",
166    /// Round flask: potion, remedy.
167    Potion => "potion",
168    Fish => "fish",
169    /// Eighth note.
170    Music => "music",
171    /// Speech bubble.
172    Chat => "chat",
173    /// Cooking pot.
174    Pot => "pot",
175    Thermometer => "thermometer",
176    /// Fuel can.
177    Fuel => "fuel",
178    /// Two chevrons: fast forward, speed up, skip ahead.
179    FastForward => "fast_forward",
180    /// Horseshoe magnet: attract, collect, crane.
181    Magnet => "magnet",
182    /// Beetle: pests, enemies, bugs.
183    Bug => "bug",
184    /// Two interlocking links: chains, combos, links.
185    Chain => "chain",
186    /// Four-way arrows: move, steer, drag.
187    Move => "move",
188    /// Two fanned playing cards: deck, hand, draw pile.
189    Cards => "cards",
190    /// A ball with a highlight: golf, sports, projectiles.
191    Ball => "ball",
192}
193
194/// Colors for one icon draw.
195#[derive(Clone, Copy, Debug, PartialEq)]
196pub struct IconStyle {
197    /// Silhouette color.
198    pub color: Color,
199    /// Inner marks; [`IconStyle::new`] derives a darker shade of `color`.
200    pub detail: Color,
201    /// Optional soft drop shadow drawn under the silhouette.
202    pub shadow: Option<Color>,
203}
204
205impl IconStyle {
206    /// Two-tone style whose detail tone is `color` mixed 45% toward black.
207    pub fn new(color: Color) -> Self {
208        let dark = shapes::mix(color, Color::new(0., 0., 0., color.a), 0.45);
209        Self {
210            color,
211            detail: Color::new(dark.r, dark.g, dark.b, color.a),
212            shadow: None,
213        }
214    }
215    /// Replace the inner-mark color.
216    pub fn detail(mut self, detail: Color) -> Self {
217        self.detail = detail;
218        self
219    }
220    /// Add a soft drop shadow in `color`.
221    pub fn shadow(mut self, color: Color) -> Self {
222        self.shadow = Some(color);
223        self
224    }
225}
226
227/// Draw `icon` centered at `center`, fitted to a `size` × `size` box.
228pub fn draw(icon: Icon, center: Vec2, size: f32, color: Color) {
229    draw_styled(icon, center, size, IconStyle::new(color));
230}
231
232/// Draw `icon` with explicit detail and shadow colors.
233pub fn draw_styled(icon: Icon, center: Vec2, size: f32, style: IconStyle) {
234    if !(center.is_finite() && size.is_finite() && size > 0.) {
235        return;
236    }
237    if let Some(shadow) = style.shadow {
238        let offset = vec2(0., (size * 0.06).max(1.));
239        let pen = Pen::new(center + offset, size);
240        paint(icon, &pen, shadow, shadow);
241    }
242    let pen = Pen::new(center, size);
243    paint(icon, &pen, style.color, style.detail);
244}
245
246/// A filled circle of `diameter` with `icon` centered on it at 58% size.
247pub fn badge(icon: Icon, center: Vec2, diameter: f32, background: Color, foreground: Color) {
248    if !(center.is_finite() && diameter.is_finite() && diameter > 0.) {
249        return;
250    }
251    shapes::fill_circle(center, diameter * 0.5, background);
252    let detail = shapes::mix(foreground, background, 0.55);
253    draw_styled(
254        icon,
255        center,
256        diameter * 0.58,
257        IconStyle::new(foreground).detail(Color::new(detail.r, detail.g, detail.b, foreground.a)),
258    );
259}
260
261// ---------------------------------------------------------------------------
262// Drawing kit. Icons are authored in a unit box `[-1, 1]²` with Y down and
263// angles in radians, clockwise on screen from +X (as `shapes::stroke_arc`).
264
265/// Degrees to radians, for readable authoring.
266const fn deg(degrees: f32) -> f32 {
267    degrees * (PI / 180.)
268}
269
270/// Most points in one outline, fillet arcs included.
271const MAX_POINTS: usize = 160;
272
273/// Maps the unit box `[-1, 1]²` (Y down) onto the destination square, with an
274/// optional rotation for sub-drawings such as tools lying on the diagonal.
275#[derive(Clone, Copy)]
276struct Pen {
277    center: Vec2,
278    half: f32,
279    /// `(cos, sin)` of `angle`, the clockwise rotation of unit-box points.
280    turn: Vec2,
281    angle: f32,
282}
283
284impl Pen {
285    fn new(center: Vec2, size: f32) -> Self {
286        Self {
287            center,
288            half: size * 0.5,
289            turn: Vec2::X,
290            angle: 0.,
291        }
292    }
293    /// A pen whose unit box is centered at `(x, y)`, scaled by `scale` and
294    /// turned `angle` radians clockwise.
295    fn sub(&self, x: f32, y: f32, scale: f32, angle: f32) -> Self {
296        let angle = self.angle + angle;
297        Self {
298            center: self.p(x, y),
299            half: self.half * scale,
300            turn: if angle == 0. {
301                Vec2::X
302            } else {
303                Vec2::from_angle(angle)
304            },
305            angle,
306        }
307    }
308    /// Unit-box point to logical units.
309    fn p(&self, x: f32, y: f32) -> Vec2 {
310        self.center + vec2(x, y).rotate(self.turn) * self.half
311    }
312    /// Unit length to logical units.
313    fn l(&self, length: f32) -> f32 {
314        length * self.half
315    }
316    fn circle(&self, x: f32, y: f32, r: f32, c: Color) {
317        shapes::fill_circle(self.p(x, y), self.l(r), c);
318    }
319    fn ring(&self, x: f32, y: f32, r: f32, width: f32, c: Color) {
320        shapes::stroke_circle(self.p(x, y), self.l(r), self.l(width), c);
321    }
322    /// Rounded rectangle from its top-left corner; turned pens fill it as a
323    /// rounded outline instead.
324    fn rect(&self, x: f32, y: f32, w: f32, h: f32, radius: f32, c: Color) {
325        if self.angle == 0. {
326            let a = self.p(x, y);
327            shapes::fill_rounded_rect(Rect::new(a.x, a.y, self.l(w), self.l(h)), self.l(radius), c);
328        } else {
329            let r = radius.min(w * 0.5).min(h * 0.5);
330            let corners = [(x, y, r), (x + w, y, r), (x + w, y + h, r), (x, y + h, r)];
331            self.fill(&Outline::corners(&corners), c);
332        }
333    }
334    /// Rounded-rectangle border of `width` centered on the edge (unturned pens).
335    fn frame(&self, (x, y, w, h): (f32, f32, f32, f32), radius: f32, width: f32, c: Color) {
336        let a = self.p(x, y);
337        shapes::stroke_rounded_rect(
338            Rect::new(a.x, a.y, self.l(w), self.l(h)),
339            self.l(radius),
340            self.l(width),
341            c,
342        );
343    }
344    fn capsule(&self, a: (f32, f32), b: (f32, f32), r: f32, c: Color) {
345        shapes::fill_capsule(self.p(a.0, a.1), self.p(b.0, b.1), self.l(r), c);
346    }
347    /// Convex polygon through unit-box points.
348    fn poly(&self, points: &[(f32, f32)], c: Color) {
349        self.fill(&Outline::of(points), c);
350    }
351    /// Convex outline.
352    fn fill(&self, outline: &Outline, c: Color) {
353        let (points, n) = self.place(outline);
354        #[cfg(test)]
355        tests::assert_convex(&points[..n]);
356        if n <= shapes::MAX_POLYGON_POINTS {
357            shapes::fill_polygon(&points[..n], c);
358        } else if n > 0 {
359            let hub = points[..n].iter().copied().sum::<Vec2>() / n as f32;
360            fill_mesh(&points[..n], Some(hub), c);
361        }
362    }
363    /// Concave outline that is star-shaped about `hub` (every edge faces it),
364    /// filled as one piece.
365    fn fan(&self, hub: (f32, f32), outline: &Outline, c: Color) {
366        let (points, n) = self.place(outline);
367        let hub = self.p(hub.0, hub.1);
368        #[cfg(test)]
369        tests::assert_star_shaped(&points[..n], hub);
370        fill_mesh(&points[..n], Some(hub), c);
371    }
372    /// Round-capped stroke along a smooth open outline (sampled arcs and
373    /// curves). Mitred joins are exact for gentle turns and, unlike round
374    /// joins, never overlap the body, so translucent curves stay even.
375    fn stroke(&self, outline: &Outline, width: f32, c: Color) {
376        let (points, n) = self.place(outline);
377        let stroke = Stroke::new(self.l(width), c)
378            .cap(Cap::Round)
379            .join(Join::Miter);
380        shapes::polyline(&points[..n], stroke);
381    }
382    /// Mitre-joined stroke around a smooth closed outline.
383    fn stroke_closed(&self, outline: &Outline, width: f32, c: Color) {
384        let (points, n) = self.place(outline);
385        let stroke = Stroke::new(self.l(width), c).join(Join::Miter).closed(true);
386        shapes::polyline(&points[..n], stroke);
387    }
388    /// Round-capped, round-joined stroke through a few unit-box points, for
389    /// marks with real corners (clock hands, facets).
390    fn line(&self, points: &[(f32, f32)], width: f32, c: Color) {
391        let (points, n) = self.place(&Outline::of(points));
392        let stroke = Stroke::new(self.l(width), c)
393            .cap(Cap::Round)
394            .join(Join::Round);
395        shapes::polyline(&points[..n], stroke);
396    }
397    /// Round-capped arc of `r` around `(x, y)` from `start` sweeping `sweep`.
398    fn arc(&self, (x, y, r): (f32, f32, f32), start: f32, sweep: f32, width: f32, c: Color) {
399        let stroke = Stroke::new(self.l(width), c).cap(Cap::Round);
400        shapes::stroke_arc(self.p(x, y), self.l(r), start + self.angle, sweep, stroke);
401    }
402    fn sector(&self, (x, y, r): (f32, f32, f32), start: f32, sweep: f32, c: Color) {
403        shapes::fill_sector(self.p(x, y), self.l(r), start + self.angle, sweep, c);
404    }
405    /// The part of circle `outer` outside circle `inner` (each `(x, y, r)`),
406    /// filled as one ladder mesh between the two arcs.
407    fn crescent(&self, outer: (f32, f32, f32), inner: (f32, f32, f32), c: Color) {
408        let (co, ro) = (vec2(outer.0, outer.1), outer.2);
409        let (ci, ri) = (vec2(inner.0, inner.1), inner.2);
410        let span = ci - co;
411        let dist = span.length();
412        if !(dist > 1e-4 && dist < ro + ri && dist > (ro - ri).abs()) {
413            return;
414        }
415        let axis = span / dist;
416        let along = (ro * ro - ri * ri + dist * dist) / (2. * dist);
417        let lift = axis.perp() * (ro * ro - along * along).max(0.).sqrt();
418        let (tip_a, tip_b) = (co + axis * along + lift, co + axis * along - lift);
419        // The arc of `center` from `tip_a` to `tip_b` passing through `via`.
420        let span_through = |center: Vec2, via: Vec2| {
421            let from = (tip_a - center).to_angle();
422            let ahead = ((tip_b - center).to_angle() - from).rem_euclid(TAU);
423            let via = ((via - center).to_angle() - from).rem_euclid(TAU);
424            (from, if via < ahead { ahead } else { ahead - TAU })
425        };
426        let (o_from, o_sweep) = span_through(co, co - axis * ro);
427        let (i_from, i_sweep) = span_through(ci, ci - axis * ri);
428        // Out along the outer arc from tip to tip, back along the inner one;
429        // point `k` and point `2 * steps - k` share a parameter (a rung).
430        let steps = arc_steps(ro, o_sweep).clamp(8, MAX_POINTS / 2);
431        let mut points = [Vec2::ZERO; MAX_POINTS];
432        for k in 0..=steps {
433            let t = k as f32 / steps as f32;
434            let o = co + Vec2::from_angle(o_from + o_sweep * t) * ro;
435            points[k] = self.p(o.x, o.y);
436            if k > 0 && k < steps {
437                let i = ci + Vec2::from_angle(i_from + i_sweep * t) * ri;
438                points[2 * steps - k] = self.p(i.x, i.y);
439            }
440        }
441        #[cfg(test)]
442        tests::assert_ladder(&points[..2 * steps]);
443        fill_mesh(&points[..2 * steps], None, c);
444    }
445    /// Outline points (fillets expanded) in logical units.
446    fn place(&self, outline: &Outline) -> ([Vec2; MAX_POINTS], usize) {
447        let mut out = [Vec2::ZERO; MAX_POINTS];
448        let mut n = 0;
449        outline.trace(|x, y| {
450            #[cfg(test)]
451            assert!(n < MAX_POINTS, "outline overflows {MAX_POINTS} points");
452            if n < MAX_POINTS {
453                out[n] = self.p(x, y);
454                n += 1;
455            }
456        });
457        (out, n)
458    }
459}
460
461/// A stack-built outline in unit-box coordinates. Each corner may carry a
462/// radius, which [`Outline::trace`] replaces with a tangent fillet arc.
463#[derive(Clone, Copy)]
464struct Outline {
465    corners: [(f32, f32, f32); MAX_POINTS],
466    len: usize,
467}
468
469impl Outline {
470    fn new() -> Self {
471        Self {
472            corners: [(0., 0., 0.); MAX_POINTS],
473            len: 0,
474        }
475    }
476    fn of(points: &[(f32, f32)]) -> Self {
477        let mut o = Self::new();
478        for &(x, y) in points {
479            o.at(x, y);
480        }
481        o
482    }
483    /// Corners `(x, y, radius)`; a zero radius keeps the corner sharp.
484    fn corners(corners: &[(f32, f32, f32)]) -> Self {
485        let mut o = Self::new();
486        for &(x, y, r) in corners {
487            o.round(x, y, r);
488        }
489        o
490    }
491    fn at(&mut self, x: f32, y: f32) -> &mut Self {
492        self.round(x, y, 0.)
493    }
494    /// A corner rounded with radius `r`. Repeated points are skipped.
495    fn round(&mut self, x: f32, y: f32, r: f32) -> &mut Self {
496        let repeat = self.len > 0 && {
497            let (px, py, _) = self.corners[self.len - 1];
498            (px - x).abs() < 1e-5 && (py - y).abs() < 1e-5
499        };
500        #[cfg(test)]
501        assert!(
502            repeat || self.len < MAX_POINTS,
503            "outline overflows {MAX_POINTS} corners"
504        );
505        if !repeat && self.len < MAX_POINTS {
506            self.corners[self.len] = (x, y, r);
507            self.len += 1;
508        }
509        self
510    }
511    /// Points along an arc of `r` around `(cx, cy)` from `start` sweeping
512    /// `sweep` radians clockwise, both ends included.
513    fn arc(&mut self, (cx, cy, r): (f32, f32, f32), start: f32, sweep: f32) -> &mut Self {
514        let steps = arc_steps(r, sweep);
515        for i in 0..=steps {
516            let a = start + sweep * i as f32 / steps as f32;
517            self.at(cx + r * a.cos(), cy + r * a.sin());
518        }
519        self
520    }
521    /// Quadratic Bézier from the last point through control `q` to `(x, y)`.
522    fn curve(&mut self, q: (f32, f32), to: (f32, f32), steps: usize) -> &mut Self {
523        let Some(&(sx, sy, _)) = self.corners[..self.len].last() else {
524            return self.at(to.0, to.1);
525        };
526        // A quadratic is the cubic with controls two thirds of the way to `q`.
527        let lerp = |a: f32, b: f32| a + (b - a) * (2. / 3.);
528        let q1 = (lerp(sx, q.0), lerp(sy, q.1));
529        let q2 = (lerp(to.0, q.0), lerp(to.1, q.1));
530        self.cubic(q1, q2, to, steps)
531    }
532    /// Cubic Bézier from the last point through controls `q1`, `q2` to `to`.
533    fn cubic(&mut self, q1: (f32, f32), q2: (f32, f32), to: (f32, f32), steps: usize) -> &mut Self {
534        let Some(&(sx, sy, _)) = self.corners[..self.len].last() else {
535            return self.at(to.0, to.1);
536        };
537        for i in 1..=steps {
538            let t = i as f32 / steps as f32;
539            let u = 1. - t;
540            let w = [u * u * u, 3. * u * u * t, 3. * u * t * t, t * t * t];
541            self.at(
542                w[0] * sx + w[1] * q1.0 + w[2] * q2.0 + w[3] * to.0,
543                w[0] * sy + w[1] * q1.1 + w[2] * q2.1 + w[3] * to.1,
544            );
545        }
546        self
547    }
548    /// Calls `f` for every final point: sharp corners as given and rounded
549    /// corners as fillet arcs. A last point repeating the first is dropped.
550    fn trace(&self, mut f: impl FnMut(f32, f32)) {
551        let mut n = self.len;
552        if n > 2 {
553            let (a, b) = (self.corners[0], self.corners[n - 1]);
554            if (a.0 - b.0).abs() < 1e-5 && (a.1 - b.1).abs() < 1e-5 {
555                n -= 1;
556            }
557        }
558        for i in 0..n {
559            let (x, y, r) = self.corners[i];
560            if r > 0. && n > 2 {
561                let (px, py, _) = self.corners[(i + n - 1) % n];
562                let (nx, ny, _) = self.corners[(i + 1) % n];
563                fillet(vec2(px, py), vec2(x, y), vec2(nx, ny), r, &mut f);
564            } else {
565                f(x, y);
566            }
567        }
568    }
569}
570
571/// The signed turn from angle `from` to angle `to` the short way, in `(-π, π]`.
572fn short_sweep(from: f32, to: f32) -> f32 {
573    let turn = (to - from).rem_euclid(TAU);
574    if turn > PI { turn - TAU } else { turn }
575}
576
577/// Segments for an arc of radius `r` (unit box): under a fifth of a pixel of
578/// chord error on a 128-pixel icon.
579fn arc_steps(r: f32, sweep: f32) -> usize {
580    let step = (0.14 / r.abs().max(1e-3).sqrt()).clamp(0.08, 0.7);
581    ((sweep.abs() / step).ceil() as usize).clamp(1, 48)
582}
583
584/// Emits the arc rounding corner `p` (between `prev` and `next`) with radius
585/// `r`, shrunk so it uses at most half of either edge. Works for convex and
586/// reflex corners alike.
587fn fillet(prev: Vec2, p: Vec2, next: Vec2, r: f32, f: &mut impl FnMut(f32, f32)) {
588    let (a, b) = (prev - p, next - p);
589    let (u, v) = (a.normalize_or_zero(), b.normalize_or_zero());
590    let theta = u.dot(v).clamp(-1., 1.).acos();
591    if !(theta > 1e-3 && theta < PI - 1e-3) {
592        return f(p.x, p.y);
593    }
594    let half = theta * 0.5;
595    let reach = (r / half.tan()).min(0.5 * a.length().min(b.length()));
596    let r = reach * half.tan();
597    let center = p + (u + v).normalize_or_zero() * (r / half.sin());
598    let start = (p + u * reach - center).to_angle();
599    let sweep = short_sweep(start, (p + v * reach - center).to_angle());
600    let steps = arc_steps(r, sweep);
601    for i in 0..=steps {
602        let angle = start + sweep * i as f32 / steps as f32;
603        f(center.x + r * angle.cos(), center.y + r * angle.sin());
604    }
605}
606
607/// Fills the closed outline `points` (logical units) as one mesh, so concave
608/// pictograms stay a single piece: no seams, and a translucent icon is not
609/// darkened where convex parts would overlap. With a `hub` the interior is a
610/// fan from it, for outlines star-shaped about it (stars, hearts, gears,
611/// arrows). Without one the outline is a ladder of even length `n`: rungs
612/// join point `k` to point `n - k` (crescents). Edges get the same half-pixel
613/// fringe and mitre limit as [`shapes::fill_polygon`].
614fn fill_mesh(points: &[Vec2], hub: Option<Vec2>, color: Color) {
615    let n = points.len();
616    if n < 3
617        || (hub.is_none() && !n.is_multiple_of(2))
618        || !hub.is_none_or(|h| h.is_finite())
619        || !points.iter().all(|p| p.is_finite())
620    {
621        return;
622    }
623    let twice_area: f32 = (0..n)
624        .map(|i| points[i].perp_dot(points[(i + 1) % n]))
625        .sum();
626    if !(twice_area.is_finite() && twice_area.abs() > 1e-9) {
627        return;
628    }
629    let outward = if twice_area > 0. { -1. } else { 1. };
630    let clear = Color::new(color.r, color.g, color.b, 0.);
631    crate::edit(|s| {
632        let mut sink = shapes::Sink::new(s);
633        let f = sink.device_pixel() * 0.5;
634        // Inner (opaque) and outer (clear) fringe points of corner `i`.
635        let fringe = |i: usize| {
636            let p = points[i];
637            let n_in = (p - points[(i + n - 1) % n]).normalize_or_zero().perp() * outward;
638            let n_out = (points[(i + 1) % n] - p).normalize_or_zero().perp() * outward;
639            let miter = (n_in + n_out).normalize_or(n_out);
640            let reach = f / miter.dot(n_out).abs().max(0.25);
641            (p - miter * reach, p + miter * reach)
642        };
643        sink.reserve(n * 9);
644        let (mut inner, mut outer) = fringe(n - 1);
645        for i in 0..n {
646            let (next_inner, next_outer) = fringe(i);
647            if let Some(hub) = hub {
648                sink.tri(hub, color, inner, color, next_inner, color);
649            }
650            sink.tri(inner, color, next_inner, color, outer, clear);
651            sink.tri(outer, clear, next_inner, color, next_outer, clear);
652            (inner, outer) = (next_inner, next_outer);
653        }
654        if hub.is_none() {
655            // Ladder interior: a quad between consecutive rungs.
656            for k in 0..n / 2 {
657                let (a, b) = (fringe(k).0, fringe(k + 1).0);
658                let (e, g) = (fringe(n - k - 1).0, fringe((n - k) % n).0);
659                sink.tri(a, color, b, color, e, color);
660                sink.tri(a, color, e, color, g, color);
661            }
662        }
663    });
664}
665
666// ---------------------------------------------------------------------------
667// Reusable outlines.
668
669/// Star-shaped outline of `n` round-ended arms of half-width `half` reaching
670/// `len` from the origin (the arm's cap center): a plus, a cross, an asterisk.
671fn arms(n: usize, first: f32, len: f32, half: f32) -> Outline {
672    let mut o = Outline::new();
673    let step = TAU / n as f32;
674    let reach = half / (step * 0.5).sin();
675    for k in 0..n {
676        let a = first + step * k as f32;
677        let between = a - step * 0.5;
678        o.at(reach * between.cos(), reach * between.sin());
679        o.arc((len * a.cos(), len * a.sin(), half), a - FRAC_PI_2, PI);
680    }
681    o
682}
683
684/// Five-point star about `(cx, cy)` with outer radius `r`, inner radius
685/// `r * inner`, and rounded tips and notches.
686fn star_outline(cx: f32, cy: f32, r: f32, inner: f32, (tip, notch): (f32, f32)) -> Outline {
687    let mut o = Outline::new();
688    for k in 0..10 {
689        let a = -FRAC_PI_2 + k as f32 * PI / 5.;
690        let (radius, round) = if k % 2 == 0 {
691            (r, tip)
692        } else {
693            (r * inner, notch)
694        };
695        o.round(cx + radius * a.cos(), cy + radius * a.sin(), round);
696    }
697    o
698}
699
700/// Four-point sparkle about `(cx, cy)`.
701fn sparkle(cx: f32, cy: f32, r: f32) -> Outline {
702    let mut o = Outline::new();
703    for k in 0..8 {
704        let a = k as f32 * PI / 4.;
705        let radius = if k % 2 == 0 { r } else { r * 0.34 };
706        o.round(cx + radius * a.cos(), cy + radius * a.sin(), 0.);
707    }
708    o
709}
710
711/// Circle `(cx, cy, r)` drawn out to a point at `(tx, ty)` along its two
712/// tangents: drops, pins and flames. Convex.
713fn teardrop((cx, cy, r): (f32, f32, f32), (tx, ty): (f32, f32), tip_round: f32) -> Outline {
714    let to_tip = vec2(tx - cx, ty - cy);
715    let base = to_tip.to_angle();
716    let off = (r / to_tip.length()).clamp(-1., 1.).acos();
717    let mut o = Outline::new();
718    o.round(tx, ty, tip_round);
719    o.arc((cx, cy, r), base + off, TAU - 2. * off);
720    o
721}
722
723/// Convex lens between tips `a` and `b`, each side an arc bulging `bulge`
724/// (at most half the tip distance) from the chord.
725fn lens(a: (f32, f32), b: (f32, f32), bulge: f32) -> Outline {
726    let (a, b) = (vec2(a.0, a.1), vec2(b.0, b.1));
727    let half = (b - a).length() * 0.5;
728    let normal = ((b - a) / (2. * half)).perp();
729    let radius = (half * half + bulge * bulge) / (2. * bulge);
730    let mut o = Outline::new();
731    for (side, from, to) in [(1., a, b), (-1., b, a)] {
732        let center = (a + b) * 0.5 - normal * (side * (radius - bulge));
733        let start = (from - center).to_angle();
734        let sweep = short_sweep(start, (to - center).to_angle());
735        o.arc((center.x, center.y, radius), start, sweep);
736    }
737    o
738}
739
740/// A leaf from `base` to `tip`, about `bulge` wide on each side: two cubic
741/// curves, fullest nearer the base and meeting at a sharp (about 55°) tip.
742/// The control polygons turn one way only (`0.4 * 1.86 < 0.88 * 0.92`), so
743/// the outline is convex for any length and width.
744fn leaf_outline(base: (f32, f32), tip: (f32, f32), bulge: f32) -> Outline {
745    let (b, t) = (vec2(base.0, base.1), vec2(tip.0, tip.1));
746    let (axis, normal) = (t - b, (t - b).normalize_or(Vec2::X).perp());
747    let at = |along: f32, out: f32| {
748        let q = b + axis * along + normal * (bulge * out);
749        (q.x, q.y)
750    };
751    let mut o = Outline::new();
752    o.at(b.x, b.y)
753        .cubic(at(0.12, 1.86), at(0.6, 0.92), (t.x, t.y), 14)
754        .cubic(at(0.6, -0.92), at(0.12, -1.86), (b.x, b.y), 14);
755    o
756}
757
758/// Ellipse with radii `rx`, `ry` turned `tilt` radians clockwise.
759fn ellipse(cx: f32, cy: f32, (rx, ry): (f32, f32), tilt: f32) -> Outline {
760    let mut o = Outline::new();
761    let turn = Vec2::from_angle(tilt);
762    for k in 0..28 {
763        let t = k as f32 * TAU / 28.;
764        let p = vec2(rx * t.cos(), ry * t.sin()).rotate(turn);
765        o.at(cx + p.x, cy + p.y);
766    }
767    o
768}
769
770/// Intersection of line `a`–`b` with line `c`–`d`.
771fn meet(a: (f32, f32), b: (f32, f32), c: (f32, f32), d: (f32, f32)) -> (f32, f32) {
772    let (r, s) = (vec2(b.0 - a.0, b.1 - a.1), vec2(d.0 - c.0, d.1 - c.1));
773    let t = vec2(c.0 - a.0, c.1 - a.1).perp_dot(s) / r.perp_dot(s);
774    (a.0 + r.x * t, a.1 + r.y * t)
775}
776
777/// A filled arrowhead whose base is centered at angle `at` on the circle
778/// `(cx, cy, r)`, pointing along the circle (`dir` +1 is clockwise).
779fn arrowhead(
780    p: &Pen,
781    (cx, cy, r): (f32, f32, f32),
782    at: f32,
783    dir: f32,
784    (len, half): (f32, f32),
785    c: Color,
786) {
787    let center = vec2(cx, cy);
788    let base = center + Vec2::from_angle(at) * r;
789    // Aim at where the circle is one head length ahead, following the curve.
790    let ahead = center + Vec2::from_angle(at + dir * len / r) * r;
791    let forward = (ahead - base).normalize_or(Vec2::X);
792    let (tip, side) = (base + forward * len, forward.perp() * half);
793    p.fill(
794        &Outline::corners(&[
795            (tip.x, tip.y, 0.05),
796            (base.x + side.x, base.y + side.y, 0.04),
797            (base.x - side.x, base.y - side.y, 0.04),
798        ]),
799        c,
800    );
801}
802
803// ---------------------------------------------------------------------------
804// The pictograms.
805
806/// Paint one icon with the silhouette `c` and inner-mark tone `d`.
807fn paint(icon: Icon, p: &Pen, c: Color, d: Color) {
808    match icon {
809        Icon::Play => play(p, c),
810        Icon::Pause => pause(p, c),
811        Icon::Restart => restart(p, c),
812        Icon::Undo => undo(p, c),
813        Icon::Check => check(p, c),
814        Icon::Cross => p.fan((0., 0.), &arms(4, deg(45.), 0.88, 0.15), c),
815        Icon::Plus => p.fan((0., 0.), &arms(4, 0., 0.66, 0.16), c),
816        Icon::Minus => p.capsule((-0.66, 0.), (0.66, 0.), 0.16, c),
817        Icon::ArrowUp => arrow(p, -FRAC_PI_2, c),
818        Icon::ArrowDown => arrow(p, FRAC_PI_2, c),
819        Icon::ArrowLeft => arrow(p, PI, c),
820        Icon::ArrowRight => arrow(p, 0., c),
821        Icon::Question => question(p, c),
822        Icon::Info => info(p, c),
823        Icon::Alert => alert(p, c, d),
824        Icon::SoundOn => sound(p, c, true),
825        Icon::SoundOff => sound(p, c, false),
826        Icon::Gear => gear(p, c, d),
827        Icon::Gamepad => gamepad(p, c, d),
828        Icon::Pointer => pointer(p, c),
829        Icon::Hand => hand(p, c),
830        Icon::Eye => eye(p, c, d, false),
831        Icon::EyeOff => eye(p, c, d, true),
832        Icon::Lock => lock(p, c, d, false),
833        Icon::Unlock => lock(p, c, d, true),
834        Icon::Key => key(p, c, d),
835        Icon::Home => home(p, c, d),
836        Icon::Trophy => trophy(p, c, d),
837        Icon::Crown => crown(p, c, d),
838        Icon::Flag => flag(p, c),
839        Icon::Target => target(p, c),
840        Icon::Pin => pin(p, c, d),
841        Icon::Map => map(p, c, d),
842        Icon::Search => search(p, c),
843        Icon::Bell => bell(p, c),
844        Icon::Star => p.fan(
845            (0., 0.08),
846            &star_outline(0., 0.08, 1.02, 0.5, (0.1, 0.04)),
847            c,
848        ),
849        Icon::Heart => heart(p, c),
850        Icon::Skull => skull(p, c, d),
851        Icon::Clock => clock(p, c, d),
852        Icon::Hourglass => hourglass(p, c, d),
853        Icon::Calendar => calendar(p, c, d),
854        Icon::Bolt => bolt(p, c),
855        Icon::Battery => battery(p, c, d),
856        Icon::Shield => shield(p, c, d),
857        Icon::Sword => sword(p, c, d),
858        Icon::Bomb => bomb(p, c),
859        Icon::Flame => flame(p, c, d),
860        Icon::Drop => water_drop(p, c, d),
861        Icon::Snowflake => snowflake(p, c),
862        Icon::Sun => sun(p, c),
863        Icon::Moon => p.crescent((-0.04, 0.04, 0.84), (0.42, -0.32, 0.68), c),
864        Icon::Cloud => cloud(p, c),
865        Icon::Rain => rain(p, c),
866        Icon::Wind => wind(p, c),
867        Icon::Wave => wave(p, c),
868        Icon::Leaf => leaf(p, c, d),
869        Icon::Sprout => sprout(p, c, d),
870        Icon::Tree => tree(p, c),
871        Icon::Mountain => mountain(p, c, d),
872        Icon::Coin => coin(p, c, d),
873        Icon::Gem => gem(p, c, d),
874        Icon::Bag => bag(p, c, d),
875        Icon::Crate => wooden_crate(p, c, d),
876        Icon::Scroll => scroll(p, c, d),
877        Icon::Book => book(p, c, d),
878        Icon::Bulb => bulb(p, c, d),
879        Icon::Hammer => hammer(p, c, d),
880        Icon::Wrench => wrench(p, c),
881        Icon::Pickaxe => pickaxe(p, c, d),
882        Icon::Anchor => anchor(p, c),
883        Icon::Ship => ship(p, c, d),
884        Icon::Rocket => rocket(p, c, d),
885        Icon::Planet => planet(p, c, d),
886        Icon::Train => train(p, c, d),
887        Icon::Person => person(p, c),
888        Icon::People => people(p, c, d),
889        Icon::Paw => paw(p, c),
890        Icon::Bone => bone(p, c),
891        Icon::Potion => potion(p, c, d),
892        Icon::Fish => fish(p, c, d),
893        Icon::Music => music(p, c),
894        Icon::Chat => chat(p, c, d),
895        Icon::Pot => pot(p, c, d),
896        Icon::Thermometer => thermometer(p, c, d),
897        Icon::Fuel => fuel(p, c, d),
898        Icon::FastForward => fast_forward(p, c),
899        Icon::Magnet => magnet(p, c, d),
900        Icon::Bug => bug(p, c, d),
901        Icon::Chain => chain(p, c),
902        Icon::Move => four_way(p, c),
903        Icon::Cards => cards(p, c, d),
904        Icon::Ball => ball(p, c, d),
905    }
906}
907
908fn cards(p: &Pen, c: Color, d: Color) {
909    // The back card leans left; the front card leans right and carries a pip.
910    let card = |s: &Pen| {
911        s.fill(
912            &Outline::corners(&[
913                (-0.42, -0.62, 0.12),
914                (0.42, -0.62, 0.12),
915                (0.42, 0.62, 0.12),
916                (-0.42, 0.62, 0.12),
917            ]),
918            c,
919        );
920    };
921    let back = p.sub(-0.2, 0.04, 1., deg(-14.));
922    card(&back);
923    back.stroke_closed(
924        &Outline::corners(&[
925            (-0.28, -0.48, 0.06),
926            (0.28, -0.48, 0.06),
927            (0.28, 0.48, 0.06),
928            (-0.28, 0.48, 0.06),
929        ]),
930        0.08,
931        d,
932    );
933    let front = p.sub(0.22, 0.02, 1., deg(10.));
934    // A dark rim separates the overlapping cards without a cut-out.
935    front.fill(
936        &Outline::corners(&[
937            (-0.5, -0.7, 0.16),
938            (0.5, -0.7, 0.16),
939            (0.5, 0.7, 0.16),
940            (-0.5, 0.7, 0.16),
941        ]),
942        d,
943    );
944    card(&front);
945    front.fill(
946        &Outline::corners(&[
947            (0., -0.26, 0.04),
948            (0.2, 0., 0.04),
949            (0., 0.26, 0.04),
950            (-0.2, 0., 0.04),
951        ]),
952        d,
953    );
954}
955
956fn ball(p: &Pen, c: Color, d: Color) {
957    p.circle(0., 0., 0.84, c);
958    // Dimples in the detail tone, and a soft highlight.
959    for (x, y) in [
960        (0.28, 0.22),
961        (-0.06, 0.42),
962        (0.42, -0.12),
963        (-0.3, 0.12),
964        (0.12, -0.02),
965    ] {
966        p.circle(x, y, 0.08, d);
967    }
968    p.arc(
969        (0., 0., 0.58),
970        deg(200.),
971        deg(60.),
972        0.12,
973        Color::new(1., 1., 1., c.a * 0.55),
974    );
975}
976
977// Additions requested by the games: speed, magnets, pests, links, movement.
978
979fn fast_forward(p: &Pen, c: Color) {
980    for x in [-0.9, -0.02] {
981        p.fill(
982            &Outline::corners(&[(x, -0.74, 0.12), (x + 0.9, 0., 0.12), (x, 0.74, 0.12)]),
983            c,
984        );
985    }
986}
987
988fn magnet(p: &Pen, c: Color, d: Color) {
989    // An upright horseshoe: a thick bottom arc and two legs with light poles.
990    p.arc((0., 0.08, 0.52), 0., PI, 0.36, c);
991    p.rect(-0.7, -0.62, 0.36, 0.72, 0.03, c);
992    p.rect(0.34, -0.62, 0.36, 0.72, 0.03, c);
993    p.rect(-0.7, -0.9, 0.36, 0.34, 0.07, d);
994    p.rect(0.34, -0.9, 0.36, 0.34, 0.07, d);
995}
996
997fn bug(p: &Pen, c: Color, d: Color) {
998    // Legs first so the shell covers their roots.
999    for (y, reach) in [(-0.16, -0.36), (0.16, 0.), (0.46, 0.36)] {
1000        p.line(
1001            &[(-0.3, y), (-0.66, y + reach * 0.45), (-0.84, y + reach)],
1002            0.11,
1003            c,
1004        );
1005        p.line(
1006            &[(0.3, y), (0.66, y + reach * 0.45), (0.84, y + reach)],
1007            0.11,
1008            c,
1009        );
1010    }
1011    p.line(&[(-0.12, -0.62), (-0.3, -0.86)], 0.09, c);
1012    p.line(&[(0.12, -0.62), (0.3, -0.86)], 0.09, c);
1013    p.circle(0., -0.5, 0.24, c);
1014    p.fill(&ellipse(0., 0.18, (0.48, 0.6), 0.), c);
1015    // Wing split and two spots in the detail tone.
1016    p.line(&[(0., -0.3), (0., 0.7)], 0.07, d);
1017    p.circle(-0.22, 0.1, 0.09, d);
1018    p.circle(0.24, 0.38, 0.09, d);
1019}
1020
1021fn chain(p: &Pen, c: Color) {
1022    // Rounded links as outlines, so they follow the pen's rotation.
1023    let s = p.sub(0., 0., 1., deg(-45.));
1024    for x in [-0.94, -0.12] {
1025        let link = Outline::corners(&[
1026            (x, -0.28, 0.26),
1027            (x + 1.06, -0.28, 0.26),
1028            (x + 1.06, 0.28, 0.26),
1029            (x, 0.28, 0.26),
1030        ]);
1031        s.stroke_closed(&link, 0.19, c);
1032    }
1033}
1034
1035fn four_way(p: &Pen, c: Color) {
1036    p.capsule((-0.56, 0.), (0.56, 0.), 0.11, c);
1037    p.capsule((0., -0.56), (0., 0.56), 0.11, c);
1038    for angle in [0., FRAC_PI_2, PI, -FRAC_PI_2] {
1039        p.sub(0., 0., 1., angle).fill(
1040            &Outline::corners(&[(0.92, 0., 0.06), (0.5, -0.32, 0.05), (0.5, 0.32, 0.05)]),
1041            c,
1042        );
1043    }
1044}
1045
1046// Interface and menus.
1047
1048fn play(p: &Pen, c: Color) {
1049    p.fill(
1050        &Outline::corners(&[(-0.62, -0.9, 0.15), (0.92, 0., 0.15), (-0.62, 0.9, 0.15)]),
1051        c,
1052    );
1053}
1054
1055fn pause(p: &Pen, c: Color) {
1056    p.rect(-0.66, -0.8, 0.46, 1.6, 0.14, c);
1057    p.rect(0.2, -0.8, 0.46, 1.6, 0.14, c);
1058}
1059
1060fn restart(p: &Pen, c: Color) {
1061    let circle = (0., 0.06, 0.6);
1062    let (start, sweep) = (deg(-28.), deg(284.));
1063    p.arc(circle, start, sweep, 0.24, c);
1064    arrowhead(p, circle, start + sweep, 1., (0.44, 0.32), c);
1065}
1066
1067fn undo(p: &Pen, c: Color) {
1068    let (y, r, cx) = (-0.3, 0.4, 0.38);
1069    let mut hook = Outline::new();
1070    hook.at(-0.4, y)
1071        .arc((cx, y + r, r), -FRAC_PI_2, PI)
1072        .at(-0.12, y + 2. * r);
1073    p.stroke(&hook, 0.22, c);
1074    p.fill(
1075        &Outline::corners(&[
1076            (-0.9, y, 0.05),
1077            (-0.34, y - 0.4, 0.04),
1078            (-0.34, y + 0.4, 0.04),
1079        ]),
1080        c,
1081    );
1082}
1083
1084fn check(p: &Pen, c: Color) {
1085    let (a, corner, b, half) = (vec2(-0.68, 0.04), vec2(-0.2, 0.52), vec2(0.72, -0.48), 0.14);
1086    let (u, v) = ((a - corner).normalize(), (b - corner).normalize());
1087    // Unit normals toward the inside of the V for each arm.
1088    let inner_a = u.perp() * u.perp().dot(v).signum();
1089    let inner_b = v.perp() * v.perp().dot(u).signum();
1090    let edge = |from: Vec2, dir: Vec2, normal: Vec2, side: f32| {
1091        let p = from + normal * (half * side);
1092        ((p.x, p.y), (p.x + dir.x, p.y + dir.y))
1093    };
1094    let (ia, ib) = (edge(corner, u, inner_a, 1.), edge(corner, v, inner_b, 1.));
1095    let (oa, ob) = (edge(corner, u, inner_a, -1.), edge(corner, v, inner_b, -1.));
1096    let inner = meet(ia.0, ia.1, ib.0, ib.1);
1097    let outer = meet(oa.0, oa.1, ob.0, ob.1);
1098    // Semicircular cap at `end` from the inner side round to the outer side.
1099    let cap = |o: &mut Outline, end: Vec2, dir: Vec2, normal: Vec2| {
1100        let start = normal.to_angle();
1101        let ahead = (dir.to_angle() - start).rem_euclid(TAU);
1102        o.arc(
1103            (end.x, end.y, half),
1104            start,
1105            if ahead < PI { PI } else { -PI },
1106        );
1107    };
1108    let mut o = Outline::new();
1109    o.at(inner.0, inner.1);
1110    cap(&mut o, a, u, inner_a);
1111    o.round(outer.0, outer.1, half);
1112    cap(&mut o, b, v, -inner_b);
1113    p.fan((corner.x, corner.y), &o, c);
1114}
1115
1116fn arrow(p: &Pen, angle: f32, c: Color) {
1117    let shape = Outline::corners(&[
1118        (-0.82, -0.17, 0.08),
1119        (0.02, -0.17, 0.03),
1120        (0.02, -0.68, 0.08),
1121        (0.86, 0., 0.1),
1122        (0.02, 0.68, 0.08),
1123        (0.02, 0.17, 0.03),
1124        (-0.82, 0.17, 0.08),
1125    ]);
1126    p.sub(0., 0., 1., angle).fan((0.2, 0.), &shape, c);
1127}
1128
1129fn question(p: &Pen, c: Color) {
1130    let mut hook = Outline::new();
1131    hook.arc((0., -0.34, 0.36), deg(192.), deg(218.))
1132        .at(0.06, 0.02)
1133        .at(0.02, 0.12)
1134        .at(0.02, 0.24);
1135    p.stroke(&hook, 0.27, c);
1136    p.circle(0.02, 0.66, 0.17, c);
1137}
1138
1139fn info(p: &Pen, c: Color) {
1140    p.circle(0.04, -0.62, 0.18, c);
1141    let stem = Outline::corners(&[
1142        (-0.36, -0.26, 0.06),
1143        (0.2, -0.26, 0.06),
1144        (0.2, 0.6, 0.),
1145        (-0.14, 0.6, 0.),
1146        (-0.14, 0., 0.03),
1147        (-0.36, 0., 0.06),
1148    ]);
1149    p.fan((0.03, -0.13), &stem, c);
1150    p.rect(-0.38, 0.52, 0.82, 0.28, 0.08, c);
1151}
1152
1153fn alert(p: &Pen, c: Color, d: Color) {
1154    p.fill(
1155        &Outline::corners(&[(0., -0.88, 0.16), (0.94, 0.8, 0.16), (-0.94, 0.8, 0.16)]),
1156        c,
1157    );
1158    p.capsule((0., -0.28), (0., 0.2), 0.11, d);
1159    p.circle(0., 0.5, 0.12, d);
1160}
1161
1162fn sound(p: &Pen, c: Color, on: bool) {
1163    let x = -0.4;
1164    let speaker = Outline::corners(&[
1165        (x - 0.44, -0.27, 0.08),
1166        (x, -0.27, 0.02),
1167        (x + 0.44, -0.7, 0.08),
1168        (x + 0.44, 0.7, 0.08),
1169        (x, 0.27, 0.02),
1170        (x - 0.44, 0.27, 0.08),
1171    ]);
1172    p.fan((x - 0.04, 0.), &speaker, c);
1173    if on {
1174        p.arc((0.02, 0., 0.42), deg(-44.), deg(88.), 0.17, c);
1175        p.arc((0.02, 0., 0.72), deg(-46.), deg(92.), 0.17, c);
1176    } else {
1177        p.sub(0.5, 0., 1., 0.)
1178            .fan((0., 0.), &arms(4, deg(45.), 0.3, 0.09), c);
1179    }
1180}
1181
1182fn gear(p: &Pen, c: Color, d: Color) {
1183    let (teeth, body, tip) = (8, 0.62, 0.9);
1184    let step = TAU / teeth as f32;
1185    let (base_half, tip_half) = (deg(13.), deg(8.5));
1186    let mut o = Outline::new();
1187    for k in 0..teeth {
1188        let a = step * k as f32;
1189        let at = |r: f32, t: f32| (r * t.cos(), r * t.sin());
1190        for (r, t, round) in [
1191            (body, a - base_half, 0.03),
1192            (tip, a - tip_half, 0.06),
1193            (tip, a + tip_half, 0.06),
1194            (body, a + base_half, 0.03),
1195            (body, a + step * 0.5, 0.),
1196        ] {
1197            let (x, y) = at(r, t);
1198            o.round(x, y, round);
1199        }
1200    }
1201    p.fan((0., 0.), &o, c);
1202    p.circle(0., 0., 0.26, d);
1203}
1204
1205fn gamepad(p: &Pen, c: Color, d: Color) {
1206    p.rect(-0.88, -0.6, 1.76, 0.76, 0.32, c);
1207    p.capsule((-0.52, -0.1), (-0.64, 0.4), 0.26, c);
1208    p.capsule((0.52, -0.1), (0.64, 0.4), 0.26, c);
1209    p.sub(-0.46, -0.22, 1., 0.)
1210        .fan((0., 0.), &arms(4, 0., 0.14, 0.075), d);
1211    p.circle(0.34, -0.12, 0.1, d);
1212    p.circle(0.56, -0.32, 0.1, d);
1213}
1214
1215fn pointer(p: &Pen, c: Color) {
1216    let cursor = Outline::corners(&[
1217        (-0.52, -0.9, 0.06),
1218        (0.56, 0.18, 0.06),
1219        (0.06, 0.2, 0.02),
1220        (0.34, 0.76, 0.06),
1221        (0.12, 0.88, 0.06),
1222        (-0.16, 0.32, 0.02),
1223        (-0.52, 0.66, 0.06),
1224    ]);
1225    p.fan((-0.16, 0.12), &cursor, c);
1226}
1227
1228fn hand(p: &Pen, c: Color) {
1229    let p = &p.sub(0.12, 0., 1., 0.);
1230    p.rect(-0.46, -0.02, 0.98, 0.9, 0.3, c);
1231    p.capsule((0.04, -0.06), (0.04, 0.2), 0.15, c);
1232    p.capsule((0.32, 0.), (0.32, 0.2), 0.14, c);
1233    p.capsule((-0.26, -0.5), (-0.26, 0.2), 0.16, c);
1234    p.capsule((-0.66, 0.1), (-0.42, 0.42), 0.13, c);
1235    p.arc((-0.26, -0.5, 0.32), deg(200.), deg(140.), 0.12, c);
1236}
1237
1238fn eye(p: &Pen, c: Color, d: Color, off: bool) {
1239    p.stroke_closed(&lens((-0.82, 0.), (0.82, 0.), 0.54), 0.17, c);
1240    p.circle(0., 0., 0.3, c);
1241    if off {
1242        // The slash, set off from the iris by a detail band clipped to it.
1243        p.fill(&disc_band(0.3, Vec2::from_angle(deg(45.)), 0.17), d);
1244        p.capsule((-0.7, -0.7), (0.7, 0.7), 0.09, c);
1245    } else {
1246        p.circle(0., 0., 0.12, d);
1247    }
1248}
1249
1250/// The part of the origin-centered disc of radius `r` lying within `half` of
1251/// the line through its center along `dir`: a convex band across the disc.
1252fn disc_band(r: f32, dir: Vec2, half: f32) -> Outline {
1253    let u = dir.normalize_or(Vec2::X);
1254    let (along, across) = (u * (r * r - half * half).max(0.).sqrt(), u.perp() * half);
1255    let mut o = Outline::new();
1256    for side in [1., -1.] {
1257        // Along one edge of the band, then round the rim to the other edge.
1258        let (from, to) = ((across + along) * side, (across - along) * side);
1259        let end = (-across - along) * side;
1260        let start = to.to_angle();
1261        o.at(from.x, from.y).at(to.x, to.y).arc(
1262            (0., 0., r),
1263            start,
1264            short_sweep(start, end.to_angle()),
1265        );
1266    }
1267    o
1268}
1269
1270fn lock(p: &Pen, c: Color, d: Color, open: bool) {
1271    let p = &p.sub(0., -0.03, 1., 0.);
1272    let r = 0.3;
1273    let top = if open { -0.48 } else { -0.36 };
1274    let mut shackle = Outline::new();
1275    shackle
1276        .at(-r, 0.)
1277        .at(-r, top)
1278        .arc((0., top, r), PI, PI)
1279        .at(r, if open { top + 0.12 } else { 0. });
1280    p.stroke(&shackle, 0.2, c);
1281    p.rect(-0.66, -0.06, 1.32, 0.94, 0.16, c);
1282    p.circle(0., 0.36, 0.13, d);
1283    p.capsule((0., 0.36), (0., 0.62), 0.07, d);
1284}
1285
1286fn key(p: &Pen, c: Color, d: Color) {
1287    let k = p.sub(0.03, 0.03, 1., deg(45.));
1288    k.circle(-0.66, 0., 0.4, c);
1289    k.rect(-0.4, -0.13, 1.32, 0.26, 0.06, c);
1290    k.rect(0.34, -0.06, 0.17, 0.36, 0.04, c);
1291    k.rect(0.62, -0.06, 0.2, 0.42, 0.04, c);
1292    k.circle(-0.74, 0., 0.15, d);
1293}
1294
1295fn home(p: &Pen, c: Color, d: Color) {
1296    p.rect(0.36, -0.76, 0.22, 0.4, 0.04, c);
1297    let house = Outline::corners(&[
1298        (0., -0.86, 0.1),
1299        (0.92, -0.02, 0.08),
1300        (0.66, -0.02, 0.),
1301        (0.66, 0.86, 0.1),
1302        (-0.66, 0.86, 0.1),
1303        (-0.66, -0.02, 0.),
1304        (-0.92, -0.02, 0.08),
1305    ]);
1306    p.fan((0., -0.12), &house, c);
1307    p.rect(-0.18, 0.32, 0.36, 0.54, 0.08, d);
1308}
1309
1310fn trophy(p: &Pen, c: Color, d: Color) {
1311    let mut cup = Outline::new();
1312    cup.round(-0.56, -0.86, 0.06)
1313        .round(0.56, -0.86, 0.06)
1314        .arc((0., -0.34, 0.56), 0., PI);
1315    p.fill(&cup, c);
1316    p.arc((-0.56, -0.54, 0.22), FRAC_PI_2, PI, 0.15, c);
1317    p.arc((0.56, -0.54, 0.22), -FRAC_PI_2, PI, 0.15, c);
1318    p.rect(-0.1, 0.12, 0.2, 0.42, 0., c);
1319    p.rect(-0.44, 0.5, 0.88, 0.36, 0.08, c);
1320    p.fan(
1321        (0., -0.48),
1322        &star_outline(0., -0.48, 0.27, 0.5, (0.03, 0.)),
1323        d,
1324    );
1325}
1326
1327fn crown(p: &Pen, c: Color, d: Color) {
1328    let peaks = [(-0.8, -0.44), (0., -0.66), (0.8, -0.44)];
1329    let body = Outline::corners(&[
1330        (-0.76, 0.74, 0.08),
1331        (peaks[0].0, peaks[0].1, 0.04),
1332        (-0.4, 0., 0.03),
1333        (peaks[1].0, peaks[1].1, 0.04),
1334        (0.4, 0., 0.03),
1335        (peaks[2].0, peaks[2].1, 0.04),
1336        (0.76, 0.74, 0.08),
1337    ]);
1338    p.fan((0., 0.6), &body, c);
1339    // Balls sit on the peak corners, overlapping the rounded tips.
1340    for (x, y) in peaks {
1341        p.circle(x, y - 0.015, 0.11, c);
1342    }
1343    for x in [-0.4, 0., 0.4] {
1344        p.circle(x, 0.46, 0.1, d);
1345    }
1346}
1347
1348fn flag(p: &Pen, c: Color) {
1349    p.capsule((-0.66, -0.76), (-0.66, 0.8), 0.09, c);
1350    p.circle(-0.66, -0.8, 0.11, c);
1351    let mut cloth = Outline::new();
1352    let wave = |t: f32| 0.09 * (t * TAU).sin();
1353    for i in 0..=10 {
1354        let t = i as f32 / 10.;
1355        cloth.at(-0.6 + 1.46 * t, -0.74 + wave(t));
1356    }
1357    for i in (0..=10).rev() {
1358        let t = i as f32 / 10.;
1359        cloth.at(-0.6 + 1.46 * t, wave(t));
1360    }
1361    p.fan((0.13, -0.37), &cloth, c);
1362}
1363
1364fn target(p: &Pen, c: Color) {
1365    p.ring(0., 0., 0.76, 0.2, c);
1366    p.ring(0., 0., 0.4, 0.2, c);
1367    p.circle(0., 0., 0.14, c);
1368}
1369
1370fn pin(p: &Pen, c: Color, d: Color) {
1371    p.fill(&teardrop((0., -0.26, 0.6), (0., 0.92), 0.06), c);
1372    p.circle(0., -0.26, 0.24, d);
1373}
1374
1375fn map(p: &Pen, c: Color, d: Color) {
1376    let sheet = Outline::corners(&[
1377        (-0.88, -0.66, 0.06),
1378        (-0.3, -0.86, 0.02),
1379        (0.3, -0.66, 0.02),
1380        (0.88, -0.86, 0.06),
1381        (0.88, 0.66, 0.06),
1382        (0.3, 0.86, 0.02),
1383        (-0.3, 0.66, 0.02),
1384        (-0.88, 0.86, 0.06),
1385    ]);
1386    p.fan((0., 0.), &sheet, c);
1387    p.line(&[(-0.3, -0.74), (-0.3, 0.54)], 0.13, d);
1388    p.line(&[(0.3, -0.54), (0.3, 0.74)], 0.13, d);
1389}
1390
1391fn search(p: &Pen, c: Color) {
1392    p.ring(-0.16, -0.16, 0.48, 0.22, c);
1393    p.capsule((0.22, 0.22), (0.66, 0.66), 0.16, c);
1394}
1395
1396fn bell(p: &Pen, c: Color) {
1397    let mut body = Outline::new();
1398    body.round(-0.86, 0.58, 0.08)
1399        .round(-0.86, 0.38, 0.08)
1400        .round(-0.68, 0.38, 0.04)
1401        .arc((0., -0.22, 0.5), PI, PI)
1402        .round(0.68, 0.38, 0.04)
1403        .round(0.86, 0.38, 0.08)
1404        .round(0.86, 0.58, 0.08);
1405    p.fan((0., 0.48), &body, c);
1406    p.circle(0., 0.74, 0.14, c);
1407    p.circle(0., -0.76, 0.1, c);
1408}
1409
1410// Stats and resources.
1411
1412fn heart(p: &Pen, c: Color) {
1413    let (a, cy, r): (f32, f32, f32) = (0.42, -0.3, 0.46);
1414    let tip = vec2(0., 0.84);
1415    let notch = -(r * r - a * a).sqrt();
1416    // Angle of the notch seen from the left lobe and of the left lobe's
1417    // outer tangent toward the tip; the right lobe mirrors both.
1418    let notch_angle = notch.atan2(a);
1419    let to_tip = tip - vec2(-a, cy);
1420    let tangent = to_tip.to_angle() + (r / to_tip.length()).acos();
1421    let mut o = Outline::new();
1422    o.arc((-a, cy, r), notch_angle, tangent - TAU - notch_angle)
1423        .round(tip.x, tip.y, 0.1)
1424        .arc((a, cy, r), PI - tangent, tangent - TAU - notch_angle);
1425    p.fan((0., 0.), &o, c);
1426}
1427
1428fn skull(p: &Pen, c: Color, d: Color) {
1429    let (cy, r, jaw): (f32, f32, f32) = (-0.16, 0.72, 0.42);
1430    let drop = (r * r - jaw * jaw).sqrt();
1431    let left = drop.atan2(-jaw);
1432    let mut head = Outline::new();
1433    head.arc((0., cy, r), left, TAU - (2. * left - PI))
1434        .round(jaw, 0.86, 0.16)
1435        .round(-jaw, 0.86, 0.16);
1436    p.fan((0., 0.1), &head, c);
1437    p.circle(-0.28, -0.08, 0.2, d);
1438    p.circle(0.28, -0.08, 0.2, d);
1439    p.fill(
1440        &Outline::corners(&[(0., 0.16, 0.03), (0.11, 0.36, 0.03), (-0.11, 0.36, 0.03)]),
1441        d,
1442    );
1443    p.line(&[(-0.13, 0.62), (-0.13, 0.8)], 0.1, d);
1444    p.line(&[(0.13, 0.62), (0.13, 0.8)], 0.1, d);
1445}
1446
1447fn clock(p: &Pen, c: Color, d: Color) {
1448    p.circle(0., 0., 0.88, c);
1449    p.line(&[(0., -0.54), (0., 0.), (0.36, 0.22)], 0.17, d);
1450}
1451
1452fn hourglass(p: &Pen, c: Color, d: Color) {
1453    p.rect(-0.66, -0.9, 1.32, 0.2, 0.08, c);
1454    p.rect(-0.66, 0.7, 1.32, 0.2, 0.08, c);
1455    let glass = Outline::corners(&[
1456        (-0.52, -0.74, 0.),
1457        (0.52, -0.74, 0.),
1458        (0.52, -0.5, 0.2),
1459        (0.1, -0.04, 0.06),
1460        (0.1, 0.04, 0.06),
1461        (0.52, 0.5, 0.2),
1462        (0.52, 0.74, 0.),
1463        (-0.52, 0.74, 0.),
1464        (-0.52, 0.5, 0.2),
1465        (-0.1, 0.04, 0.06),
1466        (-0.1, -0.04, 0.06),
1467        (-0.52, -0.5, 0.2),
1468    ]);
1469    p.fan((0., 0.), &glass, c);
1470    p.poly(
1471        &[(-0.28, -0.36), (0.28, -0.36), (0.06, -0.12), (-0.06, -0.12)],
1472        d,
1473    );
1474    p.fill(
1475        &Outline::corners(&[(-0.38, 0.66, 0.), (0.38, 0.66, 0.), (0., 0.32, 0.1)]),
1476        d,
1477    );
1478}
1479
1480fn calendar(p: &Pen, c: Color, d: Color) {
1481    p.rect(-0.82, -0.64, 1.64, 1.5, 0.18, c);
1482    p.capsule((-0.42, -0.8), (-0.42, -0.5), 0.1, c);
1483    p.capsule((0.42, -0.8), (0.42, -0.5), 0.1, c);
1484    p.rect(-0.68, -0.3, 1.36, 0.12, 0.06, d);
1485    for row in 0..2 {
1486        for col in 0..3 {
1487            let (x, y) = (-0.52 + col as f32 * 0.4, -0.04 + row as f32 * 0.4);
1488            p.rect(x, y, 0.24, 0.24, 0.05, d);
1489        }
1490    }
1491}
1492
1493fn bolt(p: &Pen, c: Color) {
1494    let o = Outline::corners(&[
1495        (0.08, -0.92, 0.05),
1496        (0.64, -0.92, 0.05),
1497        (0.2, -0.16, 0.02),
1498        (0.66, -0.16, 0.05),
1499        (-0.34, 0.94, 0.04),
1500        (-0.16, 0.14, 0.02),
1501        (-0.64, 0.14, 0.05),
1502    ]);
1503    p.fan((0., 0.), &o, c);
1504}
1505
1506fn battery(p: &Pen, c: Color, d: Color) {
1507    p.rect(-0.88, -0.46, 1.6, 0.92, 0.16, c);
1508    p.rect(0.66, -0.2, 0.24, 0.4, 0.08, c);
1509    for i in 0..3 {
1510        p.rect(-0.7 + i as f32 * 0.34, -0.28, 0.24, 0.56, 0.05, d);
1511    }
1512}
1513
1514/// Heater-shield outline (or only its right half) with half-width `w`.
1515fn shield_outline(
1516    w: f32,
1517    (top, peak, side, tip): (f32, f32, f32, f32),
1518    right_half: bool,
1519) -> Outline {
1520    let mut o = Outline::new();
1521    if right_half {
1522        o.at(0., peak);
1523    } else {
1524        o.round(-w, top, 0.14).round(0., peak, 0.1);
1525    }
1526    o.round(w, top, 0.14)
1527        .at(w, side)
1528        .curve((w, side + (tip - side) * 0.6), (0., tip), 8);
1529    if !right_half {
1530        o.curve((-w, side + (tip - side) * 0.6), (-w, side), 8);
1531    }
1532    o
1533}
1534
1535fn shield(p: &Pen, c: Color, d: Color) {
1536    p.fill(&shield_outline(0.8, (-0.66, -0.86, -0.08, 0.9), false), c);
1537    p.fill(&shield_outline(0.56, (-0.49, -0.64, -0.04, 0.61), true), d);
1538}
1539
1540fn sword(p: &Pen, c: Color, d: Color) {
1541    let s = p.sub(0., 0., 1., deg(45.));
1542    s.fill(
1543        &Outline::corners(&[
1544            (-0.17, 0.3, 0.),
1545            (-0.17, -0.82, 0.),
1546            (0., -1.16, 0.04),
1547            (0.17, -0.82, 0.),
1548            (0.17, 0.3, 0.),
1549        ]),
1550        c,
1551    );
1552    s.poly(&[(0., 0.3), (0., -1.04), (0.13, -0.8), (0.13, 0.3)], d);
1553    s.capsule((-0.5, 0.36), (0.5, 0.36), 0.13, c);
1554    s.rect(-0.1, 0.38, 0.2, 0.52, 0.04, c);
1555    s.circle(0., 1.0, 0.17, c);
1556}
1557
1558fn bomb(p: &Pen, c: Color) {
1559    let p = &p.sub(-0.06, 0.03, 1., 0.);
1560    p.circle(-0.1, 0.16, 0.68, c);
1561    p.sub(0.34, -0.28, 1., deg(45.))
1562        .rect(-0.2, -0.2, 0.4, 0.3, 0.06, c);
1563    let mut fuse = Outline::new();
1564    fuse.at(0.44, -0.42).curve((0.48, -0.66), (0.7, -0.7), 6);
1565    p.stroke(&fuse, 0.13, c);
1566    p.fan((0.72, -0.72), &sparkle(0.72, -0.72, 0.2), c);
1567}
1568
1569fn flame(p: &Pen, c: Color, d: Color) {
1570    p.fill(&teardrop((-0.38, 0.4, 0.3), (-0.62, -0.26), 0.04), c);
1571    p.fill(&teardrop((0.4, 0.44, 0.28), (0.66, -0.1), 0.04), c);
1572    p.fill(&teardrop((0., 0.3, 0.58), (0.1, -0.92), 0.05), c);
1573    p.fill(&teardrop((0.02, 0.5, 0.3), (0.06, -0.1), 0.04), d);
1574}
1575
1576fn water_drop(p: &Pen, c: Color, d: Color) {
1577    p.fill(&teardrop((0., 0.28, 0.6), (0., -0.92), 0.04), c);
1578    p.arc((0., 0.28, 0.38), deg(105.), deg(60.), 0.13, d);
1579}
1580
1581fn snowflake(p: &Pen, c: Color) {
1582    p.fan((0., 0.), &arms(6, -FRAC_PI_2, 0.78, 0.09), c);
1583    for k in 0..6 {
1584        let u = Vec2::from_angle(-FRAC_PI_2 + k as f32 * TAU / 6.);
1585        let base = u * 0.44;
1586        let (l, r) = (
1587            base + u * 0.24 + u.perp() * 0.22,
1588            base + u * 0.24 - u.perp() * 0.22,
1589        );
1590        p.line(&[(l.x, l.y), (base.x, base.y), (r.x, r.y)], 0.16, c);
1591    }
1592}
1593
1594fn sun(p: &Pen, c: Color) {
1595    p.circle(0., 0., 0.42, c);
1596    for k in 0..8 {
1597        let u = Vec2::from_angle(k as f32 * TAU / 8.);
1598        p.capsule((u.x * 0.64, u.y * 0.64), (u.x * 0.8, u.y * 0.8), 0.095, c);
1599    }
1600}
1601
1602/// The upper crossing point of circles `a` and `b`, each `(x, y, r)`.
1603fn upper_crossing(a: (f32, f32, f32), b: (f32, f32, f32)) -> Vec2 {
1604    let (ca, cb) = (vec2(a.0, a.1), vec2(b.0, b.1));
1605    let dist = (cb - ca).length();
1606    let axis = (cb - ca) / dist;
1607    let along = (a.2 * a.2 - b.2 * b.2 + dist * dist) / (2. * dist);
1608    let lift = axis.perp() * (a.2 * a.2 - along * along).max(0.).sqrt();
1609    let (p, q) = (ca + axis * along + lift, ca + axis * along - lift);
1610    if p.y < q.y { p } else { q }
1611}
1612
1613/// A cloud: a flat-bottomed run of bumps traced as one outline.
1614fn cloud(p: &Pen, c: Color) {
1615    // End caps of the flat base, then the bumps from right to left.
1616    let circles = [
1617        (0.56, 0.3, 0.3),
1618        (0.14, -0.12, 0.5),
1619        (-0.36, 0.1, 0.36),
1620        (-0.56, 0.3, 0.3),
1621    ];
1622    let mut o = Outline::new();
1623    o.at(-0.56, 0.6);
1624    // Counter-clockwise along each circle, from where the previous one hands
1625    // over to where the next takes over; the last runs down to the base.
1626    let mut from = FRAC_PI_2;
1627    for (i, &(x, y, r)) in circles.iter().enumerate() {
1628        let (to, next_from) = match circles.get(i + 1) {
1629            Some(&(nx, ny, nr)) => {
1630                let crossing = upper_crossing((x, y, r), (nx, ny, nr));
1631                (
1632                    (crossing - vec2(x, y)).to_angle(),
1633                    (crossing - vec2(nx, ny)).to_angle(),
1634                )
1635            }
1636            None => (FRAC_PI_2, FRAC_PI_2),
1637        };
1638        o.arc((x, y, r), from, -(from - to).rem_euclid(TAU));
1639        from = next_from;
1640    }
1641    p.fan((0., 0.3), &o, c);
1642}
1643
1644fn rain(p: &Pen, c: Color) {
1645    cloud(&p.sub(0., -0.24, 0.8, 0.), c);
1646    for x in [-0.42, 0., 0.42] {
1647        p.capsule((x + 0.08, 0.44), (x - 0.04, 0.72), 0.08, c);
1648    }
1649}
1650
1651fn wind(p: &Pen, c: Color) {
1652    let w = 0.17;
1653    let mut top = Outline::new();
1654    top.at(-0.74, -0.3)
1655        .at(0.38, -0.3)
1656        .arc((0.38, -0.52, 0.22), FRAC_PI_2, deg(-250.));
1657    p.stroke(&top, w, c);
1658    p.line(&[(-0.74, 0.04), (0.72, 0.04)], w, c);
1659    let mut bottom = Outline::new();
1660    bottom
1661        .at(-0.58, 0.38)
1662        .at(0.42, 0.38)
1663        .arc((0.42, 0.58, 0.2), -FRAC_PI_2, deg(250.));
1664    p.stroke(&bottom, w, c);
1665}
1666
1667fn wave(p: &Pen, c: Color) {
1668    for y in [-0.28, 0.3] {
1669        let mut o = Outline::new();
1670        for i in 0..=16 {
1671            let t = i as f32 / 16.;
1672            o.at(-0.82 + 1.64 * t, y - 0.15 * (t * TAU * 2.).sin());
1673        }
1674        p.stroke(&o, 0.18, c);
1675    }
1676}
1677
1678fn leaf(p: &Pen, c: Color, d: Color) {
1679    let s = p.sub(0.06, -0.06, 1., deg(-45.));
1680    s.capsule((-0.9, 0.), (-1.12, 0.), 0.07, c);
1681    s.fill(&leaf_outline((-0.92, 0.), (1., 0.), 0.42), c);
1682    s.line(&[(-0.74, 0.), (0.7, 0.)], 0.11, d);
1683    for x in [-0.3, 0.16] {
1684        s.line(&[(x, 0.), (x + 0.24, -0.22)], 0.09, d);
1685        s.line(&[(x, 0.), (x + 0.24, 0.22)], 0.09, d);
1686    }
1687}
1688
1689fn sprout(p: &Pen, c: Color, d: Color) {
1690    p.capsule((0., 0.8), (0., -0.12), 0.09, c);
1691    p.capsule((-0.5, 0.8), (0.5, 0.8), 0.08, c);
1692    p.fill(&leaf_outline((0., -0.08), (-0.84, -0.46), 0.24), c);
1693    p.fill(&leaf_outline((0., -0.2), (0.86, -0.8), 0.3), c);
1694    p.line(&[(-0.1, -0.12), (-0.66, -0.4)], 0.09, d);
1695    p.line(&[(0.1, -0.26), (0.66, -0.68)], 0.09, d);
1696}
1697
1698fn tree(p: &Pen, c: Color) {
1699    p.rect(-0.13, 0.46, 0.26, 0.44, 0.04, c);
1700    for (top, bottom, half) in [(-0.3, 0.56, 0.82), (-0.62, 0.14, 0.64), (-0.92, -0.3, 0.46)] {
1701        p.fill(
1702            &Outline::corners(&[(0., top, 0.06), (half, bottom, 0.08), (-half, bottom, 0.08)]),
1703            c,
1704        );
1705    }
1706}
1707
1708fn mountain(p: &Pen, c: Color, d: Color) {
1709    let (back, front) = ((0.42, -0.5), (-0.2, -0.84));
1710    let valley = meet(front, (0.6, 0.72), back, (-0.08, 0.72));
1711    let range = Outline::corners(&[
1712        (-0.92, 0.72, 0.06),
1713        (front.0, front.1, 0.08),
1714        (valley.0, valley.1, 0.03),
1715        (back.0, back.1, 0.08),
1716        (0.92, 0.72, 0.06),
1717    ]);
1718    p.fan((0.1, 0.55), &range, c);
1719    let cap = Outline::corners(&[
1720        (front.0, front.1, 0.08),
1721        (0.025, -0.4, 0.),
1722        (-0.06, -0.3, 0.02),
1723        (-0.18, -0.42, 0.02),
1724        (-0.3, -0.3, 0.02),
1725        (-0.403, -0.4, 0.),
1726    ]);
1727    p.fan((-0.19, -0.55), &cap, d);
1728}
1729
1730fn coin(p: &Pen, c: Color, d: Color) {
1731    p.circle(0., 0., 0.86, c);
1732    p.ring(0., 0., 0.65, 0.12, d);
1733    p.fan(
1734        (0., 0.03),
1735        &star_outline(0., 0.03, 0.4, 0.5, (0.05, 0.02)),
1736        d,
1737    );
1738}
1739
1740fn gem(p: &Pen, c: Color, d: Color) {
1741    let p = &p.sub(0., -0.05, 1., 0.);
1742    p.fill(
1743        &Outline::corners(&[
1744            (-0.48, -0.66, 0.06),
1745            (0.48, -0.66, 0.06),
1746            (0.92, -0.18, 0.06),
1747            (0., 0.86, 0.08),
1748            (-0.92, -0.18, 0.06),
1749        ]),
1750        c,
1751    );
1752    p.line(&[(-0.72, -0.18), (0.72, -0.18)], 0.1, d);
1753    p.line(
1754        &[
1755            (-0.24, -0.6),
1756            (-0.4, -0.18),
1757            (0., 0.66),
1758            (0.4, -0.18),
1759            (0.24, -0.6),
1760        ],
1761        0.1,
1762        d,
1763    );
1764}
1765
1766fn bag(p: &Pen, c: Color, d: Color) {
1767    let p = &p.sub(0., -0.03, 1., 0.);
1768    p.circle(0., 0.28, 0.6, c);
1769    p.poly(
1770        &[(-0.24, -0.44), (0.24, -0.44), (0.52, 0.04), (-0.52, 0.04)],
1771        c,
1772    );
1773    p.fill(
1774        &Outline::corners(&[
1775            (-0.24, -0.44, 0.),
1776            (0.24, -0.44, 0.),
1777            (0.46, -0.8, 0.1),
1778            (-0.46, -0.8, 0.1),
1779        ]),
1780        c,
1781    );
1782    p.capsule((-0.26, -0.44), (0.26, -0.44), 0.075, d);
1783}
1784
1785fn wooden_crate(p: &Pen, c: Color, d: Color) {
1786    p.rect(-0.82, -0.82, 1.64, 1.64, 0.12, c);
1787    p.frame((-0.62, -0.62, 1.24, 1.24), 0.04, 0.14, d);
1788    p.line(&[(-0.5, -0.5), (0.5, 0.5)], 0.14, d);
1789}
1790
1791fn scroll(p: &Pen, c: Color, d: Color) {
1792    p.rect(-0.58, -0.68, 1.16, 1.36, 0.04, c);
1793    p.capsule((-0.76, -0.7), (0.76, -0.7), 0.16, c);
1794    p.capsule((-0.76, 0.7), (0.76, 0.7), 0.16, c);
1795    for y in [-0.26, 0., 0.26] {
1796        p.capsule((-0.34, y), (0.34, y), 0.065, d);
1797    }
1798}
1799
1800fn book(p: &Pen, c: Color, d: Color) {
1801    p.rect(-0.66, -0.86, 1.36, 1.72, 0.12, c);
1802    p.capsule((-0.4, -0.74), (-0.4, 0.74), 0.07, d);
1803    p.capsule((-0.24, 0.6), (0.56, 0.6), 0.08, d);
1804    p.rect(-0.14, -0.58, 0.58, 0.2, 0.05, d);
1805}
1806
1807fn bulb(p: &Pen, c: Color, d: Color) {
1808    let (cy, r, neck) = (-0.26, 0.6, (0.3, 0.38));
1809    let to_neck = vec2(neck.0, neck.1 - cy);
1810    let tangent = to_neck.to_angle() - (r / to_neck.length()).acos();
1811    let mut glass = Outline::new();
1812    glass
1813        .at(neck.0, neck.1)
1814        .arc((0., cy, r), tangent, -(TAU - (PI - 2. * tangent)))
1815        .at(-neck.0, neck.1);
1816    p.fill(&glass, c);
1817    p.rect(-0.3, 0.36, 0.6, 0.34, 0.06, c);
1818    p.rect(-0.15, 0.66, 0.3, 0.22, 0.08, c);
1819    p.line(&[(-0.22, 0.47), (0.22, 0.47)], 0.1, d);
1820    p.line(&[(-0.22, 0.6), (0.22, 0.6)], 0.1, d);
1821}
1822
1823// Tools and travel.
1824
1825fn hammer(p: &Pen, c: Color, d: Color) {
1826    let s = p.sub(0., 0., 1., deg(45.));
1827    s.capsule((0., -0.3), (0., 1.06), 0.13, c);
1828    s.rect(-0.62, -0.62, 1.24, 0.44, 0.08, c);
1829    s.capsule((0., 0.64), (0., 1.06), 0.13, d);
1830}
1831
1832fn wrench(p: &Pen, c: Color) {
1833    p.capsule((-0.62, 0.62), (0.12, -0.12), 0.16, c);
1834    p.arc((0.34, -0.34, 0.28), deg(20.), deg(230.), 0.34, c);
1835}
1836
1837fn pickaxe(p: &Pen, c: Color, d: Color) {
1838    let s = p.sub(0.06, -0.06, 1., deg(45.));
1839    s.capsule((0., -0.5), (0., 1.06), 0.12, c);
1840    s.crescent((0., 0.05, 0.75), (0., 0.707, 1.127), c);
1841    s.rect(-0.16, -0.62, 0.32, 0.28, 0.05, d);
1842}
1843
1844fn anchor(p: &Pen, c: Color) {
1845    p.ring(0., -0.66, 0.15, 0.14, c);
1846    p.capsule((0., -0.46), (0., 0.74), 0.1, c);
1847    p.capsule((-0.38, -0.24), (0.38, -0.24), 0.09, c);
1848    let arms = (0., 0.12, 0.64);
1849    p.arc(arms, deg(22.), deg(136.), 0.18, c);
1850    arrowhead(p, arms, deg(22.), -1., (0.32, 0.23), c);
1851    arrowhead(p, arms, deg(158.), 1., (0.32, 0.23), c);
1852}
1853
1854fn ship(p: &Pen, c: Color, d: Color) {
1855    p.fill(
1856        &Outline::corners(&[
1857            (-0.9, 0.34, 0.04),
1858            (0.9, 0.34, 0.04),
1859            (0.62, 0.82, 0.14),
1860            (-0.62, 0.82, 0.14),
1861        ]),
1862        c,
1863    );
1864    p.rect(-0.07, -0.9, 0.14, 1.3, 0.05, c);
1865    p.fill(
1866        &Outline::corners(&[(0.16, -0.84, 0.04), (0.76, 0.2, 0.05), (0.16, 0.2, 0.04)]),
1867        c,
1868    );
1869    p.fill(
1870        &Outline::corners(&[(-0.16, -0.6, 0.04), (-0.16, 0.2, 0.04), (-0.64, 0.2, 0.05)]),
1871        c,
1872    );
1873    p.line(&[(-0.52, 0.58), (0.52, 0.58)], 0.1, d);
1874}
1875
1876fn rocket(p: &Pen, c: Color, d: Color) {
1877    let s = p.sub(-0.02, 0.02, 0.98, deg(45.));
1878    let (w, top, bottom, tip): (f32, f32, f32, f32) = (0.3, -0.3, 0.5, -1.1);
1879    // Ogive nose: each side is an arc tangent to the body, meeting at the tip.
1880    let radius = (w * w + (top - tip) * (top - tip)) / (2. * w);
1881    let reach = (top - tip).atan2(radius - w);
1882    let mut body = Outline::new();
1883    body.round(-w, bottom, 0.06)
1884        .round(w, bottom, 0.06)
1885        .arc((w - radius, top, radius), 0., -reach)
1886        .arc((radius - w, top, radius), -(PI - reach), -reach);
1887    s.fill(&body, c);
1888    for side in [-1., 1.] {
1889        s.fill(
1890            &Outline::corners(&[
1891                (side * 0.26, 0., 0.),
1892                (side * 0.58, 0.38, 0.06),
1893                (side * 0.58, 0.66, 0.06),
1894                (side * 0.26, 0.48, 0.),
1895            ]),
1896            c,
1897        );
1898    }
1899    s.fill(&teardrop((0., 0.72, 0.16), (0., 1.08), 0.03), c);
1900    s.circle(0., -0.36, 0.15, d);
1901}
1902
1903fn planet(p: &Pen, c: Color, d: Color) {
1904    let (a, b, tilt) = (0.88, 0.3, Vec2::from_angle(deg(-20.)));
1905    let ring = |from: f32, to: f32| {
1906        let mut o = Outline::new();
1907        for i in 0..=14 {
1908            let t = from + (to - from) * i as f32 / 14.;
1909            let q = vec2(a * t.cos(), b * t.sin()).rotate(tilt);
1910            o.at(q.x, q.y);
1911        }
1912        o
1913    };
1914    p.stroke(&ring(PI, TAU), 0.13, c);
1915    p.circle(0., 0., 0.52, c);
1916    p.stroke(&ring(deg(52.), deg(128.)), 0.27, d);
1917    p.stroke(&ring(0., PI), 0.13, c);
1918}
1919
1920fn train(p: &Pen, c: Color, d: Color) {
1921    p.rect(-0.62, -0.88, 1.24, 1.38, 0.26, c);
1922    p.rect(-0.42, -0.64, 0.84, 0.46, 0.08, d);
1923    p.circle(-0.3, 0.2, 0.1, d);
1924    p.circle(0.3, 0.2, 0.1, d);
1925    p.capsule((-0.3, 0.5), (-0.54, 0.82), 0.09, c);
1926    p.capsule((0.3, 0.5), (0.54, 0.82), 0.09, c);
1927}
1928
1929// People and creatures.
1930
1931fn person(p: &Pen, c: Color) {
1932    p.circle(0., -0.46, 0.36, c);
1933    p.fill(
1934        &Outline::corners(&[
1935            (-0.74, 0.88, 0.08),
1936            (-0.74, 0.04, 0.4),
1937            (0.74, 0.04, 0.4),
1938            (0.74, 0.88, 0.08),
1939        ]),
1940        c,
1941    );
1942}
1943
1944fn people(p: &Pen, c: Color, d: Color) {
1945    let p = &p.sub(0., -0.04, 1., 0.);
1946    person(&p.sub(0.3, -0.12, 0.76, 0.), d);
1947    person(&p.sub(-0.18, 0.08, 0.86, 0.), c);
1948}
1949
1950fn paw(p: &Pen, c: Color) {
1951    p.fill(
1952        &Outline::corners(&[(-0.54, 0.74, 0.28), (0., 0.06, 0.3), (0.54, 0.74, 0.28)]),
1953        c,
1954    );
1955    for (x, y, tilt) in [
1956        (-0.68, -0.04, -20.),
1957        (-0.26, -0.46, -8.),
1958        (0.26, -0.46, 8.),
1959        (0.68, -0.04, 20.),
1960    ] {
1961        p.fill(&ellipse(x, y, (0.17, 0.23), deg(tilt)), c);
1962    }
1963}
1964
1965fn bone(p: &Pen, c: Color) {
1966    p.capsule((-0.48, 0.48), (0.48, -0.48), 0.17, c);
1967    for (x, y) in [(0.65, -0.35), (0.35, -0.65), (-0.65, 0.35), (-0.35, 0.65)] {
1968        p.circle(x, y, 0.22, c);
1969    }
1970}
1971
1972fn potion(p: &Pen, c: Color, d: Color) {
1973    p.circle(0., 0.26, 0.62, c);
1974    p.rect(-0.21, -0.68, 0.42, 0.52, 0.04, c);
1975    p.rect(-0.31, -0.88, 0.62, 0.24, 0.08, c);
1976    let (cy, r, surface): (f32, f32, f32) = (0.26, 0.46, 0.14);
1977    let s = ((surface - cy) / r).asin();
1978    let mut liquid = Outline::new();
1979    liquid.arc((0., cy, r), s, PI - 2. * s);
1980    p.fill(&liquid, d);
1981    p.circle(0.16, 0.42, 0.08, c);
1982    p.circle(-0.12, 0.56, 0.06, c);
1983}
1984
1985fn fish(p: &Pen, c: Color, d: Color) {
1986    p.fill(&teardrop((0.28, 0., 0.58), (-0.46, 0.), 0.04), c);
1987    let tail = Outline::corners(&[
1988        (-0.34, 0., 0.),
1989        (-0.88, -0.52, 0.08),
1990        (-0.7, 0., 0.04),
1991        (-0.88, 0.52, 0.08),
1992    ]);
1993    p.fan((-0.58, 0.), &tail, c);
1994    p.circle(0.52, -0.14, 0.11, d);
1995    p.arc((-0.22, 0., 0.46), deg(-38.), deg(76.), 0.1, d);
1996}
1997
1998fn music(p: &Pen, c: Color) {
1999    p.fill(&ellipse(-0.3, 0.56, (0.33, 0.25), deg(-24.)), c);
2000    p.rect(-0.04, -0.86, 0.18, 1.42, 0.05, c);
2001    p.line(
2002        &[(0.08, -0.8), (0.36, -0.6), (0.58, -0.32), (0.52, -0.02)],
2003        0.18,
2004        c,
2005    );
2006}
2007
2008fn chat(p: &Pen, c: Color, d: Color) {
2009    p.rect(-0.88, -0.78, 1.76, 1.2, 0.34, c);
2010    p.fill(
2011        &Outline::corners(&[(-0.52, 0.26, 0.), (-0.08, 0.26, 0.), (-0.62, 0.84, 0.06)]),
2012        c,
2013    );
2014    for x in [-0.4, 0., 0.4] {
2015        p.circle(x, -0.18, 0.11, d);
2016    }
2017}
2018
2019fn pot(p: &Pen, c: Color, d: Color) {
2020    p.capsule((-0.8, 0.16), (-0.6, 0.16), 0.09, c);
2021    p.capsule((0.6, 0.16), (0.8, 0.16), 0.09, c);
2022    p.rect(-0.66, -0.12, 1.32, 0.98, 0.22, c);
2023    p.rect(-0.74, -0.28, 1.48, 0.2, 0.08, c);
2024    p.sector((0., -0.28, 0.5), PI, PI, c);
2025    p.circle(0., -0.78, 0.11, c);
2026    p.line(&[(-0.46, -0.28), (0.46, -0.28)], 0.1, d);
2027}
2028
2029fn thermometer(p: &Pen, c: Color, d: Color) {
2030    let x = -0.16;
2031    p.capsule((x, -0.7), (x, 0.3), 0.21, c);
2032    p.circle(x, 0.52, 0.35, c);
2033    p.capsule((x, -0.3), (x, 0.4), 0.085, d);
2034    p.circle(x, 0.52, 0.2, d);
2035    for (y, len) in [(-0.56, 0.28), (-0.3, 0.2), (-0.04, 0.28)] {
2036        p.capsule((x + 0.4, y), (x + 0.4 + len, y), 0.07, c);
2037    }
2038}
2039
2040fn fuel(p: &Pen, c: Color, d: Color) {
2041    let p = &p.sub(0.08, 0., 1., 0.);
2042    p.frame((-0.04, -0.82, 0.6, 0.4), 0.12, 0.15, c);
2043    p.capsule((-0.5, -0.36), (-0.68, -0.56), 0.15, c);
2044    p.fill(
2045        &Outline::corners(&[
2046            (-0.68, -0.08, 0.06),
2047            (-0.24, -0.52, 0.06),
2048            (0.64, -0.52, 0.12),
2049            (0.64, 0.88, 0.12),
2050            (-0.68, 0.88, 0.12),
2051        ]),
2052        c,
2053    );
2054    p.line(&[(-0.36, 0.0), (0.32, 0.64)], 0.14, d);
2055    p.line(&[(0.32, 0.0), (-0.36, 0.64)], 0.14, d);
2056}
2057
2058#[cfg(test)]
2059mod tests {
2060    use super::*;
2061    use crate::shapes::tests::capture;
2062
2063    /// Every polygon handed to `fill_polygon` must be convex: a fan from its
2064    /// centroid only covers convex outlines exactly.
2065    pub(super) fn assert_convex(points: &[Vec2]) {
2066        let n = points.len();
2067        let mut sign = 0.;
2068        for i in 0..n {
2069            let (a, b, e) = (points[i], points[(i + 1) % n], points[(i + 2) % n]);
2070            let (u, v) = (b - a, e - b);
2071            let turn = u.perp_dot(v) / (u.length() * v.length()).max(1e-12);
2072            if turn.abs() < 2e-3 {
2073                continue;
2074            }
2075            if sign == 0. {
2076                sign = turn.signum();
2077            }
2078            assert!(turn * sign > 0., "concave corner at {b:?} in {points:?}");
2079        }
2080    }
2081
2082    /// A ladder outline must split into convex quads between its rungs.
2083    pub(super) fn assert_ladder(points: &[Vec2]) {
2084        let n = points.len();
2085        assert!(n >= 4 && n.is_multiple_of(2), "ladder of {n} points");
2086        for k in 0..n / 2 {
2087            let quad = [
2088                points[k],
2089                points[k + 1],
2090                points[n - k - 1],
2091                points[(n - k) % n],
2092            ];
2093            let mut corners = [Vec2::ZERO; 4];
2094            let mut len = 0;
2095            for p in quad {
2096                if len == 0 || (p - corners[len - 1]).length() > 1e-4 {
2097                    corners[len] = p;
2098                    len += 1;
2099                }
2100            }
2101            if len > 3 && (corners[len - 1] - corners[0]).length() <= 1e-4 {
2102                len -= 1;
2103            }
2104            assert_convex(&corners[..len]);
2105        }
2106    }
2107
2108    /// A fanned outline must face its hub along every edge.
2109    pub(super) fn assert_star_shaped(points: &[Vec2], hub: Vec2) {
2110        let n = points.len();
2111        let area: f32 = (0..n)
2112            .map(|i| points[i].perp_dot(points[(i + 1) % n]))
2113            .sum();
2114        for i in 0..n {
2115            let (a, b) = (points[i], points[(i + 1) % n]);
2116            let side =
2117                (b - a).perp_dot(hub - a) / ((b - a).length() * (hub - a).length()).max(1e-12);
2118            assert!(
2119                side * area.signum() > -1e-3,
2120                "hub {hub:?} is behind edge {a:?}-{b:?}"
2121            );
2122        }
2123    }
2124
2125    #[test]
2126    fn names_round_trip_and_are_unique() {
2127        let mut names: Vec<_> = Icon::ALL.iter().map(|i| i.name()).collect();
2128        assert!(names.len() >= 80);
2129        for icon in Icon::ALL {
2130            assert_eq!(Icon::from_name(icon.name()), Some(*icon));
2131        }
2132        names.sort_unstable();
2133        names.dedup();
2134        assert_eq!(names.len(), Icon::ALL.len());
2135        assert_eq!(Icon::from_name("not-an-icon"), None);
2136    }
2137
2138    #[test]
2139    fn every_icon_draws_finite_geometry_inside_its_box() {
2140        for &icon in Icon::ALL {
2141            let v = capture(200, 200, || {
2142                draw(icon, vec2(100., 100.), 64., Color::new(1., 1., 1., 1.))
2143            });
2144            assert!(!v.is_empty(), "{} drew nothing", icon.name());
2145            assert!(v.len() <= 12_000, "{}: {} vertices", icon.name(), v.len());
2146            for vertex in &v {
2147                assert!(vertex.iter().all(|x| x.is_finite()), "{}", icon.name());
2148                // Clip space to pixels; allow the fringe and round caps.
2149                let x = (vertex[0] + 1.) * 100.;
2150                let y = (1. - vertex[1]) * 100.;
2151                assert!(
2152                    (64. ..=136.).contains(&x) && (64. ..=136.).contains(&y),
2153                    "{} leaves its box at ({x}, {y})",
2154                    icon.name()
2155                );
2156            }
2157        }
2158    }
2159
2160    #[test]
2161    fn invalid_input_draws_nothing() {
2162        let v = capture(100, 100, || {
2163            draw(
2164                Icon::Star,
2165                vec2(f32::NAN, 0.),
2166                20.,
2167                Color::new(1., 1., 1., 1.),
2168            );
2169            draw(Icon::Star, vec2(10., 10.), 0., Color::new(1., 1., 1., 1.));
2170            draw(Icon::Star, vec2(10., 10.), -4., Color::new(1., 1., 1., 1.));
2171            badge(
2172                Icon::Star,
2173                vec2(10., 10.),
2174                f32::INFINITY,
2175                Color::new(0., 0., 0., 1.),
2176                Color::new(1., 1., 1., 1.),
2177            );
2178        });
2179        assert!(v.is_empty());
2180    }
2181
2182    #[test]
2183    fn every_icon_stays_in_its_box_at_hud_and_menu_sizes() {
2184        for &icon in Icon::ALL {
2185            for size in [12., 24., 128.] {
2186                let v = capture(400, 400, || {
2187                    let style = IconStyle::new(Color::new(0.9, 0.6, 0.2, 0.7))
2188                        .shadow(Color::new(0., 0., 0., 0.3));
2189                    draw_styled(icon, vec2(200., 200.), size, style);
2190                });
2191                assert!(!v.is_empty(), "{} drew nothing at {size}", icon.name());
2192                // Geometry stays within 95% of the half size; allow the
2193                // shadow's drop and the fringe (up to two pixels at mitres).
2194                let (reach, drop) = (size * 0.5 * 0.95 + 2.5, (size * 0.06).max(1.));
2195                for p in &v {
2196                    let (x, y) = ((p[0] + 1.) * 200. - 200., (1. - p[1]) * 200. - 200.);
2197                    assert!(
2198                        x.abs() <= reach && y >= -reach && y <= reach + drop,
2199                        "{} at {size} px reaches ({x}, {y})",
2200                        icon.name()
2201                    );
2202                }
2203            }
2204            let v = capture(400, 400, || {
2205                badge(
2206                    icon,
2207                    vec2(200., 200.),
2208                    40.,
2209                    Color::new(0.1, 0.2, 0.3, 1.),
2210                    Color::new(1., 1., 1., 1.),
2211                )
2212            });
2213            assert!(
2214                v.iter().flatten().all(|x| x.is_finite()),
2215                "{} badge",
2216                icon.name()
2217            );
2218        }
2219    }
2220
2221    /// Detail-free icons drawn translucent: no pixel is covered by two opaque
2222    /// interior triangles, so a faded heart, star or arrow stays even.
2223    #[test]
2224    fn single_piece_icons_cover_each_pixel_once() {
2225        let single = [
2226            Icon::Play,
2227            Icon::Pause,
2228            Icon::Check,
2229            Icon::Cross,
2230            Icon::Plus,
2231            Icon::Minus,
2232            Icon::ArrowUp,
2233            Icon::ArrowDown,
2234            Icon::ArrowLeft,
2235            Icon::ArrowRight,
2236            Icon::Pointer,
2237            Icon::Target,
2238            Icon::Star,
2239            Icon::Heart,
2240            Icon::Bolt,
2241            Icon::Sun,
2242            Icon::Moon,
2243            Icon::Cloud,
2244        ];
2245        for icon in single {
2246            let alpha = 0.5;
2247            let v = capture(200, 200, || {
2248                draw(icon, vec2(100., 100.), 64., Color::new(1., 1., 1., alpha))
2249            });
2250            let mut hits = vec![0u8; 200 * 200];
2251            for t in v.chunks_exact(3) {
2252                if !t.iter().all(|p| (p[7] - alpha).abs() < 1e-6) {
2253                    continue; // fringe
2254                }
2255                let q = [0, 1, 2].map(|k| vec2((t[k][0] + 1.) * 100., (1. - t[k][1]) * 100.));
2256                let side = |a: Vec2, b: Vec2, p: Vec2| (b - a).perp_dot(p - a);
2257                let (lo, hi) = (q[0].min(q[1]).min(q[2]), q[0].max(q[1]).max(q[2]));
2258                for y in lo.y.floor().max(0.) as usize..(hi.y.ceil() as usize).min(200) {
2259                    for x in lo.x.floor().max(0.) as usize..(hi.x.ceil() as usize).min(200) {
2260                        // Off-grid samples avoid landing on shared edges.
2261                        let p = vec2(x as f32 + 0.37, y as f32 + 0.61);
2262                        let s = [
2263                            side(q[0], q[1], p),
2264                            side(q[1], q[2], p),
2265                            side(q[2], q[0], p),
2266                        ];
2267                        if s.iter().all(|&s| s > 0.) || s.iter().all(|&s| s < 0.) {
2268                            hits[y * 200 + x] += 1;
2269                        }
2270                    }
2271                }
2272            }
2273            let twice = hits.iter().filter(|&&h| h > 1).count();
2274            assert_eq!(twice, 0, "{} covers {twice} pixels twice", icon.name());
2275        }
2276    }
2277
2278    #[test]
2279    fn rounded_corners_are_tangent_fillets() {
2280        let square = Outline::corners(&[
2281            (-1., -1., 0.5),
2282            (1., -1., 0.5),
2283            (1., 1., 0.5),
2284            (-1., 1., 0.5),
2285        ]);
2286        let (mut lo, mut hi, mut nearest) = (Vec2::splat(9.), Vec2::splat(-9.), f32::MAX);
2287        square.trace(|x, y| {
2288            let p = vec2(x, y);
2289            (lo, hi) = (lo.min(p), hi.max(p));
2290            nearest = nearest.min((p - vec2(1., 1.)).length());
2291            // Every point lies on the rounded square: an edge or a fillet arc.
2292            let corner = p.abs() - Vec2::splat(0.5);
2293            let on_arc = corner.x >= 0. && corner.y >= 0.;
2294            assert!(!on_arc || (corner.length() - 0.5).abs() < 1e-4, "{p:?}");
2295        });
2296        assert!((lo - Vec2::splat(-1.)).length() < 1e-5 && (hi - Vec2::ONE).length() < 1e-5);
2297        // The arc passes (√2 - 1) / 2 from the sharp corner it replaces.
2298        assert!((0.2071..0.23).contains(&nearest), "{nearest}");
2299        // A closing point that repeats the first is dropped.
2300        let mut count = 0;
2301        Outline::of(&[(0., 0.), (1., 0.), (1., 1.), (0., 0.)]).trace(|_, _| count += 1);
2302        assert_eq!(count, 3);
2303    }
2304
2305    #[test]
2306    #[should_panic(expected = "concave corner")]
2307    fn convexity_check_rejects_concave_outlines() {
2308        let dart = [vec2(0., 0.), vec2(2., 1.), vec2(0., 2.), vec2(0.6, 1.)];
2309        assert_convex(&dart);
2310    }
2311}