e89596b1c459eddc4422fdd56c7043955c20e849
[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.io.JalviewFileChooser;
24
25 import java.awt.Graphics;
26 import java.awt.Graphics2D;
27 import java.awt.RenderingHints;
28 import java.awt.image.BufferedImage;
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32
33 import javax.imageio.ImageIO;
34
35 import org.jfree.graphics2d.svg.SVGGraphics2D;
36 import org.jfree.graphics2d.svg.SVGHints;
37 import org.jibble.epsgraphics.EpsGraphics2D;
38
39 public class ImageMaker
40 {
41   public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
42
43   public static final String SVG_EXTENSION = "svg";
44
45   public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
46
47   public static final String EPS_EXTENSION = "eps";
48
49   public static final String PNG_EXTENSION = "png";
50
51   public static final String PNG_DESCRIPTION = "Portable  network graphics";
52
53   EpsGraphics2D pg;
54
55   Graphics graphics;
56
57   FileOutputStream out;
58
59   BufferedImage bi;
60
61   TYPE type;
62
63   public enum TYPE
64   {
65     EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
66             EPS_DESCRIPTION),
67     PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
68             PNG_DESCRIPTION),
69     SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
70
71     public final String name;
72
73     public final String label;
74
75     public final String extension;
76
77     public final String description;
78
79     TYPE(String name, String label, String ext, String desc)
80     {
81       this.name = name;
82       this.label = label;
83       this.extension = ext;
84       this.description = desc;
85     }
86
87     public String getName()
88     {
89       return name;
90     }
91
92     public JalviewFileChooser getFileChooser()
93     {
94       return new JalviewFileChooser(extension, description);
95     }
96
97     public String getLabel()
98     {
99       return label;
100     }
101
102   }
103
104   /**
105    * Constructor configures the graphics context ready for writing to
106    * 
107    * @param imageType
108    * @param width
109    * @param height
110    * @param file
111    * @param fileTitle
112    * @param useLineart
113    * @throws IOException
114    */
115   public ImageMaker(TYPE imageType, int width, int height,
116           File file, String fileTitle, boolean useLineart)
117           throws IOException
118   {
119     this.type = imageType;
120
121     out = new FileOutputStream(file);
122     switch (imageType)
123     {
124     case SVG:
125       setupSVG(width, height, useLineart);
126       break;
127     case EPS:
128       setupEPS(width, height, fileTitle, useLineart);
129       break;
130     case PNG:
131       setupPNG(width, height);
132       break;
133     default:
134     }
135   }
136
137   public Graphics getGraphics()
138   {
139     return graphics;
140   }
141
142   /**
143    * For SVG or PNG, writes the generated graphics data to the file output
144    * stream. For EPS, flushes the output graphics (which is written to file as
145    * it is generated).
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_EXTENSION, out);
165         out.flush();
166         out.close();
167         break;
168       }
169     } catch (Exception ex)
170     {
171       ex.printStackTrace();
172     }
173   }
174
175   /**
176    * Sets up a graphics object for the PNG image to be written on
177    * 
178    * @param width
179    * @param height
180    */
181   protected void setupPNG(int width, int height)
182   {
183     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
184     graphics = bi.getGraphics();
185     Graphics2D ig2 = (Graphics2D) graphics;
186     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
187             RenderingHints.VALUE_ANTIALIAS_ON);
188   }
189
190   /**
191    * A helper method to configure the SVG output graphics, with choice of Text
192    * or Lineart character rendering
193    * 
194    * @param width
195    * @param height
196    * @param useLineart
197    *          true for Lineart character rendering, false for Text
198    */
199   protected void setupSVG(int width, int height, boolean useLineart)
200   {
201     SVGGraphics2D g2 = new SVGGraphics2D(width, height);
202     if (useLineart)
203     {
204       g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
205               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
206     }
207     graphics = g2;
208   }
209
210   /**
211    * A helper method that sets up the EPS graphics output with user choice of
212    * Text or Lineart character rendering
213    * 
214    * @param width
215    * @param height
216    * @param title
217    * @param useLineart
218    *          true for Lineart character rendering, false for Text
219    * @throws IOException
220    */
221   protected void setupEPS(int width, int height, String title,
222           boolean useLineart) throws IOException
223   {
224     pg = new EpsGraphics2D(title, out, 0, 0, width, height);
225     Graphics2D ig2 = pg;
226     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
227             RenderingHints.VALUE_ANTIALIAS_ON);
228     pg.setAccurateTextMode(useLineart);
229     graphics = pg;
230   }
231 }