Java Code Examples for org.apache.batik.bridge.BridgeContext#dispose()

The following examples show how to use org.apache.batik.bridge.BridgeContext#dispose() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SVGFigure.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Reads color value from the document.
 */
protected Color getColor(final Element element, final String attributeName) {
    if (getDocument() == null || getDocument() != element.getOwnerDocument()) {
        return null;
    }
    Color color = null;
    // Make sure that CSSEngine is available.
    final BridgeContext ctx = transcoder.initCSSEngine();
    try {
        color = SVGUtils.toSWTColor(element, attributeName);
    } finally {
        if (ctx != null) {
            ctx.dispose();
        }
    }
    return color;
}
 
Example 2
Source File: SvgTranscoder.java    From radiance with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Transcodes the SVG image into Java2D code. Does nothing if the
 * {@link #listener} is <code>null</code>.
 *
 * @param templateStream Stream with the template content
 */
public Document transcode(InputStream templateStream) {
    if (this.listener == null)
        return null;

    UserAgentAdapter ua = new UserAgentAdapter();
    DocumentLoader loader = new DocumentLoader(ua);

    BridgeContext batikBridgeContext = new BridgeContext(ua, loader);
    batikBridgeContext.setDynamicState(BridgeContext.DYNAMIC);
    ua.setBridgeContext(batikBridgeContext);

    GVTBuilder builder = new GVTBuilder();
    Document svgDoc;
    try {
        svgDoc = loader.loadDocument(this.uri);
        GraphicsNode gvtRoot = builder.build(batikBridgeContext, svgDoc);

        this.transcode(gvtRoot, templateStream);
        return svgDoc;
    } catch (IOException ex) {
        Logger.getLogger(SvgTranscoder.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } finally {
        try {
            loader.dispose();
            batikBridgeContext.dispose();
        } catch (Throwable t) {
            Logger.getLogger(SvgTranscoder.class.getName()).log(Level.SEVERE, null, t);
        }
    }
}
 
Example 3
Source File: SVGIcon.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Load the original SVG resource.
 *
 * @param toSize if not null, will be set to the image's size
 */
private static GraphicsNode loadGraphicsNode(URL url, Dimension toSize)
        throws IOException
{
    Parameters.notNull("url", url);
    final GraphicsNode graphicsNode;
    final Dimension2D documentSize;
    final Document doc;
    InputStream is = url.openStream();
    try {
        // See http://batik.2283329.n4.nabble.com/rendering-directly-to-java-awt-Graphics2D-td3716202.html
        SAXSVGDocumentFactory factory = DOCUMENT_FACTORY.get();
        /* Don't provide an URI here; we shouldn't commit to supporting relative links from
        loaded SVG documents. */
        doc = factory.createDocument(null, is);
        UserAgent userAgent = new UserAgentAdapter();
        DocumentLoader loader = new DocumentLoader(userAgent);
        BridgeContext bctx = new BridgeContext(userAgent, loader);
        try {
            bctx.setDynamicState(BridgeContext.STATIC);
            graphicsNode = new GVTBuilder().build(bctx, doc);
            documentSize = bctx.getDocumentSize();
        } finally {
            bctx.dispose();
        }
    } catch (RuntimeException e) {
        /* Rethrow the many different exceptions that can occur when parsing invalid SVG files;
        DOMException, BridgeException etc. */
        throw new IOException("Error parsing SVG file", e);
    } finally {
        is.close();
    }
    if (toSize != null) {
        int width = (int) Math.ceil(documentSize.getWidth());
        int height = (int) Math.ceil(documentSize.getHeight());
        final int widthLimited = Math.min(MAX_DIMENSION_PIXELS, width);
        final int heightLimited = Math.min(MAX_DIMENSION_PIXELS, height);
        if (width != widthLimited || height != heightLimited) {
            LOG.log(Level.WARNING,
                    "SVG image {0} too large (dimensions were {1}x{2}, each side can be at most {3}px)",
                    new Object[]{url, width, height, MAX_DIMENSION_PIXELS});
        } else if (width <= 1 && height <= 1) {
            LOG.log(Level.WARNING,
                    "SVG image {0} did not specify a width/height, or is incorrectly sized", url);
        }
        toSize.width = widthLimited;
        toSize.height = heightLimited;
    }
    return graphicsNode;
}