Search in sources :

Example 1 with CTPath2D

use of org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D in project poi by apache.

the class XSLFFreeformShape method setPath.

@Override
public int setPath(Path2D.Double path) {
    CTPath2D ctPath = CTPath2D.Factory.newInstance();
    Rectangle2D bounds = path.getBounds2D();
    int x0 = Units.toEMU(bounds.getX());
    int y0 = Units.toEMU(bounds.getY());
    PathIterator it = path.getPathIterator(new AffineTransform());
    int numPoints = 0;
    ctPath.setH(Units.toEMU(bounds.getHeight()));
    ctPath.setW(Units.toEMU(bounds.getWidth()));
    while (!it.isDone()) {
        double[] vals = new double[6];
        int type = it.currentSegment(vals);
        switch(type) {
            case PathIterator.SEG_MOVETO:
                CTAdjPoint2D mv = ctPath.addNewMoveTo().addNewPt();
                mv.setX(Units.toEMU(vals[0]) - x0);
                mv.setY(Units.toEMU(vals[1]) - y0);
                numPoints++;
                break;
            case PathIterator.SEG_LINETO:
                CTAdjPoint2D ln = ctPath.addNewLnTo().addNewPt();
                ln.setX(Units.toEMU(vals[0]) - x0);
                ln.setY(Units.toEMU(vals[1]) - y0);
                numPoints++;
                break;
            case PathIterator.SEG_QUADTO:
                CTPath2DQuadBezierTo qbez = ctPath.addNewQuadBezTo();
                CTAdjPoint2D qp1 = qbez.addNewPt();
                qp1.setX(Units.toEMU(vals[0]) - x0);
                qp1.setY(Units.toEMU(vals[1]) - y0);
                CTAdjPoint2D qp2 = qbez.addNewPt();
                qp2.setX(Units.toEMU(vals[2]) - x0);
                qp2.setY(Units.toEMU(vals[3]) - y0);
                numPoints += 2;
                break;
            case PathIterator.SEG_CUBICTO:
                CTPath2DCubicBezierTo bez = ctPath.addNewCubicBezTo();
                CTAdjPoint2D p1 = bez.addNewPt();
                p1.setX(Units.toEMU(vals[0]) - x0);
                p1.setY(Units.toEMU(vals[1]) - y0);
                CTAdjPoint2D p2 = bez.addNewPt();
                p2.setX(Units.toEMU(vals[2]) - x0);
                p2.setY(Units.toEMU(vals[3]) - y0);
                CTAdjPoint2D p3 = bez.addNewPt();
                p3.setX(Units.toEMU(vals[4]) - x0);
                p3.setY(Units.toEMU(vals[5]) - y0);
                numPoints += 3;
                break;
            case PathIterator.SEG_CLOSE:
                numPoints++;
                ctPath.addNewClose();
                break;
            default:
                throw new IllegalStateException("Unrecognized path segment type: " + type);
        }
        it.next();
    }
    XmlObject xo = getShapeProperties();
    if (!(xo instanceof CTShapeProperties)) {
        return -1;
    }
    ((CTShapeProperties) xo).getCustGeom().getPathLst().setPathArray(new CTPath2D[] { ctPath });
    setAnchor(bounds);
    return numPoints;
}
Also used : CTAdjPoint2D(org.openxmlformats.schemas.drawingml.x2006.main.CTAdjPoint2D) CTPath2DCubicBezierTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DCubicBezierTo) CTPath2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D) CTShapeProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties) PathIterator(java.awt.geom.PathIterator) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) XmlObject(org.apache.xmlbeans.XmlObject) CTPath2DQuadBezierTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DQuadBezierTo)

Example 2 with CTPath2D

use of org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D in project poi by apache.

the class XSLFFreeformShape method getPath.

@Override
public Path2D.Double getPath() {
    Path2D.Double path = new Path2D.Double();
    Rectangle2D bounds = getAnchor();
    XmlObject xo = getShapeProperties();
    if (!(xo instanceof CTShapeProperties)) {
        return null;
    }
    CTCustomGeometry2D geom = ((CTShapeProperties) xo).getCustGeom();
    for (CTPath2D spPath : geom.getPathLst().getPathArray()) {
        double scaleW = bounds.getWidth() / Units.toPoints(spPath.getW());
        double scaleH = bounds.getHeight() / Units.toPoints(spPath.getH());
        for (XmlObject ch : spPath.selectPath("*")) {
            if (ch instanceof CTPath2DMoveTo) {
                CTAdjPoint2D pt = ((CTPath2DMoveTo) ch).getPt();
                path.moveTo((float) (Units.toPoints((Long) pt.getX()) * scaleW), (float) (Units.toPoints((Long) pt.getY()) * scaleH));
            } else if (ch instanceof CTPath2DLineTo) {
                CTAdjPoint2D pt = ((CTPath2DLineTo) ch).getPt();
                path.lineTo((float) Units.toPoints((Long) pt.getX()), (float) Units.toPoints((Long) pt.getY()));
            } else if (ch instanceof CTPath2DQuadBezierTo) {
                CTPath2DQuadBezierTo bez = ((CTPath2DQuadBezierTo) ch);
                CTAdjPoint2D pt1 = bez.getPtArray(0);
                CTAdjPoint2D pt2 = bez.getPtArray(1);
                path.quadTo((float) (Units.toPoints((Long) pt1.getX()) * scaleW), (float) (Units.toPoints((Long) pt1.getY()) * scaleH), (float) (Units.toPoints((Long) pt2.getX()) * scaleW), (float) (Units.toPoints((Long) pt2.getY()) * scaleH));
            } else if (ch instanceof CTPath2DCubicBezierTo) {
                CTPath2DCubicBezierTo bez = ((CTPath2DCubicBezierTo) ch);
                CTAdjPoint2D pt1 = bez.getPtArray(0);
                CTAdjPoint2D pt2 = bez.getPtArray(1);
                CTAdjPoint2D pt3 = bez.getPtArray(2);
                path.curveTo((float) (Units.toPoints((Long) pt1.getX()) * scaleW), (float) (Units.toPoints((Long) pt1.getY()) * scaleH), (float) (Units.toPoints((Long) pt2.getX()) * scaleW), (float) (Units.toPoints((Long) pt2.getY()) * scaleH), (float) (Units.toPoints((Long) pt3.getX()) * scaleW), (float) (Units.toPoints((Long) pt3.getY()) * scaleH));
            } else if (ch instanceof CTPath2DClose) {
                path.closePath();
            }
        }
    }
    // the created path starts at (x=0, y=0).
    // The returned path should fit in the bounding rectangle
    AffineTransform at = new AffineTransform();
    at.translate(bounds.getX(), bounds.getY());
    return new Path2D.Double(at.createTransformedShape(path));
}
Also used : CTPath2DLineTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DLineTo) CTPath2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D) CTShapeProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties) CTCustomGeometry2D(org.openxmlformats.schemas.drawingml.x2006.main.CTCustomGeometry2D) Path2D(java.awt.geom.Path2D) CTPath2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D) Rectangle2D(java.awt.geom.Rectangle2D) CTPath2DQuadBezierTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DQuadBezierTo) CTPath2DMoveTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DMoveTo) CTPath2DClose(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DClose) CTAdjPoint2D(org.openxmlformats.schemas.drawingml.x2006.main.CTAdjPoint2D) CTPath2DCubicBezierTo(org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DCubicBezierTo) AffineTransform(java.awt.geom.AffineTransform) XmlObject(org.apache.xmlbeans.XmlObject)

Aggregations

AffineTransform (java.awt.geom.AffineTransform)2 Rectangle2D (java.awt.geom.Rectangle2D)2 XmlObject (org.apache.xmlbeans.XmlObject)2 CTAdjPoint2D (org.openxmlformats.schemas.drawingml.x2006.main.CTAdjPoint2D)2 CTPath2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPath2D)2 CTPath2DCubicBezierTo (org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DCubicBezierTo)2 CTPath2DQuadBezierTo (org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DQuadBezierTo)2 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)2 Path2D (java.awt.geom.Path2D)1 PathIterator (java.awt.geom.PathIterator)1 CTCustomGeometry2D (org.openxmlformats.schemas.drawingml.x2006.main.CTCustomGeometry2D)1 CTPath2DClose (org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DClose)1 CTPath2DLineTo (org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DLineTo)1 CTPath2DMoveTo (org.openxmlformats.schemas.drawingml.x2006.main.CTPath2DMoveTo)1