From 4dea6aa258cab56f48e62533057b2d040c1ffa9e Mon Sep 17 00:00:00 2001 From: Sebastian K Date: Fri, 29 May 2020 22:42:00 +0300 Subject: [PATCH 1/4] Add a `transformed()` method to `Outline` and `Contour`. --- content/src/outline.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/content/src/outline.rs b/content/src/outline.rs index 8da6c6bd..e2b26d22 100644 --- a/content/src/outline.rs +++ b/content/src/outline.rs @@ -178,6 +178,11 @@ impl Outline { self.bounds = new_bounds.unwrap_or_else(|| RectF::default()); } + pub fn transformed(mut self, transform: &Transform2F) -> Outline { + self.transform(transform); + self + } + pub fn apply_perspective(&mut self, perspective: &Perspective) { let mut new_bounds = None; for contour in &mut self.contours { @@ -614,6 +619,12 @@ impl Contour { } } + #[inline] + pub fn transformed(mut self, transform: &Transform2F) -> Contour { + self.transform(transform); + self + } + pub fn apply_perspective(&mut self, perspective: &Perspective) { for (point_index, point) in self.points.iter_mut().enumerate() { *point = *perspective * *point; From 5eb346f77dac05b0a679219f2f0efbca8b017fdb Mon Sep 17 00:00:00 2001 From: Sebastian K Date: Wed, 24 Jun 2020 17:40:51 -0700 Subject: [PATCH 2/4] Add an `Outline::with_capacity()` method --- content/src/outline.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/content/src/outline.rs b/content/src/outline.rs index e2b26d22..e91a0af5 100644 --- a/content/src/outline.rs +++ b/content/src/outline.rs @@ -61,6 +61,15 @@ impl Outline { } } + /// Returns a new `Outline` with storage for `capacity` contours preallocated. + #[inline] + pub fn with_capacity(capacity: usize) -> Outline { + Outline { + contours: Vec::with_capacity(capacity), + bounds: RectF::default(), + } + } + #[inline] pub fn from_segments(segments: I) -> Outline where From e5eee41b48a5ba705c8feaaf1ea621d66774fd29 Mon Sep 17 00:00:00 2001 From: Sebastian K Date: Wed, 24 Jun 2020 17:42:02 -0700 Subject: [PATCH 3/4] Micro-optimize `Contour::from_rect()` --- content/src/outline.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/src/outline.rs b/content/src/outline.rs index e91a0af5..5223682c 100644 --- a/content/src/outline.rs +++ b/content/src/outline.rs @@ -289,7 +289,7 @@ impl Contour { #[inline] pub fn from_rect(rect: RectF) -> Contour { - let mut contour = Contour::new(); + let mut contour = Contour::with_capacity(4); contour.push_point(rect.origin(), PointFlags::empty(), false); contour.push_point(rect.upper_right(), PointFlags::empty(), false); contour.push_point(rect.lower_right(), PointFlags::empty(), false); From 9d86f663ed93b04dbeb2d38ada706840a0bd7d27 Mon Sep 17 00:00:00 2001 From: Sebastian K Date: Wed, 24 Jun 2020 17:47:30 -0700 Subject: [PATCH 4/4] Add a new `Outline::len()` method --- content/src/outline.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/content/src/outline.rs b/content/src/outline.rs index 5223682c..b8aed554 100644 --- a/content/src/outline.rs +++ b/content/src/outline.rs @@ -238,6 +238,12 @@ impl Outline { self.contours.iter().all(Contour::is_empty) } + /// Returns the number of contours in this outline. + #[inline] + pub fn len(&self) -> usize { + self.contours.len() + } + /// Appends the contours in another outline to this one. pub fn push_outline(&mut self, other: Outline) { if other.is_empty() {