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);
}
});
}
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;
}
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;
}
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;
}
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();
}
}
Aggregations