Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 308 lines (275 sloc) 9.169 kb
297ae856 » Chris
2010-04-21 initial commit
1 // -----------------------------------------------------------------------------------
2 // NKDEAN13Barcode.m
3 // -----------------------------------------------------------------------------------
4 // Created by Jeff LaMarche on Fri May 10 2002.
eb2a23e3 » dalewking
2010-08-17 Fix issue with EAN-13 barcodes that start with 2. See http://code.goo…
5 // �2002 Naked Software. All rights reserved.
297ae856 » Chris
2010-04-21 initial commit
6 // -----------------------------------------------------------------------------------
7 // THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
8 // -----------------------------------------------------------------------------------
9 // You may use and redistribute this source code without limitation
10 // -----------------------------------------------------------------------------------
11 #import "NKDEAN13Barcode.h"
12
13
14 @implementation NKDEAN13Barcode
15 // -----------------------------------------------------------------------------------
16 -(id)initWithContent: (NSString *)inContent
17 printsCaption: (BOOL)inPrints;
18 // -----------------------------------------------------------------------------------
19 {
20
21 NSRange range = NSMakeRange(0, 12);
22 char *tempString;
23
24 self = [super initWithContent: inContent
25 printsCaption:inPrints];
26
27 [self setContent:inContent];
28
29 if ([[self content] length] >= 13)
30 {
e17cf1e7 » mdestagnol
2011-03-18 FIXED › Compatibility with iOS4 (replaced deprecated methods)
31 tempString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae856 » Chris
2010-04-21 initial commit
32 checkDigit = tempString[12];
33 [self setContent:[[self content] substringWithRange:range]];
34 //[self generateChecksum];
35 }
36 else
37 [self generateChecksum];
38
39 [self calculateWidth];
40 return self;
41 }
42 // -----------------------------------------------------------------------------------
43 -(NSString *)barcode
44 // -----------------------------------------------------------------------------------
45 {
46 NSMutableString *theReturn = [NSMutableString stringWithString:@""];
47 int i;
48 char *contentString;
49
e17cf1e7 » mdestagnol
2011-03-18 FIXED › Compatibility with iOS4 (replaced deprecated methods)
50 contentString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae856 » Chris
2010-04-21 initial commit
51
52
53 // First six characters after system digit encoded with left-hand encoding,
54 // parity based on value of system digit
55 // --------------------------------------------------------
56 for (i = 1; i < 7; i++)
57 {
58 if ([self _parityForSystemDigit:contentString[0] forIndex: i] == ODD_PARITY)
59 [theReturn appendString:[self _encodeChar:contentString[i]]];
60 else
61 [theReturn appendString:[[self _encodeChar:contentString[i]] swapParity]];
62 }
63 // Center guard bars
64 // -----------------
65 [theReturn appendString:@"01010"];
66
67 // Next six characters (next five digits plus check digit)
68 // are encoded with right hand encoding.
69 // --------------------------------------------------------
70 for (i = 7; i < 12; i++)
71 [theReturn appendString:[[self _encodeChar:contentString[i]] swapHandedness]];
72
73 // And encode the check digit
74 // --------------------------
75 [theReturn appendString:[[self _encodeChar:checkDigit] swapHandedness]];
76
77 return theReturn;
78 }
79 // -----------------------------------------------------------------------------------
80 -(BOOL)_parityForSystemDigit: (char) systemDigit
81 forIndex: (int) index
82 // -----------------------------------------------------------------------------------
83 {
84
85 switch (systemDigit)
86 {
87 case '0':
88 return ODD_PARITY;
89 case '1':
90 switch (index)
91 {
92 case 1:
93 case 2:
94 case 4:
95 return ODD_PARITY;
96 default:
97 return EVEN_PARITY;
98 }
99 case '2':
100 switch (index)
101 {
102 case 1:
eb2a23e3 » dalewking
2010-08-17 Fix issue with EAN-13 barcodes that start with 2. See http://code.goo…
103 case 2:
104 case 5:
297ae856 » Chris
2010-04-21 initial commit
105 return ODD_PARITY;
106 default:
107 return EVEN_PARITY;
108 }
109 case '3':
110 switch (index)
111 {
112 case 1:
113 case 2:
114 case 6:
115 return ODD_PARITY;
116 default:
117 return EVEN_PARITY;
118 }
119 case '4':
120 switch (index)
121 {
122 case 1:
123 case 3:
124 case 4:
125 return ODD_PARITY;
126 default:
127 return EVEN_PARITY;
128 }
129 case '5':
130 switch (index)
131 {
132 case 1:
133 case 4:
134 case 5:
135 return ODD_PARITY;
136 default:
137 return EVEN_PARITY;
138 }
139 case '6':
140 switch (index)
141 {
142 case 1:
143 case 5:
144 case 6:
145 return ODD_PARITY;
146 default:
147 return EVEN_PARITY;
148 }
149 case '7':
150 switch (index)
151 {
152 case 1:
153 case 3:
154 case 5:
155 return ODD_PARITY;
156 default:
157 return EVEN_PARITY;
158 }
159 case '8':
160 switch (index)
161 {
162 case 1:
163 case 3:
164 case 6:
165 return ODD_PARITY;
166 default:
167 return EVEN_PARITY;
168 }
169 case '9':
170 switch (index)
171 {
172 case 1:
173 case 4:
174 case 6:
175 return ODD_PARITY;
176 default:
177 return EVEN_PARITY;
178 }
179 break;
180 }
181 // shouldn't ever get here, but...
182 return ODD_PARITY;
183 }
184 // -----------------------------------------------------------------------------------
185 -(int)digitsToRight
186 // -----------------------------------------------------------------------------------
187 {
188 return 0;
189 }
190 // -----------------------------------------------------------------------------------
191 -(NSString *)rightCaption
192 // -----------------------------------------------------------------------------------
193 {
194 return @"";
195 }
196 // -----------------------------------------------------------------------------------
197 -(NSString *)caption
198 // -----------------------------------------------------------------------------------
199 {
200 return [NSString stringWithFormat: @" %@\t%@%c", [content substringWithRange:NSMakeRange(1, 6)],
201 [content substringWithRange:NSMakeRange(7, 5)], [self checkDigit]];
202 }
203 /*
204 // -----------------------------------------------------------------------------------
205 -(void)generateChecksum
206 // -----------------------------------------------------------------------------------
207 {
208
209 {
210 int oddSum = 0;
211 int evenSum = 0;
212 int i, checkInt;
e17cf1e7 » mdestagnol
2011-03-18 FIXED › Compatibility with iOS4 (replaced deprecated methods)
213 char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae856 » Chris
2010-04-21 initial commit
214
215 if (strlen(code) == 11)
216 {
217 for (i=10; i > 0; i-=2)
218 {
219 oddSum += (code[i] - '0');
220 evenSum += (code[i-1] - '0');
221 }
222
223 checkInt = 10 - (((oddSum * 3) + evenSum) % 10);
224 if (checkInt == 10)
225 checkInt = 0;
226
227 checkDigit = checkInt + '0';
228 }
229 }
230
231
232
233 int oddSum = 0;
234 int evenSum = 0;
235 int i, checkInt;
e17cf1e7 » mdestagnol
2011-03-18 FIXED › Compatibility with iOS4 (replaced deprecated methods)
236 char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae856 » Chris
2010-04-21 initial commit
237
238 if (strlen(code) == 12)
239 {
240 for (i=11; i > 1; i-=2)
241 {
242 oddSum += (code[i] - '0');
243 evenSum += (code[i-1] - '0');
244 }
245
246 checkInt = 10 - (((oddSum * 3) + evenSum) % 10);
247 if (checkInt == 10)
248 checkInt = 0;
249
250 checkDigit = checkInt + '0';
251 }
252 }
253 */
254 // -----------------------------------------------------------------------------------
255 -(void)generateChecksum
256 // -----------------------------------------------------------------------------------
257 {
258 int oddSum = 0;
259 int evenSum = 0;
260 int i, checkInt;
261 int even = 1;
e17cf1e7 » mdestagnol
2011-03-18 FIXED › Compatibility with iOS4 (replaced deprecated methods)
262 //char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
263 char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae856 » Chris
2010-04-21 initial commit
264 if (strlen(code) == 12)
265 {
266 i = strlen(code);
267 while (i-- > 0) {
268 if (even) evenSum += code[i]-'0';
269 else oddSum += code[i]-'0';
270 even = !even;
271 }
272
273 i = (3*evenSum + oddSum) % 10;
274 checkInt = (10-i) % 10; // complement to 10
275
276 checkDigit = checkInt + '0';
277 }
278 }
279
280 // -----------------------------------------------------------------------------------
281 -(BOOL) isContentValid
282 // -----------------------------------------------------------------------------------
283 {
284
285 int i;
286 char *contentString;
287 char tempCheck=0;
288
e17cf1e7 » mdestagnol
2011-03-18 FIXED › Compatibility with iOS4 (replaced deprecated methods)
289 contentString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae856 » Chris
2010-04-21 initial commit
290
291 if (strlen(contentString) != 12)
292 return NO;
293
294 for (i = 0; i < strlen(contentString); i++)
295 if ([[self _encodeChar:contentString[i]] isEqual:@""])
296 return NO;
297
298 tempCheck = [self checkDigit];
299 [self generateChecksum];
300 if (tempCheck != [self checkDigit])
301 {
302 [self setCheckDigit:tempCheck];
303 return NO;
304 }
305
306 return YES;
307 }
308 @end
Something went wrong with that request. Please try again.