Search in sources :

Example 1 with CustomGeometry

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;
}
Also used : ShapeType(org.apache.poi.sl.usermodel.ShapeType) PresetGeometries(org.apache.poi.sl.draw.geom.PresetGeometries) CustomGeometry(org.apache.poi.sl.draw.geom.CustomGeometry)

Example 2 with CustomGeometry

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;
}
Also used : XSLFGeometryProperties(org.apache.poi.xslf.usermodel.XSLFPropertiesDelegate.XSLFGeometryProperties) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) PresetGeometries(org.apache.poi.sl.draw.geom.PresetGeometries) CustomGeometry(org.apache.poi.sl.draw.geom.CustomGeometry)

Example 3 with CustomGeometry

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;
}
Also used : Path(org.apache.poi.sl.draw.geom.Path) JAXBContext(javax.xml.bind.JAXBContext) Context(org.apache.poi.sl.draw.geom.Context) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) Outline(org.apache.poi.sl.draw.geom.Outline) AffineTransform(java.awt.geom.AffineTransform) CustomGeometry(org.apache.poi.sl.draw.geom.CustomGeometry)

Example 4 with CustomGeometry

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);
}
Also used : InputStream(java.io.InputStream) CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) JAXBContext(javax.xml.bind.JAXBContext) EventFilter(javax.xml.stream.EventFilter) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) CustomGeometry(org.apache.poi.sl.draw.geom.CustomGeometry) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

CustomGeometry (org.apache.poi.sl.draw.geom.CustomGeometry)4 JAXBContext (javax.xml.bind.JAXBContext)2 PresetGeometries (org.apache.poi.sl.draw.geom.PresetGeometries)2 AffineTransform (java.awt.geom.AffineTransform)1 Rectangle2D (java.awt.geom.Rectangle2D)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 EventFilter (javax.xml.stream.EventFilter)1 XMLEventReader (javax.xml.stream.XMLEventReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 StartElement (javax.xml.stream.events.StartElement)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 CTCustomGeometry2D (org.apache.poi.sl.draw.binding.CTCustomGeometry2D)1 Context (org.apache.poi.sl.draw.geom.Context)1 Outline (org.apache.poi.sl.draw.geom.Outline)1 Path (org.apache.poi.sl.draw.geom.Path)1 ShapeType (org.apache.poi.sl.usermodel.ShapeType)1