Search in sources :

Example 6 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project dbeaver by serge-rider.

the class ERDExportSVG method exportDiagram.

@Override
public void exportDiagram(EntityDiagram diagram, IFigure diagramFigure, DiagramPart diagramPart, File targetFile) throws DBException {
    checkWriterRegister();
    try {
        IFigure figure = diagramPart.getFigure();
        Rectangle contentBounds = figure instanceof FreeformLayeredPane ? ((FreeformLayeredPane) figure).getFreeformExtent() : figure.getBounds();
        String svgNS = "http://www.w3.org/2000/svg";
        // domImpl.createDocument(svgNS, "svg", null);
        Document document = XMLUtils.createDocument();
        document.createAttributeNS(svgNS, "svg");
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        // We need a converter from Draw2D.Graphics (GEF) to awt.Graphics2D (Batik)
        Graphics graphics = new GraphicsToGraphics2DAdaptor(svgGenerator);
        // Reset origin to make it the top/left most part of the diagram
        graphics.translate(contentBounds.x * -1, contentBounds.y * -1);
        paintDiagram(graphics, figure);
        LayerManager layerManager = (LayerManager) diagramPart.getViewer().getEditPartRegistry().get(LayerManager.ID);
        IFigure connectionLayer = layerManager.getLayer("Connection Layer");
        if (connectionLayer != null) {
            paintDiagram(graphics, connectionLayer);
        }
        String filePath = targetFile.getAbsolutePath();
        svgGenerator.stream(filePath);
        UIUtils.launchProgram(filePath);
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Save ERD as SVG", null, e);
    }
}
Also used : Rectangle(org.eclipse.draw2d.geometry.Rectangle) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) Document(org.w3c.dom.Document) LayerManager(org.eclipse.gef.editparts.LayerManager) DBException(org.jkiss.dbeaver.DBException)

Example 7 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project webanno by webanno.

the class SvgChart method renderSvg.

private String renderSvg() {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);
    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    // draw the chart in the SVG generator
    getModelObject().draw(svgGenerator, new Rectangle(options.getViewBoxWidth(), options.getViewBoxHeight()));
    Element svgRoot = svgGenerator.getRoot();
    svgRoot.setAttributeNS(XMLNS_NAMESPACE_URI, XMLNS_PREFIX, SVG_NAMESPACE_URI);
    svgRoot.setAttributeNS(XMLNS_NAMESPACE_URI, XMLNS_PREFIX + ":" + XLINK_PREFIX, XLINK_NAMESPACE_URI);
    SVGCSSStyler.style(svgRoot);
    // String style = svgRoot.getAttributeNS(null, SVG_STYLE_ATTRIBUTE);
    // style = "height: " + SVG_HEIGHT + "px; width: " + SVG_WIDTH + "px; " + style;
    // svgRoot.setAttributeNS(null, SVG_STYLE_ATTRIBUTE, style);
    String style = svgRoot.getAttributeNS(null, SVG_STYLE_ATTRIBUTE);
    style = "height: auto; width: 100%; " + style;
    svgRoot.setAttributeNS(null, SVG_STYLE_ATTRIBUTE, style);
    svgRoot.setAttributeNS(null, SVG_VIEW_BOX_ATTRIBUTE, "0 0 " + options.getViewBoxWidth() + " " + options.getViewBoxHeight());
    try {
        @SuppressWarnings("resource") StringWriter writer = new StringWriter();
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        trans.transform(new DOMSource(svgRoot), new StreamResult(writer));
        writer.close();
        return writer.toString();
    } catch (Exception e) {
        LOG.error("Unable to render SVG", e);
        return "";
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) Rectangle(java.awt.Rectangle) DOMImplementation(org.w3c.dom.DOMImplementation) GenericDOMImplementation(org.apache.batik.dom.GenericDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) Document(org.w3c.dom.Document)

Example 8 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project processing by processing.

the class PGraphicsSVG method beginDraw.

public void beginDraw() {
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    // Create an instance of org.w3c.dom.Document.
    String ns = "http://www.w3.org/2000/svg";
    Document document = domImpl.createDocument(ns, "svg", null);
    // Create an instance of the SVG Generator.
    g2 = new SVGGraphics2D(document);
    ((SVGGraphics2D) g2).setSVGCanvasSize(new Dimension(width, height));
    // Done with our work, let's check on defaults and the rest
    //super.beginDraw();
    // Can't call super.beginDraw() because it'll nuke our g2
    checkSettings();
    // reset model matrix
    resetMatrix();
    vertexCount = 0;
    // Also need to push the matrix since the matrix doesn't reset on each run
    // http://dev.processing.org/bugs/show_bug.cgi?id=1227
    pushMatrix();
}
Also used : GenericDOMImplementation(org.apache.batik.dom.GenericDOMImplementation) DOMImplementation(org.w3c.dom.DOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) Dimension(java.awt.Dimension) Document(org.w3c.dom.Document)

Example 9 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D 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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) SAXDocumentFactory(org.apache.batik.dom.util.SAXDocumentFactory) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrinterException(java.awt.print.PrinterException) Graphics2D(java.awt.Graphics2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) GraphicsNode(org.apache.batik.gvt.GraphicsNode)

Example 10 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project knime-core by knime.

the class HistogramColumn method createSvgImageCell.

/**
 * @param histogramData A {@link HistogramModel}.
 * @return The SVG image cell.
 */
private DataCell createSvgImageCell(final HistogramModel<?> histogramData, final boolean paintLabels) {
    DOMImplementation domImpl = new SVGDOMImplementation();
    String svgNS = "http://www.w3.org/2000/svg";
    Document myFactory = domImpl.createDocument(svgNS, "svg", null);
    SVGGraphics2D g = new SVGGraphics2D(myFactory);
    g.setSVGCanvasSize(new Dimension(m_width, m_height));
    paint(histogramData, paintLabels, g);
    myFactory.replaceChild(g.getRoot(), myFactory.getDocumentElement());
    DataCell dc = new SvgCell((SVGDocument) myFactory);
    return dc;
}
Also used : SVGDOMImplementation(org.apache.batik.dom.svg.SVGDOMImplementation) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.dom.svg.SVGDOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) DataCell(org.knime.core.data.DataCell) SvgCell(org.knime.base.data.xml.SvgCell) Dimension(java.awt.Dimension) Document(org.w3c.dom.Document) SVGDocument(org.w3c.dom.svg.SVGDocument)

Aggregations

SVGGraphics2D (org.apache.batik.svggen.SVGGraphics2D)21 Document (org.w3c.dom.Document)14 DOMImplementation (org.w3c.dom.DOMImplementation)12 OutputStreamWriter (java.io.OutputStreamWriter)8 GenericDOMImplementation (org.apache.batik.dom.GenericDOMImplementation)8 Dimension (java.awt.Dimension)7 FileOutputStream (java.io.FileOutputStream)7 Writer (java.io.Writer)6 IOException (java.io.IOException)5 File (java.io.File)4 SVGGeneratorContext (org.apache.batik.svggen.SVGGeneratorContext)4 Rectangle (java.awt.Rectangle)3 Rectangle2D (java.awt.geom.Rectangle2D)3 FileNotFoundException (java.io.FileNotFoundException)3 SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)3 Element (org.w3c.dom.Element)3 PdfWriter (com.itextpdf.text.pdf.PdfWriter)2 Font (java.awt.Font)2 BufferedOutputStream (java.io.BufferedOutputStream)2 PrintWriter (java.io.PrintWriter)2