/**
* Copyright 2010-2013 Jingjing Li.
*
* This file is part of jplot2d.
*
* jplot2d is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or any later version.
*
* jplot2d is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with jplot2d. If not, see .
*/
package org.jplot2d.renderer;
import java.awt.*;
import java.io.*;
import java.util.List;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.dom.GenericDOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.DOMImplementation;
import org.jplot2d.element.impl.ComponentEx;
/**
* Export plot image to a EPS stream or file.
*
* @author Jingjing Li
*
*/
public class SVGExporter extends Renderer {
private final OutputStream os;
public SVGExporter(String pathname) throws FileNotFoundException {
this(new File(pathname));
}
public SVGExporter(File file) throws FileNotFoundException {
this(new FileOutputStream(file));
}
public SVGExporter(OutputStream out) {
super();
os = new BufferedOutputStream(out);
}
@Override
public void render(ComponentEx comp, List cacheBlockList) {
Dimension size = getDeviceBounds(comp).getSize();
// Get a DOMImplementation.
DOMImplementation domImpl =
GenericDOMImplementation.getDOMImplementation();
String svgNS = "http://www.w3.org/2000/svg";
Document document = domImpl.createDocument(svgNS, "svg", null);
// Create an instance of the SVG Generator.
SVGGraphics2D g = new SVGGraphics2D(document);
g.setSVGCanvasSize(size);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//int n=0;
for (CacheableBlock cb : cacheBlockList) {
List sublist = cb.getSubcomps();
for (ComponentEx subcomp : sublist) {
subcomp.draw(g);
}
}
g.dispose();
try {
Writer out=new OutputStreamWriter(os,"UTF-8");
g.stream(out,true);
os.flush();
os.close();
} catch (IOException a) {System.err.println("Error while exporting to SVGZ");};
/*
OutputStream outputStream=new FileOutputStream(svgFile);
Writer out=new OutputStreamWriter(outputStream,"UTF-8");
svgGenerator.stream(out,true);
outputStream.flush();
outputStream.close();
*/
/*
try {
boolean useCSS = false;
//Writer out = new OutputStreamWriter(System.out, "UTF-8");
PrintWriter out = new PrintWriter("demo.svg", "UTF-8");
g.stream(out, useCSS);
out.close();
} catch (IOException a) {};
*/
}
}
Ads help maintain this website.