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

The following examples show how to use org.apache.batik.bridge.BridgeContext#setDynamicState() . 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: 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 2
Source File: SVGLoader.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Load the SVG image with the specified name. This operation may either
 * create a new graphics node of returned a previously created one.
 * @param name Name of the SVG file to load.
 * @return GraphicsNode containing SVG image or null if none found.
 */
public static GraphicsNode getSVGImage(String name) {
	if (svgCache == null) svgCache = new HashMap<String, GraphicsNode>();

	GraphicsNode found = svgCache.get(name);
	if (found == null) {
		String fileName = SVG_DIR + name;
		URL resource = SVGLoader.class.getResource(fileName);

		try {
			String xmlParser = XMLResourceDescriptor.getXMLParserClassName();
			SAXSVGDocumentFactory df = new SAXSVGDocumentFactory(xmlParser);
			SVGDocument doc = df.createSVGDocument(resource.toString());
			UserAgent userAgent = new UserAgentAdapter();
			DocumentLoader loader = new DocumentLoader(userAgent);
			BridgeContext ctx = new BridgeContext(userAgent, loader);
			ctx.setDynamicState(BridgeContext.DYNAMIC);
			GVTBuilder builder = new GVTBuilder();
			found = builder.build(ctx, doc);

			svgCache.put(name, found);
		}
		catch (Exception e) {
			System.err.println("getSVGImage error: " + fileName);
			e.printStackTrace(System.err);
		}
	}

	return found;
}
 
Example 3
Source File: QualifiedSVGResourceFactory.java    From androidsvgdrawable-plugin with Apache License 2.0 5 votes vote down vote up
private GraphicsNode getGraphicsNode(SVGDocument svgDocument, int dpi) throws IOException {
    UserAgent userAgent = new DensityAwareUserAgent(dpi);
    DocumentLoader loader = new DocumentLoader(userAgent);
    BridgeContext ctx = new BridgeContext(userAgent, loader);
    ctx.setDynamicState(BridgeContext.DYNAMIC);
    GVTBuilder builder = new GVTBuilder();
    GraphicsNode rootGN = builder.build(ctx, svgDocument);
    return rootGN;
}
 
Example 4
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;
}