0N/A/*
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 *
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 *
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 *
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.
0N/A *
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
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d.pisces;
0N/A
2956N/Aimport java.util.Arrays;
2956N/Aimport java.util.Iterator;
4066N/Aimport static java.lang.Math.ulp;
4066N/Aimport static java.lang.Math.sqrt;
2956N/A
2956N/Aimport sun.awt.geom.PathConsumer2D;
2956N/A
2956N/A// TODO: some of the arithmetic here is too verbose and prone to hard to
2956N/A// debug typos. We should consider making a small Point/Vector class that
2956N/A// has methods like plus(Point), minus(Point), dot(Point), cross(Point)and such
3444N/Afinal class Stroker implements PathConsumer2D {
0N/A
0N/A private static final int MOVE_TO = 0;
2956N/A private static final int DRAWING_OP_TO = 1; // ie. curve, line, or quad
0N/A private static final int CLOSE = 2;
0N/A
0N/A /**
0N/A * Constant value for join style.
0N/A */
0N/A public static final int JOIN_MITER = 0;
0N/A
0N/A /**
0N/A * Constant value for join style.
0N/A */
0N/A public static final int JOIN_ROUND = 1;
0N/A
0N/A /**
0N/A * Constant value for join style.
0N/A */
0N/A public static final int JOIN_BEVEL = 2;
0N/A
0N/A /**
0N/A * Constant value for end cap style.
0N/A */
0N/A public static final int CAP_BUTT = 0;
0N/A
0N/A /**
0N/A * Constant value for end cap style.
0N/A */
0N/A public static final int CAP_ROUND = 1;
0N/A
0N/A /**
0N/A * Constant value for end cap style.
0N/A */
0N/A public static final int CAP_SQUARE = 2;
0N/A
2956N/A private final PathConsumer2D out;
0N/A
2638N/A private final int capStyle;
2638N/A private final int joinStyle;
0N/A
2638N/A private final float lineWidth2;
0N/A
2956N/A private final float[][] offset = new float[3][2];
2638N/A private final float[] miter = new float[2];
2638N/A private final float miterLimitSq;
0N/A
2638N/A private int prev;
0N/A
2956N/A // The starting point of the path, and the slope there.
2956N/A private float sx0, sy0, sdx, sdy;
2956N/A // the current point and the slope there.
2956N/A private float cx0, cy0, cdx, cdy; // c stands for current
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.
2956N/A private float smx, smy, cmx, cmy;
0N/A
2956N/A private final PolyStack reverse = new PolyStack();
0N/A
0N/A /**
0N/A * Constructs a <code>Stroker</code>.
0N/A *
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
0N/A */
2956N/A public Stroker(PathConsumer2D pc2d,
2638N/A float lineWidth,
0N/A int capStyle,
0N/A int joinStyle,
2956N/A float miterLimit)
2956N/A {
2956N/A this.out = pc2d;
0N/A
2638N/A this.lineWidth2 = lineWidth / 2;
0N/A this.capStyle = capStyle;
0N/A this.joinStyle = joinStyle;
0N/A
2956N/A float limit = miterLimit * lineWidth2;
2638N/A this.miterLimitSq = limit*limit;
0N/A
2956N/A this.prev = CLOSE;
0N/A }
0N/A
2956N/A private static void computeOffset(final float lx, final float ly,
2956N/A final float w, final float[] m)
2956N/A {
4066N/A final float len = (float) sqrt(lx*lx + ly*ly);
2956N/A if (len == 0) {
2956N/A m[0] = m[1] = 0;
0N/A } else {
2956N/A m[0] = (ly * w)/len;
2956N/A m[1] = -(lx * w)/len;
0N/A }
0N/A }
0N/A
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 // clockwise order.
2956N/A // NOTE: "clockwise" here assumes coordinates with 0,0 at the bottom left.
2956N/A private static boolean isCW(final float dx1, final float dy1,
2956N/A final float dx2, final float dy2)
2956N/A {
2956N/A return dx1 * dy2 <= dy1 * dx2;
0N/A }
0N/A
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.
2638N/A private static final float ROUND_JOIN_THRESHOLD = 1000/65536f;
0N/A
2638N/A private void drawRoundJoin(float x, float y,
2638N/A float omx, float omy, float mx, float my,
0N/A boolean rev,
2956N/A float threshold)
2956N/A {
0N/A if ((omx == 0 && omy == 0) || (mx == 0 && my == 0)) {
0N/A return;
0N/A }
0N/A
2638N/A float domx = omx - mx;
2638N/A float domy = omy - my;
2638N/A float len = domx*domx + domy*domy;
0N/A if (len < threshold) {
0N/A return;
0N/A }
0N/A
0N/A if (rev) {
0N/A omx = -omx;
0N/A omy = -omy;
0N/A mx = -mx;
0N/A my = -my;
0N/A }
2956N/A drawRoundJoin(x, y, omx, omy, mx, my, rev);
2956N/A }
0N/A
2956N/A private void drawRoundJoin(float cx, float cy,
2956N/A float omx, float omy,
2956N/A float mx, float my,
2956N/A boolean rev)
2956N/A {
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 double cosext = omx * mx + omy * 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 // and mx,my.
2956N/A final int numCurves = cosext >= 0 ? 1 : 2;
0N/A
2956N/A switch (numCurves) {
2956N/A case 1:
2956N/A drawBezApproxForArc(cx, cy, omx, omy, mx, my, rev);
2956N/A break;
2956N/A case 2:
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 float nx = my - omy, ny = omx - mx;
4066N/A float nlen = (float) sqrt(nx*nx + ny*ny);
2956N/A float scale = lineWidth2/nlen;
2956N/A float mmx = nx * scale, mmy = ny * scale;
2956N/A
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 if (rev) {
2956N/A mmx = -mmx;
2956N/A mmy = -mmy;
2956N/A }
2956N/A drawBezApproxForArc(cx, cy, omx, omy, mmx, mmy, rev);
2956N/A drawBezApproxForArc(cx, cy, mmx, mmy, mx, my, rev);
2956N/A break;
0N/A }
0N/A }
0N/A
2956N/A // the input arc defined by omx,omy and mx,my must span <= 90 degrees.
2956N/A private void drawBezApproxForArc(final float cx, final float cy,
2956N/A final float omx, final float omy,
2956N/A final float mx, final float my,
2956N/A boolean rev)
2956N/A {
2956N/A float cosext2 = (omx * mx + omy * my) / (2 * lineWidth2 * lineWidth2);
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|.
4066N/A float cv = (float) ((4.0 / 3.0) * sqrt(0.5-cosext2) /
4066N/A (1.0 + sqrt(cosext2+0.5)));
2956N/A // if clockwise, we need to negate cv.
2956N/A if (rev) { // rev is equivalent to isCW(omx, omy, mx, my)
2956N/A cv = -cv;
2956N/A }
2956N/A final float x1 = cx + omx;
2956N/A final float y1 = cy + omy;
2956N/A final float x2 = x1 - cv * omy;
2956N/A final float y2 = y1 + cv * omx;
2956N/A
2956N/A final float x4 = cx + mx;
2956N/A final float y4 = cy + my;
2956N/A final float x3 = x4 + cv * my;
2956N/A final float y3 = y4 - cv * mx;
2956N/A
2956N/A emitCurveTo(x1, y1, x2, y2, x3, y3, x4, y4, rev);
2956N/A }
2956N/A
2956N/A private void drawRoundCap(float cx, float cy, float mx, float 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).
2956N/A emitCurveTo(cx+mx, cy+my,
2956N/A cx+mx-C*my, cy+my+C*mx,
2956N/A cx-my+C*mx, cy+mx+C*my,
2956N/A cx-my, cy+mx,
2956N/A false);
2956N/A emitCurveTo(cx-my, cy+mx,
2956N/A cx-my-C*mx, cy+mx-C*my,
2956N/A cx-mx-C*my, cy-my+C*mx,
2956N/A cx-mx, cy-my,
2956N/A false);
2956N/A }
2956N/A
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 private void computeIntersection(final float x0, final float y0,
4066N/A final float x1, final float y1,
4066N/A final float x0p, final float y0p,
4066N/A final float x1p, final float y1p,
4066N/A final float[] m, int off)
2956N/A {
2638N/A float x10 = x1 - x0;
2638N/A float y10 = y1 - y0;
2638N/A float x10p = x1p - x0p;
2638N/A float y10p = y1p - y0p;
0N/A
2638N/A float den = x10*y10p - x10p*y10;
2956N/A float t = x10p*(y0-y0p) - y10p*(x0-x0p);
2956N/A t /= den;
2956N/A m[off++] = x0 + t*x10;
2956N/A m[off] = y0 + t*y10;
0N/A }
0N/A
2956N/A private void drawMiter(final float pdx, final float pdy,
2956N/A final float x0, final float y0,
2956N/A final float dx, final float dy,
2638N/A float omx, float omy, float mx, float my,
2956N/A boolean rev)
2956N/A {
2956N/A if ((mx == omx && my == omy) ||
2956N/A (pdx == 0 && pdy == 0) ||
4066N/A (dx == 0 && dy == 0))
4066N/A {
0N/A return;
0N/A }
0N/A
0N/A if (rev) {
0N/A omx = -omx;
0N/A omy = -omy;
0N/A mx = -mx;
0N/A my = -my;
0N/A }
0N/A
4066N/A computeIntersection((x0 - pdx) + omx, (y0 - pdy) + omy, x0 + omx, y0 + omy,
4066N/A (dx + x0) + mx, (dy + y0) + my, x0 + mx, y0 + my,
4066N/A miter, 0);
0N/A
2956N/A float lenSq = (miter[0]-x0)*(miter[0]-x0) + (miter[1]-y0)*(miter[1]-y0);
0N/A
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.
0N/A if (lenSq < miterLimitSq) {
0N/A emitLineTo(miter[0], miter[1], rev);
0N/A }
0N/A }
0N/A
2638N/A public void moveTo(float x0, float y0) {
2956N/A if (prev == DRAWING_OP_TO) {
0N/A finish();
0N/A }
2956N/A this.sx0 = this.cx0 = x0;
2956N/A this.sy0 = this.cy0 = y0;
2956N/A this.cdx = this.sdx = 1;
2956N/A this.cdy = this.sdy = 0;
0N/A this.prev = MOVE_TO;
0N/A }
0N/A
2638N/A public void lineTo(float x1, float y1) {
2956N/A float dx = x1 - cx0;
2956N/A float dy = y1 - cy0;
2956N/A if (dx == 0f && dy == 0f) {
2956N/A dx = 1;
2956N/A }
2956N/A computeOffset(dx, dy, lineWidth2, offset[0]);
2956N/A float mx = offset[0][0];
2956N/A float my = offset[0][1];
0N/A
2956N/A drawJoin(cdx, cdy, cx0, cy0, dx, dy, cmx, cmy, mx, my);
0N/A
2956N/A emitLineTo(cx0 + mx, cy0 + my);
2956N/A emitLineTo(x1 + mx, y1 + my);
2956N/A
2956N/A emitLineTo(cx0 - mx, cy0 - my, true);
2956N/A emitLineTo(x1 - mx, y1 - my, true);
0N/A
2956N/A this.cmx = mx;
2956N/A this.cmy = my;
2956N/A this.cdx = dx;
2956N/A this.cdy = dy;
2956N/A this.cx0 = x1;
2956N/A this.cy0 = y1;
2956N/A this.prev = DRAWING_OP_TO;
0N/A }
0N/A
2956N/A public void closePath() {
2956N/A if (prev != DRAWING_OP_TO) {
2956N/A if (prev == CLOSE) {
2956N/A return;
0N/A }
2956N/A emitMoveTo(cx0, cy0 - lineWidth2);
2956N/A this.cmx = this.smx = 0;
2956N/A this.cmy = this.smy = -lineWidth2;
2956N/A this.cdx = this.sdx = 1;
2956N/A this.cdy = this.sdy = 0;
0N/A finish();
0N/A return;
0N/A }
0N/A
2956N/A if (cx0 != sx0 || cy0 != sy0) {
2956N/A lineTo(sx0, sy0);
0N/A }
0N/A
2956N/A drawJoin(cdx, cdy, cx0, cy0, sdx, sdy, cmx, cmy, smx, smy);
0N/A
2956N/A emitLineTo(sx0 + smx, sy0 + smy);
0N/A
2956N/A emitMoveTo(sx0 - smx, sy0 - smy);
2956N/A emitReverse();
0N/A
0N/A this.prev = CLOSE;
0N/A emitClose();
0N/A }
0N/A
2956N/A private void emitReverse() {
2956N/A while(!reverse.isEmpty()) {
2956N/A reverse.pop(out);
2956N/A }
2956N/A }
0N/A
2956N/A public void pathDone() {
2956N/A if (prev == DRAWING_OP_TO) {
0N/A finish();
0N/A }
0N/A
2956N/A out.pathDone();
2956N/A // this shouldn't matter since this object won't be used
2956N/A // after the call to this method.
2956N/A this.prev = CLOSE;
0N/A }
0N/A
0N/A private void finish() {
0N/A if (capStyle == CAP_ROUND) {
2956N/A drawRoundCap(cx0, cy0, cmx, cmy);
0N/A } else if (capStyle == CAP_SQUARE) {
2956N/A emitLineTo(cx0 - cmy + cmx, cy0 + cmx + cmy);
2956N/A emitLineTo(cx0 - cmy - cmx, cy0 + cmx - cmy);
0N/A }
0N/A
2956N/A emitReverse();
0N/A
0N/A if (capStyle == CAP_ROUND) {
2956N/A drawRoundCap(sx0, sy0, -smx, -smy);
0N/A } else if (capStyle == CAP_SQUARE) {
2956N/A emitLineTo(sx0 + smy - smx, sy0 - smx - smy);
2956N/A emitLineTo(sx0 + smy + smx, sy0 - smx + smy);
0N/A }
0N/A
0N/A emitClose();
0N/A }
0N/A
2956N/A private void emitMoveTo(final float x0, final float y0) {
2956N/A out.moveTo(x0, y0);
0N/A }
0N/A
2956N/A private void emitLineTo(final float x1, final float y1) {
2956N/A out.lineTo(x1, y1);
0N/A }
0N/A
2956N/A private void emitLineTo(final float x1, final float y1,
2956N/A final boolean rev)
2956N/A {
0N/A if (rev) {
2956N/A reverse.pushLine(x1, y1);
0N/A } else {
0N/A emitLineTo(x1, y1);
0N/A }
0N/A }
0N/A
2956N/A private void emitQuadTo(final float x0, final float y0,
2956N/A final float x1, final float y1,
2956N/A final float x2, final float y2, final boolean rev)
2956N/A {
2956N/A if (rev) {
2956N/A reverse.pushQuad(x0, y0, x1, y1);
2956N/A } else {
2956N/A out.quadTo(x1, y1, x2, y2);
2956N/A }
2956N/A }
2956N/A
2956N/A private void emitCurveTo(final float x0, final float y0,
2956N/A final float x1, final float y1,
2956N/A final float x2, final float y2,
2956N/A final float x3, final float y3, final boolean rev)
2956N/A {
2956N/A if (rev) {
2956N/A reverse.pushCubic(x0, y0, x1, y1, x2, y2);
2956N/A } else {
2956N/A out.curveTo(x1, y1, x2, y2, x3, y3);
2956N/A }
2956N/A }
2956N/A
0N/A private void emitClose() {
2956N/A out.closePath();
2956N/A }
2956N/A
2956N/A private void drawJoin(float pdx, float pdy,
2956N/A float x0, float y0,
2956N/A float dx, float dy,
2956N/A float omx, float omy,
2956N/A float mx, float my)
2956N/A {
2956N/A if (prev != DRAWING_OP_TO) {
2956N/A emitMoveTo(x0 + mx, y0 + my);
2956N/A this.sdx = dx;
2956N/A this.sdy = dy;
2956N/A this.smx = mx;
2956N/A this.smy = my;
2956N/A } else {
2956N/A boolean cw = isCW(pdx, pdy, dx, dy);
2956N/A if (joinStyle == JOIN_MITER) {
2956N/A drawMiter(pdx, pdy, x0, y0, dx, dy, omx, omy, mx, my, cw);
2956N/A } else if (joinStyle == JOIN_ROUND) {
2956N/A drawRoundJoin(x0, y0,
2956N/A omx, omy,
2956N/A mx, my, cw,
2956N/A ROUND_JOIN_THRESHOLD);
2956N/A }
2956N/A emitLineTo(x0, y0, !cw);
2956N/A }
2956N/A prev = DRAWING_OP_TO;
2956N/A }
2956N/A
2956N/A private static boolean within(final float x1, final float y1,
2956N/A final float x2, final float y2,
2956N/A final float ERR)
2956N/A {
2956N/A assert ERR > 0 : "";
2956N/A // compare taxicab distance. ERR will always be small, so using
2956N/A // true distance won't give much benefit
2956N/A return (Helpers.within(x1, x2, ERR) && // we want to avoid calling Math.abs
2956N/A Helpers.within(y1, y2, ERR)); // this is just as good.
2956N/A }
2956N/A
2956N/A private void getLineOffsets(float x1, float y1,
2956N/A float x2, float y2,
2956N/A float[] left, float[] right) {
2956N/A computeOffset(x2 - x1, y2 - y1, lineWidth2, offset[0]);
2956N/A left[0] = x1 + offset[0][0];
2956N/A left[1] = y1 + offset[0][1];
2956N/A left[2] = x2 + offset[0][0];
2956N/A left[3] = y2 + offset[0][1];
2956N/A right[0] = x1 - offset[0][0];
2956N/A right[1] = y1 - offset[0][1];
2956N/A right[2] = x2 - offset[0][0];
2956N/A right[3] = y2 - offset[0][1];
2956N/A }
2956N/A
2956N/A private int computeOffsetCubic(float[] pts, final int off,
2956N/A float[] leftOff, float[] rightOff)
2956N/A {
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 final float x1 = pts[off + 0], y1 = pts[off + 1];
2956N/A final float x2 = pts[off + 2], y2 = pts[off + 3];
2956N/A final float x3 = pts[off + 4], y3 = pts[off + 5];
2956N/A final float x4 = pts[off + 6], y4 = pts[off + 7];
2956N/A
2956N/A float dx4 = x4 - x3;
2956N/A float dy4 = y4 - y3;
2956N/A float dx1 = x2 - x1;
2956N/A float dy1 = y2 - y1;
2956N/A
2956N/A // if p1 == p2 && p3 == p4: draw line from p1->p4, unless p1 == p4,
2956N/A // in which case ignore if p1 == p2
4066N/A final boolean p1eqp2 = within(x1,y1,x2,y2, 6 * ulp(y2));
4066N/A final boolean p3eqp4 = within(x3,y3,x4,y4, 6 * ulp(y4));
2956N/A if (p1eqp2 && p3eqp4) {
2956N/A getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
2956N/A return 4;
2956N/A } else if (p1eqp2) {
2956N/A dx1 = x3 - x1;
2956N/A dy1 = y3 - y1;
2956N/A } else if (p3eqp4) {
2956N/A dx4 = x4 - x2;
2956N/A dy4 = y4 - y2;
2956N/A }
2956N/A
2956N/A // if p2-p1 and p4-p3 are parallel, that must mean this curve is a line
2956N/A float dotsq = (dx1 * dx4 + dy1 * dy4);
2956N/A dotsq = dotsq * dotsq;
2956N/A float l1sq = dx1 * dx1 + dy1 * dy1, l4sq = dx4 * dx4 + dy4 * dy4;
4066N/A if (Helpers.within(dotsq, l1sq * l4sq, 4 * ulp(dotsq))) {
2956N/A getLineOffsets(x1, y1, x4, y4, leftOff, rightOff);
2956N/A return 4;
2956N/A }
2956N/A
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//
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//
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// nonzero constant.)
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//
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// which is equivalent to
2956N/A// (6) c1*(p2-p1) + c2*(p4-p3) = (4/3) * (Bp(0.5) * 2 - p1p - p4p)
2956N/A//
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//
2956N/A// [x2-x1, x4-x3][c1]
2956N/A// [y2-y1, y4-y3][c2]
2956N/A// which, is equal to
2956N/A// [dx1, dx4][c1]
2956N/A// [dy1, dy4][c2]
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// p2p and p3p.
2956N/A
2956N/A float x = 0.125f * (x1 + 3 * (x2 + x3) + x4);
2956N/A float y = 0.125f * (y1 + 3 * (y2 + y3) + y4);
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 float dxm = x3 + x4 - x1 - x2, dym = y3 + y4 - y1 - y2;
2956N/A
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 // the (dx/dt, dy/dt) vectors at the endpoints.
2956N/A computeOffset(dx1, dy1, lineWidth2, offset[0]);
2956N/A computeOffset(dxm, dym, lineWidth2, offset[1]);
2956N/A computeOffset(dx4, dy4, lineWidth2, offset[2]);
2956N/A float x1p = x1 + offset[0][0]; // start
2956N/A float y1p = y1 + offset[0][1]; // point
2956N/A float xi = x + offset[1][0]; // interpolation
2956N/A float yi = y + offset[1][1]; // point
2956N/A float x4p = x4 + offset[2][0]; // end
2956N/A float y4p = y4 + offset[2][1]; // point
2956N/A
2956N/A float invdet43 = 4f / (3f * (dx1 * dy4 - dy1 * dx4));
2956N/A
2956N/A float two_pi_m_p1_m_p4x = 2*xi - x1p - x4p;
2956N/A float two_pi_m_p1_m_p4y = 2*yi - y1p - y4p;
2956N/A float c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
2956N/A float c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);
2956N/A
2956N/A float x2p, y2p, x3p, y3p;
2956N/A x2p = x1p + c1*dx1;
2956N/A y2p = y1p + c1*dy1;
2956N/A x3p = x4p + c2*dx4;
2956N/A y3p = y4p + c2*dy4;
2956N/A
2956N/A leftOff[0] = x1p; leftOff[1] = y1p;
2956N/A leftOff[2] = x2p; leftOff[3] = y2p;
2956N/A leftOff[4] = x3p; leftOff[5] = y3p;
2956N/A leftOff[6] = x4p; leftOff[7] = y4p;
2956N/A
2956N/A x1p = x1 - offset[0][0]; y1p = y1 - offset[0][1];
2956N/A xi = xi - 2 * offset[1][0]; yi = yi - 2 * offset[1][1];
2956N/A x4p = x4 - offset[2][0]; y4p = y4 - offset[2][1];
2956N/A
2956N/A two_pi_m_p1_m_p4x = 2*xi - x1p - x4p;
2956N/A two_pi_m_p1_m_p4y = 2*yi - y1p - y4p;
2956N/A c1 = invdet43 * (dy4 * two_pi_m_p1_m_p4x - dx4 * two_pi_m_p1_m_p4y);
2956N/A c2 = invdet43 * (dx1 * two_pi_m_p1_m_p4y - dy1 * two_pi_m_p1_m_p4x);
2956N/A
2956N/A x2p = x1p + c1*dx1;
2956N/A y2p = y1p + c1*dy1;
2956N/A x3p = x4p + c2*dx4;
2956N/A y3p = y4p + c2*dy4;
2956N/A
2956N/A rightOff[0] = x1p; rightOff[1] = y1p;
2956N/A rightOff[2] = x2p; rightOff[3] = y2p;
2956N/A rightOff[4] = x3p; rightOff[5] = y3p;
2956N/A rightOff[6] = x4p; rightOff[7] = y4p;
2956N/A return 8;
2956N/A }
2956N/A
2956N/A // return the kind of curve in the right and left arrays.
2956N/A private int computeOffsetQuad(float[] pts, final int off,
2956N/A float[] leftOff, float[] rightOff)
2956N/A {
2956N/A final float x1 = pts[off + 0], y1 = pts[off + 1];
2956N/A final float x2 = pts[off + 2], y2 = pts[off + 3];
2956N/A final float x3 = pts[off + 4], y3 = pts[off + 5];
2956N/A
4066N/A final float dx3 = x3 - x2;
4066N/A final float dy3 = y3 - y2;
4066N/A final float dx1 = x2 - x1;
4066N/A final float dy1 = y2 - y1;
4066N/A
4066N/A // this computes the offsets at t = 0, 1
4066N/A computeOffset(dx1, dy1, lineWidth2, offset[0]);
4066N/A computeOffset(dx3, dy3, lineWidth2, offset[1]);
4066N/A
4066N/A leftOff[0] = x1 + offset[0][0]; leftOff[1] = y1 + offset[0][1];
4066N/A leftOff[4] = x3 + offset[1][0]; leftOff[5] = y3 + offset[1][1];
4066N/A rightOff[0] = x1 - offset[0][0]; rightOff[1] = y1 - offset[0][1];
4066N/A rightOff[4] = x3 - offset[1][0]; rightOff[5] = y3 - offset[1][1];
4066N/A
4066N/A float x1p = leftOff[0]; // start
4066N/A float y1p = leftOff[1]; // point
4066N/A float x3p = leftOff[4]; // end
4066N/A float y3p = leftOff[5]; // point
2956N/A
4066N/A // Corner cases:
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 computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, leftOff, 2);
4066N/A float cx = leftOff[2];
4066N/A float cy = leftOff[3];
2956N/A
4066N/A if (!(isFinite(cx) && isFinite(cy))) {
4066N/A // maybe the right path is not degenerate.
4066N/A x1p = rightOff[0];
4066N/A y1p = rightOff[1];
4066N/A x3p = rightOff[4];
4066N/A y3p = rightOff[5];
4066N/A computeIntersection(x1p, y1p, x1p+dx1, y1p+dy1, x3p, y3p, x3p-dx3, y3p-dy3, rightOff, 2);
4066N/A cx = rightOff[2];
4066N/A cy = rightOff[3];
4066N/A if (!(isFinite(cx) && isFinite(cy))) {
4066N/A // both are degenerate. This curve is a line.
4066N/A getLineOffsets(x1, y1, x3, y3, leftOff, rightOff);
4066N/A return 4;
4066N/A }
4066N/A // {left,right}Off[0,1,4,5] are already set to the correct values.
4066N/A leftOff[2] = 2*x2 - cx;
4066N/A leftOff[3] = 2*y2 - cy;
4066N/A return 6;
2956N/A }
2956N/A
4066N/A // rightOff[2,3] = (x2,y2) - ((left_x2, left_y2) - (x2, y2))
4066N/A // == 2*(x2, y2) - (left_x2, left_y2)
4066N/A rightOff[2] = 2*x2 - cx;
4066N/A rightOff[3] = 2*y2 - cy;
4066N/A return 6;
4066N/A }
2956N/A
4066N/A private static boolean isFinite(float x) {
4066N/A return (Float.NEGATIVE_INFINITY < x && x < Float.POSITIVE_INFINITY);
2956N/A }
2956N/A
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.
3444N/A private float[] middle = new float[2*8];
2956N/A private float[] lp = new float[8];
2956N/A private float[] rp = new float[8];
2956N/A private static final int MAX_N_CURVES = 11;
2956N/A private float[] subdivTs = new float[MAX_N_CURVES - 1];
2956N/A
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 switch(type) {
2956N/A case 6:
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 }
2956N/A break;
2956N/A case 8:
2956N/A boolean p1eqp2 = (dxs == 0f && dys == 0f);
2956N/A boolean p3eqp4 = (dxf == 0f && dyf == 0f);
2956N/A if (p1eqp2) {
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];
2956N/A }
2956N/A }
2956N/A if (p3eqp4) {
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 }
2956N/A }
2956N/A }
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 return;
2956N/A }
2956N/A // if these vectors are too small, normalize them, to avoid future
2956N/A // precision problems.
2956N/A if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
4066N/A float len = (float) sqrt(dxs*dxs + dys*dys);
3444N/A dxs /= len;
3444N/A dys /= len;
2956N/A }
2956N/A if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
4066N/A float len = (float) sqrt(dxf*dxf + dyf*dyf);
3444N/A dxf /= len;
3444N/A dyf /= len;
2956N/A }
2956N/A
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);
2956N/A
3444N/A int nSplits = findSubdivPoints(middle, subdivTs, type, lineWidth2);
2956N/A
2956N/A int kind = 0;
3444N/A Iterator<Integer> it = Curve.breakPtsAtTs(middle, type, subdivTs, nSplits);
2956N/A while(it.hasNext()) {
3444N/A int curCurveOff = it.next();
2956N/A
2956N/A switch (type) {
2956N/A case 8:
3444N/A kind = computeOffsetCubic(middle, curCurveOff, lp, rp);
2956N/A break;
2956N/A case 6:
3444N/A kind = computeOffsetQuad(middle, curCurveOff, lp, rp);
2956N/A break;
2956N/A }
4066N/A emitLineTo(lp[0], lp[1]);
4066N/A switch(kind) {
4066N/A case 8:
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 break;
4066N/A case 6:
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 break;
4066N/A case 4:
4066N/A emitLineTo(lp[2], lp[3]);
4066N/A emitLineTo(rp[0], rp[1], true);
4066N/A break;
2956N/A }
4066N/A emitLineTo(rp[kind - 2], rp[kind - 1], true);
2956N/A }
2956N/A
2956N/A this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;
2956N/A this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;
2956N/A this.cdx = dxf;
2956N/A this.cdy = dyf;
2956N/A this.cx0 = xf;
2956N/A this.cy0 = yf;
2956N/A this.prev = DRAWING_OP_TO;
2956N/A }
3719N/A****************************** END WORKAROUND *******************************/
2956N/A
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 private static Curve c = new Curve();
3444N/A private static int findSubdivPoints(float[] pts, float[] ts, final int type, final float w)
2956N/A {
2956N/A final float x12 = pts[2] - pts[0];
2956N/A final float y12 = pts[3] - pts[1];
2956N/A // if the curve is already parallel to either axis we gain nothing
2956N/A // from rotating it.
2956N/A if (y12 != 0f && x12 != 0f) {
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.
4066N/A final float hypot = (float) sqrt(x12 * x12 + y12 * y12);
2956N/A final float cos = x12 / hypot;
2956N/A final float sin = y12 / hypot;
2956N/A final float x1 = cos * pts[0] + sin * pts[1];
2956N/A final float y1 = cos * pts[1] - sin * pts[0];
2956N/A final float x2 = cos * pts[2] + sin * pts[3];
2956N/A final float y2 = cos * pts[3] - sin * pts[2];
2956N/A final float x3 = cos * pts[4] + sin * pts[5];
2956N/A final float y3 = cos * pts[5] - sin * pts[4];
2956N/A switch(type) {
2956N/A case 8:
2956N/A final float x4 = cos * pts[6] + sin * pts[7];
2956N/A final float y4 = cos * pts[7] - sin * pts[6];
2956N/A c.set(x1, y1, x2, y2, x3, y3, x4, y4);
2956N/A break;
2956N/A case 6:
2956N/A c.set(x1, y1, x2, y2, x3, y3);
2956N/A break;
2956N/A }
2956N/A } else {
2956N/A c.set(pts, type);
2956N/A }
2956N/A
2956N/A int ret = 0;
2956N/A // we subdivide at values of t such that the remaining rotated
2956N/A // curves are monotonic in x and y.
2956N/A ret += c.dxRoots(ts, ret);
2956N/A ret += c.dyRoots(ts, ret);
2956N/A // subdivide at inflection points.
2956N/A if (type == 8) {
2956N/A // quadratic curves can't have inflection points
2956N/A ret += c.infPoints(ts, ret);
2956N/A }
2956N/A
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.
2956N/A ret += c.rootsOfROCMinusW(ts, ret, w, 0.0001f);
3444N/A
2956N/A ret = Helpers.filterOutNotInAB(ts, 0, ret, 0.0001f, 0.9999f);
2956N/A Helpers.isort(ts, 0, ret);
2956N/A return ret;
2956N/A }
2956N/A
2956N/A @Override public void curveTo(float x1, float y1,
2956N/A float x2, float y2,
2956N/A float x3, float y3)
2956N/A {
3444N/A middle[0] = cx0; middle[1] = cy0;
3444N/A middle[2] = x1; middle[3] = y1;
3444N/A middle[4] = x2; middle[5] = y2;
3444N/A middle[6] = x3; middle[7] = y3;
3719N/A
3719N/A // inlined version of somethingTo(8);
3719N/A // See the TODO on somethingTo
3719N/A
3719N/A // need these so we can update the state at the end of this method
3719N/A final float xf = middle[6], yf = middle[7];
3719N/A float dxs = middle[2] - middle[0];
3719N/A float dys = middle[3] - middle[1];
3719N/A float dxf = middle[6] - middle[4];
3719N/A float dyf = middle[7] - middle[5];
3719N/A
3719N/A boolean p1eqp2 = (dxs == 0f && dys == 0f);
3719N/A boolean p3eqp4 = (dxf == 0f && dyf == 0f);
3719N/A if (p1eqp2) {
3719N/A dxs = middle[4] - middle[0];
3719N/A dys = middle[5] - middle[1];
3719N/A if (dxs == 0f && dys == 0f) {
3719N/A dxs = middle[6] - middle[0];
3719N/A dys = middle[7] - middle[1];
3719N/A }
3719N/A }
3719N/A if (p3eqp4) {
3719N/A dxf = middle[6] - middle[2];
3719N/A dyf = middle[7] - middle[3];
3719N/A if (dxf == 0f && dyf == 0f) {
3719N/A dxf = middle[6] - middle[0];
3719N/A dyf = middle[7] - middle[1];
3719N/A }
3719N/A }
3719N/A if (dxs == 0f && dys == 0f) {
3719N/A // this happens iff the "curve" is just a point
3719N/A lineTo(middle[0], middle[1]);
3719N/A return;
3719N/A }
2956N/A
3719N/A // if these vectors are too small, normalize them, to avoid future
3719N/A // precision problems.
3719N/A if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
4066N/A float len = (float) sqrt(dxs*dxs + dys*dys);
3719N/A dxs /= len;
3719N/A dys /= len;
3719N/A }
3719N/A if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
4066N/A float len = (float) sqrt(dxf*dxf + dyf*dyf);
3719N/A dxf /= len;
3719N/A dyf /= len;
3719N/A }
3719N/A
3719N/A computeOffset(dxs, dys, lineWidth2, offset[0]);
3719N/A final float mx = offset[0][0];
3719N/A final float my = offset[0][1];
3719N/A drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);
3719N/A
3719N/A int nSplits = findSubdivPoints(middle, subdivTs, 8, lineWidth2);
3719N/A
3719N/A int kind = 0;
3719N/A Iterator<Integer> it = Curve.breakPtsAtTs(middle, 8, subdivTs, nSplits);
3719N/A while(it.hasNext()) {
3719N/A int curCurveOff = it.next();
3719N/A
3719N/A kind = computeOffsetCubic(middle, curCurveOff, lp, rp);
4066N/A emitLineTo(lp[0], lp[1]);
4066N/A switch(kind) {
4066N/A case 8:
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 break;
4066N/A case 4:
4066N/A emitLineTo(lp[2], lp[3]);
4066N/A emitLineTo(rp[0], rp[1], true);
4066N/A break;
3719N/A }
4066N/A emitLineTo(rp[kind - 2], rp[kind - 1], true);
3719N/A }
3719N/A
3719N/A this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;
3719N/A this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;
3719N/A this.cdx = dxf;
3719N/A this.cdy = dyf;
3719N/A this.cx0 = xf;
3719N/A this.cy0 = yf;
3719N/A this.prev = DRAWING_OP_TO;
2956N/A }
2956N/A
2956N/A @Override public void quadTo(float x1, float y1, float x2, float y2) {
3444N/A middle[0] = cx0; middle[1] = cy0;
3444N/A middle[2] = x1; middle[3] = y1;
3444N/A middle[4] = x2; middle[5] = y2;
3719N/A
3719N/A // inlined version of somethingTo(8);
3719N/A // See the TODO on somethingTo
3719N/A
3719N/A // need these so we can update the state at the end of this method
3719N/A final float xf = middle[4], yf = middle[5];
3719N/A float dxs = middle[2] - middle[0];
3719N/A float dys = middle[3] - middle[1];
3719N/A float dxf = middle[4] - middle[2];
3719N/A float dyf = middle[5] - middle[3];
3719N/A if ((dxs == 0f && dys == 0f) || (dxf == 0f && dyf == 0f)) {
3719N/A dxs = dxf = middle[4] - middle[0];
3719N/A dys = dyf = middle[5] - middle[1];
3719N/A }
3719N/A if (dxs == 0f && dys == 0f) {
3719N/A // this happens iff the "curve" is just a point
3719N/A lineTo(middle[0], middle[1]);
3719N/A return;
3719N/A }
3719N/A // if these vectors are too small, normalize them, to avoid future
3719N/A // precision problems.
3719N/A if (Math.abs(dxs) < 0.1f && Math.abs(dys) < 0.1f) {
4066N/A float len = (float) sqrt(dxs*dxs + dys*dys);
3719N/A dxs /= len;
3719N/A dys /= len;
3719N/A }
3719N/A if (Math.abs(dxf) < 0.1f && Math.abs(dyf) < 0.1f) {
4066N/A float len = (float) sqrt(dxf*dxf + dyf*dyf);
3719N/A dxf /= len;
3719N/A dyf /= len;
3719N/A }
3719N/A
3719N/A computeOffset(dxs, dys, lineWidth2, offset[0]);
3719N/A final float mx = offset[0][0];
3719N/A final float my = offset[0][1];
3719N/A drawJoin(cdx, cdy, cx0, cy0, dxs, dys, cmx, cmy, mx, my);
3719N/A
3719N/A int nSplits = findSubdivPoints(middle, subdivTs, 6, lineWidth2);
3719N/A
3719N/A int kind = 0;
3719N/A Iterator<Integer> it = Curve.breakPtsAtTs(middle, 6, subdivTs, nSplits);
3719N/A while(it.hasNext()) {
3719N/A int curCurveOff = it.next();
3719N/A
3719N/A kind = computeOffsetQuad(middle, curCurveOff, lp, rp);
4066N/A emitLineTo(lp[0], lp[1]);
4066N/A switch(kind) {
4066N/A case 6:
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 break;
4066N/A case 4:
4066N/A emitLineTo(lp[2], lp[3]);
4066N/A emitLineTo(rp[0], rp[1], true);
4066N/A break;
3719N/A }
4066N/A emitLineTo(rp[kind - 2], rp[kind - 1], true);
3719N/A }
3719N/A
3719N/A this.cmx = (lp[kind - 2] - rp[kind - 2]) / 2;
3719N/A this.cmy = (lp[kind - 1] - rp[kind - 1]) / 2;
3719N/A this.cdx = dxf;
3719N/A this.cdy = dyf;
3719N/A this.cx0 = xf;
3719N/A this.cy0 = yf;
3719N/A this.prev = DRAWING_OP_TO;
3719N/A }
3719N/A
3719N/A @Override public long getNativeConsumer() {
3719N/A throw new InternalError("Stroker doesn't use a native consumer");
2956N/A }
2956N/A
2956N/A // a stack of polynomial curves where each curve shares endpoints with
2956N/A // adjacent ones.
2956N/A private static final class PolyStack {
2956N/A float[] curves;
2956N/A int end;
2956N/A int[] curveTypes;
2956N/A int numCurves;
2956N/A
2956N/A private static final int INIT_SIZE = 50;
2956N/A
2956N/A PolyStack() {
2956N/A curves = new float[8 * INIT_SIZE];
2956N/A curveTypes = new int[INIT_SIZE];
2956N/A end = 0;
2956N/A numCurves = 0;
2956N/A }
2956N/A
2956N/A public boolean isEmpty() {
2956N/A return numCurves == 0;
2956N/A }
2956N/A
2956N/A private void ensureSpace(int n) {
2956N/A if (end + n >= curves.length) {
2956N/A int newSize = (end + n) * 2;
2956N/A curves = Arrays.copyOf(curves, newSize);
2956N/A }
2956N/A if (numCurves >= curveTypes.length) {
2956N/A int newSize = numCurves * 2;
2956N/A curveTypes = Arrays.copyOf(curveTypes, newSize);
2956N/A }
2956N/A }
2956N/A
2956N/A public void pushCubic(float x0, float y0,
2956N/A float x1, float y1,
2956N/A float x2, float y2)
2956N/A {
2956N/A ensureSpace(6);
2956N/A curveTypes[numCurves++] = 8;
2956N/A // assert(x0 == lastX && y0 == lastY)
2956N/A
2956N/A // we reverse the coordinate order to make popping easier
2956N/A curves[end++] = x2; curves[end++] = y2;
2956N/A curves[end++] = x1; curves[end++] = y1;
2956N/A curves[end++] = x0; curves[end++] = y0;
2956N/A }
2956N/A
2956N/A public void pushQuad(float x0, float y0,
2956N/A float x1, float y1)
2956N/A {
2956N/A ensureSpace(4);
2956N/A curveTypes[numCurves++] = 6;
2956N/A // assert(x0 == lastX && y0 == lastY)
2956N/A curves[end++] = x1; curves[end++] = y1;
2956N/A curves[end++] = x0; curves[end++] = y0;
2956N/A }
2956N/A
2956N/A public void pushLine(float x, float y) {
2956N/A ensureSpace(2);
2956N/A curveTypes[numCurves++] = 4;
2956N/A // assert(x0 == lastX && y0 == lastY)
2956N/A curves[end++] = x; curves[end++] = y;
2956N/A }
2956N/A
2956N/A @SuppressWarnings("unused")
2956N/A public int pop(float[] pts) {
2956N/A int ret = curveTypes[numCurves - 1];
2956N/A numCurves--;
2956N/A end -= (ret - 2);
2956N/A System.arraycopy(curves, end, pts, 0, ret - 2);
2956N/A return ret;
2956N/A }
2956N/A
2956N/A public void pop(PathConsumer2D io) {
2956N/A numCurves--;
2956N/A int type = curveTypes[numCurves];
2956N/A end -= (type - 2);
2956N/A switch(type) {
2956N/A case 8:
2956N/A io.curveTo(curves[end+0], curves[end+1],
2956N/A curves[end+2], curves[end+3],
2956N/A curves[end+4], curves[end+5]);
2956N/A break;
2956N/A case 6:
2956N/A io.quadTo(curves[end+0], curves[end+1],
2956N/A curves[end+2], curves[end+3]);
2956N/A break;
2956N/A case 4:
2956N/A io.lineTo(curves[end], curves[end+1]);
2956N/A }
2956N/A }
2956N/A
2956N/A @Override
2956N/A public String toString() {
2956N/A String ret = "";
2956N/A int nc = numCurves;
2956N/A int end = this.end;
2956N/A while (nc > 0) {
2956N/A nc--;
2956N/A int type = curveTypes[numCurves];
2956N/A end -= (type - 2);
2956N/A switch(type) {
2956N/A case 8:
2956N/A ret += "cubic: ";
2956N/A break;
2956N/A case 6:
2956N/A ret += "quad: ";
2956N/A break;
2956N/A case 4:
2956N/A ret += "line: ";
2956N/A break;
2956N/A }
2956N/A ret += Arrays.toString(Arrays.copyOfRange(curves, end, end+type-2)) + "\n";
2956N/A }
2956N/A return ret;
2956N/A }
0N/A }
0N/A}