Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package org.jibble.epsgraphics

File EpsDocument.java

 

Coverage histogram

../../../img/srcFileCovDistChart6.png
33% of files have more coverage

Code metrics

36
86
11
1
276
187
30
0.35
7.82
11
2.73

Classes

Class Line # Actions
EpsDocument 39 86 30
0.5939849659.4%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package org.jibble.epsgraphics;
22   
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;
30   
31    /**
32    * This represents an EPS document. Several EpsGraphics2D objects may point to
33    * the same EpsDocument.
34    * <p>
35    * Copyright Paul Mutton, <a
36    * href="http://www.jibble.org/">http://www.jibble.org/</a>
37    *
38    */
 
39    public class EpsDocument
40    {
41   
42    /**
43    * Constructs an empty EpsDevice.
44    */
 
45  5 toggle public EpsDocument(String title)
46    {
47  5 _title = title;
48  5 minX = Float.POSITIVE_INFINITY;
49  5 minY = Float.POSITIVE_INFINITY;
50  5 maxX = Float.NEGATIVE_INFINITY;
51  5 maxY = Float.NEGATIVE_INFINITY;
52  5 _stringWriter = new StringWriter();
53  5 _bufferedWriter = new BufferedWriter(_stringWriter);
54    }
55   
56    /**
57    * Constructs an empty EpsDevice that writes directly to a file. Bounds must
58    * be set before use.
59    */
 
60  5 toggle public EpsDocument(String title, OutputStream outputStream, int minX,
61    int minY, int maxX, int maxY) throws IOException
62    {
63  5 _title = title;
64  5 this.minX = minX;
65  5 this.minY = minY;
66  5 this.maxX = maxX;
67  5 this.maxY = maxY;
68  5 _bufferedWriter = new BufferedWriter(new OutputStreamWriter(
69    outputStream));
70  5 write(_bufferedWriter);
71    }
72   
73    /**
74    * Returns the title of the EPS document.
75    */
 
76  0 toggle public synchronized String getTitle()
77    {
78  0 return _title;
79    }
80   
81    /**
82    * Updates the bounds of the current EPS document.
83    */
 
84  6960 toggle public synchronized void updateBounds(double x, double y)
85    {
86  6960 if (x > maxX)
87    {
88  5 maxX = (float) x;
89    }
90  6960 if (x < minX)
91    {
92  5 minX = (float) x;
93    }
94  6960 if (y > maxY)
95    {
96  0 maxY = (float) y;
97    }
98  6960 if (y < minY)
99    {
100  5 minY = (float) y;
101    }
102    }
103   
104    /**
105    * Appends a line to the EpsDocument. A new line character is added to the end
106    * of the line when it is added.
107    */
 
108  119825 toggle public synchronized void append(EpsGraphics2D g, String line)
109    {
110  119825 if (_lastG == null)
111    {
112  10 _lastG = g;
113    }
114  119815 else if (g != _lastG)
115    {
116  0 EpsGraphics2D lastG = _lastG;
117  0 _lastG = g;
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  0 if (g.getClip() != lastG.getClip())
121    {
122  0 g.setClip(g.getClip());
123    }
124  0 if (!g.getColor().equals(lastG.getColor()))
125    {
126  0 g.setColor(g.getColor());
127    }
128  0 if (!g.getBackground().equals(lastG.getBackground()))
129    {
130  0 g.setBackground(g.getBackground());
131    }
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());
136    // }
137  0 if (!g.getPaint().equals(lastG.getPaint()))
138    {
139  0 g.setPaint(g.getPaint());
140    }
141  0 if (!g.getComposite().equals(lastG.getComposite()))
142    {
143  0 g.setComposite(g.getComposite());
144    }
145  0 if (!g.getComposite().equals(lastG.getComposite()))
146    {
147  0 g.setComposite(g.getComposite());
148    }
149  0 if (!g.getFont().equals(lastG.getFont()))
150    {
151  0 g.setFont(g.getFont());
152    }
153  0 if (!g.getStroke().equals(lastG.getStroke()))
154    {
155  0 g.setStroke(g.getStroke());
156    }
157    }
158  119825 _lastG = g;
159   
160  119825 try
161    {
162  119825 _bufferedWriter.write(line + "\n");
163    } catch (IOException e)
164    {
165  0 throw new EpsException("Could not write to the output file: " + e);
166    }
167    }
168   
169    /**
170    * Outputs the contents of the EPS document to the specified Writer, complete
171    * with headers and bounding box.
172    */
 
173  5 toggle public synchronized void write(Writer writer) throws IOException
174    {
175  5 float offsetX = -minX;
176  5 float offsetY = -minY;
177   
178  5 writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");
179  5 writer.write("%%Creator: Jalview "
180    + jalview.bin.Cache.getProperty("VERSION") + " \n");
181  5 writer.write("%%Title: " + _title + "\n");
182  5 writer.write("%%CreationDate: " + new Date() + "\n");
183  5 writer.write("%%BoundingBox: 0 0 " + ((int) Math.ceil(maxX + offsetX))
184    + " " + ((int) Math.ceil(maxY + offsetY)) + "\n");
185  5 writer.write("%%DocumentData: Clean7Bit\n");
186  5 writer.write("%%DocumentProcessColors: Black\n");
187  5 writer.write("%%ColorUsage: Color\n");
188  5 writer.write("%%Origin: 0 0\n");
189  5 writer.write("%%Pages: 1\n");
190  5 writer.write("%%Page: 1 1\n");
191  5 writer.write("%%EndComments\n\n");
192   
193  5 writer.write("gsave\n");
194   
195  5 if (_stringWriter != null)
196    {
197  0 writer.write(offsetX + " " + (offsetY) + " translate\n");
198   
199  0 _bufferedWriter.flush();
200  0 StringBuffer buffer = _stringWriter.getBuffer();
201  0 for (int i = 0; i < buffer.length(); i++)
202    {
203  0 writer.write(buffer.charAt(i));
204    }
205   
206  0 writeFooter(writer);
207    }
208    else
209    {
210  5 writer.write(offsetX + " " + ((maxY - minY) - offsetY)
211    + " translate\n");
212    }
213   
214  5 writer.flush();
215    }
216   
 
217  5 toggle private void writeFooter(Writer writer) throws IOException
218    {
219  5 writer.write("grestore\n");
220  5 if (isClipSet())
221    {
222  0 writer.write("grestore\n");
223    }
224  5 writer.write("showpage\n");
225  5 writer.write("\n");
226  5 writer.write("%%EOF");
227  5 writer.flush();
228    }
229   
 
230  10 toggle public synchronized void flush() throws IOException
231    {
232  10 _bufferedWriter.flush();
233    }
234   
 
235  5 toggle public synchronized void close() throws IOException
236    {
237  5 if (_stringWriter == null)
238    {
239  5 writeFooter(_bufferedWriter);
240  5 _bufferedWriter.flush();
241  5 _bufferedWriter.close();
242    }
243    }
244   
 
245  5 toggle public boolean isClipSet()
246    {
247  5 return _isClipSet;
248    }
249   
 
250  0 toggle public void setClipSet(boolean isClipSet)
251    {
252  0 _isClipSet = isClipSet;
253    }
254   
255    private float minX;
256   
257    private float minY;
258   
259    private float maxX;
260   
261    private float maxY;
262   
263    private boolean _isClipSet = false;
264   
265    private String _title;
266   
267    private StringWriter _stringWriter;
268   
269    private BufferedWriter _bufferedWriter = null;
270   
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;
275   
276    }