1 | /* | |
2 | * $Id: Barcode.java 3117 2008-01-31 05:53:22Z xlv $ | |
3 | * | |
4 | * Copyright 2002-2006 by Paulo Soares. | |
5 | * | |
6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
7 | * (the "License"); you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
9 | * | |
10 | * Software distributed under the License is distributed on an "AS IS" basis, | |
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 | * for the specific language governing rights and limitations under the License. | |
13 | * | |
14 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
17 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
18 | * All Rights Reserved. | |
19 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
20 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
21 | * | |
22 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | package com.lowagie.text.pdf; | |
50 | ||
51 | import java.awt.Color; | |
52 | ||
53 | ||
54 | import com.lowagie.text.Image; | |
55 | import com.lowagie.text.Rectangle; | |
56 | import com.lowagie.text.ExceptionConverter; | |
57 | ||
58 | /** Base class containing properties and methods common to all | |
59 | * barcode types. | |
60 | * | |
61 | * @author Paulo Soares (psoares@consiste.pt) | |
62 | */ | |
63 | public abstract class Barcode { | |
64 | /** A type of barcode */ | |
65 | public static final int EAN13 = 1; | |
66 | /** A type of barcode */ | |
67 | public static final int EAN8 = 2; | |
68 | /** A type of barcode */ | |
69 | public static final int UPCA = 3; | |
70 | /** A type of barcode */ | |
71 | public static final int UPCE = 4; | |
72 | /** A type of barcode */ | |
73 | public static final int SUPP2 = 5; | |
74 | /** A type of barcode */ | |
75 | public static final int SUPP5 = 6; | |
76 | /** A type of barcode */ | |
77 | public static final int POSTNET = 7; | |
78 | /** A type of barcode */ | |
79 | public static final int PLANET = 8; | |
80 | /** A type of barcode */ | |
81 | public static final int CODE128 = 9; | |
82 | /** A type of barcode */ | |
83 | public static final int CODE128_UCC = 10; | |
84 | /** A type of barcode */ | |
85 | public static final int CODE128_RAW = 11; | |
86 | /** A type of barcode */ | |
87 | public static final int CODABAR = 12; | |
88 | ||
89 | /** The minimum bar width. | |
90 | */ | |
91 | protected float x; | |
92 | ||
93 | /** The bar multiplier for wide bars or the distance between | |
94 | * bars for Postnet and Planet. | |
95 | */ | |
96 | protected float n; | |
97 | | |
98 | /** The text font. <CODE>null</CODE> if no text. | |
99 | */ | |
100 | protected BaseFont font; | |
101 | ||
102 | /** The size of the text or the height of the shorter bar | |
103 | * in Postnet. | |
104 | */ | |
105 | protected float size; | |
106 | | |
107 | /** If positive, the text distance under the bars. If zero or negative, | |
108 | * the text distance above the bars. | |
109 | */ | |
110 | protected float baseline; | |
111 | | |
112 | /** The height of the bars. | |
113 | */ | |
114 | protected float barHeight; | |
115 | | |
116 | /** The text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>, | |
117 | * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>. | |
118 | */ | |
119 | protected int textAlignment; | |
120 | | |
121 | /** The optional checksum generation. | |
122 | */ | |
123 | protected boolean generateChecksum; | |
124 | | |
125 | /** Shows the generated checksum in the the text. | |
126 | */ | |
127 | protected boolean checksumText; | |
128 | | |
129 | /** Show the start and stop character '*' in the text for | |
130 | * the barcode 39 or 'ABCD' for codabar. | |
131 | */ | |
132 | protected boolean startStopText; | |
133 | | |
134 | /** Generates extended barcode 39. | |
135 | */ | |
136 | protected boolean extended; | |
137 | | |
138 | /** The code to generate. | |
139 | */ | |
140 | protected String code = ""; | |
141 | | |
142 | /** Show the guard bars for barcode EAN. | |
143 | */ | |
144 | protected boolean guardBars; | |
145 | | |
146 | /** The code type. | |
147 | */ | |
148 | protected int codeType; | |
149 | | |
150 | /** The ink spreading. */ | |
151 | protected float inkSpreading = 0; | |
152 | | |
153 | /** Gets the minimum bar width. | |
154 | * @return the minimum bar width | |
155 | */ | |
156 | public float getX() { | |
157 | return x; | |
158 | } | |
159 | | |
160 | /** Sets the minimum bar width. | |
161 | * @param x the minimum bar width | |
162 | */ | |
163 | public void setX(float x) { | |
164 | this.x = x; | |
165 | } | |
166 | | |
167 | /** Gets the bar multiplier for wide bars. | |
168 | * @return the bar multiplier for wide bars | |
169 | */ | |
170 | public float getN() { | |
171 | return n; | |
172 | } | |
173 | | |
174 | /** Sets the bar multiplier for wide bars. | |
175 | * @param n the bar multiplier for wide bars | |
176 | */ | |
177 | public void setN(float n) { | |
178 | this.n = n; | |
179 | } | |
180 | | |
181 | /** Gets the text font. <CODE>null</CODE> if no text. | |
182 | * @return the text font. <CODE>null</CODE> if no text | |
183 | */ | |
184 | public BaseFont getFont() { | |
185 | return font; | |
186 | } | |
187 | | |
188 | /** Sets the text font. | |
189 | * @param font the text font. Set to <CODE>null</CODE> to suppress any text | |
190 | */ | |
191 | public void setFont(BaseFont font) { | |
192 | this.font = font; | |
193 | } | |
194 | | |
195 | /** Gets the size of the text. | |
196 | * @return the size of the text | |
197 | */ | |
198 | public float getSize() { | |
199 | return size; | |
200 | } | |
201 | | |
202 | /** Sets the size of the text. | |
203 | * @param size the size of the text | |
204 | */ | |
205 | public void setSize(float size) { | |
206 | this.size = size; | |
207 | } | |
208 | | |
209 | /** Gets the text baseline. | |
210 | * If positive, the text distance under the bars. If zero or negative, | |
211 | * the text distance above the bars. | |
212 | * @return the baseline. | |
213 | */ | |
214 | public float getBaseline() { | |
215 | return baseline; | |
216 | } | |
217 | | |
218 | /** Sets the text baseline. | |
219 | * If positive, the text distance under the bars. If zero or negative, | |
220 | * the text distance above the bars. | |
221 | * @param baseline the baseline. | |
222 | */ | |
223 | public void setBaseline(float baseline) { | |
224 | this.baseline = baseline; | |
225 | } | |
226 | | |
227 | /** Gets the height of the bars. | |
228 | * @return the height of the bars | |
229 | */ | |
230 | public float getBarHeight() { | |
231 | return barHeight; | |
232 | } | |
233 | | |
234 | /** Sets the height of the bars. | |
235 | * @param barHeight the height of the bars | |
236 | */ | |
237 | public void setBarHeight(float barHeight) { | |
238 | this.barHeight = barHeight; | |
239 | } | |
240 | | |
241 | /** Gets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>, | |
242 | * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>. | |
243 | * @return the text alignment | |
244 | */ | |
245 | public int getTextAlignment() { | |
246 | return textAlignment; | |
247 | } | |
248 | | |
249 | /** Sets the text alignment. Can be <CODE>Element.ALIGN_LEFT</CODE>, | |
250 | * <CODE>Element.ALIGN_CENTER</CODE> or <CODE>Element.ALIGN_RIGHT</CODE>. | |
251 | * @param textAlignment the text alignment | |
252 | */ | |
253 | public void setTextAlignment(int textAlignment) { | |
254 | this.textAlignment = textAlignment; | |
255 | } | |
256 | | |
257 | /** Gets the optional checksum generation. | |
258 | * @return the optional checksum generation | |
259 | */ | |
260 | public boolean isGenerateChecksum() { | |
261 | return generateChecksum; | |
262 | } | |
263 | | |
264 | /** Setter for property generateChecksum. | |
265 | * @param generateChecksum New value of property generateChecksum. | |
266 | */ | |
267 | public void setGenerateChecksum(boolean generateChecksum) { | |
268 | this.generateChecksum = generateChecksum; | |
269 | } | |
270 | | |
271 | /** Gets the property to show the generated checksum in the the text. | |
272 | * @return value of property checksumText | |
273 | */ | |
274 | public boolean isChecksumText() { | |
275 | return checksumText; | |
276 | } | |
277 | | |
278 | /** Sets the property to show the generated checksum in the the text. | |
279 | * @param checksumText new value of property checksumText | |
280 | */ | |
281 | public void setChecksumText(boolean checksumText) { | |
282 | this.checksumText = checksumText; | |
283 | } | |
284 | | |
285 | /** Sets the property to show the start and stop character '*' in the text for | |
286 | * the barcode 39. | |
287 | * @return value of property startStopText | |
288 | */ | |
289 | public boolean isStartStopText() { | |
290 | return startStopText; | |
291 | } | |
292 | | |
293 | /** Gets the property to show the start and stop character '*' in the text for | |
294 | * the barcode 39. | |
295 | * @param startStopText new value of property startStopText | |
296 | */ | |
297 | public void setStartStopText(boolean startStopText) { | |
298 | this.startStopText = startStopText; | |
299 | } | |
300 | | |
301 | /** Gets the property to generate extended barcode 39. | |
302 | * @return value of property extended. | |
303 | */ | |
304 | public boolean isExtended() { | |
305 | return extended; | |
306 | } | |
307 | | |
308 | /** Sets the property to generate extended barcode 39. | |
309 | * @param extended new value of property extended | |
310 | */ | |
311 | public void setExtended(boolean extended) { | |
312 | this.extended = extended; | |
313 | } | |
314 | | |
315 | /** Gets the code to generate. | |
316 | * @return the code to generate | |
317 | */ | |
318 | public String getCode() { | |
319 | return code; | |
320 | } | |
321 | | |
322 | /** Sets the code to generate. | |
323 | * @param code the code to generate | |
324 | */ | |
325 | public void setCode(String code) { | |
326 | this.code = code; | |
327 | } | |
328 | | |
329 | /** Gets the property to show the guard bars for barcode EAN. | |
330 | * @return value of property guardBars | |
331 | */ | |
332 | public boolean isGuardBars() { | |
333 | return guardBars; | |
334 | } | |
335 | | |
336 | /** Sets the property to show the guard bars for barcode EAN. | |
337 | * @param guardBars new value of property guardBars | |
338 | */ | |
339 | public void setGuardBars(boolean guardBars) { | |
340 | this.guardBars = guardBars; | |
341 | } | |
342 | | |
343 | /** Gets the code type. | |
344 | * @return the code type | |
345 | */ | |
346 | public int getCodeType() { | |
347 | return codeType; | |
348 | } | |
349 | | |
350 | /** Sets the code type. | |
351 | * @param codeType the code type | |
352 | */ | |
353 | public void setCodeType(int codeType) { | |
354 | this.codeType = codeType; | |
355 | } | |
356 | | |
357 | /** Gets the maximum area that the barcode and the text, if | |
358 | * any, will occupy. The lower left corner is always (0, 0). | |
359 | * @return the size the barcode occupies. | |
360 | */ | |
361 | public abstract Rectangle getBarcodeSize(); | |
362 | | |
363 | /** Places the barcode in a <CODE>PdfContentByte</CODE>. The | |
364 | * barcode is always placed at coordinates (0, 0). Use the | |
365 | * translation matrix to move it elsewhere.<p> | |
366 | * The bars and text are written in the following colors:<p> | |
367 | * <P><TABLE BORDER=1> | |
368 | * <TR> | |
369 | * <TH><P><CODE>barColor</CODE></TH> | |
370 | * <TH><P><CODE>textColor</CODE></TH> | |
371 | * <TH><P>Result</TH> | |
372 | * </TR> | |
373 | * <TR> | |
374 | * <TD><P><CODE>null</CODE></TD> | |
375 | * <TD><P><CODE>null</CODE></TD> | |
376 | * <TD><P>bars and text painted with current fill color</TD> | |
377 | * </TR> | |
378 | * <TR> | |
379 | * <TD><P><CODE>barColor</CODE></TD> | |
380 | * <TD><P><CODE>null</CODE></TD> | |
381 | * <TD><P>bars and text painted with <CODE>barColor</CODE></TD> | |
382 | * </TR> | |
383 | * <TR> | |
384 | * <TD><P><CODE>null</CODE></TD> | |
385 | * <TD><P><CODE>textColor</CODE></TD> | |
386 | * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD> | |
387 | * </TR> | |
388 | * <TR> | |
389 | * <TD><P><CODE>barColor</CODE></TD> | |
390 | * <TD><P><CODE>textColor</CODE></TD> | |
391 | * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD> | |
392 | * </TR> | |
393 | * </TABLE> | |
394 | * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed | |
395 | * @param barColor the color of the bars. It can be <CODE>null</CODE> | |
396 | * @param textColor the color of the text. It can be <CODE>null</CODE> | |
397 | * @return the dimensions the barcode occupies | |
398 | */ | |
399 | public abstract Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor); | |
400 | | |
401 | /** Creates a template with the barcode. | |
402 | * @param cb the <CODE>PdfContentByte</CODE> to create the template. It | |
403 | * serves no other use | |
404 | * @param barColor the color of the bars. It can be <CODE>null</CODE> | |
405 | * @param textColor the color of the text. It can be <CODE>null</CODE> | |
406 | * @return the template | |
407 | * @see #placeBarcode(PdfContentByte cb, Color barColor, Color textColor) | |
408 | */ | |
409 | public PdfTemplate createTemplateWithBarcode(PdfContentByte cb, Color barColor, Color textColor) { | |
410 | PdfTemplate tp = cb.createTemplate(0, 0); | |
411 | Rectangle rect = placeBarcode(tp, barColor, textColor); | |
412 |
1
1. createTemplateWithBarcode : removed call to com/lowagie/text/pdf/PdfTemplate::setBoundingBox → NO_COVERAGE |
tp.setBoundingBox(rect); |
413 |
1
1. createTemplateWithBarcode : mutated return of Object value for com/lowagie/text/pdf/Barcode::createTemplateWithBarcode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return tp; |
414 | } | |
415 | | |
416 | /** Creates an <CODE>Image</CODE> with the barcode. | |
417 | * @param cb the <CODE>PdfContentByte</CODE> to create the <CODE>Image</CODE>. It | |
418 | * serves no other use | |
419 | * @param barColor the color of the bars. It can be <CODE>null</CODE> | |
420 | * @param textColor the color of the text. It can be <CODE>null</CODE> | |
421 | * @return the <CODE>Image</CODE> | |
422 | * @see #placeBarcode(PdfContentByte cb, Color barColor, Color textColor) | |
423 | */ | |
424 | public Image createImageWithBarcode(PdfContentByte cb, Color barColor, Color textColor) { | |
425 | try { | |
426 |
1
1. createImageWithBarcode : mutated return of Object value for com/lowagie/text/pdf/Barcode::createImageWithBarcode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return Image.getInstance(createTemplateWithBarcode(cb, barColor, textColor)); |
427 | } | |
428 | catch (Exception e) { | |
429 | throw new ExceptionConverter(e); | |
430 | } | |
431 | } | |
432 | | |
433 | /** Creates a <CODE>java.awt.Image</CODE>. This image only | |
434 | * contains the bars without any text. | |
435 | * @param foreground the color of the bars | |
436 | * @param background the color of the background | |
437 | * @return the image | |
438 | */ | |
439 | public abstract java.awt.Image createAwtImage(Color foreground, Color background); | |
440 | | |
441 | /** Gets the amount of ink spreading. | |
442 | * @return the ink spreading | |
443 | * | |
444 | */ | |
445 | public float getInkSpreading() { | |
446 | return this.inkSpreading; | |
447 | } | |
448 | | |
449 | /** Sets the amount of ink spreading. This value will be subtracted | |
450 | * to the width of each bar. The actual value will depend on the ink | |
451 | * and the printing medium. | |
452 | * @param inkSpreading the ink spreading | |
453 | * | |
454 | */ | |
455 | public void setInkSpreading(float inkSpreading) { | |
456 | this.inkSpreading = inkSpreading; | |
457 | } | |
458 | ||
459 | /** | |
460 | * The alternate text to be used, if present. | |
461 | */ | |
462 | protected String altText; | |
463 | ||
464 | /** | |
465 | * Gets the alternate text. | |
466 | * @return the alternate text | |
467 | */ | |
468 | public String getAltText() { | |
469 | return this.altText; | |
470 | } | |
471 | ||
472 | /** | |
473 | * Sets the alternate text. If present, this text will be used instead of the | |
474 | * text derived from the supplied code. | |
475 | * @param altText the alternate text | |
476 | */ | |
477 | public void setAltText(String altText) { | |
478 | this.altText = altText; | |
479 | } | |
480 | | |
481 | } | |
Mutations | ||
412 |
1.1 |
|
413 |
1.1 |
|
426 |
1.1 |