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;
31 import org.scilab.modules.helptools.HTMLDocbookTagConverter;
34 * SVG to PNG converter
36 public class SVGImageConverter implements ExternalImageConverter {
38 private static SVGImageConverter instance;
39 private final HTMLDocbookTagConverter.GenerationType type;
41 private SVGImageConverter(HTMLDocbookTagConverter.GenerationType type) {
48 public String getMimeType() {
53 * Since this a singleton class...
56 public static ExternalImageConverter getInstance(HTMLDocbookTagConverter.GenerationType type) {
57 if (instance == null) {
58 instance = new SVGImageConverter(type);
67 public String convertToImage(String currentFile, String svg, Map<String, String> attributes, File imageFile, String imageName) {
68 return convertToPNG(new TranscoderInput(new StringReader(svg)), imageFile, imageName);
74 public String convertToImage(File svg, Map<String, String> attributes, File imageFile, String imageName) {
76 return convertToPNG(new TranscoderInput(new FileInputStream(svg)), imageFile, imageName);
77 } catch (FileNotFoundException e) {
78 System.err.println("Problem when exporting SVG to " + imageFile + "!\n" + e.toString());
85 * Make really the conversion from svg to png
87 private static String convertToPNG(TranscoderInput input, File imageFile, String imageName) {
88 Transcoder trans = new PNGTranscoder();
89 trans.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE, Boolean.TRUE);
92 OutputStream os = new FileOutputStream(imageFile);
93 TranscoderOutput output = new TranscoderOutput(os);
94 trans.transcode(input, output);
97 return "<img src=\'" + imageName + "\'/>";
98 } catch (Exception e) {
99 System.err.println("Problem when exporting SVG to " + imageFile + "!\n" + e.toString());