Search in sources :

Example 1 with GraphicsNode

use of org.apache.batik.gvt.GraphicsNode in project pdfbox-graphics2d by rototor.

the class RenderSVGsTest method renderSVG.

private void renderSVG(String name, final double scale) throws IOException {
    String uri = RenderSVGsTest.class.getResource(name).toString();
    // create the document
    String parser = XMLResourceDescriptor.getXMLParserClassName();
    SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
    Document document = f.createDocument(uri, RenderSVGsTest.class.getResourceAsStream(name));
    // create the GVT
    UserAgent userAgent = new UserAgentAdapter();
    DocumentLoader loader = new DocumentLoader(userAgent);
    BridgeContext bctx = new BridgeContext(userAgent, loader);
    bctx.setDynamicState(BridgeContext.STATIC);
    GVTBuilder builder = new GVTBuilder();
    final GraphicsNode gvtRoot = builder.build(bctx, document);
    this.exportGraphic("svg", name.replace(".svg", ""), new GraphicsExporter() {

        @Override
        public void draw(Graphics2D gfx) {
            gfx.scale(scale, scale);
            gvtRoot.paint(gfx);
        }
    });
}
Also used : Document(org.w3c.dom.Document) SAXSVGDocumentFactory(org.apache.batik.anim.dom.SAXSVGDocumentFactory) GraphicsNode(org.apache.batik.gvt.GraphicsNode)

Example 2 with GraphicsNode

use of org.apache.batik.gvt.GraphicsNode in project charts by vaadin.

the class PdfExportDemo method buildBatikGraphicsNode.

/**
 * Use Batik SVG Toolkit to create GraphicsNode for the target SVG.
 * <ol>
 * <li>Create SVGDocument</li>
 * <li>Create BridgeContext</li>
 * <li>Build GVT tree. Results to GraphicsNode</li>
 * </ol>
 *
 * @param svg
 *            SVG as a String
 * @return GraphicsNode
 * @throws IOException
 *             Thrown when SVG could not be read properly.
 */
private GraphicsNode buildBatikGraphicsNode(String svg) throws IOException {
    UserAgent agent = new UserAgentAdapter();
    SVGDocument svgdoc = createSVGDocument(svg, agent);
    DocumentLoader loader = new DocumentLoader(agent);
    BridgeContext bridgeContext = new BridgeContext(agent, loader);
    bridgeContext.setDynamicState(BridgeContext.STATIC);
    GVTBuilder builder = new GVTBuilder();
    GraphicsNode imageGraphics = builder.build(bridgeContext, svgdoc);
    return imageGraphics;
}
Also used : UserAgentAdapter(org.apache.batik.bridge.UserAgentAdapter) UserAgent(org.apache.batik.bridge.UserAgent) BridgeContext(org.apache.batik.bridge.BridgeContext) DocumentLoader(org.apache.batik.bridge.DocumentLoader) SVGDocument(org.w3c.dom.svg.SVGDocument) GVTBuilder(org.apache.batik.bridge.GVTBuilder) GraphicsNode(org.apache.batik.gvt.GraphicsNode)

Example 3 with GraphicsNode

use of org.apache.batik.gvt.GraphicsNode in project megameklab by MegaMek.

the class PrintRecordSheet method print.

@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) {
    Graphics2D g2d = (Graphics2D) graphics;
    if (null != g2d) {
        createDocument(pageIndex, pageFormat, true);
        GraphicsNode node = build();
        node.paint(g2d);
    /* Testing code that outputs the generated svg
            try {
                javax.xml.transform.Transformer transformer = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
                javax.xml.transform.Result output = new javax.xml.transform.stream.StreamResult(new File("out.svg"));
                javax.xml.transform.Source input = new javax.xml.transform.dom.DOMSource(svgDocument);
                transformer.transform(input, output);
            } catch (Exception ex) {
                LogManager.getLogger.error(ex);
            }
            */
    }
    if (callback != null) {
        callback.accept(pageIndex);
    }
    return Printable.PAGE_EXISTS;
}
Also used : SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) GraphicsNode(org.apache.batik.gvt.GraphicsNode)

Example 4 with GraphicsNode

use of org.apache.batik.gvt.GraphicsNode in project megameklab by MegaMek.

the class PrintRecordSheet method print.

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
    final String METHOD_NAME = "print(Graphics,PageFormat,int)";
    Graphics2D g2d = (Graphics2D) graphics;
    if (null != g2d) {
        File f = new File("data/images/recordsheets/" + getSVGFileName());
        svgDocument = null;
        try {
            InputStream is = new FileInputStream(f);
            DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
            final String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXDocumentFactory df = new SAXDocumentFactory(impl, parser);
            svgDocument = df.createDocument(f.toURI().toASCIIString(), is);
        } catch (Exception e) {
            MegaMekLab.getLogger().log(PrintRecordSheet.class, METHOD_NAME, e);
        }
        if (null == svgDocument) {
            MegaMekLab.getLogger().log(PrintRecordSheet.class, METHOD_NAME, LogLevel.ERROR, "Failed to open Mech SVG file! Path: data/images/recordsheets/" + getSVGFileName());
        } else {
            svgGenerator = new SVGGraphics2D(svgDocument);
            printImage(g2d, pageFormat, pageIndex - firstPage);
            GraphicsNode node = build();
            node.paint(g2d);
        }
    }
    return Printable.PAGE_EXISTS;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) SAXDocumentFactory(org.apache.batik.dom.util.SAXDocumentFactory) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrinterException(java.awt.print.PrinterException) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) GraphicsNode(org.apache.batik.gvt.GraphicsNode)

Example 5 with GraphicsNode

use of org.apache.batik.gvt.GraphicsNode in project charts by vaadin.

the class PdfExportDemo method drawUnscaledSvg.

private Image drawUnscaledSvg(PdfContentByte contentByte) throws IOException {
    // First, lets create a graphics node for the SVG image.
    GraphicsNode imageGraphics = buildBatikGraphicsNode(svgStr);
    // SVG's width and height
    float width = (float) imageGraphics.getBounds().getWidth();
    float height = (float) imageGraphics.getBounds().getHeight();
    // Create a PDF template for the SVG image
    PdfTemplate template = contentByte.createTemplate(width, height);
    // Create Graphics2D rendered object from the template
    Graphics2D graphics = template.createGraphics(width, height);
    try {
        // SVGs can have their corner at coordinates other than (0,0).
        Rectangle2D bounds = imageGraphics.getBounds();
        graphics.translate(-bounds.getX(), -bounds.getY());
        // Paint SVG GraphicsNode with the 2d-renderer.
        imageGraphics.paint(graphics);
        // image.
        return new ImgTemplate(template);
    } catch (BadElementException e) {
        throw new RuntimeException("Couldn't generate PDF from SVG", e);
    } finally {
        // Manual cleaning (optional)
        graphics.dispose();
    }
}
Also used : ImgTemplate(com.lowagie.text.ImgTemplate) BadElementException(com.lowagie.text.BadElementException) Rectangle2D(java.awt.geom.Rectangle2D) PdfTemplate(com.lowagie.text.pdf.PdfTemplate) GraphicsNode(org.apache.batik.gvt.GraphicsNode) Graphics2D(java.awt.Graphics2D)

Aggregations

GraphicsNode (org.apache.batik.gvt.GraphicsNode)5 Graphics2D (java.awt.Graphics2D)2 SVGGraphics2D (org.apache.batik.svggen.SVGGraphics2D)2 BadElementException (com.lowagie.text.BadElementException)1 ImgTemplate (com.lowagie.text.ImgTemplate)1 PdfTemplate (com.lowagie.text.pdf.PdfTemplate)1 Rectangle2D (java.awt.geom.Rectangle2D)1 PrinterException (java.awt.print.PrinterException)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 SAXSVGDocumentFactory (org.apache.batik.anim.dom.SAXSVGDocumentFactory)1 SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)1 BridgeContext (org.apache.batik.bridge.BridgeContext)1 DocumentLoader (org.apache.batik.bridge.DocumentLoader)1 GVTBuilder (org.apache.batik.bridge.GVTBuilder)1 UserAgent (org.apache.batik.bridge.UserAgent)1