use of org.apache.poi.ddf.AbstractEscherOptRecord in project poi by apache.
the class HSLFFreeformShape method getPath.
@Override
public Path2D.Double getPath() {
AbstractEscherOptRecord opt = getEscherOptRecord();
EscherArrayProperty verticesProp = getShapeProp(opt, EscherProperties.GEOMETRY__VERTICES);
EscherArrayProperty segmentsProp = getShapeProp(opt, EscherProperties.GEOMETRY__SEGMENTINFO);
// return empty path if either GEOMETRY__VERTICES or GEOMETRY__SEGMENTINFO is missing, see Bugzilla 54188
Path2D.Double path = new Path2D.Double();
//sanity check
if (verticesProp == null) {
LOG.log(POILogger.WARN, "Freeform is missing GEOMETRY__VERTICES ");
return path;
}
if (segmentsProp == null) {
LOG.log(POILogger.WARN, "Freeform is missing GEOMETRY__SEGMENTINFO ");
return path;
}
Iterator<byte[]> vertIter = verticesProp.iterator();
Iterator<byte[]> segIter = segmentsProp.iterator();
double[] xyPoints = new double[2];
while (vertIter.hasNext() && segIter.hasNext()) {
byte[] segElem = segIter.next();
PathInfo pi = getPathInfo(segElem);
switch(pi) {
case escape:
{
// handleEscapeInfo(path, segElem, vertIter);
break;
}
case moveTo:
{
fillPoint(vertIter.next(), xyPoints);
double x = xyPoints[0];
double y = xyPoints[1];
path.moveTo(x, y);
break;
}
case curveTo:
{
fillPoint(vertIter.next(), xyPoints);
double x1 = xyPoints[0];
double y1 = xyPoints[1];
fillPoint(vertIter.next(), xyPoints);
double x2 = xyPoints[0];
double y2 = xyPoints[1];
fillPoint(vertIter.next(), xyPoints);
double x3 = xyPoints[0];
double y3 = xyPoints[1];
path.curveTo(x1, y1, x2, y2, x3, y3);
break;
}
case lineTo:
if (vertIter.hasNext()) {
fillPoint(vertIter.next(), xyPoints);
double x = xyPoints[0];
double y = xyPoints[1];
path.lineTo(x, y);
}
break;
case close:
path.closePath();
break;
default:
break;
}
}
EscherSimpleProperty shapePath = getShapeProp(opt, EscherProperties.GEOMETRY__SHAPEPATH);
ShapePath sp = ShapePath.valueOf(shapePath == null ? 1 : shapePath.getPropertyValue());
if (sp == ShapePath.LINES_CLOSED || sp == ShapePath.CURVES_CLOSED) {
path.closePath();
}
Rectangle2D anchor = getAnchor();
Rectangle2D bounds = path.getBounds2D();
AffineTransform at = new AffineTransform();
at.translate(anchor.getX(), anchor.getY());
at.scale(anchor.getWidth() / bounds.getWidth(), anchor.getHeight() / bounds.getHeight());
return new Path2D.Double(at.createTransformedShape(path));
}
use of org.apache.poi.ddf.AbstractEscherOptRecord 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.AbstractEscherOptRecord in project poi by apache.
the class HSLFShape method getEscherProperty.
/**
* Get the value of a simple escher property for this shape.
*
* @param propId The id of the property. One of the constants defined in EscherOptRecord.
*/
public int getEscherProperty(short propId) {
AbstractEscherOptRecord opt = getEscherOptRecord();
EscherSimpleProperty prop = getEscherProperty(opt, propId);
return prop == null ? 0 : prop.getPropertyValue();
}
use of org.apache.poi.ddf.AbstractEscherOptRecord in project poi by apache.
the class HSLFShape method getColor.
Color getColor(short colorProperty, short opacityProperty, int defaultColor) {
AbstractEscherOptRecord opt = getEscherOptRecord();
EscherSimpleProperty p = getEscherProperty(opt, colorProperty);
if (p == null && defaultColor == -1)
return null;
int val = (p == null) ? defaultColor : p.getPropertyValue();
EscherColorRef ecr = new EscherColorRef(val);
Color col = getColor(ecr);
if (col == null) {
return null;
}
double alpha = getAlpha(opacityProperty);
return new Color(col.getRed(), col.getGreen(), col.getBlue(), (int) (alpha * 255.0));
}
use of org.apache.poi.ddf.AbstractEscherOptRecord in project poi by apache.
the class HSLFShape method getAlpha.
double getAlpha(short opacityProperty) {
AbstractEscherOptRecord opt = getEscherOptRecord();
EscherSimpleProperty op = getEscherProperty(opt, opacityProperty);
int defaultOpacity = 0x00010000;
int opacity = (op == null) ? defaultOpacity : op.getPropertyValue();
return Units.fixedPointToDouble(opacity);
}
Aggregations