--- /dev/null 2015-11-20 13:32:52.000000000 -0800
+++ new/src/java.desktop/share/classes/sun/java2d/marlin/Dasher.java 2015-11-20 13:32:52.000000000 -0800
@@ -0,0 +1,702 @@
+/*
+ * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package sun.java2d.marlin;
+
+import java.util.Arrays;
+import sun.awt.geom.PathConsumer2D;
+
+/**
+ * The Dasher
class takes a series of linear commands
+ * (moveTo
, lineTo
, close
and
+ * end
) and breaks them into smaller segments according to a
+ * dash pattern array and a starting dash phase.
+ *
+ *
Issues: in J2Se, a zero length dash segment as drawn as a very
+ * short dash, whereas Pisces does not draw anything. The PostScript
+ * semantics are unclear.
+ *
+ */
+final class Dasher implements sun.awt.geom.PathConsumer2D, MarlinConst {
+
+ static final int recLimit = 4;
+ static final float ERR = 0.01f;
+ static final float minTincrement = 1f / (1 << recLimit);
+
+ private PathConsumer2D out;
+ private float[] dash;
+ private int dashLen;
+ private float startPhase;
+ private boolean startDashOn;
+ private int startIdx;
+
+ private boolean starting;
+ private boolean needsMoveTo;
+
+ private int idx;
+ private boolean dashOn;
+ private float phase;
+
+ private float sx, sy;
+ private float x0, y0;
+
+ // temporary storage for the current curve
+ private final float[] curCurvepts;
+
+ // per-thread renderer context
+ final RendererContext rdrCtx;
+
+ // dashes array (dirty)
+ final float[] dashes_initial = new float[INITIAL_ARRAY];
+
+ // flag to recycle dash array copy
+ boolean recycleDashes;
+
+ // per-thread initial arrays (large enough to satisfy most usages
+ // +1 to avoid recycling in Helpers.widenArray()
+ private final float[] firstSegmentsBuffer_initial = new float[INITIAL_ARRAY + 1];
+
+ /**
+ * Constructs a Dasher
.
+ * @param rdrCtx per-thread renderer context
+ */
+ Dasher(final RendererContext rdrCtx) {
+ this.rdrCtx = rdrCtx;
+
+ firstSegmentsBuffer = firstSegmentsBuffer_initial;
+
+ // we need curCurvepts to be able to contain 2 curves because when
+ // dashing curves, we need to subdivide it
+ curCurvepts = new float[8 * 2];
+ }
+
+ /**
+ * Initialize the Dasher
.
+ *
+ * @param out an output PathConsumer2D
.
+ * @param dash an array of float
s containing the dash pattern
+ * @param dashLen length of the given dash array
+ * @param phase a float
containing the dash phase
+ * @param recycleDashes true to indicate to recycle the given dash array
+ * @return this instance
+ */
+ Dasher init(final PathConsumer2D out, float[] dash, int dashLen,
+ float phase, boolean recycleDashes)
+ {
+ if (phase < 0f) {
+ throw new IllegalArgumentException("phase < 0 !");
+ }
+ this.out = out;
+
+ // Normalize so 0 <= phase < dash[0]
+ int idx = 0;
+ dashOn = true;
+ float d;
+ while (phase >= (d = dash[idx])) {
+ phase -= d;
+ idx = (idx + 1) % dashLen;
+ dashOn = !dashOn;
+ }
+
+ this.dash = dash;
+ this.dashLen = dashLen;
+ this.startPhase = this.phase = phase;
+ this.startDashOn = dashOn;
+ this.startIdx = idx;
+ this.starting = true;
+ needsMoveTo = false;
+ firstSegidx = 0;
+
+ this.recycleDashes = recycleDashes;
+
+ return this; // fluent API
+ }
+
+ /**
+ * Disposes this dasher:
+ * clean up before reusing this instance
+ */
+ void dispose() {
+ if (doCleanDirty) {
+ // Force zero-fill dirty arrays:
+ Arrays.fill(curCurvepts, 0f);
+ Arrays.fill(firstSegmentsBuffer, 0f);
+ }
+ // Return arrays:
+ if (recycleDashes && dash != dashes_initial) {
+ rdrCtx.putDirtyFloatArray(dash);
+ dash = null;
+ }
+
+ if (firstSegmentsBuffer != firstSegmentsBuffer_initial) {
+ rdrCtx.putDirtyFloatArray(firstSegmentsBuffer);
+ firstSegmentsBuffer = firstSegmentsBuffer_initial;
+ }
+ }
+
+ @Override
+ public void moveTo(float x0, float y0) {
+ if (firstSegidx > 0) {
+ out.moveTo(sx, sy);
+ emitFirstSegments();
+ }
+ needsMoveTo = true;
+ this.idx = startIdx;
+ this.dashOn = this.startDashOn;
+ this.phase = this.startPhase;
+ this.sx = this.x0 = x0;
+ this.sy = this.y0 = y0;
+ this.starting = true;
+ }
+
+ private void emitSeg(float[] buf, int off, int type) {
+ switch (type) {
+ case 8:
+ out.curveTo(buf[off+0], buf[off+1],
+ buf[off+2], buf[off+3],
+ buf[off+4], buf[off+5]);
+ return;
+ case 6:
+ out.quadTo(buf[off+0], buf[off+1],
+ buf[off+2], buf[off+3]);
+ return;
+ case 4:
+ out.lineTo(buf[off], buf[off+1]);
+ return;
+ default:
+ }
+ }
+
+ private void emitFirstSegments() {
+ final float[] fSegBuf = firstSegmentsBuffer;
+
+ for (int i = 0; i < firstSegidx; ) {
+ int type = (int)fSegBuf[i];
+ emitSeg(fSegBuf, i + 1, type);
+ i += (type - 1);
+ }
+ firstSegidx = 0;
+ }
+ // We don't emit the first dash right away. If we did, caps would be
+ // drawn on it, but we need joins to be drawn if there's a closePath()
+ // So, we store the path elements that make up the first dash in the
+ // buffer below.
+ private float[] firstSegmentsBuffer; // dynamic array
+ private int firstSegidx;
+
+ // precondition: pts must be in relative coordinates (relative to x0,y0)
+ // fullCurve is true iff the curve in pts has not been split.
+ private void goTo(float[] pts, int off, final int type) {
+ float x = pts[off + type - 4];
+ float y = pts[off + type - 3];
+ if (dashOn) {
+ if (starting) {
+ int len = type - 2 + 1;
+ int segIdx = firstSegidx;
+ float[] buf = firstSegmentsBuffer;
+ if (segIdx + len > buf.length) {
+ if (doStats) {
+ RendererContext.stats.stat_array_dasher_firstSegmentsBuffer
+ .add(segIdx + len);
+ }
+ firstSegmentsBuffer = buf
+ = rdrCtx.widenDirtyFloatArray(buf, segIdx, segIdx + len);
+ }
+ buf[segIdx++] = type;
+ len--;
+ // small arraycopy (2, 4 or 6) but with offset:
+ System.arraycopy(pts, off, buf, segIdx, len);
+ segIdx += len;
+ firstSegidx = segIdx;
+ } else {
+ if (needsMoveTo) {
+ out.moveTo(x0, y0);
+ needsMoveTo = false;
+ }
+ emitSeg(pts, off, type);
+ }
+ } else {
+ starting = false;
+ needsMoveTo = true;
+ }
+ this.x0 = x;
+ this.y0 = y;
+ }
+
+ @Override
+ public void lineTo(float x1, float y1) {
+ float dx = x1 - x0;
+ float dy = y1 - y0;
+
+ float len = dx*dx + dy*dy;
+ if (len == 0f) {
+ return;
+ }
+ len = (float) Math.sqrt(len);
+
+ // The scaling factors needed to get the dx and dy of the
+ // transformed dash segments.
+ final float cx = dx / len;
+ final float cy = dy / len;
+
+ final float[] _curCurvepts = curCurvepts;
+ final float[] _dash = dash;
+
+ float leftInThisDashSegment;
+ float dashdx, dashdy, p;
+
+ while (true) {
+ leftInThisDashSegment = _dash[idx] - phase;
+
+ if (len <= leftInThisDashSegment) {
+ _curCurvepts[0] = x1;
+ _curCurvepts[1] = y1;
+ goTo(_curCurvepts, 0, 4);
+
+ // Advance phase within current dash segment
+ phase += len;
+ // TODO: compare float values using epsilon:
+ if (len == leftInThisDashSegment) {
+ phase = 0f;
+ idx = (idx + 1) % dashLen;
+ dashOn = !dashOn;
+ }
+ return;
+ }
+
+ dashdx = _dash[idx] * cx;
+ dashdy = _dash[idx] * cy;
+
+ if (phase == 0f) {
+ _curCurvepts[0] = x0 + dashdx;
+ _curCurvepts[1] = y0 + dashdy;
+ } else {
+ p = leftInThisDashSegment / _dash[idx];
+ _curCurvepts[0] = x0 + p * dashdx;
+ _curCurvepts[1] = y0 + p * dashdy;
+ }
+
+ goTo(_curCurvepts, 0, 4);
+
+ len -= leftInThisDashSegment;
+ // Advance to next dash segment
+ idx = (idx + 1) % dashLen;
+ dashOn = !dashOn;
+ phase = 0f;
+ }
+ }
+
+ // shared instance in Dasher
+ private final LengthIterator li = new LengthIterator();
+
+ // preconditions: curCurvepts must be an array of length at least 2 * type,
+ // that contains the curve we want to dash in the first type elements
+ private void somethingTo(int type) {
+ if (pointCurve(curCurvepts, type)) {
+ return;
+ }
+ li.initializeIterationOnCurve(curCurvepts, type);
+
+ // initially the current curve is at curCurvepts[0...type]
+ int curCurveoff = 0;
+ float lastSplitT = 0f;
+ float t;
+ float leftInThisDashSegment = dash[idx] - phase;
+
+ while ((t = li.next(leftInThisDashSegment)) < 1f) {
+ if (t != 0f) {
+ Helpers.subdivideAt((t - lastSplitT) / (1f - lastSplitT),
+ curCurvepts, curCurveoff,
+ curCurvepts, 0,
+ curCurvepts, type, type);
+ lastSplitT = t;
+ goTo(curCurvepts, 2, type);
+ curCurveoff = type;
+ }
+ // Advance to next dash segment
+ idx = (idx + 1) % dashLen;
+ dashOn = !dashOn;
+ phase = 0f;
+ leftInThisDashSegment = dash[idx];
+ }
+ goTo(curCurvepts, curCurveoff+2, type);
+ phase += li.lastSegLen();
+ if (phase >= dash[idx]) {
+ phase = 0f;
+ idx = (idx + 1) % dashLen;
+ dashOn = !dashOn;
+ }
+ // reset LengthIterator:
+ li.reset();
+ }
+
+ private static boolean pointCurve(float[] curve, int type) {
+ for (int i = 2; i < type; i++) {
+ if (curve[i] != curve[i-2]) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ // Objects of this class are used to iterate through curves. They return
+ // t values where the left side of the curve has a specified length.
+ // It does this by subdividing the input curve until a certain error
+ // condition has been met. A recursive subdivision procedure would
+ // return as many as 1<