JAL-1620 version bump and release notes
[jalview.git] / src / jalview / util / ImageMaker.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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.gui.EPSOptions;
24 import jalview.gui.SVGOptions;
25 import jalview.io.JalviewFileChooser;
26
27 import java.awt.Component;
28 import java.awt.Graphics;
29 import java.awt.Graphics2D;
30 import java.awt.RenderingHints;
31 import java.awt.image.BufferedImage;
32 import java.io.File;
33 import java.io.FileOutputStream;
34
35 import javax.imageio.ImageIO;
36
37 import org.jfree.graphics2d.svg.SVGGraphics2D;
38 import org.jfree.graphics2d.svg.SVGHints;
39 import org.jibble.epsgraphics.EpsGraphics2D;
40
41 public class ImageMaker
42 {
43   EpsGraphics2D pg;
44
45   SVGGraphics2D g2;
46
47   Graphics graphics;
48
49   FileOutputStream out;
50
51   BufferedImage bi;
52
53   TYPE type;
54
55   public enum TYPE
56   {
57     EPS("EPS", MessageManager.getString("label.eps_file"), getEPSChooser()), PNG(
58             "PNG", MessageManager.getString("label.png_image"),
59             getPNGChooser()), SVG("SVG", "SVG", getSVGChooser());
60
61     private JalviewFileChooser chooser;
62
63     private String name;
64
65     private String label;
66
67     TYPE(String name, String label, JalviewFileChooser chooser)
68     {
69       this.name = name;
70       this.label = label;
71       this.chooser = chooser;
72     }
73
74     public String getName()
75     {
76       return name;
77     }
78
79     public JalviewFileChooser getChooser()
80     {
81       return chooser;
82     }
83
84     public String getLabel()
85     {
86       return label;
87     }
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 "
135  + type.getName() + " file.");
136       }
137     }
138   }
139
140   public Graphics getGraphics()
141   {
142     return graphics;
143   }
144
145
146
147   public void writeImage()
148   {
149     try
150     {
151       switch (type)
152       {
153       case EPS:
154         pg.flush();
155         pg.close();
156         break;
157       case SVG:
158         String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
159         out.write(svgData.getBytes());
160         out.flush();
161         out.close();
162         break;
163       case PNG:
164         ImageIO.write(bi, "png", out);
165         out.flush();
166         out.close();
167         break;
168       }
169     } catch (Exception ex)
170     {
171       ex.printStackTrace();
172     }
173   }
174
175   void setupEPS(int width, int height, String title)
176   {
177     boolean accurateText = true;
178
179     String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",
180             "Prompt each time");
181
182     // If we need to prompt, and if the GUI is visible then
183     // Prompt for EPS rendering style
184     if (renderStyle.equalsIgnoreCase("Prompt each time")
185             && !(System.getProperty("java.awt.headless") != null && System
186                     .getProperty("java.awt.headless").equals("true")))
187     {
188       EPSOptions eps = new EPSOptions();
189       renderStyle = eps.getValue();
190
191       if (renderStyle == null || eps.cancelled)
192       {
193         return;
194       }
195     }
196
197     if (renderStyle.equalsIgnoreCase("text"))
198     {
199       accurateText = false;
200     }
201
202     try
203     {
204       pg = new EpsGraphics2D(title, out, 0, 0, width, height);
205       Graphics2D ig2 = pg;
206       ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
207               RenderingHints.VALUE_ANTIALIAS_ON);
208
209       pg.setAccurateTextMode(accurateText);
210
211       graphics = pg;
212     } catch (Exception ex)
213     {
214     }
215   }
216
217   void setupPNG(int width, int height)
218   {
219     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
220     graphics = bi.getGraphics();
221     Graphics2D ig2 = (Graphics2D) graphics;
222     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
223             RenderingHints.VALUE_ANTIALIAS_ON);
224
225   }
226
227   void setupSVG(int width, int height, String title)
228   {
229
230     g2 = new SVGGraphics2D(width, height);
231     Graphics2D ig2 = g2;
232
233     String renderStyle = jalview.bin.Cache.getDefault("SVG_RENDERING",
234             "Prompt each time");
235
236     // If we need to prompt, and if the GUI is visible then
237     // Prompt for EPS rendering style
238     if (renderStyle.equalsIgnoreCase("Prompt each time")
239             && !(System.getProperty("java.awt.headless") != null && System
240                     .getProperty("java.awt.headless").equals("true")))
241     {
242       SVGOptions svgOption = new SVGOptions();
243       renderStyle = svgOption.getValue();
244
245       if (renderStyle == null || svgOption.cancelled)
246       {
247         return;
248       }
249     }
250
251     if (renderStyle.equalsIgnoreCase("lineart"))
252     {
253       ig2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
254               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
255     }
256
257     graphics = g2;
258   }
259
260   static JalviewFileChooser getPNGChooser()
261   {
262     return new jalview.io.JalviewFileChooser(
263             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
264             { "png" }, new String[]
265             { "Portable network graphics" }, "Portable network graphics");
266   }
267
268   static JalviewFileChooser getEPSChooser()
269   {
270     return new jalview.io.JalviewFileChooser(
271             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
272             { "eps" }, new String[]
273             { "Encapsulated Postscript" }, "Encapsulated Postscript");
274   }
275
276   static JalviewFileChooser getSVGChooser()
277   {
278     return new jalview.io.JalviewFileChooser(
279             jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[]
280             { "svg" }, new String[]
281             { "Scalable Vector Graphics" }, "Scalable Vector Graphics");
282   }
283 }