skia-rs-path 0.3.0

Path geometry and operations for skia-rs
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! Path builder for constructing paths.

use crate::{FillType, Path, Verb};
use skia_rs_core::cast::{ceil_to_i32, scalar_from_i32};
use skia_rs_core::{Point, Rect, Scalar};

/// sqrt(2)/2, the conic weight for a 90-degree circular arc.
const OVAL_CONIC_WEIGHT: Scalar = std::f32::consts::FRAC_1_SQRT_2;

/// Cubic-bezier control-point factor approximating a circular arc with a
/// rounded-rect corner radius (Skia's `SK_ScalarRoot2Over2` derived constant).
const ROUND_RECT_KAPPA: Scalar = 0.552_284_8;

/// Builder for constructing paths.
#[derive(Debug, Clone, Default)]
pub struct PathBuilder {
    path: Path,
    last_move: Option<Point>,
}

impl PathBuilder {
    /// Create a new path builder.
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a path builder with specified fill type.
    #[inline]
    #[must_use]
    pub fn with_fill_type(fill_type: FillType) -> Self {
        let mut builder = Self::new();
        builder.path.fill_type = fill_type;
        builder
    }

    /// Set the fill type.
    #[inline]
    pub const fn fill_type(&mut self, fill_type: FillType) -> &mut Self {
        self.path.fill_type = fill_type;
        self
    }

    /// Move to a point.
    ///
    /// Consecutive `move_to` calls overwrite the pending Move point instead of
    /// appending a redundant verb (upstream `SkPathBuilder::moveTo`).
    #[inline]
    pub fn move_to(&mut self, x: Scalar, y: Scalar) -> &mut Self {
        let p = Point::new(x, y);
        if self.path.verbs.last() == Some(&Verb::Move) {
            if let Some(last) = self.path.points.last_mut() {
                *last = p;
            }
        } else {
            self.path.verbs.push(Verb::Move);
            self.path.points.push(p);
        }
        self.last_move = Some(p);
        self.path.bounds = None;
        self
    }

    /// Line to a point.
    #[inline]
    pub fn line_to(&mut self, x: Scalar, y: Scalar) -> &mut Self {
        self.ensure_move();
        self.path.verbs.push(Verb::Line);
        self.path.points.push(Point::new(x, y));
        self.path.bounds = None;
        self
    }

    /// Quadratic bezier curve.
    #[inline]
    pub fn quad_to(&mut self, x1: Scalar, y1: Scalar, x2: Scalar, y2: Scalar) -> &mut Self {
        self.ensure_move();
        self.path.verbs.push(Verb::Quad);
        self.path.points.push(Point::new(x1, y1));
        self.path.points.push(Point::new(x2, y2));
        self.path.bounds = None;
        self
    }

    /// Conic curve (weighted quadratic).
    #[inline]
    pub fn conic_to(
        &mut self,
        x1: Scalar,
        y1: Scalar,
        x2: Scalar,
        y2: Scalar,
        w: Scalar,
    ) -> &mut Self {
        self.ensure_move();
        self.path.verbs.push(Verb::Conic);
        self.path.points.push(Point::new(x1, y1));
        self.path.points.push(Point::new(x2, y2));
        self.path.conic_weights.push(w);
        self.path.bounds = None;
        self
    }

    /// Cubic bezier curve.
    #[inline]
    pub fn cubic_to(
        &mut self,
        x1: Scalar,
        y1: Scalar,
        x2: Scalar,
        y2: Scalar,
        x3: Scalar,
        y3: Scalar,
    ) -> &mut Self {
        self.ensure_move();
        self.path.verbs.push(Verb::Cubic);
        self.path.points.push(Point::new(x1, y1));
        self.path.points.push(Point::new(x2, y2));
        self.path.points.push(Point::new(x3, y3));
        self.path.bounds = None;
        self
    }

    /// Close the current contour.
    ///
    /// A repeated `close()` is a no-op (upstream `SkPathBuilder::close`).
    #[inline]
    pub fn close(&mut self) -> &mut Self {
        if let Some(&last) = self.path.verbs.last() {
            if last != Verb::Close {
                self.path.verbs.push(Verb::Close);
            }
        }
        self
    }

    /// Add a rectangle.
    pub fn add_rect(&mut self, rect: &Rect) -> &mut Self {
        self.move_to(rect.left, rect.top)
            .line_to(rect.right, rect.top)
            .line_to(rect.right, rect.bottom)
            .line_to(rect.left, rect.bottom)
            .close()
    }

    /// Add an oval inscribed in the rectangle.
    ///
    /// Uses four quarter-circle conics of weight √2/2 (upstream
    /// `gFourQuarterCircleConics`), CW starting at the 3-o'clock point.
    pub fn add_oval(&mut self, rect: &Rect) -> &mut Self {
        let cx = f32::midpoint(rect.left, rect.right);
        let cy = f32::midpoint(rect.top, rect.bottom);
        let rx = rect.width() / 2.0;
        let ry = rect.height() / 2.0;
        let w = OVAL_CONIC_WEIGHT;

        self.move_to(cx + rx, cy)
            .conic_to(cx + rx, cy + ry, cx, cy + ry, w)
            .conic_to(cx - rx, cy + ry, cx - rx, cy, w)
            .conic_to(cx - rx, cy - ry, cx, cy - ry, w)
            .conic_to(cx + rx, cy - ry, cx + rx, cy, w)
            .close()
    }

    /// Add a circle.
    pub fn add_circle(&mut self, cx: Scalar, cy: Scalar, radius: Scalar) -> &mut Self {
        self.add_oval(&Rect::new(
            cx - radius,
            cy - radius,
            cx + radius,
            cy + radius,
        ))
    }

    /// Add a rounded rectangle.
    pub fn add_round_rect(&mut self, rect: &Rect, rx: Scalar, ry: Scalar) -> &mut Self {
        if rx <= 0.0 || ry <= 0.0 {
            return self.add_rect(rect);
        }

        let rx = rx.min(rect.width() / 2.0);
        let ry = ry.min(rect.height() / 2.0);

        let kx = rx * ROUND_RECT_KAPPA;
        let ky = ry * ROUND_RECT_KAPPA;

        self.move_to(rect.left + rx, rect.top)
            .line_to(rect.right - rx, rect.top)
            .cubic_to(
                rect.right - rx + kx,
                rect.top,
                rect.right,
                rect.top + ry - ky,
                rect.right,
                rect.top + ry,
            )
            .line_to(rect.right, rect.bottom - ry)
            .cubic_to(
                rect.right,
                rect.bottom - ry + ky,
                rect.right - rx + kx,
                rect.bottom,
                rect.right - rx,
                rect.bottom,
            )
            .line_to(rect.left + rx, rect.bottom)
            .cubic_to(
                rect.left + rx - kx,
                rect.bottom,
                rect.left,
                rect.bottom - ry + ky,
                rect.left,
                rect.bottom - ry,
            )
            .line_to(rect.left, rect.top + ry)
            .cubic_to(
                rect.left,
                rect.top + ry - ky,
                rect.left + rx - kx,
                rect.top,
                rect.left + rx,
                rect.top,
            )
            .close()
    }

    /// Add an arc as a new contour.
    ///
    /// The arc is inscribed in the oval bounded by `oval`, starting at `start_angle`
    /// and sweeping `sweep_angle` degrees. Angles are measured in degrees, with
    /// 0 at the 3 o'clock position, increasing clockwise.
    pub fn add_arc(&mut self, oval: &Rect, start_angle: Scalar, sweep_angle: Scalar) -> &mut Self {
        if oval.is_empty() || sweep_angle == 0.0 {
            return self;
        }

        // For a full sweep we can use the oval shortcut only when the start
        // angle is a multiple of 90 degrees (upstream SkPathBuilder::addArc).
        // Otherwise we honor the start angle by drawing the actual arc. The oval
        // helper only emits CW geometry, so the shortcut is limited to sweep > 0.
        if sweep_angle >= 360.0 || sweep_angle <= -360.0 {
            let start_over_90 = start_angle / 90.0;
            let start_over_90i = start_over_90.round();
            if (start_over_90 - start_over_90i).abs() < 1e-4 && sweep_angle > 0.0 {
                return self.add_oval(oval);
            }
        }

        let cx = oval.center().x;
        let cy = oval.center().y;
        let rx = oval.width() / 2.0;
        let ry = oval.height() / 2.0;

        // Convert to radians
        let start_rad = start_angle.to_radians();
        let sweep_rad = sweep_angle.to_radians();

        // Starting point honors the start angle; direction follows the sweep sign.
        let start_x = cx + rx * start_rad.cos();
        let start_y = cy + ry * start_rad.sin();
        self.move_to(start_x, start_y);

        // Add arc segments (approximate with cubics), no x-axis rotation.
        self.add_arc_to_impl(cx, cy, rx, ry, start_rad, sweep_rad, None);

        self
    }

    /// Arc to a point using radii and rotation.
    ///
    /// This matches the SVG arc command semantics.
    #[allow(
        clippy::too_many_arguments,
        reason = "faithful port of the SVG elliptical-arc command's fixed parameter list (rx, ry, x-axis-rotation, large-arc-flag, sweep-flag, x, y)"
    )]
    pub fn arc_to(
        &mut self,
        rx: Scalar,
        ry: Scalar,
        x_axis_rotate: Scalar,
        large_arc: bool,
        sweep: bool,
        x: Scalar,
        y: Scalar,
    ) -> &mut Self {
        self.ensure_move();

        // Get current point
        let current = self.current_point();
        #[allow(
            clippy::float_cmp,
            reason = "exact degenerate-endpoint check mirrors upstream SkPath::ArcTo's bitwise comparison"
        )]
        if current.x == x && current.y == y {
            return self;
        }

        // Handle degenerate cases
        if rx == 0.0 || ry == 0.0 {
            return self.line_to(x, y);
        }

        // Convert to center parameterization and add cubics
        self.svg_arc_to_cubics(
            current.x,
            current.y,
            rx.abs(),
            ry.abs(),
            x_axis_rotate,
            large_arc,
            sweep,
            x,
            y,
        );

        self
    }

    /// Add relative line.
    pub fn r_line_to(&mut self, dx: Scalar, dy: Scalar) -> &mut Self {
        let current = self.current_point();
        self.line_to(current.x + dx, current.y + dy)
    }

    /// Add relative move.
    pub fn r_move_to(&mut self, dx: Scalar, dy: Scalar) -> &mut Self {
        let current = self.current_point();
        self.move_to(current.x + dx, current.y + dy)
    }

    /// Add relative quadratic.
    pub fn r_quad_to(&mut self, dx1: Scalar, dy1: Scalar, dx2: Scalar, dy2: Scalar) -> &mut Self {
        let current = self.current_point();
        self.quad_to(
            current.x + dx1,
            current.y + dy1,
            current.x + dx2,
            current.y + dy2,
        )
    }

    /// Add relative cubic.
    pub fn r_cubic_to(
        &mut self,
        dx1: Scalar,
        dy1: Scalar,
        dx2: Scalar,
        dy2: Scalar,
        dx3: Scalar,
        dy3: Scalar,
    ) -> &mut Self {
        let current = self.current_point();
        self.cubic_to(
            current.x + dx1,
            current.y + dy1,
            current.x + dx2,
            current.y + dy2,
            current.x + dx3,
            current.y + dy3,
        )
    }

    /// Add a line from the path.
    pub fn add_line(&mut self, p0: Point, p1: Point) -> &mut Self {
        self.move_to(p0.x, p0.y).line_to(p1.x, p1.y)
    }

    /// Add a polygon.
    pub fn add_polygon(&mut self, points: &[Point], close: bool) -> &mut Self {
        if points.is_empty() {
            return self;
        }

        self.move_to(points[0].x, points[0].y);
        for p in &points[1..] {
            self.line_to(p.x, p.y);
        }
        if close {
            self.close();
        }
        self
    }

    /// Add another path to this builder.
    pub fn add_path(&mut self, path: &Path) -> &mut Self {
        for element in path {
            match element {
                crate::PathElement::Move(p) => {
                    self.move_to(p.x, p.y);
                }
                crate::PathElement::Line(p) => {
                    self.line_to(p.x, p.y);
                }
                crate::PathElement::Quad(p1, p2) => {
                    self.quad_to(p1.x, p1.y, p2.x, p2.y);
                }
                crate::PathElement::Conic(p1, p2, w) => {
                    self.conic_to(p1.x, p1.y, p2.x, p2.y, w);
                }
                crate::PathElement::Cubic(p1, p2, p3) => {
                    self.cubic_to(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
                }
                crate::PathElement::Close => {
                    self.close();
                }
            }
        }
        self
    }

    /// Build the path.
    #[inline]
    pub fn build(self) -> Path {
        self.path
    }

    /// Get the current point.
    ///
    /// After a `close()`, the current point returns to the subpath's initial
    /// (last move) point, matching Skia's `case 'Z': c = first`.
    pub fn current_point(&self) -> Point {
        if self.path.verbs.last() == Some(&Verb::Close) {
            if let Some(mv) = self.last_move {
                return mv;
            }
        }
        self.path.points.last().copied().unwrap_or(Point::zero())
    }

    /// Ensure we have an open contour to draw into.
    ///
    /// Ported from `SkPathBuilder::ensureMove`: when the path is empty, start at
    /// the origin; when the last verb is `Close`, inject a `moveTo` at the last
    /// move point so the drawing op starts a new contour there.
    fn ensure_move(&mut self) {
        match self.path.verbs.last() {
            None => {
                self.move_to(0.0, 0.0);
            }
            Some(&Verb::Close) => {
                let mv = self.last_move.unwrap_or(Point::zero());
                self.move_to(mv.x, mv.y);
            }
            _ => {}
        }
    }

    /// Internal helper to add arc segments as cubic beziers.
    ///
    /// The optional `rot` matrix (a rotation about the ellipse center) is
    /// applied to every emitted point, used to realize the SVG arc's
    /// x-axis-rotation.
    #[allow(
        clippy::too_many_arguments,
        reason = "faithful port of Skia's addArcSegments helper geometry parameter list (center, radii, angles, rotation)"
    )]
    fn add_arc_to_impl(
        &mut self,
        cx: Scalar,
        cy: Scalar,
        rx: Scalar,
        ry: Scalar,
        start_angle: Scalar,
        sweep_angle: Scalar,
        rot: Option<&skia_rs_core::Matrix>,
    ) {
        // Break arc into segments of at most 90 degrees
        let num_segments = ceil_to_i32(sweep_angle.abs() / std::f32::consts::FRAC_PI_2).max(1);
        let segment_angle = sweep_angle / scalar_from_i32(num_segments);

        let mut angle = start_angle;
        for _ in 0..num_segments {
            let end_angle = angle + segment_angle;
            self.add_arc_segment(cx, cy, rx, ry, angle, end_angle, rot);
            angle = end_angle;
        }
    }

    /// Add a single arc segment (at most 90 degrees) as a cubic bezier.
    #[allow(
        clippy::too_many_arguments,
        reason = "faithful port of Skia's addArcSegment helper geometry parameter list (center, radii, angles, rotation)"
    )]
    fn add_arc_segment(
        &mut self,
        cx: Scalar,
        cy: Scalar,
        rx: Scalar,
        ry: Scalar,
        start_angle: Scalar,
        end_angle: Scalar,
        rot: Option<&skia_rs_core::Matrix>,
    ) {
        let sweep = end_angle - start_angle;
        // A zero (or degenerate) sweep would divide by zero and produce NaN
        // control points; skip it (upstream early-returns on zero sweep).
        if sweep == 0.0 {
            return;
        }
        let half_sweep = sweep / 2.0;
        let sin_half = half_sweep.sin();
        if sin_half == 0.0 {
            return;
        }

        // Control point distance factor
        let k = (4.0 / 3.0) * (1.0 - half_sweep.cos()) / sin_half;

        let (sin_start, cos_start) = start_angle.sin_cos();
        let (sin_end, cos_end) = end_angle.sin_cos();

        let x0 = rx.mul_add(cos_start, cx);
        let y0 = ry.mul_add(sin_start, cy);
        let mut p1 = Point::new(
            (k * rx).mul_add(-sin_start, x0),
            (k * ry).mul_add(cos_start, y0),
        );
        let x3 = rx.mul_add(cos_end, cx);
        let y3 = ry.mul_add(sin_end, cy);
        let mut p3 = Point::new(x3, y3);
        let mut p2 = Point::new(
            (k * rx).mul_add(sin_end, x3),
            (k * ry).mul_add(-cos_end, y3),
        );

        if let Some(m) = rot {
            p1 = m.map_point(p1);
            p2 = m.map_point(p2);
            p3 = m.map_point(p3);
        }

        self.cubic_to(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y);
    }

    /// Convert SVG arc to cubic bezier segments.
    #[allow(
        clippy::too_many_arguments,
        reason = "faithful port of the W3C SVG arc-to-cubic conversion algorithm's fixed parameter list"
    )]
    fn svg_arc_to_cubics(
        &mut self,
        x1: Scalar,
        y1: Scalar,
        mut rx: Scalar,
        mut ry: Scalar,
        phi: Scalar,
        large_arc: bool,
        sweep: bool,
        x2: Scalar,
        y2: Scalar,
    ) {
        // Based on W3C SVG arc implementation notes
        let phi_rad = phi.to_radians();
        let (sin_phi, cos_phi) = phi_rad.sin_cos();

        // Step 1: Compute (x1', y1')
        let dx = (x1 - x2) / 2.0;
        let dy = (y1 - y2) / 2.0;
        let x1p = cos_phi.mul_add(dx, sin_phi * dy);
        let y1p = (-sin_phi).mul_add(dx, cos_phi * dy);

        // Scale radii if needed
        let lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
        if lambda > 1.0 {
            let sqrt_lambda = lambda.sqrt();
            rx *= sqrt_lambda;
            ry *= sqrt_lambda;
        }

        // Step 2: Compute (cx', cy')
        let rx2 = rx * rx;
        let ry2 = ry * ry;
        let x1p2 = x1p * x1p;
        let y1p2 = y1p * y1p;

        let mut sq = (ry2.mul_add(-x1p2, rx2.mul_add(ry2, -(rx2 * y1p2)))
            / rx2.mul_add(y1p2, ry2 * x1p2))
        .max(0.0);
        sq = sq.sqrt();
        if large_arc == sweep {
            sq = -sq;
        }

        let cxp = sq * rx * y1p / ry;
        let cyp = -sq * ry * x1p / rx;

        // Step 3: Compute (cx, cy)
        let cx = cos_phi.mul_add(cxp, -(sin_phi * cyp)) + f32::midpoint(x1, x2);
        let cy = sin_phi.mul_add(cxp, cos_phi * cyp) + f32::midpoint(y1, y2);

        // Step 4: Compute angles
        let theta1 = angle_between(1.0, 0.0, (x1p - cxp) / rx, (y1p - cyp) / ry);
        let mut dtheta = angle_between(
            (x1p - cxp) / rx,
            (y1p - cyp) / ry,
            (-x1p - cxp) / rx,
            (-y1p - cyp) / ry,
        );

        if !sweep && dtheta > 0.0 {
            dtheta -= std::f32::consts::TAU;
        } else if sweep && dtheta < 0.0 {
            dtheta += std::f32::consts::TAU;
        }

        // The center parameterization above is computed in the ellipse-aligned
        // frame; the emitted geometry must be rotated by phi about the center so
        // it lies on the actual x-axis-rotated ellipse (SVG 2 B.2.3). Rotating
        // the control points is an exact affine map of the axis-aligned arc.
        let rot = skia_rs_core::Matrix::rotate_around(phi_rad, Point::new(cx, cy));
        self.add_arc_to_impl(cx, cy, rx, ry, theta1, dtheta, Some(&rot));
    }
}

/// Compute angle between two vectors.
fn angle_between(ux: Scalar, uy: Scalar, vx: Scalar, vy: Scalar) -> Scalar {
    let n = ux.hypot(uy) * vx.hypot(vy);
    if n == 0.0 {
        return 0.0;
    }
    let c = ux.mul_add(vx, uy * vy) / n;
    let s = ux.mul_add(vy, -(uy * vx));
    s.atan2(c.clamp(-1.0, 1.0))
}

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

    #[test]
    fn test_line_after_close_injects_move() {
        // After close(), a line_to must start a new contour at the last move point.
        let mut b = PathBuilder::new();
        b.move_to(0.0, 0.0);
        b.line_to(10.0, 0.0);
        b.close();
        b.line_to(20.0, 20.0);
        let path = b.build();
        assert_eq!(
            path.verbs(),
            &[Verb::Move, Verb::Line, Verb::Close, Verb::Move, Verb::Line]
        );
        // The injected move returns to the subpath initial point (0,0).
        let move_idx = 2; // point index of the injected Move
        assert_eq!(path.points()[move_idx], Point::new(0.0, 0.0));
    }

    #[test]
    fn test_repeated_close_is_noop() {
        let mut b = PathBuilder::new();
        b.move_to(0.0, 0.0);
        b.line_to(10.0, 0.0);
        b.close();
        b.close();
        b.close();
        let path = b.build();
        assert_eq!(
            path.verbs().iter().filter(|v| **v == Verb::Close).count(),
            1
        );
    }

    #[test]
    fn test_consecutive_move_to_overwrites() {
        let mut b = PathBuilder::new();
        b.move_to(0.0, 0.0);
        b.move_to(5.0, 5.0);
        b.line_to(10.0, 10.0);
        let path = b.build();
        assert_eq!(path.verbs(), &[Verb::Move, Verb::Line]);
        assert_eq!(path.points()[0], Point::new(5.0, 5.0));
    }

    #[test]
    fn test_current_point_after_close_is_subpath_start() {
        let mut b = PathBuilder::new();
        b.move_to(3.0, 4.0);
        b.line_to(10.0, 0.0);
        b.close();
        assert_eq!(b.current_point(), Point::new(3.0, 4.0));
    }

    #[test]
    fn test_add_oval_uses_conics() {
        let mut b = PathBuilder::new();
        b.add_oval(&Rect::new(0.0, 0.0, 100.0, 50.0));
        let path = b.build();
        assert_eq!(
            path.verbs(),
            &[
                Verb::Move,
                Verb::Conic,
                Verb::Conic,
                Verb::Conic,
                Verb::Conic,
                Verb::Close
            ]
        );
        assert!(path.is_oval());
    }

    #[test]
    fn test_add_arc_zero_sweep_is_finite() {
        let mut b = PathBuilder::new();
        b.add_arc(&Rect::new(0.0, 0.0, 100.0, 100.0), 0.0, 0.0);
        let path = b.build();
        // Zero sweep must not produce NaN control points.
        for p in path.points() {
            assert!(
                p.x.is_finite() && p.y.is_finite(),
                "arc produced non-finite point"
            );
        }
    }

    #[test]
    fn test_add_arc_full_circle_honors_start_angle() {
        // Full circle starting at 45 degrees: start point must honor startAngle,
        // not snap to the 3-o'clock oval start.
        let oval = Rect::new(0.0, 0.0, 100.0, 100.0);
        let mut b = PathBuilder::new();
        b.add_arc(&oval, 45.0, 360.0);
        let path = b.build();
        let start = path.points()[0];
        let (cx, cy) = (50.0, 50.0);
        let r = 50.0;
        let expected = Point::new(
            cx + r * 45.0_f32.to_radians().cos(),
            cy + r * 45.0_f32.to_radians().sin(),
        );
        assert!(
            (start.x - expected.x).abs() < 0.5 && (start.y - expected.y).abs() < 0.5,
            "full-circle arc start {start:?} should honor 45deg start {expected:?}"
        );
    }

    #[test]
    fn test_svg_arc_rotation_endpoint_lands_on_target() {
        // A rotated elliptical arc must land exactly on the target endpoint.
        let mut b = PathBuilder::new();
        b.move_to(0.0, 0.0);
        b.arc_to(50.0, 20.0, 45.0, false, true, 40.0, 30.0);
        let path = b.build();
        let last = path.last_point().unwrap();
        assert!(
            (last.x - 40.0).abs() < 0.1 && (last.y - 30.0).abs() < 0.1,
            "rotated arc endpoint {last:?} must land on target (40, 30)"
        );
    }

    #[test]
    fn test_svg_arc_rotation_not_axis_aligned() {
        // With x-axis-rotation, intermediate geometry must differ from an
        // axis-aligned ellipse arc (the rotation must actually be applied).
        let mut rotated = PathBuilder::new();
        rotated.move_to(0.0, 0.0);
        rotated.arc_to(50.0, 20.0, 60.0, false, true, 40.0, 30.0);
        let rp = rotated.build();

        let mut plain = PathBuilder::new();
        plain.move_to(0.0, 0.0);
        plain.arc_to(50.0, 20.0, 0.0, false, true, 40.0, 30.0);
        let pp = plain.build();

        // Some interior control point must differ noticeably.
        let differ = rp
            .points()
            .iter()
            .zip(pp.points().iter())
            .any(|(a, b)| (a.x - b.x).abs() > 1.0 || (a.y - b.y).abs() > 1.0);
        assert!(differ, "rotation must change the emitted geometry");
    }
}