KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > output > eps > EPSCanvasProvider


1 /*
2  * $Id: EPSCanvasProvider.java,v 1.7 2003/08/18 19:12:41 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.output.eps;
59
60 import java.io.IOException JavaDoc;
61 import java.io.OutputStream JavaDoc;
62 import java.io.UnsupportedEncodingException JavaDoc;
63 import java.io.Writer JavaDoc;
64 import java.text.DecimalFormat JavaDoc;
65 import java.text.DecimalFormatSymbols JavaDoc;
66 import java.text.SimpleDateFormat JavaDoc;
67
68 import org.apache.avalon.framework.CascadingRuntimeException;
69 import org.krysalis.barcode.BarcodeDimension;
70 import org.krysalis.barcode.output.AbstractCanvasProvider;
71 import org.krysalis.barcode.tools.UnitConv;
72
73 /**
74  * CanvasProvider implementation for EPS output (Encapsulated PostScript).
75  * @author Jeremias Maerki
76  */

77 public class EPSCanvasProvider extends AbstractCanvasProvider {
78
79     private OutputStream JavaDoc out;
80     private Writer JavaDoc writer;
81     private DecimalFormat JavaDoc df;
82     private IOException JavaDoc firstError;
83     private double height;
84
85     /**
86      * Main constructor.
87      * @param out OutputStream to write the EPS to
88      * @throws IOException in case of an I/O problem
89      */

90     public EPSCanvasProvider(OutputStream JavaDoc out) throws IOException JavaDoc {
91         super();
92         this.out = out;
93         try {
94             this.writer = new java.io.OutputStreamWriter JavaDoc(out, "US-ASCII");
95         } catch (UnsupportedEncodingException JavaDoc uee) {
96             throw new CascadingRuntimeException(
97                     "Incompatible VM: Need US-ASCII encoding", uee);
98         }
99     }
100     
101     /**
102      * Returns the DecimalFormat instance to use internally to format numbers.
103      * @return a DecimalFormat instance
104      */

105     protected DecimalFormat JavaDoc getDecimalFormat() {
106         if (this.df == null) {
107             DecimalFormatSymbols JavaDoc dfs = new DecimalFormatSymbols JavaDoc();
108             dfs.setDecimalSeparator('.');
109             this.df = new DecimalFormat JavaDoc("0.####", dfs);
110         }
111         return this.df;
112     }
113     
114     private String JavaDoc format(double coord) {
115         return getDecimalFormat().format(coord);
116     }
117     
118     private String JavaDoc formatmm(double coord) {
119         return getDecimalFormat().format(UnitConv.mm2pt(coord));
120     }
121     
122     private String JavaDoc formatmm(double x, double y) {
123         return formatmm(x) + " " + formatmm(this.height - y);
124     }
125     
126     private void writeHeader(double width, double height) throws IOException JavaDoc {
127         writer.write("%!PS-Adobe-3.0 EPSF-3.0\n");
128         double widthpt = UnitConv.mm2pt(width);
129         double heightpt = UnitConv.mm2pt(height);
130         writer.write("%%BoundingBox: 0 0 "
131                 + Math.round(Math.ceil(widthpt)) + " "
132                 + Math.round(Math.ceil(heightpt)) + "\n");
133         writer.write("%%HiResBoundingBox: 0 0 "
134                 + format(widthpt) + " "
135                 + format(heightpt) + "\n");
136         writer.write("%%Creator: Krysalis Barcode (http://www.krysalis.org/barcode)\n");
137         final SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("yyyy-MM-dd'T'HH:mm:ss");
138         writer.write("%%CreationDate: " + sdf.format(new java.util.Date JavaDoc()) + "\n");
139         writer.write("%%LanguageLevel: 1\n");
140         writer.write("%%EndComments\n");
141         writer.write("%%BeginProlog\n");
142         writer.write("%%BeginProcSet: krysalis-barcode-procset 1.0\n");
143         writer.write("/rf {\n"); //rect fill: x y w h rf
144
writer.write("newpath\n");
145         writer.write("4 -2 roll moveto\n");
146         writer.write("dup neg 0 exch rlineto\n");
147         writer.write("exch 0 rlineto\n");
148         writer.write("0 neg exch rlineto\n");
149         writer.write("closepath fill\n");
150         writer.write("} def\n");
151         writer.write("/ct {\n"); //centered text: (text) middle-x y ct
152
writer.write("moveto dup stringwidth\n");
153         writer.write("2 div neg exch 2 div neg exch\n");
154         writer.write("rmoveto show\n");
155         writer.write("} def\n");
156         writer.write("/jt {\n"); //justified: (text) x1 x2 y jt
157
//Calc string width
158
writer.write("4 -1 roll dup stringwidth pop\n");
159         //Calc available width (x2-x1)
160
writer.write("5 -2 roll 1 index sub\n");
161         //Calc (text-width - avail-width)
162
writer.write("3 -1 roll sub\n");
163         //Get string length
164
writer.write("2 index length\n");
165         //avail-width / (string-length - 1) = distributable-space
166
writer.write("1 sub div\n");
167         //setup moveto and ashow
168
writer.write("0 4 -1 roll 4 -1 roll 5 -1 roll\n");
169         writer.write("moveto ashow\n");
170         writer.write("} def\n");
171         writer.write("%%EndProcSet: krysalis-barcode-procset 1.0\n");
172         writer.write("%%EndProlog\n");
173     }
174
175     /**
176      * Writes the EPS trailer. Must be called after barcode painting call
177      * returns.
178      * @throws IOException if an I/O error happened during EPS generation
179      */

180     public void finish() throws IOException JavaDoc {
181         if (firstError != null) {
182             throw firstError;
183         }
184         writer.write("%%EOF\n");
185         writer.flush();
186     }
187
188     /** {@inheritDoc} */
189     public void establishDimensions(BarcodeDimension dim) {
190         super.establishDimensions(dim);
191         if (firstError != null) {
192             return;
193         }
194         this.height = dim.getHeightPlusQuiet();
195         try {
196             writeHeader(dim.getWidthPlusQuiet(), dim.getHeightPlusQuiet());
197         } catch (IOException JavaDoc ioe) {
198             firstError = ioe;
199         }
200     }
201
202     /** {@inheritDoc} */
203     public void deviceFillRect(double x, double y, double w, double h) {
204         if (firstError != null) {
205             return;
206         }
207         try {
208             /*
209             writer.write(formatmm(w) + " " + formatmm(h) + " "
210                        + formatmm(x) + " " + formatmm(y) + " rf\n");
211             */

212             writer.write(formatmm(x, y) + " "
213                        + formatmm(w) + " " + formatmm(h) + " rf\n");
214         } catch (IOException JavaDoc ioe) {
215             firstError = ioe;
216         }
217     }
218
219     /** {@inheritDoc} */
220     public void deviceJustifiedText(
221                 String JavaDoc text,
222                 double x1,
223                 double x2,
224                 double y1,
225                 String JavaDoc fontName,
226                 double fontSize) {
227         if (firstError != null) {
228             return;
229         }
230         try {
231             writer.write("/" + fontName + " findfont "
232                     + fontSize + " scalefont setfont\n");
233
234             writer.write("(" + text + ") "
235                     + formatmm(x1) + " "
236                     + formatmm(x2) + " "
237                     + formatmm(this.height - y1) + " jt\n");
238         } catch (IOException JavaDoc ioe) {
239             firstError = ioe;
240         }
241     }
242
243     /** {@inheritDoc} */
244     public void deviceCenteredText(
245                 String JavaDoc text,
246                 double x1,
247                 double x2,
248                 double y1,
249                 String JavaDoc fontName,
250                 double fontSize) {
251         if (firstError != null) {
252             return;
253         }
254         try {
255             writer.write("/" + fontName + " findfont "
256                     + fontSize + " scalefont setfont\n");
257             writer.write("(" + text + ") "
258                     + formatmm((x1 + x2) / 2, y1) + " ct\n");
259         } catch (IOException JavaDoc ioe) {
260             firstError = ioe;
261         }
262     }
263
264 }
265
Popular Tags