Search in sources :

Example 16 with SVGGraphics2D

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

the class PGraphicsSVG method endDraw.

@Override
public void endDraw() {
    Writer writer;
    // Also need to pop the matrix since the matrix doesn't reset on each run
    // http://dev.processing.org/bugs/show_bug.cgi?id=1227
    popMatrix();
    // can be inserted, because SVG doesn't support multiple pages.
    if (output == null) {
        if (path == null) {
            throw new RuntimeException("setOutput() or setPath() must be " + "used with the SVG renderer");
        } else {
            // insert the frame number and create intermediate directories
            File save = parent.saveFile(parent.insertFrame(path));
            try {
                output = new FileOutputStream(save);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
    // This needs to be overridden so that the endDraw() from PGraphicsJava2D
    // is not inherited (it calls loadPixels).
    // http://dev.processing.org/bugs/show_bug.cgi?id=1169
    // Finally, stream out SVG to the standard output using UTF-8 encoding.
    // we want to use CSS style attributes
    boolean useCSS = true;
    writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output, StandardCharsets.UTF_8)));
    try {
        ((SVGGraphics2D) g2).stream(writer, useCSS);
    } catch (SVGGraphics2DIOException e) {
    }
    try {
        writer.flush();
        writer.close();
    } catch (IOException e) {
    } finally {
        output = null;
    }
    try {
        writer.close();
    } catch (IOException ex) {
        Logger.getLogger(PGraphicsSVG.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) SVGGraphics2DIOException(org.apache.batik.svggen.SVGGraphics2DIOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) SVGGraphics2DIOException(org.apache.batik.svggen.SVGGraphics2DIOException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 17 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project pentaho-platform by pentaho.

the class JFreeChartEngine method saveChartAsSVG.

/**
 * Create an SVG image file from a JFreeChart object
 *
 * @param chart
 *          The chart object to create an image from
 * @param path
 *          The path and name of the image file to create
 * @param width
 *          The width of the image in pixels
 * @param height
 *          The height of the image in pixels
 * @throws IOException
 */
private static void saveChartAsSVG(final JFreeChart chart, final String path, final int width, final int height, final ChartRenderingInfo info) throws IOException {
    // THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...
    // Get a DOMImplementation
    org.w3c.dom.DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    // Create an instance of org.w3c.dom.Document
    // $NON-NLS-1$
    Document document = domImpl.createDocument(null, "svg", null);
    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    // set the precision to avoid a null pointer exception in Batik 1.5
    svgGenerator.getGeneratorContext().setPrecision(6);
    // Ask the chart to render into the SVG Graphics2D implementation
    chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), info);
    // Finally, stream out SVG to a file using UTF-8 character to byte
    // encoding
    boolean useCSS = true;
    Writer out = // $NON-NLS-1$
    new OutputStreamWriter(new FileOutputStream(new File(path + ".svg")), LocaleHelper.getSystemEncoding());
    svgGenerator.stream(out, useCSS);
}
Also used : FileOutputStream(java.io.FileOutputStream) Rectangle2D(java.awt.geom.Rectangle2D) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) OutputStreamWriter(java.io.OutputStreamWriter) Document(org.w3c.dom.Document) File(java.io.File) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 18 with SVGGraphics2D

use of org.apache.batik.svggen.SVGGraphics2D in project dkpro-lab by dkpro.

the class ChartUtil method writeChartAsSVG.

/**
 * Exports a JFreeChart to a SVG file.
 *
 * @param chart JFreeChart to export
 * @param aOS stream to write to.
 * @param aWidth width of the chart in pixels
 * @param aHeight height of the chart in pixels
 * @throws IOException if writing the svgFile fails.
 * @see <a href="http://dolf.trieschnigg.nl/jfreechart/">Saving JFreeChart as SVG vector images
 *      using Batik</a>
 */
public static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight) throws IOException {
    // 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
    chart.draw(svgGenerator, new Rectangle(aWidth, aHeight));
    // Write svg file
    Writer out = new OutputStreamWriter(aOS, "UTF-8");
    svgGenerator.stream(out, true);
    out.flush();
    out.close();
}
Also used : Rectangle(java.awt.Rectangle) GenericDOMImplementation(org.apache.batik.dom.GenericDOMImplementation) DOMImplementation(org.w3c.dom.DOMImplementation) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) OutputStreamWriter(java.io.OutputStreamWriter) Document(org.w3c.dom.Document) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 19 with SVGGraphics2D

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

the class ExportPdf method export.

public void export(MapModel map, File chosenFile) {
    if (!ExportController.getContoller().checkCurrentMap(map)) {
        return;
    }
    try {
        final MapView view = (MapView) Controller.getCurrentController().getMapViewManager().getMapViewComponent();
        if (view == null) {
            return;
        }
        Controller.getCurrentController().getViewController().setWaitingCursor(true);
        final SVGGraphics2D g2d = fillSVGGraphics2D(view);
        final PDFTranscoder pdfTranscoder = new PDFTranscoder();
        /*
			 * according to https: &aid=1921334&group_id=7118 Submitted By:
			 * Frank Spangenberg (f_spangenberg) Summary: Large mind maps
			 * produce invalid PDF
			 */
        pdfTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_MAX_HEIGHT, new Float(19200));
        pdfTranscoder.addTranscodingHint(SVGAbstractTranscoder.KEY_MAX_WIDTH, new Float(19200));
        pdfTranscoder.addTranscodingHint(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, 25.4f / 72f / UITools.FONT_SCALE_FACTOR);
        /* end patch */
        final Document doc = g2d.getDOMFactory();
        final Element rootE = doc.getDocumentElement();
        g2d.getRoot(rootE);
        final TranscoderInput input = new TranscoderInput(doc);
        final FileOutputStream ostream = new FileOutputStream(chosenFile);
        final BufferedOutputStream bufStream = new BufferedOutputStream(ostream);
        final TranscoderOutput output = new TranscoderOutput(bufStream);
        pdfTranscoder.transcode(input, output);
        ostream.flush();
        ostream.close();
    } catch (final Exception ex) {
        org.freeplane.core.util.LogUtils.warn(ex);
        UITools.errorMessage(ex.getLocalizedMessage());
    } finally {
        Controller.getCurrentController().getViewController().setWaitingCursor(false);
    }
}
Also used : TranscoderOutput(org.apache.batik.transcoder.TranscoderOutput) TranscoderInput(org.apache.batik.transcoder.TranscoderInput) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) MapView(org.freeplane.view.swing.map.MapView) SVGGraphics2D(org.apache.batik.svggen.SVGGraphics2D) Document(org.w3c.dom.Document) BufferedOutputStream(java.io.BufferedOutputStream) PDFTranscoder(org.apache.fop.svg.PDFTranscoder)

Example 20 with SVGGraphics2D

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

the class ExportVectorGraphic method fillSVGGraphics2D.

/**
 */
protected SVGGraphics2D fillSVGGraphics2D(final MapView view) {
    // work around svg/pdf-Export problems when exporting with Gtk or Nimbus L&Fs
    final String previousLnF = UIManager.getLookAndFeel().getClass().getName();
    setLnF(view, UIManager.getCrossPlatformLookAndFeelClassName());
    try {
        final DOMImplementation impl = GenericDOMImplementation.getDOMImplementation();
        final String namespaceURI = SVGConstants.SVG_NAMESPACE_URI;
        final Document domFactory = impl.createDocument(namespaceURI, "svg", null);
        final SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(domFactory);
        ctx.setEmbeddedFontsOn(true);
        final GraphicContextDefaults defaults = new GraphicContextDefaults();
        defaults.setFont(new Font("Arial", Font.PLAIN, 12));
        ctx.setGraphicContextDefaults(defaults);
        ctx.setPrecision(12);
        final SVGGraphics2D g2d = new SVGGraphics2D(ctx, false);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT);
        view.preparePrinting();
        final Rectangle innerBounds = view.getInnerBounds();
        g2d.setSVGCanvasSize(new Dimension(innerBounds.width, innerBounds.height));
        g2d.translate(-innerBounds.x, -innerBounds.y);
        view.print(g2d);
        view.endPrinting();
        return g2d;
    } finally {
        setLnF(view, previousLnF);
    }
}
Also used : Rectangle(java.awt.Rectangle) 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) SVGGeneratorContext(org.apache.batik.svggen.SVGGeneratorContext) GraphicContextDefaults(org.apache.batik.svggen.SVGGeneratorContext.GraphicContextDefaults) Font(java.awt.Font)

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