KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: GenericBarcodeImpl.java,v 1.6 2003/10/29 07:50:26 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 org.apache.avalon.framework.configuration.Configurable;
61 import org.apache.avalon.framework.configuration.Configuration;
62 import org.apache.avalon.framework.configuration.ConfigurationException;
63 import org.krysalis.barcode.BarcodeDimension;
64 import org.krysalis.barcode.BarcodeGenerator;
65 import org.krysalis.barcode.HumanReadablePlacement;
66 import org.krysalis.barcode.output.Canvas;
67 import org.krysalis.barcode.output.CanvasProvider;
68 import org.krysalis.barcode.tools.Length;
69 import org.krysalis.barcode.tools.UnitConv;
70
71 /**
72  * Base class for most barcode implementations.
73  *
74  * @author Jeremias Maerki
75  */

76 public abstract class GenericBarcodeImpl
77             implements BarcodeGenerator, Configurable {
78
79     /** Net height of bars in mm */
80     protected double height = 15.0; //mm
81
/** Width of narrow module in mm */
82     protected double moduleWidth;
83     /** Position of human-readable text */
84     protected HumanReadablePlacement msgPos = HumanReadablePlacement.HRP_BOTTOM;
85     /** Font size in pt */
86     protected double fontSize = 8; //pt
87
/** Font name */
88     protected String JavaDoc fontName = "Helvetica"; //"OCR-B,Helvetica,Arial";
89
/** True if quiet zone should be rendered */
90     protected boolean doQuietZone = true;
91     /** Width of the quiet zone left and right of the barcode in mm */
92     protected double quietZone;
93
94     /**
95      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
96      */

97     public void configure(Configuration cfg) throws ConfigurationException {
98         //Height
99
Length h = new Length(cfg.getChild("height").getValue("15mm"), "mm");
100         this.setHeight(h.getValueAsMillimeter());
101         
102         //Quiet zone
103
this.doQuietZone = cfg.getChild("quiet-zone").getAttributeAsBoolean("enabled", true);
104         Length qz = new Length(cfg.getChild("quiet-zone").getValue("10mw"), "mw");
105         if (qz.getUnit().equalsIgnoreCase("mw")) {
106             this.quietZone = qz.getValue() * getModuleWidth();
107         } else {
108             this.quietZone = qz.getValueAsMillimeter();
109         }
110         
111         //Human-readable placement
112
this.msgPos = HumanReadablePlacement.byName(
113             cfg.getChild("human-readable").getValue(HumanReadablePlacement.HRP_BOTTOM.getName()));
114     }
115     
116     /**
117      * Returns the height of the human-readable part.
118      * @return the height of the human-readable part (in mm)
119      */

120     public double getHumanReadableHeight() {
121         double textHeight = UnitConv.pt2mm(this.fontSize);
122         return 1.0 * textHeight;
123     }
124
125     /**
126      * Returns the height of the bars.
127      * @return the height of the bars (in mm)
128      */

129     public double getBarHeight() {
130         return this.height;
131     }
132
133     /**
134      * Returns the full height of the barcode.
135      * @return the full height (in mm)
136      */

137     public double getHeight() {
138         return getBarHeight() + getHumanReadableHeight();
139     }
140
141     /**
142      * Sets the height of the bars.
143      * @param height the height of the bars (in mm)
144      */

145     public void setBarHeight(double height) {
146         this.height = height;
147     }
148
149     /**
150      * Sets the full height of the barcode.
151      * @param height the full height (in mm)
152      */

153     public void setHeight(double height) {
154         this.height = height - getHumanReadableHeight();
155     }
156
157     /**
158      * Returns the width of the narrow module.
159      * @return the width of the narrow module (in mm)
160      */

161     public double getModuleWidth() {
162         return this.moduleWidth;
163     }
164
165     /**
166      * Sets the width of the narrow module.
167      * @param width the width of the narrow module (in mm)
168      */

169     public void setModuleWidth(double width) {
170         this.moduleWidth = width;
171     }
172     
173     /**
174      * Returns the effective width of a bar with a given logical width.
175      * @param width the logical width (1=narrow, 2=wide)
176      * @return the effective width of a bar (in mm)
177      */

178     public abstract double getBarWidth(int width);
179
180     /**
181      * Indicates whether a quiet zone is included.
182      * @return true if a quiet zone is included
183      */

184     public boolean hasQuietZone() {
185         return this.doQuietZone;
186     }
187
188     /**
189      * Controls whether a quiet zone should be included or not.
190      * @param value true if a quiet zone should be included
191      */

192     public void doQuietZone(boolean value) {
193         this.doQuietZone = value;
194     }
195
196     /**
197      * Returns the width of the quiet zone.
198      * @return the width of the quiet zone (in mm)
199      */

200     public double getQuietZone() {
201         return this.quietZone;
202     }
203
204     /**
205      * Returns the placement of the human-readable part.
206      * @return the placement of the human-readable part
207      */

208     public HumanReadablePlacement getMsgPosition() {
209         return this.msgPos;
210     }
211
212     /**
213      * Sets the placement of the human-readable part.
214      * @param placement the placement of the human-readable part
215      */

216     public void setMsgPosition(HumanReadablePlacement placement) {
217         this.msgPos = placement;
218     }
219
220     /**
221      * Draws a centered character on a canvas.
222      * @param canvas the canvas to paint on
223      * @param ch the character
224      * @param x1 the left boundary
225      * @param x2 the right boundary
226      * @param y1 the y coordinate of the font's baseline
227      */

228     protected void drawCenteredChar(Canvas canvas, char ch,
229                                     double x1, double x2, double y1) {
230         canvas.drawCenteredChar(ch, x1, x2, y1 - UnitConv.pt2mm(fontSize) * 0.2,
231                 fontName, fontSize);
232     }
233
234     /**
235      * Draws justified text on a canvas.
236      * @param canvas the canvas to paint on
237      * @param text the text to paint
238      * @param x1 the left boundary
239      * @param x2 the right boundary
240      * @param y1 the y coordinate of the font's baseline
241      */

242     protected void drawJustifiedText(Canvas canvas, String JavaDoc text,
243                                     double x1, double x2, double y1) {
244         canvas.drawJustifiedText(text, x1, x2, y1 - UnitConv.pt2mm(fontSize) * 0.2,
245                 fontName, fontSize);
246     }
247
248     /**
249      * Draws centered text on a canvas.
250      * @param canvas the canvas to paint on
251      * @param text the text to paint
252      * @param x1 the left boundary
253      * @param x2 the right boundary
254      * @param y1 the y coordinate of the font's baseline
255      */

256     protected void drawCenteredText(Canvas canvas, String JavaDoc text,
257                                     double x1, double x2, double y1) {
258         canvas.drawCenteredText(text, x1, x2, y1 - UnitConv.pt2mm(fontSize) * 0.2,
259                 fontName, fontSize);
260     }
261
262     /** @see org.krysalis.barcode.BarcodeGenerator */
263     public abstract void generateBarcode(CanvasProvider canvas, String JavaDoc msg);
264     
265     /** @see org.krysalis.barcode.BarcodeGenerator */
266     public BarcodeDimension calcDimensions(String JavaDoc msg) {
267         throw new UnsupportedOperationException JavaDoc("NYI");
268     }
269 }
Popular Tags