if (prev != DRAWING_OP_TO) { emitMoveTo(x0 + mx, y0 + my); this.sdx = dx; this.sdy = dy; this.smx = mx; this.smy = my; } else { boolean cw = isCW(pdx, pdy, dx, dy); if (joinStyle == JOIN_MITER) { drawMiter(pdx, pdy, x0, y0, dx, dy, omx, omy, mx, my, cw); } else if (joinStyle == JOIN_ROUND) { drawRoundJoin(x0, y0, omx, omy, mx, my, cw, ROUND_JOIN_THRESHOLD); } emitLineTo(x0, y0, !cw); } prev = DRAWING_OP_TO; } private static boolean within(final float x1, final float y1, final float x2, final float y2, final float ERR) { assert ERR > 0 : ""; // compare taxicab distance. ERR will always be small, so using // true distance won't give much benefit return (Helpers.within(x1, x2, ERR) && // we want to avoid calling Math.abs Helpers.within(y1, y2, ERR)); // this is just as good. } private void getLineOffsets(float x1, float y1, float x2, float y2, float[] left, float[] right) { computeOffset(x2 - x1, y2 - y1, lineWidth2, offset[0]); left[0] = x1 + offset[0][0]; left[1] = y1 + offset[0][1]; left[2] = x2 + offset[0][0]; left[3] = y2 + offset[0][1]; right[0] = x1 - offset[0][0]; right[1] = y1 - offset[0][1]; right[2] = x2 - offset[0][0]; right[3] = y2 - offset[0][1]; } private int computeOffsetCubic(float[] pts, final int off, float[] leftOff, float[] rightOff) { // if p1=p2 or p3=p4 it means that the derivative at the endpoint // vanishes, which creates problems with computeOffset. Usually // this happens when this stroker object is trying to winden // a curve with a cusp. What happens is that curveTo splits // the input curve at the cusp, and passes it to this function. // because of inaccuracies in the splitting, we consider points // equal if they're very close to each other. final float x1 = pts[off + 0], y1 = pts[off + 1]; final float x2 = pts[off + 2], y2 = pts[off + 3]; final float x3 = pts[off + 4], y3 = pts[off + 5]; final float x4 = pts[off + 6], y4 = pts[off + 7]; float dx4 = x4 - x3; float dy4 = y4 - y3; float dx1 = x2 - x1; float dy1 = y2 - y1; // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4, // in which case ignore if p1 == p2 final boolean p1eqp2 = within(x1,y1,x2,y2, 6 * ulp(y2)); final boolean p3eqp4 = within(x3,y3,x4,y4, 6 * ulp(y4)); if (p1eqp2 && p3eqp4) { getLineOffsets(x1, y1, x4, y4, leftOff, rightOff); return 4; } else if (p1eqp2) { dx1 = x3 - x1; dy1 = y3 - y1; } else if (p3eqp4) { dx4 = x4 - x2; dy4 = y4 - y2; } // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line float dotsq = (dx1 * dx4 + dy1 * dy4); dotsq = dotsq * dotsq; float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4; if (Helpers.within(dotsq, l1sq * l4sq, 4 * ulp(dotsq))) { getLineOffsets(x1, y1, x4, y4, leftOff, rightOff); return 4; } // What we're trying to do in this function is to approximate an ideal// offset curve (call it I) of the input curve B using a bezier curve Bp.// The constraints I use to get the equations are://// 1. The computed curve Bp should go through I(0) and I(1). These are// x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find// 4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p).//// 2. Bp should have slope equal in absolute value to I at the endpoints. So,// (by the way, the operator || in the comments below means "aligned with".// It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that// vectors I'(0) and Bp'(0) are aligned, which is the same as saying// that the tangent lines of I and Bp at 0 are parallel. Mathematically// this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some// nonzero constant.)// I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and// I'(1) || B'(1); therefore, Bp'(0) || B'(0) and Bp'(1) || B'(1).// We know that Bp'(0) || (p2p-p1p) and Bp'(1) || (p4p-p3p) and the same// is true for any bezier curve; therefore, we get the equations// (1) p2p = c1 * (p2-p1) + p1p// (2) p3p = c2 * (p4-p3) + p4p// We know p1p, p4p, p2, p1, p3, and p4; therefore, this reduces the number// of unknowns from 4 to 2 (i.e. just c1 and c2).// To eliminate these 2 unknowns we use the following constraint://// 3. Bp(0.5) == I(0.5). Bp(0.5)=(x,y) and I(0.5)=(xi,yi), and I should note// that I(0.5) is *the only* reason for computing dxm,dym. This gives us// (3) Bp(0.5) = (p1p + 3 * (p2p + p3p) + p4p)/8, which is equivalent to// (4) p2p + p3p = (Bp(0.5)*8 - p1p - p4p) / 3// We can substitute (1) and (2) from above into (4) and we get:// (5) c1*(p2-p1) + c2*(p4-p3) = (Bp(0.5)*8 - p1p - p4p)/3 - p1p - p4p// which is equivalent to// (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p)//// The right side of this is a 2D vector, and we know I(0.5), which gives us// Bp(0.5), which gives us the value of the right side.// The left side is just a matrix vector multiplication in disguise. It is//// [x2-x1, x4-x3][c1]// [y2-y1, y4-y3][c2]// which, is equal to// [dx1, dx4][c1]// [dy1, dy4][c2]// At this point we are left with a simple linear system and we solve it by// getting the inverse of the matrix above. Then we use [c1,c2] to compute// p2p and p3p. float x = 0.125f * (x1 + 3 * (x2 + x3) + x4); float y = 0.125f * (y1 + 3 * (y2 + y3) + y4); // (dxm,dym) is some tangent of B at t=0.5. This means it's equal to // c*B'(0.5) for some constant c. float dxm = x3 + x4 - x1 - x2, dym = y3 + y4 - y1 - y2; // this computes the offsets at t=0, 0.5, 1, using the property that // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to // the (dx/dt, dy/dt) vectors at the endpoints. computeOffset(dx1, dy1, lineWidth2, offset[0]); computeOffset(dxm, dym, lineWidth2, offset[1]); computeOffset(dx4, dy4, lineWidth2, offset[2]); float x1p = x1 + offset[0][0]; // start float y1p = y1 + offset[0][1]; // point float xi = x + offset[1][0]; // interpolation float yi = y + offset[1][1]; // point float x4p = x4 + offset[2][0]; // end float y4p = y4 + offset[2][1]; // point float invdet43 = 4f / (3f * (dx1 * dy4 - dy1 * dx4)); float two_pi_m_p1_m_p4x = 2*xi - x1p - x4p; float two_pi_m_p1_m_p4y = 2*yi - y1p - y4p; float c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y); float c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x); float x2p, y2p, x3p, y3p; x2p = x1p + c1*dx1; y2p = y1p + c1*dy1; x3p = x4p + c2*dx4; y3p = y4p + c2*dy4; leftOff[0] = x1p; leftOff[1] = y1p; leftOff[2] = x2p; leftOff[3] = y2p; leftOff[4] = x3p; leftOff[5] = y3p; leftOff[6] = x4p; leftOff[7] = y4p; x1p = x1 - offset[0][0]; y1p = y1 - offset[0][1]; xi = xi - 2 * offset[1][0]; yi = yi - 2 * offset[1][1]; x4p = x4 - offset[2][0]; y4p = y4 - offset[2][1]; two_pi_m_p1_m_p4x = 2*xi - x1p - x4p; two_pi_m_p1_m_p4y = 2*yi - y1p - y4p; c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y); c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x); x2p = x1p + c1*dx1; y2p = y1p + c1*dy1; x3p = x4p + c2*dx4; y3p = y4p + c2*dy4; rightOff[0] = x1p; rightOff[1] = y1p; rightOff[2] = x2p; rightOff[3] = y2p; rightOff[4] = x3p; rightOff[5] = y3p; rightOff[6] = x4p; rightOff[7] = y4p; return 8; } // return the kind of curve in the right and left arrays. private int computeOffsetQuad(float[] pts, final int off, float[] leftOff, float[] rightOff) { final float x1 = pts[off + 0], y1 = pts[off + 1]; final float x2 = pts[off + 2], y2 = pts[off + 3]; final float x3 = pts[off + 4], y3 = pts[off + 5]; final float dx3 = x3 - x2; final float dy3 = y3 - y2; final float dx1 = x2 - x1; final float dy1 = y2 - y1; // this computes the offsets at t = 0, 1 computeOffset(dx1, dy1, lineWidth2, offset[0]); computeOffset(dx3, dy3, lineWidth2, offset[1]); leftOff[0] = x1 + offset[0][0]; leftOff[1] = y1 + offset[0][1]; leftOff[4] = x3 + offset[1][0]; leftOff[5] = y3 + offset[1][1]; rightOff[0] = x1 - offset[0][0]; rightOff[1] = y1 - offset[0][1]; rightOff[4] = x3 - offset[1][0]; rightOff[5] = y3 - offset[1][1]; float x1p = leftOff[0]; // start float y1p = leftOff[1]; // point float x3p = leftOff[4]; // end float y3p = leftOff[5]; // point // Corner cases: // 1. If the two control vectors are parallel, we'll end up with NaN's // in leftOff (and rightOff in the body of the if below), so we'll // do getLineOffsets, which is right. // 2. If the first or second two points are equal, then (dx1,dy1)==(0,0) // or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1) // or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that // computeIntersection will put NaN's in leftOff and right off, and // we will do getLineOffsets, which is right. computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2); float cx = leftOff[2]; float cy = leftOff[3]; if (!(isFinite(cx) && isFinite(cy))) { // maybe the right path is not degenerate. x1p = rightOff[0]; y1p = rightOff[1]; x3p = rightOff[4]; y3p = rightOff[5]; computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2); cx = rightOff[2]; cy = rightOff[3]; if (!(isFinite(cx) && isFinite(cy))) { // both are degenerate. This curve is a line. getLineOffsets(x1, y1, x3, y3, leftOff, rightOff); return 4; } // {left,right}Off[0,1,4,5] are already set to the correct values. leftOff[2] = 2*x2 - cx; leftOff[3] = 2*y2 - cy; return 6; } // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2)) // == 2*(x2, y2) - (left_x2, left_y2) rightOff[2] = 2*x2 - cx; rightOff[3] = 2*y2 - cy; return 6; } private static boolean isFinite(float x) { return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY); } // This is where the curve to be processed is put. We give it // enough room to store 2 curves: one for the current subdivision, the // other for the rest of the curve. private float[] middle = new float[2*8]; private float[] lp = new float[8]; private float[] rp = new float[8]; private static final int MAX_N_CURVES = 11; private float[] subdivTs = new float[MAX_N_CURVES - 1]; // If this class is compiled with ecj, then Hotspot crashes when OSR // compiling this function. See bugs 7004570 and 6675699 // TODO: until those are fixed, we should work around that by // manually inlining this into curveTo and quadTo./******************************* WORKAROUND ********************************** private void somethingTo(final int type) { // need these so we can update the state at the end of this method final float xf = middle[type-2], yf = middle[type-1]; float dxs = middle[2] - middle[0]; float dys = middle[3] - middle[1]; float dxf = middle[type - 2] - middle[type - 4]; float dyf = middle[type - 1] - middle[type - 3]; switch(type) { case 6: if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) { dxs = dxf = middle[4] - middle[0]; dys = dyf = middle[5] - middle[1]; } break; case 8: boolean p1eqp2 = (dxs == 0f && dys == 0f); boolean p3eqp4 = (dxf == 0f && dyf == 0f); if (p1eqp2) { dxs = middle[4] - middle[0]; dys = middle[5] - middle[1]; if (dxs == 0f && dys == 0f) { dxs = middle[6] - middle[0]; dys = middle[7] - middle[1]; } } if (p3eqp4) { dxf = middle[6] - middle[2]; dyf = middle[7] - middle[3]; if (dxf == 0f && dyf == 0f) { dxf = middle[6] - middle[0]; dyf = middle[7] - middle[1]; } } } if (dxs == 0f && dys == 0f) { // this happens iff the "curve" is just a point lineTo(middle[0], middle[1]); return; } // if these vectors are too small, normalize them, to avoid future // precision problems. if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) { float len = (float) sqrt(dxs*dxs + dys*dys); dxs /= len; dys /= len; } if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) { float len = (float) sqrt(dxf*dxf + dyf*dyf); dxf /= len; dyf /= len; } computeOffset(dxs, dys, lineWidth2, offset[0]); final float mx = offset[0][0]; final float my = offset[0][1]; drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my); int nSplits = findSubdivPoints(middle, subdivTs, type, lineWidth2); int kind = 0; Iterator<Integer> it = Curve.breakPtsAtTs(middle, type, subdivTs, nSplits); while(it.hasNext()) { int curCurveOff = it.next(); switch (type) { case 8: kind = computeOffsetCubic(middle, curCurveOff, lp, rp); break; case 6: kind = computeOffsetQuad(middle, curCurveOff, lp, rp); break; } emitLineTo(lp[0], lp[1]); switch(kind) { case 8: emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false); emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true); break; case 6: emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false); emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true); break; case 4: emitLineTo(lp[2], lp[3]); emitLineTo(rp[0], rp[1], true); break; } emitLineTo(rp[kind - 2], rp[kind - 1], true); } this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2; this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2; this.cdx = dxf; this.cdy = dyf; this.cx0 = xf; this.cy0 = yf; this.prev = DRAWING_OP_TO; }****************************** END WORKAROUND *******************************/ // finds values of t where the curve in pts should be subdivided in order // to get good offset curves a distance of w away from the middle curve. // Stores the points in ts, and returns how many of them there were. private static Curve c = new Curve(); private static int findSubdivPoints(float[] pts, float[] ts, final int type, final float w) { final float x12 = pts[2] - pts[0]; final float y12 = pts[3] - pts[1]; // if the curve is already parallel to either axis we gain nothing // from rotating it. if (y12 != 0f && x12 != 0f) { // we rotate it so that the first vector in the control polygon is // parallel to the x-axis. This will ensure that rotated quarter // circles won't be subdivided. final float hypot = (float) sqrt(x12 * x12 + y12 * y12); final float cos = x12 / hypot; final float sin = y12 / hypot; final float x1 = cos * pts[0] + sin * pts[1]; final float y1 = cos * pts[1] - sin * pts[0]; final float x2 = cos * pts[2] + sin * pts[3]; final float y2 = cos * pts[3] - sin * pts[2]; final float x3 = cos * pts[4] + sin * pts[5]; final float y3 = cos * pts[5] - sin * pts[4]; switch(type) { case 8: final float x4 = cos * pts[6] + sin * pts[7]; final float y4 = cos * pts[7] - sin * pts[6]; c.set(x1, y1, x2, y2, x3, y3, x4, y4); break; case 6: c.set(x1, y1, x2, y2, x3, y3); break; } } else { c.set(pts, type); } int ret = 0; // we subdivide at values of t such that the remaining rotated // curves are monotonic in x and y. ret += c.dxRoots(ts, ret); ret += c.dyRoots(ts, ret); // subdivide at inflection points. if (type == 8) { // quadratic curves can't have inflection points ret += c.infPoints(ts, ret); } // now we must subdivide at points where one of the offset curves will have // a cusp. This happens at ts where the radius of curvature is equal to w. ret += c.rootsOfROCMinusW(ts, ret, w, 0.0001f); ret = Helpers.filterOutNotInAB(ts, 0, ret, 0.0001f, 0.9999f); Helpers.isort(ts, 0, ret); return ret; } @Override public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) { middle[0] = cx0; middle[1] = cy0; middle[2] = x1; middle[3] = y1; middle[4] = x2; middle[5] = y2; middle[6] = x3; middle[7] = y3; // inlined version of somethingTo(8); // See the TODO on somethingTo // need these so we can update the state at the end of this method final float xf = middle[6], yf = middle[7]; float dxs = middle[2] - middle[0]; float dys = middle[3] - middle[1]; float dxf = middle[6] - middle[4]; float dyf = middle[7] - middle[5]; boolean p1eqp2 = (dxs == 0f && dys == 0f); boolean p3eqp4 = (dxf == 0f && dyf == 0f); if (p1eqp2) { dxs = middle[4] - middle[0]; dys = middle[5] - middle[1]; if (dxs == 0f && dys == 0f) { dxs = middle[6] - middle[0]; dys = middle[7] - middle[1]; } } if (p3eqp4) { dxf = middle[6] - middle[2]; dyf = middle[7] - middle[3]; if (dxf == 0f && dyf == 0f) { dxf = middle[6] - middle[0]; dyf = middle[7] - middle[1]; } } if (dxs == 0f && dys == 0f) { // this happens iff the "curve" is just a point lineTo(middle[0], middle[1]); return; } // if these vectors are too small, normalize them, to avoid future // precision problems. if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) { float len = (float) sqrt(dxs*dxs + dys*dys); dxs /= len; dys /= len; } if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) { float len = (float) sqrt(dxf*dxf + dyf*dyf); dxf /= len; dyf /= len; } computeOffset(dxs, dys, lineWidth2, offset[0]); final float mx = offset[0][0]; final float my = offset[0][1]; drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my); int nSplits = findSubdivPoints(middle, subdivTs, 8, lineWidth2);Lines 501–1000 of 1223. Previous lines Load more
if (prev != DRAWING_OP_TO) { emitMoveTo(x0 + mx, y0 + my); this.sdx = dx; this.sdy = dy; this.smx = mx; this.smy = my; } else { boolean cw = isCW(pdx, pdy, dx, dy); if (joinStyle == JOIN_MITER) { drawMiter(pdx, pdy, x0, y0, dx, dy, omx, omy, mx, my, cw); } else if (joinStyle == JOIN_ROUND) { drawRoundJoin(x0, y0, omx, omy, mx, my, cw, ROUND_JOIN_THRESHOLD); } emitLineTo(x0, y0, !cw); } prev = DRAWING_OP_TO; } private static boolean within(final float x1, final float y1, final float x2, final float y2, final float ERR) { assert ERR > 0 : ""; // compare taxicab distance. ERR will always be small, so using // true distance won't give much benefit return (Helpers.within(x1, x2, ERR) && // we want to avoid calling Math.abs Helpers.within(y1, y2, ERR)); // this is just as good. } private void getLineOffsets(float x1, float y1, float x2, float y2, float[] left, float[] right) { computeOffset(x2 - x1, y2 - y1, lineWidth2, offset[0]); left[0] = x1 + offset[0][0]; left[1] = y1 + offset[0][1]; left[2] = x2 + offset[0][0]; left[3] = y2 + offset[0][1]; right[0] = x1 - offset[0][0]; right[1] = y1 - offset[0][1]; right[2] = x2 - offset[0][0]; right[3] = y2 - offset[0][1]; } private int computeOffsetCubic(float[] pts, final int off, float[] leftOff, float[] rightOff) { // if p1=p2 or p3=p4 it means that the derivative at the endpoint // vanishes, which creates problems with computeOffset. Usually // this happens when this stroker object is trying to winden // a curve with a cusp. What happens is that curveTo splits // the input curve at the cusp, and passes it to this function. // because of inaccuracies in the splitting, we consider points // equal if they're very close to each other. final float x1 = pts[off + 0], y1 = pts[off + 1]; final float x2 = pts[off + 2], y2 = pts[off + 3]; final float x3 = pts[off + 4], y3 = pts[off + 5]; final float x4 = pts[off + 6], y4 = pts[off + 7]; float dx4 = x4 - x3; float dy4 = y4 - y3; float dx1 = x2 - x1; float dy1 = y2 - y1; // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4, // in which case ignore if p1 == p2 final boolean p1eqp2 = within(x1,y1,x2,y2, 6 * ulp(y2)); final boolean p3eqp4 = within(x3,y3,x4,y4, 6 * ulp(y4)); if (p1eqp2 && p3eqp4) { getLineOffsets(x1, y1, x4, y4, leftOff, rightOff); return 4; } else if (p1eqp2) { dx1 = x3 - x1; dy1 = y3 - y1; } else if (p3eqp4) { dx4 = x4 - x2; dy4 = y4 - y2; } // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line float dotsq = (dx1 * dx4 + dy1 * dy4); dotsq = dotsq * dotsq; float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4; if (Helpers.within(dotsq, l1sq * l4sq, 4 * ulp(dotsq))) { getLineOffsets(x1, y1, x4, y4, leftOff, rightOff); return 4; } // What we're trying to do in this function is to approximate an ideal // offset curve (call it I) of the input curve B using a bezier curve Bp. // The constraints I use to get the equations are: // // 1. The computed curve Bp should go through I(0) and I(1). These are // x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find // 4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p). // // 2. Bp should have slope equal in absolute value to I at the endpoints. So, // (by the way, the operator || in the comments below means "aligned with". // It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that // vectors I'(0) and Bp'(0) are aligned, which is the same as saying // that the tangent lines of I and Bp at 0 are parallel. Mathematically // this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some // nonzero constant.) // I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and // I'(1) || B'(1); therefore, Bp'(0) || B'(0) and Bp'(1) || B'(1). // We know that Bp'(0) || (p2p-p1p) and Bp'(1) || (p4p-p3p) and the same // is true for any bezier curve; therefore, we get the equations // (1) p2p = c1 * (p2-p1) + p1p // (2) p3p = c2 * (p4-p3) + p4p // We know p1p, p4p, p2, p1, p3, and p4; therefore, this reduces the number // of unknowns from 4 to 2 (i.e. just c1 and c2). // To eliminate these 2 unknowns we use the following constraint: // // 3. Bp(0.5) == I(0.5). Bp(0.5)=(x,y) and I(0.5)=(xi,yi), and I should note // that I(0.5) is *the only* reason for computing dxm,dym. This gives us // (3) Bp(0.5) = (p1p + 3 * (p2p + p3p) + p4p)/8, which is equivalent to // (4) p2p + p3p = (Bp(0.5)*8 - p1p - p4p) / 3 // We can substitute (1) and (2) from above into (4) and we get: // (5) c1*(p2-p1) + c2*(p4-p3) = (Bp(0.5)*8 - p1p - p4p)/3 - p1p - p4p // which is equivalent to // (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p) // // The right side of this is a 2D vector, and we know I(0.5), which gives us // Bp(0.5), which gives us the value of the right side. // The left side is just a matrix vector multiplication in disguise. It is // // [x2-x1, x4-x3][c1] // [y2-y1, y4-y3][c2] // which, is equal to // [dx1, dx4][c1] // [dy1, dy4][c2] // At this point we are left with a simple linear system and we solve it by // getting the inverse of the matrix above. Then we use [c1,c2] to compute // p2p and p3p. float x = 0.125f * (x1 + 3 * (x2 + x3) + x4); float y = 0.125f * (y1 + 3 * (y2 + y3) + y4); // (dxm,dym) is some tangent of B at t=0.5. This means it's equal to // c*B'(0.5) for some constant c. float dxm = x3 + x4 - x1 - x2, dym = y3 + y4 - y1 - y2; // this computes the offsets at t=0, 0.5, 1, using the property that // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to // the (dx/dt, dy/dt) vectors at the endpoints. computeOffset(dx1, dy1, lineWidth2, offset[0]); computeOffset(dxm, dym, lineWidth2, offset[1]); computeOffset(dx4, dy4, lineWidth2, offset[2]); float x1p = x1 + offset[0][0]; // start float y1p = y1 + offset[0][1]; // point float xi = x + offset[1][0]; // interpolation float yi = y + offset[1][1]; // point float x4p = x4 + offset[2][0]; // end float y4p = y4 + offset[2][1]; // point float invdet43 = 4f / (3f * (dx1 * dy4 - dy1 * dx4)); float two_pi_m_p1_m_p4x = 2*xi - x1p - x4p; float two_pi_m_p1_m_p4y = 2*yi - y1p - y4p; float c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y); float c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x); float x2p, y2p, x3p, y3p; x2p = x1p + c1*dx1; y2p = y1p + c1*dy1; x3p = x4p + c2*dx4; y3p = y4p + c2*dy4; leftOff[0] = x1p; leftOff[1] = y1p; leftOff[2] = x2p; leftOff[3] = y2p; leftOff[4] = x3p; leftOff[5] = y3p; leftOff[6] = x4p; leftOff[7] = y4p; x1p = x1 - offset[0][0]; y1p = y1 - offset[0][1]; xi = xi - 2 * offset[1][0]; yi = yi - 2 * offset[1][1]; x4p = x4 - offset[2][0]; y4p = y4 - offset[2][1]; two_pi_m_p1_m_p4x = 2*xi - x1p - x4p; two_pi_m_p1_m_p4y = 2*yi - y1p - y4p; c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y); c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x); x2p = x1p + c1*dx1; y2p = y1p + c1*dy1; x3p = x4p + c2*dx4; y3p = y4p + c2*dy4; rightOff[0] = x1p; rightOff[1] = y1p; rightOff[2] = x2p; rightOff[3] = y2p; rightOff[4] = x3p; rightOff[5] = y3p; rightOff[6] = x4p; rightOff[7] = y4p; return 8; } // return the kind of curve in the right and left arrays. private int computeOffsetQuad(float[] pts, final int off, float[] leftOff, float[] rightOff) { final float x1 = pts[off + 0], y1 = pts[off + 1]; final float x2 = pts[off + 2], y2 = pts[off + 3]; final float x3 = pts[off + 4], y3 = pts[off + 5]; final float dx3 = x3 - x2; final float dy3 = y3 - y2; final float dx1 = x2 - x1; final float dy1 = y2 - y1; // this computes the offsets at t = 0, 1 computeOffset(dx1, dy1, lineWidth2, offset[0]); computeOffset(dx3, dy3, lineWidth2, offset[1]); leftOff[0] = x1 + offset[0][0]; leftOff[1] = y1 + offset[0][1]; leftOff[4] = x3 + offset[1][0]; leftOff[5] = y3 + offset[1][1]; rightOff[0] = x1 - offset[0][0]; rightOff[1] = y1 - offset[0][1]; rightOff[4] = x3 - offset[1][0]; rightOff[5] = y3 - offset[1][1]; float x1p = leftOff[0]; // start float y1p = leftOff[1]; // point float x3p = leftOff[4]; // end float y3p = leftOff[5]; // point // Corner cases: // 1. If the two control vectors are parallel, we'll end up with NaN's // in leftOff (and rightOff in the body of the if below), so we'll // do getLineOffsets, which is right. // 2. If the first or second two points are equal, then (dx1,dy1)==(0,0) // or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1) // or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that // computeIntersection will put NaN's in leftOff and right off, and // we will do getLineOffsets, which is right. computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2); float cx = leftOff[2]; float cy = leftOff[3]; if (!(isFinite(cx) && isFinite(cy))) { // maybe the right path is not degenerate. x1p = rightOff[0]; y1p = rightOff[1]; x3p = rightOff[4]; y3p = rightOff[5]; computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2); cx = rightOff[2]; cy = rightOff[3]; if (!(isFinite(cx) && isFinite(cy))) { // both are degenerate. This curve is a line. getLineOffsets(x1, y1, x3, y3, leftOff, rightOff); return 4; } // {left,right}Off[0,1,4,5] are already set to the correct values. leftOff[2] = 2*x2 - cx; leftOff[3] = 2*y2 - cy; return 6; } // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2)) // == 2*(x2, y2) - (left_x2, left_y2) rightOff[2] = 2*x2 - cx; rightOff[3] = 2*y2 - cy; return 6; } private static boolean isFinite(float x) { return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY); } // This is where the curve to be processed is put. We give it // enough room to store 2 curves: one for the current subdivision, the // other for the rest of the curve. private float[] middle = new float[2*8]; private float[] lp = new float[8]; private float[] rp = new float[8]; private static final int MAX_N_CURVES = 11; private float[] subdivTs = new float[MAX_N_CURVES - 1]; // If this class is compiled with ecj, then Hotspot crashes when OSR // compiling this function. See bugs 7004570 and 6675699 // TODO: until those are fixed, we should work around that by // manually inlining this into curveTo and quadTo. /******************************* WORKAROUND ********************************** private void somethingTo(final int type) { // need these so we can update the state at the end of this method final float xf = middle[type-2], yf = middle[type-1]; float dxs = middle[2] - middle[0]; float dys = middle[3] - middle[1]; float dxf = middle[type - 2] - middle[type - 4]; float dyf = middle[type - 1] - middle[type - 3]; switch(type) { case 6: if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) { dxs = dxf = middle[4] - middle[0]; dys = dyf = middle[5] - middle[1]; } break; case 8: boolean p1eqp2 = (dxs == 0f && dys == 0f); boolean p3eqp4 = (dxf == 0f && dyf == 0f); if (p1eqp2) { dxs = middle[4] - middle[0]; dys = middle[5] - middle[1]; if (dxs == 0f && dys == 0f) { dxs = middle[6] - middle[0]; dys = middle[7] - middle[1]; } } if (p3eqp4) { dxf = middle[6] - middle[2]; dyf = middle[7] - middle[3]; if (dxf == 0f && dyf == 0f) { dxf = middle[6] - middle[0]; dyf = middle[7] - middle[1]; } } } if (dxs == 0f && dys == 0f) { // this happens iff the "curve" is just a point lineTo(middle[0], middle[1]); return; } // if these vectors are too small, normalize them, to avoid future // precision problems. if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) { float len = (float) sqrt(dxs*dxs + dys*dys); dxs /= len; dys /= len; } if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) { float len = (float) sqrt(dxf*dxf + dyf*dyf); dxf /= len; dyf /= len; } computeOffset(dxs, dys, lineWidth2, offset[0]); final float mx = offset[0][0]; final float my = offset[0][1]; drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my); int nSplits = findSubdivPoints(middle, subdivTs, type, lineWidth2); int kind = 0; Iterator<Integer> it = Curve.breakPtsAtTs(middle, type, subdivTs, nSplits); while(it.hasNext()) { int curCurveOff = it.next(); switch (type) { case 8: kind = computeOffsetCubic(middle, curCurveOff, lp, rp); break; case 6: kind = computeOffsetQuad(middle, curCurveOff, lp, rp); break; } emitLineTo(lp[0], lp[1]); switch(kind) { case 8: emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false); emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true); break; case 6: emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false); emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true); break; case 4: emitLineTo(lp[2], lp[3]); emitLineTo(rp[0], rp[1], true); break; } emitLineTo(rp[kind - 2], rp[kind - 1], true); } this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2; this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2; this.cdx = dxf; this.cdy = dyf; this.cx0 = xf; this.cy0 = yf; this.prev = DRAWING_OP_TO; } ****************************** END WORKAROUND *******************************/ // finds values of t where the curve in pts should be subdivided in order // to get good offset curves a distance of w away from the middle curve. // Stores the points in ts, and returns how many of them there were. private static Curve c = new Curve(); private static int findSubdivPoints(float[] pts, float[] ts, final int type, final float w) { final float x12 = pts[2] - pts[0]; final float y12 = pts[3] - pts[1]; // if the curve is already parallel to either axis we gain nothing // from rotating it. if (y12 != 0f && x12 != 0f) { // we rotate it so that the first vector in the control polygon is // parallel to the x-axis. This will ensure that rotated quarter // circles won't be subdivided. final float hypot = (float) sqrt(x12 * x12 + y12 * y12); final float cos = x12 / hypot; final float sin = y12 / hypot; final float x1 = cos * pts[0] + sin * pts[1]; final float y1 = cos * pts[1] - sin * pts[0]; final float x2 = cos * pts[2] + sin * pts[3]; final float y2 = cos * pts[3] - sin * pts[2]; final float x3 = cos * pts[4] + sin * pts[5]; final float y3 = cos * pts[5] - sin * pts[4]; switch(type) { case 8: final float x4 = cos * pts[6] + sin * pts[7]; final float y4 = cos * pts[7] - sin * pts[6]; c.set(x1, y1, x2, y2, x3, y3, x4, y4); break; case 6: c.set(x1, y1, x2, y2, x3, y3); break; } } else { c.set(pts, type); } int ret = 0; // we subdivide at values of t such that the remaining rotated // curves are monotonic in x and y. ret += c.dxRoots(ts, ret); ret += c.dyRoots(ts, ret); // subdivide at inflection points. if (type == 8) { // quadratic curves can't have inflection points ret += c.infPoints(ts, ret); } // now we must subdivide at points where one of the offset curves will have // a cusp. This happens at ts where the radius of curvature is equal to w. ret += c.rootsOfROCMinusW(ts, ret, w, 0.0001f); ret = Helpers.filterOutNotInAB(ts, 0, ret, 0.0001f, 0.9999f); Helpers.isort(ts, 0, ret); return ret; } @Override public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3) { middle[0] = cx0; middle[1] = cy0; middle[2] = x1; middle[3] = y1; middle[4] = x2; middle[5] = y2; middle[6] = x3; middle[7] = y3; // inlined version of somethingTo(8); // See the TODO on somethingTo // need these so we can update the state at the end of this method final float xf = middle[6], yf = middle[7]; float dxs = middle[2] - middle[0]; float dys = middle[3] - middle[1]; float dxf = middle[6] - middle[4]; float dyf = middle[7] - middle[5]; boolean p1eqp2 = (dxs == 0f && dys == 0f); boolean p3eqp4 = (dxf == 0f && dyf == 0f); if (p1eqp2) { dxs = middle[4] - middle[0]; dys = middle[5] - middle[1]; if (dxs == 0f && dys == 0f) { dxs = middle[6] - middle[0]; dys = middle[7] - middle[1]; } } if (p3eqp4) { dxf = middle[6] - middle[2]; dyf = middle[7] - middle[3]; if (dxf == 0f && dyf == 0f) { dxf = middle[6] - middle[0]; dyf = middle[7] - middle[1]; } } if (dxs == 0f && dys == 0f) { // this happens iff the "curve" is just a point lineTo(middle[0], middle[1]); return; } // if these vectors are too small, normalize them, to avoid future // precision problems. if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) { float len = (float) sqrt(dxs*dxs + dys*dys); dxs /= len; dys /= len; } if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) { float len = (float) sqrt(dxf*dxf + dyf*dyf); dxf /= len; dyf /= len; } computeOffset(dxs, dys, lineWidth2, offset[0]); final float mx = offset[0][0]; final float my = offset[0][1]; drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my); int nSplits = findSubdivPoints(middle, subdivTs, 8, lineWidth2);