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.
21 package org.jibble.epsgraphics;
23 import java.io.BufferedWriter;
24 import java.io.IOException;
25 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.io.StringWriter;
28 import java.io.Writer;
29 import java.util.Date;
32 * This represents an EPS document. Several EpsGraphics2D objects may point to
33 * the same EpsDocument.
35 * Copyright Paul Mutton,
36 * <a href="http://www.jibble.org/">http://www.jibble.org/</a>
39 public class EpsDocument
43 * Constructs an empty EpsDevice.
45 public EpsDocument(String title)
48 minX = Float.POSITIVE_INFINITY;
49 minY = Float.POSITIVE_INFINITY;
50 maxX = Float.NEGATIVE_INFINITY;
51 maxY = Float.NEGATIVE_INFINITY;
52 _stringWriter = new StringWriter();
53 _bufferedWriter = new BufferedWriter(_stringWriter);
57 * Constructs an empty EpsDevice that writes directly to a file. Bounds must
60 public EpsDocument(String title, OutputStream outputStream, int minX,
61 int minY, int maxX, int maxY) throws IOException
68 _bufferedWriter = new BufferedWriter(
69 new OutputStreamWriter(outputStream));
70 write(_bufferedWriter);
74 * Returns the title of the EPS document.
76 public synchronized String getTitle()
82 * Updates the bounds of the current EPS document.
84 public synchronized void updateBounds(double x, double y)
105 * Appends a line to the EpsDocument. A new line character is added to the end
106 * of the line when it is added.
108 public synchronized void append(EpsGraphics2D g, String line)
114 else if (g != _lastG)
116 EpsGraphics2D lastG = _lastG;
118 // We are being drawn on with a different EpsGraphics2D context.
119 // We may need to update the clip, etc from this new context.
120 if (g.getClip() != lastG.getClip())
122 g.setClip(g.getClip());
124 if (!g.getColor().equals(lastG.getColor()))
126 g.setColor(g.getColor());
128 if (!g.getBackground().equals(lastG.getBackground()))
130 g.setBackground(g.getBackground());
132 // We don't need this, as this only affects the stroke and font,
133 // which are dealt with separately later on.
134 // if (!g.getTransform().equals(lastG.getTransform())) {
135 // g.setTransform(g.getTransform());
137 if (!g.getPaint().equals(lastG.getPaint()))
139 g.setPaint(g.getPaint());
141 if (!g.getComposite().equals(lastG.getComposite()))
143 g.setComposite(g.getComposite());
145 if (!g.getComposite().equals(lastG.getComposite()))
147 g.setComposite(g.getComposite());
149 if (!g.getFont().equals(lastG.getFont()))
151 g.setFont(g.getFont());
153 if (!g.getStroke().equals(lastG.getStroke()))
155 g.setStroke(g.getStroke());
162 _bufferedWriter.write(line + "\n");
163 } catch (IOException e)
165 throw new EpsException("Could not write to the output file: " + e);
170 * Outputs the contents of the EPS document to the specified Writer, complete
171 * with headers and bounding box.
173 public synchronized void write(Writer writer) throws IOException
175 float offsetX = -minX;
176 float offsetY = -minY;
178 writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");
179 writer.write("%%Creator: Jalview "
180 + jalview.bin.Cache.getProperty("VERSION") + " \n");
181 writer.write("%%Title: " + _title + "\n");
182 writer.write("%%CreationDate: " + new Date() + "\n");
183 writer.write("%%BoundingBox: 0 0 " + ((int) Math.ceil(maxX + offsetX))
184 + " " + ((int) Math.ceil(maxY + offsetY)) + "\n");
185 writer.write("%%DocumentData: Clean7Bit\n");
186 writer.write("%%DocumentProcessColors: Black\n");
187 writer.write("%%ColorUsage: Color\n");
188 writer.write("%%Origin: 0 0\n");
189 writer.write("%%Pages: 1\n");
190 writer.write("%%Page: 1 1\n");
191 writer.write("%%EndComments\n\n");
193 writer.write("gsave\n");
195 if (_stringWriter != null)
197 writer.write(offsetX + " " + (offsetY) + " translate\n");
199 _bufferedWriter.flush();
200 StringBuffer buffer = _stringWriter.getBuffer();
201 for (int i = 0; i < buffer.length(); i++)
203 writer.write(buffer.charAt(i));
211 offsetX + " " + ((maxY - minY) - offsetY) + " translate\n");
217 private void writeFooter(Writer writer) throws IOException
219 writer.write("grestore\n");
222 writer.write("grestore\n");
224 writer.write("showpage\n");
226 writer.write("%%EOF");
230 public synchronized void flush() throws IOException
232 _bufferedWriter.flush();
235 public synchronized void close() throws IOException
237 if (_stringWriter == null)
239 writeFooter(_bufferedWriter);
240 _bufferedWriter.flush();
241 _bufferedWriter.close();
245 public boolean isClipSet()
250 public void setClipSet(boolean isClipSet)
252 _isClipSet = isClipSet;
263 private boolean _isClipSet = false;
265 private String _title;
267 private StringWriter _stringWriter;
269 private BufferedWriter _bufferedWriter = null;
271 // We need to remember which was the last EpsGraphics2D object to use
272 // us, as we need to replace the clipping region if another EpsGraphics2D
273 // object tries to use us.
274 private EpsGraphics2D _lastG = null;