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.SVGOptions;
27 import jalview.io.JalviewFileChooser;
28 import jalview.io.JalviewFileView;
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
60 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
61 "PNG", MessageManager.getString("label.png_image"),
62 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
64 private JalviewFileChooser chooser;
70 TYPE(String name, String label, JalviewFileChooser chooser)
74 this.chooser = chooser;
77 public String getName()
82 public JalviewFileChooser getChooser()
87 public String getLabel()
95 public ImageMaker(Component parent, TYPE type, String title, int width,
96 int height, File file, String fileTitle)
102 JalviewFileChooser chooser;
103 chooser = type.getChooser();
104 chooser.setFileView(new JalviewFileView());
105 chooser.setDialogTitle(title);
106 chooser.setToolTipText(MessageManager.getString("action.save"));
107 int value = chooser.showSaveDialog(parent);
109 if (value == JalviewFileChooser.APPROVE_OPTION)
111 Cache.setProperty("LAST_DIRECTORY", chooser
112 .getSelectedFile().getParent());
113 file = chooser.getSelectedFile();
121 out = new FileOutputStream(file);
122 if (type == TYPE.SVG)
124 setupSVG(width, height, fileTitle);
126 else if (type == TYPE.EPS)
128 setupEPS(width, height, fileTitle);
130 else if (type == TYPE.PNG)
132 setupPNG(width, height);
135 } catch (Exception ex)
137 System.out.println("Error creating "
138 + type.getName() + " file.");
143 public Graphics getGraphics()
150 public void writeImage()
161 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
162 out.write(svgData.getBytes());
167 ImageIO.write(bi, "png", out);
172 } catch (Exception ex)
174 ex.printStackTrace();
178 void setupEPS(int width, int height, String title)
180 boolean accurateText = true;
182 String renderStyle = Cache.getDefault("EPS_RENDERING",
185 // If we need to prompt, and if the GUI is visible then
186 // Prompt for EPS rendering style
187 if (renderStyle.equalsIgnoreCase("Prompt each time")
188 && !(System.getProperty("java.awt.headless") != null && System
189 .getProperty("java.awt.headless").equals("true")))
191 EPSOptions eps = new EPSOptions();
192 renderStyle = eps.getValue();
194 if (renderStyle == null || eps.cancelled)
200 if (renderStyle.equalsIgnoreCase("text"))
202 accurateText = false;
207 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
209 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
210 RenderingHints.VALUE_ANTIALIAS_ON);
212 pg.setAccurateTextMode(accurateText);
215 } catch (Exception ex)
220 void setupPNG(int width, int height)
222 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
223 graphics = bi.getGraphics();
224 Graphics2D ig2 = (Graphics2D) graphics;
225 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
226 RenderingHints.VALUE_ANTIALIAS_ON);
230 void setupSVG(int width, int height, String title)
233 g2 = new SVGGraphics2D(width, height);
236 String renderStyle = Cache.getDefault("SVG_RENDERING",
239 // If we need to prompt, and if the GUI is visible then
240 // Prompt for EPS rendering style
241 if (renderStyle.equalsIgnoreCase("Prompt each time")
242 && !(System.getProperty("java.awt.headless") != null && System
243 .getProperty("java.awt.headless").equals("true")))
245 SVGOptions svgOption = new SVGOptions();
246 renderStyle = svgOption.getValue();
248 if (renderStyle == null || svgOption.cancelled)
254 if (renderStyle.equalsIgnoreCase("lineart"))
256 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
257 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
263 static JalviewFileChooser getPNGChooser()
265 if (Jalview.isHeadlessMode())
269 return new jalview.io.JalviewFileChooser(
270 Cache.getProperty("LAST_DIRECTORY"), new String[]
271 { "png" }, new String[]
272 { "Portable network graphics" }, "Portable network graphics");
275 static JalviewFileChooser getEPSChooser()
277 if (Jalview.isHeadlessMode())
281 return new JalviewFileChooser(
282 Cache.getProperty("LAST_DIRECTORY"), new String[]
283 { "eps" }, new String[]
284 { "Encapsulated Postscript" }, "Encapsulated Postscript");
287 static JalviewFileChooser getSVGChooser()
289 if (Jalview.isHeadlessMode())
293 return new JalviewFileChooser(
294 Cache.getProperty("LAST_DIRECTORY"), new String[]
295 { "svg" }, new String[]
296 { "Scalable Vector Graphics" }, "Scalable Vector Graphics");