forked from netshade/Cocoa-Touch-Barcodes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NKDPostnetBarcode.m
executable file
·224 lines (203 loc) · 6.92 KB
/
NKDPostnetBarcode.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// -----------------------------------------------------------------------------------
// NKDPostnetBarcode.m
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Sun May 05 2002.
// �2002 Naked Software. All rights reserved.
// -----------------------------------------------------------------------------------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
// -----------------------------------------------------------------------------------
// You may use and redistribute this source code without limitation
// -----------------------------------------------------------------------------------
#import "NKDPostnetBarcode.h"
@implementation NKDPostnetBarcode
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
printsCaption: (BOOL)inPrints
// -----------------------------------------------------------------------------------
{
if (self = [super init])
{
if (!inContent)
{
[self release];
return nil;
}
[self setContent:inContent];
[self setBarWidth:.015*screenResolution()];
[self setPrintsCaption:inPrints];
// Postnet has a mandatory check digit
[self generateChecksum];
// .125 inch bar height is about the middle of the allowed values
[self setHeight:.125*screenResolution()];
// Calculate width based on number of bars needed to encode this content
[self calculateWidth];
}
return self;
}
// -----------------------------------------------------------------------------------
-(BOOL)printsCaption
// -----------------------------------------------------------------------------------
{
// PostNet barcodes NEVER,EVER use a caption
return NO;
}
// -----------------------------------------------------------------------------------
-(NSString *)initiator
// -----------------------------------------------------------------------------------
{
return @"100";
}
// -----------------------------------------------------------------------------------
-(NSString *)terminator
// -----------------------------------------------------------------------------------
{
return @"1";
}
// -----------------------------------------------------------------------------------
-(NSString *)_encodeChar:(char)inChar
// -----------------------------------------------------------------------------------
{
// Each postnet character takes up 5 bars
return @"100100100100100";
}
// -----------------------------------------------------------------------------------
-(float)barTop:(int)index
// -----------------------------------------------------------------------------------
{
int digit, bar;
// Postnet works by varying the height of the bars - there are two heights, tall
// and short. The terminator and initiator bars are always single tall bars
// Look for and deal with terminator and initiator first
if (index == 0)
return TALL_BAR * screenResolution();
if (index >= ([[self completeBarcode] length] - 1))
return TALL_BAR * screenResolution();
// Now, figure out which digit we're encoding, and which bar of that digit
digit = (int)((index-3) / 15);
bar = (index - (digit * 15)) / 3;
return [self _heightForDigit:digit andBar:bar] * screenResolution();
}
// -----------------------------------------------------------------------------------
-(float)_heightForDigit:(int)index
andBar:(int)bar
// -----------------------------------------------------------------------------------
{
char * barcode = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
char check;
check = (index >= strlen(barcode)) ? checkDigit : barcode[index];
switch (check)
{
case '1':
switch (bar)
{
case 4:
case 5:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '2':
switch (bar)
{
case 3:
case 5:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '3':
switch (bar)
{
case 3:
case 4:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '4':
switch (bar)
{
case 2:
case 5:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '5':
switch (bar)
{
case 2:
case 4:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '6':
switch (bar)
{
case 2:
case 3:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '7':
switch (bar)
{
case 1:
case 5:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '8':
switch (bar)
{
case 1:
case 4:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '9':
switch (bar)
{
case 1:
case 3:
return TALL_BAR;
default:
return SHORT_BAR;
}
case '0':
switch (bar)
{
case 1:
case 2:
return TALL_BAR;
default:
return SHORT_BAR;
}
}
return 0;
}
// -----------------------------------------------------------------------------------
-(void)generateChecksum
// -----------------------------------------------------------------------------------
{
char * code = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
int i, sum=0;
for (i = 0; i < strlen(code); i++)
sum += code[i] - '0';
[self setCheckDigit: (sum%10 == 0) ? '0': (10-(sum%10)) + '0'];
}
// Method contributed by Nik Sands <[email protected]>
// -----------------------------------------------------------------------------------
-(BOOL) isContentValid
// -----------------------------------------------------------------------------------
{
return ( [[[self content] stringByTrimmingCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]] isEqualToString:@""]
&& ( [[self content] length] == 5
|| [[self content] length] == 9
|| [[self content] length] == 11 ));
}
@end