Example #1
0
  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();
  }
  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);
  }