2 Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/
4 This file is part of EpsGraphics2D.
6 This software is dual-licensed, allowing you to choose between the GNU
7 General Public License (GPL) and the www.jibble.org Commercial License.
8 Since the GPL may be too restrictive for use in a proprietary application,
9 a commercial license is also provided. Full license information can be
10 found at http://www.jibble.org/licenses/
17 package org.jibble.epsgraphics;
23 * This represents an EPS document. Several EpsGraphics2D objects may point to
24 * the same EpsDocument.
26 * Copyright Paul Mutton, <a
27 * href="http://www.jibble.org/">http://www.jibble.org/</a>
30 public class EpsDocument
34 * Constructs an empty EpsDevice.
36 public EpsDocument(String title)
39 minX = Float.POSITIVE_INFINITY;
40 minY = Float.POSITIVE_INFINITY;
41 maxX = Float.NEGATIVE_INFINITY;
42 maxY = Float.NEGATIVE_INFINITY;
43 _stringWriter = new StringWriter();
44 _bufferedWriter = new BufferedWriter(_stringWriter);
48 * Constructs an empty EpsDevice that writes directly to a file. Bounds must
51 public EpsDocument(String title, OutputStream outputStream, int minX,
52 int minY, int maxX, int maxY) throws IOException
59 _bufferedWriter = new BufferedWriter(new OutputStreamWriter(
61 write(_bufferedWriter);
65 * Returns the title of the EPS document.
67 public synchronized String getTitle()
73 * Updates the bounds of the current EPS document.
75 public synchronized void updateBounds(double x, double y)
96 * Appends a line to the EpsDocument. A new line character is added to the end
97 * of the line when it is added.
99 public synchronized void append(EpsGraphics2D g, String line)
105 else if (g != _lastG)
107 EpsGraphics2D lastG = _lastG;
109 // We are being drawn on with a different EpsGraphics2D context.
110 // We may need to update the clip, etc from this new context.
111 if (g.getClip() != lastG.getClip())
113 g.setClip(g.getClip());
115 if (!g.getColor().equals(lastG.getColor()))
117 g.setColor(g.getColor());
119 if (!g.getBackground().equals(lastG.getBackground()))
121 g.setBackground(g.getBackground());
123 // We don't need this, as this only affects the stroke and font,
124 // which are dealt with separately later on.
125 // if (!g.getTransform().equals(lastG.getTransform())) {
126 // g.setTransform(g.getTransform());
128 if (!g.getPaint().equals(lastG.getPaint()))
130 g.setPaint(g.getPaint());
132 if (!g.getComposite().equals(lastG.getComposite()))
134 g.setComposite(g.getComposite());
136 if (!g.getComposite().equals(lastG.getComposite()))
138 g.setComposite(g.getComposite());
140 if (!g.getFont().equals(lastG.getFont()))
142 g.setFont(g.getFont());
144 if (!g.getStroke().equals(lastG.getStroke()))
146 g.setStroke(g.getStroke());
153 _bufferedWriter.write(line + "\n");
154 } catch (IOException e)
156 throw new EpsException("Could not write to the output file: " + e);
161 * Outputs the contents of the EPS document to the specified Writer, complete
162 * with headers and bounding box.
164 public synchronized void write(Writer writer) throws IOException
166 float offsetX = -minX;
167 float offsetY = -minY;
169 writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");
170 writer.write("%%Creator: Jalview "
171 + jalview.bin.Cache.getProperty("VERSION") + " \n");
172 writer.write("%%Title: " + _title + "\n");
173 writer.write("%%CreationDate: " + new Date() + "\n");
174 writer.write("%%BoundingBox: 0 0 " + ((int) Math.ceil(maxX + offsetX))
175 + " " + ((int) Math.ceil(maxY + offsetY)) + "\n");
176 writer.write("%%DocumentData: Clean7Bit\n");
177 writer.write("%%DocumentProcessColors: Black\n");
178 writer.write("%%ColorUsage: Color\n");
179 writer.write("%%Origin: 0 0\n");
180 writer.write("%%Pages: 1\n");
181 writer.write("%%Page: 1 1\n");
182 writer.write("%%EndComments\n\n");
184 writer.write("gsave\n");
186 if (_stringWriter != null)
188 writer.write(offsetX + " " + (offsetY) + " translate\n");
190 _bufferedWriter.flush();
191 StringBuffer buffer = _stringWriter.getBuffer();
192 for (int i = 0; i < buffer.length(); i++)
194 writer.write(buffer.charAt(i));
201 writer.write(offsetX + " " + ((maxY - minY) - offsetY)
208 private void writeFooter(Writer writer) throws IOException
210 writer.write("grestore\n");
213 writer.write("grestore\n");
215 writer.write("showpage\n");
217 writer.write("%%EOF");
221 public synchronized void flush() throws IOException
223 _bufferedWriter.flush();
226 public synchronized void close() throws IOException
228 if (_stringWriter == null)
230 writeFooter(_bufferedWriter);
231 _bufferedWriter.flush();
232 _bufferedWriter.close();
236 public boolean isClipSet()
241 public void setClipSet(boolean isClipSet)
243 _isClipSet = isClipSet;
254 private boolean _isClipSet = false;
256 private String _title;
258 private StringWriter _stringWriter;
260 private BufferedWriter _bufferedWriter = null;
262 // We need to remember which was the last EpsGraphics2D object to use
263 // us, as we need to replace the clipping region if another EpsGraphics2D
264 // object tries to use us.
265 private EpsGraphics2D _lastG = null;