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("function _generate_image_from_doc\n");
125 buffer.append("__olddrv__=driver();\n");
126 buffer.append("disp(\"Generate image " + imageName + " from Scilab code from file " + new File(currentFile).getName() + "\");\n");
127 buffer.append("driver(\"png\");\n");
128 buffer.append("xinit(\"").append(imageFile.getAbsolutePath()).append("\");\n");
129 buffer.append(code).append("\n");
130 buffer.append("___f___=gcf();___f___.anti_aliasing=\"2x\";clear(\"___f___\");\n");
131 buffer.append("xend();\n");
132 buffer.append("driver(__olddrv__);\n");
133 buffer.append("endfunction\n");
134 buffer.append("_generate_image_from_doc();\n");
135 buffer.append("clear _generate_image_from_doc;\n");
137 return getHTMLCodeToReturn(code, "<img src=\'" + imageName + "\'/>");
140 public String getHTMLCodeToReturn(String code, String imageTag) {
141 if (type == HTMLDocbookTagConverter.GenerationType.WEB) {
142 /* Prepare the code for the html inclusion */
143 code = convertCode(code);
144 /* Provide a tooltip */
145 return "<div rel='tooltip' title='" + code + "'>" + imageTag + "</div>";
147 /* No tooltip in the javahelp browser ...
148 * too limited html capabilities */
153 private static final String convertCode(String code) {
154 if (code == null || code.length() == 0) {
158 StringBuffer buffer = new StringBuffer(2 * code.length());
160 int end = code.length() - 1;
161 char c = code.charAt(0);
164 while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
166 c = code.charAt(++start);
171 c = code.charAt(end);
172 while (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
174 c = code.charAt(--end);
180 // replace chars by their html entities equivalent
181 for (int j = start; j <= end; j++) {
185 buffer.append("&");
188 buffer.append("<");
191 buffer.append(">");
194 buffer.append("<br />");
197 buffer.append("'");
200 buffer.append(""");
207 return buffer.toString();