/**
 * 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;
import java.util.zip.*;

/**
 * Export plot image to a SVG compressed stream or file.
 * 
 * @author Sergei Chekanov 
 * 
 */
public class SVGZExporter extends Renderer {

	private final OutputStream os;
        private String fname;

	public SVGZExporter(String pathname) throws FileNotFoundException {
		this(new File(pathname));
                fname=pathname;
	}

	public SVGZExporter(File file) throws FileNotFoundException {
		this(new FileOutputStream(file));
	}

	public SVGZExporter(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 {
                FileOutputStream fos = new FileOutputStream(fname);
                GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
                Writer out=new OutputStreamWriter(gzipOS,"UTF-8");
                g.stream(out,true);
                gzipOS.flush(); 
                gzipOS.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.