2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.RenderingHints;
26 import java.awt.image.BufferedImage;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
31 import javax.imageio.ImageIO;
33 import org.jfree.graphics2d.svg.SVGGraphics2D;
34 import org.jfree.graphics2d.svg.SVGHints;
35 import org.jibble.epsgraphics.EpsGraphics2D;
37 import jalview.io.JalviewFileChooser;
39 public class ImageMaker
41 public static final String SVG_DESCRIPTION = "Scalable Vector Graphics";
43 public static final String SVG_EXTENSION = "svg";
45 public static final String EPS_DESCRIPTION = "Encapsulated Postscript";
47 public static final String EPS_EXTENSION = "eps";
49 public static final String PNG_EXTENSION = "png";
51 public static final String PNG_DESCRIPTION = "Portable network graphics";
65 EPS("EPS", MessageManager.getString("label.eps_file"), EPS_EXTENSION,
67 PNG("PNG", MessageManager.getString("label.png_image"), PNG_EXTENSION,
69 SVG("SVG", "SVG", SVG_EXTENSION, SVG_DESCRIPTION);
71 public final String name;
73 public final String label;
75 public final String extension;
77 public final String description;
79 TYPE(String name, String label, String ext, String desc)
84 this.description = desc;
87 public String getName()
92 public JalviewFileChooser getFileChooser()
94 return new JalviewFileChooser(extension, description);
97 public String getLabel()
105 * Constructor configures the graphics context ready for writing to
114 * @throws IOException
116 public ImageMaker(TYPE imageType, int width, int height, File file,
117 String fileTitle, boolean useLineart, float bitmapscale,
118 int bitmapwidth, int bitmapheight) throws IOException
120 this.type = imageType;
122 out = new FileOutputStream(file);
126 setupSVG(width, height, useLineart);
129 setupEPS(width, height, fileTitle, useLineart);
132 setupPNG(width, height, bitmapscale, bitmapwidth, bitmapheight);
138 public Graphics getGraphics()
144 * For SVG or PNG, writes the generated graphics data to the file output
145 * stream. For EPS, flushes the output graphics (which is written to file as
148 public void writeImage()
159 String svgData = ((SVGGraphics2D) getGraphics()).getSVGDocument();
160 out.write(svgData.getBytes());
165 ImageIO.write(bi, PNG_EXTENSION, out);
170 } catch (Exception ex)
172 ex.printStackTrace();
177 * Sets up a graphics object for the PNG image to be written on
183 protected void setupPNG(int width, int height, float scale,
184 int bitmapwidth, int bitmapheight)
186 if (width == 0 || height == 0)
189 float usescale = 0.0f;
190 int usewidth = width;
191 int useheight = height;
193 // use the smallest positive scale (i.e. fit in the box)
197 usewidth = Math.round(scale * width);
198 useheight = Math.round(scale * height);
202 float wscale = (float) bitmapwidth / width;
203 if (wscale > 0.0f && (usescale == 0.0f || wscale < usescale))
206 usewidth = bitmapwidth;
207 useheight = Math.round(usescale * height);
210 if (bitmapheight > 0)
212 float hscale = (float) bitmapheight / height;
213 if (hscale > 0.0f && (usescale == 0.0f || hscale < usescale))
216 usewidth = Math.round(usescale * width);
217 useheight = bitmapheight;
220 bi = new BufferedImage(usewidth, useheight, BufferedImage.TYPE_INT_RGB);
221 graphics = bi.getGraphics();
222 Graphics2D ig2 = (Graphics2D) graphics;
223 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
224 RenderingHints.VALUE_ANTIALIAS_ON);
226 ig2.scale(usescale, usescale);
230 * A helper method to configure the SVG output graphics, with choice of Text
231 * or Lineart character rendering
236 * true for Lineart character rendering, false for Text
238 protected void setupSVG(int width, int height, boolean useLineart)
240 SVGGraphics2D g2 = new SVGGraphics2D(width, height);
243 g2.setRenderingHint(SVGHints.KEY_DRAW_STRING_TYPE,
244 SVGHints.VALUE_DRAW_STRING_TYPE_VECTOR);
250 * A helper method that sets up the EPS graphics output with user choice of
251 * Text or Lineart character rendering
257 * true for Lineart character rendering, false for Text
258 * @throws IOException
260 protected void setupEPS(int width, int height, String title,
261 boolean useLineart) throws IOException
263 pg = new EpsGraphics2D(title, out, 0, 0, width, height);
265 ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
266 RenderingHints.VALUE_ANTIALIAS_ON);
267 pg.setAccurateTextMode(useLineart);