2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.bin.Jalview;
24 import jalview.gui.EPSOptions;
25 import jalview.gui.IProgressIndicator;
26 import jalview.gui.SVGOptions;
27 import jalview.io.JalviewFileChooser;
29 import java.awt.Component;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.RenderingHints;
33 import java.awt.image.BufferedImage;
35 import java.io.FileOutputStream;
37 import javax.imageio.ImageIO;
39 import org.jfree.graphics2d.svg.SVGGraphics2D;
40 import org.jfree.graphics2d.svg.SVGHints;
41 import org.jibble.epsgraphics.EpsGraphics2D;
43 public class ImageMaker
45 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
47 public static final String SVG_EXTENSION = "svg";
49 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
51 public static final String EPS_EXTENSION = "eps";
53 public static final String PNG_EXTENSION = "png";
55 public static final String PNG_DESCRIPTION = "Portable network graphics";
57 public static final String HTML_EXTENSION = "html";
59 public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
73 private IProgressIndicator pIndicator;
75 private long pSessionId;
77 private boolean headless;
81 EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
83 PNG("PNG", MessageManager.getString("label.png_image"),
84 PNG_EXTENSION, PNG_DESCRIPTION),
85 SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
91 private String extension;
93 private String description;
95 TYPE(String name, String label, String ext, String desc)
100 this.description = desc;
103 public String getName()
108 public JalviewFileChooser getFileChooser()
110 return new JalviewFileChooser(extension, description);
113 public String getLabel()
120 public ImageMaker(Component parent, TYPE type, String title, int width,
121 int height, File file, String fileTitle,
122 IProgressIndicator pIndicator, long pSessionId, boolean headless)
124 this.pIndicator = pIndicator;
126 this.pSessionId = pSessionId;
127 this.headless = headless;
130 setProgressMessage(MessageManager.formatMessage(
131 "status.waiting_for_user_to_select_output_file", type.name));
132 JalviewFileChooser chooser;
133 chooser = type.getFileChooser();
134 chooser.setFileView(new jalview.io.JalviewFileView());
135 chooser.setDialogTitle(title);
136 chooser.setToolTipText(MessageManager.getString("action.save"));
137 int value = chooser.showSaveDialog(parent);
139 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
141 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
142 chooser.getSelectedFile().getParent());
143 file = chooser.getSelectedFile();
147 setProgressMessage(MessageManager.formatMessage(
148 "status.cancelled_image_export_operation", type.name));
156 out = new FileOutputStream(file);
157 setProgressMessage(null);
158 setProgressMessage(MessageManager.formatMessage(
159 "status.exporting_alignment_as_x_file", type.getName()));
160 if (type == TYPE.SVG)
162 setupSVG(width, height, fileTitle);
164 else if (type == TYPE.EPS)
166 setupEPS(width, height, fileTitle);
168 else if (type == TYPE.PNG)
170 setupPNG(width, height);
173 } catch (Exception ex)
175 System.out.println("Error creating " + type.getName() + " file.");
177 setProgressMessage(MessageManager
178 .formatMessage("info.error_creating_file", type.getName()));
183 public Graphics getGraphics()
188 public void writeImage()
199 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
200 out.write(svgData.getBytes());
205 ImageIO.write(bi, PNG_EXTENSION, out);
210 } catch (Exception ex)
212 ex.printStackTrace();
216 void setupEPS(int width, int height, String title)
218 boolean accurateText = true;
220 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
223 // If we need to prompt, and if the GUI is visible then
224 // Prompt for EPS rendering style
225 if (renderStyle.equalsIgnoreCase("Prompt each time")
226 && !(System.getProperty("java.awt.headless") != null && System
227 .getProperty("java.awt.headless").equals("true")))
229 EPSOptions eps = new EPSOptions();
230 renderStyle = eps.getValue();
232 if (renderStyle == null || eps.cancelled)
234 setProgressMessage(MessageManager.formatMessage(
235 "status.cancelled_image_export_operation", "EPS"));
240 if (renderStyle.equalsIgnoreCase("text"))
242 accurateText = false;
247 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
249 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
250 RenderingHints.VALUE_ANTIALIAS_ON);
252 pg.setAccurateTextMode(accurateText);
255 setProgressMessage(MessageManager
256 .formatMessage("status.export_complete", type.getName()));
257 } catch (Exception ex)
262 void setupPNG(int width, int height)
264 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
265 graphics = bi.getGraphics();
266 Graphics2D ig2 = (Graphics2D) graphics;
267 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
268 RenderingHints.VALUE_ANTIALIAS_ON);
269 setProgressMessage(MessageManager
270 .formatMessage("status.export_complete", type.getName()));
274 void setupSVG(int width, int height, String title)
277 g2 = new SVGGraphics2D(width, height);
280 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
283 // If we need to prompt, and if the GUI is visible then
284 // Prompt for EPS rendering style
285 if (renderStyle.equalsIgnoreCase("Prompt each time")
286 && !(System.getProperty("java.awt.headless") != null && System
287 .getProperty("java.awt.headless").equals("true")))
289 SVGOptions svgOption = new SVGOptions();
290 renderStyle = svgOption.getValue();
292 if (renderStyle == null || svgOption.cancelled)
294 setProgressMessage(MessageManager.formatMessage(
295 "status.cancelled_image_export_operation", "SVG"));
300 if (renderStyle.equalsIgnoreCase("Lineart"))
302 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
303 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
306 setProgressMessage(MessageManager
307 .formatMessage("status.export_complete", type.getName()));
311 static JalviewFileChooser getPNGChooser()
313 if (Jalview.isHeadlessMode())
317 return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
320 static JalviewFileChooser getEPSChooser()
322 if (Jalview.isHeadlessMode())
326 return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
329 private void setProgressMessage(String message)
331 if (pIndicator != null && !headless)
333 pIndicator.setProgressBar(message, pSessionId);
337 static JalviewFileChooser getSVGChooser()
339 if (Jalview.isHeadlessMode())
343 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);