Skip to content

Commit

Permalink
Add Path2D::add_path()
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Mar 27, 2020
1 parent 15718d2 commit c258616
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
12 changes: 12 additions & 0 deletions canvas/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,18 @@ impl Path2D {
}
}

// https://html.spec.whatwg.org/multipage/canvas.html#dom-path2d-addpath
pub fn add_path(&mut self, mut path: Path2D, transform: &Transform2F) {
self.flush_current_contour();
path.flush_current_contour();
path.outline.transform(transform);
let last_contour = path.outline.pop_contour();
for contour in path.outline.into_contours() {
self.outline.push_contour(contour);
}
self.current_contour = last_contour.unwrap_or_else(Contour::new);
}

pub fn into_outline(mut self) -> Outline {
self.flush_current_contour();
self.outline
Expand Down
17 changes: 17 additions & 0 deletions content/src/outline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ impl Outline {
&self.contours
}

#[inline]
pub fn into_contours(self) -> Vec<Contour> {
self.contours
}

/// Removes all contours from this outline.
#[inline]
pub fn clear(&mut self) {
Expand All @@ -141,6 +146,18 @@ impl Outline {
self.contours.push(contour);
}

pub fn pop_contour(&mut self) -> Option<Contour> {
let last_contour = self.contours.pop();

let mut new_bounds = None;
for contour in &mut self.contours {
contour.update_bounds(&mut new_bounds);
}
self.bounds = new_bounds.unwrap_or_else(|| RectF::default());

last_contour
}

pub fn transform(&mut self, transform: &Transform2F) {
if transform.is_identity() {
return;
Expand Down

0 comments on commit c258616

Please sign in to comment.