3909N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 2956N/A// TODO: some of the arithmetic here is too verbose and prone to hard to 2956N/A// has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such 0N/A * Constant value for join style. 0N/A * Constant value for join style. 0N/A * Constant value for join style. 0N/A * Constant value for end cap style. 0N/A * Constant value for end cap style. 0N/A * Constant value for end cap style. 2956N/A // The starting point of the path, and the slope there. 2956N/A // the current point and the slope there. 2956N/A // vectors that when added to (sx0,sy0) and (cx0,cy0) respectively yield the 2956N/A // first and last points on the left parallel path. Since this path is 2956N/A // parallel, it's slope at any point is parallel to the slope of the 2956N/A // original path (thought they may have different directions), so these 2956N/A // could be computed from sdx,sdy and cdx,cdy (and vice versa), but that 2956N/A // would be error prone and hard to read, so we keep these anyway. 0N/A * Constructs a <code>Stroker</code>. 2956N/A * @param pc2d an output <code>PathConsumer2D</code>. 2638N/A * @param lineWidth the desired line width in pixels 0N/A * @param capStyle the desired end cap style, one of 0N/A * <code>CAP_BUTT</code>, <code>CAP_ROUND</code> or 0N/A * <code>CAP_SQUARE</code>. 0N/A * @param joinStyle the desired line join style, one of 0N/A * <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or 0N/A * <code>JOIN_BEVEL</code>. 2638N/A * @param miterLimit the desired miter limit 2956N/A final float w,
final float[] m)
2956N/A // Returns true if the vectors (dx1, dy1) and (dx2, dy2) are 2956N/A // clockwise (if dx1,dy1 needs to be rotated clockwise to close 2956N/A // the smallest angle between it and dx2,dy2). 2956N/A // This is equivalent to detecting whether a point q is on the right side 2956N/A // of a line passing through points p1, p2 where p2 = p1+(dx1,dy1) and 2956N/A // q = p2+(dx2,dy2), which is the same as saying p1, p2, q are in a 2956N/A // NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left. 2638N/A // pisces used to use fixed point arithmetic with 16 decimal digits. I 2956N/A // didn't want to change the values of the constant below when I converted 2638N/A // it to floating point, so that's why the divisions by 2^16 are there. 2956N/A // The sign of the dot product of mx,my and omx,omy is equal to the 2956N/A // the sign of the cosine of ext 2956N/A // (ext is the angle between omx,omy and mx,my). 2956N/A // If it is >=0, we know that abs(ext) is <= 90 degrees, so we only 2956N/A // need 1 curve to approximate the circle section that joins omx,omy 2956N/A // we need to split the arc into 2 arcs spanning the same angle. 2956N/A // The point we want will be one of the 2 intersections of the 2956N/A // perpendicular bisector of the chord (omx,omy)->(mx,my) and the 2956N/A // circle. We could find this by scaling the vector 2956N/A // (omx+mx, omy+my)/2 so that it has length=lineWidth2 (and thus lies 2956N/A // on the circle), but that can have numerical problems when the angle 2956N/A // between omx,omy and mx,my is close to 180 degrees. So we compute a 2956N/A // normal of (omx,omy)-(mx,my). This will be the direction of the 2956N/A // perpendicular bisector. To get one of the intersections, we just scale 2956N/A // this vector that its length is lineWidth2 (this works because the 2956N/A // perpendicular bisector goes through the origin). This scaling doesn't 2956N/A // have numerical problems because we know that lineWidth2 divided by 2956N/A // this normal's length is at least 0.5 and at most sqrt(2)/2 (because 2956N/A // we know the angle of the arc is > 90 degrees). 2956N/A // if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've 2956N/A // computed the wrong intersection so we get the other one. 2956N/A // The test above is equivalent to if (rev). 2956N/A // the input arc defined by omx,omy and mx,my must span <= 90 degrees. 2956N/A // cv is the length of P1-P0 and P2-P3 divided by the radius of the arc 2956N/A // (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that 2956N/A // define the bezier curve we're computing. 2956N/A // It is computed using the constraints that P1-P0 and P3-P2 are parallel 2956N/A // to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|. 2956N/A // if clockwise, we need to negate cv. 2956N/A if (
rev) {
// rev is equivalent to isCW(omx, omy, mx, my) 2956N/A final float C =
0.5522847498307933f;
2956N/A // the first and second arguments of the following two calls 2956N/A // are really will be ignored by emitCurveTo (because of the false), 2956N/A // but we put them in anyway, as opposed to just giving it 4 zeroes, 2956N/A // because it's just 4 additions and it's not good to rely on this 2956N/A // sort of assumption (right now it's true, but that may change). 4066N/A // Put the intersection point of the lines (x0, y0) -> (x1, y1) 4066N/A // and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1]. 4066N/A // If the lines are parallel, it will put a non finite number in m. 4066N/A // If the lines are parallel, lenSq will be either NaN or +inf 4066N/A // (actually, I'm not sure if the latter is possible. The important 4066N/A // thing is that -inf is not possible, because lenSq is a square). 4066N/A // For both of those values, the comparison below will fail and 4066N/A // no miter will be drawn, which is correct. 2956N/A // this shouldn't matter since this object won't be used 2956N/A // after the call to this method. 2956N/A drawMiter(
pdx,
pdy,
x0,
y0,
dx,
dy,
omx,
omy,
mx,
my,
cw);
2956N/A // compare taxicab distance. ERR will always be small, so using 2956N/A // true distance won't give much benefit 2956N/A // if p1=p2 or p3=p4 it means that the derivative at the endpoint 2956N/A // vanishes, which creates problems with computeOffset. Usually 2956N/A // this happens when this stroker object is trying to winden 2956N/A // a curve with a cusp. What happens is that curveTo splits 2956N/A // the input curve at the cusp, and passes it to this function. 2956N/A // because of inaccuracies in the splitting, we consider points 2956N/A // equal if they're very close to each other. 2956N/A // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4, 2956N/A // in which case ignore if p1 == p2 2956N/A // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line 2956N/A// What we're trying to do in this function is to approximate an ideal 2956N/A// offset curve (call it I) of the input curve B using a bezier curve Bp. 2956N/A// The constraints I use to get the equations are: 2956N/A// 1. The computed curve Bp should go through I(0) and I(1). These are 2956N/A// x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find 2956N/A// 4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p). 2956N/A// 2. Bp should have slope equal in absolute value to I at the endpoints. So, 2956N/A// (by the way, the operator || in the comments below means "aligned with". 2956N/A// It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that 2956N/A// vectors I'(0) and Bp'(0) are aligned, which is the same as saying 2956N/A// that the tangent lines of I and Bp at 0 are parallel. Mathematically 2956N/A// this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some 2956N/A// I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and 2956N/A// I'(1) || B'(1); therefore, Bp'(0) || B'(0) and Bp'(1) || B'(1). 2956N/A// We know that Bp'(0) || (p2p-p1p) and Bp'(1) || (p4p-p3p) and the same 2956N/A// is true for any bezier curve; therefore, we get the equations 2956N/A// (1) p2p = c1 * (p2-p1) + p1p 2956N/A// (2) p3p = c2 * (p4-p3) + p4p 2956N/A// We know p1p, p4p, p2, p1, p3, and p4; therefore, this reduces the number 2956N/A// of unknowns from 4 to 2 (i.e. just c1 and c2). 2956N/A// To eliminate these 2 unknowns we use the following constraint: 2956N/A// 3. Bp(0.5) == I(0.5). Bp(0.5)=(x,y) and I(0.5)=(xi,yi), and I should note 2956N/A// that I(0.5) is *the only* reason for computing dxm,dym. This gives us 2956N/A// (3) Bp(0.5) = (p1p + 3 * (p2p + p3p) + p4p)/8, which is equivalent to 2956N/A// (4) p2p + p3p = (Bp(0.5)*8 - p1p - p4p) / 3 2956N/A// We can substitute (1) and (2) from above into (4) and we get: 2956N/A// (5) c1*(p2-p1) + c2*(p4-p3) = (Bp(0.5)*8 - p1p - p4p)/3 - p1p - p4p 2956N/A// (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p) 2956N/A// The right side of this is a 2D vector, and we know I(0.5), which gives us 2956N/A// Bp(0.5), which gives us the value of the right side. 2956N/A// The left side is just a matrix vector multiplication in disguise. It is 2956N/A// At this point we are left with a simple linear system and we solve it by 2956N/A// getting the inverse of the matrix above. Then we use [c1,c2] to compute 2956N/A // (dxm,dym) is some tangent of B at t=0.5. This means it's equal to 2956N/A // c*B'(0.5) for some constant c. 2956N/A // this computes the offsets at t=0, 0.5, 1, using the property that 2956N/A // for any bezier curve the vectors p2-p1 and p4-p3 are parallel to 2956N/A // return the kind of curve in the right and left arrays. 4066N/A // this computes the offsets at t = 0, 1 4066N/A // 1. If the two control vectors are parallel, we'll end up with NaN's 4066N/A // in leftOff (and rightOff in the body of the if below), so we'll 4066N/A // do getLineOffsets, which is right. 4066N/A // 2. If the first or second two points are equal, then (dx1,dy1)==(0,0) 4066N/A // or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1) 4066N/A // or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that 4066N/A // computeIntersection will put NaN's in leftOff and right off, and 4066N/A // we will do getLineOffsets, which is right. 4066N/A // maybe the right path is not degenerate. 4066N/A // both are degenerate. This curve is a line. 4066N/A // {left,right}Off[0,1,4,5] are already set to the correct values. 4066N/A // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2)) 4066N/A // == 2*(x2, y2) - (left_x2, left_y2) 2956N/A // This is where the curve to be processed is put. We give it 2956N/A // enough room to store 2 curves: one for the current subdivision, the 2956N/A // other for the rest of the curve. 3719N/A // If this class is compiled with ecj, then Hotspot crashes when OSR 3719N/A // compiling this function. See bugs 7004570 and 6675699 3719N/A // TODO: until those are fixed, we should work around that by 3719N/A // manually inlining this into curveTo and quadTo. 3719N/A/******************************* WORKAROUND ********************************** 2956N/A private void somethingTo(final int type) { 2956N/A // need these so we can update the state at the end of this method 3444N/A final float xf = middle[type-2], yf = middle[type-1]; 3444N/A float dxs = middle[2] - middle[0]; 3444N/A float dys = middle[3] - middle[1]; 3444N/A float dxf = middle[type - 2] - middle[type - 4]; 3444N/A float dyf = middle[type - 1] - middle[type - 3]; 2956N/A if ((dxs == 0f && dys == 0f) || 2956N/A (dxf == 0f && dyf == 0f)) { 3444N/A dxs = dxf = middle[4] - middle[0]; 3444N/A dys = dyf = middle[5] - middle[1]; 2956N/A boolean p1eqp2 = (dxs == 0f && dys == 0f); 2956N/A boolean p3eqp4 = (dxf == 0f && dyf == 0f); 3444N/A dxs = middle[4] - middle[0]; 3444N/A dys = middle[5] - middle[1]; 2956N/A if (dxs == 0f && dys == 0f) { 3444N/A dxs = middle[6] - middle[0]; 3444N/A dys = middle[7] - middle[1]; 3444N/A dxf = middle[6] - middle[2]; 3444N/A dyf = middle[7] - middle[3]; 2956N/A if (dxf == 0f && dyf == 0f) { 3444N/A dxf = middle[6] - middle[0]; 3444N/A dyf = middle[7] - middle[1]; 2956N/A if (dxs == 0f && dys == 0f) { 2956N/A // this happens iff the "curve" is just a point 3444N/A lineTo(middle[0], middle[1]); 2956N/A // if these vectors are too small, normalize them, to avoid future 2956N/A if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) { 4066N/A float len = (float) sqrt(dxs*dxs + dys*dys); 2956N/A if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) { 4066N/A float len = (float) sqrt(dxf*dxf + dyf*dyf); 2956N/A computeOffset(dxs, dys, lineWidth2, offset[0]); 2956N/A final float mx = offset[0][0]; 2956N/A final float my = offset[0][1]; 2956N/A drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my); 3444N/A int nSplits = findSubdivPoints(middle, subdivTs, type, lineWidth2); 3444N/A Iterator<Integer> it = Curve.breakPtsAtTs(middle, type, subdivTs, nSplits); 3444N/A int curCurveOff = it.next(); 3444N/A kind = computeOffsetCubic(middle, curCurveOff, lp, rp); 3444N/A kind = computeOffsetQuad(middle, curCurveOff, lp, rp); 4066N/A emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false); 4066N/A emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true); 4066N/A emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false); 4066N/A emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true); 4066N/A emitLineTo(rp[0], rp[1], true); 4066N/A emitLineTo(rp[kind - 2], rp[kind - 1], true); 2956N/A this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2; 2956N/A this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2; 3719N/A****************************** END WORKAROUND *******************************/ 2956N/A // finds values of t where the curve in pts should be subdivided in order 2956N/A // to get good offset curves a distance of w away from the middle curve. 2956N/A // Stores the points in ts, and returns how many of them there were. 2956N/A // if the curve is already parallel to either axis we gain nothing 2956N/A // we rotate it so that the first vector in the control polygon is 2956N/A // parallel to the x-axis. This will ensure that rotated quarter 2956N/A // circles won't be subdivided. 2956N/A // we subdivide at values of t such that the remaining rotated 2956N/A // curves are monotonic in x and y. 2956N/A // subdivide at inflection points. 2956N/A // quadratic curves can't have inflection points 2956N/A // now we must subdivide at points where one of the offset curves will have 2956N/A // a cusp. This happens at ts where the radius of curvature is equal to w. 3719N/A // inlined version of somethingTo(8); 3719N/A // See the TODO on somethingTo 3719N/A // need these so we can update the state at the end of this method 3719N/A // this happens iff the "curve" is just a point 3719N/A // if these vectors are too small, normalize them, to avoid future 3719N/A // inlined version of somethingTo(8); 3719N/A // See the TODO on somethingTo 3719N/A // need these so we can update the state at the end of this method 3719N/A // this happens iff the "curve" is just a point 3719N/A // if these vectors are too small, normalize them, to avoid future 2956N/A // a stack of polynomial curves where each curve shares endpoints with 2956N/A // assert(x0 == lastX && y0 == lastY) 2956N/A // we reverse the coordinate order to make popping easier 2956N/A // assert(x0 == lastX && y0 == lastY) 2956N/A // assert(x0 == lastX && y0 == lastY)