1 /***********************************************************************************************************************
2 * Copyright (c) 2003, International Barcode Consortium
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this list of
11 * conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14 * or promote products derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
25 ***********************************************************************************************************************/
26
27 package net.sourceforge.barbecue.output;
28
29 import net.sourceforge.barbecue.env.DefaultEnvironment;
30
31 import java.io.BufferedWriter;
32 import java.io.IOException;
33 import java.io.Writer;
34 import java.awt.*;
35
36 /**
37 * EPS outputter to output barcodes as Encapsulated Postscript files.
38 *
39 * Contributed by Tim Molteno.
40 *
41 * @author <a href="mailto:tim@molteno.net">Tim Molteno</a>
42 */
43 public class EPSOutput extends AbstractOutput {
44 private StringBuffer epsHeader;
45 private StringBuffer epsBody;
46 private final Writer writer;
47 private boolean backgroundDrawing = false;
48
49 /**
50 * Creates a new instance of EPSOutput.
51 * @param writer The Writer to output the EPS text to
52 */
53 public EPSOutput(Writer writer) {
54 super(DefaultEnvironment.DEFAULT_FONT, true, 1.0, Color.black, Color.white);
55 this.writer = new BufferedWriter(writer);
56 epsBody = new StringBuffer();
57 epsHeader = new StringBuffer();
58 backgroundDrawing = false;
59 }
60
61 public void beginDraw() {
62 }
63
64 /**
65 * From AbstractOutput - finished up the EPS output.
66 */
67 public void endDraw(int width, int height) {
68 epsHeader.setLength(0);
69 epsHeader.append("%!PS-Adobe-2.0 EPSF-1.2\n");
70 epsHeader.append("%%Creator: barbeque\n");
71 epsHeader.append("%%BoundingBox: 0 0 ");
72 epsHeader.append((int)getScaledDimension(width));
73 epsHeader.append(" ");
74 epsHeader.append((int)getScaledDimension(height));
75 epsHeader.append("\n");
76 epsHeader.append("%%EndComments\n");
77 epsHeader.append("% Printing barcode for \"");
78 epsHeader.append("\", scaled 1.00\n");
79
80 try {
81 writer.write(epsHeader.toString());
82 writer.write(epsBody.toString());
83 writer.write("% End barcode\n");
84 }
85 catch (java.io.IOException ex) {
86 System.err.println("IO Exception writing EPS epilogue: " + ex.toString());
87 }
88 finally {
89 try {
90 writer.flush();
91 writer.close();
92 }
93 catch (IOException e) {
94 System.err.println("IO Exception closing EPS stream: " + e.toString());
95 }
96 }
97 }
98
99 /**
100 * From AbstractOutput - outputs the correct rectangle to the EPS output.
101 * @param x the x coordinate
102 * @param y the y coordinate
103 * @param width the width
104 * @param height the height
105 * @param paintWithForegroundColor if true, use the foreground color, otherwise use the background color
106 */
107 public int drawBar(int x, int y, int width, int height, boolean paintWithForegroundColor) {
108 return (int)drawBarEPS( x, y, width, height, paintWithForegroundColor);
109 }
110
111 /**
112 * From AbstractOutput - outputs the correct rectangle to the EPS output.
113 * @param x the x coordinate
114 * @param y the y coordinate
115 * @param width the width
116 * @param height the height
117 * @param paintWithForegroundColor if true, use the foreground color, otherwise use the background color
118 */
119 public double drawBarEPS(double x, double y, double width, double height, boolean paintWithForegroundColor) {
120
121
122 {
123 epsBody.append("%");
124 }
125 epsBody.append("\t["); epsBody.append(getScaledDimension(height));
126 epsBody.append(" "); epsBody.append(getScaledDimension(x));
127 epsBody.append(" "); epsBody.append(getScaledDimension(y));
128 epsBody.append(" "); epsBody.append(getScaledDimension(width));
129 epsBody.append("]\n");
130
131 if (true == paintWithForegroundColor && (false == backgroundDrawing)) {
132 epsBody.append("newpath\n");
133 epsBody.append(x); epsBody.append(" "); epsBody.append(y); epsBody.append(" moveto\n");
134 epsBody.append(0); epsBody.append(" "); epsBody.append(height); epsBody.append(" rlineto\n");
135 epsBody.append(width); epsBody.append(" "); epsBody.append(0); epsBody.append(" rlineto\n");
136 epsBody.append(0); epsBody.append(" "); epsBody.append(-height); epsBody.append(" rlineto\n");
137 epsBody.append("closepath\n");
138 epsBody.append("fill\n\n");
139 }
140
141 return width;
142 }
143
144 public int drawText(String text, LabelLayout layout) throws OutputException {
145 return 0;
146 }
147
148 public void toggleDrawingColor() {
149 backgroundDrawing = !backgroundDrawing;
150 }
151
152 /**
153 * Paint the background the background colour, based on the height and the width.
154 * @param x the x coordinate
155 * @param y the y coordinate
156 * @param width the width to be painted
157 * @param height the height to be painted
158 */
159 public void paintBackground(int x, int y, int width, int height) {
160 }
161
162 private double getScaledDimension(double value) {
163 return scalar*value;
164 }
165 }