1 | /* | |
2 | * Copyright 2002 by Paulo Soares. | |
3 | * | |
4 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
5 | * (the "License"); you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
7 | * | |
8 | * Software distributed under the License is distributed on an "AS IS" basis, | |
9 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
10 | * for the specific language governing rights and limitations under the License. | |
11 | * | |
12 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
15 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
16 | * All Rights Reserved. | |
17 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
18 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
19 | * | |
20 | * Contributor(s): all the names of the contributors are added in the source code | |
21 | * where applicable. | |
22 | * | |
23 | * Alternatively, the contents of this file may be used under the terms of the | |
24 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
25 | * provisions of LGPL are applicable instead of those above. If you wish to | |
26 | * allow use of your version of this file only under the terms of the LGPL | |
27 | * License and not to allow others to use your version of this file under | |
28 | * the MPL, indicate your decision by deleting the provisions above and | |
29 | * replace them with the notice and other provisions required by the LGPL. | |
30 | * If you do not delete the provisions above, a recipient may use your version | |
31 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
32 | * | |
33 | * This library is free software; you can redistribute it and/or modify it | |
34 | * under the terms of the MPL as stated above or under the terms of the GNU | |
35 | * Library General Public License as published by the Free Software Foundation; | |
36 | * either version 2 of the License, or any later version. | |
37 | * | |
38 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
39 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
40 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
41 | * details. | |
42 | * | |
43 | * If you didn't download this code from the following link, you should check if | |
44 | * you aren't using an obsolete version: | |
45 | * http://www.lowagie.com/iText/ | |
46 | */ | |
47 | package com.lowagie.text.pdf; | |
48 | import java.awt.Canvas; | |
49 | import java.awt.Color; | |
50 | import java.awt.Image; | |
51 | import java.awt.image.MemoryImageSource; | |
52 | ||
53 | import com.lowagie.text.Rectangle; | |
54 | ||
55 | /** Implements the Postnet and Planet barcodes. The default parameters are: | |
56 | * <pre> | |
57 | *n = 72f / 22f; // distance between bars | |
58 | *x = 0.02f * 72f; // bar width | |
59 | *barHeight = 0.125f * 72f; // height of the tall bars | |
60 | *size = 0.05f * 72f; // height of the short bars | |
61 | *codeType = POSTNET; // type of code | |
62 | * </pre> | |
63 | * | |
64 | * @author Paulo Soares (psoares@consiste.pt) | |
65 | */ | |
66 | public class BarcodePostnet extends Barcode{ | |
67 | ||
68 | /** | |
69 | * The bars for each character. | |
70 | */ | |
71 | private static final byte[][] BARS = | |
72 | { | |
73 | {1, 1, 0, 0, 0}, | |
74 | {0, 0, 0, 1, 1}, | |
75 | {0, 0, 1, 0, 1}, | |
76 | {0, 0, 1, 1, 0}, | |
77 | {0, 1, 0, 0, 1}, | |
78 | {0, 1, 0, 1, 0}, | |
79 | {0, 1, 1, 0, 0}, | |
80 | {1, 0, 0, 0, 1}, | |
81 | {1, 0, 0, 1, 0}, | |
82 | {1, 0, 1, 0, 0} | |
83 | }; | |
84 | | |
85 | /** Creates new BarcodePostnet */ | |
86 | public BarcodePostnet() { | |
87 | n = 72f / 22f; // distance between bars | |
88 | x = 0.02f * 72f; // bar width | |
89 | barHeight = 0.125f * 72f; // height of the tall bars | |
90 | size = 0.05f * 72f; // height of the short bars | |
91 | codeType = POSTNET; // type of code | |
92 | } | |
93 | | |
94 | /** Creates the bars for Postnet. | |
95 | * @param text the code to be created without checksum | |
96 | * @return the bars | |
97 | */ | |
98 | public static byte[] getBarsPostnet(String text) { | |
99 | int total = 0; | |
100 |
4
1. getBarsPostnet : changed conditional boundary → NO_COVERAGE 2. getBarsPostnet : Changed increment from -1 to 1 → NO_COVERAGE 3. getBarsPostnet : Replaced integer subtraction with addition → NO_COVERAGE 4. getBarsPostnet : negated conditional → NO_COVERAGE |
for (int k = text.length() - 1; k >= 0; --k) { |
101 |
1
1. getBarsPostnet : Replaced integer subtraction with addition → NO_COVERAGE |
int n = text.charAt(k) - '0'; |
102 |
1
1. getBarsPostnet : Replaced integer addition with subtraction → NO_COVERAGE |
total += n; |
103 | } | |
104 |
4
1. getBarsPostnet : Replaced integer modulus with multiplication → NO_COVERAGE 2. getBarsPostnet : Replaced integer subtraction with addition → NO_COVERAGE 3. getBarsPostnet : Replaced integer modulus with multiplication → NO_COVERAGE 4. getBarsPostnet : Replaced integer addition with subtraction → NO_COVERAGE |
text += (char)(((10 - (total % 10)) % 10) + '0'); |
105 |
2
1. getBarsPostnet : Replaced integer multiplication with division → NO_COVERAGE 2. getBarsPostnet : Replaced integer addition with subtraction → NO_COVERAGE |
byte[] bars = new byte[text.length() * 5 + 2]; |
106 | bars[0] = 1; | |
107 |
1
1. getBarsPostnet : Replaced integer subtraction with addition → NO_COVERAGE |
bars[bars.length - 1] = 1; |
108 |
3
1. getBarsPostnet : changed conditional boundary → NO_COVERAGE 2. getBarsPostnet : Changed increment from 1 to -1 → NO_COVERAGE 3. getBarsPostnet : negated conditional → NO_COVERAGE |
for (int k = 0; k < text.length(); ++k) { |
109 |
1
1. getBarsPostnet : Replaced integer subtraction with addition → NO_COVERAGE |
int c = text.charAt(k) - '0'; |
110 |
3
1. getBarsPostnet : Replaced integer multiplication with division → NO_COVERAGE 2. getBarsPostnet : Replaced integer addition with subtraction → NO_COVERAGE 3. getBarsPostnet : removed call to java/lang/System::arraycopy → NO_COVERAGE |
System.arraycopy(BARS[c], 0, bars, k * 5 + 1, 5); |
111 | } | |
112 |
1
1. getBarsPostnet : mutated return of Object value for com/lowagie/text/pdf/BarcodePostnet::getBarsPostnet to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return bars; |
113 | } | |
114 | ||
115 | /** Gets the maximum area that the barcode and the text, if | |
116 | * any, will occupy. The lower left corner is always (0, 0). | |
117 | * @return the size the barcode occupies. | |
118 | */ | |
119 | public Rectangle getBarcodeSize() { | |
120 |
5
1. getBarcodeSize : Replaced integer addition with subtraction → NO_COVERAGE 2. getBarcodeSize : Replaced integer multiplication with division → NO_COVERAGE 3. getBarcodeSize : Replaced integer addition with subtraction → NO_COVERAGE 4. getBarcodeSize : Replaced float multiplication with division → NO_COVERAGE 5. getBarcodeSize : Replaced float addition with subtraction → NO_COVERAGE |
float width = ((code.length() + 1) * 5 + 1) * n + x; |
121 |
1
1. getBarcodeSize : mutated return of Object value for com/lowagie/text/pdf/BarcodePostnet::getBarcodeSize to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return new Rectangle(width, barHeight); |
122 | } | |
123 | | |
124 | /** Places the barcode in a <CODE>PdfContentByte</CODE>. The | |
125 | * barcode is always placed at coordinates (0, 0). Use the | |
126 | * translation matrix to move it elsewhere.<p> | |
127 | * The bars and text are written in the following colors:<p> | |
128 | * <P><TABLE BORDER=1> | |
129 | * <TR> | |
130 | * <TH><P><CODE>barColor</CODE></TH> | |
131 | * <TH><P><CODE>textColor</CODE></TH> | |
132 | * <TH><P>Result</TH> | |
133 | * </TR> | |
134 | * <TR> | |
135 | * <TD><P><CODE>null</CODE></TD> | |
136 | * <TD><P><CODE>null</CODE></TD> | |
137 | * <TD><P>bars and text painted with current fill color</TD> | |
138 | * </TR> | |
139 | * <TR> | |
140 | * <TD><P><CODE>barColor</CODE></TD> | |
141 | * <TD><P><CODE>null</CODE></TD> | |
142 | * <TD><P>bars and text painted with <CODE>barColor</CODE></TD> | |
143 | * </TR> | |
144 | * <TR> | |
145 | * <TD><P><CODE>null</CODE></TD> | |
146 | * <TD><P><CODE>textColor</CODE></TD> | |
147 | * <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD> | |
148 | * </TR> | |
149 | * <TR> | |
150 | * <TD><P><CODE>barColor</CODE></TD> | |
151 | * <TD><P><CODE>textColor</CODE></TD> | |
152 | * <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD> | |
153 | * </TR> | |
154 | * </TABLE> | |
155 | * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed | |
156 | * @param barColor the color of the bars. It can be <CODE>null</CODE> | |
157 | * @param textColor the color of the text. It can be <CODE>null</CODE> | |
158 | * @return the dimensions the barcode occupies | |
159 | */ | |
160 | public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) { | |
161 |
1
1. placeBarcode : negated conditional → NO_COVERAGE |
if (barColor != null) |
162 |
1
1. placeBarcode : removed call to com/lowagie/text/pdf/PdfContentByte::setColorFill → NO_COVERAGE |
cb.setColorFill(barColor); |
163 | byte[] bars = getBarsPostnet(code); | |
164 | byte flip = 1; | |
165 |
1
1. placeBarcode : negated conditional → NO_COVERAGE |
if (codeType == PLANET) { |
166 | flip = 0; | |
167 | bars[0] = 0; | |
168 |
1
1. placeBarcode : Replaced integer subtraction with addition → NO_COVERAGE |
bars[bars.length - 1] = 0; |
169 | } | |
170 | float startX = 0; | |
171 | for (byte bar : bars) { | |
172 |
3
1. placeBarcode : Replaced float subtraction with addition → NO_COVERAGE 2. placeBarcode : negated conditional → NO_COVERAGE 3. placeBarcode : removed call to com/lowagie/text/pdf/PdfContentByte::rectangle → NO_COVERAGE |
cb.rectangle(startX, 0, x - inkSpreading, bar == flip ? barHeight : size); |
173 |
1
1. placeBarcode : Replaced float addition with subtraction → NO_COVERAGE |
startX += n; |
174 | } | |
175 |
1
1. placeBarcode : removed call to com/lowagie/text/pdf/PdfContentByte::fill → NO_COVERAGE |
cb.fill(); |
176 |
1
1. placeBarcode : mutated return of Object value for com/lowagie/text/pdf/BarcodePostnet::placeBarcode to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return getBarcodeSize(); |
177 | } | |
178 | | |
179 | /** Creates a <CODE>java.awt.Image</CODE>. This image only | |
180 | * contains the bars without any text. | |
181 | * @param foreground the color of the bars | |
182 | * @param background the color of the background | |
183 | * @return the image | |
184 | * | |
185 | */ | |
186 | public java.awt.Image createAwtImage(Color foreground, Color background) { | |
187 | int f = foreground.getRGB(); | |
188 | int g = background.getRGB(); | |
189 | Canvas canvas = new Canvas(); | |
190 | int barWidth = (int)x; | |
191 |
2
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : negated conditional → NO_COVERAGE |
if (barWidth <= 0) |
192 | barWidth = 1; | |
193 | int barDistance = (int)n; | |
194 |
2
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : negated conditional → NO_COVERAGE |
if (barDistance <= barWidth) |
195 |
1
1. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE |
barDistance = barWidth + 1; |
196 | int barShort = (int)size; | |
197 |
2
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : negated conditional → NO_COVERAGE |
if (barShort <= 0) |
198 | barShort = 1; | |
199 | int barTall = (int)barHeight; | |
200 |
2
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : negated conditional → NO_COVERAGE |
if (barTall <= barShort) |
201 |
1
1. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE |
barTall = barShort + 1; |
202 |
5
1. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 2. createAwtImage : Replaced integer multiplication with division → NO_COVERAGE 3. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 4. createAwtImage : Replaced integer multiplication with division → NO_COVERAGE 5. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE |
int width = ((code.length() + 1) * 5 + 1) * barDistance + barWidth; |
203 |
1
1. createAwtImage : Replaced integer multiplication with division → NO_COVERAGE |
int[] pix = new int[width * barTall]; |
204 | byte[] bars = getBarsPostnet(code); | |
205 | byte flip = 1; | |
206 |
1
1. createAwtImage : negated conditional → NO_COVERAGE |
if (codeType == PLANET) { |
207 | flip = 0; | |
208 | bars[0] = 0; | |
209 |
1
1. createAwtImage : Replaced integer subtraction with addition → NO_COVERAGE |
bars[bars.length - 1] = 0; |
210 | } | |
211 | int idx = 0; | |
212 | for (byte bar : bars) { | |
213 |
1
1. createAwtImage : negated conditional → NO_COVERAGE |
boolean dot = (bar == flip); |
214 |
3
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : Changed increment from 1 to -1 → NO_COVERAGE 3. createAwtImage : negated conditional → NO_COVERAGE |
for (int j = 0; j < barDistance; ++j) { |
215 |
4
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 3. createAwtImage : negated conditional → NO_COVERAGE 4. createAwtImage : negated conditional → NO_COVERAGE |
pix[idx + j] = ((dot && j < barWidth) ? f : g); |
216 | } | |
217 |
1
1. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE |
idx += barDistance; |
218 | } | |
219 |
2
1. createAwtImage : Replaced integer subtraction with addition → NO_COVERAGE 2. createAwtImage : Replaced integer multiplication with division → NO_COVERAGE |
int limit = width * (barTall - barShort); |
220 |
3
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 3. createAwtImage : negated conditional → NO_COVERAGE |
for (int k = width; k < limit; k += width) |
221 |
1
1. createAwtImage : removed call to java/lang/System::arraycopy → NO_COVERAGE |
System.arraycopy(pix, 0, pix, k, width); |
222 | idx = limit; | |
223 |
3
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : Changed increment from 1 to -1 → NO_COVERAGE 3. createAwtImage : negated conditional → NO_COVERAGE |
for (int k = 0; k < bars.length; ++k) { |
224 |
2
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : negated conditional → NO_COVERAGE |
for (int j = 0; j < barDistance; ++j) { |
225 |
3
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 3. createAwtImage : negated conditional → NO_COVERAGE |
pix[idx + j] = ((j < barWidth) ? f : g); |
226 | } | |
227 |
1
1. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE |
idx += barDistance; |
228 | } | |
229 |
4
1. createAwtImage : changed conditional boundary → NO_COVERAGE 2. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 3. createAwtImage : Replaced integer addition with subtraction → NO_COVERAGE 4. createAwtImage : negated conditional → NO_COVERAGE |
for (int k = limit + width; k < pix.length; k += width) |
230 |
1
1. createAwtImage : removed call to java/lang/System::arraycopy → NO_COVERAGE |
System.arraycopy(pix, limit, pix, k, width); |
231 | Image img = canvas.createImage(new MemoryImageSource(width, barTall, pix, 0, width)); | |
232 | | |
233 |
1
1. createAwtImage : mutated return of Object value for com/lowagie/text/pdf/BarcodePostnet::createAwtImage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return img; |
234 | } | |
235 | } | |
Mutations | ||
100 |
1.1 2.2 3.3 4.4 |
|
101 |
1.1 |
|
102 |
1.1 |
|
104 |
1.1 2.2 3.3 4.4 |
|
105 |
1.1 2.2 |
|
107 |
1.1 |
|
108 |
1.1 2.2 3.3 |
|
109 |
1.1 |
|
110 |
1.1 2.2 3.3 |
|
112 |
1.1 |
|
120 |
1.1 2.2 3.3 4.4 5.5 |
|
121 |
1.1 |
|
161 |
1.1 |
|
162 |
1.1 |
|
165 |
1.1 |
|
168 |
1.1 |
|
172 |
1.1 2.2 3.3 |
|
173 |
1.1 |
|
175 |
1.1 |
|
176 |
1.1 |
|
191 |
1.1 2.2 |
|
194 |
1.1 2.2 |
|
195 |
1.1 |
|
197 |
1.1 2.2 |
|
200 |
1.1 2.2 |
|
201 |
1.1 |
|
202 |
1.1 2.2 3.3 4.4 5.5 |
|
203 |
1.1 |
|
206 |
1.1 |
|
209 |
1.1 |
|
213 |
1.1 |
|
214 |
1.1 2.2 3.3 |
|
215 |
1.1 2.2 3.3 4.4 |
|
217 |
1.1 |
|
219 |
1.1 2.2 |
|
220 |
1.1 2.2 3.3 |
|
221 |
1.1 |
|
223 |
1.1 2.2 3.3 |
|
224 |
1.1 2.2 |
|
225 |
1.1 2.2 3.3 |
|
227 |
1.1 |
|
229 |
1.1 2.2 3.3 4.4 |
|
230 |
1.1 |
|
233 |
1.1 |