From 7ccb27163b56feae8cafbfa276f48e7f785385c6 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Sun, 22 Sep 2024 07:00:33 +0700 Subject: [PATCH] Derive `PartialEq` for `Stroke`, common derives for `StrokeOpts`. (#379) As discussed in https://xi.zulipchat.com/#narrow/stream/260979-kurbo/topic/PartialEq.20for.20Stroke.3F --- CHANGELOG.md | 5 +++++ src/stroke.rs | 14 ++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7810278f..fb5cd72d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ You can find its changes [documented below](#0111-2024-09-12). This release has an [MSRV][] of 1.65. +### Changed + +- `Stroke` is now `PartialEq`, `StrokeOpts` is now `Clone`, `Copy`, `Debug`, `Eq`, `PartialEq`. ([#379] by [@waywardmonkeys]) + ## [0.11.1][] (2024-09-12) This release has an [MSRV][] of 1.65. @@ -75,6 +79,7 @@ Note: A changelog was not kept for or before this release [#370]: https://github.com/linebender/kurbo/pull/370 [#375]: https://github.com/linebender/kurbo/pull/375 [#376]: https://github.com/linebender/kurbo/pull/376 +[#379]: https://github.com/linebender/kurbo/pull/379 [Unreleased]: https://github.com/linebender/kurbo/compare/v0.11.1...HEAD [0.11.0]: https://github.com/linebender/kurbo/releases/tag/v0.11.0 diff --git a/src/stroke.rs b/src/stroke.rs index ad8183c8..40015f39 100644 --- a/src/stroke.rs +++ b/src/stroke.rs @@ -42,7 +42,7 @@ pub enum Cap { } /// Describes the visual style of a stroke. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Stroke { @@ -63,11 +63,13 @@ pub struct Stroke { } /// Options for path stroking. +#[derive(Clone, Copy, Debug, PartialEq)] pub struct StrokeOpts { opt_level: StrokeOptLevel, } /// Optimization level for computing +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum StrokeOptLevel { /// Adaptively subdivide segments in half. Subdivide, @@ -201,10 +203,10 @@ pub fn stroke( tolerance: f64, ) -> BezPath { if style.dash_pattern.is_empty() { - stroke_undashed(path, style, tolerance, opts) + stroke_undashed(path, style, tolerance, *opts) } else { let dashed = dash(path.into_iter(), style.dash_offset, &style.dash_pattern); - stroke_undashed(dashed, style, tolerance, opts) + stroke_undashed(dashed, style, tolerance, *opts) } } @@ -213,7 +215,7 @@ fn stroke_undashed( path: impl IntoIterator, style: &Stroke, tolerance: f64, - opts: &StrokeOpts, + opts: StrokeOpts, ) -> BezPath { let mut ctx = StrokeCtx { join_thresh: 2.0 * tolerance / style.width, @@ -307,7 +309,7 @@ fn extend_reversed(out: &mut BezPath, elements: &[PathEl]) { } } -fn fit_with_opts(co: &CubicOffset, tolerance: f64, opts: &StrokeOpts) -> BezPath { +fn fit_with_opts(co: &CubicOffset, tolerance: f64, opts: StrokeOpts) -> BezPath { match opts.opt_level { StrokeOptLevel::Subdivide => fit_to_bezpath(co, tolerance), StrokeOptLevel::Optimized => fit_to_bezpath_opt(co, tolerance), @@ -428,7 +430,7 @@ impl StrokeCtx { self.last_pt = p1; } - fn do_cubic(&mut self, style: &Stroke, c: CubicBez, tolerance: f64, opts: &StrokeOpts) { + fn do_cubic(&mut self, style: &Stroke, c: CubicBez, tolerance: f64, opts: StrokeOpts) { // First, detect degenerate linear case // Ordinarily, this is the direction of the chord, but if the chord is very