1use crate::geometry::{Point, Rect, Transform2D};
5use crate::paint::FillRule;
6use teksilo_tokens::CornerRadius;
7
8#[derive(Debug, Clone, Copy, PartialEq)]
10pub enum PathCommand {
11 MoveTo(Point),
12 LineTo(Point),
13 QuadTo {
14 control: Point,
15 to: Point,
16 },
17 CubicTo {
18 control1: Point,
19 control2: Point,
20 to: Point,
21 },
22 ArcTo {
23 rect: Rect,
24 start_angle: f32,
25 sweep_angle: f32,
26 },
27 Close,
28}
29
30const STAMP_SEED: u64 = 0x2545_F491_4F6C_DD1D;
36
37#[inline]
49const fn fold(state: u64, word: u64) -> u64 {
50 let mut x = state ^ word.wrapping_mul(0x9E37_79B9_7F4A_7C15);
51 x ^= x >> 30;
52 x = x.wrapping_mul(0xBF58_476D_1CE4_E5B9);
53 x ^= x >> 27;
54 x = x.wrapping_mul(0x94D0_49BB_1331_11EB);
55 x ^ (x >> 31)
56}
57
58#[inline]
59fn fold_f32(state: u64, v: f32) -> u64 {
60 fold(state, v.to_bits() as u64)
64}
65
66#[inline]
67fn fold_point(state: u64, p: Point) -> u64 {
68 fold_f32(fold_f32(state, p.x), p.y)
69}
70
71fn fold_command(state: u64, cmd: &PathCommand) -> u64 {
73 match *cmd {
74 PathCommand::MoveTo(p) => fold_point(fold(state, 1), p),
75 PathCommand::LineTo(p) => fold_point(fold(state, 2), p),
76 PathCommand::QuadTo { control, to } => fold_point(fold_point(fold(state, 3), control), to),
77 PathCommand::CubicTo {
78 control1,
79 control2,
80 to,
81 } => fold_point(
82 fold_point(fold_point(fold(state, 4), control1), control2),
83 to,
84 ),
85 PathCommand::ArcTo {
86 rect,
87 start_angle,
88 sweep_angle,
89 } => {
90 let s = fold(state, 5);
91 let s = fold_f32(
92 fold_f32(fold_f32(fold_f32(s, rect.x), rect.y), rect.width),
93 rect.height,
94 );
95 fold_f32(fold_f32(s, start_angle), sweep_angle)
96 }
97 PathCommand::Close => fold(state, 6),
98 }
99}
100
101#[derive(Debug, Clone)]
122pub struct Path {
123 commands: Vec<PathCommand>,
124 stamp: u64,
125}
126
127impl Default for Path {
128 fn default() -> Self {
129 Self {
130 commands: Vec::new(),
131 stamp: STAMP_SEED,
132 }
133 }
134}
135
136impl PartialEq for Path {
137 fn eq(&self, other: &Self) -> bool {
140 self.commands == other.commands
141 }
142}
143
144impl Path {
145 pub fn new() -> Self {
146 Self::default()
147 }
148
149 pub fn from_commands(commands: Vec<PathCommand>) -> Self {
154 let stamp = commands.iter().fold(STAMP_SEED, fold_command);
155 Self { commands, stamp }
156 }
157
158 #[inline]
160 pub fn commands(&self) -> &[PathCommand] {
161 &self.commands
162 }
163
164 #[inline]
175 pub fn stamp(&self) -> u64 {
176 self.stamp
177 }
178
179 pub fn push(&mut self, cmd: PathCommand) -> &mut Self {
181 self.stamp = fold_command(self.stamp, &cmd);
182 self.commands.push(cmd);
183 self
184 }
185
186 pub fn clear(&mut self) -> &mut Self {
188 self.commands.clear();
189 self.stamp = STAMP_SEED;
190 self
191 }
192
193 pub fn move_to(&mut self, p: Point) -> &mut Self {
194 self.push(PathCommand::MoveTo(p))
195 }
196
197 pub fn line_to(&mut self, p: Point) -> &mut Self {
198 self.push(PathCommand::LineTo(p))
199 }
200
201 pub fn quad_to(&mut self, control: Point, to: Point) -> &mut Self {
202 self.push(PathCommand::QuadTo { control, to })
203 }
204
205 pub fn cubic_to(&mut self, control1: Point, control2: Point, to: Point) -> &mut Self {
206 self.push(PathCommand::CubicTo {
207 control1,
208 control2,
209 to,
210 })
211 }
212
213 pub fn arc_to(&mut self, rect: Rect, start_angle: f32, sweep_angle: f32) -> &mut Self {
214 self.push(PathCommand::ArcTo {
215 rect,
216 start_angle,
217 sweep_angle,
218 })
219 }
220
221 pub fn close(&mut self) -> &mut Self {
222 self.push(PathCommand::Close)
223 }
224
225 pub fn is_empty(&self) -> bool {
226 self.commands.is_empty()
227 }
228
229 pub fn bounds(&self) -> Rect {
233 let mut min_x = f32::MAX;
234 let mut min_y = f32::MAX;
235 let mut max_x = f32::MIN;
236 let mut max_y = f32::MIN;
237
238 let mut include = |p: Point| {
239 min_x = min_x.min(p.x);
240 min_y = min_y.min(p.y);
241 max_x = max_x.max(p.x);
242 max_y = max_y.max(p.y);
243 };
244
245 for cmd in &self.commands {
246 match *cmd {
247 PathCommand::MoveTo(p) | PathCommand::LineTo(p) => include(p),
248 PathCommand::QuadTo { control, to } => {
249 include(control);
250 include(to);
251 }
252 PathCommand::CubicTo {
253 control1,
254 control2,
255 to,
256 } => {
257 include(control1);
258 include(control2);
259 include(to);
260 }
261 PathCommand::ArcTo { rect, .. } => {
262 include(Point::new(rect.x, rect.y));
263 include(Point::new(rect.right(), rect.bottom()));
264 }
265 PathCommand::Close => {}
266 }
267 }
268
269 if min_x > max_x {
270 return Rect::ZERO;
271 }
272 Rect::new(min_x, min_y, max_x - min_x, max_y - min_y)
273 }
274
275 pub fn circle(center: Point, radius: f32) -> Self {
277 let rect = Rect::new(
278 center.x - radius,
279 center.y - radius,
280 radius * 2.0,
281 radius * 2.0,
282 );
283 let mut path = Self::new();
284 path.move_to(Point::new(center.x + radius, center.y));
288 path.arc_to(rect, 0.0, 360.0);
289 path.close();
290 path
291 }
292
293 pub fn rounded_rect(rect: Rect, radii: CornerRadius) -> Self {
295 let [tl, tr, br, bl] = radii.to_array();
296 let mut path = Self::new();
297
298 path.move_to(Point::new(rect.x + tl, rect.y));
300
301 path.line_to(Point::new(rect.right() - tr, rect.y));
303 if tr > 0.0 {
304 let arc_rect = Rect::new(rect.right() - tr * 2.0, rect.y, tr * 2.0, tr * 2.0);
305 path.arc_to(arc_rect, -90.0, 90.0);
306 }
307
308 path.line_to(Point::new(rect.right(), rect.bottom() - br));
310 if br > 0.0 {
311 let arc_rect = Rect::new(
312 rect.right() - br * 2.0,
313 rect.bottom() - br * 2.0,
314 br * 2.0,
315 br * 2.0,
316 );
317 path.arc_to(arc_rect, 0.0, 90.0);
318 }
319
320 path.line_to(Point::new(rect.x + bl, rect.bottom()));
322 if bl > 0.0 {
323 let arc_rect = Rect::new(rect.x, rect.bottom() - bl * 2.0, bl * 2.0, bl * 2.0);
324 path.arc_to(arc_rect, 90.0, 90.0);
325 }
326
327 path.line_to(Point::new(rect.x, rect.y + tl));
329 if tl > 0.0 {
330 let arc_rect = Rect::new(rect.x, rect.y, tl * 2.0, tl * 2.0);
331 path.arc_to(arc_rect, 180.0, 90.0);
332 }
333
334 path.close();
335 path
336 }
337
338 pub fn star(center: Point, outer_radius: f32, inner_radius: f32, points: u32) -> Self {
340 let mut path = Self::new();
341 let total = points * 2;
342 for i in 0..total {
343 let angle =
344 (i as f32) * std::f32::consts::PI / points as f32 - std::f32::consts::FRAC_PI_2;
345 let r = if i % 2 == 0 {
346 outer_radius
347 } else {
348 inner_radius
349 };
350 let p = Point::new(center.x + r * angle.cos(), center.y + r * angle.sin());
351 if i == 0 {
352 path.move_to(p);
353 } else {
354 path.line_to(p);
355 }
356 }
357 path.close();
358 path
359 }
360
361 pub fn rect(rect: Rect) -> Self {
363 let mut path = Self::new();
364 path.move_to(Point::new(rect.x, rect.y));
365 path.line_to(Point::new(rect.right(), rect.y));
366 path.line_to(Point::new(rect.right(), rect.bottom()));
367 path.line_to(Point::new(rect.x, rect.bottom()));
368 path.close();
369 path
370 }
371
372 pub fn line(from: Point, to: Point) -> Self {
374 let mut path = Self::new();
375 path.move_to(from);
376 path.line_to(to);
377 path
378 }
379
380 pub fn ellipse(rect: Rect) -> Self {
382 const KAPPA: f32 = 0.552_284_8;
385 let cx = rect.x + rect.width / 2.0;
386 let cy = rect.y + rect.height / 2.0;
387 let rx = rect.width / 2.0;
388 let ry = rect.height / 2.0;
389 let kx = rx * KAPPA;
390 let ky = ry * KAPPA;
391
392 let mut path = Self::new();
393 path.move_to(Point::new(cx, cy - ry));
395 path.cubic_to(
397 Point::new(cx + kx, cy - ry),
398 Point::new(cx + rx, cy - ky),
399 Point::new(cx + rx, cy),
400 );
401 path.cubic_to(
403 Point::new(cx + rx, cy + ky),
404 Point::new(cx + kx, cy + ry),
405 Point::new(cx, cy + ry),
406 );
407 path.cubic_to(
409 Point::new(cx - kx, cy + ry),
410 Point::new(cx - rx, cy + ky),
411 Point::new(cx - rx, cy),
412 );
413 path.cubic_to(
415 Point::new(cx - rx, cy - ky),
416 Point::new(cx - kx, cy - ry),
417 Point::new(cx, cy - ry),
418 );
419 path.close();
420 path
421 }
422
423 pub fn append(&mut self, other: &Path) {
425 for cmd in &other.commands {
426 self.push(*cmd);
427 }
428 }
429
430 pub fn transformed(&self, transform: &Transform2D) -> Path {
448 let arcs_survive = arc_transform_is_exact(transform);
449 let mut result = Path::new();
450 for cmd in &self.commands {
451 match *cmd {
452 PathCommand::MoveTo(p) => {
453 result.move_to(transform.apply_point(p));
454 }
455 PathCommand::LineTo(p) => {
456 result.line_to(transform.apply_point(p));
457 }
458 PathCommand::QuadTo { control, to } => {
459 result.quad_to(transform.apply_point(control), transform.apply_point(to));
460 }
461 PathCommand::CubicTo {
462 control1,
463 control2,
464 to,
465 } => {
466 result.cubic_to(
467 transform.apply_point(control1),
468 transform.apply_point(control2),
469 transform.apply_point(to),
470 );
471 }
472 PathCommand::ArcTo {
473 rect,
474 start_angle,
475 sweep_angle,
476 } => {
477 if arcs_survive {
478 result.arc_to(transform.apply_rect(rect), start_angle, sweep_angle);
479 } else {
480 for seg in arc_to_cubics(rect, start_angle, sweep_angle) {
481 let from = transform.apply_point(seg.from);
482 match result.commands.last() {
483 Some(PathCommand::MoveTo(p) | PathCommand::LineTo(p))
486 if *p == from => {}
487 Some(
488 PathCommand::QuadTo { to, .. }
489 | PathCommand::CubicTo { to, .. },
490 ) if *to == from => {}
491 None | Some(PathCommand::Close) => {
506 result.move_to(from);
507 }
508 Some(_) => {
512 result.line_to(from);
513 }
514 }
515 result.cubic_to(
516 transform.apply_point(seg.control1),
517 transform.apply_point(seg.control2),
518 transform.apply_point(seg.to),
519 );
520 }
521 }
522 }
523 PathCommand::Close => {
524 result.close();
525 }
526 }
527 }
528 result
529 }
530
531 pub fn polygon(points: &[Point]) -> Self {
533 let mut path = Self::new();
534 if let Some((&first, rest)) = points.split_first() {
535 path.move_to(first);
536 for &p in rest {
537 path.line_to(p);
538 }
539 path.close();
540 }
541 path
542 }
543
544 pub fn flatten(&self, tolerance: f32) -> Vec<Subpath> {
556 let mut out: Vec<Subpath> = Vec::new();
557 let mut current: Vec<Point> = Vec::new();
558 let mut closed = false;
559 let mut cursor = Point::ZERO;
560
561 for cmd in &self.commands {
562 match *cmd {
563 PathCommand::MoveTo(p) => {
564 flush_subpath(&mut out, &mut current, &mut closed);
565 current.push(p);
566 cursor = p;
567 }
568 PathCommand::LineTo(p) => {
569 if current.is_empty() {
570 current.push(cursor);
571 }
572 current.push(p);
573 cursor = p;
574 }
575 PathCommand::QuadTo { control, to } => {
576 if current.is_empty() {
577 current.push(cursor);
578 }
579 push_quad(&mut current, cursor, control, to, tolerance);
580 cursor = to;
581 }
582 PathCommand::CubicTo {
583 control1,
584 control2,
585 to,
586 } => {
587 if current.is_empty() {
588 current.push(cursor);
589 }
590 push_cubic(&mut current, cursor, control1, control2, to, tolerance);
591 cursor = to;
592 }
593 PathCommand::ArcTo {
594 rect,
595 start_angle,
596 sweep_angle,
597 } => {
598 for seg in arc_to_cubics(rect, start_angle, sweep_angle) {
599 if current.is_empty() {
600 current.push(seg.from);
601 } else if current[current.len() - 1] != seg.from {
602 current.push(seg.from);
606 }
607 push_cubic(
608 &mut current,
609 seg.from,
610 seg.control1,
611 seg.control2,
612 seg.to,
613 tolerance,
614 );
615 cursor = seg.to;
616 }
617 }
618 PathCommand::Close => {
619 if !current.is_empty() {
620 closed = true;
621 let start = current[0];
622 flush_subpath(&mut out, &mut current, &mut closed);
623 cursor = start;
624 }
625 }
626 }
627 }
628 flush_subpath(&mut out, &mut current, &mut closed);
629 out
630 }
631
632 pub fn exact_bounds(&self, tolerance: f32) -> Rect {
639 let subpaths = self.flatten(tolerance);
640 let mut acc: Option<Rect> = None;
641 for sp in &subpaths {
642 if sp.points.is_empty() {
643 continue;
644 }
645 let b = sp.bounds();
646 acc = Some(match acc {
647 None => b,
648 Some(a) => {
649 let x = a.x.min(b.x);
650 let y = a.y.min(b.y);
651 Rect::new(
652 x,
653 y,
654 a.right().max(b.right()) - x,
655 a.bottom().max(b.bottom()) - y,
656 )
657 }
658 });
659 }
660 acc.unwrap_or(Rect::ZERO)
661 }
662
663 pub fn contains_point(&self, p: Point, rule: FillRule, tolerance: f32) -> bool {
673 subpaths_contain_point(&self.flatten(tolerance), p, rule)
674 }
675}
676
677fn flush_subpath(out: &mut Vec<Subpath>, current: &mut Vec<Point>, closed: &mut bool) {
680 if !current.is_empty() {
681 out.push(Subpath {
682 points: std::mem::take(current),
683 closed: *closed,
684 });
685 }
686 *closed = false;
687}
688
689#[derive(Debug, Clone, Copy, PartialEq)]
695pub struct ArcCubic {
696 pub from: Point,
697 pub control1: Point,
698 pub control2: Point,
699 pub to: Point,
700}
701
702pub fn arc_transform_is_exact(transform: &Transform2D) -> bool {
714 const EPS: f32 = 1e-5;
715 let [a, b, c, d, _, _] = transform.m;
716 b.abs() < EPS && c.abs() < EPS && a > 0.0 && d > 0.0
717}
718
719pub fn arc_to_cubics(rect: Rect, start_angle: f32, sweep_angle: f32) -> Vec<ArcCubic> {
732 let rx = rect.width * 0.5;
733 let ry = rect.height * 0.5;
734 let center_x = rect.x + rx;
735 let center_y = rect.y + ry;
736
737 let mut out = Vec::new();
738 let mut remaining = sweep_angle.to_radians();
739 let mut angle = start_angle.to_radians();
740 if !remaining.is_finite() || !angle.is_finite() {
741 return out;
742 }
743 let sign = if remaining >= 0.0 { 1.0 } else { -1.0 };
744
745 while remaining.abs() > 0.001 {
746 let chunk = sign * remaining.abs().min(std::f32::consts::FRAC_PI_2);
747 let half = chunk * 0.5;
748 let k = (4.0 / 3.0) * (1.0 - half.cos()) / half.sin();
749
750 let cos_a = angle.cos();
751 let sin_a = angle.sin();
752 let cos_b = (angle + chunk).cos();
753 let sin_b = (angle + chunk).sin();
754
755 out.push(ArcCubic {
756 from: Point::new(center_x + rx * cos_a, center_y + ry * sin_a),
757 control1: Point::new(
758 center_x + rx * (cos_a - k * sin_a),
759 center_y + ry * (sin_a + k * cos_a),
760 ),
761 control2: Point::new(
762 center_x + rx * (cos_b + k * sin_b),
763 center_y + ry * (sin_b - k * cos_b),
764 ),
765 to: Point::new(center_x + rx * cos_b, center_y + ry * sin_b),
766 });
767
768 angle += chunk;
769 remaining -= chunk;
770 }
771 out
772}
773
774#[derive(Debug, Clone, PartialEq)]
780pub struct Subpath {
781 pub points: Vec<Point>,
784 pub closed: bool,
788}
789
790impl Subpath {
791 pub fn bounds(&self) -> Rect {
797 bounds_of_points(&self.points)
798 }
799
800 pub fn is_empty(&self) -> bool {
802 self.points.is_empty()
803 }
804
805 pub fn segments(&self, close_implicitly: bool) -> impl Iterator<Item = (Point, Point)> + '_ {
809 let n = self.points.len();
810 let wrap = (self.closed || close_implicitly) && n > 2;
811 (0..n.saturating_sub(1))
812 .map(move |i| (self.points[i], self.points[i + 1]))
813 .chain(wrap.then(|| (self.points[n - 1], self.points[0])))
814 }
815}
816
817fn bounds_of_points(points: &[Point]) -> Rect {
818 let mut min_x = f32::MAX;
819 let mut min_y = f32::MAX;
820 let mut max_x = f32::MIN;
821 let mut max_y = f32::MIN;
822 for p in points {
823 min_x = min_x.min(p.x);
824 min_y = min_y.min(p.y);
825 max_x = max_x.max(p.x);
826 max_y = max_y.max(p.y);
827 }
828 if min_x > max_x {
829 return Rect::ZERO;
830 }
831 Rect::new(min_x, min_y, max_x - min_x, max_y - min_y)
832}
833
834const MAX_CURVE_SEGMENTS: u32 = 64;
839
840fn quad_segment_count(p0: Point, c: Point, p1: Point, tolerance: f32) -> u32 {
841 let dx = p0.x - 2.0 * c.x + p1.x;
844 let dy = p0.y - 2.0 * c.y + p1.y;
845 let d = (dx * dx + dy * dy).sqrt();
846 segment_count(d / 8.0, tolerance)
847}
848
849fn cubic_segment_count(p0: Point, c1: Point, c2: Point, p1: Point, tolerance: f32) -> u32 {
850 let d1 = ((p0.x - 2.0 * c1.x + c2.x).powi(2) + (p0.y - 2.0 * c1.y + c2.y).powi(2)).sqrt();
853 let d2 = ((c1.x - 2.0 * c2.x + p1.x).powi(2) + (c1.y - 2.0 * c2.y + p1.y).powi(2)).sqrt();
854 segment_count(0.75 * d1.max(d2), tolerance)
855}
856
857fn segment_count(numerator: f32, tolerance: f32) -> u32 {
858 let tol = tolerance.max(1e-4);
859 if !numerator.is_finite() || numerator <= 0.0 {
860 return 1;
861 }
862 let n = (numerator / tol).sqrt().ceil();
863 if !n.is_finite() {
864 return MAX_CURVE_SEGMENTS;
865 }
866 (n as u32).clamp(1, MAX_CURVE_SEGMENTS)
867}
868
869fn push_quad(out: &mut Vec<Point>, p0: Point, c: Point, p1: Point, tolerance: f32) {
870 let n = quad_segment_count(p0, c, p1, tolerance);
871 for i in 1..=n {
872 let t = i as f32 / n as f32;
873 let mt = 1.0 - t;
874 out.push(Point::new(
875 mt * mt * p0.x + 2.0 * mt * t * c.x + t * t * p1.x,
876 mt * mt * p0.y + 2.0 * mt * t * c.y + t * t * p1.y,
877 ));
878 }
879}
880
881fn push_cubic(out: &mut Vec<Point>, p0: Point, c1: Point, c2: Point, p1: Point, tolerance: f32) {
882 let n = cubic_segment_count(p0, c1, c2, p1, tolerance);
883 for i in 1..=n {
884 let t = i as f32 / n as f32;
885 let mt = 1.0 - t;
886 let a = mt * mt * mt;
887 let b = 3.0 * mt * mt * t;
888 let c = 3.0 * mt * t * t;
889 let d = t * t * t;
890 out.push(Point::new(
891 a * p0.x + b * c1.x + c * c2.x + d * p1.x,
892 a * p0.y + b * c1.y + c * c2.y + d * p1.y,
893 ));
894 }
895}
896
897pub fn subpaths_contain_point(subpaths: &[Subpath], p: Point, rule: FillRule) -> bool {
902 let mut winding: i32 = 0;
903 let mut crossings: u32 = 0;
904 for sp in subpaths {
905 if sp.points.len() < 3 {
909 continue;
910 }
911 for (a, b) in sp.segments(true) {
912 if (a.y <= p.y) == (b.y <= p.y) {
914 continue;
915 }
916 let t = (p.y - a.y) / (b.y - a.y);
917 let x = a.x + t * (b.x - a.x);
918 if x > p.x {
919 crossings += 1;
920 winding += if b.y > a.y { 1 } else { -1 };
921 }
922 }
923 }
924 match rule {
925 FillRule::Winding => winding != 0,
926 FillRule::EvenOdd => crossings % 2 == 1,
927 }
928}
929
930#[cfg(test)]
931mod tests {
932 use super::*;
933
934 #[test]
939 fn flatten_polyline_is_verbatim() {
940 let mut path = Path::new();
941 path.move_to(Point::new(0.0, 0.0))
942 .line_to(Point::new(10.0, 0.0))
943 .line_to(Point::new(10.0, 10.0));
944 let sp = path.flatten(0.25);
945 assert_eq!(sp.len(), 1);
946 assert_eq!(sp[0].points.len(), 3);
947 assert!(!sp[0].closed);
948 }
949
950 #[test]
951 fn flatten_close_marks_subpath_and_does_not_duplicate_start() {
952 let p = Path::rect(Rect::new(0.0, 0.0, 10.0, 10.0));
953 let sp = p.flatten(0.25);
954 assert_eq!(sp.len(), 1);
955 assert!(sp[0].closed);
956 assert_eq!(sp[0].points.len(), 4);
958 }
959
960 #[test]
961 fn flatten_splits_on_each_moveto() {
962 let mut path = Path::new();
963 path.move_to(Point::new(0.0, 0.0))
964 .line_to(Point::new(10.0, 0.0));
965 path.move_to(Point::new(20.0, 20.0))
966 .line_to(Point::new(30.0, 20.0));
967 assert_eq!(path.flatten(0.25).len(), 2);
968 }
969
970 #[test]
971 fn flatten_quad_tracks_the_true_curve() {
972 let mut path = Path::new();
975 path.move_to(Point::new(0.0, 0.0))
976 .quad_to(Point::new(50.0, 100.0), Point::new(100.0, 0.0));
977 let sp = path.flatten(0.25);
978 let max_y = sp[0].points.iter().fold(f32::MIN, |m, p| m.max(p.y));
979 assert!(
980 (max_y - 50.0).abs() < 1.0,
981 "apex should be ~50, got {max_y}"
982 );
983 }
984
985 #[test]
986 fn exact_bounds_beats_control_point_hull() {
987 let mut path = Path::new();
988 path.move_to(Point::new(0.0, 0.0))
989 .quad_to(Point::new(50.0, 100.0), Point::new(100.0, 0.0));
990 assert_eq!(path.bounds().height, 100.0, "hull over-reports");
991 let exact = path.exact_bounds(0.25);
992 assert!(
993 (exact.height - 50.0).abs() < 1.0,
994 "exact bounds should be ~50 tall, got {}",
995 exact.height
996 );
997 assert!((exact.width - 100.0).abs() < 0.01);
998 }
999
1000 #[test]
1001 fn exact_bounds_of_empty_path_is_zero() {
1002 assert_eq!(Path::new().exact_bounds(0.25), Rect::ZERO);
1003 }
1004
1005 #[test]
1006 fn contains_point_square() {
1007 let p = Path::rect(Rect::new(0.0, 0.0, 100.0, 100.0));
1008 assert!(p.contains_point(Point::new(50.0, 50.0), FillRule::Winding, 0.25));
1009 assert!(!p.contains_point(Point::new(150.0, 50.0), FillRule::Winding, 0.25));
1010 assert!(!p.contains_point(Point::new(50.0, -1.0), FillRule::Winding, 0.25));
1011 }
1012
1013 #[test]
1014 fn contains_point_even_odd_ring_has_a_hole() {
1015 let mut p = Path::new();
1018 p.move_to(Point::new(0.0, 0.0))
1019 .line_to(Point::new(100.0, 0.0))
1020 .line_to(Point::new(100.0, 100.0))
1021 .line_to(Point::new(0.0, 100.0))
1022 .close();
1023 p.move_to(Point::new(25.0, 25.0))
1024 .line_to(Point::new(75.0, 25.0))
1025 .line_to(Point::new(75.0, 75.0))
1026 .line_to(Point::new(25.0, 75.0))
1027 .close();
1028
1029 let centre = Point::new(50.0, 50.0);
1030 let band = Point::new(10.0, 50.0);
1031 assert!(p.contains_point(centre, FillRule::Winding, 0.25));
1032 assert!(!p.contains_point(centre, FillRule::EvenOdd, 0.25));
1033 assert!(p.contains_point(band, FillRule::Winding, 0.25));
1034 assert!(p.contains_point(band, FillRule::EvenOdd, 0.25));
1035 }
1036
1037 #[test]
1038 fn contains_point_open_subpath_fills_as_closed() {
1039 let mut p = Path::new();
1041 p.move_to(Point::new(0.0, 0.0))
1042 .line_to(Point::new(100.0, 0.0))
1043 .line_to(Point::new(100.0, 100.0))
1044 .line_to(Point::new(0.0, 100.0));
1045 assert!(p.contains_point(Point::new(50.0, 50.0), FillRule::Winding, 0.25));
1046 }
1047
1048 #[test]
1049 fn contains_point_ignores_a_degenerate_subpath() {
1050 let p = Path::line(Point::new(0.0, 0.0), Point::new(100.0, 100.0));
1054 assert!(!p.contains_point(Point::new(2.0, 85.0), FillRule::Winding, 0.25));
1055 assert!(!p.contains_point(Point::new(85.0, 2.0), FillRule::Winding, 0.25));
1056 }
1057
1058 #[test]
1059 fn contains_point_circle() {
1060 let c = Path::circle(Point::new(50.0, 50.0), 25.0);
1061 assert!(c.contains_point(Point::new(50.0, 50.0), FillRule::Winding, 0.25));
1062 assert!(c.contains_point(Point::new(68.0, 50.0), FillRule::Winding, 0.25));
1063 assert!(!c.contains_point(Point::new(80.0, 50.0), FillRule::Winding, 0.25));
1064 }
1065
1066 #[test]
1067 fn arc_to_cubics_covers_a_full_circle_in_four_segments() {
1068 let segs = arc_to_cubics(Rect::new(0.0, 0.0, 100.0, 100.0), 0.0, 360.0);
1069 assert_eq!(segs.len(), 4);
1070 assert!((segs[0].from.x - 100.0).abs() < 0.01);
1072 assert!((segs[0].from.y - 50.0).abs() < 0.01);
1073 for w in segs.windows(2) {
1075 assert!((w[0].to.x - w[1].from.x).abs() < 0.01);
1076 assert!((w[0].to.y - w[1].from.y).abs() < 0.01);
1077 }
1078 }
1079
1080 #[test]
1081 fn arc_to_cubics_ignores_a_sub_threshold_sweep() {
1082 assert!(arc_to_cubics(Rect::new(0.0, 0.0, 10.0, 10.0), 0.0, 0.0).is_empty());
1083 }
1084
1085 #[test]
1086 fn subpath_segments_wrap_only_when_closed() {
1087 let open = Subpath {
1088 points: vec![
1089 Point::new(0.0, 0.0),
1090 Point::new(10.0, 0.0),
1091 Point::new(10.0, 10.0),
1092 ],
1093 closed: false,
1094 };
1095 assert_eq!(open.segments(false).count(), 2);
1096 assert_eq!(open.segments(true).count(), 3);
1097 let shut = Subpath {
1098 closed: true,
1099 ..open.clone()
1100 };
1101 assert_eq!(shut.segments(false).count(), 3);
1102 }
1103
1104 #[test]
1105 fn subpath_bounds_is_the_polyline_box() {
1106 let sp = Subpath {
1107 points: vec![Point::new(10.0, 20.0), Point::new(50.0, 80.0)],
1108 closed: false,
1109 };
1110 assert_eq!(sp.bounds(), Rect::new(10.0, 20.0, 40.0, 60.0));
1111 assert_eq!(
1112 Subpath {
1113 points: vec![],
1114 closed: false
1115 }
1116 .bounds(),
1117 Rect::ZERO
1118 );
1119 }
1120
1121 #[test]
1122 fn curve_subdivision_is_capped() {
1123 let mut path = Path::new();
1126 path.move_to(Point::new(0.0, 0.0)).cubic_to(
1127 Point::new(1.0e9, 1.0e9),
1128 Point::new(-1.0e9, 1.0e9),
1129 Point::new(1.0, 0.0),
1130 );
1131 let sp = path.flatten(0.001);
1132 assert_eq!(sp[0].points.len(), 65);
1133 }
1134
1135 #[test]
1136 fn flatten_tolerance_floor_keeps_a_zero_request_finite() {
1137 let mut path = Path::new();
1138 path.move_to(Point::new(0.0, 0.0))
1139 .quad_to(Point::new(50.0, 100.0), Point::new(100.0, 0.0));
1140 assert!(path.flatten(0.0)[0].points.len() <= 65);
1141 }
1142
1143 #[test]
1144 fn circle_path_not_empty() {
1145 let p = Path::circle(Point::new(50.0, 50.0), 25.0);
1146 assert!(!p.is_empty());
1147 }
1148
1149 #[test]
1150 fn circle_path_opens_with_moveto() {
1151 let p = Path::circle(Point::new(50.0, 50.0), 25.0);
1154 match p.commands.first() {
1155 Some(PathCommand::MoveTo(pt)) => {
1156 assert!((pt.x - 75.0).abs() < 0.01 && (pt.y - 50.0).abs() < 0.01);
1157 }
1158 other => panic!("circle must open with MoveTo, got {other:?}"),
1159 }
1160 }
1161
1162 #[test]
1163 fn star_path_has_correct_commands() {
1164 let p = Path::star(Point::new(50.0, 50.0), 30.0, 15.0, 5);
1165 assert!(!p.is_empty());
1166 assert_eq!(p.commands.len(), 11);
1168 }
1169
1170 #[test]
1171 fn bounds_of_simple_path() {
1172 let mut path = Path::new();
1173 path.move_to(Point::new(10.0, 20.0));
1174 path.line_to(Point::new(50.0, 80.0));
1175 path.line_to(Point::new(30.0, 40.0));
1176 let b = path.bounds();
1177 assert_eq!(b.x, 10.0);
1178 assert_eq!(b.y, 20.0);
1179 assert_eq!(b.width, 40.0);
1180 assert_eq!(b.height, 60.0);
1181 }
1182
1183 #[test]
1184 fn bounds_of_empty_path_is_zero() {
1185 let path = Path::new();
1186 let b = path.bounds();
1187 assert_eq!(b, Rect::ZERO);
1188 }
1189
1190 #[test]
1191 fn rounded_rect_uses_corner_radii() {
1192 use teksilo_tokens::CornerRadius;
1193 let p = Path::rounded_rect(
1194 Rect::new(0.0, 0.0, 100.0, 50.0),
1195 CornerRadius::uniform(10.0),
1196 );
1197 assert!(p.commands.len() > 5);
1199 let arc_count = p
1201 .commands
1202 .iter()
1203 .filter(|c| matches!(c, PathCommand::ArcTo { .. }))
1204 .count();
1205 assert_eq!(arc_count, 4); }
1207
1208 #[test]
1209 fn rounded_rect_zero_radii_is_plain_rect() {
1210 use teksilo_tokens::CornerRadius;
1211 let p = Path::rounded_rect(Rect::new(0.0, 0.0, 100.0, 50.0), CornerRadius::uniform(0.0));
1212 let arc_count = p
1214 .commands
1215 .iter()
1216 .filter(|c| matches!(c, PathCommand::ArcTo { .. }))
1217 .count();
1218 assert_eq!(arc_count, 0);
1219 }
1220
1221 #[test]
1222 fn rect_path_has_four_lines() {
1223 let p = Path::rect(Rect::new(10.0, 20.0, 30.0, 40.0));
1224 assert_eq!(p.commands.len(), 5);
1226 assert!(matches!(p.commands[0], PathCommand::MoveTo(_)));
1227 assert!(matches!(p.commands[4], PathCommand::Close));
1228 }
1229
1230 #[test]
1231 fn line_path() {
1232 let p = Path::line(Point::new(0.0, 0.0), Point::new(100.0, 50.0));
1233 assert_eq!(p.commands.len(), 2);
1234 assert!(matches!(p.commands[0], PathCommand::MoveTo(_)));
1235 assert!(matches!(p.commands[1], PathCommand::LineTo(_)));
1236 }
1237
1238 #[test]
1239 fn ellipse_path_uses_cubics() {
1240 let p = Path::ellipse(Rect::new(0.0, 0.0, 100.0, 50.0));
1241 let cubic_count = p
1242 .commands
1243 .iter()
1244 .filter(|c| matches!(c, PathCommand::CubicTo { .. }))
1245 .count();
1246 assert_eq!(cubic_count, 4);
1247 }
1248
1249 #[test]
1250 fn polygon_path() {
1251 let points = vec![
1252 Point::new(0.0, 0.0),
1253 Point::new(100.0, 0.0),
1254 Point::new(50.0, 80.0),
1255 ];
1256 let p = Path::polygon(&points);
1257 assert_eq!(p.commands.len(), 4);
1259 }
1260
1261 #[test]
1262 fn polygon_empty_points() {
1263 let p = Path::polygon(&[]);
1264 assert!(p.is_empty());
1265 }
1266
1267 #[test]
1268 fn append_merges_paths() {
1269 let mut a = Path::new();
1270 a.move_to(Point::new(0.0, 0.0));
1271 a.line_to(Point::new(10.0, 10.0));
1272
1273 let mut b = Path::new();
1274 b.move_to(Point::new(20.0, 20.0));
1275 b.line_to(Point::new(30.0, 30.0));
1276
1277 a.append(&b);
1278 assert_eq!(a.commands.len(), 4);
1279 assert!(matches!(a.commands[2], PathCommand::MoveTo(p) if (p.x - 20.0).abs() < 0.01));
1280 }
1281
1282 #[test]
1283 fn append_empty_is_noop() {
1284 let mut a = Path::new();
1285 a.move_to(Point::new(1.0, 2.0));
1286 let b = Path::new();
1287 a.append(&b);
1288 assert_eq!(a.commands.len(), 1);
1289 }
1290
1291 #[test]
1292 fn transformed_translate() {
1293 let mut path = Path::new();
1294 path.move_to(Point::new(10.0, 20.0));
1295 path.line_to(Point::new(30.0, 40.0));
1296 path.close();
1297
1298 let t = Transform2D::translate(100.0, 200.0);
1299 let result = path.transformed(&t);
1300
1301 assert_eq!(result.commands.len(), 3);
1302 match result.commands[0] {
1303 PathCommand::MoveTo(p) => {
1304 assert!((p.x - 110.0).abs() < 0.01);
1305 assert!((p.y - 220.0).abs() < 0.01);
1306 }
1307 _ => panic!("expected MoveTo"),
1308 }
1309 match result.commands[1] {
1310 PathCommand::LineTo(p) => {
1311 assert!((p.x - 130.0).abs() < 0.01);
1312 assert!((p.y - 240.0).abs() < 0.01);
1313 }
1314 _ => panic!("expected LineTo"),
1315 }
1316 assert!(matches!(result.commands[2], PathCommand::Close));
1317 }
1318
1319 #[test]
1320 fn transformed_scale() {
1321 let mut path = Path::new();
1322 path.move_to(Point::new(10.0, 20.0));
1323 path.quad_to(Point::new(15.0, 25.0), Point::new(30.0, 40.0));
1324
1325 let t = Transform2D::scale(2.0, 3.0);
1326 let result = path.transformed(&t);
1327
1328 match result.commands[1] {
1329 PathCommand::QuadTo { control, to } => {
1330 assert!((control.x - 30.0).abs() < 0.01);
1331 assert!((control.y - 75.0).abs() < 0.01);
1332 assert!((to.x - 60.0).abs() < 0.01);
1333 assert!((to.y - 120.0).abs() < 0.01);
1334 }
1335 _ => panic!("expected QuadTo"),
1336 }
1337 }
1338
1339 #[test]
1340 fn transformed_cubic() {
1341 let mut path = Path::new();
1342 path.move_to(Point::new(0.0, 0.0));
1343 path.cubic_to(
1344 Point::new(1.0, 0.0),
1345 Point::new(2.0, 1.0),
1346 Point::new(3.0, 3.0),
1347 );
1348
1349 let t = Transform2D::translate(10.0, 20.0);
1350 let result = path.transformed(&t);
1351
1352 match result.commands[1] {
1353 PathCommand::CubicTo {
1354 control1,
1355 control2,
1356 to,
1357 } => {
1358 assert!((control1.x - 11.0).abs() < 0.01);
1359 assert!((control2.x - 12.0).abs() < 0.01);
1360 assert!((to.x - 13.0).abs() < 0.01);
1361 }
1362 _ => panic!("expected CubicTo"),
1363 }
1364 }
1365
1366 #[test]
1367 fn transformed_preserves_command_count() {
1368 let p = Path::circle(Point::new(50.0, 50.0), 25.0);
1369 let t = Transform2D::scale(2.0, 2.0);
1370 let result = p.transformed(&t);
1371 assert_eq!(result.commands.len(), p.commands.len());
1372 }
1373
1374 #[test]
1375 fn arc_transform_is_exact_only_for_a_positive_axis_aligned_scale() {
1376 assert!(arc_transform_is_exact(&Transform2D::identity()));
1377 assert!(arc_transform_is_exact(&Transform2D::translate(9.0, -3.0)));
1378 assert!(arc_transform_is_exact(&Transform2D::scale(2.0, 7.0)));
1379 assert!(
1380 !arc_transform_is_exact(&Transform2D::rotate(0.4)),
1381 "a rotation mixes the axes"
1382 );
1383 assert!(
1384 !arc_transform_is_exact(&Transform2D::rotate(std::f32::consts::FRAC_PI_2)),
1385 "even a quarter turn: the angles would have to move with it"
1386 );
1387 assert!(
1388 !arc_transform_is_exact(&Transform2D::scale(-1.0, 1.0)),
1389 "a mirror reverses the sweep"
1390 );
1391 }
1392
1393 #[test]
1394 fn a_rotated_circle_is_still_that_circle() {
1395 let c = Path::circle(Point::ZERO, 30.0);
1400 for turns in 1..8 {
1401 let t = Transform2D::rotate(turns as f32 * 0.4);
1402 let rotated = c.transformed(&t);
1403 let b = rotated.exact_bounds(0.25);
1404 assert!(
1405 (b.width - 60.0).abs() < 0.5 && (b.height - 60.0).abs() < 0.5,
1406 "rotation by {turns} resized the circle to {b:?}"
1407 );
1408 let outside = t.apply_point(Point::new(38.0, 0.0));
1409 assert!(
1410 !rotated.contains_point(outside, FillRule::Winding, 0.25),
1411 "a pure rotation must not change containment (turn {turns})"
1412 );
1413 assert!(rotated.contains_point(
1414 t.apply_point(Point::new(20.0, 0.0)),
1415 FillRule::Winding,
1416 0.25
1417 ));
1418 }
1419 }
1420
1421 #[test]
1422 fn a_rotated_rounded_rect_keeps_its_corners_where_they_belong() {
1423 let r = Path::rounded_rect(
1424 Rect::new(-50.0, -50.0, 100.0, 100.0),
1425 teksilo_tokens::CornerRadius::uniform(25.0),
1426 );
1427 let t = Transform2D::rotate(std::f32::consts::FRAC_PI_4);
1428 let rotated = r.transformed(&t);
1429 assert!(rotated.contains_point(Point::ZERO, FillRule::Winding, 0.25));
1430 let corner = t.apply_point(Point::new(-49.0, -49.0));
1434 assert!(!rotated.contains_point(corner, FillRule::Winding, 0.25));
1435 let edge = t.apply_point(Point::new(0.0, -49.0));
1437 assert!(rotated.contains_point(edge, FillRule::Winding, 0.25));
1438 }
1439
1440 #[test]
1441 fn an_arc_the_transform_cannot_carry_becomes_cubics_not_a_moved_rectangle() {
1442 let c = Path::circle(Point::ZERO, 10.0);
1446 let arcs = |p: &Path| {
1447 p.commands
1448 .iter()
1449 .filter(|c| matches!(c, PathCommand::ArcTo { .. }))
1450 .count()
1451 };
1452 assert!(arcs(&c) > 0);
1453 assert_eq!(
1454 arcs(&c.transformed(&Transform2D::scale(3.0, 2.0))),
1455 arcs(&c)
1456 );
1457 assert_eq!(arcs(&c.transformed(&Transform2D::rotate(0.3))), 0);
1458 }
1459
1460 const STRUCT_TOL: f32 = 0.05;
1467
1468 #[derive(Debug, Clone, Copy)]
1479 struct SubpathShape {
1480 closed: bool,
1481 start: Point,
1482 bounds: Rect,
1483 }
1484
1485 fn structure(p: &Path, back: &Transform2D) -> Vec<SubpathShape> {
1486 p.flatten(STRUCT_TOL)
1487 .iter()
1488 .map(|sp| {
1489 let pts: Vec<Point> = sp.points.iter().map(|q| back.apply_point(*q)).collect();
1490 SubpathShape {
1491 closed: sp.closed,
1492 start: pts[0],
1493 bounds: bounds_of_points(&pts),
1494 }
1495 })
1496 .collect()
1497 }
1498
1499 #[track_caller]
1509 fn assert_transform_commutes_with_flatten(label: &str, p: &Path, t: &Transform2D) {
1510 const NEAR: f32 = 4.0 * STRUCT_TOL;
1511 let back = t.inverse().expect("test transforms are invertible");
1512 let plain = structure(p, &Transform2D::identity());
1513 let moved = structure(&p.transformed(t), &back);
1514 assert_eq!(
1515 moved.len(),
1516 plain.len(),
1517 "{label}: {} subpaths after the transform, {} before\n after: {moved:?}\n before: {plain:?}",
1518 moved.len(),
1519 plain.len()
1520 );
1521 for (i, (m, e)) in moved.iter().zip(&plain).enumerate() {
1522 assert_eq!(m.closed, e.closed, "{label}: subpath {i} closed flag");
1523 assert!(
1524 (m.start.x - e.start.x).abs() < NEAR && (m.start.y - e.start.y).abs() < NEAR,
1525 "{label}: subpath {i} starts at {:?}, not {:?}",
1526 m.start,
1527 e.start
1528 );
1529 assert!(
1530 (m.bounds.x - e.bounds.x).abs() < NEAR
1531 && (m.bounds.y - e.bounds.y).abs() < NEAR
1532 && (m.bounds.width - e.bounds.width).abs() < NEAR
1533 && (m.bounds.height - e.bounds.height).abs() < NEAR,
1534 "{label}: subpath {i} covers {:?}, not {:?}",
1535 m.bounds,
1536 e.bounds
1537 );
1538 }
1539 }
1540
1541 fn arc_rect() -> Rect {
1543 Rect::new(180.0, -20.0, 40.0, 40.0)
1544 }
1545
1546 fn square() -> Path {
1547 let mut p = Path::new();
1548 p.move_to(Point::new(0.0, 0.0))
1549 .line_to(Point::new(10.0, 0.0))
1550 .line_to(Point::new(10.0, 10.0))
1551 .line_to(Point::new(0.0, 10.0))
1552 .close();
1553 p
1554 }
1555
1556 fn structural_battery() -> Vec<(&'static str, Path)> {
1559 let mut out: Vec<(&'static str, Path)> = Vec::new();
1560
1561 let mut p = square();
1563 p.arc_to(arc_rect(), 0.0, 360.0).close();
1564 out.push(("close then full-sweep arc", p));
1565
1566 let mut p = square();
1569 p.arc_to(arc_rect(), 0.0, 200.0).close();
1570 out.push(("close then partial arc", p));
1571
1572 let mut p = Path::new();
1573 p.arc_to(arc_rect(), 0.0, 360.0);
1574 out.push(("arc opens the path", p));
1575
1576 let mut p = Path::new();
1577 p.close();
1578 p.arc_to(arc_rect(), 0.0, 360.0);
1579 out.push(("close with no geometry, then arc", p));
1580
1581 let mut p = Path::new();
1582 p.move_to(Point::new(5.0, 5.0)).close();
1583 p.arc_to(arc_rect(), 0.0, 360.0);
1584 out.push(("lone move_to, close, then arc", p));
1585
1586 let mut p = Path::new();
1587 p.move_to(Point::new(220.0, 0.0));
1588 p.arc_to(arc_rect(), 0.0, 180.0);
1589 p.arc_to(arc_rect(), 180.0, 180.0);
1590 out.push(("arc continuing an arc at the shared point", p));
1591
1592 let mut p = Path::new();
1593 p.arc_to(arc_rect(), 0.0, 90.0);
1594 p.arc_to(Rect::new(0.0, 0.0, 20.0, 20.0), 0.0, 90.0);
1595 out.push(("arc after a disjoint arc", p));
1596
1597 let mut p = Path::new();
1598 p.move_to(Point::new(50.0, 50.0));
1599 p.arc_to(arc_rect(), 0.0, 360.0);
1600 out.push(("arc after a move_to somewhere else", p));
1601
1602 let mut p = square();
1603 p.quad_to(Point::new(120.0, -40.0), Point::new(200.0, 0.0));
1604 out.push(("close then quad", p));
1605
1606 let mut p = square();
1607 p.cubic_to(
1608 Point::new(60.0, -40.0),
1609 Point::new(140.0, 40.0),
1610 Point::new(200.0, 0.0),
1611 );
1612 out.push(("close then cubic", p));
1613
1614 let mut p = Path::new();
1615 p.quad_to(Point::new(120.0, -40.0), Point::new(200.0, 0.0));
1616 out.push(("quad opens the path", p));
1617
1618 let mut p = Path::new();
1619 p.cubic_to(
1620 Point::new(60.0, -40.0),
1621 Point::new(140.0, 40.0),
1622 Point::new(200.0, 0.0),
1623 );
1624 out.push(("cubic opens the path", p));
1625
1626 let mut p = square();
1627 p.line_to(Point::new(200.0, 0.0));
1628 out.push(("close then line", p));
1629
1630 out.push(("rounded rect", {
1631 Path::rounded_rect(
1632 Rect::new(-50.0, -50.0, 100.0, 100.0),
1633 teksilo_tokens::CornerRadius::uniform(25.0),
1634 )
1635 }));
1636 out.push(("circle", Path::circle(Point::new(30.0, 10.0), 20.0)));
1637
1638 out
1639 }
1640
1641 #[test]
1642 fn transformed_preserves_subpath_structure_in_both_branches() {
1643 let survives = Transform2D::scale(3.0, 2.0);
1648 let expands = Transform2D::rotate(30f32.to_radians());
1649 assert!(arc_transform_is_exact(&survives));
1650 assert!(!arc_transform_is_exact(&expands));
1651
1652 for (label, path) in structural_battery() {
1653 assert_transform_commutes_with_flatten(
1654 &format!("{label} / arcs survive"),
1655 &path,
1656 &survives,
1657 );
1658 assert_transform_commutes_with_flatten(
1659 &format!("{label} / arcs expand"),
1660 &path,
1661 &expands,
1662 );
1663 }
1664 }
1665
1666 #[test]
1667 fn an_expanded_arc_after_a_close_opens_its_own_subpath() {
1668 let mut p = square();
1672 p.arc_to(arc_rect(), 0.0, 360.0).close();
1673
1674 let rotated = p.transformed(&Transform2D::rotate(30f32.to_radians()));
1675 let after_close = rotated
1676 .commands
1677 .iter()
1678 .position(|c| matches!(c, PathCommand::Close))
1679 .expect("the square is closed")
1680 + 1;
1681 assert!(
1682 matches!(rotated.commands[after_close], PathCommand::MoveTo(_)),
1683 "the arc has to open a subpath, got {:?}",
1684 rotated.commands[after_close]
1685 );
1686
1687 let sub = rotated.flatten(STRUCT_TOL);
1690 assert_eq!(sub.len(), 2, "two loops, still");
1691 let back = Transform2D::rotate(30f32.to_radians()).inverse().unwrap();
1692 let pts: Vec<Point> = sub[1].points.iter().map(|q| back.apply_point(*q)).collect();
1693 let b = bounds_of_points(&pts);
1694 assert!(
1695 (b.width - 40.0).abs() < 0.2 && (b.height - 40.0).abs() < 0.2,
1696 "the arc's loop grew to {b:?}"
1697 );
1698 }
1699
1700 #[test]
1701 fn a_rotated_arc_that_opens_a_subpath_still_opens_one() {
1702 let mut p = Path::new();
1705 p.arc_to(Rect::new(0.0, 0.0, 20.0, 20.0), 0.0, 360.0);
1706 let rotated = p.transformed(&Transform2D::rotate(0.7));
1707 assert!(matches!(
1708 rotated.commands.first(),
1709 Some(PathCommand::MoveTo(_))
1710 ));
1711 assert_eq!(rotated.flatten(0.25).len(), 1);
1712 }
1713}