2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - Calixte DENIZET
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 package org.scilab.modules.helptools.image;
16 import java.io.FileInputStream;
17 import java.io.FileNotFoundException;
18 import java.io.FileOutputStream;
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.io.StringReader;
24 import org.apache.batik.transcoder.TranscoderException;
25 import org.apache.batik.transcoder.TranscoderInput;
26 import org.apache.batik.transcoder.TranscoderOutput;
27 import org.apache.batik.transcoder.Transcoder;
28 import org.apache.batik.transcoder.image.ImageTranscoder;
29 import org.apache.batik.transcoder.image.PNGTranscoder;
32 * SVG to PNG converter
34 public class SVGImageConverter implements ExternalImageConverter {
36 private static SVGImageConverter instance;
38 private SVGImageConverter() { }
43 public String getMimeType() {
48 * Since this a singleton class...
51 public static ExternalImageConverter getInstance() {
52 if (instance == null) {
53 instance = new SVGImageConverter();
62 public String convertToImage(String currentFile, String svg, Map<String, String> attributes, File imageFile, String imageName) {
63 return convertToPNG(new TranscoderInput(new StringReader(svg)), imageFile, imageName);
69 public String convertToImage(File svg, Map<String, String> attributes, File imageFile, String imageName) {
71 return convertToPNG(new TranscoderInput(new FileInputStream(svg)), imageFile, imageName);
72 } catch (FileNotFoundException e) {
73 System.err.println("Problem when exporting SVG to " + imageFile + "!\n" + e.toString());
80 * Make really the conversion from svg to png
82 private static String convertToPNG(TranscoderInput input, File imageFile, String imageName) {
83 Transcoder trans = new PNGTranscoder();
84 trans.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE, Boolean.TRUE);
87 OutputStream os = new FileOutputStream(imageFile);
88 TranscoderOutput output = new TranscoderOutput(os);
89 trans.transcode(input, output);
92 return "<img src=\'" + imageName + "\'/>";
93 } catch (Exception e) {
94 System.err.println("Problem when exporting SVG to " + imageFile + "!\n" + e.toString());