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() {
55 public boolean mustRegenerate() {
60 * Since this a singleton class...
63 public static ExternalImageConverter getInstance(HTMLDocbookTagConverter.GenerationType type) {
64 if (instance == null) {
65 instance = new SVGImageConverter(type);
74 public String convertToImage(String currentFile, String svg, Map<String, String> attributes, File imageFile, String imageName) {
75 return convertToPNG(new TranscoderInput(new StringReader(svg)), imageFile, imageName);
81 public String convertToImage(File svg, Map<String, String> attributes, File imageFile, String imageName) {
83 return convertToPNG(new TranscoderInput(new FileInputStream(svg)), imageFile, imageName);
84 } catch (FileNotFoundException e) {
85 System.err.println("Problem when exporting SVG to " + imageFile + "!\n" + e.toString());
92 * Make really the conversion from svg to png
94 private static String convertToPNG(TranscoderInput input, File imageFile, String imageName) {
95 Transcoder trans = new PNGTranscoder();
96 trans.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE, Boolean.TRUE);
99 OutputStream os = new FileOutputStream(imageFile);
100 TranscoderOutput output = new TranscoderOutput(os);
101 trans.transcode(input, output);
104 return "<img src=\'" + imageName + "\'/>";
105 } catch (Exception e) {
106 System.err.println("Problem when exporting SVG to " + imageFile + "!\n" + e.toString());