Example usage for java.awt.geom PathIterator currentSegment

List of usage examples for java.awt.geom PathIterator currentSegment

Introduction

In this page you can find the example usage for java.awt.geom PathIterator currentSegment.

Prototype

public int currentSegment(double[] coords);

Source Link

Document

Returns the coordinates and type of the current path segment in the iteration.

Usage

From source file:com.jhlabs.awt.TextStroke.java

public Shape createStrokedShape(Shape shape) {
    FontRenderContext frc = new FontRenderContext(null, true, true);
    GlyphVector glyphVector = font.createGlyphVector(frc, text);

    GeneralPath result = new GeneralPath();
    PathIterator it = new FlatteningPathIterator(shape.getPathIterator(null), FLATNESS);
    float points[] = new float[6];
    float moveX = 0, moveY = 0;
    float lastX = 0, lastY = 0;
    float thisX = 0, thisY = 0;
    int type = 0;
    float next = 0;
    int currentChar = 0;
    int length = glyphVector.getNumGlyphs();

    if (length == 0)
        return result;

    float factor = stretchToFit ? measurePathLength(shape) / (float) glyphVector.getLogicalBounds().getWidth()
            : 1.0f;//from   w  ww . j  av a  2s . c o  m
    float height = (float) glyphVector.getLogicalBounds().getHeight();
    float nextAdvance = 0;

    while (currentChar < length && !it.isDone()) {
        type = it.currentSegment(points);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            moveX = lastX = points[0];
            moveY = lastY = points[1];
            result.moveTo(moveX, moveY);
            nextAdvance = glyphVector.getGlyphMetrics(currentChar).getAdvance() * 0.5f;
            next = nextAdvance;
            break;

        case PathIterator.SEG_CLOSE:
            points[0] = moveX;
            points[1] = moveY;
            // Fall into....

        case PathIterator.SEG_LINETO:
            thisX = points[0];
            thisY = points[1];
            float dx = thisX - lastX;
            float dy = thisY - lastY;
            float distance = (float) FastMath.sqrt(dx * dx + dy * dy);
            if (distance >= next) {
                float r = 1.0f / distance;
                float angle = (float) FastMath.atan2(dy, dx);
                while (currentChar < length && distance >= next) {
                    Shape glyph = glyphVector.getGlyphOutline(currentChar);
                    Point2D p = glyphVector.getGlyphPosition(currentChar);
                    float px = (float) p.getX();
                    float py = (float) p.getY();
                    float x = lastX + next * dx * r;
                    float y = lastY + next * dy * r;
                    float advance = nextAdvance;
                    nextAdvance = currentChar < length - 1
                            ? glyphVector.getGlyphMetrics(currentChar + 1).getAdvance() * 0.5f
                            : 0;
                    t.setToTranslation(x, y);
                    t.rotate(angle);
                    t.translate(-px - advance, -py + height * factor / 2.0f);
                    result.append(t.createTransformedShape(glyph), false);
                    next += (advance + nextAdvance) * factor;
                    currentChar++;
                    if (repeat)
                        currentChar %= length;
                }
            }
            next -= distance;
            lastX = thisX;
            lastY = thisY;
            break;
        }
        it.next();
    }

    return result;
}

From source file:ExtendedGeneralPath.java

/**
 * Delegates to the enclosed <code>GeneralPath</code>.
 *///from  w w w.j  a v  a2s . co m
public void append(PathIterator pi, boolean connect) {
    double[] vals = new double[6];

    while (!pi.isDone()) {
        Arrays.fill(vals, 0);
        int type = pi.currentSegment(vals);
        pi.next();
        if (connect && (numVals != 0)) {
            if (type == PathIterator.SEG_MOVETO) {
                double x = vals[0];
                double y = vals[1];
                if ((x != cx) || (y != cy)) {
                    // Change MOVETO to LINETO.
                    type = PathIterator.SEG_LINETO;
                } else {
                    // Redundent segment (move to current loc) drop it...
                    if (pi.isDone())
                        break; // Nothing interesting
                    type = pi.currentSegment(vals);
                    pi.next();
                }
            }
            connect = false;
        }

        switch (type) {
        case PathIterator.SEG_CLOSE:
            closePath();
            break;
        case PathIterator.SEG_MOVETO:
            moveTo((float) vals[0], (float) vals[1]);
            break;
        case PathIterator.SEG_LINETO:
            lineTo((float) vals[0], (float) vals[1]);
            break;
        case PathIterator.SEG_QUADTO:
            quadTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            curveTo((float) vals[0], (float) vals[1], (float) vals[2], (float) vals[3], (float) vals[4],
                    (float) vals[5]);
            break;
        }
    }
}

From source file:SWTGraphics2D.java

/**
 * Converts an AWT <code>Shape</code> into a SWT <code>Path</code>.
 *
 * @param shape  the shape (<code>null</code> not permitted).
 *
 * @return The path.//from   ww w . ja v  a2 s  .c  o m
 */
private Path toSwtPath(Shape shape) {
    int type;
    float[] coords = new float[6];
    Path path = new Path(this.gc.getDevice());
    PathIterator pit = shape.getPathIterator(null);
    while (!pit.isDone()) {
        type = pit.currentSegment(coords);
        switch (type) {
        case (PathIterator.SEG_MOVETO):
            path.moveTo(coords[0], coords[1]);
            break;
        case (PathIterator.SEG_LINETO):
            path.lineTo(coords[0], coords[1]);
            break;
        case (PathIterator.SEG_QUADTO):
            path.quadTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        case (PathIterator.SEG_CUBICTO):
            path.cubicTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
        case (PathIterator.SEG_CLOSE):
            path.close();
            break;
        default:
            break;
        }
        pit.next();
    }
    return path;
}

From source file:PathLength.java

/**
 * Flattens the path and determines the path length.
 *//*from  ww  w.j  a va 2  s.  co  m*/
protected void initialise() {
    pathLength = 0f;

    PathIterator pi = path.getPathIterator(new AffineTransform());
    SingleSegmentPathIterator sspi = new SingleSegmentPathIterator();
    segments = new ArrayList(20);
    List indexes = new ArrayList(20);
    int index = 0;
    int origIndex = -1;
    float lastMoveX = 0f;
    float lastMoveY = 0f;
    float currentX = 0f;
    float currentY = 0f;
    float[] seg = new float[6];
    int segType;

    segments.add(new PathSegment(PathIterator.SEG_MOVETO, 0f, 0f, 0f, origIndex));

    while (!pi.isDone()) {
        origIndex++;
        indexes.add(new Integer(index));
        segType = pi.currentSegment(seg);
        switch (segType) {
        case PathIterator.SEG_MOVETO:
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            lastMoveX = currentX;
            lastMoveY = currentY;
            index++;
            pi.next();
            break;
        case PathIterator.SEG_LINETO:
            pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
            segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
            currentX = seg[0];
            currentY = seg[1];
            index++;
            pi.next();
            break;
        case PathIterator.SEG_CLOSE:
            pathLength += Point2D.distance(currentX, currentY, lastMoveX, lastMoveY);
            segments.add(new PathSegment(PathIterator.SEG_LINETO, lastMoveX, lastMoveY, pathLength, origIndex));
            currentX = lastMoveX;
            currentY = lastMoveY;
            index++;
            pi.next();
            break;
        default:
            sspi.setPathIterator(pi, currentX, currentY);
            FlatteningPathIterator fpi = new FlatteningPathIterator(sspi, 0.01f);
            while (!fpi.isDone()) {
                segType = fpi.currentSegment(seg);
                if (segType == PathIterator.SEG_LINETO) {
                    pathLength += Point2D.distance(currentX, currentY, seg[0], seg[1]);
                    segments.add(new PathSegment(segType, seg[0], seg[1], pathLength, origIndex));
                    currentX = seg[0];
                    currentY = seg[1];
                    index++;
                }
                fpi.next();
            }
        }
    }
    segmentIndexes = new int[indexes.size()];
    for (int i = 0; i < segmentIndexes.length; i++) {
        segmentIndexes[i] = ((Integer) indexes.get(i)).intValue();
    }
    initialised = true;
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Handle the Batik drawing event//w  ww . ja  v a  2 s.  c  om
 *
 * @param shape
 *            the shape to draw
 * @param fill
 *            true if the shape is to be drawn filled
 */
private void doDrawing(Shape shape, boolean fill) {
    if (!fill) {
        graphicsObj.newSegment();
    }

    graphicsObj.setColor(gc.getColor());

    applyPaint(gc.getPaint(), fill);

    if (fill) {
        graphicsObj.beginArea();
    } else {
        applyStroke(gc.getStroke());
    }

    AffineTransform trans = gc.getTransform();
    PathIterator iter = shape.getPathIterator(trans);
    if (shape instanceof Line2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.currentSegment(dstPts);
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        graphicsObj.addLine(coords);
    } else if (shape instanceof Rectangle2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.next();
        iter.currentSegment(dstPts);
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        graphicsObj.addBox(coords);
    } else if (shape instanceof Ellipse2D) {
        double[] dstPts = new double[6];
        Ellipse2D elip = (Ellipse2D) shape;
        double scale = trans.getScaleX();
        double radiusWidth = elip.getWidth() / 2;
        double radiusHeight = elip.getHeight() / 2;
        graphicsObj.setArcParams((int) Math.round(radiusWidth * scale), (int) Math.round(radiusHeight * scale),
                0, 0);
        double[] srcPts = new double[] { elip.getCenterX(), elip.getCenterY() };
        trans.transform(srcPts, 0, dstPts, 0, 1);
        final int mh = 1;
        final int mhr = 0;
        graphicsObj.addFullArc((int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]), mh, mhr);
    } else {
        processPathIterator(iter);
    }

    if (fill) {
        graphicsObj.endArea();
    }
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Processes a path iterator generating the necessary painting operations.
 *
 * @param iter PathIterator to process/*from w ww.j  av a 2 s .com*/
 */
private void processPathIterator(PathIterator iter) {
    double[] dstPts = new double[6];
    double[] currentPosition = new double[2];
    for (int[] openingCoords = new int[2]; !iter.isDone(); iter.next()) {
        switch (iter.currentSegment(dstPts)) {
        case PathIterator.SEG_LINETO:
            graphicsObj.addLine(new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) }, true);
            currentPosition = new double[] { dstPts[X], dstPts[Y] };
            break;
        case PathIterator.SEG_QUADTO:
            graphicsObj.addFillet(new int[] { (int) Math.round(dstPts[X1]), (int) Math.round(dstPts[Y1]),
                    (int) Math.round(dstPts[X2]), (int) Math.round(dstPts[Y2]) }, true);
            currentPosition = new double[] { dstPts[X2], dstPts[Y2] };
            break;
        case PathIterator.SEG_CUBICTO:
            double[] cubicCoords = new double[] { currentPosition[0], currentPosition[1], dstPts[X1],
                    dstPts[Y1], dstPts[X2], dstPts[Y2], dstPts[X3], dstPts[Y3] };
            double[][] quadParts = CubicBezierApproximator.fixedMidPointApproximation(cubicCoords);
            if (quadParts.length >= 4) {
                for (int segIndex = 0; segIndex < quadParts.length; segIndex++) {
                    double[] quadPts = quadParts[segIndex];
                    if (quadPts != null && quadPts.length == 4) {
                        graphicsObj.addFillet(
                                new int[] { (int) Math.round(quadPts[X1]), (int) Math.round(quadPts[Y1]),
                                        (int) Math.round(quadPts[X2]), (int) Math.round(quadPts[Y2]) },
                                true);
                        currentPosition = new double[] { quadPts[X2], quadPts[Y2] };
                    }
                }
            }
            break;
        case PathIterator.SEG_MOVETO:
            openingCoords = new int[] { (int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]) };
            currentPosition = new double[] { dstPts[X], dstPts[Y] };
            graphicsObj.setCurrentPosition(openingCoords);
            break;
        case PathIterator.SEG_CLOSE:
            graphicsObj.addLine(openingCoords, true);
            currentPosition = new double[] { openingCoords[0], openingCoords[1] };
            break;
        default:
            LOG.debug("Unrecognised path iterator type");
            break;
        }
    }
}

From source file:org.apache.pdfbox.pdfviewer.font.CFFGlyph2D.java

private GeneralPath transformGlyph(GeneralPath glyph) {
    // we have to invert all y-coordinates due to the moved 0,0-reference
    PathIterator iter = glyph.getPathIterator(null);
    float[] currentSegment = new float[6];
    Path2D.Float path = new Path2D.Float(iter.getWindingRule());
    boolean glyphTransformed = false;
    while (!iter.isDone()) {
        glyphTransformed = true;//w  w w. ja v a2 s  .c o m
        int type = iter.currentSegment(currentSegment);
        switch (type) {
        case PathIterator.SEG_MOVETO:
            path.moveTo(currentSegment[0], -currentSegment[1]);
            break;
        case PathIterator.SEG_LINETO:
            path.lineTo(currentSegment[0], -currentSegment[1]);
            break;
        case PathIterator.SEG_QUADTO:
            path.quadTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            path.curveTo(currentSegment[0], -currentSegment[1], currentSegment[2], -currentSegment[3],
                    currentSegment[4], -currentSegment[5]);
            break;
        case PathIterator.SEG_CLOSE:
            path.closePath();
            break;
        }
        iter.next();
    }
    if (glyphTransformed) {
        return new GeneralPath(path);
    } else {
        return glyph;
    }
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

/**
 * Returns true if the given path is rectangular.
 *//* w w w. ja  v  a 2s . c  om*/
private boolean isRectangular(GeneralPath path) {
    PathIterator iter = path.getPathIterator(null);
    double[] coords = new double[6];
    int count = 0;
    int[] xs = new int[4];
    int[] ys = new int[4];
    while (!iter.isDone()) {
        switch (iter.currentSegment(coords)) {
        case PathIterator.SEG_MOVETO:
            if (count == 0) {
                xs[count] = (int) Math.floor(coords[0]);
                ys[count] = (int) Math.floor(coords[1]);
            } else {
                return false;
            }
            count++;
            break;

        case PathIterator.SEG_LINETO:
            if (count < 4) {
                xs[count] = (int) Math.floor(coords[0]);
                ys[count] = (int) Math.floor(coords[1]);
            } else {
                return false;
            }
            count++;
            break;

        case PathIterator.SEG_CUBICTO:
            return false;

        case PathIterator.SEG_CLOSE:
            break;
        }
        iter.next();
    }

    if (count == 4) {
        return xs[0] == xs[1] || xs[0] == xs[2] || ys[0] == ys[1] || ys[0] == ys[3];
    }
    return false;
}

From source file:org.eclipse.birt.chart.device.svg.SVGGraphics2D.java

protected Element createShape(Shape shape) {
    PathIterator pathIter = shape.getPathIterator(null);
    StringBuffer pathStr = new StringBuffer();
    while (!pathIter.isDone()) {
        float[] points = new float[6];
        int TYPE = pathIter.currentSegment(points);
        switch (TYPE) {
        case PathIterator.SEG_CLOSE:
            pathStr.append(" Z"); //$NON-NLS-1$
            break;
        case PathIterator.SEG_LINETO:
            pathStr.append(" L").append(toString(points, 2, ' ')); //$NON-NLS-1$ 
            break;
        case PathIterator.SEG_QUADTO:
            pathStr.append(" Q").append(toString(points, 4, ' ')); //$NON-NLS-1$ 
            break;
        case PathIterator.SEG_CUBICTO:
            pathStr.append(" C").append(toString(points, 6, ' ')); //$NON-NLS-1$ 
            break;
        case PathIterator.SEG_MOVETO:
            pathStr.append(" M").append(toString(points, 2, ' ')); //$NON-NLS-1$ 
            break;
        }// w ww .  ja  v a2 s.  c om
        pathIter.next();
    }
    Element elem = dom.createElement("path"); //$NON-NLS-1$
    elem.setAttribute("d", pathStr.toString()); //$NON-NLS-1$
    return elem;
}

From source file:org.esa.snap.graphbuilder.gpf.ui.worldmap.NestWorldMapPane.java

public static GeneralPath areaToPath(final Area negativeArea, final double deltaX,
        final GeneralPath pixelPath) {

    final float[] floats = new float[6];
    // move to correct rectangle
    final AffineTransform transform = AffineTransform.getTranslateInstance(deltaX, 0.0);
    final PathIterator iterator = negativeArea.getPathIterator(transform);

    while (!iterator.isDone()) {
        final int segmentType = iterator.currentSegment(floats);
        if (segmentType == PathIterator.SEG_LINETO) {
            pixelPath.lineTo(floats[0], floats[1]);
        } else if (segmentType == PathIterator.SEG_MOVETO) {
            pixelPath.moveTo(floats[0], floats[1]);
        } else if (segmentType == PathIterator.SEG_CLOSE) {
            pixelPath.closePath();//from  w  ww.  j a  v a 2s .c o m
        }
        iterator.next();
    }
    return pixelPath;
}