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.Cache;
24 import jalview.bin.Jalview;
25 import jalview.gui.EPSOptions;
26 import jalview.gui.IProgressIndicator;
27 import jalview.gui.SVGOptions;
28 import jalview.io.JalviewFileChooser;
30 import java.awt.Component;
31 import java.awt.Graphics;
32 import java.awt.Graphics2D;
33 import java.awt.RenderingHints;
34 import java.awt.image.BufferedImage;
36 import java.io.FileOutputStream;
38 import javax.imageio.ImageIO;
40 import org.jfree.graphics2d.svg.SVGGraphics2D;
41 import org.jfree.graphics2d.svg.SVGHints;
42 import org.jibble.epsgraphics.EpsGraphics2D;
44 public class ImageMaker
46 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
48 public static final String SVG_EXTENSION = "svg";
50 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
52 public static final String EPS_EXTENSION = "eps";
54 public static final String PNG_EXTENSION = "png";
56 public static final String PNG_DESCRIPTION = "Portable network graphics";
58 public static final String HTML_EXTENSION = "html";
60 public static final String HTML_DESCRIPTION = "Hypertext Markup Language";
74 private IProgressIndicator pIndicator;
76 private long pSessionId;
78 private boolean headless;
82 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()),
83 PNG("PNG", MessageManager.getString("label.png_image"), getPNGChooser()),
84 SVG("SVG", "SVG", getSVGChooser());
86 private JalviewFileChooser chooser;
92 TYPE(String name, String label, JalviewFileChooser chooser)
96 this.chooser = chooser;
99 public String getName()
104 public JalviewFileChooser getChooser()
109 public String getLabel()
116 public ImageMaker(Component parent, TYPE type, String title, int width,
117 int height, File file, String fileTitle,
118 IProgressIndicator pIndicator, long pSessionId, boolean headless)
120 this.pIndicator = pIndicator;
122 this.pSessionId = pSessionId;
123 this.headless = headless;
126 setProgressMessage(MessageManager.formatMessage(
127 "status.waiting_for_user_to_select_output_file", type.name));
128 JalviewFileChooser chooser;
129 chooser = type.getChooser();
130 chooser.setFileView(new jalview.io.JalviewFileView());
131 chooser.setDialogTitle(title);
132 chooser.setToolTipText(MessageManager.getString("action.save"));
133 int value = chooser.showSaveDialog(parent);
135 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
137 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
138 .getSelectedFile().getParent());
139 file = chooser.getSelectedFile();
143 setProgressMessage(MessageManager.formatMessage(
144 "status.cancelled_image_export_operation", type.name));
152 out = new FileOutputStream(file);
153 setProgressMessage(null);
154 setProgressMessage(MessageManager.formatMessage(
155 "status.exporting_alignment_as_x_file", type.getName()));
156 if (type == TYPE.SVG)
158 setupSVG(width, height, fileTitle);
160 else if (type == TYPE.EPS)
162 setupEPS(width, height, fileTitle);
164 else if (type == TYPE.PNG)
166 setupPNG(width, height);
169 } catch (Exception ex)
171 System.out.println("Error creating " + type.getName() + " file.");
173 setProgressMessage(MessageManager.formatMessage(
174 "info.error_creating_file", type.getName()));
179 public Graphics getGraphics()
184 public void writeImage()
195 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
196 out.write(svgData.getBytes());
201 ImageIO.write(bi, PNG_EXTENSION, out);
206 } catch (Exception ex)
208 ex.printStackTrace();
212 void setupEPS(int width, int height, String title)
214 boolean accurateText = true;
216 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
219 // If we need to prompt, and if the GUI is visible then
220 // Prompt for EPS rendering style
221 if (renderStyle.equalsIgnoreCase("Prompt each time")
222 && !(System.getProperty("java.awt.headless") != null && System
223 .getProperty("java.awt.headless").equals("true")))
225 EPSOptions eps = new EPSOptions();
226 renderStyle = eps.getValue();
228 if (renderStyle == null || eps.cancelled)
230 setProgressMessage(MessageManager.formatMessage(
231 "status.cancelled_image_export_operation", "EPS"));
236 if (renderStyle.equalsIgnoreCase("text"))
238 accurateText = false;
243 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
245 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
246 RenderingHints.VALUE_ANTIALIAS_ON);
248 pg.setAccurateTextMode(accurateText);
251 setProgressMessage(MessageManager.formatMessage(
252 "status.export_complete", type.getName()));
253 } catch (Exception ex)
258 void setupPNG(int width, int height)
260 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
261 graphics = bi.getGraphics();
262 Graphics2D ig2 = (Graphics2D) graphics;
263 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
264 RenderingHints.VALUE_ANTIALIAS_ON);
265 setProgressMessage(MessageManager.formatMessage(
266 "status.export_complete", type.getName()));
270 void setupSVG(int width, int height, String title)
273 g2 = new SVGGraphics2D(width, height);
276 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
279 // If we need to prompt, and if the GUI is visible then
280 // Prompt for EPS rendering style
281 if (renderStyle.equalsIgnoreCase("Prompt each time")
282 && !(System.getProperty("java.awt.headless") != null && System
283 .getProperty("java.awt.headless").equals("true")))
285 SVGOptions svgOption = new SVGOptions();
286 renderStyle = svgOption.getValue();
288 if (renderStyle == null || svgOption.cancelled)
290 setProgressMessage(MessageManager.formatMessage(
291 "status.cancelled_image_export_operation", "SVG"));
296 if (renderStyle.equalsIgnoreCase("Lineart"))
298 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
299 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
302 setProgressMessage(MessageManager.formatMessage(
303 "status.export_complete", type.getName()));
307 static JalviewFileChooser getPNGChooser()
309 if (Jalview.isHeadlessMode())
313 return new JalviewFileChooser(Cache.getProperty("LAST_DIRECTORY"),
314 PNG_EXTENSION, PNG_DESCRIPTION);
317 static JalviewFileChooser getEPSChooser()
319 if (Jalview.isHeadlessMode())
323 return new JalviewFileChooser(Cache.getProperty("LAST_DIRECTORY"),
324 EPS_EXTENSION, EPS_DESCRIPTION);
327 private void setProgressMessage(String message)
329 if (pIndicator != null && !headless)
331 pIndicator.setProgressBar(message, pSessionId);
335 static JalviewFileChooser getSVGChooser()
337 if (Jalview.isHeadlessMode())
341 return new JalviewFileChooser(Cache.getProperty("LAST_DIRECTORY"),
342 SVG_EXTENSION, SVG_DESCRIPTION);