JAL-3438 spotless for 2.11.2.0
[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, File file,
116           String fileTitle, boolean useLineart) throws IOException
117   {
118     this.type = imageType;
119
120     out = new FileOutputStream(file);
121     switch (imageType)
122     {
123     case SVG:
124       setupSVG(width, height, useLineart);
125       break;
126     case EPS:
127       setupEPS(width, height, fileTitle, useLineart);
128       break;
129     case PNG:
130       setupPNG(width, height);
131       break;
132     default:
133     }
134   }
135
136   public Graphics getGraphics()
137   {
138     return graphics;
139   }
140
141   /**
142    * For SVG or PNG, writes the generated graphics data to the file output
143    * stream. For EPS, flushes the output graphics (which is written to file as
144    * it is generated).
145    */
146   public void writeImage()
147   {
148     try
149     {
150       switch (type)
151       {
152       case EPS:
153         pg.flush();
154         pg.close();
155         break;
156       case SVG:
157         String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
158         out.write(svgData.getBytes());
159         out.flush();
160         out.close();
161         break;
162       case PNG:
163         ImageIO.write(bi, PNG_EXTENSION, out);
164         out.flush();
165         out.close();
166         break;
167       }
168     } catch (Exception ex)
169     {
170       ex.printStackTrace();
171     }
172   }
173
174   /**
175    * Sets up a graphics object for the PNG image to be written on
176    * 
177    * @param width
178    * @param height
179    */
180   protected void setupPNG(int width, int height)
181   {
182     bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
183     graphics = bi.getGraphics();
184     Graphics2D ig2 = (Graphics2D) graphics;
185     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
186             RenderingHints.VALUE_ANTIALIAS_ON);
187   }
188
189   /**
190    * A helper method to configure the SVG output graphics, with choice of Text
191    * or Lineart character rendering
192    * 
193    * @param width
194    * @param height
195    * @param useLineart
196    *          true for Lineart character rendering, false for Text
197    */
198   protected void setupSVG(int width, int height, boolean useLineart)
199   {
200     SVGGraphics2D g2 = new SVGGraphics2D(width, height);
201     if (useLineart)
202     {
203       g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
204               SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
205     }
206     graphics = g2;
207   }
208
209   /**
210    * A helper method that sets up the EPS graphics output with user choice of
211    * Text or Lineart character rendering
212    * 
213    * @param width
214    * @param height
215    * @param title
216    * @param useLineart
217    *          true for Lineart character rendering, false for Text
218    * @throws IOException
219    */
220   protected void setupEPS(int width, int height, String title,
221           boolean useLineart) throws IOException
222   {
223     pg = new EpsGraphics2D(title, out, 0, 0, width, height);
224     Graphics2D ig2 = pg;
225     ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
226             RenderingHints.VALUE_ANTIALIAS_ON);
227     pg.setAccurateTextMode(useLineart);
228     graphics = pg;
229   }
230 }