2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
21 package org.jibble.epsgraphics;
27 * This represents an EPS document. Several EpsGraphics2D objects may point to
28 * the same EpsDocument.
30 * Copyright Paul Mutton, <a
31 * href="http://www.jibble.org/">http://www.jibble.org/</a>
34 public class EpsDocument
38 * Constructs an empty EpsDevice.
40 public EpsDocument(String title)
43 minX = Float.POSITIVE_INFINITY;
44 minY = Float.POSITIVE_INFINITY;
45 maxX = Float.NEGATIVE_INFINITY;
46 maxY = Float.NEGATIVE_INFINITY;
47 _stringWriter = new StringWriter();
48 _bufferedWriter = new BufferedWriter(_stringWriter);
52 * Constructs an empty EpsDevice that writes directly to a file. Bounds must
55 public EpsDocument(String title, OutputStream outputStream, int minX,
56 int minY, int maxX, int maxY) throws IOException
63 _bufferedWriter = new BufferedWriter(new OutputStreamWriter(
65 write(_bufferedWriter);
69 * Returns the title of the EPS document.
71 public synchronized String getTitle()
77 * Updates the bounds of the current EPS document.
79 public synchronized void updateBounds(double x, double y)
100 * Appends a line to the EpsDocument. A new line character is added to the end
101 * of the line when it is added.
103 public synchronized void append(EpsGraphics2D g, String line)
109 else if (g != _lastG)
111 EpsGraphics2D lastG = _lastG;
113 // We are being drawn on with a different EpsGraphics2D context.
114 // We may need to update the clip, etc from this new context.
115 if (g.getClip() != lastG.getClip())
117 g.setClip(g.getClip());
119 if (!g.getColor().equals(lastG.getColor()))
121 g.setColor(g.getColor());
123 if (!g.getBackground().equals(lastG.getBackground()))
125 g.setBackground(g.getBackground());
127 // We don't need this, as this only affects the stroke and font,
128 // which are dealt with separately later on.
129 // if (!g.getTransform().equals(lastG.getTransform())) {
130 // g.setTransform(g.getTransform());
132 if (!g.getPaint().equals(lastG.getPaint()))
134 g.setPaint(g.getPaint());
136 if (!g.getComposite().equals(lastG.getComposite()))
138 g.setComposite(g.getComposite());
140 if (!g.getComposite().equals(lastG.getComposite()))
142 g.setComposite(g.getComposite());
144 if (!g.getFont().equals(lastG.getFont()))
146 g.setFont(g.getFont());
148 if (!g.getStroke().equals(lastG.getStroke()))
150 g.setStroke(g.getStroke());
157 _bufferedWriter.write(line + "\n");
158 } catch (IOException e)
160 throw new EpsException("Could not write to the output file: " + e);
165 * Outputs the contents of the EPS document to the specified Writer, complete
166 * with headers and bounding box.
168 public synchronized void write(Writer writer) throws IOException
170 float offsetX = -minX;
171 float offsetY = -minY;
173 writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");
174 writer.write("%%Creator: Jalview "
175 + jalview.bin.Cache.getProperty("VERSION") + " \n");
176 writer.write("%%Title: " + _title + "\n");
177 writer.write("%%CreationDate: " + new Date() + "\n");
178 writer.write("%%BoundingBox: 0 0 " + ((int) Math.ceil(maxX + offsetX))
179 + " " + ((int) Math.ceil(maxY + offsetY)) + "\n");
180 writer.write("%%DocumentData: Clean7Bit\n");
181 writer.write("%%DocumentProcessColors: Black\n");
182 writer.write("%%ColorUsage: Color\n");
183 writer.write("%%Origin: 0 0\n");
184 writer.write("%%Pages: 1\n");
185 writer.write("%%Page: 1 1\n");
186 writer.write("%%EndComments\n\n");
188 writer.write("gsave\n");
190 if (_stringWriter != null)
192 writer.write(offsetX + " " + (offsetY) + " translate\n");
194 _bufferedWriter.flush();
195 StringBuffer buffer = _stringWriter.getBuffer();
196 for (int i = 0; i < buffer.length(); i++)
198 writer.write(buffer.charAt(i));
205 writer.write(offsetX + " " + ((maxY - minY) - offsetY)
212 private void writeFooter(Writer writer) throws IOException
214 writer.write("grestore\n");
217 writer.write("grestore\n");
219 writer.write("showpage\n");
221 writer.write("%%EOF");
225 public synchronized void flush() throws IOException
227 _bufferedWriter.flush();
230 public synchronized void close() throws IOException
232 if (_stringWriter == null)
234 writeFooter(_bufferedWriter);
235 _bufferedWriter.flush();
236 _bufferedWriter.close();
240 public boolean isClipSet()
245 public void setClipSet(boolean isClipSet)
247 _isClipSet = isClipSet;
258 private boolean _isClipSet = false;
260 private String _title;
262 private StringWriter _stringWriter;
264 private BufferedWriter _bufferedWriter = null;
266 // We need to remember which was the last EpsGraphics2D object to use
267 // us, as we need to replace the clipping region if another EpsGraphics2D
268 // object tries to use us.
269 private EpsGraphics2D _lastG = null;