This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
NKDRoyalMailBarcode.m
301 lines (256 loc) · 9.06 KB
/
NKDRoyalMailBarcode.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// -----------------------------------------------------------------------------------
// NKDPostnetBarcode.m
// -----------------------------------------------------------------------------------
// Created by Trevor Strohman <[email protected]> on Sun Apr 20 2003.
// -----------------------------------------------------------------------------------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
// -----------------------------------------------------------------------------------
// You may use and redistribute this source code without limitation
// -----------------------------------------------------------------------------------
#import "NKDRoyalMailBarcode.h"
// -----------------------------------------------------------------------------------
// Constants
// -----------------------------------------------------------------------------------
// Each hex constant represents a 4-bar code of the barcode corresponding to a single
// number or letter. The digits represent bars in this fashion:
// 0 - track bar (CBC_TRACK_BOTTOM to CBC_TRACK_TOP)
// 1 - descender (CBC_DESCENDER_BOTTOM to CBC_TRACK_TOP)
// 2 - ascender (CBC_TRACK_BOTTOM to CBC_ASCENDER_TOP)
// 3 - full bar (CBC_DESCENDER_BOTTOM to CBC_ASCENDER_TOP)
int numberCodes[] = {
0x0033, // 0
0x0123, // 1
0x0132, // 2
0x1023, // 3
0x1032, // 4
0x1122, // 5
0x0213, // 6
0x0303, // 7
0x0312, // 8
0x1203 // 9
};
int letterCodes[] = {
0x1212, // A
0x1302, // B
0x0231, // C
0x0321, // D
0x0330, // E
0x1221, // F
0x1230, // G
0x1320, // H
0x2013, // I
0x2103, // J
0x2112, // K
0x3003, // L
0x3012, // M
0x3102, // N
0x2031, // O
0x2121, // P
0x2130, // Q
0x3021, // R
0x3030, // S
0x3120, // T
0x2211, // U
0x2301, // V
0x2310, // W
0x3201, // X
0x3210, // Y
0x3300 // Z
};
#define CBC_OPEN_BRACKET (0x2)
#define CBC_CLOSE_BRACKET (0x3)
#define CBC_ASCENDER_MASK (0x2222)
#define CBC_DESCENDER_MASK (0x1111)
// ----------------------------------------------------------------------------------
// Internal Functions
// ----------------------------------------------------------------------------------
//
// Returns the descender bits of each bar, collapsed into a binary number.
// For instance, for the letter X (0x3201), the descender bits are 0x1001, which when
// collapsed become 0x9.
//
static int royalmail_collapseDescenders( int hexDigit ) {
int descenders = (hexDigit & 0x0001) |
(hexDigit & 0x0010)>>(4-1) |
(hexDigit & 0x0100)>>(8-2) |
(hexDigit & 0x1000)>>(12-3);
return descenders;
}
//
// Returns the descender bits of each bar, collapsed into a binary number.
// For instance, for the letter X (0x3201), the descender bits are 0x1100, which when
// collapsed become 0xC.
//
static int royalmail_collapseAscenders( int hexDigit ) {
int ascenders = (hexDigit & 0x0002)>>1 |
(hexDigit & 0x0020)>>(5-1) |
(hexDigit & 0x0200)>>(9-2) |
(hexDigit & 0x2000)>>(13-3);
return ascenders;
}
static int royalmail_characterDescriptor( unichar character ) {
int descriptor;
if( character >= 'A' && character <= 'Z' ) {
descriptor = letterCodes[character-'A'];
} else if ( character >= '0' && character <= '9' ) {
descriptor = numberCodes[character-'0'];
} else {
descriptor = 0;
}
return descriptor;
}
static int royalmail_barDescriptor( int descriptor, int bar ) {
int mask;
int shift;
int hexDigit = 0;
shift = (3-(bar/2))*4;
mask = ( 0xF << shift );
hexDigit = ( descriptor & mask ) >> shift;
return hexDigit;
}
static float royalmail_barTop( int hexDigit ) {
if( hexDigit & CBC_ASCENDER_MASK ) {
return CBC_ASCENDER_TOP;
} else {
return CBC_TRACK_TOP;
}
}
static float royalmail_barBottom( int hexDigit ) {
if( hexDigit & CBC_DESCENDER_MASK ) {
return CBC_DESCENDER_BOTTOM;
} else {
return CBC_TRACK_BOTTOM;
}
}
@implementation NKDRoyalMailBarcode
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
printsCaption: (BOOL)inPrints
// -----------------------------------------------------------------------------------
{
if (self = [super init])
{
if (!inContent)
{
[self release];
return nil;
}
[self setContent:inContent];
[self setBarWidth:CBC_BAR_WIDTH*CBC_INCHES_PER_MILLIMETER*kScreenResolution];
[self setPrintsCaption:NO];
// RM4SCC has a mandatory check digit
[self generateChecksum];
[self setHeight:CBC_ASCENDER_TOP*CBC_INCHES_PER_MILLIMETER*kScreenResolution];
// Calculate width based on number of bars needed to encode this content
[self calculateWidth];
}
return self;
}
// -----------------------------------------------------------------------------------
-(BOOL)printsCaption
// -----------------------------------------------------------------------------------
{
// RM4SCC barcodes NEVER,EVER use a caption
return NO;
}
// -----------------------------------------------------------------------------------
-(NSString *)initiator
// -----------------------------------------------------------------------------------
{
return @"10";
}
// -----------------------------------------------------------------------------------
-(NSString *)terminator
// -----------------------------------------------------------------------------------
{
return @"1";
}
// -----------------------------------------------------------------------------------
-(NSString *)_encodeChar:(char)inChar
// -----------------------------------------------------------------------------------
{
// Each RM4SCC character takes up 4 bars
return @"10101010";
}
// -----------------------------------------------------------------------------------
-(int)_barDescriptor:(int)index
// -----------------------------------------------------------------------------------
{
// barcode is 10(10101010){contentLength}(10101010)1
// 10 - initiator
// (10101010)* - digits
// (10101010) - checksum
// 1 - terminator
int contentLength = [[self content] length];
int hexDigit;
int descriptor = 0;
if( index == 0 ) {
descriptor = CBC_OPEN_BRACKET;
} else if ( index >= (contentLength+1)*8+2 ) {
descriptor = CBC_CLOSE_BRACKET;
} else {
int digit = (index-2)/8;
int bar = (index-2)%8;
if( digit != contentLength ) {
// regular content digit
hexDigit = royalmail_characterDescriptor( [[self content] characterAtIndex:digit] );
descriptor = royalmail_barDescriptor( hexDigit, bar );
} else {
// last digit is checksum
hexDigit = royalmail_characterDescriptor( [self checkDigit] );
descriptor = royalmail_barDescriptor( hexDigit, bar );
}
}
return descriptor;
}
// -----------------------------------------------------------------------------------
-(float)barTop:(int)index
// -----------------------------------------------------------------------------------
{
return royalmail_barTop( [self _barDescriptor:index] ) *
CBC_INCHES_PER_MILLIMETER *
kScreenResolution;
}
// -----------------------------------------------------------------------------------
-(float)barBottom:(int)index
// -----------------------------------------------------------------------------------
{
return royalmail_barBottom( [self _barDescriptor:index] ) *
CBC_INCHES_PER_MILLIMETER *
kScreenResolution;
}
// -----------------------------------------------------------------------------------
-(void)generateChecksum
// -----------------------------------------------------------------------------------
{
int i;
int ascenders = 0;
int descenders = 0;
int digitIndex = 0;
unichar character;
for( i=0; i<[[self content] length]; i++ ) {
unichar current = [[self content] characterAtIndex:i];
int descriptor = royalmail_characterDescriptor( current );
ascenders += royalmail_collapseAscenders( descriptor ) >> 1;
descenders += royalmail_collapseDescenders( descriptor ) >> 1;
ascenders %= 6;
descenders %= 6;
}
// The table in the Mailsort 700 publication lists indices as 1, 2, 3, 4, 5, 0 (6).
// The following computation is easier if we rotate the index order from 0, 1, 2, 3, 4, 5
// so we'll subtract one from each index and rotate the 0 around to the 5 position.
ascenders--;
descenders--;
if( ascenders < 0 )
ascenders = 5;
if( descenders < 0 )
descenders = 5;
digitIndex = (ascenders * 6) + descenders;
if( digitIndex < 10 ) {
character = digitIndex + '0';
} else {
character = digitIndex - 10 + 'A';
}
[self setCheckDigit:character];
}
@end