Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pisces/Stroker.java
38918 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.pisces;2627import java.util.Arrays;28import java.util.Iterator;29import static java.lang.Math.ulp;30import static java.lang.Math.sqrt;3132import sun.awt.geom.PathConsumer2D;3334// TODO: some of the arithmetic here is too verbose and prone to hard to35// debug typos. We should consider making a small Point/Vector class that36// has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such37final class Stroker implements PathConsumer2D {3839private static final int MOVE_TO = 0;40private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad41private static final int CLOSE = 2;4243/**44* Constant value for join style.45*/46public static final int JOIN_MITER = 0;4748/**49* Constant value for join style.50*/51public static final int JOIN_ROUND = 1;5253/**54* Constant value for join style.55*/56public static final int JOIN_BEVEL = 2;5758/**59* Constant value for end cap style.60*/61public static final int CAP_BUTT = 0;6263/**64* Constant value for end cap style.65*/66public static final int CAP_ROUND = 1;6768/**69* Constant value for end cap style.70*/71public static final int CAP_SQUARE = 2;7273private final PathConsumer2D out;7475private final int capStyle;76private final int joinStyle;7778private final float lineWidth2;7980private final float[][] offset = new float[3][2];81private final float[] miter = new float[2];82private final float miterLimitSq;8384private int prev;8586// The starting point of the path, and the slope there.87private float sx0, sy0, sdx, sdy;88// the current point and the slope there.89private float cx0, cy0, cdx, cdy; // c stands for current90// vectors that when added to (sx0,sy0) and (cx0,cy0) respectively yield the91// first and last points on the left parallel path. Since this path is92// parallel, it's slope at any point is parallel to the slope of the93// original path (thought they may have different directions), so these94// could be computed from sdx,sdy and cdx,cdy (and vice versa), but that95// would be error prone and hard to read, so we keep these anyway.96private float smx, smy, cmx, cmy;9798private final PolyStack reverse = new PolyStack();99100/**101* Constructs a <code>Stroker</code>.102*103* @param pc2d an output <code>PathConsumer2D</code>.104* @param lineWidth the desired line width in pixels105* @param capStyle the desired end cap style, one of106* <code>CAP_BUTT</code>, <code>CAP_ROUND</code> or107* <code>CAP_SQUARE</code>.108* @param joinStyle the desired line join style, one of109* <code>JOIN_MITER</code>, <code>JOIN_ROUND</code> or110* <code>JOIN_BEVEL</code>.111* @param miterLimit the desired miter limit112*/113public Stroker(PathConsumer2D pc2d,114float lineWidth,115int capStyle,116int joinStyle,117float miterLimit)118{119this.out = pc2d;120121this.lineWidth2 = lineWidth / 2;122this.capStyle = capStyle;123this.joinStyle = joinStyle;124125float limit = miterLimit * lineWidth2;126this.miterLimitSq = limit*limit;127128this.prev = CLOSE;129}130131private static void computeOffset(final float lx, final float ly,132final float w, final float[] m)133{134final float len = (float) sqrt(lx*lx + ly*ly);135if (len == 0) {136m[0] = m[1] = 0;137} else {138m[0] = (ly * w)/len;139m[1] = -(lx * w)/len;140}141}142143// Returns true if the vectors (dx1, dy1) and (dx2, dy2) are144// clockwise (if dx1,dy1 needs to be rotated clockwise to close145// the smallest angle between it and dx2,dy2).146// This is equivalent to detecting whether a point q is on the right side147// of a line passing through points p1, p2 where p2 = p1+(dx1,dy1) and148// q = p2+(dx2,dy2), which is the same as saying p1, p2, q are in a149// clockwise order.150// NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left.151private static boolean isCW(final float dx1, final float dy1,152final float dx2, final float dy2)153{154return dx1 * dy2 <= dy1 * dx2;155}156157// pisces used to use fixed point arithmetic with 16 decimal digits. I158// didn't want to change the values of the constant below when I converted159// it to floating point, so that's why the divisions by 2^16 are there.160private static final float ROUND_JOIN_THRESHOLD = 1000/65536f;161162private void drawRoundJoin(float x, float y,163float omx, float omy, float mx, float my,164boolean rev,165float threshold)166{167if ((omx == 0 && omy == 0) || (mx == 0 && my == 0)) {168return;169}170171float domx = omx - mx;172float domy = omy - my;173float len = domx*domx + domy*domy;174if (len < threshold) {175return;176}177178if (rev) {179omx = -omx;180omy = -omy;181mx = -mx;182my = -my;183}184drawRoundJoin(x, y, omx, omy, mx, my, rev);185}186187private void drawRoundJoin(float cx, float cy,188float omx, float omy,189float mx, float my,190boolean rev)191{192// The sign of the dot product of mx,my and omx,omy is equal to the193// the sign of the cosine of ext194// (ext is the angle between omx,omy and mx,my).195final float cosext = omx * mx + omy * my;196// If it is >=0, we know that abs(ext) is <= 90 degrees, so we only197// need 1 curve to approximate the circle section that joins omx,omy198// and mx,my.199final int numCurves = (cosext >= 0f) ? 1 : 2;200201switch (numCurves) {202case 1:203drawBezApproxForArc(cx, cy, omx, omy, mx, my, rev);204break;205case 2:206// we need to split the arc into 2 arcs spanning the same angle.207// The point we want will be one of the 2 intersections of the208// perpendicular bisector of the chord (omx,omy)->(mx,my) and the209// circle. We could find this by scaling the vector210// (omx+mx, omy+my)/2 so that it has length=lineWidth2 (and thus lies211// on the circle), but that can have numerical problems when the angle212// between omx,omy and mx,my is close to 180 degrees. So we compute a213// normal of (omx,omy)-(mx,my). This will be the direction of the214// perpendicular bisector. To get one of the intersections, we just scale215// this vector that its length is lineWidth2 (this works because the216// perpendicular bisector goes through the origin). This scaling doesn't217// have numerical problems because we know that lineWidth2 divided by218// this normal's length is at least 0.5 and at most sqrt(2)/2 (because219// we know the angle of the arc is > 90 degrees).220float nx = my - omy, ny = omx - mx;221float nlen = (float) sqrt(nx*nx + ny*ny);222float scale = lineWidth2/nlen;223float mmx = nx * scale, mmy = ny * scale;224225// if (isCW(omx, omy, mx, my) != isCW(mmx, mmy, mx, my)) then we've226// computed the wrong intersection so we get the other one.227// The test above is equivalent to if (rev).228if (rev) {229mmx = -mmx;230mmy = -mmy;231}232drawBezApproxForArc(cx, cy, omx, omy, mmx, mmy, rev);233drawBezApproxForArc(cx, cy, mmx, mmy, mx, my, rev);234break;235}236}237238// the input arc defined by omx,omy and mx,my must span <= 90 degrees.239private void drawBezApproxForArc(final float cx, final float cy,240final float omx, final float omy,241final float mx, final float my,242boolean rev)243{244final float cosext2 = (omx * mx + omy * my) / (2f * lineWidth2 * lineWidth2);245246// check round off errors producing cos(ext) > 1 and a NaN below247// cos(ext) == 1 implies colinear segments and an empty join anyway248if (cosext2 >= 0.5f) {249// just return to avoid generating a flat curve:250return;251}252253// cv is the length of P1-P0 and P2-P3 divided by the radius of the arc254// (so, cv assumes the arc has radius 1). P0, P1, P2, P3 are the points that255// define the bezier curve we're computing.256// It is computed using the constraints that P1-P0 and P3-P2 are parallel257// to the arc tangents at the endpoints, and that |P1-P0|=|P3-P2|.258float cv = (float) ((4.0 / 3.0) * sqrt(0.5 - cosext2) /259(1.0 + sqrt(cosext2 + 0.5)));260// if clockwise, we need to negate cv.261if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)262cv = -cv;263}264final float x1 = cx + omx;265final float y1 = cy + omy;266final float x2 = x1 - cv * omy;267final float y2 = y1 + cv * omx;268269final float x4 = cx + mx;270final float y4 = cy + my;271final float x3 = x4 + cv * my;272final float y3 = y4 - cv * mx;273274emitCurveTo(x1, y1, x2, y2, x3, y3, x4, y4, rev);275}276277private void drawRoundCap(float cx, float cy, float mx, float my) {278final float C = 0.5522847498307933f;279// the first and second arguments of the following two calls280// are really will be ignored by emitCurveTo (because of the false),281// but we put them in anyway, as opposed to just giving it 4 zeroes,282// because it's just 4 additions and it's not good to rely on this283// sort of assumption (right now it's true, but that may change).284emitCurveTo(cx+mx, cy+my,285cx+mx-C*my, cy+my+C*mx,286cx-my+C*mx, cy+mx+C*my,287cx-my, cy+mx,288false);289emitCurveTo(cx-my, cy+mx,290cx-my-C*mx, cy+mx-C*my,291cx-mx-C*my, cy-my+C*mx,292cx-mx, cy-my,293false);294}295296// Put the intersection point of the lines (x0, y0) -> (x1, y1)297// and (x0p, y0p) -> (x1p, y1p) in m[off] and m[off+1].298// If the lines are parallel, it will put a non finite number in m.299private void computeIntersection(final float x0, final float y0,300final float x1, final float y1,301final float x0p, final float y0p,302final float x1p, final float y1p,303final float[] m, int off)304{305float x10 = x1 - x0;306float y10 = y1 - y0;307float x10p = x1p - x0p;308float y10p = y1p - y0p;309310float den = x10*y10p - x10p*y10;311float t = x10p*(y0-y0p) - y10p*(x0-x0p);312t /= den;313m[off++] = x0 + t*x10;314m[off] = y0 + t*y10;315}316317private void drawMiter(final float pdx, final float pdy,318final float x0, final float y0,319final float dx, final float dy,320float omx, float omy, float mx, float my,321boolean rev)322{323if ((mx == omx && my == omy) ||324(pdx == 0 && pdy == 0) ||325(dx == 0 && dy == 0))326{327return;328}329330if (rev) {331omx = -omx;332omy = -omy;333mx = -mx;334my = -my;335}336337computeIntersection((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,338(dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,339miter, 0);340341float lenSq = (miter[0]-x0)*(miter[0]-x0) + (miter[1]-y0)*(miter[1]-y0);342343// If the lines are parallel, lenSq will be either NaN or +inf344// (actually, I'm not sure if the latter is possible. The important345// thing is that -inf is not possible, because lenSq is a square).346// For both of those values, the comparison below will fail and347// no miter will be drawn, which is correct.348if (lenSq < miterLimitSq) {349emitLineTo(miter[0], miter[1], rev);350}351}352353public void moveTo(float x0, float y0) {354if (prev == DRAWING_OP_TO) {355finish();356}357this.sx0 = this.cx0 = x0;358this.sy0 = this.cy0 = y0;359this.cdx = this.sdx = 1;360this.cdy = this.sdy = 0;361this.prev = MOVE_TO;362}363364public void lineTo(float x1, float y1) {365float dx = x1 - cx0;366float dy = y1 - cy0;367if (dx == 0f && dy == 0f) {368dx = 1;369}370computeOffset(dx, dy, lineWidth2, offset[0]);371float mx = offset[0][0];372float my = offset[0][1];373374drawJoin(cdx, cdy, cx0, cy0, dx, dy, cmx, cmy, mx, my);375376emitLineTo(cx0 + mx, cy0 + my);377emitLineTo(x1 + mx, y1 + my);378379emitLineTo(cx0 - mx, cy0 - my, true);380emitLineTo(x1 - mx, y1 - my, true);381382this.cmx = mx;383this.cmy = my;384this.cdx = dx;385this.cdy = dy;386this.cx0 = x1;387this.cy0 = y1;388this.prev = DRAWING_OP_TO;389}390391public void closePath() {392if (prev != DRAWING_OP_TO) {393if (prev == CLOSE) {394return;395}396emitMoveTo(cx0, cy0 - lineWidth2);397this.cmx = this.smx = 0;398this.cmy = this.smy = -lineWidth2;399this.cdx = this.sdx = 1;400this.cdy = this.sdy = 0;401finish();402return;403}404405if (cx0 != sx0 || cy0 != sy0) {406lineTo(sx0, sy0);407}408409drawJoin(cdx, cdy, cx0, cy0, sdx, sdy, cmx, cmy, smx, smy);410411emitLineTo(sx0 + smx, sy0 + smy);412413emitMoveTo(sx0 - smx, sy0 - smy);414emitReverse();415416this.prev = CLOSE;417emitClose();418}419420private void emitReverse() {421while(!reverse.isEmpty()) {422reverse.pop(out);423}424}425426public void pathDone() {427if (prev == DRAWING_OP_TO) {428finish();429}430431out.pathDone();432// this shouldn't matter since this object won't be used433// after the call to this method.434this.prev = CLOSE;435}436437private void finish() {438if (capStyle == CAP_ROUND) {439drawRoundCap(cx0, cy0, cmx, cmy);440} else if (capStyle == CAP_SQUARE) {441emitLineTo(cx0 - cmy + cmx, cy0 + cmx + cmy);442emitLineTo(cx0 - cmy - cmx, cy0 + cmx - cmy);443}444445emitReverse();446447if (capStyle == CAP_ROUND) {448drawRoundCap(sx0, sy0, -smx, -smy);449} else if (capStyle == CAP_SQUARE) {450emitLineTo(sx0 + smy - smx, sy0 - smx - smy);451emitLineTo(sx0 + smy + smx, sy0 - smx + smy);452}453454emitClose();455}456457private void emitMoveTo(final float x0, final float y0) {458out.moveTo(x0, y0);459}460461private void emitLineTo(final float x1, final float y1) {462out.lineTo(x1, y1);463}464465private void emitLineTo(final float x1, final float y1,466final boolean rev)467{468if (rev) {469reverse.pushLine(x1, y1);470} else {471emitLineTo(x1, y1);472}473}474475private void emitQuadTo(final float x0, final float y0,476final float x1, final float y1,477final float x2, final float y2, final boolean rev)478{479if (rev) {480reverse.pushQuad(x0, y0, x1, y1);481} else {482out.quadTo(x1, y1, x2, y2);483}484}485486private void emitCurveTo(final float x0, final float y0,487final float x1, final float y1,488final float x2, final float y2,489final float x3, final float y3, final boolean rev)490{491if (rev) {492reverse.pushCubic(x0, y0, x1, y1, x2, y2);493} else {494out.curveTo(x1, y1, x2, y2, x3, y3);495}496}497498private void emitClose() {499out.closePath();500}501502private void drawJoin(float pdx, float pdy,503float x0, float y0,504float dx, float dy,505float omx, float omy,506float mx, float my)507{508if (prev != DRAWING_OP_TO) {509emitMoveTo(x0 + mx, y0 + my);510this.sdx = dx;511this.sdy = dy;512this.smx = mx;513this.smy = my;514} else {515boolean cw = isCW(pdx, pdy, dx, dy);516if (joinStyle == JOIN_MITER) {517drawMiter(pdx, pdy, x0, y0, dx, dy, omx, omy, mx, my, cw);518} else if (joinStyle == JOIN_ROUND) {519drawRoundJoin(x0, y0,520omx, omy,521mx, my, cw,522ROUND_JOIN_THRESHOLD);523}524emitLineTo(x0, y0, !cw);525}526prev = DRAWING_OP_TO;527}528529private static boolean within(final float x1, final float y1,530final float x2, final float y2,531final float ERR)532{533assert ERR > 0 : "";534// compare taxicab distance. ERR will always be small, so using535// true distance won't give much benefit536return (Helpers.within(x1, x2, ERR) && // we want to avoid calling Math.abs537Helpers.within(y1, y2, ERR)); // this is just as good.538}539540private void getLineOffsets(float x1, float y1,541float x2, float y2,542float[] left, float[] right) {543computeOffset(x2 - x1, y2 - y1, lineWidth2, offset[0]);544left[0] = x1 + offset[0][0];545left[1] = y1 + offset[0][1];546left[2] = x2 + offset[0][0];547left[3] = y2 + offset[0][1];548right[0] = x1 - offset[0][0];549right[1] = y1 - offset[0][1];550right[2] = x2 - offset[0][0];551right[3] = y2 - offset[0][1];552}553554private int computeOffsetCubic(float[] pts, final int off,555float[] leftOff, float[] rightOff)556{557// if p1=p2 or p3=p4 it means that the derivative at the endpoint558// vanishes, which creates problems with computeOffset. Usually559// this happens when this stroker object is trying to winden560// a curve with a cusp. What happens is that curveTo splits561// the input curve at the cusp, and passes it to this function.562// because of inaccuracies in the splitting, we consider points563// equal if they're very close to each other.564final float x1 = pts[off + 0], y1 = pts[off + 1];565final float x2 = pts[off + 2], y2 = pts[off + 3];566final float x3 = pts[off + 4], y3 = pts[off + 5];567final float x4 = pts[off + 6], y4 = pts[off + 7];568569float dx4 = x4 - x3;570float dy4 = y4 - y3;571float dx1 = x2 - x1;572float dy1 = y2 - y1;573574// if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,575// in which case ignore if p1 == p2576final boolean p1eqp2 = within(x1,y1,x2,y2, 6 * ulp(y2));577final boolean p3eqp4 = within(x3,y3,x4,y4, 6 * ulp(y4));578if (p1eqp2 && p3eqp4) {579getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);580return 4;581} else if (p1eqp2) {582dx1 = x3 - x1;583dy1 = y3 - y1;584} else if (p3eqp4) {585dx4 = x4 - x2;586dy4 = y4 - y2;587}588589// if p2-p1 and p4-p3 are parallel, that must mean this curve is a line590float dotsq = (dx1 * dx4 + dy1 * dy4);591dotsq = dotsq * dotsq;592float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;593if (Helpers.within(dotsq, l1sq * l4sq, 4 * ulp(dotsq))) {594getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);595return 4;596}597598// What we're trying to do in this function is to approximate an ideal599// offset curve (call it I) of the input curve B using a bezier curve Bp.600// The constraints I use to get the equations are:601//602// 1. The computed curve Bp should go through I(0) and I(1). These are603// x1p, y1p, x4p, y4p, which are p1p and p4p. We still need to find604// 4 variables: the x and y components of p2p and p3p (i.e. x2p, y2p, x3p, y3p).605//606// 2. Bp should have slope equal in absolute value to I at the endpoints. So,607// (by the way, the operator || in the comments below means "aligned with".608// It is defined on vectors, so when we say I'(0) || Bp'(0) we mean that609// vectors I'(0) and Bp'(0) are aligned, which is the same as saying610// that the tangent lines of I and Bp at 0 are parallel. Mathematically611// this means (I'(t) || Bp'(t)) <==> (I'(t) = c * Bp'(t)) where c is some612// nonzero constant.)613// I'(0) || Bp'(0) and I'(1) || Bp'(1). Obviously, I'(0) || B'(0) and614// I'(1) || B'(1); therefore, Bp'(0) || B'(0) and Bp'(1) || B'(1).615// We know that Bp'(0) || (p2p-p1p) and Bp'(1) || (p4p-p3p) and the same616// is true for any bezier curve; therefore, we get the equations617// (1) p2p = c1 * (p2-p1) + p1p618// (2) p3p = c2 * (p4-p3) + p4p619// We know p1p, p4p, p2, p1, p3, and p4; therefore, this reduces the number620// of unknowns from 4 to 2 (i.e. just c1 and c2).621// To eliminate these 2 unknowns we use the following constraint:622//623// 3. Bp(0.5) == I(0.5). Bp(0.5)=(x,y) and I(0.5)=(xi,yi), and I should note624// that I(0.5) is *the only* reason for computing dxm,dym. This gives us625// (3) Bp(0.5) = (p1p + 3 * (p2p + p3p) + p4p)/8, which is equivalent to626// (4) p2p + p3p = (Bp(0.5)*8 - p1p - p4p) / 3627// We can substitute (1) and (2) from above into (4) and we get:628// (5) c1*(p2-p1) + c2*(p4-p3) = (Bp(0.5)*8 - p1p - p4p)/3 - p1p - p4p629// which is equivalent to630// (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p)631//632// The right side of this is a 2D vector, and we know I(0.5), which gives us633// Bp(0.5), which gives us the value of the right side.634// The left side is just a matrix vector multiplication in disguise. It is635//636// [x2-x1, x4-x3][c1]637// [y2-y1, y4-y3][c2]638// which, is equal to639// [dx1, dx4][c1]640// [dy1, dy4][c2]641// At this point we are left with a simple linear system and we solve it by642// getting the inverse of the matrix above. Then we use [c1,c2] to compute643// p2p and p3p.644645float x = 0.125f * (x1 + 3 * (x2 + x3) + x4);646float y = 0.125f * (y1 + 3 * (y2 + y3) + y4);647// (dxm,dym) is some tangent of B at t=0.5. This means it's equal to648// c*B'(0.5) for some constant c.649float dxm = x3 + x4 - x1 - x2, dym = y3 + y4 - y1 - y2;650651// this computes the offsets at t=0, 0.5, 1, using the property that652// for any bezier curve the vectors p2-p1 and p4-p3 are parallel to653// the (dx/dt, dy/dt) vectors at the endpoints.654computeOffset(dx1, dy1, lineWidth2, offset[0]);655computeOffset(dxm, dym, lineWidth2, offset[1]);656computeOffset(dx4, dy4, lineWidth2, offset[2]);657float x1p = x1 + offset[0][0]; // start658float y1p = y1 + offset[0][1]; // point659float xi = x + offset[1][0]; // interpolation660float yi = y + offset[1][1]; // point661float x4p = x4 + offset[2][0]; // end662float y4p = y4 + offset[2][1]; // point663664float invdet43 = 4f / (3f * (dx1 * dy4 - dy1 * dx4));665666float two_pi_m_p1_m_p4x = 2*xi - x1p - x4p;667float two_pi_m_p1_m_p4y = 2*yi - y1p - y4p;668float c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);669float c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);670671float x2p, y2p, x3p, y3p;672x2p = x1p + c1*dx1;673y2p = y1p + c1*dy1;674x3p = x4p + c2*dx4;675y3p = y4p + c2*dy4;676677leftOff[0] = x1p; leftOff[1] = y1p;678leftOff[2] = x2p; leftOff[3] = y2p;679leftOff[4] = x3p; leftOff[5] = y3p;680leftOff[6] = x4p; leftOff[7] = y4p;681682x1p = x1 - offset[0][0]; y1p = y1 - offset[0][1];683xi = xi - 2 * offset[1][0]; yi = yi - 2 * offset[1][1];684x4p = x4 - offset[2][0]; y4p = y4 - offset[2][1];685686two_pi_m_p1_m_p4x = 2*xi - x1p - x4p;687two_pi_m_p1_m_p4y = 2*yi - y1p - y4p;688c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);689c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);690691x2p = x1p + c1*dx1;692y2p = y1p + c1*dy1;693x3p = x4p + c2*dx4;694y3p = y4p + c2*dy4;695696rightOff[0] = x1p; rightOff[1] = y1p;697rightOff[2] = x2p; rightOff[3] = y2p;698rightOff[4] = x3p; rightOff[5] = y3p;699rightOff[6] = x4p; rightOff[7] = y4p;700return 8;701}702703// return the kind of curve in the right and left arrays.704private int computeOffsetQuad(float[] pts, final int off,705float[] leftOff, float[] rightOff)706{707final float x1 = pts[off + 0], y1 = pts[off + 1];708final float x2 = pts[off + 2], y2 = pts[off + 3];709final float x3 = pts[off + 4], y3 = pts[off + 5];710711final float dx3 = x3 - x2;712final float dy3 = y3 - y2;713final float dx1 = x2 - x1;714final float dy1 = y2 - y1;715716// this computes the offsets at t = 0, 1717computeOffset(dx1, dy1, lineWidth2, offset[0]);718computeOffset(dx3, dy3, lineWidth2, offset[1]);719720leftOff[0] = x1 + offset[0][0]; leftOff[1] = y1 + offset[0][1];721leftOff[4] = x3 + offset[1][0]; leftOff[5] = y3 + offset[1][1];722rightOff[0] = x1 - offset[0][0]; rightOff[1] = y1 - offset[0][1];723rightOff[4] = x3 - offset[1][0]; rightOff[5] = y3 - offset[1][1];724725float x1p = leftOff[0]; // start726float y1p = leftOff[1]; // point727float x3p = leftOff[4]; // end728float y3p = leftOff[5]; // point729730// Corner cases:731// 1. If the two control vectors are parallel, we'll end up with NaN's732// in leftOff (and rightOff in the body of the if below), so we'll733// do getLineOffsets, which is right.734// 2. If the first or second two points are equal, then (dx1,dy1)==(0,0)735// or (dx3,dy3)==(0,0), so (x1p, y1p)==(x1p+dx1, y1p+dy1)736// or (x3p, y3p)==(x3p-dx3, y3p-dy3), which means that737// computeIntersection will put NaN's in leftOff and right off, and738// we will do getLineOffsets, which is right.739computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);740float cx = leftOff[2];741float cy = leftOff[3];742743if (!(isFinite(cx) && isFinite(cy))) {744// maybe the right path is not degenerate.745x1p = rightOff[0];746y1p = rightOff[1];747x3p = rightOff[4];748y3p = rightOff[5];749computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);750cx = rightOff[2];751cy = rightOff[3];752if (!(isFinite(cx) && isFinite(cy))) {753// both are degenerate. This curve is a line.754getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);755return 4;756}757// {left,right}Off[0,1,4,5] are already set to the correct values.758leftOff[2] = 2*x2 - cx;759leftOff[3] = 2*y2 - cy;760return 6;761}762763// rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2))764// == 2*(x2, y2) - (left_x2, left_y2)765rightOff[2] = 2*x2 - cx;766rightOff[3] = 2*y2 - cy;767return 6;768}769770private static boolean isFinite(float x) {771return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY);772}773774// This is where the curve to be processed is put. We give it775// enough room to store 2 curves: one for the current subdivision, the776// other for the rest of the curve.777private float[] middle = new float[2*8];778private float[] lp = new float[8];779private float[] rp = new float[8];780private static final int MAX_N_CURVES = 11;781private float[] subdivTs = new float[MAX_N_CURVES - 1];782783// If this class is compiled with ecj, then Hotspot crashes when OSR784// compiling this function. See bugs 7004570 and 6675699785// TODO: until those are fixed, we should work around that by786// manually inlining this into curveTo and quadTo.787/******************************* WORKAROUND **********************************788private void somethingTo(final int type) {789// need these so we can update the state at the end of this method790final float xf = middle[type-2], yf = middle[type-1];791float dxs = middle[2] - middle[0];792float dys = middle[3] - middle[1];793float dxf = middle[type - 2] - middle[type - 4];794float dyf = middle[type - 1] - middle[type - 3];795switch(type) {796case 6:797if ((dxs == 0f && dys == 0f) ||798(dxf == 0f && dyf == 0f)) {799dxs = dxf = middle[4] - middle[0];800dys = dyf = middle[5] - middle[1];801}802break;803case 8:804boolean p1eqp2 = (dxs == 0f && dys == 0f);805boolean p3eqp4 = (dxf == 0f && dyf == 0f);806if (p1eqp2) {807dxs = middle[4] - middle[0];808dys = middle[5] - middle[1];809if (dxs == 0f && dys == 0f) {810dxs = middle[6] - middle[0];811dys = middle[7] - middle[1];812}813}814if (p3eqp4) {815dxf = middle[6] - middle[2];816dyf = middle[7] - middle[3];817if (dxf == 0f && dyf == 0f) {818dxf = middle[6] - middle[0];819dyf = middle[7] - middle[1];820}821}822}823if (dxs == 0f && dys == 0f) {824// this happens iff the "curve" is just a point825lineTo(middle[0], middle[1]);826return;827}828// if these vectors are too small, normalize them, to avoid future829// precision problems.830if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {831float len = (float) sqrt(dxs*dxs + dys*dys);832dxs /= len;833dys /= len;834}835if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {836float len = (float) sqrt(dxf*dxf + dyf*dyf);837dxf /= len;838dyf /= len;839}840841computeOffset(dxs, dys, lineWidth2, offset[0]);842final float mx = offset[0][0];843final float my = offset[0][1];844drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);845846int nSplits = findSubdivPoints(middle, subdivTs, type, lineWidth2);847848int kind = 0;849Iterator<Integer> it = Curve.breakPtsAtTs(middle, type, subdivTs, nSplits);850while(it.hasNext()) {851int curCurveOff = it.next();852853switch (type) {854case 8:855kind = computeOffsetCubic(middle, curCurveOff, lp, rp);856break;857case 6:858kind = computeOffsetQuad(middle, curCurveOff, lp, rp);859break;860}861emitLineTo(lp[0], lp[1]);862switch(kind) {863case 8:864emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false);865emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true);866break;867case 6:868emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false);869emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true);870break;871case 4:872emitLineTo(lp[2], lp[3]);873emitLineTo(rp[0], rp[1], true);874break;875}876emitLineTo(rp[kind - 2], rp[kind - 1], true);877}878879this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;880this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;881this.cdx = dxf;882this.cdy = dyf;883this.cx0 = xf;884this.cy0 = yf;885this.prev = DRAWING_OP_TO;886}887****************************** END WORKAROUND *******************************/888889// finds values of t where the curve in pts should be subdivided in order890// to get good offset curves a distance of w away from the middle curve.891// Stores the points in ts, and returns how many of them there were.892private static Curve c = new Curve();893private static int findSubdivPoints(float[] pts, float[] ts, final int type, final float w)894{895final float x12 = pts[2] - pts[0];896final float y12 = pts[3] - pts[1];897// if the curve is already parallel to either axis we gain nothing898// from rotating it.899if (y12 != 0f && x12 != 0f) {900// we rotate it so that the first vector in the control polygon is901// parallel to the x-axis. This will ensure that rotated quarter902// circles won't be subdivided.903final float hypot = (float) sqrt(x12 * x12 + y12 * y12);904final float cos = x12 / hypot;905final float sin = y12 / hypot;906final float x1 = cos * pts[0] + sin * pts[1];907final float y1 = cos * pts[1] - sin * pts[0];908final float x2 = cos * pts[2] + sin * pts[3];909final float y2 = cos * pts[3] - sin * pts[2];910final float x3 = cos * pts[4] + sin * pts[5];911final float y3 = cos * pts[5] - sin * pts[4];912switch(type) {913case 8:914final float x4 = cos * pts[6] + sin * pts[7];915final float y4 = cos * pts[7] - sin * pts[6];916c.set(x1, y1, x2, y2, x3, y3, x4, y4);917break;918case 6:919c.set(x1, y1, x2, y2, x3, y3);920break;921}922} else {923c.set(pts, type);924}925926int ret = 0;927// we subdivide at values of t such that the remaining rotated928// curves are monotonic in x and y.929ret += c.dxRoots(ts, ret);930ret += c.dyRoots(ts, ret);931// subdivide at inflection points.932if (type == 8) {933// quadratic curves can't have inflection points934ret += c.infPoints(ts, ret);935}936937// now we must subdivide at points where one of the offset curves will have938// a cusp. This happens at ts where the radius of curvature is equal to w.939ret += c.rootsOfROCMinusW(ts, ret, w, 0.0001f);940941ret = Helpers.filterOutNotInAB(ts, 0, ret, 0.0001f, 0.9999f);942Helpers.isort(ts, 0, ret);943return ret;944}945946@Override public void curveTo(float x1, float y1,947float x2, float y2,948float x3, float y3)949{950middle[0] = cx0; middle[1] = cy0;951middle[2] = x1; middle[3] = y1;952middle[4] = x2; middle[5] = y2;953middle[6] = x3; middle[7] = y3;954955// inlined version of somethingTo(8);956// See the TODO on somethingTo957958// need these so we can update the state at the end of this method959final float xf = middle[6], yf = middle[7];960float dxs = middle[2] - middle[0];961float dys = middle[3] - middle[1];962float dxf = middle[6] - middle[4];963float dyf = middle[7] - middle[5];964965boolean p1eqp2 = (dxs == 0f && dys == 0f);966boolean p3eqp4 = (dxf == 0f && dyf == 0f);967if (p1eqp2) {968dxs = middle[4] - middle[0];969dys = middle[5] - middle[1];970if (dxs == 0f && dys == 0f) {971dxs = middle[6] - middle[0];972dys = middle[7] - middle[1];973}974}975if (p3eqp4) {976dxf = middle[6] - middle[2];977dyf = middle[7] - middle[3];978if (dxf == 0f && dyf == 0f) {979dxf = middle[6] - middle[0];980dyf = middle[7] - middle[1];981}982}983if (dxs == 0f && dys == 0f) {984// this happens iff the "curve" is just a point985lineTo(middle[0], middle[1]);986return;987}988989// if these vectors are too small, normalize them, to avoid future990// precision problems.991if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {992float len = (float) sqrt(dxs*dxs + dys*dys);993dxs /= len;994dys /= len;995}996if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {997float len = (float) sqrt(dxf*dxf + dyf*dyf);998dxf /= len;999dyf /= len;1000}10011002computeOffset(dxs, dys, lineWidth2, offset[0]);1003final float mx = offset[0][0];1004final float my = offset[0][1];1005drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);10061007int nSplits = findSubdivPoints(middle, subdivTs, 8, lineWidth2);10081009int kind = 0;1010Iterator<Integer> it = Curve.breakPtsAtTs(middle, 8, subdivTs, nSplits);1011while(it.hasNext()) {1012int curCurveOff = it.next();10131014kind = computeOffsetCubic(middle, curCurveOff, lp, rp);1015emitLineTo(lp[0], lp[1]);1016switch(kind) {1017case 8:1018emitCurveTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], lp[6], lp[7], false);1019emitCurveTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], rp[6], rp[7], true);1020break;1021case 4:1022emitLineTo(lp[2], lp[3]);1023emitLineTo(rp[0], rp[1], true);1024break;1025}1026emitLineTo(rp[kind - 2], rp[kind - 1], true);1027}10281029this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;1030this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;1031this.cdx = dxf;1032this.cdy = dyf;1033this.cx0 = xf;1034this.cy0 = yf;1035this.prev = DRAWING_OP_TO;1036}10371038@Override public void quadTo(float x1, float y1, float x2, float y2) {1039middle[0] = cx0; middle[1] = cy0;1040middle[2] = x1; middle[3] = y1;1041middle[4] = x2; middle[5] = y2;10421043// inlined version of somethingTo(8);1044// See the TODO on somethingTo10451046// need these so we can update the state at the end of this method1047final float xf = middle[4], yf = middle[5];1048float dxs = middle[2] - middle[0];1049float dys = middle[3] - middle[1];1050float dxf = middle[4] - middle[2];1051float dyf = middle[5] - middle[3];1052if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) {1053dxs = dxf = middle[4] - middle[0];1054dys = dyf = middle[5] - middle[1];1055}1056if (dxs == 0f && dys == 0f) {1057// this happens iff the "curve" is just a point1058lineTo(middle[0], middle[1]);1059return;1060}1061// if these vectors are too small, normalize them, to avoid future1062// precision problems.1063if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {1064float len = (float) sqrt(dxs*dxs + dys*dys);1065dxs /= len;1066dys /= len;1067}1068if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {1069float len = (float) sqrt(dxf*dxf + dyf*dyf);1070dxf /= len;1071dyf /= len;1072}10731074computeOffset(dxs, dys, lineWidth2, offset[0]);1075final float mx = offset[0][0];1076final float my = offset[0][1];1077drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);10781079int nSplits = findSubdivPoints(middle, subdivTs, 6, lineWidth2);10801081int kind = 0;1082Iterator<Integer> it = Curve.breakPtsAtTs(middle, 6, subdivTs, nSplits);1083while(it.hasNext()) {1084int curCurveOff = it.next();10851086kind = computeOffsetQuad(middle, curCurveOff, lp, rp);1087emitLineTo(lp[0], lp[1]);1088switch(kind) {1089case 6:1090emitQuadTo(lp[0], lp[1], lp[2], lp[3], lp[4], lp[5], false);1091emitQuadTo(rp[0], rp[1], rp[2], rp[3], rp[4], rp[5], true);1092break;1093case 4:1094emitLineTo(lp[2], lp[3]);1095emitLineTo(rp[0], rp[1], true);1096break;1097}1098emitLineTo(rp[kind - 2], rp[kind - 1], true);1099}11001101this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;1102this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;1103this.cdx = dxf;1104this.cdy = dyf;1105this.cx0 = xf;1106this.cy0 = yf;1107this.prev = DRAWING_OP_TO;1108}11091110@Override public long getNativeConsumer() {1111throw new InternalError("Stroker doesn't use a native consumer");1112}11131114// a stack of polynomial curves where each curve shares endpoints with1115// adjacent ones.1116private static final class PolyStack {1117float[] curves;1118int end;1119int[] curveTypes;1120int numCurves;11211122private static final int INIT_SIZE = 50;11231124PolyStack() {1125curves = new float[8 * INIT_SIZE];1126curveTypes = new int[INIT_SIZE];1127end = 0;1128numCurves = 0;1129}11301131public boolean isEmpty() {1132return numCurves == 0;1133}11341135private void ensureSpace(int n) {1136if (end + n >= curves.length) {1137int newSize = (end + n) * 2;1138curves = Arrays.copyOf(curves, newSize);1139}1140if (numCurves >= curveTypes.length) {1141int newSize = numCurves * 2;1142curveTypes = Arrays.copyOf(curveTypes, newSize);1143}1144}11451146public void pushCubic(float x0, float y0,1147float x1, float y1,1148float x2, float y2)1149{1150ensureSpace(6);1151curveTypes[numCurves++] = 8;1152// assert(x0 == lastX && y0 == lastY)11531154// we reverse the coordinate order to make popping easier1155curves[end++] = x2; curves[end++] = y2;1156curves[end++] = x1; curves[end++] = y1;1157curves[end++] = x0; curves[end++] = y0;1158}11591160public void pushQuad(float x0, float y0,1161float x1, float y1)1162{1163ensureSpace(4);1164curveTypes[numCurves++] = 6;1165// assert(x0 == lastX && y0 == lastY)1166curves[end++] = x1; curves[end++] = y1;1167curves[end++] = x0; curves[end++] = y0;1168}11691170public void pushLine(float x, float y) {1171ensureSpace(2);1172curveTypes[numCurves++] = 4;1173// assert(x0 == lastX && y0 == lastY)1174curves[end++] = x; curves[end++] = y;1175}11761177@SuppressWarnings("unused")1178public int pop(float[] pts) {1179int ret = curveTypes[numCurves - 1];1180numCurves--;1181end -= (ret - 2);1182System.arraycopy(curves, end, pts, 0, ret - 2);1183return ret;1184}11851186public void pop(PathConsumer2D io) {1187numCurves--;1188int type = curveTypes[numCurves];1189end -= (type - 2);1190switch(type) {1191case 8:1192io.curveTo(curves[end+0], curves[end+1],1193curves[end+2], curves[end+3],1194curves[end+4], curves[end+5]);1195break;1196case 6:1197io.quadTo(curves[end+0], curves[end+1],1198curves[end+2], curves[end+3]);1199break;1200case 4:1201io.lineTo(curves[end], curves[end+1]);1202}1203}12041205@Override1206public String toString() {1207String ret = "";1208int nc = numCurves;1209int end = this.end;1210while (nc > 0) {1211nc--;1212int type = curveTypes[numCurves];1213end -= (type - 2);1214switch(type) {1215case 8:1216ret += "cubic: ";1217break;1218case 6:1219ret += "quad: ";1220break;1221case 4:1222ret += "line: ";1223break;1224}1225ret += Arrays.toString(Arrays.copyOfRange(curves, end, end+type-2)) + "\n";1226}1227return ret;1228}1229}1230}123112321233