Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
armansito committed Nov 22, 2023
1 parent b360513 commit 8001151
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions crates/encoding/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,8 @@ impl<'a> PathEncoder<'a> {
self.move_to(self.first_point[0], self.first_point[1]);
}
if self.state == PathState::MoveTo {
let p0 = (self.first_point[0], self.first_point[1]);
// Ensure that we don't end up with a zero-length start tangent.
let Some((x, y)) = start_tangent_for_curve(p0, (x, y), None, None) else {
let Some((x, y)) = self.start_tangent_for_curve((x, y), None, None) else {
return;
};
self.first_start_tangent_end = [x, y];
Expand All @@ -552,9 +551,8 @@ impl<'a> PathEncoder<'a> {
self.move_to(self.first_point[0], self.first_point[1]);
}
if self.state == PathState::MoveTo {
let p0 = (self.first_point[0], self.first_point[1]);
// Ensure that we don't end up with a zero-length start tangent.
let Some((x, y)) = start_tangent_for_curve(p0, (x1, y1), Some((x2, y2)), None) else {
let Some((x, y)) = self.start_tangent_for_curve((x1, y1), Some((x2, y2)), None) else {
return;
};
self.first_start_tangent_end = [x, y];
Expand All @@ -581,10 +579,9 @@ impl<'a> PathEncoder<'a> {
self.move_to(self.first_point[0], self.first_point[1]);
}
if self.state == PathState::MoveTo {
let p0 = (self.first_point[0], self.first_point[1]);
// Ensure that we don't end up with a zero-length start tangent.
let Some((x, y)) =
start_tangent_for_curve(p0, (x1, y1), Some((x2, y2)), Some((x3, y3)))
self.start_tangent_for_curve((x1, y1), Some((x2, y2)), Some((x3, y3)))
else {
return;
};
Expand Down Expand Up @@ -738,6 +735,33 @@ impl<'a> PathEncoder<'a> {

!(x_max - x_min > EPSILON || y_max - y_min > EPSILON)
}

// Returns the end point of the start tangent of a curve starting at `(x0, y0)`, or `None` if the
// curve is degenerate / has zero-length. The inputs are a sequence of control points that can
// represent a line, a quadratic Bezier, or a cubic Bezier. Lines and quadratic Beziers can be
// passed to this function by simply setting the invalid control point degrees equal to `None`.
//
// `self.first_point` is always treated as the first control point of the curve.
fn start_tangent_for_curve(
&self,
p1: (f32, f32),
p2: Option<(f32, f32)>,
p3: Option<(f32, f32)>,
) -> Option<(f32, f32)> {
let p0 = (self.first_point[0], self.first_point[1]);
let p2 = p2.unwrap_or(p0);
let p3 = p3.unwrap_or(p0);
let pt = if (p1.0 - p0.0).abs() > EPSILON || (p1.1 - p0.1).abs() > EPSILON {
p1
} else if (p2.0 - p0.0).abs() > EPSILON || (p2.1 - p0.1).abs() > EPSILON {
p2
} else if (p3.0 - p0.0).abs() > EPSILON || (p3.1 - p0.1).abs() > EPSILON {
p3
} else {
return None;
};
Some(pt)
}
}

#[cfg(feature = "full")]
Expand Down Expand Up @@ -765,30 +789,6 @@ impl fello::scale::Pen for PathEncoder<'_> {

const EPSILON: f32 = 1e-12;

// Returns the end point of the start tangent of a curve starting at `(x0, y0)`, or `None` if the
// curve is degenerate / has zero-length. The inputs are a sequence of control points that can
// represent a line, a quadratic Bezier, or a cubic Bezier. Lines and quadratic Beziers can be
// passed to this function by simply setting the invalid control point degrees equal to `(x0, y0)`.
fn start_tangent_for_curve(
p0: (f32, f32),
p1: (f32, f32),
p2: Option<(f32, f32)>,
p3: Option<(f32, f32)>,
) -> Option<(f32, f32)> {
let p2 = p2.unwrap_or(p0);
let p3 = p3.unwrap_or(p0);
let pt = if (p1.0 - p0.0).abs() > EPSILON || (p1.1 - p0.1).abs() > EPSILON {
p1
} else if (p2.0 - p0.0).abs() > EPSILON || (p2.1 - p0.1).abs() > EPSILON {
p2
} else if (p3.0 - p0.0).abs() > EPSILON || (p3.1 - p0.1).abs() > EPSILON {
p3
} else {
return None;
};
Some(pt)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 8001151

Please sign in to comment.