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
59 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
60 "PNG", MessageManager.getString("label.png_image"),
61 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
63 private JalviewFileChooser chooser;
69 TYPE(String name, String label, JalviewFileChooser chooser)
73 this.chooser = chooser;
76 public String getName()
81 public JalviewFileChooser getChooser()
86 public String getLabel()
93 public ImageMaker(Component parent, TYPE type, String title, int width,
94 int height, File file, String fileTitle,
95 IProgressIndicator pIndicator, long pSessionId, boolean headless)
101 if (pIndicator != null && !headless)
103 pIndicator.setProgressBar(
104 MessageManager.formatMessage(
105 "status.waiting_for_user_to_select_output_file",
106 type.name), pSessionId);
108 JalviewFileChooser chooser;
109 chooser = type.getChooser();
110 chooser.setFileView(new jalview.io.JalviewFileView());
111 chooser.setDialogTitle(title);
112 chooser.setToolTipText(MessageManager.getString("action.save"));
113 int value = chooser.showSaveDialog(parent);
115 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
117 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
118 .getSelectedFile().getParent());
119 file = chooser.getSelectedFile();
123 if (pIndicator != null && !headless)
125 pIndicator.setProgressBar(MessageManager.formatMessage(
126 "status.cancelled_image_export_operation", type.name),
136 out = new FileOutputStream(file);
137 pIndicator.setProgressBar(null, pSessionId);
138 pIndicator.setProgressBar(MessageManager.formatMessage(
139 "status.exporting_alignment_as_x_file", type.getName()),
141 if (type == TYPE.SVG)
143 setupSVG(width, height, fileTitle);
145 else if (type == TYPE.EPS)
147 setupEPS(width, height, fileTitle);
149 else if (type == TYPE.PNG)
151 setupPNG(width, height);
153 if (pIndicator != null && !headless)
155 pIndicator.setProgressBar(
156 MessageManager.formatMessage(
157 "status.export_complete", type.getName()),
160 } catch (Exception ex)
162 System.out.println("Error creating " + type.getName() + " file.");
164 pIndicator.setProgressBar(MessageManager.formatMessage(
165 "info.error_creating_file", type.getName()), pSessionId);
170 public Graphics getGraphics()
175 public void writeImage()
186 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
187 out.write(svgData.getBytes());
192 ImageIO.write(bi, "png", out);
197 } catch (Exception ex)
199 ex.printStackTrace();
203 void setupEPS(int width, int height, String title)
205 boolean accurateText = true;
207 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
210 // If we need to prompt, and if the GUI is visible then
211 // Prompt for EPS rendering style
212 if (renderStyle.equalsIgnoreCase("Prompt each time")
213 && !(System.getProperty("java.awt.headless") != null && System
214 .getProperty("java.awt.headless").equals("true")))
216 EPSOptions eps = new EPSOptions();
217 renderStyle = eps.getValue();
219 if (renderStyle == null || eps.cancelled)
225 if (renderStyle.equalsIgnoreCase("text"))
227 accurateText = false;
232 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
234 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
235 RenderingHints.VALUE_ANTIALIAS_ON);
237 pg.setAccurateTextMode(accurateText);
240 } catch (Exception ex)
245 void setupPNG(int width, int height)
247 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
248 graphics = bi.getGraphics();
249 Graphics2D ig2 = (Graphics2D) graphics;
250 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
251 RenderingHints.VALUE_ANTIALIAS_ON);
255 void setupSVG(int width, int height, String title)
258 g2 = new SVGGraphics2D(width, height);
261 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
264 // If we need to prompt, and if the GUI is visible then
265 // Prompt for EPS rendering style
266 if (renderStyle.equalsIgnoreCase("Prompt each time")
267 && !(System.getProperty("java.awt.headless") != null && System
268 .getProperty("java.awt.headless").equals("true")))
270 SVGOptions svgOption = new SVGOptions();
271 renderStyle = svgOption.getValue();
273 if (renderStyle == null || svgOption.cancelled)
279 if (renderStyle.equalsIgnoreCase("Lineart"))
281 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
282 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
288 static JalviewFileChooser getPNGChooser()
290 if (Jalview.isHeadlessMode())
294 return new jalview.io.JalviewFileChooser(
295 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
296 new String[] { "png" },
297 new String[] { "Portable network graphics" },
298 "Portable network graphics");
301 static JalviewFileChooser getEPSChooser()
303 if (Jalview.isHeadlessMode())
307 return new jalview.io.JalviewFileChooser(
308 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
309 new String[] { "eps" },
310 new String[] { "Encapsulated Postscript" },
311 "Encapsulated Postscript");
314 static JalviewFileChooser getSVGChooser()
316 if (Jalview.isHeadlessMode())
320 return new jalview.io.JalviewFileChooser(
321 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
322 new String[] { "svg" },
323 new String[] { "Scalable Vector Graphics" },
324 "Scalable Vector Graphics");