use of org.apache.poi.sl.draw.geom.CustomGeometry in project poi by apache.
the class HSLFSimpleShape method getGeometry.
@Override
public CustomGeometry getGeometry() {
PresetGeometries dict = PresetGeometries.getInstance();
ShapeType st = getShapeType();
String name = (st != null) ? st.getOoxmlName() : null;
CustomGeometry geom = dict.get(name);
if (geom == null) {
if (name == null) {
name = (st != null) ? st.toString() : "<unknown>";
}
LOG.log(POILogger.WARN, "No preset shape definition for shapeType: " + name);
}
return geom;
}
use of org.apache.poi.sl.draw.geom.CustomGeometry in project poi by apache.
the class XSLFSimpleShape method getGeometry.
/**
*
* @return definition of the shape geometry
*/
@Override
public CustomGeometry getGeometry() {
XSLFGeometryProperties gp = XSLFPropertiesDelegate.getGeometryDelegate(getShapeProperties());
if (gp == null) {
return null;
}
CustomGeometry geom;
PresetGeometries dict = PresetGeometries.getInstance();
if (gp.isSetPrstGeom()) {
String name = gp.getPrstGeom().getPrst().toString();
geom = dict.get(name);
if (geom == null) {
throw new IllegalStateException("Unknown shape geometry: " + name + ", available geometries are: " + dict.keySet());
}
} else if (gp.isSetCustGeom()) {
XMLStreamReader staxReader = gp.getCustGeom().newXMLStreamReader();
geom = PresetGeometries.convertCustomGeometry(staxReader);
try {
staxReader.close();
} catch (XMLStreamException e) {
LOG.log(POILogger.WARN, "An error occurred while closing a Custom Geometry XML Stream Reader: " + e.getMessage());
}
} else {
geom = dict.get("rect");
}
return geom;
}
use of org.apache.poi.sl.draw.geom.CustomGeometry in project poi by apache.
the class DrawSimpleShape method computeOutlines.
protected Collection<Outline> computeOutlines(Graphics2D graphics) {
final SimpleShape<?, ?> sh = getShape();
List<Outline> lst = new ArrayList<Outline>();
CustomGeometry geom = sh.getGeometry();
if (geom == null) {
return lst;
}
Rectangle2D anchor = getAnchor(graphics, sh);
for (Path p : geom) {
double w = p.getW(), h = p.getH(), scaleX = Units.toPoints(1), scaleY = scaleX;
if (w == -1) {
w = Units.toEMU(anchor.getWidth());
} else {
scaleX = anchor.getWidth() / w;
}
if (h == -1) {
h = Units.toEMU(anchor.getHeight());
} else {
scaleY = anchor.getHeight() / h;
}
// the guides in the shape definitions are all defined relative to each other,
// so we build the path starting from (0,0).
final Rectangle2D pathAnchor = new Rectangle2D.Double(0, 0, w, h);
Context ctx = new Context(geom, pathAnchor, sh);
java.awt.Shape gp = p.getPath(ctx);
// translate the result to the canvas coordinates in points
AffineTransform at = new AffineTransform();
at.translate(anchor.getX(), anchor.getY());
at.scale(scaleX, scaleY);
java.awt.Shape canvasShape = at.createTransformedShape(gp);
lst.add(new Outline(canvasShape, p));
}
return lst;
}
use of org.apache.poi.sl.draw.geom.CustomGeometry in project poi by apache.
the class DrawSimpleShape method getCustomGeometry.
protected static CustomGeometry getCustomGeometry(String name, Graphics2D graphics) {
@SuppressWarnings("unchecked") Map<String, CustomGeometry> presets = (graphics == null) ? null : (Map<String, CustomGeometry>) graphics.getRenderingHint(Drawable.PRESET_GEOMETRY_CACHE);
if (presets == null) {
presets = new HashMap<String, CustomGeometry>();
if (graphics != null) {
graphics.setRenderingHint(Drawable.PRESET_GEOMETRY_CACHE, presets);
}
String packageName = "org.apache.poi.sl.draw.binding";
InputStream presetIS = Drawable.class.getResourceAsStream("presetShapeDefinitions.xml");
// StAX:
EventFilter startElementFilter = new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
try {
XMLInputFactory staxFactory = XMLInputFactory.newInstance();
XMLEventReader staxReader = staxFactory.createXMLEventReader(presetIS);
XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter);
// Ignore StartElement:
staxFiltRd.nextEvent();
// JAXB:
JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
while (staxFiltRd.peek() != null) {
StartElement evRoot = (StartElement) staxFiltRd.peek();
String cusName = evRoot.getName().getLocalPart();
// XMLEvent ev = staxReader.nextEvent();
JAXBElement<org.apache.poi.sl.draw.binding.CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class);
CTCustomGeometry2D cusGeom = el.getValue();
presets.put(cusName, new CustomGeometry(cusGeom));
}
staxFiltRd.close();
staxReader.close();
} catch (Exception e) {
throw new RuntimeException("Unable to load preset geometries.", e);
} finally {
IOUtils.closeQuietly(presetIS);
}
}
return presets.get(name);
}
Aggregations