2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - 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;
15 import java.io.BufferedOutputStream;
16 import java.io.BufferedReader;
18 import java.io.FileInputStream;
19 import java.io.FileNotFoundException;
20 import java.io.FileOutputStream;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.OutputStream;
26 import org.scilab.modules.commons.ScilabCommons;
27 import org.scilab.modules.helptools.HTMLDocbookTagConverter;
30 * Scilab code to PNG converter
32 public class ScilabImageConverter implements ExternalImageConverter {
34 private static ScilabImageConverter instance;
35 private final StringBuilder buffer;
36 private final HTMLDocbookTagConverter.GenerationType type;
38 private ScilabImageConverter(HTMLDocbookTagConverter.GenerationType type) {
39 buffer = new StringBuilder(8192);
43 public String getMimeType() {
44 return "image/scilab";
50 public boolean mustRegenerate() {
54 public static String getFileWithScilabCode() {
55 if (instance.buffer.length() != 0) {
57 File f = File.createTempFile("help-", ".sce", new File(ScilabCommons.getTMPDIR()));
58 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f));
59 byte[] arr = instance.buffer.toString().getBytes();
60 out.write(arr, 0, arr.length);
64 return f.getAbsolutePath();
65 } catch (Exception e) {
66 System.err.println("Cannot generate the file with Scilab code to execute:\n" + e);
78 * Since this a singleton class...
81 public static ScilabImageConverter getInstance(HTMLDocbookTagConverter.GenerationType type) {
82 if (instance == null) {
83 instance = new ScilabImageConverter(type);
89 public static ScilabImageConverter getInstance() {
96 public String convertToImage(String currentFile, String code, Map<String, String> attributes, File imageFile, String imageName) {
97 return convertToPNG(currentFile, code, imageFile, imageName);
103 public String convertToImage(File code, Map<String, String> attributes, File imageFile, String imageName) {
105 BufferedReader in = new BufferedReader(new FileReader(code));
106 StringBuilder buffer = new StringBuilder(8192);
109 while ((line = in.readLine()) != null) {
110 buffer.append(line).append("\n");
115 return convertToPNG(code.getName(), buffer.toString(), imageFile, imageName);
116 } catch (Exception e) {
117 System.err.println("Problem when exporting Scilab code to " + imageFile + "!\n" + e.toString());
123 private final String convertToPNG(String currentFile, String code, File imageFile, String imageName) {
124 buffer.append("__olddrv__=driver();\n");
125 buffer.append("disp(\"Generate image " + imageName + " from Scilab code from file " + new File(currentFile).getName() + "\");\n");
126 buffer.append("driver(\"png\");\n");
127 buffer.append("xinit(\"").append(imageFile.getAbsolutePath()).append("\");\n");
128 buffer.append(code).append("\n");
129 buffer.append("___f___=gcf();___f___.anti_aliasing=\"2x\";clear(\"___f___\");\n");
130 buffer.append("xend();\n");
131 buffer.append("driver(__olddrv__);\n");
133 return getHTMLCodeToReturn(code, "<img src=\'" + imageName + "\'/>");
136 public String getHTMLCodeToReturn(String code, String imageTag) {
137 if (type == HTMLDocbookTagConverter.GenerationType.WEB) {
138 /* Prepare the code for the html inclusion */
139 code = convertCode(code);
140 /* Provide a tooltip */
141 return "<div rel='tooltip' title='" + code + "'>" + imageTag + "</div>";
143 /* No tooltip in the javahelp browser ...
144 * too limited html capabilities */
149 private static final String convertCode(String code) {
150 if (code == null || code.length() == 0) {
154 StringBuffer buffer = new StringBuffer(2 * code.length());
156 int end = code.length() - 1;
157 char c = code.charAt(0);
160 while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
162 c = code.charAt(++start);
167 c = code.charAt(end);
168 while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
170 c = code.charAt(--end);
176 // replace chars by their html entities equivalent
177 for (int j = start; j <= end; j++) {
181 buffer.append("&");
184 buffer.append("<");
187 buffer.append(">");
190 buffer.append("<br />");
193 buffer.append("'");
196 buffer.append(""");
203 return buffer.toString();