e1543bf1898ba057a9944a3703c1918b8821edac
[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   public ImageMaker(Component parent, TYPE type, String title, int width,
93           int height, File file, String fileTitle)
94   {
95     this.type = type;
96
97     if (file == null)
98     {
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);
105
106       if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
107       {
108         jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser
109                 .getSelectedFile().getParent());
110         file = chooser.getSelectedFile();
111       }
112     }
113
114     if (file != null)
115     {
116       try
117       {
118         out = new FileOutputStream(file);
119         if (type == TYPE.SVG)
120         {
121           setupSVG(width, height, fileTitle);
122         }
123         else if (type == TYPE.EPS)
124         {
125           setupEPS(width, height, fileTitle);
126         }
127         else if (type == TYPE.PNG)
128         {
129           setupPNG(width, height);
130         }
131
132       } catch (Exception ex)
133       {
134         System.out.println("Error creating " + type.getName() + " file.");
135       }
136     }
137   }
138
139   public Graphics getGraphics()
140   {
141     return graphics;
142   }
143
144   public void writeImage()
145   {
146     try
147     {
148       switch (type)
149       {
150       case EPS:
151         pg.flush();
152         pg.close();
153         break;
154       case SVG:
155         String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
156         out.write(svgData.getBytes());
157         out.flush();
158         out.close();
159         break;
160       case PNG:
161         ImageIO.write(bi, "png", out);
162         out.flush();
163         out.close();
164         break;
165       }
166     } catch (Exception ex)
167     {
168       ex.printStackTrace();
169     }
170   }
171
172   void setupEPS(int width, int height, String title)
173   {
174     boolean accurateText = true;
175
176     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
177             "Prompt each time");
178
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")))
184     {
185       EPSOptions eps = new EPSOptions();
186       renderStyle = eps.getValue();
187
188       if (renderStyle == null || eps.cancelled)
189       {
190         return;
191       }
192     }
193
194     if (renderStyle.equalsIgnoreCase("text"))
195     {
196       accurateText = false;
197     }
198
199     try
200     {
201       pg = new EpsGraphics2D(title, out, 0, 0, width, height);
202       Graphics2D ig2 = pg;
203       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
204               RenderingHints.VALUE_ANTIALIAS_ON);
205
206       pg.setAccurateTextMode(accurateText);
207
208       graphics = pg;
209     } catch (Exception ex)
210     {
211     }
212   }
213
214   void setupPNG(int width, int height)
215   {
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);
221
222   }
223
224   void setupSVG(int width, int height, String title)
225   {
226
227     g2 = new SVGGraphics2D(width, height);
228     Graphics2D ig2 = g2;
229
230     String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
231             "Prompt each time");
232
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")))
238     {
239       SVGOptions svgOption = new SVGOptions();
240       renderStyle = svgOption.getValue();
241
242       if (renderStyle == null || svgOption.cancelled)
243       {
244         return;
245       }
246     }
247
248     if (renderStyle.equalsIgnoreCase("lineart"))
249     {
250       ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
251               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
252     }
253
254     graphics = g2;
255   }
256
257   static JalviewFileChooser getPNGChooser()
258   {
259     if (Jalview.isHeadlessMode())
260     {
261       return null;
262     }
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");
268   }
269
270   static JalviewFileChooser getEPSChooser()
271   {
272     if (Jalview.isHeadlessMode())
273     {
274       return null;
275     }
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");
281   }
282
283   static JalviewFileChooser getSVGChooser()
284   {
285     if (Jalview.isHeadlessMode())
286     {
287       return null;
288     }
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");
294   }
295 }