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;
}
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();
}
Aggregations