KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > impl > UPCEANCanvasLogicHandler


1 /*
2  * $Id: UPCEANCanvasLogicHandler.java,v 1.11 2003/06/11 09:25:11 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.impl;
59
60 import java.util.Stack JavaDoc;
61
62 import org.krysalis.barcode.BarGroup;
63 import org.krysalis.barcode.BarcodeDimension;
64 import org.krysalis.barcode.ClassicBarcodeLogicHandler;
65 import org.krysalis.barcode.HumanReadablePlacement;
66 import org.krysalis.barcode.output.Canvas;
67
68 /**
69  * Logic Handler implementation for painting on a Canvas. This is a special
70  * implementation for UPC and EAN barcodes.
71  *
72  * @author Jeremias Maerki
73  */

74 public class UPCEANCanvasLogicHandler implements ClassicBarcodeLogicHandler {
75     
76     private UPCEAN bcImpl;
77     private Canvas canvas;
78     private double x = 0.0;
79     private BarcodeDimension dim;
80     private String JavaDoc msg;
81     private String JavaDoc lastgroup;
82     private double groupx;
83     private boolean inMsgGroup;
84     private boolean inSupplemental;
85     private Stack JavaDoc groupStack = new Stack JavaDoc();
86     
87     /**
88      * Main constructor.
89      * @param bcImpl the barcode implementation class
90      * @param canvas the canvas to paint to
91      */

92     public UPCEANCanvasLogicHandler(GenericBarcodeImpl bcImpl, Canvas canvas) {
93         if (!(bcImpl instanceof UPCEAN)) {
94             throw new IllegalArgumentException JavaDoc("This LogicHandler can only be "
95                 + "used with UPC and EAN barcode implementations");
96         }
97         this.bcImpl = (UPCEAN)bcImpl;
98         this.canvas = canvas;
99     }
100     
101     private double getStartX() {
102         if (bcImpl.hasQuietZone()) {
103             return bcImpl.getQuietZone();
104         } else {
105             return 0.0;
106         }
107     }
108
109     /** @see org.krysalis.barcode.ClassicBarcodeLogicHandler */
110     public void startBarcode(String JavaDoc msg) {
111         this.msg = msg;
112         //Calculate extents
113
this.dim = bcImpl.calcDimensions(msg);
114         
115         canvas.establishDimensions(dim);
116         x = getStartX();
117         inMsgGroup = false;
118         inSupplemental = false;
119         
120     }
121
122     /** @see org.krysalis.barcode.ClassicBarcodeLogicHandler */
123     public void startBarGroup(BarGroup type, String JavaDoc submsg) {
124         if (type == BarGroup.UPC_EAN_GUARD) {
125             //nop
126
} else if (type == BarGroup.UPC_EAN_GROUP) {
127             inMsgGroup = true;
128             groupx = x;
129             lastgroup = submsg;
130         } else if (type == BarGroup.UPC_EAN_LEAD) {
131             lastgroup = submsg;
132         } else if (type == BarGroup.UPC_EAN_CHECK) {
133             if (!inMsgGroup) {
134                 lastgroup = submsg;
135             }
136         } else if (type == BarGroup.UPC_EAN_SUPP) {
137             inSupplemental = true;
138             x += bcImpl.getQuietZone();
139             groupx = x;
140         }
141         groupStack.push(type);
142     }
143
144     /** @see org.krysalis.barcode.ClassicBarcodeLogicHandler */
145     public void addBar(boolean black, int width) {
146         final double w = bcImpl.getBarWidth(width);
147         if (black) {
148             final double h;
149             final double y;
150             if (!inSupplemental) {
151                 if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) {
152                     canvas.drawRectWH(x, 0, w, bcImpl.getHeight());
153                 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) {
154                     if (inMsgGroup) {
155                         h = bcImpl.getBarHeight();
156                         y = bcImpl.getHumanReadableHeight();
157                     } else {
158                         h = bcImpl.getBarHeight() + (bcImpl.getHumanReadableHeight() / 2);
159                         y = bcImpl.getHumanReadableHeight() / 2;
160                     }
161                     canvas.drawRectWH(x, y, w, h);
162                 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) {
163                     if (inMsgGroup) {
164                         h = bcImpl.getBarHeight();
165                     } else {
166                         h = bcImpl.getBarHeight() + (bcImpl.getHumanReadableHeight() / 2);
167                     }
168                     canvas.drawRectWH(x, 0.0, w, h);
169                 }
170             } else {
171                 //Special painting in supplemental
172
if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) {
173                     h = bcImpl.getBarHeight();
174                     y = bcImpl.getHumanReadableHeight();
175                     canvas.drawRectWH(x, y, w, h);
176                 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) {
177                     h = bcImpl.getBarHeight()
178                         + (bcImpl.getHumanReadableHeight() / 2)
179                         - bcImpl.getHumanReadableHeight();
180                     y = bcImpl.getHumanReadableHeight() / 2;
181                     canvas.drawRectWH(x, y, w, h);
182                 } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) {
183                     h = bcImpl.getBarHeight()
184                         + (bcImpl.getHumanReadableHeight() / 2)
185                         - bcImpl.getHumanReadableHeight();
186                     y = bcImpl.getHumanReadableHeight();
187                     canvas.drawRectWH(x, y, w, h);
188                 }
189             }
190         }
191         x += w;
192     }
193     
194     private boolean isEAN() {
195         return (bcImpl instanceof EAN13) || (bcImpl instanceof EAN8);
196     }
197
198     /** @see org.krysalis.barcode.ClassicBarcodeLogicHandler */
199     public void endBarGroup() {
200         BarGroup group = (BarGroup)groupStack.pop();
201
202         if (group == BarGroup.UPC_EAN_GROUP) {
203             inMsgGroup = false;
204             if (lastgroup == null) {
205                 //Guards don't set the lastgroup variable
206
return;
207             }
208             int colonPos = lastgroup.indexOf(":");
209             String JavaDoc grouptext = lastgroup;
210             if (colonPos >= 0) {
211                 String JavaDoc lead = new Character JavaDoc(grouptext.charAt(0)).toString();
212                 drawLeadChar(lead);
213                 grouptext = grouptext.substring(colonPos + 1);
214             }
215     
216             //character group text
217
drawGroupText(grouptext);
218         } else if (group == BarGroup.UPC_EAN_LEAD) {
219             if (!isEAN()) {
220                 drawLeadChar(lastgroup);
221             }
222         } else if (group == BarGroup.UPC_EAN_CHECK) {
223             if (!isEAN()) {
224                 drawTrailingChar(lastgroup);
225             }
226         } else if (group == BarGroup.UPC_EAN_SUPP) {
227             drawSupplementalText(UPCEANLogicImpl.retrieveSupplemental(this.msg));
228             inSupplemental = false;
229         }
230     }
231     
232     private void drawLeadChar(String JavaDoc lead) {
233         final double leadw = 7 * bcImpl.getBarWidth(1);
234         final double leadx = getStartX()
235                     - 3 * bcImpl.getBarWidth(1)
236                     - leadw;
237                     
238         if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) {
239             //nop
240
} else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) {
241             bcImpl.drawCenteredText(canvas, lead, leadx, leadx + leadw,
242                     bcImpl.getHumanReadableHeight());
243         } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) {
244             bcImpl.drawCenteredText(canvas, lead, leadx, leadx + leadw,
245                     bcImpl.getHeight());
246         }
247     }
248
249     private void drawTrailingChar(String JavaDoc trailer) {
250         final double trailerw = 7 * bcImpl.getBarWidth(1);
251         final double trailerx = getStartX()
252                     + this.dim.getWidth()
253                     - bcImpl.supplementalWidth(this.msg)
254                     + 3 * bcImpl.getBarWidth(1);
255                     
256         if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) {
257             //nop
258
} else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) {
259             bcImpl.drawCenteredText(canvas,
260                     trailer, trailerx, trailerx + trailerw,
261                     bcImpl.getHumanReadableHeight());
262         } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) {
263             bcImpl.drawCenteredText(canvas,
264                     trailer, trailerx, trailerx + trailerw,
265                     bcImpl.getHeight());
266         }
267     }
268
269     private void drawGroupText(String JavaDoc text) {
270         if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_NONE) {
271             //nop
272
} else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) {
273             bcImpl.drawJustifiedText(canvas, text,
274                     groupx + bcImpl.getBarWidth(1),
275                     x - bcImpl.getBarWidth(1),
276                     bcImpl.getHumanReadableHeight());
277         } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) {
278             bcImpl.drawJustifiedText(canvas, text,
279                     groupx + bcImpl.getBarWidth(1),
280                     x - bcImpl.getBarWidth(1),
281                     bcImpl.getHeight());
282         }
283     }
284     
285     private void drawSupplementalText(String JavaDoc supp) {
286         if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_TOP) {
287             bcImpl.drawCenteredText(canvas, supp,
288                     groupx,
289                     x,
290                     bcImpl.getHeight());
291         } else if (bcImpl.getMsgPosition() == HumanReadablePlacement.HRP_BOTTOM) {
292             bcImpl.drawCenteredText(canvas, supp,
293                     groupx,
294                     x,
295                     bcImpl.getHumanReadableHeight());
296         }
297     }
298
299     /** @see org.krysalis.barcode.ClassicBarcodeLogicHandler */
300     public void endBarcode() {
301     }
302
303 }
304
305
Popular Tags