forked from netshade/Cocoa-Touch-Barcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NKDBarcode.m
395 lines (371 loc) · 15.2 KB
/
NKDBarcode.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// -----------------------------------------------------------------------------------
// NKDBarcode.m
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Wed May 01 2002.
// Copyright (c) 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 "NKDBarcode.h"
#import <UIKit/UIKit.h>
@implementation NKDBarcode
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
// -----------------------------------------------------------------------------------
{
return [self initWithContent: inContent
printsCaption: YES];
}
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
printsCaption: (BOOL)inPrints
// -----------------------------------------------------------------------------------
{
return [self initWithContent:inContent
printsCaption:inPrints
andBarWidth:.013*kScreenResolution
andHeight:.5*kScreenResolution
andFontSize:6.0
andCheckDigit:(char)-1];
}
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
printsCaption: (BOOL)inPrints
andBarWidth: (float)inBarWidth
andHeight: (float)inHeight
andFontSize: (float)inFontSize
andCheckDigit: (char)inDigit
// -----------------------------------------------------------------------------------
{
if (self = [super init])
{
if (!inContent)
{
[self release];
return nil;
}
[self setContent:inContent];
[self setBarWidth:inBarWidth];
[self setPrintsCaption:inPrints];
//if (![self printsCaption])
[self setHeight:inHeight];
//else
[self setHeight:inHeight + (captionHeight*kScreenResolution)];
// Calculate width based on number of bars needed to encode this content
[self calculateWidth];
[self setFontSize: inFontSize];
// Check digit of -1 means no check digit - couldn't use 0 because
// 0 could be a valid check digit. Typically calculated, not set
// outside of the class
[self setCheckDigit: inDigit];
}
return self;
}
// -----------------------------------------------------------------------------------
-(void)setContent:(NSString *)inContent
// -----------------------------------------------------------------------------------
{
[content autorelease];
content = [inContent retain];
}
// -----------------------------------------------------------------------------------
-(void)setHeight:(float)inHeight
// -----------------------------------------------------------------------------------
{
height = inHeight;
}
// -----------------------------------------------------------------------------------
-(void)setWidth:(float)inWidth
// -----------------------------------------------------------------------------------
{
width = inWidth;
}
// -----------------------------------------------------------------------------------
-(void)calculateWidth
// -----------------------------------------------------------------------------------
{
[self setWidth:[[self completeBarcode] length] * [self barWidth]];
}
// -----------------------------------------------------------------------------------
-(void)setCheckDigit:(char)inCheckDigit
// -----------------------------------------------------------------------------------
{
checkDigit = inCheckDigit;
}
// -----------------------------------------------------------------------------------
-(void)setPrintsCaption:(BOOL)inPrints
// -----------------------------------------------------------------------------------
{
printsCaption = inPrints;
}
// -----------------------------------------------------------------------------------
-(void)setBarWidth:(float)inBarWidth
// -----------------------------------------------------------------------------------
{
barWidth = inBarWidth;
}
// -----------------------------------------------------------------------------------
-(NSString *)content
// -----------------------------------------------------------------------------------
{
return content;
}
// -----------------------------------------------------------------------------------
-(float)height
// -----------------------------------------------------------------------------------
{
return height;
}
// -----------------------------------------------------------------------------------
-(float)width
// -----------------------------------------------------------------------------------
{
return width;
}
// -----------------------------------------------------------------------------------
-(char)checkDigit
// -----------------------------------------------------------------------------------
{
return checkDigit;
}
// -----------------------------------------------------------------------------------
-(BOOL)printsCaption
// -----------------------------------------------------------------------------------
{
return printsCaption;
}
// -----------------------------------------------------------------------------------
-(float)barWidth
// -----------------------------------------------------------------------------------
{
return barWidth;
}
// -----------------------------------------------------------------------------------
-(BOOL)isSizeValid
// -----------------------------------------------------------------------------------
{
return YES;
}
// -----------------------------------------------------------------------------------
-(void)generateChecksum
// -----------------------------------------------------------------------------------
{
// Stub
}
// -----------------------------------------------------------------------------------
-(BOOL) isContentValid
// -----------------------------------------------------------------------------------
{
int i;
char *contentString;
contentString = (char *)[[self content] lossyCString];
for (i = 0; i < strlen(contentString); i++)
if ([[self _encodeChar:contentString[i]] isEqual:@""])
return NO;
return YES;
}
// -----------------------------------------------------------------------------------
-(NSString *)initiator
// -----------------------------------------------------------------------------------
{
return @"";
}
// -----------------------------------------------------------------------------------
-(NSString *)terminator
// -----------------------------------------------------------------------------------
{
return @"";
}
// -----------------------------------------------------------------------------------
-(NSString *)_encodeChar:(char)inChar
// -----------------------------------------------------------------------------------
{
// Subclasses MUST override - we'll encode dummy character for testing
return @"1010";
}
// -----------------------------------------------------------------------------------
-(NSString *)barcode
// -----------------------------------------------------------------------------------
{
NSMutableString *theReturn = [NSMutableString stringWithString:@""];
int i;
char *contentString;
contentString = (char *)[[self content] lossyCString];
for (i = 0; i < strlen(contentString); i++)
[theReturn appendString:[self _encodeChar:contentString[i]]];
if (checkDigit != -1)
[theReturn appendString:[self _encodeChar:checkDigit]];
return theReturn;
}
// -----------------------------------------------------------------------------------
-(NSString *)completeBarcode
// -----------------------------------------------------------------------------------
{
return [NSString stringWithFormat:@"%@%@%@", [self initiator], [self barcode], [self terminator]];
}
// -----------------------------------------------------------------------------------
-(void)setCaptionHeight:(float)inHeight
// -----------------------------------------------------------------------------------
{
captionHeight = inHeight;
}
// -----------------------------------------------------------------------------------
-(float)captionHeight
// -----------------------------------------------------------------------------------
{
return captionHeight;
}
// -----------------------------------------------------------------------------------
-(void)setFontSize:(float)inSize
// -----------------------------------------------------------------------------------
{
UIFont *font;
fontSize = inSize;
font = [UIFont systemFontOfSize:[self fontSize]];
[self setCaptionHeight: ([font capHeight]/72) + (.35 * ([font capHeight]/72))];
}
// -----------------------------------------------------------------------------------
-(float) fontSize
// -----------------------------------------------------------------------------------
{
return fontSize;
}
// -----------------------------------------------------------------------------------
-(int)digitsToLeft
// -----------------------------------------------------------------------------------
{
return 0;
}
// -----------------------------------------------------------------------------------
-(int)digitsToRight
// -----------------------------------------------------------------------------------
{
return 0;
}
// -----------------------------------------------------------------------------------
-(NSString *)leftCaption
// -----------------------------------------------------------------------------------
{
return [content substringWithRange:NSMakeRange(0, [self digitsToLeft])];
}
// -----------------------------------------------------------------------------------
-(NSString *)rightCaption
// -----------------------------------------------------------------------------------
{
return [content substringWithRange:NSMakeRange([content length] - [self digitsToRight], [self digitsToRight])];
}
// -----------------------------------------------------------------------------------
-(NSString *)caption
// -----------------------------------------------------------------------------------
{
if ( ([self digitsToRight] == 0) && ([self digitsToLeft] == 0))
return content;
else
return [content substringWithRange:NSMakeRange([self digitsToLeft],
[content length] - [self digitsToLeft] - [self digitsToRight])];
}
// -----------------------------------------------------------------------------------
-(float)barBottom:(int)index
// -----------------------------------------------------------------------------------
{
if ([self printsCaption])
return captionHeight * kScreenResolution;
else
return 0.0;
}
// -----------------------------------------------------------------------------------
-(float)barTop:(int)index
// -----------------------------------------------------------------------------------
{
return [self height];
}
// -----------------------------------------------------------------------------------
-(float)firstBar
// -----------------------------------------------------------------------------------
{
return 0;
}
// -----------------------------------------------------------------------------------
-(float)lastBar
// -----------------------------------------------------------------------------------
{
return [self width] - [self barWidth];
}
// -----------------------------------------------------------------------------------
-(NSString *)description
// -----------------------------------------------------------------------------------
{
return [NSString stringWithFormat:@"\n\tBarcode Class: %@\n\tContent: %@\n\tCheck Digit:%c\n\tWidth:%f\n\t%Height:%f\n\tBar Width:%f\n\tFont Size:%f\n\tCaption Height:%f",
[self class],
[self content],
[self checkDigit],
[self width],
[self height],
[self barWidth],
[self fontSize],
[self captionHeight]];
}
// -----------------------------------------------------------------------------------
- (BOOL)isEqual:(id)anObject
// -----------------------------------------------------------------------------------
{
NKDBarcode *compare;
compare = anObject;
return ([[self content] isEqual:[compare content]]);
}
// -----------------------------------------------------------------------------------
- (id)copyWithZone:(NSZone *)zone
// -----------------------------------------------------------------------------------
{
NKDBarcode *ret = [[[self class] alloc] initWithContent: [self content]
printsCaption: [self printsCaption]
andBarWidth: [self barWidth]
andHeight: [self height]
andFontSize: [self fontSize]
andCheckDigit: [self checkDigit]];
[ret calculateWidth];
return ret;
}
// -----------------------------------------------------------------------------------
- (id)initWithCoder:(NSCoder *)coder
// -----------------------------------------------------------------------------------
{
// Since we subclass NSObject, this call to super
// init is not necessary, but it's good form to
// include it, as it is possible that someday
// NSObject's -init method will do something
// -----------------------------------------------
self = [super init];
[self setContent:[coder decodeObject]];
[coder decodeValueOfObjCType:@encode(BOOL) at:&printsCaption];
[coder decodeValueOfObjCType:@encode(float) at:&barWidth];
[coder decodeValueOfObjCType:@encode(float) at:&width];
[coder decodeValueOfObjCType:@encode(float) at:&height];
[coder decodeValueOfObjCType:@encode(float) at:&fontSize];
[coder decodeValueOfObjCType:@encode(float) at:&captionHeight];
[coder decodeValueOfObjCType:@encode(char) at:&checkDigit];
return self;
}
//----------------------------------------------------------------------
- (void) encodeWithCoder: (NSCoder *)coder
//----------------------------------------------------------------------
{
[coder encodeObject:[self content]];
[coder encodeValueOfObjCType:@encode(BOOL) at:&printsCaption];
[coder encodeValueOfObjCType:@encode(float) at:&barWidth];
[coder encodeValueOfObjCType:@encode(float) at:&width];
[coder encodeValueOfObjCType:@encode(float) at:&height];
[coder encodeValueOfObjCType:@encode(float) at:&fontSize];
[coder encodeValueOfObjCType:@encode(float) at:&captionHeight];
[coder encodeValueOfObjCType:@encode(char) at:&checkDigit];
}
// -----------------------------------------------------------------------------------
-(void)dealloc
// -----------------------------------------------------------------------------------
{
[content release];
[super dealloc];
}
@end