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