odox-core 0.2.0

Read and write OpenDocument text, spreadsheet and presentation packages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//! `draw:enhanced-geometry`: the outline of a custom shape, worked out.
//!
//! A custom shape does not state its outline as points. It states a *path* in a
//! compact command language, in a coordinate space of its own, whose numbers may
//! be references to named formulas that are themselves arithmetic over the
//! space's edges and over adjustment values a person dragged. A rounded
//! rectangle is `M ?f7 0 X 0 ?f8 L 0 ?f9 Y ?f7 21600 …`, and none of that means
//! anything until the formulas are evaluated.
//!
//! What comes out of here is [`Geometry`]: the same outline as flat polylines in
//! the shape's own space, ready for a renderer to map onto a rectangle. Curves
//! and arcs are flattened here rather than passed on, because the number of
//! segments a curve needs depends on the size of the coordinate space and not on
//! the size of the window, and this is where the space is known.
//!
//! # What is implemented
//!
//! The whole command language except `Q`'s smooth variants, and the formula
//! grammar in full. What is *measured* is narrower: across the twenty-three
//! presentation templates `LibreOffice` ships, the commands that occur are `M`,
//! `L`, `C`, `Z`, `N`, `U`, `X`, `Y` and `V`, and the formulas use the four edge
//! constants, `pi`, and `if`, `sin`, `cos` and `abs`. The arc commands `A`, `B`
//! and `W` occur nowhere in that set and their sweep direction is taken from the
//! specification rather than from a document.
//
// Author: David M. Anderson
// Built with AI assistance (Claude, Anthropic)

use std::cell::{Cell, RefCell};
use std::collections::HashMap;

use crate::xml::{Element, Ns};

/// The coordinate space a shape states its outline in.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ViewBox {
    /// The left edge.
    pub x: f32,
    /// The top edge.
    pub y: f32,
    /// How wide.
    pub width: f32,
    /// How tall.
    pub height: f32,
}

impl ViewBox {
    fn parse(text: &str) -> Option<Self> {
        let numbers: Vec<f32> = text
            .split_whitespace()
            .filter_map(|n| n.parse().ok())
            .collect();
        let [x, y, width, height] = numbers[..] else {
            return None;
        };
        (width > 0.0 && height > 0.0).then_some(Self {
            x,
            y,
            width,
            height,
        })
    }
}

/// One stroke of the pen: a run of points, and what is done with them.
#[derive(Debug, Clone, PartialEq)]
pub struct SubPath {
    /// The points, in the shape's own coordinate space.
    pub points: Vec<(f32, f32)>,
    /// Whether the last point joins the first.
    pub closed: bool,
    /// Whether the inside is filled. `F` in the path turns it off for the rest
    /// of the path, which is how a shape draws a detail over its own body.
    pub fill: bool,
    /// Whether the outline is drawn. `S` turns it off.
    pub stroke: bool,
}

/// A custom shape's outline.
#[derive(Debug, Clone, PartialEq)]
pub struct Geometry {
    /// The space the points are in.
    pub view: ViewBox,
    /// The strokes, in the order they are drawn.
    pub paths: Vec<SubPath>,
}

/// How finely a curve is broken into straight lines.
///
/// Sixteen segments for a whole cubic and one for every six degrees of an arc,
/// which at the sizes ODF's coordinate spaces use — twenty-one thousand six
/// hundred units across, drawn into a few hundred pixels — is below what a
/// screen can show.
const CURVE_SEGMENTS: usize = 16;
const DEGREES_PER_SEGMENT: f32 = 6.0;

impl Geometry {
    /// Read a `draw:enhanced-geometry` element.
    ///
    /// `None` where it states no path or no coordinate space, which is a shape
    /// nothing can draw.
    pub fn read(geometry: &Element) -> Option<Self> {
        let view = ViewBox::parse(geometry.attr(&Ns::Svg, "viewBox")?)?;
        let path = geometry.attr(&Ns::Draw, "enhanced-path")?;

        let formulas = Formulas::new(geometry, view);
        let mut pen = Pen::new();
        pen.run(path, &formulas);
        let mut paths = pen.finish();

        // A mirrored shape states its outline once and is drawn flipped.
        let flip_x = geometry.attr(&Ns::Draw, "mirror-horizontal") == Some("true");
        let flip_y = geometry.attr(&Ns::Draw, "mirror-vertical") == Some("true");
        if flip_x || flip_y {
            for path in &mut paths {
                for (x, y) in &mut path.points {
                    if flip_x {
                        *x = view.x + view.width - (*x - view.x);
                    }
                    if flip_y {
                        *y = view.y + view.height - (*y - view.y);
                    }
                }
            }
        }

        Some(Self { view, paths })
    }
}

/// The named formulas of one shape, and the values they are over.
struct Formulas<'a> {
    by_name: HashMap<&'a str, &'a str>,
    modifiers: Vec<f32>,
    view: ViewBox,
    /// Evaluated formulas, kept because a chain of thirty each referring to the
    /// one before is ordinary and re-evaluating it per reference is not.
    known: RefCell<HashMap<String, f32>>,
    depth: Cell<u32>,
}

/// How deep a formula may refer before it is called a cycle.
///
/// No writer produces one; a hand-edited file can, and the alternative to a
/// limit is a window that stops responding.
const MAX_DEPTH: u32 = 64;

impl<'a> Formulas<'a> {
    fn new(geometry: &'a Element, view: ViewBox) -> Self {
        let by_name = geometry
            .elements()
            .filter(|e| e.is(&Ns::Draw, "equation"))
            .filter_map(|e| Some((e.attr(&Ns::Draw, "name")?, e.attr(&Ns::Draw, "formula")?)))
            .collect();
        let modifiers = geometry
            .attr(&Ns::Draw, "modifiers")
            .unwrap_or_default()
            .split_whitespace()
            .filter_map(|n| n.parse().ok())
            .collect();
        Self {
            by_name,
            modifiers,
            view,
            known: RefCell::new(HashMap::new()),
            depth: Cell::new(0),
        }
    }

    /// The value of a named formula.
    fn named(&self, name: &str) -> f32 {
        if let Some(value) = self.known.borrow().get(name) {
            return *value;
        }
        if self.depth.get() >= MAX_DEPTH {
            return 0.0;
        }
        let Some(text) = self.by_name.get(name) else {
            return 0.0;
        };
        self.depth.set(self.depth.get() + 1);
        let value = self.eval(text);
        self.depth.set(self.depth.get() - 1);
        self.known.borrow_mut().insert(name.to_owned(), value);
        value
    }

    /// A modifier, which is an adjustment a person dragged and the document
    /// stored.
    fn modifier(&self, index: usize) -> f32 {
        self.modifiers.get(index).copied().unwrap_or(0.0)
    }

    /// One of the constants a formula may name.
    fn constant(&self, name: &str) -> Option<f32> {
        Some(match name {
            "left" => self.view.x,
            "top" => self.view.y,
            "right" => self.view.x + self.view.width,
            "bottom" => self.view.y + self.view.height,
            "width" | "logwidth" => self.view.width,
            "height" | "logheight" => self.view.height,
            "pi" => std::f32::consts::PI,
            // A shape may ask whether it is being stroked or filled and draw
            // differently. Nothing here answers no.
            "hasstroke" | "hasfill" => 1.0,
            "xstretch" | "ystretch" => 0.0,
            _ => return None,
        })
    }

    fn eval(&self, text: &str) -> f32 {
        Expression {
            text: text.as_bytes(),
            at: 0,
            formulas: self,
        }
        .expression()
    }
}

/// A formula, read left to right.
struct Expression<'a, 'f> {
    text: &'a [u8],
    at: usize,
    formulas: &'a Formulas<'f>,
}

impl Expression<'_, '_> {
    fn skip(&mut self) {
        while self.at < self.text.len() && self.text[self.at].is_ascii_whitespace() {
            self.at += 1;
        }
    }

    fn peek(&mut self) -> Option<u8> {
        self.skip();
        self.text.get(self.at).copied()
    }

    fn take(&mut self, byte: u8) -> bool {
        if self.peek() == Some(byte) {
            self.at += 1;
            return true;
        }
        false
    }

    fn expression(&mut self) -> f32 {
        let mut value = self.term();
        loop {
            if self.take(b'+') {
                value += self.term();
            } else if self.take(b'-') {
                value -= self.term();
            } else {
                return value;
            }
        }
    }

    fn term(&mut self) -> f32 {
        let mut value = self.factor();
        loop {
            if self.take(b'*') {
                value *= self.factor();
            } else if self.take(b'/') {
                let divisor = self.factor();
                // A formula dividing by zero is a shape somebody edited by hand.
                // Nought is a point that can be drawn; infinity is not.
                value = if divisor == 0.0 { 0.0 } else { value / divisor };
            } else {
                return value;
            }
        }
    }

    fn factor(&mut self) -> f32 {
        if self.take(b'-') {
            return -self.factor();
        }
        if self.take(b'+') {
            return self.factor();
        }
        if self.take(b'(') {
            let value = self.expression();
            self.take(b')');
            return value;
        }
        if self.take(b'?') {
            let name = self.word();
            return self.formulas.named(&name);
        }
        if self.take(b'$') {
            let index = self.word().parse().unwrap_or(0);
            return self.formulas.modifier(index);
        }
        match self.peek() {
            Some(byte) if byte.is_ascii_alphabetic() => {
                let name = self.word();
                if self.take(b'(') {
                    let arguments = self.arguments();
                    return call(&name, &arguments);
                }
                self.formulas.constant(&name).unwrap_or(0.0)
            }
            _ => self.number(),
        }
    }

    fn arguments(&mut self) -> Vec<f32> {
        let mut arguments = Vec::new();
        if self.take(b')') {
            return arguments;
        }
        loop {
            arguments.push(self.expression());
            if !self.take(b',') {
                self.take(b')');
                return arguments;
            }
        }
    }

    /// A name or a run of digits, whichever is under the cursor.
    fn word(&mut self) -> String {
        self.skip();
        let start = self.at;
        while self
            .text
            .get(self.at)
            .is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'_')
        {
            self.at += 1;
        }
        String::from_utf8_lossy(&self.text[start..self.at]).into_owned()
    }

    fn number(&mut self) -> f32 {
        self.skip();
        let start = self.at;
        while self
            .text
            .get(self.at)
            .is_some_and(|b| b.is_ascii_digit() || *b == b'.')
        {
            self.at += 1;
        }
        if start == self.at {
            // Nothing readable here: step over it so that a malformed formula
            // ends rather than spins.
            self.at += 1;
            return 0.0;
        }
        String::from_utf8_lossy(&self.text[start..self.at])
            .parse()
            .unwrap_or(0.0)
    }
}

fn call(name: &str, arguments: &[f32]) -> f32 {
    let argument = |n: usize| arguments.get(n).copied().unwrap_or(0.0);
    match name {
        "abs" => argument(0).abs(),
        "sqrt" => argument(0).max(0.0).sqrt(),
        // Radians. A formula that means degrees writes the conversion itself,
        // as `sin($0 * (pi/180))`, which is how every one in the corpus does it.
        "sin" => argument(0).sin(),
        "cos" => argument(0).cos(),
        "tan" => argument(0).tan(),
        "atan" => argument(0).atan(),
        "atan2" => argument(0).atan2(argument(1)),
        "min" => argument(0).min(argument(1)),
        "max" => argument(0).max(argument(1)),
        // Greater than nought is true, which is the specification's rule and not
        // the usual one.
        "if" => {
            if argument(0) > 0.0 {
                argument(1)
            } else {
                argument(2)
            }
        }
        _ => 0.0,
    }
}

/// The state of drawing one path: where the pen is and what it has drawn.
struct Pen {
    done: Vec<SubPath>,
    points: Vec<(f32, f32)>,
    closed: bool,
    fill: bool,
    stroke: bool,
}

impl Pen {
    fn new() -> Self {
        Self {
            done: Vec::new(),
            points: Vec::new(),
            closed: false,
            fill: true,
            stroke: true,
        }
    }

    fn at(&self) -> (f32, f32) {
        self.points.last().copied().unwrap_or((0.0, 0.0))
    }

    /// Finish the run of points in hand and begin another.
    fn brk(&mut self) {
        if self.points.len() >= 2 {
            self.done.push(SubPath {
                points: std::mem::take(&mut self.points),
                closed: self.closed,
                fill: self.fill,
                stroke: self.stroke,
            });
        } else {
            self.points.clear();
        }
        self.closed = false;
    }

    fn finish(mut self) -> Vec<SubPath> {
        self.brk();
        self.done
    }

    /// Walk the path, command by command.
    fn run(&mut self, path: &str, formulas: &Formulas<'_>) {
        let mut tokens = Tokens {
            text: path.as_bytes(),
            at: 0,
            formulas,
            pushed: None,
        };
        let mut command = None;
        loop {
            match tokens.next() {
                Some(Token::Command(letter)) => {
                    command = Some(letter);
                    self.command(letter, &mut tokens);
                }
                // A command's arguments may repeat: `L x y x y x y` is three
                // lines, and the letter is written once.
                Some(Token::Number(first)) => match command {
                    Some(letter) => {
                        tokens.pushed = Some(first);
                        self.command(letter, &mut tokens);
                    }
                    None => return,
                },
                None => return,
            }
        }
    }

    fn command(&mut self, letter: char, tokens: &mut Tokens<'_, '_>) {
        match letter {
            'M' => {
                let point = tokens.point();
                self.brk();
                self.points.push(point);
            }
            'L' => {
                let point = tokens.point();
                self.points.push(point);
            }
            'C' => {
                let (a, b, end) = (tokens.point(), tokens.point(), tokens.point());
                self.cubic(a, b, end);
            }
            'Q' => {
                let (control, end) = (tokens.point(), tokens.point());
                // A quadratic is a cubic whose two controls sit two thirds of
                // the way from each end towards the single one.
                let from = self.at();
                let third = |a: f32, b: f32| a + 2.0 / 3.0 * (b - a);
                self.cubic(
                    (third(from.0, control.0), third(from.1, control.1)),
                    (third(end.0, control.0), third(end.1, control.1)),
                    end,
                );
            }
            'Z' => {
                self.closed = true;
                self.brk();
            }
            'N' => self.brk(),
            'F' => self.fill = false,
            'S' => self.stroke = false,
            'T' | 'U' => {
                let (centre, radii) = (tokens.point(), tokens.point());
                let (from, to) = (tokens.number(), tokens.number());
                if letter == 'U' {
                    self.brk();
                }
                self.arc(centre, radii, from, to);
            }
            'X' | 'Y' => {
                let to = tokens.point();
                self.quadrant(to, letter == 'X');
            }
            'A' | 'B' | 'W' | 'V' => {
                let (corner, opposite) = (tokens.point(), tokens.point());
                let (from, to) = (tokens.point(), tokens.point());
                if letter == 'B' || letter == 'V' {
                    self.brk();
                }
                self.box_arc(corner, opposite, from, to, letter == 'W' || letter == 'V');
            }
            _ => {}
        }
    }

    fn cubic(&mut self, a: (f32, f32), b: (f32, f32), end: (f32, f32)) {
        let from = self.at();
        for step in 1..=CURVE_SEGMENTS {
            #[allow(clippy::cast_precision_loss)]
            let t = step as f32 / CURVE_SEGMENTS as f32;
            let u = 1.0 - t;
            let blend = |p0: f32, p1: f32, p2: f32, p3: f32| {
                u * u * u * p0 + 3.0 * u * u * t * p1 + 3.0 * u * t * t * p2 + t * t * t * p3
            };
            self.points.push((
                blend(from.0, a.0, b.0, end.0),
                blend(from.1, a.1, b.1, end.1),
            ));
        }
    }

    /// A run of points along an ellipse, from one angle to another in degrees.
    fn arc(&mut self, centre: (f32, f32), radii: (f32, f32), from: f32, to: f32) {
        // A sweep that would be nothing or negative is the long way round, which
        // is what `0 360` means and what every full ellipse in the corpus says.
        let mut sweep = to - from;
        if sweep <= 0.0 {
            sweep += 360.0;
        }
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let steps = ((sweep / DEGREES_PER_SEGMENT).ceil() as usize).max(2);
        for step in 0..=steps {
            #[allow(clippy::cast_precision_loss)]
            let angle = (from + sweep * step as f32 / steps as f32).to_radians();
            self.points.push((
                centre.0 + radii.0 * angle.cos(),
                centre.1 + radii.1 * angle.sin(),
            ));
        }
    }

    /// A quarter of an ellipse from where the pen is to a point.
    ///
    /// `X` leaves horizontally and arrives vertically, `Y` the other way round.
    /// Between them they are how every rounded corner in ODF is written.
    fn quadrant(&mut self, to: (f32, f32), x_first: bool) {
        let from = self.at();
        let centre = if x_first {
            (to.0, from.1)
        } else {
            (from.0, to.1)
        };
        let radii = ((to.0 - from.0).abs(), (to.1 - from.1).abs());
        if radii.0 == 0.0 || radii.1 == 0.0 {
            self.points.push(to);
            return;
        }
        let angle_of = |p: (f32, f32)| (p.1 - centre.1).atan2(p.0 - centre.0).to_degrees();
        let (start, end) = (angle_of(from), angle_of(to));
        // The quarter that joins the two, taken the short way.
        let mut sweep = end - start;
        while sweep > 180.0 {
            sweep -= 360.0;
        }
        while sweep < -180.0 {
            sweep += 360.0;
        }
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let steps = ((sweep.abs() / DEGREES_PER_SEGMENT).ceil() as usize).max(2);
        for step in 1..=steps {
            #[allow(clippy::cast_precision_loss)]
            let angle = (start + sweep * step as f32 / steps as f32).to_radians();
            self.points.push((
                centre.0 + radii.0 * angle.cos(),
                centre.1 + radii.1 * angle.sin(),
            ));
        }
    }

    /// An arc of the ellipse that fills a box, between two points on it.
    ///
    /// **The sweep direction here is the specification's and not a measurement.**
    /// `V` occurs four times in the twenty-three templates surveyed and `A`, `B`
    /// and `W` occur in none of them, so nothing in the corpus tells these apart.
    fn box_arc(
        &mut self,
        corner: (f32, f32),
        opposite: (f32, f32),
        from: (f32, f32),
        to: (f32, f32),
        clockwise: bool,
    ) {
        let centre = (
            f32::midpoint(corner.0, opposite.0),
            f32::midpoint(corner.1, opposite.1),
        );
        let radii = (
            (opposite.0 - corner.0).abs() / 2.0,
            (opposite.1 - corner.1).abs() / 2.0,
        );
        if radii.0 == 0.0 || radii.1 == 0.0 {
            self.points.push(to);
            return;
        }
        let angle_of = |p: (f32, f32)| {
            ((p.1 - centre.1) / radii.1)
                .atan2((p.0 - centre.0) / radii.0)
                .to_degrees()
        };
        let (start, end) = (angle_of(from), angle_of(to));
        let sweep = if clockwise { start - end } else { end - start };
        let sweep = if sweep <= 0.0 { sweep + 360.0 } else { sweep };
        let (a, b) = if clockwise {
            (start, start - sweep)
        } else {
            (start, start + sweep)
        };
        self.arc_between(centre, radii, a, b);
    }

    fn arc_between(&mut self, centre: (f32, f32), radii: (f32, f32), from: f32, to: f32) {
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let steps = (((to - from).abs() / DEGREES_PER_SEGMENT).ceil() as usize).max(2);
        for step in 0..=steps {
            #[allow(clippy::cast_precision_loss)]
            let angle = (from + (to - from) * step as f32 / steps as f32).to_radians();
            self.points.push((
                centre.0 + radii.0 * angle.cos(),
                centre.1 + radii.1 * angle.sin(),
            ));
        }
    }
}

enum Token {
    Command(char),
    Number(f32),
}

/// The path, read one token at a time, with every reference already resolved.
struct Tokens<'a, 'f> {
    text: &'a [u8],
    at: usize,
    formulas: &'a Formulas<'f>,
    // Set when a repeated argument group put a number back.
    pushed: Option<f32>,
}

impl Tokens<'_, '_> {
    fn next(&mut self) -> Option<Token> {
        if let Some(number) = self.pushed.take() {
            return Some(Token::Number(number));
        }
        while self
            .text
            .get(self.at)
            .is_some_and(|b| b.is_ascii_whitespace() || *b == b',')
        {
            self.at += 1;
        }
        let byte = *self.text.get(self.at)?;
        if byte.is_ascii_alphabetic() {
            self.at += 1;
            return Some(Token::Command(char::from(byte)));
        }
        Some(Token::Number(self.value()))
    }

    /// One number, which may be written as a reference to a formula or to a
    /// modifier rather than as digits.
    fn value(&mut self) -> f32 {
        let mut expression = Expression {
            text: self.text,
            at: self.at,
            formulas: self.formulas,
        };
        // A path's numbers are single values and never arithmetic, so a factor
        // is the whole of what may appear — and `?f7` and `$0` are factors.
        let value = expression.factor();
        self.at = expression.at;
        value
    }

    fn number(&mut self) -> f32 {
        match self.next() {
            Some(Token::Number(number)) => number,
            // A command short of its arguments: nought keeps the pen somewhere
            // rather than ending the shape.
            _ => 0.0,
        }
    }

    fn point(&mut self) -> (f32, f32) {
        (self.number(), self.number())
    }
}