📄 flatteningpathiterator.java
字号:
return srcSegType; case PathIterator.SEG_QUADTO: if (stackSize == 0) { coords[0] = srcPosX; coords[1] = srcPosY; } else { int sp = stack.length - 4 * stackSize; coords[0] = stack[sp + 2]; coords[1] = stack[sp + 3]; } return PathIterator.SEG_LINETO; case PathIterator.SEG_CUBICTO: if (stackSize == 0) { coords[0] = srcPosX; coords[1] = srcPosY; } else { int sp = stack.length - 6 * stackSize; coords[0] = stack[sp + 4]; coords[1] = stack[sp + 5]; } return PathIterator.SEG_LINETO; } throw new IllegalStateException(); } // Documentation will be copied from PathIterator. public int currentSegment(float[] coords) { if (done) throw new NoSuchElementException(); switch (srcSegType) { case PathIterator.SEG_CLOSE: return srcSegType; case PathIterator.SEG_MOVETO: case PathIterator.SEG_LINETO: coords[0] = (float) srcPosX; coords[1] = (float) srcPosY; return srcSegType; case PathIterator.SEG_QUADTO: if (stackSize == 0) { coords[0] = (float) srcPosX; coords[1] = (float) srcPosY; } else { int sp = stack.length - 4 * stackSize; coords[0] = (float) stack[sp + 2]; coords[1] = (float) stack[sp + 3]; } return PathIterator.SEG_LINETO; case PathIterator.SEG_CUBICTO: if (stackSize == 0) { coords[0] = (float) srcPosX; coords[1] = (float) srcPosY; } else { int sp = stack.length - 6 * stackSize; coords[0] = (float) stack[sp + 4]; coords[1] = (float) stack[sp + 5]; } return PathIterator.SEG_LINETO; } throw new IllegalStateException(); } /** * Fetches the next segment from the source iterator. */ private void fetchSegment() { int sp; if (srcIter.isDone()) { done = true; return; } srcSegType = srcIter.currentSegment(scratch); switch (srcSegType) { case PathIterator.SEG_CLOSE: return; case PathIterator.SEG_MOVETO: case PathIterator.SEG_LINETO: srcPosX = scratch[0]; srcPosY = scratch[1]; return; case PathIterator.SEG_QUADTO: if (recursionLimit == 0) { srcPosX = scratch[2]; srcPosY = scratch[3]; stackSize = 0; return; } sp = 4 * recursionLimit; stackSize = 1; if (stack == null) { stack = new double[sp + /* 4 + 2 */ 6]; recLevel = new int[recursionLimit + 1]; } recLevel[0] = 0; stack[sp] = srcPosX; // P1.x stack[sp + 1] = srcPosY; // P1.y stack[sp + 2] = scratch[0]; // C.x stack[sp + 3] = scratch[1]; // C.y srcPosX = stack[sp + 4] = scratch[2]; // P2.x srcPosY = stack[sp + 5] = scratch[3]; // P2.y subdivideQuadratic(); break; case PathIterator.SEG_CUBICTO: if (recursionLimit == 0) { srcPosX = scratch[4]; srcPosY = scratch[5]; stackSize = 0; return; } sp = 6 * recursionLimit; stackSize = 1; if ((stack == null) || (stack.length < sp + 8)) { stack = new double[sp + /* 6 + 2 */ 8]; recLevel = new int[recursionLimit + 1]; } recLevel[0] = 0; stack[sp] = srcPosX; // P1.x stack[sp + 1] = srcPosY; // P1.y stack[sp + 2] = scratch[0]; // C1.x stack[sp + 3] = scratch[1]; // C1.y stack[sp + 4] = scratch[2]; // C2.x stack[sp + 5] = scratch[3]; // C2.y srcPosX = stack[sp + 6] = scratch[4]; // P2.x srcPosY = stack[sp + 7] = scratch[5]; // P2.y subdivideCubic(); return; } } /** * Repeatedly subdivides the quadratic curve segment that is on top * of the stack. The iteration terminates when the recursion limit * has been reached, or when the resulting segment is flat enough. */ private void subdivideQuadratic() { int sp; int level; sp = stack.length - 4 * stackSize - 2; level = recLevel[stackSize - 1]; while ((level < recursionLimit) && (QuadCurve2D.getFlatnessSq(stack, sp) >= flatnessSq)) { recLevel[stackSize] = recLevel[stackSize - 1] = ++level; QuadCurve2D.subdivide(stack, sp, stack, sp - 4, stack, sp); ++stackSize; sp -= 4; } } /** * Repeatedly subdivides the cubic curve segment that is on top * of the stack. The iteration terminates when the recursion limit * has been reached, or when the resulting segment is flat enough. */ private void subdivideCubic() { int sp; int level; sp = stack.length - 6 * stackSize - 2; level = recLevel[stackSize - 1]; while ((level < recursionLimit) && (CubicCurve2D.getFlatnessSq(stack, sp) >= flatnessSq)) { recLevel[stackSize] = recLevel[stackSize - 1] = ++level; CubicCurve2D.subdivide(stack, sp, stack, sp - 6, stack, sp); ++stackSize; sp -= 6; } } /* These routines were useful for debugging. Since they would * just bloat the implementation, they are commented out. * * private static String segToString(int segType, double[] d, int offset) { String s; switch (segType) { case PathIterator.SEG_CLOSE: return "SEG_CLOSE"; case PathIterator.SEG_MOVETO: return "SEG_MOVETO (" + d[offset] + ", " + d[offset + 1] + ")"; case PathIterator.SEG_LINETO: return "SEG_LINETO (" + d[offset] + ", " + d[offset + 1] + ")"; case PathIterator.SEG_QUADTO: return "SEG_QUADTO (" + d[offset] + ", " + d[offset + 1] + ") (" + d[offset + 2] + ", " + d[offset + 3] + ")"; case PathIterator.SEG_CUBICTO: return "SEG_CUBICTO (" + d[offset] + ", " + d[offset + 1] + ") (" + d[offset + 2] + ", " + d[offset + 3] + ") (" + d[offset + 4] + ", " + d[offset + 5] + ")"; } throw new IllegalStateException(); } private void dumpQuadraticStack(String msg) { int sp = stack.length - 4 * stackSize - 2; int i = 0; System.err.print(" " + msg + ":"); while (sp < stack.length) { System.err.print(" (" + stack[sp] + ", " + stack[sp+1] + ")"); if (i < recLevel.length) System.out.print("/" + recLevel[i++]); if (sp + 3 < stack.length) System.err.print(" [" + stack[sp+2] + ", " + stack[sp+3] + "]"); sp += 4; } System.err.println(); } private void dumpCubicStack(String msg) { int sp = stack.length - 6 * stackSize - 2; int i = 0; System.err.print(" " + msg + ":"); while (sp < stack.length) { System.err.print(" (" + stack[sp] + ", " + stack[sp+1] + ")"); if (i < recLevel.length) System.out.print("/" + recLevel[i++]); if (sp + 3 < stack.length) { System.err.print(" [" + stack[sp+2] + ", " + stack[sp+3] + "]"); System.err.print(" [" + stack[sp+4] + ", " + stack[sp+5] + "]"); } sp += 6; } System.err.println(); } * * */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -