tikra 1.2.0

TikZ figures to SVG and PNG with no TeX install; math labels by mathra.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
//! What happens to a path once built (`\pgfusepath`): rounded corners, shortening,
//! clipping, filling and stroking, arrow tips, `double`, and the picture's bounding box.

use crate::canvas::{Fill, Item, Paint, Stroke};
use crate::scene::{Point, Segment};
use crate::tex::Token;
use crate::Error;

use super::arrows::{Curvilinear, EndTips, TipsMode, Widths};
use super::path::{valid_segment, MAX_COORDINATE};
use super::Engine;
use crate::PT;

impl Engine {
    /// Whether what is drawn now makes the picture larger: not after `use as bounding box`, nor
    /// after a clip in the scope.
    pub fn counts_for_size(&self) -> bool {
        !self.canvas_frozen && !self.scopes.iter().any(|s| s.clipped)
    }

    /// Uses the path as the state says: clip, fill, draw, arrows.
    pub fn use_path(&mut self, segments: Vec<Segment>, command: &str) -> Result<(), Error> {
        if !segments.iter().all(valid_segment) {
            self.warn(format!("{command}: coordinate is not finite or beyond ±{MAX_COORDINATE} cm (division by zero?): path ignored"));
            return Ok(());
        }
        self.use_path_here(segments)
    }

    fn use_path_here(&mut self, segments: Vec<Segment>) -> Result<(), Error> {
        let mut segs = segments;
        // `use path=\p`: the path kept by `save path=\p` instead; `save path` keeps this one
        if let Some(Token::Cs(n)) = self.st.use_saved_path.take() {
            if let Some(saved) = self.saved_paths.get(&*n) {
                segs = saved.clone();
            }
        }
        if let Some(Token::Cs(n)) = self.st.save_path.take() {
            self.saved_paths.insert(n.to_string(), segs.clone());
        }
        if !self.st.nonlinear.is_empty() {
            segs = self.nonlinear_path(&segs)?;
        }
        if self.st.rounded > 0.0 {
            segs = round_corners(&segs, self.st.rounded);
        }
        let mode = self.st.mode.clone();
        // the bounding box grows as the path is built: before any shortening
        if mode.bounding_box {
            self.canvas.include_path(&segs);
            self.canvas_frozen = true;
        } else if self.counts_for_size() {
            self.canvas.include_path(&segs);
            // a stroked path adds half its line width around its own box (\pgfusepath)
            if mode.draw {
                let w = match self.st.double {
                    Some(_) => 2.0 * self.st.line_width + self.st.double_distance,
                    None => self.st.line_width,
                };
                let mut path = crate::canvas::Canvas::default();
                path.include_path(&segs);
                if let (Some(lo), Some(hi)) = (path.min, path.max) {
                    self.canvas.include(Point::new(lo.x - w / 2.0, lo.y - w / 2.0));
                    self.canvas.include(Point::new(hi.x + w / 2.0, hi.y + w / 2.0));
                }
            }
        }
        let drawn = segs.iter().any(|s| !matches!(s, Segment::MoveTo(_)));
        // a functional shading: cells clipped to the path, under its stroke
        let functional = if mode.shade && drawn && !mode.pattern { self.functional_cells(&segs) } else { None };
        // a fill without a color of its own fills with the basic layer's fill pattern, if one is set
        // (`\pgfsetfillpattern{stars}{red}` before `\filldraw`)
        let pattern = mode.pattern || (mode.fill && self.st.fill_pattern && self.st.fill_color.is_none());
        let mode = super::state::Mode { shade: mode.shade && functional.is_none(), pattern, ..mode };
        let fill = if mode.fill || mode.pattern || mode.shade {
            let paint = if mode.pattern {
                let name = self.st.pattern_name.clone().unwrap_or_else(|| "north east lines".into());
                let color = self.st.pattern_color.unwrap_or(crate::color::Color::BLACK);
                self.pattern_paint(&name, color).unwrap_or(Paint::Pattern { name, color })
            } else if mode.shade {
                Paint::Shading(self.shading_for(&segs))
            } else {
                Paint::Solid(self.st.fill_paint_color())
            };
            Some(Fill { paint, opacity: self.st.fill_opacity, even_odd: self.st.even_odd })
        } else {
            None
        };
        // a pattern is the fill: \fill[pattern=…] paints no solid color under it unless fill= names one
        let solid_under = if mode.pattern && mode.fill && self.st.fill_color.is_some() {
            Some(Fill { paint: Paint::Solid(self.st.fill_paint_color()), opacity: self.st.fill_opacity, even_odd: self.st.even_odd })
        } else {
            None
        };
        let stroke = mode.draw.then(|| self.stroke());
        // a path fading masks this path, fitted to it
        let fading = match self.st.path_fading.clone().filter(|_| mode.fade) {
            Some(f) if self.picture_fadings.contains_key(&f) => self.picture_fading_for(&f, &segs, self.st.fit_fading),
            Some(f) => self.fading_for(&f, &segs).map(|mask| Item::Fade { mask }),
            None => None,
        };
        let faded = drawn && (fill.is_some() || stroke.is_some()) && fading.is_some();
        let blended = drawn && (fill.is_some() || stroke.is_some()) && self.st.blend.is_some();
        if blended {
            self.canvas.push(Item::Blend { mode: self.st.blend.clone().unwrap_or_default() });
        }
        if faded {
            self.canvas.push(fading.expect("checked"));
        }
        // `tips=true`/`proper`: tips on a path that is neither filled nor stroked
        let tips_only = stroke.is_none() && fill.is_none() && !mode.clip && matches!(self.st.tips_mode, TipsMode::True | TipsMode::Proper);
        // a clipping path has no tips and is not shortened; a lone point still gets them
        let tips = if !segs.is_empty() && !mode.clip && (stroke.is_some() || tips_only) { self.arrow_ends(&mut segs) } else { Tips::default() };
        if let Some(cells) = functional {
            self.canvas.push(Item::Clip { segments: segs.clone(), even_odd: self.st.even_odd });
            self.canvas.items.extend(cells);
            self.canvas.push(Item::End);
        }
        if drawn && (fill.is_some() || stroke.is_some()) {
            if let Some(under) = solid_under {
                self.canvas.push(Item::Path { segments: segs.clone(), fill: Some(under), stroke: None });
            }
            match (self.st.double, &stroke) {
                (Some(core), Some(s)) => {
                    // two lines: the outer one, then the inner one in the double color
                    let outer = Stroke { width: 2.0 * s.width + self.st.double_distance, ..s.clone() };
                    let inner = Stroke { width: self.st.double_distance, color: core, ..s.clone() };
                    self.canvas.push(Item::Path { segments: segs.clone(), fill, stroke: Some(outer) });
                    self.canvas.push(Item::Path { segments: segs.clone(), fill: None, stroke: Some(inner) });
                }
                (None, Some(s)) if self.st.inner_line.0 > 0.0 => {
                    // the basic layer's inner line, over the stroke
                    let inner = Stroke { width: self.st.inner_line.0, color: self.st.inner_line.1.unwrap_or(crate::color::Color::WHITE), ..s.clone() };
                    self.canvas.push(Item::Path { segments: segs.clone(), fill, stroke: Some(s.clone()) });
                    self.canvas.push(Item::Path { segments: segs.clone(), fill: None, stroke: Some(inner) });
                }
                _ => self.canvas.push(Item::Path { segments: segs.clone(), fill, stroke }),
            }
        }
        self.push_tips(tips);
        if faded {
            self.canvas.push(Item::End);
        }
        if blended {
            self.canvas.push(Item::End);
        }
        if mode.clip {
            self.open_in_scope(Item::Clip { segments: segs, even_odd: self.st.even_odd });
            // what the scope draws after a clip does not make the picture larger (pgf's
            // \pgf@relevantforpicturesizefalse)
            if let Some(s) = self.scopes.last_mut() {
                s.clipped = true;
            }
        }
        Ok(())
    }

    /// A fading by name, fitted to the path's bounding box (`fit fading`, on by default).
    pub(super) fn fading_for(&mut self, name: &str, segs: &[Segment]) -> Option<crate::canvas::Shading> {
        let mut c = crate::canvas::Canvas::default();
        c.include_path(segs);
        let (lo, hi) = (c.min?, c.max?);
        let center = lo.lerp(hi, 0.5);
        let fit = crate::scene::Transform::translation(center.x, center.y)
            .compose(&crate::scene::Transform::scaling(((hi.x - lo.x) / 50.0).max(1e-9), ((hi.y - lo.y) / 50.0).max(1e-9)))
            .compose(&self.st.fading_transform);
        if let Some(sh) = library_fading(name, &fit) {
            return Some(sh);
        }
        match self.fadings.get(name).cloned() {
            Some(mut sh) => {
                let (crate::canvas::Shading::Axial { transform, .. } | crate::canvas::Shading::Radial { transform, .. }) = &mut sh;
                *transform = fit.compose(transform);
                Some(sh)
            }
            None => {
                self.warn(format!("fading '{name}' not supported: drawn without it"));
                None
            }
        }
    }

    /// `blend mode=…`, `\pgfsetblendmode{…}`: pgf's names (`color dodge`) as SVG's (`color-dodge`).
    pub fn set_blend_mode(&mut self, mode: &str) {
        let mode = mode.trim().replace(' ', "-");
        self.st.blend = (mode != "normal" && !mode.is_empty()).then_some(mode);
    }

    /// The cells of the current shading if it is a functional one, fitted to the path's box.
    fn functional_cells(&mut self, segs: &[Segment]) -> Option<Vec<Item>> {
        if self.basic_shadings.contains_key(&self.st.shading) {
            return None;
        }
        let name = self.st.shading.clone();
        let f = self.functional_shading(&name)?;
        let mut c = crate::canvas::Canvas::default();
        c.include_path(segs);
        let (lo, hi) = (c.min?, c.max?);
        Some(f.cells(&f.fit(lo, hi, self.st.shade_angle)))
    }

    pub fn shading_of(&self, segs: &[Segment]) -> crate::canvas::Shading {
        self.shading_for(segs)
    }

    /// The current shading fitted to the path's bounding box, as `\pgfshadepath` fits it.
    fn shading_for(&self, segs: &[Segment]) -> crate::canvas::Shading {
        // `shading=name` of a shading the basic layer declared
        if let Some(sh) = self.basic_shading_for(&self.st.shading, segs, self.st.shade_angle) {
            return sh;
        }
        let mut c = crate::canvas::Canvas::default();
        c.include_path(segs);
        let (lo, hi) = (c.min.unwrap_or_default(), c.max.unwrap_or_default());
        let (w, h) = (hi.x - lo.x, hi.y - lo.y);
        let a = self.st.shade_angle;
        let (s, co) = a.to_radians().sin_cos();
        let (abs_s, abs_c) = (s.abs(), co.abs());
        // pgf's scaling: the shading's middle 50 bp cover the box along the rotated axes
        let sx = (w * abs_c + h * abs_s) / (abs_c + abs_s) / 50.0;
        let sy = (w * abs_s + h * abs_c) / (abs_c + abs_s) / 50.0;
        let center = lo.lerp(hi, 0.5);
        let transform = crate::scene::Transform::translation(center.x, center.y)
            .compose(&crate::scene::Transform::rotation(a))
            .compose(&crate::scene::Transform::scaling(sx.max(1e-9), sy.max(1e-9)));
        let st = &self.st;
        let blend = |x: crate::color::Color, y: crate::color::Color, t: f64| crate::color::Color {
            r: x.r * t + y.r * (1.0 - t),
            g: x.g * t + y.g * (1.0 - t),
            b: x.b * t + y.b * (1.0 - t),
        };
        let (white, black) = (crate::color::Color::WHITE, crate::color::Color::BLACK);
        match st.shading.as_str() {
            "ball" => crate::canvas::Shading::Radial {
                transform,
                focus: Point::new(-10.0, 10.0),
                stops: vec![
                    (0.0, blend(st.ball, white, 0.15)),
                    (9.0 / 50.0, blend(st.ball, white, 0.75)),
                    (18.0 / 50.0, blend(st.ball, black, 0.7)),
                    (0.5, blend(st.ball, black, 0.5)),
                    (1.0, black),
                ],
            },
            "radial" => crate::canvas::Shading::Radial { transform, focus: Point::default(), stops: vec![(0.0, st.radial_inner), (0.5, st.radial_outer), (1.0, st.radial_outer)] },
            _ => crate::canvas::Shading::Axial {
                transform,
                stops: vec![(0.0, st.axis_bottom), (0.25, st.axis_bottom), (0.5, st.axis_middle), (0.75, st.axis_top), (1.0, st.axis_top)],
            },
        }
    }

    /// Shortens the path for its arrow tips and `shorten <`/`shorten >`, and draws the tips, as
    /// `\pgf@prepare@end@of@path` and `\pgf@prepare@start@of@path` do: the end first, then the start
    /// of the path as the end left it. Nothing on a closed path.
    fn arrow_ends(&mut self, segs: &mut [Segment]) -> Tips {
        let mut out = Tips::default();
        if segs.iter().any(|s| matches!(s, Segment::Close)) || self.st.tips_mode == TipsMode::Never {
            return out;
        }
        // `tips=proper`, `on proper draw`: not on a path that stays at one point
        if matches!(self.st.tips_mode, TipsMode::Proper | TipsMode::OnProperDraw) && !proper(segs) {
            return out;
        }
        let s = self.stroke();
        let widths = match self.st.double {
            Some(_) => Widths { line: (2.0 * s.width + self.st.double_distance) / PT, inner: self.st.double_distance / PT },
            None => Widths { line: s.width / PT, inner: 0.0 },
        };
        let user = self.st.tips.clone();
        let mut warn = Vec::new();
        let (end_spec, start_spec) = (self.st.end_arrow.clone().unwrap_or_default(), self.st.start_arrow.clone().unwrap_or_default());
        let (shorten_end, shorten_start) = (self.st.shorten_end / PT, self.st.shorten_start / PT);
        let end = EndTips::new(self, &end_spec, false, &user, widths, shorten_end, &mut warn);
        let start = EndTips::new(self, &start_spec, true, &user, widths, shorten_start, &mut warn);
        for w in warn {
            self.warn(w);
        }
        let mut at_end = None;
        if !end.is_empty() || end.shortening != 0.0 {
            if let Some((k, movable, last, reference)) = path_end(segs) {
                // a curve with flexed tips: shortened along it, and the tips laid on it
                let curve = match (segs[k], end.flexed()) {
                    (Segment::CurveTo(c1, c2, _), true) => point_of(&segs[k - 1]).map(|prev| Curvilinear::new([last, c2, c1, prev])),
                    _ => None,
                };
                if movable && end.shortening != 0.0 {
                    match &curve {
                        Some(c) => {
                            let [p, b, a, _] = c.after(c.time(end.shortening));
                            segs[k] = Segment::CurveTo(a, b, p);
                        }
                        None => {
                            let p = toward(last, reference, end.shortening * PT);
                            if let Segment::LineTo(q) | Segment::CurveTo(_, _, q) = &mut segs[k] {
                                *q = p;
                            }
                        }
                    }
                }
                at_end = Some(match curve {
                    Some(c) => Along::Curve(c),
                    None => Along::Line(last, reference),
                });
            }
        }
        let mut at_start = None;
        if !start.is_empty() || start.shortening != 0.0 {
            if let Some((m, movable, first, reference)) = path_start(segs) {
                let curve = match (segs.get(m + 1), start.flexed()) {
                    (Some(&Segment::CurveTo(c1, c2, p)), true) => Some(Curvilinear::new([first, c1, c2, p])),
                    _ => None,
                };
                if movable && start.shortening != 0.0 {
                    match &curve {
                        Some(c) => {
                            let [p, a, b, q] = c.after(c.time(start.shortening));
                            segs[m] = Segment::MoveTo(p);
                            segs[m + 1] = Segment::CurveTo(a, b, q);
                        }
                        None => segs[m] = Segment::MoveTo(toward(first, reference, start.shortening * PT)),
                    }
                }
                at_start = Some(match curve {
                    Some(c) => Along::Curve(c),
                    None => Along::Line(first, reference),
                });
            }
        }
        // the stroke color and line width, without the dash; fills with the fill opacity
        let stroke = Stroke { width: widths.line * PT, ..s };
        let fill_opacity = self.st.fill_opacity;
        for (tips, at) in [(&start, at_start), (&end, at_end)] {
            let hull = match at {
                Some(Along::Line(p, from)) => tips.draw(p, from, &stroke, fill_opacity, &mut out.items),
                Some(Along::Curve(c)) => tips.draw_on_curve(&c, &stroke, fill_opacity, &mut out.items),
                None => continue,
            };
            out.hull.extend(hull);
        }
        out
    }

    fn push_tips(&mut self, tips: Tips) {
        if self.counts_for_size() {
            for p in tips.hull {
                self.canvas.include(p);
            }
        }
        self.canvas.items.extend(tips.items);
    }
}

/// What the tips of an end are laid along: the line from a point to the end, or the end's curve.
enum Along {
    Line(Point, Point),
    Curve(Curvilinear),
}

/// The drawing of a path's tips, and the points of their hulls (for the bounding box).
#[derive(Default)]
struct Tips {
    items: Vec<Item>,
    hull: Vec<Point>,
}

/// The point at `distance` from `p` toward `q` (`\pgfpointlineatdistance`). When they coincide pgf's
/// normalized vector points up.
fn toward(p: Point, q: Point, distance: f64) -> Point {
    let d = q.minus(p);
    let u = if d.norm() > 1e-12 { d.scale(1.0 / d.norm()) } else { Point::new(0.0, 1.0) };
    p.plus(u.scale(distance))
}

fn point_of(s: &Segment) -> Option<Point> {
    match s {
        Segment::MoveTo(p) | Segment::LineTo(p) | Segment::CurveTo(_, _, p) => Some(*p),
        Segment::Close => None,
    }
}

/// Where the last subpath (the only one with tips, as pgf splits the path) begins.
fn last_subpath(segs: &[Segment]) -> Option<usize> {
    segs.iter().rposition(|s| matches!(s, Segment::MoveTo(_)))
}

/// The end of the path (`\pgf@parse@end`): the last segment, if it may be shortened (not a lone
/// point), its end point, and the point the tip is aimed from: the previous point, or the last
/// control point of a curve, the one before if they coincide. pgf moves only the end point.
fn path_end(segs: &[Segment]) -> Option<(usize, bool, Point, Point)> {
    let k = segs.len().checked_sub(1)?;
    match segs[k] {
        Segment::MoveTo(p) => Some((k, false, p, p)),
        Segment::LineTo(last) => Some((k, true, last, point_of(&segs[k.checked_sub(1)?])?)),
        Segment::CurveTo(c1, c2, last) => {
            let r = if c2 != last {
                c2
            } else if c1 != last {
                c1
            } else {
                point_of(&segs[k.checked_sub(1)?])?
            };
            Some((k, true, last, r))
        }
        Segment::Close => None,
    }
}

/// The start of the last subpath (`\pgf@parse@start`): its index, if it may be shortened, the first
/// point and the point the tip is aimed from: the next point, or the first control point of a curve
/// (then the second, then its end, while they coincide).
fn path_start(segs: &[Segment]) -> Option<(usize, bool, Point, Point)> {
    let m = last_subpath(segs)?;
    let first = point_of(&segs[m])?;
    match segs.get(m + 1) {
        None => Some((m, false, first, first)),
        Some(&Segment::LineTo(p)) => Some((m, true, first, p)),
        Some(&Segment::CurveTo(c1, c2, p)) => Some((
            m,
            true,
            first,
            if c1 != first {
                c1
            } else if c2 != first {
                c2
            } else {
                p
            },
        )),
        _ => None,
    }
}

/// A path whose last subpath leaves its first point (`\pgf@path@check@proper`: each move starts
/// the test again).
fn proper(segs: &[Segment]) -> bool {
    let last = segs.iter().rposition(|s| matches!(s, Segment::MoveTo(_))).unwrap_or(0);
    let points: Vec<Point> = segs[last..]
        .iter()
        .flat_map(|s| match *s {
            Segment::MoveTo(p) | Segment::LineTo(p) => vec![p],
            Segment::CurveTo(a, b, p) => vec![a, b, p],
            Segment::Close => vec![],
        })
        .collect();
    points.iter().any(|p| Some(p) != points.first())
}

/// Rounded corners between straight segments.
pub fn round_corners(segs: &[Segment], r: f64) -> Vec<Segment> {
    let mut points: Vec<Vec<Point>> = Vec::new();
    let mut closed: Vec<bool> = Vec::new();
    for s in segs {
        match s {
            Segment::MoveTo(p) => {
                points.push(vec![*p]);
                closed.push(false);
            }
            Segment::LineTo(p) => {
                if let Some(v) = points.last_mut() {
                    v.push(*p);
                }
            }
            Segment::Close => {
                if let Some(f) = closed.last_mut() {
                    *f = true;
                }
            }
            Segment::CurveTo(..) => return segs.to_vec(),
        }
    }
    let mut out = Vec::new();
    for (pts, is_closed) in points.iter().zip(closed) {
        let mut p: Vec<Point> = pts.clone();
        if is_closed && p.len() > 2 && p.first().map(|a| a.minus(*p.last().unwrap()).norm() <= 1e-9).unwrap_or(false) {
            p.pop();
        }
        let n = p.len();
        if n < 3 {
            out.push(Segment::MoveTo(p[0]));
            for q in &p[1..] {
                out.push(Segment::LineTo(*q));
            }
            if is_closed {
                out.push(Segment::Close);
            }
            continue;
        }
        let corner = |k: usize| -> (Point, Point, Point) {
            let prev = p[(k + n - 1) % n];
            let here = p[k];
            let next = p[(k + 1) % n];
            let d1 = prev.minus(here);
            let d2 = next.minus(here);
            let rr = r.min(d1.norm() / 2.0).min(d2.norm() / 2.0);
            (here.plus(d1.scale(rr / d1.norm().max(1e-12))), here, here.plus(d2.scale(rr / d2.norm().max(1e-12))))
        };
        let indices: Vec<usize> = if is_closed { (0..n).collect() } else { (1..n - 1).collect() };
        if is_closed {
            out.push(Segment::MoveTo(corner(0).2));
        } else {
            out.push(Segment::MoveTo(p[0]));
        }
        for &k in indices.iter().skip(if is_closed { 1 } else { 0 }) {
            let (a, v, b) = corner(k);
            out.push(Segment::LineTo(a));
            out.push(Segment::CurveTo(a.lerp(v, 0.5523), b.lerp(v, 0.5523), b));
        }
        if is_closed {
            let (a, v, b) = corner(0);
            out.push(Segment::LineTo(a));
            out.push(Segment::CurveTo(a.lerp(v, 0.5523), b.lerp(v, 0.5523), b));
            out.push(Segment::Close);
        } else {
            out.push(Segment::LineTo(p[n - 1]));
        }
    }
    out
}

/// The fadings of the fadings library (opaque on one side and transparent on the other, or round),
/// their 100 bp shadings placed by `fit`.
pub fn library_fading(name: &str, fit: &crate::scene::Transform) -> Option<crate::canvas::Shading> {
    let fit = *fit;
    let (white, black) = (crate::color::Color::WHITE, crate::color::Color::BLACK);
    let axial = |angle: f64, from: crate::color::Color, to: crate::color::Color| crate::canvas::Shading::Axial {
        transform: fit.compose(&crate::scene::Transform::rotation(angle)),
        stops: vec![(0.0, from), (0.25, from), (0.75, to), (1.0, to)],
    };
    let circle =
        |edge: f64| crate::canvas::Shading::Radial { transform: fit, focus: Point::default(), stops: vec![(0.0, white), (edge / 50.0, white), (0.5, black), (1.0, black)] };
    Some(match name {
        // the library's: opaque on one side, transparent on the other
        "north" => axial(0.0, white, black),
        "south" => axial(0.0, black, white),
        "east" => axial(-90.0, white, black),
        "west" => axial(-90.0, black, white),
        "circle with fuzzy edge 10 percent" => circle(22.5),
        "circle with fuzzy edge 15 percent" => circle(21.25),
        "circle with fuzzy edge 20 percent" => circle(20.0),
        "fuzzy ring 15 percent" => crate::canvas::Shading::Radial {
            transform: fit,
            focus: Point::default(),
            stops: vec![(0.0, black), (21.25 / 50.0, black), (23.125 / 50.0, white), (0.5, black), (1.0, black)],
        },
        _ => return None,
    })
}