< prev index next >
modules/javafx.graphics/src/main/java/com/sun/marlin/Dasher.java
Print this page
*** 41,56 ****
*/
public final class Dasher implements PathConsumer2D, MarlinConst {
static final int REC_LIMIT = 4;
static final float ERR = 0.01f;
! static final float MIN_T_INC = 1f / (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 float MAX_CYCLES = 16000000f;
private PathConsumer2D out;
private float[] dash;
private int dashLen;
private float startPhase;
--- 41,56 ----
*/
public final class Dasher implements PathConsumer2D, MarlinConst {
static final int REC_LIMIT = 4;
static final float ERR = 0.01f;
! static final float MIN_T_INC = 1.0f / (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 float MAX_CYCLES = 16000000.0f;
private PathConsumer2D out;
private float[] dash;
private int dashLen;
private float startPhase;
*** 114,148 ****
this.out = out;
// Normalize so 0 <= phase < dash[0]
int sidx = 0;
dashOn = true;
! float sum = 0f;
for (float d : dash) {
sum += d;
}
float cycles = phase / sum;
! if (phase < 0f) {
if (-cycles >= MAX_CYCLES) {
! phase = 0f;
} else {
int fullcycles = FloatMath.floor_int(-cycles);
if ((fullcycles & dash.length & 1) != 0) {
dashOn = !dashOn;
}
phase += fullcycles * sum;
! while (phase < 0f) {
if (--sidx < 0) {
sidx = dash.length - 1;
}
phase += dash[sidx];
dashOn = !dashOn;
}
}
} else if (phase > 0) {
if (cycles >= MAX_CYCLES) {
! phase = 0f;
} else {
int fullcycles = FloatMath.floor_int(cycles);
if ((fullcycles & dash.length & 1) != 0) {
dashOn = !dashOn;
}
--- 114,148 ----
this.out = out;
// Normalize so 0 <= phase < dash[0]
int sidx = 0;
dashOn = true;
! float sum = 0.0f;
for (float d : dash) {
sum += d;
}
float cycles = phase / sum;
! if (phase < 0.0f) {
if (-cycles >= MAX_CYCLES) {
! phase = 0.0f;
} else {
int fullcycles = FloatMath.floor_int(-cycles);
if ((fullcycles & dash.length & 1) != 0) {
dashOn = !dashOn;
}
phase += fullcycles * sum;
! while (phase < 0.0f) {
if (--sidx < 0) {
sidx = dash.length - 1;
}
phase += dash[sidx];
dashOn = !dashOn;
}
}
} else if (phase > 0) {
if (cycles >= MAX_CYCLES) {
! phase = 0.0f;
} else {
int fullcycles = FloatMath.floor_int(cycles);
if ((fullcycles & dash.length & 1) != 0) {
dashOn = !dashOn;
}
*** 175,185 ****
* clean up before reusing this instance
*/
void dispose() {
if (DO_CLEAN_DIRTY) {
// Force zero-fill dirty arrays:
! Arrays.fill(curCurvepts, 0f);
}
// Return arrays:
if (recycleDashes) {
dash = dashes_ref.putArray(dash);
}
--- 175,185 ----
* clean up before reusing this instance
*/
void dispose() {
if (DO_CLEAN_DIRTY) {
// Force zero-fill dirty arrays:
! Arrays.fill(curCurvepts, 0.0f);
}
// Return arrays:
if (recycleDashes) {
dash = dashes_ref.putArray(dash);
}
*** 294,304 ****
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
--- 294,304 ----
public void lineTo(float x1, float y1) {
float dx = x1 - x0;
float dy = y1 - y0;
float len = dx*dx + dy*dy;
! if (len == 0.0f) {
return;
}
len = (float) Math.sqrt(len);
// The scaling factors needed to get the dx and dy of the
*** 322,342 ****
// 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;
--- 322,342 ----
// Advance phase within current dash segment
phase += len;
// TODO: compare float values using epsilon:
if (len == leftInThisDashSegment) {
! phase = 0.0f;
idx = (idx + 1) % dashLen;
dashOn = !dashOn;
}
return;
}
dashdx = _dash[idx] * cx;
dashdy = _dash[idx] * cy;
! if (phase == 0.0f) {
_curCurvepts[0] = x0 + dashdx;
_curCurvepts[1] = y0 + dashdy;
} else {
p = leftInThisDashSegment / _dash[idx];
_curCurvepts[0] = x0 + p * dashdx;
*** 347,357 ****
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();
--- 347,357 ----
len -= leftInThisDashSegment;
// Advance to next dash segment
idx = (idx + 1) % dashLen;
dashOn = !dashOn;
! phase = 0.0f;
}
}
// shared instance in Dasher
private final LengthIterator li = new LengthIterator();
*** 364,397 ****
}
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();
--- 364,397 ----
}
li.initializeIterationOnCurve(curCurvepts, type);
// initially the current curve is at curCurvepts[0...type]
int curCurveoff = 0;
! float lastSplitT = 0.0f;
float t;
float leftInThisDashSegment = dash[idx] - phase;
! while ((t = li.next(leftInThisDashSegment)) < 1.0f) {
! if (t != 0.0f) {
! Helpers.subdivideAt((t - lastSplitT) / (1.0f - 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 = 0.0f;
leftInThisDashSegment = dash[idx];
}
goTo(curCurvepts, curCurveoff+2, type);
phase += li.lastSegLen();
if (phase >= dash[idx]) {
! phase = 0.0f;
idx = (idx + 1) % dashLen;
dashOn = !dashOn;
}
// reset LengthIterator:
li.reset();
*** 442,452 ****
private int recLevel;
private boolean done;
// the lengths of the lines of the control polygon. Only its first
// curveType/2 - 1 elements are valid. This is an optimization. See
! // next(float) for more detail.
private final float[] curLeafCtrlPolyLengths = new float[3];
LengthIterator() {
this.recCurveStack = new float[REC_LIMIT + 1][8];
this.sides = new Side[REC_LIMIT];
--- 442,452 ----
private int recLevel;
private boolean done;
// the lengths of the lines of the control polygon. Only its first
// curveType/2 - 1 elements are valid. This is an optimization. See
! // next() for more detail.
private final float[] curLeafCtrlPolyLengths = new float[3];
LengthIterator() {
this.recCurveStack = new float[REC_LIMIT + 1][8];
this.sides = new Side[REC_LIMIT];
*** 467,506 ****
// keep data dirty
// as it appears not useful to reset data:
if (DO_CLEAN_DIRTY) {
final int recLimit = recCurveStack.length - 1;
for (int i = recLimit; i >= 0; i--) {
! Arrays.fill(recCurveStack[i], 0f);
}
Arrays.fill(sides, Side.LEFT);
! Arrays.fill(curLeafCtrlPolyLengths, 0f);
! Arrays.fill(nextRoots, 0f);
! Arrays.fill(flatLeafCoefCache, 0f);
! flatLeafCoefCache[2] = -1f;
}
}
void initializeIterationOnCurve(float[] pts, int type) {
// optimize arraycopy (8 values faster than 6 = type):
System.arraycopy(pts, 0, recCurveStack[0], 0, 8);
this.curveType = type;
this.recLevel = 0;
! this.lastT = 0f;
! this.lenAtLastT = 0f;
! this.nextT = 0f;
! this.lenAtNextT = 0f;
goLeft(); // initializes nextT and lenAtNextT properly
! this.lenAtLastSplit = 0f;
if (recLevel > 0) {
this.sides[0] = Side.LEFT;
this.done = false;
} else {
// the root of the tree is a leaf so we're done.
this.sides[0] = Side.RIGHT;
this.done = true;
}
! this.lastSegLen = 0f;
}
// 0 == false, 1 == true, -1 == invalid cached value.
private int cachedHaveLowAcceleration = -1;
--- 467,506 ----
// keep data dirty
// as it appears not useful to reset data:
if (DO_CLEAN_DIRTY) {
final int recLimit = recCurveStack.length - 1;
for (int i = recLimit; i >= 0; i--) {
! Arrays.fill(recCurveStack[i], 0.0f);
}
Arrays.fill(sides, Side.LEFT);
! Arrays.fill(curLeafCtrlPolyLengths, 0.0f);
! Arrays.fill(nextRoots, 0.0f);
! Arrays.fill(flatLeafCoefCache, 0.0f);
! flatLeafCoefCache[2] = -1.0f;
}
}
void initializeIterationOnCurve(float[] pts, int type) {
// optimize arraycopy (8 values faster than 6 = type):
System.arraycopy(pts, 0, recCurveStack[0], 0, 8);
this.curveType = type;
this.recLevel = 0;
! this.lastT = 0.0f;
! this.lenAtLastT = 0.0f;
! this.nextT = 0.0f;
! this.lenAtNextT = 0.0f;
goLeft(); // initializes nextT and lenAtNextT properly
! this.lenAtLastSplit = 0.0f;
if (recLevel > 0) {
this.sides[0] = Side.LEFT;
this.done = false;
} else {
// the root of the tree is a leaf so we're done.
this.sides[0] = Side.RIGHT;
this.done = true;
}
! this.lastSegLen = 0.0f;
}
// 0 == false, 1 == true, -1 == invalid cached value.
private int cachedHaveLowAcceleration = -1;
*** 540,560 ****
// caches the coefficients of the current leaf in its flattened
// form (see inside next() for what that means). The cache is
// invalid when it's third element is negative, since in any
// valid flattened curve, this would be >= 0.
! private final float[] flatLeafCoefCache = new float[]{0f, 0f, -1f, 0f};
// returns the t value where the remaining curve should be split in
// order for the left subdivided curve to have length len. If len
// is >= than the length of the uniterated curve, it returns 1.
float next(final float len) {
final float targetLength = lenAtLastSplit + len;
while (lenAtNextT < targetLength) {
if (done) {
lastSegLen = lenAtNextT - lenAtLastSplit;
! return 1f;
}
goToNextLeaf();
}
lenAtLastSplit = targetLength;
final float leaflen = lenAtNextT - lenAtLastT;
--- 540,560 ----
// caches the coefficients of the current leaf in its flattened
// form (see inside next() for what that means). The cache is
// invalid when it's third element is negative, since in any
// valid flattened curve, this would be >= 0.
! private final float[] flatLeafCoefCache = new float[]{0.0f, 0.0f, -1.0f, 0.0f};
// returns the t value where the remaining curve should be split in
// order for the left subdivided curve to have length len. If len
// is >= than the length of the uniterated curve, it returns 1.
float next(final float len) {
final float targetLength = lenAtLastSplit + len;
while (lenAtNextT < targetLength) {
if (done) {
lastSegLen = lenAtNextT - lenAtLastSplit;
! return 1.0f;
}
goToNextLeaf();
}
lenAtLastSplit = targetLength;
final float leaflen = lenAtNextT - lenAtLastT;
*** 567,589 ****
// left with a, b, c which define a 1D Bezier curve. We then
// solve this to get the parameter of the original leaf that
// gives us the desired length.
final float[] _flatLeafCoefCache = flatLeafCoefCache;
! if (_flatLeafCoefCache[2] < 0f) {
! float x = 0f + curLeafCtrlPolyLengths[0],
! y = x + curLeafCtrlPolyLengths[1];
if (curveType == 8) {
float z = y + curLeafCtrlPolyLengths[2];
! _flatLeafCoefCache[0] = 3f * (x - y) + z;
! _flatLeafCoefCache[1] = 3f * (y - 2f * x);
! _flatLeafCoefCache[2] = 3f * x;
_flatLeafCoefCache[3] = -z;
} else if (curveType == 6) {
! _flatLeafCoefCache[0] = 0f;
! _flatLeafCoefCache[1] = y - 2f * x;
! _flatLeafCoefCache[2] = 2f * x;
_flatLeafCoefCache[3] = -y;
}
}
float a = _flatLeafCoefCache[0];
float b = _flatLeafCoefCache[1];
--- 567,589 ----
// left with a, b, c which define a 1D Bezier curve. We then
// solve this to get the parameter of the original leaf that
// gives us the desired length.
final float[] _flatLeafCoefCache = flatLeafCoefCache;
! if (_flatLeafCoefCache[2] < 0.0f) {
! float x = curLeafCtrlPolyLengths[0],
! y = x + curLeafCtrlPolyLengths[1];
if (curveType == 8) {
float z = y + curLeafCtrlPolyLengths[2];
! _flatLeafCoefCache[0] = 3.0f * (x - y) + z;
! _flatLeafCoefCache[1] = 3.0f * (y - 2.0f * x);
! _flatLeafCoefCache[2] = 3.0f * x;
_flatLeafCoefCache[3] = -z;
} else if (curveType == 6) {
! _flatLeafCoefCache[0] = 0.0f;
! _flatLeafCoefCache[1] = y - 2.0f * x;
! _flatLeafCoefCache[2] = 2.0f * x;
_flatLeafCoefCache[3] = -y;
}
}
float a = _flatLeafCoefCache[0];
float b = _flatLeafCoefCache[1];
*** 591,610 ****
float d = t * _flatLeafCoefCache[3];
// we use cubicRootsInAB here, because we want only roots in 0, 1,
// and our quadratic root finder doesn't filter, so it's just a
// matter of convenience.
! int n = Helpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0f, 1f);
if (n == 1 && !Float.isNaN(nextRoots[0])) {
t = nextRoots[0];
}
}
// t is relative to the current leaf, so we must make it a valid parameter
// of the original curve.
t = t * (nextT - lastT) + lastT;
! if (t >= 1f) {
! t = 1f;
done = true;
}
// even if done = true, if we're here, that means targetLength
// is equal to, or very, very close to the total length of the
// curve, so lastSegLen won't be too high. In cases where len
--- 591,610 ----
float d = t * _flatLeafCoefCache[3];
// we use cubicRootsInAB here, because we want only roots in 0, 1,
// and our quadratic root finder doesn't filter, so it's just a
// matter of convenience.
! int n = Helpers.cubicRootsInAB(a, b, c, d, nextRoots, 0, 0.0f, 1.0f);
if (n == 1 && !Float.isNaN(nextRoots[0])) {
t = nextRoots[0];
}
}
// t is relative to the current leaf, so we must make it a valid parameter
// of the original curve.
t = t * (nextT - lastT) + lastT;
! if (t >= 1.0f) {
! t = 1.0f;
done = true;
}
// even if done = true, if we're here, that means targetLength
// is equal to, or very, very close to the total length of the
// curve, so lastSegLen won't be too high. In cases where len
*** 647,663 ****
}
// go to the leftmost node from the current node. Return its length.
private void goLeft() {
float len = onLeaf();
! if (len >= 0f) {
lastT = nextT;
lenAtLastT = lenAtNextT;
nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC;
lenAtNextT += len;
// invalidate caches
! flatLeafCoefCache[2] = -1f;
cachedHaveLowAcceleration = -1;
} else {
Helpers.subdivide(recCurveStack[recLevel], 0,
recCurveStack[recLevel+1], 0,
recCurveStack[recLevel], 0, curveType);
--- 647,663 ----
}
// go to the leftmost node from the current node. Return its length.
private void goLeft() {
float len = onLeaf();
! if (len >= 0.0f) {
lastT = nextT;
lenAtLastT = lenAtNextT;
nextT += (1 << (REC_LIMIT - recLevel)) * MIN_T_INC;
lenAtNextT += len;
// invalidate caches
! flatLeafCoefCache[2] = -1.0f;
cachedHaveLowAcceleration = -1;
} else {
Helpers.subdivide(recCurveStack[recLevel], 0,
recCurveStack[recLevel+1], 0,
recCurveStack[recLevel], 0, curveType);
*** 669,679 ****
// this is a bit of a hack. It returns -1 if we're not on a leaf, and
// the length of the leaf if we are on a leaf.
private float onLeaf() {
float[] curve = recCurveStack[recLevel];
! float polyLen = 0f;
float x0 = curve[0], y0 = curve[1];
for (int i = 2; i < curveType; i += 2) {
final float x1 = curve[i], y1 = curve[i+1];
final float len = Helpers.linelen(x0, y0, x1, y1);
--- 669,679 ----
// this is a bit of a hack. It returns -1 if we're not on a leaf, and
// the length of the leaf if we are on a leaf.
private float onLeaf() {
float[] curve = recCurveStack[recLevel];
! float polyLen = 0.0f;
float x0 = curve[0], y0 = curve[1];
for (int i = 2; i < curveType; i += 2) {
final float x1 = curve[i], y1 = curve[i+1];
final float len = Helpers.linelen(x0, y0, x1, y1);
*** 685,697 ****
final float lineLen = Helpers.linelen(curve[0], curve[1],
curve[curveType-2],
curve[curveType-1]);
if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) {
! return (polyLen + lineLen) / 2f;
}
! return -1f;
}
}
@Override
public void curveTo(float x1, float y1,
--- 685,697 ----
final float lineLen = Helpers.linelen(curve[0], curve[1],
curve[curveType-2],
curve[curveType-1]);
if ((polyLen - lineLen) < ERR || recLevel == REC_LIMIT) {
! return (polyLen + lineLen) / 2.0f;
}
! return -1.0f;
}
}
@Override
public void curveTo(float x1, float y1,
< prev index next >