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;
80 * Image format, currently either EPS, PNG or SVG
84 EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
86 PNG("PNG", MessageManager.getString("label.png_image"),
87 PNG_EXTENSION, PNG_DESCRIPTION),
88 SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
94 private String extension;
96 private String description;
98 TYPE(String name, String label, String ext, String desc)
102 this.extension = ext;
103 this.description = desc;
106 public String getName()
111 public JalviewFileChooser getFileChooser()
113 return new JalviewFileChooser(extension, description);
116 public String getLabel()
124 * Constructor that prepares the image context for writing to. If {@code file}
125 * is null, the user is prompted to choose a file.
138 public ImageMaker(Component parent, TYPE type, String title, int width,
139 int height, File file, String fileTitle,
140 IProgressIndicator pIndicator, long pSessionId, boolean headless)
142 this.pIndicator = pIndicator;
144 this.pSessionId = pSessionId;
145 this.headless = headless;
148 setProgressMessage(MessageManager.formatMessage(
149 "status.waiting_for_user_to_select_output_file", type.name));
150 JalviewFileChooser chooser;
151 chooser = type.getFileChooser();
152 chooser.setFileView(new jalview.io.JalviewFileView());
153 chooser.setDialogTitle(title);
154 chooser.setToolTipText(MessageManager.getString("action.save"));
155 int value = chooser.showSaveDialog(parent);
157 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
159 jalview.bin.Cache.setProperty("LAST_DIRECTORY",
160 chooser.getSelectedFile().getParent());
161 file = chooser.getSelectedFile();
165 setProgressMessage(MessageManager.formatMessage(
166 "status.cancelled_image_export_operation", type.name));
174 out = new FileOutputStream(file);
175 setProgressMessage(null);
176 setProgressMessage(MessageManager.formatMessage(
177 "status.exporting_alignment_as_x_file", type.getName()));
178 if (type == TYPE.SVG)
180 setupSVG(width, height, fileTitle);
182 else if (type == TYPE.EPS)
184 setupEPS(width, height, fileTitle);
186 else if (type == TYPE.PNG)
188 setupPNG(width, height);
191 } catch (Exception ex)
193 System.out.println("Error creating " + type.getName() + " file.");
195 setProgressMessage(MessageManager
196 .formatMessage("info.error_creating_file", type.getName()));
201 public Graphics getGraphics()
206 public void writeImage()
217 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
218 out.write(svgData.getBytes());
223 ImageIO.write(bi, PNG_EXTENSION, out);
228 } catch (Exception ex)
230 ex.printStackTrace();
234 void setupEPS(int width, int height, String title)
236 boolean accurateText = true;
238 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
241 // If we need to prompt, and if the GUI is visible then
242 // Prompt for EPS rendering style
243 if (renderStyle.equalsIgnoreCase("Prompt each time")
244 && !(System.getProperty("java.awt.headless") != null && System
245 .getProperty("java.awt.headless").equals("true")))
247 EPSOptions eps = new EPSOptions();
248 renderStyle = eps.getValue();
250 if (renderStyle == null || eps.cancelled)
252 setProgressMessage(MessageManager.formatMessage(
253 "status.cancelled_image_export_operation", "EPS"));
258 if (renderStyle.equalsIgnoreCase("text"))
260 accurateText = false;
265 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
267 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
268 RenderingHints.VALUE_ANTIALIAS_ON);
270 pg.setAccurateTextMode(accurateText);
273 setProgressMessage(MessageManager
274 .formatMessage("status.export_complete", type.getName()));
275 } catch (Exception ex)
280 void setupPNG(int width, int height)
282 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
283 graphics = bi.getGraphics();
284 Graphics2D ig2 = (Graphics2D) graphics;
285 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
286 RenderingHints.VALUE_ANTIALIAS_ON);
287 setProgressMessage(MessageManager
288 .formatMessage("status.export_complete", type.getName()));
292 void setupSVG(int width, int height, String title)
295 g2 = new SVGGraphics2D(width, height);
298 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
301 // If we need to prompt, and if the GUI is visible then
302 // Prompt for EPS rendering style
303 if (renderStyle.equalsIgnoreCase("Prompt each time")
304 && !(System.getProperty("java.awt.headless") != null && System
305 .getProperty("java.awt.headless").equals("true")))
307 SVGOptions svgOption = new SVGOptions();
308 renderStyle = svgOption.getValue();
310 if (renderStyle == null || svgOption.cancelled)
312 setProgressMessage(MessageManager.formatMessage(
313 "status.cancelled_image_export_operation", "SVG"));
318 if (renderStyle.equalsIgnoreCase("Lineart"))
320 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
321 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
324 setProgressMessage(MessageManager
325 .formatMessage("status.export_complete", type.getName()));
329 static JalviewFileChooser getPNGChooser()
331 if (Jalview.isHeadlessMode())
335 return new JalviewFileChooser(PNG_EXTENSION, PNG_DESCRIPTION);
338 static JalviewFileChooser getEPSChooser()
340 if (Jalview.isHeadlessMode())
344 return new JalviewFileChooser(EPS_EXTENSION, EPS_DESCRIPTION);
347 private void setProgressMessage(String message)
349 if (pIndicator != null && !headless)
351 pIndicator.setProgressBar(message, pSessionId);
355 static JalviewFileChooser getSVGChooser()
357 if (Jalview.isHeadlessMode())
361 return new JalviewFileChooser(SVG_EXTENSION, SVG_DESCRIPTION);