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