/** * Retrieves a Batik {@link GraphicsNode} for a given SVG. * * @param svgLocation an URL that specifies the SVG. * @return the corresponding GraphicsNode. * @throws IOException * @throws URISyntaxException */ private GraphicsNode getGraphicNode(Document doc) { // instantiates objects needed for building the node UserAgent userAgent = new UserAgentAdapter(); DocumentLoader loader = new DocumentLoader(userAgent); BridgeContext ctx = new BridgeContext(userAgent, loader); ctx.setDynamic(true); // creates node builder and builds node GVTBuilder builder = new GVTBuilder(); return builder.build(ctx, doc); }
protected GraphicsNode buildGVT(BridgeContext ctx, SVGOMDocument svgDoc) throws TranscoderException { GVTBuilder builder = new GVTBuilder(); GraphicsNode gvtRoot; try { gvtRoot = builder.build(ctx, svgDoc); // dispatch an 'onload' event if needed if (ctx.isDynamic()) { BaseScriptingEnvironment se = new BaseScriptingEnvironment(ctx); se.loadScripts(); se.dispatchSVGLoadEvent(); } } catch (BridgeException ex) { throw new TranscoderException(ex); } return gvtRoot; }
/** * Builds using the specified BridgeContext and element, the specified graphics node. * * @param ctx the bridge context to use * @param e the element that describes the graphics node to build * @param node the graphics node to build */ public void buildGraphicsNode(BridgeContext ctx, Element e, GraphicsNode node) { CompositeGraphicsNode cgn = (CompositeGraphicsNode) node; // build flowRegion shapes boolean isStatic = !ctx.isDynamic(); if (isStatic) { flowRegionNodes = new HashMap(); } else { regionChangeListener = new RegionChangeListener(); } CompositeGraphicsNode cgn2 = (CompositeGraphicsNode) cgn.get(0); GVTBuilder builder = ctx.getGVTBuilder(); for (Node n = getFirstChild(e); n != null; n = getNextSibling(n)) { if (n instanceof SVGOMFlowRegionElement) { for (Node m = getFirstChild(n); m != null; m = getNextSibling(m)) { if (m.getNodeType() != Node.ELEMENT_NODE) { continue; } GraphicsNode gn = builder.build(ctx, (Element) m); if (gn != null) { cgn2.add(gn); if (isStatic) { flowRegionNodes.put(m, gn); } } } if (!isStatic) { AbstractNode an = (AbstractNode) n; XBLEventSupport es = (XBLEventSupport) an.initializeEventSupport(); es.addImplementationEventListenerNS( SVG_NAMESPACE_URI, "shapechange", regionChangeListener, false); } } } // build text node GraphicsNode tn = (GraphicsNode) cgn.get(1); super.buildGraphicsNode(ctx, e, tn); // Drop references once static build is completed. flowRegionNodes = null; }
private static void drawSvgToGraphics2D(Svg svg, Graphics2D g2, Dimension size) throws IOException { // Copied (and modified) from http://stackoverflow.com/a/12502943 String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser); UserAgent userAgent = new UserAgentAdapter(); DocumentLoader loader = new DocumentLoader(userAgent); BridgeContext ctx = new BridgeContext(userAgent, loader); ctx.setDynamicState(BridgeContext.DYNAMIC); GVTBuilder builder = new GVTBuilder(); StringReader svgReader = new StringReader(svg.toString()); SVGDocument parsedSvgDocument = factory.createSVGDocument(null, svgReader); GraphicsNode chartGfx = builder.build(ctx, parsedSvgDocument); Dimension actualSize = svg.getSize(); double scaleWidth = 1.0 * size.width / actualSize.width; double scaleHeight = 1.0 * size.height / actualSize.height; chartGfx.setTransform(AffineTransform.getScaleInstance(scaleWidth, scaleHeight)); chartGfx.paint(g2); }
public static void main(String[] args) { try { FileInputStream f = new FileInputStream(args[0]); SVGDocument doc = (SVGDocument) svgDocFactory.createDocument(null, f); BridgeContext bc = new BridgeContext(new UserAgentAdapter()); GVTBuilder gvtb = new GVTBuilder(); GraphicsNode gn = gvtb.build(bc, doc); GVTTreeWalker tw = new GVTTreeWalker(gn); gn = tw.nextGraphicsNode(); System.out.print("new Color[] { "); Graphics2D g2 = GraphicsEnvironment.getLocalGraphicsEnvironment() .createGraphics(new BufferedImage(32, 32, BufferedImage.TYPE_3BYTE_BGR)); while (true) { // System.out.println("node " + i); gn = tw.nextGraphicsNode(); if (gn == null) { break; } gn.paint(g2); Color c = g2.getColor(); System.out.print( "new Color(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + "), "); } System.out.println("}"); } catch (IOException e) { e.printStackTrace(); } }
public Document(InputStream stream) throws IOException { f = new SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName()); doc = f.createSVGDocument(null, stream); // Boot the CSS engine userAgent = new UserAgentAdapter(); loader = new DocumentLoader(userAgent); ctx = new BridgeContext(userAgent, loader); ctx.setDynamicState(BridgeContext.DYNAMIC); builder = new GVTBuilder(); rootGN = builder.build(ctx, doc); svgRoot = doc.getRootElement(); vcss = (ViewCSS) doc.getDocumentElement(); }