JAL-1769 fixed headless execution mode and created test for all possible outputs
[jalview.git] / src / jalview / util / ImageMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.util;
22
23 import jalview.bin.Jalview;
24 import jalview.gui.EPSOptions;
25 import jalview.gui.SVGOptions;
26 import jalview.io.JalviewFileChooser;
27
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;
33 import java.io.File;
34 import java.io.FileOutputStream;
35
36 import javax.imageio.ImageIO;
37
38 import org.jfree.graphics2d.svg.SVGGraphics2D;
39 import org.jfree.graphics2d.svg.SVGHints;
40 import org.jibble.epsgraphics.EpsGraphics2D;
41
42 public class ImageMaker
43 {
44   EpsGraphics2D pg;
45
46   SVGGraphics2D g2;
47
48   Graphics graphics;
49
50   FileOutputStream out;
51
52   BufferedImage bi;
53
54   TYPE type;
55
56   public enum TYPE
57   {
58     EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
59             "PNG", MessageManager.getString("label.png_image"),
60             getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
61
62     private JalviewFileChooser chooser;
63
64     private String name;
65
66     private String label;
67
68     TYPE(String name, String label, JalviewFileChooser chooser)
69     {
70       this.name = name;
71       this.label = label;
72       this.chooser = chooser;
73     }
74
75     public String getName()
76     {
77       return name;
78     }
79
80     public JalviewFileChooser getChooser()
81     {
82       return chooser;
83     }
84
85     public String getLabel()
86     {
87       return label;
88     }
89
90   }
91
92
93   public ImageMaker(Component parent, TYPE type, String title, int width,
94           int height, File file, String fileTitle)
95   {
96     this.type = type;
97
98     if (file == null)
99     {
100       JalviewFileChooser chooser;
101       chooser = type.getChooser();
102       chooser.setFileView(new jalview.io.JalviewFileView());
103       chooser.setDialogTitle(title);
104       chooser.setToolTipText(MessageManager.getString("action.save"));
105       int value = chooser.showSaveDialog(parent);
106
107       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
108       {
109         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
110                 .getSelectedFile().getParent());
111         file = chooser.getSelectedFile();
112       }
113     }
114
115     if (file != null)
116     {
117       try
118       {
119         out = new FileOutputStream(file);
120         if (type == TYPE.SVG)
121         {
122           setupSVG(width, height, fileTitle);
123         }
124         else if (type == TYPE.EPS)
125         {
126           setupEPS(width, height, fileTitle);
127         }
128         else if (type == TYPE.PNG)
129         {
130           setupPNG(width, height);
131         }
132       
133       } catch (Exception ex)
134       {
135         System.out.println("Error creating "
136  + type.getName() + " file.");
137       }
138     }
139   }
140
141   public Graphics getGraphics()
142   {
143     return graphics;
144   }
145
146
147
148   public void writeImage()
149   {
150     try
151     {
152       switch (type)
153       {
154       case EPS:
155         pg.flush();
156         pg.close();
157         break;
158       case SVG:
159         String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
160         out.write(svgData.getBytes());
161         out.flush();
162         out.close();
163         break;
164       case PNG:
165         ImageIO.write(bi, "png", out);
166         out.flush();
167         out.close();
168         break;
169       }
170     } catch (Exception ex)
171     {
172       ex.printStackTrace();
173     }
174   }
175
176   void setupEPS(int width, int height, String title)
177   {
178     boolean accurateText = true;
179
180     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
181             "Prompt each time");
182
183     // If we need to prompt, and if the GUI is visible then
184     // Prompt for EPS rendering style
185     if (renderStyle.equalsIgnoreCase("Prompt each time")
186             && !(System.getProperty("java.awt.headless") != null && System
187                     .getProperty("java.awt.headless").equals("true")))
188     {
189       EPSOptions eps = new EPSOptions();
190       renderStyle = eps.getValue();
191
192       if (renderStyle == null || eps.cancelled)
193       {
194         return;
195       }
196     }
197
198     if (renderStyle.equalsIgnoreCase("text"))
199     {
200       accurateText = false;
201     }
202
203     try
204     {
205       pg = new EpsGraphics2D(title, out, 0, 0, width, height);
206       Graphics2D ig2 = pg;
207       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
208               RenderingHints.VALUE_ANTIALIAS_ON);
209
210       pg.setAccurateTextMode(accurateText);
211
212       graphics = pg;
213     } catch (Exception ex)
214     {
215     }
216   }
217
218   void setupPNG(int width, int height)
219   {
220     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
221     graphics = bi.getGraphics();
222     Graphics2D ig2 = (Graphics2D) graphics;
223     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
224             RenderingHints.VALUE_ANTIALIAS_ON);
225
226   }
227
228   void setupSVG(int width, int height, String title)
229   {
230
231     g2 = new SVGGraphics2D(width, height);
232     Graphics2D ig2 = g2;
233
234     String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
235             "Prompt each time");
236
237     // If we need to prompt, and if the GUI is visible then
238     // Prompt for EPS rendering style
239     if (renderStyle.equalsIgnoreCase("Prompt each time")
240             && !(System.getProperty("java.awt.headless") != null && System
241                     .getProperty("java.awt.headless").equals("true")))
242     {
243       SVGOptions svgOption = new SVGOptions();
244       renderStyle = svgOption.getValue();
245
246       if (renderStyle == null || svgOption.cancelled)
247       {
248         return;
249       }
250     }
251
252     if (renderStyle.equalsIgnoreCase("lineart"))
253     {
254       ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
255               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
256     }
257
258     graphics = g2;
259   }
260
261   static JalviewFileChooser getPNGChooser()
262   {
263     if (Jalview.isHeadlessMode())
264   {
265       return null;
266     }
267     return new jalview.io.JalviewFileChooser(
268             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
269             { "png" }, new String[]
270             { "Portable network graphics" }, "Portable network graphics");
271   }
272
273   static JalviewFileChooser getEPSChooser()
274   {
275     if (Jalview.isHeadlessMode())
276     {
277       return null;
278     }
279     return new jalview.io.JalviewFileChooser(
280             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
281             { "eps" }, new String[]
282             { "Encapsulated Postscript" }, "Encapsulated Postscript");
283   }
284
285   static JalviewFileChooser getSVGChooser()
286   {
287     if (Jalview.isHeadlessMode())
288   {
289       return null;
290     }
291     return new jalview.io.JalviewFileChooser(
292             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
293             { "svg" }, new String[]
294             { "Scalable Vector Graphics" }, "Scalable Vector Graphics");
295   }
296 }