Search in sources :

Example 6 with EscherArrayProperty

use of org.apache.poi.ddf.EscherArrayProperty in project poi by apache.

the class HSLFFreeformShape method setPath.

@Override
public int setPath(Path2D.Double path) {
    Rectangle2D bounds = path.getBounds2D();
    PathIterator it = path.getPathIterator(new AffineTransform());
    List<byte[]> segInfo = new ArrayList<byte[]>();
    List<Point2D.Double> pntInfo = new ArrayList<Point2D.Double>();
    boolean isClosed = false;
    int numPoints = 0;
    while (!it.isDone()) {
        double[] vals = new double[6];
        int type = it.currentSegment(vals);
        switch(type) {
            case PathIterator.SEG_MOVETO:
                pntInfo.add(new Point2D.Double(vals[0], vals[1]));
                segInfo.add(SEGMENTINFO_MOVETO);
                numPoints++;
                break;
            case PathIterator.SEG_LINETO:
                pntInfo.add(new Point2D.Double(vals[0], vals[1]));
                segInfo.add(SEGMENTINFO_LINETO);
                segInfo.add(SEGMENTINFO_ESCAPE);
                numPoints++;
                break;
            case PathIterator.SEG_CUBICTO:
                pntInfo.add(new Point2D.Double(vals[0], vals[1]));
                pntInfo.add(new Point2D.Double(vals[2], vals[3]));
                pntInfo.add(new Point2D.Double(vals[4], vals[5]));
                segInfo.add(SEGMENTINFO_CUBICTO);
                segInfo.add(SEGMENTINFO_ESCAPE2);
                numPoints++;
                break;
            case PathIterator.SEG_QUADTO:
                //TODO: figure out how to convert SEG_QUADTO into SEG_CUBICTO
                LOG.log(POILogger.WARN, "SEG_QUADTO is not supported");
                break;
            case PathIterator.SEG_CLOSE:
                pntInfo.add(pntInfo.get(0));
                segInfo.add(SEGMENTINFO_LINETO);
                segInfo.add(SEGMENTINFO_ESCAPE);
                segInfo.add(SEGMENTINFO_LINETO);
                segInfo.add(SEGMENTINFO_CLOSE);
                isClosed = true;
                numPoints++;
                break;
            default:
                LOG.log(POILogger.WARN, "Ignoring invalid segment type " + type);
                break;
        }
        it.next();
    }
    if (!isClosed) {
        segInfo.add(SEGMENTINFO_LINETO);
    }
    segInfo.add(SEGMENTINFO_END);
    AbstractEscherOptRecord opt = getEscherOptRecord();
    opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__SHAPEPATH, 0x4));
    EscherArrayProperty verticesProp = new EscherArrayProperty((short) (EscherProperties.GEOMETRY__VERTICES + 0x4000), false, null);
    verticesProp.setNumberOfElementsInArray(pntInfo.size());
    verticesProp.setNumberOfElementsInMemory(pntInfo.size());
    verticesProp.setSizeOfElements(8);
    for (int i = 0; i < pntInfo.size(); i++) {
        Point2D.Double pnt = pntInfo.get(i);
        byte[] data = new byte[8];
        LittleEndian.putInt(data, 0, Units.pointsToMaster(pnt.getX() - bounds.getX()));
        LittleEndian.putInt(data, 4, Units.pointsToMaster(pnt.getY() - bounds.getY()));
        verticesProp.setElement(i, data);
    }
    opt.addEscherProperty(verticesProp);
    EscherArrayProperty segmentsProp = new EscherArrayProperty((short) (EscherProperties.GEOMETRY__SEGMENTINFO + 0x4000), false, null);
    segmentsProp.setNumberOfElementsInArray(segInfo.size());
    segmentsProp.setNumberOfElementsInMemory(segInfo.size());
    segmentsProp.setSizeOfElements(0x2);
    for (int i = 0; i < segInfo.size(); i++) {
        byte[] seg = segInfo.get(i);
        segmentsProp.setElement(i, seg);
    }
    opt.addEscherProperty(segmentsProp);
    opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__RIGHT, Units.pointsToMaster(bounds.getWidth())));
    opt.addEscherProperty(new EscherSimpleProperty(EscherProperties.GEOMETRY__BOTTOM, Units.pointsToMaster(bounds.getHeight())));
    opt.sortProperties();
    setAnchor(bounds);
    return numPoints;
}
Also used : PathIterator(java.awt.geom.PathIterator) EscherArrayProperty(org.apache.poi.ddf.EscherArrayProperty) Rectangle2D(java.awt.geom.Rectangle2D) ArrayList(java.util.ArrayList) AbstractEscherOptRecord(org.apache.poi.ddf.AbstractEscherOptRecord) Point2D(java.awt.geom.Point2D) EscherSimpleProperty(org.apache.poi.ddf.EscherSimpleProperty) AffineTransform(java.awt.geom.AffineTransform)

Example 7 with EscherArrayProperty

use of org.apache.poi.ddf.EscherArrayProperty in project poi by apache.

the class TestPolygon method testPolygonPoints.

@Test
public void testPolygonPoints() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sh = wb.createSheet();
    HSSFPatriarch patriarch = sh.createDrawingPatriarch();
    HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
    polygon.setPolygonDrawArea(100, 100);
    polygon.setPoints(new int[] { 0, 90, 50, 90 }, new int[] { 5, 5, 44, 88 });
    EscherArrayProperty verticesProp1 = polygon.getOptRecord().lookup(EscherProperties.GEOMETRY__VERTICES);
    String expected = "<EscherArrayProperty id=\"0x8145\" name=\"geometry.vertices\" blipId=\"false\">" + "<Element>[00, 00, 05, 00]</Element>" + "<Element>[5A, 00, 05, 00]</Element>" + "<Element>[32, 00, 2C, 00]</Element>" + "<Element>[5A, 00, 58, 00]</Element>" + "<Element>[00, 00, 05, 00]</Element>" + "</EscherArrayProperty>";
    String actual = verticesProp1.toXml("").replaceAll("[\r\n\t]", "");
    assertEquals(verticesProp1.getNumberOfElementsInArray(), 5);
    assertEquals(expected, actual);
    polygon.setPoints(new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 });
    assertArrayEquals(polygon.getXPoints(), new int[] { 1, 2, 3 });
    assertArrayEquals(polygon.getYPoints(), new int[] { 4, 5, 6 });
    verticesProp1 = polygon.getOptRecord().lookup(EscherProperties.GEOMETRY__VERTICES);
    expected = "<EscherArrayProperty id=\"0x8145\" name=\"geometry.vertices\" blipId=\"false\">" + "<Element>[01, 00, 04, 00]</Element>" + "<Element>[02, 00, 05, 00]</Element>" + "<Element>[03, 00, 06, 00]</Element>" + "<Element>[01, 00, 04, 00]</Element>" + "</EscherArrayProperty>";
    actual = verticesProp1.toXml("").replaceAll("[\r\n\t]", "");
    assertEquals(verticesProp1.getNumberOfElementsInArray(), 4);
    assertEquals(expected, actual);
    wb.close();
}
Also used : EscherArrayProperty(org.apache.poi.ddf.EscherArrayProperty) Test(org.junit.Test)

Aggregations

EscherArrayProperty (org.apache.poi.ddf.EscherArrayProperty)7 AbstractEscherOptRecord (org.apache.poi.ddf.AbstractEscherOptRecord)6 Rectangle2D (java.awt.geom.Rectangle2D)3 EscherSimpleProperty (org.apache.poi.ddf.EscherSimpleProperty)3 AffineTransform (java.awt.geom.AffineTransform)2 EscherColorRef (org.apache.poi.ddf.EscherColorRef)2 DrawPaint (org.apache.poi.sl.draw.DrawPaint)2 Test (org.junit.Test)2 Color (java.awt.Color)1 Path2D (java.awt.geom.Path2D)1 PathIterator (java.awt.geom.PathIterator)1 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1 ColorStyle (org.apache.poi.sl.usermodel.ColorStyle)1 GradientPaint (org.apache.poi.sl.usermodel.PaintStyle.GradientPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)1