Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making move_to less strict. #320

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 23 additions & 18 deletions src/bezpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,44 +220,49 @@ impl BezPath {

/// Push a "line to" element onto the path.
///
/// Will panic with a debug assert when the current subpath does not
/// start with `move_to`.
/// Will panic with a debug assert when the path is empty and there is no
/// "move to" element on the path.
///
/// If `line_to` is called immediately after `close_path` then the current
/// subpath starts at the initial point of the previous subpath.
pub fn line_to<P: Into<Point>>(&mut self, p: P) {
debug_assert!(self.is_open_subpath(), "no open subpath (missing MoveTo)");
debug_assert!(!self.0.is_empty(), "uninitialized subpath (missing MoveTo)");
self.push(PathEl::LineTo(p.into()));
}

/// Push a "quad to" element onto the path.
///
/// Will panic with a debug assert when the current subpath does not
/// start with `move_to`.
/// Will panic with a debug assert when the path is empty and there is no
/// "move to" element on the path.
///
/// If `quad_to` is called immediately after `close_path` then the current
/// subpath starts at the initial point of the previous subpath.
pub fn quad_to<P: Into<Point>>(&mut self, p1: P, p2: P) {
debug_assert!(self.is_open_subpath(), "no open subpath (missing MoveTo)");
debug_assert!(!self.0.is_empty(), "uninitialized subpath (missing MoveTo)");
self.push(PathEl::QuadTo(p1.into(), p2.into()));
}

/// Push a "curve to" element onto the path.
///
/// Will panic with a debug assert when the current subpath does not
/// start with `move_to`.
/// Will panic with a debug assert when the path is empty and there is no
/// "move to" element on the path.
///
/// If `curve_to` is called immediately after `close_path` then the current
/// subpath starts at the initial point of the previous subpath.
pub fn curve_to<P: Into<Point>>(&mut self, p1: P, p2: P, p3: P) {
debug_assert!(self.is_open_subpath(), "no open subpath (missing MoveTo)");
debug_assert!(!self.0.is_empty(), "uninitialized subpath (missing MoveTo)");
self.push(PathEl::CurveTo(p1.into(), p2.into(), p3.into()));
}

/// Push a "close path" element onto the path.
///
/// Will panic with a debug assert when the current subpath does not
/// start with `move_to`.
/// Will panic with a debug assert when the path is empty and there is no
/// "move to" element on the path.
pub fn close_path(&mut self) {
debug_assert!(self.is_open_subpath(), "no open subpath (missing MoveTo)");
debug_assert!(!self.0.is_empty(), "uninitialized subpath (missing MoveTo)");
self.push(PathEl::ClosePath);
}

fn is_open_subpath(&self) -> bool {
!self.0.is_empty() && self.0.last() != Some(&PathEl::ClosePath)
}

/// Get the path elements.
pub fn elements(&self) -> &[PathEl] {
&self.0
Expand Down Expand Up @@ -1507,7 +1512,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "no open subpath")]
#[should_panic(expected = "uninitialized subpath")]
fn test_elements_to_segments_starts_on_closepath() {
let mut path = BezPath::new();
path.close_path();
Expand All @@ -1529,7 +1534,7 @@ mod tests {
}

#[test]
#[should_panic(expected = "no open subpath")]
#[should_panic(expected = "uninitialized subpath")]
fn test_must_not_start_on_quad() {
let mut path = BezPath::new();
path.quad_to((5.0, 5.0), (10.0, 10.0));
Expand Down