Clean-up changes from #939

This commit is contained in:
Jürg Lehni 2016-02-03 12:54:54 +01:00
parent aed9d05bfc
commit 41aca10112

View file

@ -314,6 +314,7 @@ PathItem.inject(new function() {
py = point.y, py = point.y,
windLeft = 0, windLeft = 0,
windRight = 0, windRight = 0,
isOnCurve = false,
length = curves.length, length = curves.length,
roots = [], roots = [],
abs = Math.abs; abs = Math.abs;
@ -349,17 +350,15 @@ PathItem.inject(new function() {
var xBefore = px - epsilon, var xBefore = px - epsilon,
xAfter = px + epsilon, xAfter = px + epsilon,
prevWinding, prevWinding,
prevXEnd, prevXEnd;
isOnCurve;
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var curve = curves[i], var curve = curves[i],
winding = curve.winding, winding = curve.winding,
values = curve.values, values = curve.values,
yStart = values[1], yStart = values[1],
yEnd = values[7]; yEnd = values[7];
// The first curve of a loop holds the last curve with // The first curve of a loop holds the last curve with non-zero
// non-zero winding. // winding. Retrieve and use it here (See _getMonoCurve()).
// Retrieve and use it here (See _getMonoCurve()).
if (curve.last) { if (curve.last) {
// Get the end x coordinate and winding of the last // Get the end x coordinate and winding of the last
// non-horizontal curve, which will be the previous // non-horizontal curve, which will be the previous
@ -374,19 +373,19 @@ PathItem.inject(new function() {
// Horizontal curves with winding == 0 will be completely // Horizontal curves with winding == 0 will be completely
// ignored. // ignored.
if (winding && (py >= yStart && py <= yEnd if (winding && (py >= yStart && py <= yEnd
|| py >= yEnd && py <= yStart)) { || py >= yEnd && py <= yStart)) {
// Calculate the x value for the ray's intersection. // Calculate the x value for the ray's intersection.
var x = py === yStart ? values[0] var x = py === yStart ? values[0]
: py === yEnd ? values[6] : py === yEnd ? values[6]
: Curve.solveCubic(values, 1, py, roots, 0, 1) === 1 : Curve.solveCubic(values, 1, py, roots, 0, 1) === 1
? Curve.getPoint(values, roots[0]).x ? Curve.getPoint(values, roots[0]).x
: null; : null;
if (x != null) { if (x != null) {
// If the point is between the curve's start point and // If the point is between the curve's start point and
// the previous curve's end point, it must be on a // the previous curve's end point, it must be on a
// horizontal curve inbetween. // horizontal curve in between.
var isOnHorizontal = var isOnHorizontal =
(py === yStart && (px - x) * (px - prevXEnd) < 0); py === yStart && (px - x) * (px - prevXEnd) < 0;
// Test if the point is on a y-monotonic curve // Test if the point is on a y-monotonic curve
if ((x >= xBefore && x <= xAfter) || isOnHorizontal) if ((x >= xBefore && x <= xAfter) || isOnHorizontal)
isOnCurve = true; isOnCurve = true;
@ -396,7 +395,7 @@ PathItem.inject(new function() {
// - or the crossing is at the start of the curve and // - or the crossing is at the start of the curve and
// the winding did not change between curves // the winding did not change between curves
if (py != yEnd if (py != yEnd
&& (py != yStart || winding === prevWinding)) { && (py != yStart || winding === prevWinding)) {
if (x < xBefore) { if (x < xBefore) {
windLeft += winding; windLeft += winding;
} else if (x > xAfter) { } else if (x > xAfter) {
@ -411,7 +410,10 @@ PathItem.inject(new function() {
} }
} }
} }
// If the point was on a monocurve, ensure that the winding is odd. // If the point is on a monotonic curve, ensure that the winding is odd
// by applying bit-wise or `| 1` to the bigger absolute winding number:
// If winding is an even number and the point is on a curve, this will
// add up to the next uneven number, otherwise it remains the same.
return Math.max(abs(windLeft), abs(windRight)) | (isOnCurve ? 1 : 0 ); return Math.max(abs(windLeft), abs(windRight)) | (isOnCurve ? 1 : 0 );
} }