/*
* Copyright (c) 2007, 2018, 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 com.sun.marlin;
import java.util.Arrays;
import com.sun.marlin.DTransformingPathConsumer2D.CurveBasicMonotonizer;
import com.sun.marlin.DTransformingPathConsumer2D.CurveClipSplitter;
/**
* The DDasher
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.
*
*/
public final class DDasher implements DPathConsumer2D, MarlinConst {
/* huge circle with radius ~ 2E9 only needs 12 subdivision levels */
static final int REC_LIMIT = 16;
static final double CURVE_LEN_ERR = MarlinProperties.getCurveLengthError(); // 0.01 initial
static final double MIN_T_INC = 1.0d / (1 << REC_LIMIT);
// More than 24 bits of mantissa means we can no longer accurately
// measure the number of times cycled through the dash array so we
// punt and override the phase to just be 0 past that point.
static final double MAX_CYCLES = 16000000.0d;
private DPathConsumer2D out;
private double[] dash;
private int dashLen;
private double startPhase;
private boolean startDashOn;
private int startIdx;
private boolean starting;
private boolean needsMoveTo;
private int idx;
private boolean dashOn;
private double phase;
// The starting point of the path
private double sx0, sy0;
// the current point
private double cx0, cy0;
// temporary storage for the current curve
private final double[] curCurvepts;
// per-thread renderer context
final DRendererContext rdrCtx;
// flag to recycle dash array copy
boolean recycleDashes;
// 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 double[] firstSegmentsBuffer; // dynamic array
private int firstSegidx;
// dashes ref (dirty)
final DoubleArrayCache.Reference dashes_ref;
// firstSegmentsBuffer ref (dirty)
final DoubleArrayCache.Reference firstSegmentsBuffer_ref;
// Bounds of the drawing region, at pixel precision.
private double[] clipRect;
// the outcode of the current point
private int cOutCode = 0;
private boolean subdivide = DO_CLIP_SUBDIVIDER;
private final LengthIterator li = new LengthIterator();
private final CurveClipSplitter curveSplitter;
private double cycleLen;
private boolean outside;
private double totalSkipLen;
/**
* Constructs a DDasher
.
* @param rdrCtx per-thread renderer context
*/
DDasher(final DRendererContext rdrCtx) {
this.rdrCtx = rdrCtx;
dashes_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_ARRAY); // 1K
firstSegmentsBuffer_ref = rdrCtx.newDirtyDoubleArrayRef(INITIAL_ARRAY); // 1K
firstSegmentsBuffer = firstSegmentsBuffer_ref.initial;
// we need curCurvepts to be able to contain 2 curves because when
// dashing curves, we need to subdivide it
curCurvepts = new double[8 * 2];
this.curveSplitter = rdrCtx.curveClipSplitter;
}
/**
* Initialize the DDasher
.
*
* @param out an output DPathConsumer2D
.
* @param dash an array of double
s containing the dash pattern
* @param dashLen length of the given dash array
* @param phase a double
containing the dash phase
* @param recycleDashes true to indicate to recycle the given dash array
* @return this instance
*/
public DDasher init(final DPathConsumer2D out, final double[] dash, final int dashLen,
double phase, final boolean recycleDashes)
{
this.out = out;
// Normalize so 0 <= phase < dash[0]
int sidx = 0;
dashOn = true;
// note: BasicStroke constructor checks dash elements and sum > 0
double sum = 0.0d;
for (int i = 0; i < dashLen; i++) {
sum += dash[i];
}
this.cycleLen = sum;
double cycles = phase / sum;
if (phase < 0.0d) {
if (-cycles >= MAX_CYCLES) {
phase = 0.0d;
} else {
int fullcycles = FloatMath.floor_int(-cycles);
if ((fullcycles & dashLen & 1) != 0) {
dashOn = !dashOn;
}
phase += fullcycles * sum;
while (phase < 0.0d) {
if (--sidx < 0) {
sidx = dashLen - 1;
}
phase += dash[sidx];
dashOn = !dashOn;
}
}
} else if (phase > 0.0d) {
if (cycles >= MAX_CYCLES) {
phase = 0.0d;
} else {
int fullcycles = FloatMath.floor_int(cycles);
if ((fullcycles & dashLen & 1) != 0) {
dashOn = !dashOn;
}
phase -= fullcycles * sum;
double d;
while (phase >= (d = dash[sidx])) {
phase -= d;
sidx = (sidx + 1) % dashLen;
dashOn = !dashOn;
}
}
}
this.dash = dash;
this.dashLen = dashLen;
this.phase = phase;
this.startPhase = phase;
this.startDashOn = dashOn;
this.startIdx = sidx;
this.starting = true;
this.needsMoveTo = false;
this.firstSegidx = 0;
this.recycleDashes = recycleDashes;
if (rdrCtx.doClip) {
this.clipRect = rdrCtx.clipRect;
} else {
this.clipRect = null;
this.cOutCode = 0;
}
return this; // fluent API
}
/**
* Disposes this dasher:
* clean up before reusing this instance
*/
void dispose() {
if (DO_CLEAN_DIRTY) {
// Force zero-fill dirty arrays:
Arrays.fill(curCurvepts, 0.0d);
}
// Return arrays:
if (recycleDashes) {
dash = dashes_ref.putArray(dash);
}
firstSegmentsBuffer = firstSegmentsBuffer_ref.putArray(firstSegmentsBuffer);
}
public double[] copyDashArray(final float[] dashes) {
final int len = dashes.length;
final double[] newDashes;
if (len <= MarlinConst.INITIAL_ARRAY) {
newDashes = dashes_ref.initial;
} else {
if (DO_STATS) {
rdrCtx.stats.stat_array_dasher_dasher.add(len);
}
newDashes = dashes_ref.getArray(len);
}
for (int i = 0; i < len; i++) { newDashes[i] = dashes[i]; }
return newDashes;
}
@Override
public void moveTo(final double x0, final double y0) {
if (firstSegidx != 0) {
out.moveTo(sx0, sy0);
emitFirstSegments();
}
this.needsMoveTo = true;
this.idx = startIdx;
this.dashOn = this.startDashOn;
this.phase = this.startPhase;
this.cx0 = x0;
this.cy0 = y0;
// update starting point:
this.sx0 = x0;
this.sy0 = y0;
this.starting = true;
if (clipRect != null) {
final int outcode = DHelpers.outcode(x0, y0, clipRect);
this.cOutCode = outcode;
this.outside = false;
this.totalSkipLen = 0.0d;
}
}
private void emitSeg(double[] buf, int off, int type) {
switch (type) {
case 4:
out.lineTo(buf[off], buf[off + 1]);
return;
case 8:
out.curveTo(buf[off ], buf[off + 1],
buf[off + 2], buf[off + 3],
buf[off + 4], buf[off + 5]);
return;
case 6:
out.quadTo(buf[off ], buf[off + 1],
buf[off + 2], buf[off + 3]);
return;
default:
}
}
private void emitFirstSegments() {
final double[] fSegBuf = firstSegmentsBuffer;
for (int i = 0, len = firstSegidx; i < len; ) {
int type = (int)fSegBuf[i];
emitSeg(fSegBuf, i + 1, type);
i += (type - 1);
}
firstSegidx = 0;
}
// precondition: pts must be in relative coordinates (relative to x0,y0)
private void goTo(final double[] pts, final int off, final int type,
final boolean on)
{
final int index = off + type;
final double x = pts[index - 4];
final double y = pts[index - 3];
if (on) {
if (starting) {
goTo_starting(pts, off, type);
} else {
if (needsMoveTo) {
needsMoveTo = false;
out.moveTo(cx0, cy0);
}
emitSeg(pts, off, type);
}
} else {
if (starting) {
// low probability test (hotspot)
starting = false;
}
needsMoveTo = true;
}
this.cx0 = x;
this.cy0 = y;
}
private void goTo_starting(final double[] pts, final int off, final int type) {
int len = type - 1; // - 2 + 1
int segIdx = firstSegidx;
double[] buf = firstSegmentsBuffer;
if (segIdx + len > buf.length) {
if (DO_STATS) {
rdrCtx.stats.stat_array_dasher_firstSegmentsBuffer
.add(segIdx + len);
}
firstSegmentsBuffer = buf
= firstSegmentsBuffer_ref.widenArray(buf, segIdx,
segIdx + len);
}
buf[segIdx++] = type;
len--;
// small arraycopy (2, 4 or 6) but with offset:
System.arraycopy(pts, off, buf, segIdx, len);
firstSegidx = segIdx + len;
}
@Override
public void lineTo(final double x1, final double y1) {
final int outcode0 = this.cOutCode;
if (clipRect != null) {
final int outcode1 = DHelpers.outcode(x1, y1, clipRect);
// Should clip
final int orCode = (outcode0 | outcode1);
if (orCode != 0) {
final int sideCode = outcode0 & outcode1;
// basic rejection criteria:
if (sideCode == 0) {
// ovelap clip:
if (subdivide) {
// avoid reentrance
subdivide = false;
// subdivide curve => callback with subdivided parts:
boolean ret = curveSplitter.splitLine(cx0, cy0, x1, y1,
orCode, this);
// reentrance is done:
subdivide = true;
if (ret) {
return;
}
}
// already subdivided so render it
} else {
this.cOutCode = outcode1;
skipLineTo(x1, y1);
return;
}
}
this.cOutCode = outcode1;
if (this.outside) {
this.outside = false;
// Adjust current index, phase & dash:
skipLen();
}
}
_lineTo(x1, y1);
}
private void _lineTo(final double x1, final double y1) {
final double dx = x1 - cx0;
final double dy = y1 - cy0;
double len = dx * dx + dy * dy;
if (len == 0.0d) {
return;
}
len = Math.sqrt(len);
// The scaling factors needed to get the dx and dy of the
// transformed dash segments.
final double cx = dx / len;
final double cy = dy / len;
final double[] _curCurvepts = curCurvepts;
final double[] _dash = dash;
final int _dashLen = this.dashLen;
int _idx = idx;
boolean _dashOn = dashOn;
double _phase = phase;
double leftInThisDashSegment, d;
while (true) {
d = _dash[_idx];
leftInThisDashSegment = d - _phase;
if (len <= leftInThisDashSegment) {
_curCurvepts[0] = x1;
_curCurvepts[1] = y1;
goTo(_curCurvepts, 0, 4, _dashOn);
// Advance phase within current dash segment
_phase += len;
// TODO: compare double values using epsilon:
if (len == leftInThisDashSegment) {
_phase = 0.0d;
_idx = (_idx + 1) % _dashLen;
_dashOn = !_dashOn;
}
break;
}
if (_phase == 0.0d) {
_curCurvepts[0] = cx0 + d * cx;
_curCurvepts[1] = cy0 + d * cy;
} else {
_curCurvepts[0] = cx0 + leftInThisDashSegment * cx;
_curCurvepts[1] = cy0 + leftInThisDashSegment * cy;
}
goTo(_curCurvepts, 0, 4, _dashOn);
len -= leftInThisDashSegment;
// Advance to next dash segment
_idx = (_idx + 1) % _dashLen;
_dashOn = !_dashOn;
_phase = 0.0d;
}
// Save local state:
idx = _idx;
dashOn = _dashOn;
phase = _phase;
}
private void skipLineTo(final double x1, final double y1) {
final double dx = x1 - cx0;
final double dy = y1 - cy0;
double len = dx * dx + dy * dy;
if (len != 0.0d) {
len = Math.sqrt(len);
}
// Accumulate skipped length:
this.outside = true;
this.totalSkipLen += len;
// Fix initial move:
this.needsMoveTo = true;
this.starting = false;
this.cx0 = x1;
this.cy0 = y1;
}
public void skipLen() {
double len = this.totalSkipLen;
this.totalSkipLen = 0.0d;
final double[] _dash = dash;
final int _dashLen = this.dashLen;
int _idx = idx;
boolean _dashOn = dashOn;
double _phase = phase;
// -2 to ensure having 2 iterations of the post-loop
// to compensate the remaining phase
final long fullcycles = (long)Math.floor(len / cycleLen) - 2L;
if (fullcycles > 0L) {
len -= cycleLen * fullcycles;
final long iterations = fullcycles * _dashLen;
_idx = (int) (iterations + _idx) % _dashLen;
_dashOn = (iterations + (_dashOn ? 1L : 0L) & 1L) == 1L;
}
double leftInThisDashSegment, d;
while (true) {
d = _dash[_idx];
leftInThisDashSegment = d - _phase;
if (len <= leftInThisDashSegment) {
// Advance phase within current dash segment
_phase += len;
// TODO: compare double values using epsilon:
if (len == leftInThisDashSegment) {
_phase = 0.0d;
_idx = (_idx + 1) % _dashLen;
_dashOn = !_dashOn;
}
break;
}
len -= leftInThisDashSegment;
// Advance to next dash segment
_idx = (_idx + 1) % _dashLen;
_dashOn = !_dashOn;
_phase = 0.0d;
}
// Save local state:
idx = _idx;
dashOn = _dashOn;
phase = _phase;
}
// 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(final int type) {
final double[] _curCurvepts = curCurvepts;
if (pointCurve(_curCurvepts, type)) {
return;
}
final LengthIterator _li = li;
final double[] _dash = dash;
final int _dashLen = this.dashLen;
_li.initializeIterationOnCurve(_curCurvepts, type);
int _idx = idx;
boolean _dashOn = dashOn;
double _phase = phase;
// initially the current curve is at curCurvepts[0...type]
int curCurveoff = 0;
double prevT = 0.0d;
double t;
double leftInThisDashSegment = _dash[_idx] - _phase;
while ((t = _li.next(leftInThisDashSegment)) < 1.0d) {
if (t != 0.0d) {
DHelpers.subdivideAt((t - prevT) / (1.0d - prevT),
_curCurvepts, curCurveoff,
_curCurvepts, 0, type);
prevT = t;
goTo(_curCurvepts, 2, type, _dashOn);
curCurveoff = type;
}
// Advance to next dash segment
_idx = (_idx + 1) % _dashLen;
_dashOn = !_dashOn;
_phase = 0.0d;
leftInThisDashSegment = _dash[_idx];
}
goTo(_curCurvepts, curCurveoff + 2, type, _dashOn);
_phase += _li.lastSegLen();
if (_phase >= _dash[_idx]) {
_phase = 0.0d;
_idx = (_idx + 1) % _dashLen;
_dashOn = !_dashOn;
}
// Save local state:
idx = _idx;
dashOn = _dashOn;
phase = _phase;
// reset LengthIterator:
_li.reset();
}
private void skipSomethingTo(final int type) {
final double[] _curCurvepts = curCurvepts;
if (pointCurve(_curCurvepts, type)) {
return;
}
final LengthIterator _li = li;
_li.initializeIterationOnCurve(_curCurvepts, type);
// In contrary to somethingTo(),
// just estimate properly the curve length:
final double len = _li.totalLength();
// Accumulate skipped length:
this.outside = true;
this.totalSkipLen += len;
// Fix initial move:
this.needsMoveTo = true;
this.starting = false;
}
private static boolean pointCurve(final double[] curve, final 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<