Formatting
[jalview.git] / src / org / jibble / epsgraphics / EpsDocument.java
index 486fc31..a53df85 100755 (executable)
@@ -1,24 +1,23 @@
 /*\r
-Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/\r
+ Copyright Paul James Mutton, 2001-2004, http://www.jibble.org/\r
 \r
-This file is part of EpsGraphics2D.\r
+ This file is part of EpsGraphics2D.\r
 \r
-This software is dual-licensed, allowing you to choose between the GNU\r
-General Public License (GPL) and the www.jibble.org Commercial License.\r
-Since the GPL may be too restrictive for use in a proprietary application,\r
-a commercial license is also provided. Full license information can be\r
-found at http://www.jibble.org/licenses/\r
+ This software is dual-licensed, allowing you to choose between the GNU\r
+ General Public License (GPL) and the www.jibble.org Commercial License.\r
+ Since the GPL may be too restrictive for use in a proprietary application,\r
+ a commercial license is also provided. Full license information can be\r
+ found at http://www.jibble.org/licenses/\r
 \r
-$Author$\r
-$Id$\r
+ $Author$\r
+ $Id$\r
 \r
-*/\r
+ */\r
 \r
 package org.jibble.epsgraphics;\r
 \r
-import java.util.*;\r
 import java.io.*;\r
-\r
+import java.util.*;\r
 \r
 /**\r
  * This represents an EPS document. Several EpsGraphics2D objects may point\r
@@ -28,208 +27,240 @@ import java.io.*;
  *           <a href="http://www.jibble.org/">http://www.jibble.org/</a>\r
  *\r
  */\r
-public class EpsDocument {\r
-\r
-\r
-    /**\r
-     * Constructs an empty EpsDevice.\r
-     */\r
-    public EpsDocument(String title) {\r
-        _title = title;\r
-        minX = Float.POSITIVE_INFINITY;\r
-        minY = Float.POSITIVE_INFINITY;\r
-        maxX = Float.NEGATIVE_INFINITY;\r
-        maxY = Float.NEGATIVE_INFINITY;\r
-        _stringWriter = new StringWriter();\r
-        _bufferedWriter = new BufferedWriter(_stringWriter);\r
+public class EpsDocument\r
+{\r
+\r
+  /**\r
+   * Constructs an empty EpsDevice.\r
+   */\r
+  public EpsDocument(String title)\r
+  {\r
+    _title = title;\r
+    minX = Float.POSITIVE_INFINITY;\r
+    minY = Float.POSITIVE_INFINITY;\r
+    maxX = Float.NEGATIVE_INFINITY;\r
+    maxY = Float.NEGATIVE_INFINITY;\r
+    _stringWriter = new StringWriter();\r
+    _bufferedWriter = new BufferedWriter(_stringWriter);\r
+  }\r
+\r
+  /**\r
+   * Constructs an empty EpsDevice that writes directly to a file.\r
+   * Bounds must be set before use.\r
+   */\r
+  public EpsDocument(String title, OutputStream outputStream, int minX,\r
+                     int minY, int maxX, int maxY)\r
+      throws IOException\r
+  {\r
+    _title = title;\r
+    this.minX = minX;\r
+    this.minY = minY;\r
+    this.maxX = maxX;\r
+    this.maxY = maxY;\r
+    _bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));\r
+    write(_bufferedWriter);\r
+  }\r
+\r
+  /**\r
+   * Returns the title of the EPS document.\r
+   */\r
+  public synchronized String getTitle()\r
+  {\r
+    return _title;\r
+  }\r
+\r
+  /**\r
+   * Updates the bounds of the current EPS document.\r
+   */\r
+  public synchronized void updateBounds(double x, double y)\r
+  {\r
+    if (x > maxX)\r
+    {\r
+      maxX = (float) x;\r
     }\r
-\r
-    /**\r
-     * Constructs an empty EpsDevice that writes directly to a file.\r
-     * Bounds must be set before use.\r
-     */\r
-    public EpsDocument(String title, OutputStream outputStream, int minX, int minY, int maxX, int maxY) throws IOException {\r
-        _title = title;\r
-        this.minX = minX;\r
-        this.minY = minY;\r
-        this.maxX = maxX;\r
-        this.maxY = maxY;\r
-        _bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));\r
-        write(_bufferedWriter);\r
+    if (x < minX)\r
+    {\r
+      minX = (float) x;\r
     }\r
-\r
-\r
-    /**\r
-     * Returns the title of the EPS document.\r
-     */\r
-    public synchronized String getTitle() {\r
-        return _title;\r
+    if (y > maxY)\r
+    {\r
+      maxY = (float) y;\r
     }\r
-\r
-\r
-    /**\r
-     * Updates the bounds of the current EPS document.\r
-     */\r
-    public synchronized void updateBounds(double x, double y) {\r
-        if (x > maxX) {\r
-            maxX = (float) x;\r
-        }\r
-        if (x < minX) {\r
-            minX = (float) x;\r
-        }\r
-        if (y > maxY) {\r
-            maxY = (float) y;\r
-        }\r
-        if (y < minY) {\r
-            minY = (float) y;\r
-        }\r
+    if (y < minY)\r
+    {\r
+      minY = (float) y;\r
     }\r
-\r
-\r
-    /**\r
-     * Appends a line to the EpsDocument.  A new line character is added\r
-     * to the end of the line when it is added.\r
-     */\r
-    public synchronized void append(EpsGraphics2D g, String line) {\r
-        if (_lastG == null) {\r
-            _lastG = g;\r
-        }\r
-        else if (g != _lastG) {\r
-            EpsGraphics2D lastG = _lastG;\r
-            _lastG = g;\r
-            // We are being drawn on with a different EpsGraphics2D context.\r
-            // We may need to update the clip, etc from this new context.\r
-            if (g.getClip() != lastG.getClip()) {\r
-                g.setClip(g.getClip());\r
-            }\r
-            if (!g.getColor().equals(lastG.getColor())) {\r
-                g.setColor(g.getColor());\r
-            }\r
-            if (!g.getBackground().equals(lastG.getBackground())) {\r
-                g.setBackground(g.getBackground());\r
-            }\r
-            // We don't need this, as this only affects the stroke and font,\r
-            // which are dealt with separately later on.\r
-            //if (!g.getTransform().equals(lastG.getTransform())) {\r
-            //    g.setTransform(g.getTransform());\r
-            //}\r
-            if (!g.getPaint().equals(lastG.getPaint())) {\r
-                g.setPaint(g.getPaint());\r
-            }\r
-            if (!g.getComposite().equals(lastG.getComposite())) {\r
-                g.setComposite(g.getComposite());\r
-            }\r
-            if (!g.getComposite().equals(lastG.getComposite())) {\r
-                g.setComposite(g.getComposite());\r
-            }\r
-            if (!g.getFont().equals(lastG.getFont())) {\r
-                g.setFont(g.getFont());\r
-            }\r
-            if (!g.getStroke().equals(lastG.getStroke())) {\r
-                g.setStroke(g.getStroke());\r
-            }\r
-        }\r
-        _lastG = g;\r
-\r
-        try {\r
-            _bufferedWriter.write(line + "\n");\r
-        }\r
-        catch (IOException e) {\r
-            throw new EpsException("Could not write to the output file: " + e);\r
-        }\r
+  }\r
+\r
+  /**\r
+   * Appends a line to the EpsDocument.  A new line character is added\r
+   * to the end of the line when it is added.\r
+   */\r
+  public synchronized void append(EpsGraphics2D g, String line)\r
+  {\r
+    if (_lastG == null)\r
+    {\r
+      _lastG = g;\r
     }\r
-\r
-\r
-    /**\r
-     * Outputs the contents of the EPS document to the specified\r
-     * Writer, complete with headers and bounding box.\r
-     */\r
-    public synchronized void write(Writer writer) throws IOException {\r
-        float offsetX = -minX;\r
-        float offsetY = -minY;\r
-\r
-        writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");\r
-        writer.write("%%Creator: Jalview " + jalview.bin.Cache.getProperty("VERSION") + " \n");\r
-        writer.write("%%Title: " + _title + "\n");\r
-        writer.write("%%CreationDate: " + new Date() + "\n");\r
-        writer.write("%%BoundingBox: 0 0 " + ((int) Math.ceil(maxX + offsetX)) + " " + ((int) Math.ceil(maxY + offsetY)) + "\n");\r
-        writer.write("%%DocumentData: Clean7Bit\n");\r
-        writer.write("%%DocumentProcessColors: Black\n");\r
-        writer.write("%%ColorUsage: Color\n");\r
-        writer.write("%%Origin: 0 0\n");\r
-        writer.write("%%Pages: 1\n");\r
-        writer.write("%%Page: 1 1\n");\r
-        writer.write("%%EndComments\n\n");\r
-\r
-        writer.write("gsave\n");\r
-\r
-        if (_stringWriter != null) {\r
-            writer.write(offsetX + " " + (offsetY) + " translate\n");\r
-\r
-            _bufferedWriter.flush();\r
-            StringBuffer buffer = _stringWriter.getBuffer();\r
-            for (int i = 0; i < buffer.length(); i++) {\r
-                writer.write(buffer.charAt(i));\r
-            }\r
-\r
-            writeFooter(writer);\r
-        }\r
-        else {\r
-            writer.write(offsetX + " " + ((maxY - minY) - offsetY) + " translate\n");\r
-        }\r
-\r
-        writer.flush();\r
+    else if (g != _lastG)\r
+    {\r
+      EpsGraphics2D lastG = _lastG;\r
+      _lastG = g;\r
+      // We are being drawn on with a different EpsGraphics2D context.\r
+      // We may need to update the clip, etc from this new context.\r
+      if (g.getClip() != lastG.getClip())\r
+      {\r
+        g.setClip(g.getClip());\r
+      }\r
+      if (!g.getColor().equals(lastG.getColor()))\r
+      {\r
+        g.setColor(g.getColor());\r
+      }\r
+      if (!g.getBackground().equals(lastG.getBackground()))\r
+      {\r
+        g.setBackground(g.getBackground());\r
+      }\r
+      // We don't need this, as this only affects the stroke and font,\r
+      // which are dealt with separately later on.\r
+      //if (!g.getTransform().equals(lastG.getTransform())) {\r
+      //    g.setTransform(g.getTransform());\r
+      //}\r
+      if (!g.getPaint().equals(lastG.getPaint()))\r
+      {\r
+        g.setPaint(g.getPaint());\r
+      }\r
+      if (!g.getComposite().equals(lastG.getComposite()))\r
+      {\r
+        g.setComposite(g.getComposite());\r
+      }\r
+      if (!g.getComposite().equals(lastG.getComposite()))\r
+      {\r
+        g.setComposite(g.getComposite());\r
+      }\r
+      if (!g.getFont().equals(lastG.getFont()))\r
+      {\r
+        g.setFont(g.getFont());\r
+      }\r
+      if (!g.getStroke().equals(lastG.getStroke()))\r
+      {\r
+        g.setStroke(g.getStroke());\r
+      }\r
     }\r
+    _lastG = g;\r
 \r
-\r
-    private void writeFooter(Writer writer) throws IOException {\r
-        writer.write("grestore\n");\r
-        if (isClipSet()) {\r
-            writer.write("grestore\n");\r
-        }\r
-        writer.write("showpage\n");\r
-        writer.write("\n");\r
-        writer.write("%%EOF");\r
-        writer.flush();\r
+    try\r
+    {\r
+      _bufferedWriter.write(line + "\n");\r
     }\r
-\r
-\r
-    public synchronized void flush() throws IOException {\r
-        _bufferedWriter.flush();\r
+    catch (IOException e)\r
+    {\r
+      throw new EpsException("Could not write to the output file: " + e);\r
     }\r
-\r
-    public synchronized void close() throws IOException {\r
-        if (_stringWriter == null) {\r
-            writeFooter(_bufferedWriter);\r
-            _bufferedWriter.flush();\r
-            _bufferedWriter.close();\r
-        }\r
+  }\r
+\r
+  /**\r
+   * Outputs the contents of the EPS document to the specified\r
+   * Writer, complete with headers and bounding box.\r
+   */\r
+  public synchronized void write(Writer writer)\r
+      throws IOException\r
+  {\r
+    float offsetX = -minX;\r
+    float offsetY = -minY;\r
+\r
+    writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");\r
+    writer.write("%%Creator: Jalview " +\r
+                 jalview.bin.Cache.getProperty("VERSION") + " \n");\r
+    writer.write("%%Title: " + _title + "\n");\r
+    writer.write("%%CreationDate: " + new Date() + "\n");\r
+    writer.write("%%BoundingBox: 0 0 " + ( (int) Math.ceil(maxX + offsetX)) +\r
+                 " " + ( (int) Math.ceil(maxY + offsetY)) + "\n");\r
+    writer.write("%%DocumentData: Clean7Bit\n");\r
+    writer.write("%%DocumentProcessColors: Black\n");\r
+    writer.write("%%ColorUsage: Color\n");\r
+    writer.write("%%Origin: 0 0\n");\r
+    writer.write("%%Pages: 1\n");\r
+    writer.write("%%Page: 1 1\n");\r
+    writer.write("%%EndComments\n\n");\r
+\r
+    writer.write("gsave\n");\r
+\r
+    if (_stringWriter != null)\r
+    {\r
+      writer.write(offsetX + " " + (offsetY) + " translate\n");\r
+\r
+      _bufferedWriter.flush();\r
+      StringBuffer buffer = _stringWriter.getBuffer();\r
+      for (int i = 0; i < buffer.length(); i++)\r
+      {\r
+        writer.write(buffer.charAt(i));\r
+      }\r
+\r
+      writeFooter(writer);\r
+    }\r
+    else\r
+    {\r
+      writer.write(offsetX + " " + ( (maxY - minY) - offsetY) + " translate\n");\r
     }\r
 \r
+    writer.flush();\r
+  }\r
 \r
-    public boolean isClipSet() {\r
-        return _isClipSet;\r
+  private void writeFooter(Writer writer)\r
+      throws IOException\r
+  {\r
+    writer.write("grestore\n");\r
+    if (isClipSet())\r
+    {\r
+      writer.write("grestore\n");\r
     }\r
-\r
-    public void setClipSet(boolean isClipSet) {\r
-        _isClipSet = isClipSet;\r
+    writer.write("showpage\n");\r
+    writer.write("\n");\r
+    writer.write("%%EOF");\r
+    writer.flush();\r
+  }\r
+\r
+  public synchronized void flush()\r
+      throws IOException\r
+  {\r
+    _bufferedWriter.flush();\r
+  }\r
+\r
+  public synchronized void close()\r
+      throws IOException\r
+  {\r
+    if (_stringWriter == null)\r
+    {\r
+      writeFooter(_bufferedWriter);\r
+      _bufferedWriter.flush();\r
+      _bufferedWriter.close();\r
     }\r
+  }\r
+\r
+  public boolean isClipSet()\r
+  {\r
+    return _isClipSet;\r
+  }\r
 \r
+  public void setClipSet(boolean isClipSet)\r
+  {\r
+    _isClipSet = isClipSet;\r
+  }\r
 \r
-    private float minX;\r
-    private float minY;\r
-    private float maxX;\r
-    private float maxY;\r
+  private float minX;\r
+  private float minY;\r
+  private float maxX;\r
+  private float maxY;\r
 \r
-    private boolean _isClipSet = false;\r
+  private boolean _isClipSet = false;\r
 \r
-    private String _title;\r
-    private StringWriter _stringWriter;\r
-    private BufferedWriter _bufferedWriter = null;\r
+  private String _title;\r
+  private StringWriter _stringWriter;\r
+  private BufferedWriter _bufferedWriter = null;\r
 \r
-    // We need to remember which was the last EpsGraphics2D object to use\r
-    // us, as we need to replace the clipping region if another EpsGraphics2D\r
-    // object tries to use us.\r
-    private EpsGraphics2D _lastG = null;\r
+  // We need to remember which was the last EpsGraphics2D object to use\r
+  // us, as we need to replace the clipping region if another EpsGraphics2D\r
+  // object tries to use us.\r
+  private EpsGraphics2D _lastG = null;\r
 \r
 }\r