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.SVGOptions;
26 import jalview.io.JalviewFileChooser;
28 import java.awt.Component;
29 import java.awt.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.RenderingHints;
32 import java.awt.image.BufferedImage;
34 import java.io.FileOutputStream;
36 import javax.imageio.ImageIO;
38 import org.jfree.graphics2d.svg.SVGGraphics2D;
39 import org.jfree.graphics2d.svg.SVGHints;
40 import org.jibble.epsgraphics.EpsGraphics2D;
42 public class ImageMaker
58 EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
59 "PNG", MessageManager.getString("label.png_image"),
60 getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
62 private JalviewFileChooser chooser;
68 TYPE(String name, String label, JalviewFileChooser chooser)
72 this.chooser = chooser;
75 public String getName()
80 public JalviewFileChooser getChooser()
85 public String getLabel()
92 public ImageMaker(Component parent, TYPE type, String title, int width,
93 int height, File file, String fileTitle)
99 JalviewFileChooser chooser;
100 chooser = type.getChooser();
101 chooser.setFileView(new jalview.io.JalviewFileView());
102 chooser.setDialogTitle(title);
103 chooser.setToolTipText(MessageManager.getString("action.save"));
104 int value = chooser.showSaveDialog(parent);
106 if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
108 jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
109 .getSelectedFile().getParent());
110 file = chooser.getSelectedFile();
118 out = new FileOutputStream(file);
119 if (type == TYPE.SVG)
121 setupSVG(width, height, fileTitle);
123 else if (type == TYPE.EPS)
125 setupEPS(width, height, fileTitle);
127 else if (type == TYPE.PNG)
129 setupPNG(width, height);
132 } catch (Exception ex)
134 System.out.println("Error creating " + type.getName() + " file.");
139 public Graphics getGraphics()
144 public void writeImage()
155 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
156 out.write(svgData.getBytes());
161 ImageIO.write(bi, "png", out);
166 } catch (Exception ex)
168 ex.printStackTrace();
172 void setupEPS(int width, int height, String title)
174 boolean accurateText = true;
176 String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
179 // If we need to prompt, and if the GUI is visible then
180 // Prompt for EPS rendering style
181 if (renderStyle.equalsIgnoreCase("Prompt each time")
182 && !(System.getProperty("java.awt.headless") != null && System
183 .getProperty("java.awt.headless").equals("true")))
185 EPSOptions eps = new EPSOptions();
186 renderStyle = eps.getValue();
188 if (renderStyle == null || eps.cancelled)
194 if (renderStyle.equalsIgnoreCase("text"))
196 accurateText = false;
201 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
203 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
204 RenderingHints.VALUE_ANTIALIAS_ON);
206 pg.setAccurateTextMode(accurateText);
209 } catch (Exception ex)
214 void setupPNG(int width, int height)
216 bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
217 graphics = bi.getGraphics();
218 Graphics2D ig2 = (Graphics2D) graphics;
219 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
220 RenderingHints.VALUE_ANTIALIAS_ON);
224 void setupSVG(int width, int height, String title)
227 g2 = new SVGGraphics2D(width, height);
230 String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
233 // If we need to prompt, and if the GUI is visible then
234 // Prompt for EPS rendering style
235 if (renderStyle.equalsIgnoreCase("Prompt each time")
236 && !(System.getProperty("java.awt.headless") != null && System
237 .getProperty("java.awt.headless").equals("true")))
239 SVGOptions svgOption = new SVGOptions();
240 renderStyle = svgOption.getValue();
242 if (renderStyle == null || svgOption.cancelled)
248 if (renderStyle.equalsIgnoreCase("lineart"))
250 ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
251 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
257 static JalviewFileChooser getPNGChooser()
259 if (Jalview.isHeadlessMode())
263 return new jalview.io.JalviewFileChooser(
264 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
265 new String[] { "png" },
266 new String[] { "Portable network graphics" },
267 "Portable network graphics");
270 static JalviewFileChooser getEPSChooser()
272 if (Jalview.isHeadlessMode())
276 return new jalview.io.JalviewFileChooser(
277 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
278 new String[] { "eps" },
279 new String[] { "Encapsulated Postscript" },
280 "Encapsulated Postscript");
283 static JalviewFileChooser getSVGChooser()
285 if (Jalview.isHeadlessMode())
289 return new jalview.io.JalviewFileChooser(
290 jalview.bin.Cache.getProperty("LAST_DIRECTORY"),
291 new String[] { "svg" },
292 new String[] { "Scalable Vector Graphics" },
293 "Scalable Vector Graphics");