Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 396 lines (371 sloc) 15.629 kb
297ae85 initial commit
Chris authored
1 // -----------------------------------------------------------------------------------
2 // NKDBarcode.m
3 // -----------------------------------------------------------------------------------
4 // Created by Jeff LaMarche on Wed May 01 2002.
5 // Copyright (c) 2002 Naked Software. All rights reserved.
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 "NKDBarcode.h"
12 #import <UIKit/UIKit.h>
13
14 @implementation NKDBarcode
15 // -----------------------------------------------------------------------------------
16 -(id)initWithContent: (NSString *)inContent
17 // -----------------------------------------------------------------------------------
18 {
19 return [self initWithContent: inContent
20 printsCaption: YES];
21 }
22 // -----------------------------------------------------------------------------------
23 -(id)initWithContent: (NSString *)inContent
24 printsCaption: (BOOL)inPrints
25 // -----------------------------------------------------------------------------------
26 {
27 return [self initWithContent:inContent
28 printsCaption:inPrints
29 andBarWidth:.013*kScreenResolution
30 andHeight:.5*kScreenResolution
31 andFontSize:6.0
32 andCheckDigit:(char)-1];
33 }
34 // -----------------------------------------------------------------------------------
35 -(id)initWithContent: (NSString *)inContent
36 printsCaption: (BOOL)inPrints
37 andBarWidth: (float)inBarWidth
38 andHeight: (float)inHeight
39 andFontSize: (float)inFontSize
40 andCheckDigit: (char)inDigit
41 // -----------------------------------------------------------------------------------
42 {
43 if (self = [super init])
44 {
45 if (!inContent)
46 {
47 [self release];
48 return nil;
49 }
50 [self setContent:inContent];
51
52 [self setBarWidth:inBarWidth];
53 [self setPrintsCaption:inPrints];
54
55 //if (![self printsCaption])
56 [self setHeight:inHeight];
57 //else
58 [self setHeight:inHeight + (captionHeight*kScreenResolution)];
59
60 // Calculate width based on number of bars needed to encode this content
61 [self calculateWidth];
62
63 [self setFontSize: inFontSize];
64
65 // Check digit of -1 means no check digit - couldn't use 0 because
66 // 0 could be a valid check digit. Typically calculated, not set
67 // outside of the class
68 [self setCheckDigit: inDigit];
69 }
70 return self;
71 }
72 // -----------------------------------------------------------------------------------
73 -(void)setContent:(NSString *)inContent
74 // -----------------------------------------------------------------------------------
75 {
76 [content autorelease];
77 content = [inContent retain];
78 }
79 // -----------------------------------------------------------------------------------
80 -(void)setHeight:(float)inHeight
81 // -----------------------------------------------------------------------------------
82 {
83 height = inHeight;
84 }
85 // -----------------------------------------------------------------------------------
86 -(void)setWidth:(float)inWidth
87 // -----------------------------------------------------------------------------------
88 {
89 width = inWidth;
90 }
91 // -----------------------------------------------------------------------------------
92 -(void)calculateWidth
93 // -----------------------------------------------------------------------------------
94 {
95 [self setWidth:[[self completeBarcode] length] * [self barWidth]];
96 }
97 // -----------------------------------------------------------------------------------
98 -(void)setCheckDigit:(char)inCheckDigit
99 // -----------------------------------------------------------------------------------
100 {
101 checkDigit = inCheckDigit;
102 }
103 // -----------------------------------------------------------------------------------
104 -(void)setPrintsCaption:(BOOL)inPrints
105 // -----------------------------------------------------------------------------------
106 {
107 printsCaption = inPrints;
108 }
109 // -----------------------------------------------------------------------------------
110 -(void)setBarWidth:(float)inBarWidth
111 // -----------------------------------------------------------------------------------
112 {
113 barWidth = inBarWidth;
114 }
115 // -----------------------------------------------------------------------------------
116 -(NSString *)content
117 // -----------------------------------------------------------------------------------
118 {
119 return content;
120 }
121 // -----------------------------------------------------------------------------------
122 -(float)height
123 // -----------------------------------------------------------------------------------
124 {
125 return height;
126 }
127 // -----------------------------------------------------------------------------------
128 -(float)width
129 // -----------------------------------------------------------------------------------
130 {
131 return width;
132 }
133 // -----------------------------------------------------------------------------------
134 -(char)checkDigit
135 // -----------------------------------------------------------------------------------
136 {
137 return checkDigit;
138 }
139 // -----------------------------------------------------------------------------------
140 -(BOOL)printsCaption
141 // -----------------------------------------------------------------------------------
142 {
143 return printsCaption;
144 }
145 // -----------------------------------------------------------------------------------
146 -(float)barWidth
147 // -----------------------------------------------------------------------------------
148 {
149 return barWidth;
150 }
151 // -----------------------------------------------------------------------------------
152 -(BOOL)isSizeValid
153 // -----------------------------------------------------------------------------------
154 {
155 return YES;
156 }
157 // -----------------------------------------------------------------------------------
158 -(void)generateChecksum
159 // -----------------------------------------------------------------------------------
160 {
161 // Stub
162 }
163 // -----------------------------------------------------------------------------------
164 -(BOOL) isContentValid
165 // -----------------------------------------------------------------------------------
166 {
167 int i;
168 char *contentString;
169
e17cf1e @mdestagnol FIXED › Compatibility with iOS4 (replaced deprecated methods)
mdestagnol authored
170 contentString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae85 initial commit
Chris authored
171
172 for (i = 0; i < strlen(contentString); i++)
173 if ([[self _encodeChar:contentString[i]] isEqual:@""])
174 return NO;
175
176 return YES;
177 }
178 // -----------------------------------------------------------------------------------
179 -(NSString *)initiator
180 // -----------------------------------------------------------------------------------
181 {
182 return @"";
183 }
184 // -----------------------------------------------------------------------------------
185 -(NSString *)terminator
186 // -----------------------------------------------------------------------------------
187 {
188 return @"";
189 }
190 // -----------------------------------------------------------------------------------
191 -(NSString *)_encodeChar:(char)inChar
192 // -----------------------------------------------------------------------------------
193 {
194 // Subclasses MUST override - we'll encode dummy character for testing
195 return @"1010";
196 }
197 // -----------------------------------------------------------------------------------
198 -(NSString *)barcode
199 // -----------------------------------------------------------------------------------
200 {
201 NSMutableString *theReturn = [NSMutableString stringWithString:@""];
202 int i;
203 char *contentString;
204
e17cf1e @mdestagnol FIXED › Compatibility with iOS4 (replaced deprecated methods)
mdestagnol authored
205 contentString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae85 initial commit
Chris authored
206
207 for (i = 0; i < strlen(contentString); i++)
208 [theReturn appendString:[self _encodeChar:contentString[i]]];
209
210 if (checkDigit != -1)
211 [theReturn appendString:[self _encodeChar:checkDigit]];
212
213 return theReturn;
214
215 }
216 // -----------------------------------------------------------------------------------
217 -(NSString *)completeBarcode
218 // -----------------------------------------------------------------------------------
219 {
220 return [NSString stringWithFormat:@"%@%@%@", [self initiator], [self barcode], [self terminator]];
221 }
222 // -----------------------------------------------------------------------------------
223 -(void)setCaptionHeight:(float)inHeight
224 // -----------------------------------------------------------------------------------
225 {
226 captionHeight = inHeight;
227 }
228 // -----------------------------------------------------------------------------------
229 -(float)captionHeight
230 // -----------------------------------------------------------------------------------
231 {
232 return captionHeight;
233 }
234 // -----------------------------------------------------------------------------------
235 -(void)setFontSize:(float)inSize
236 // -----------------------------------------------------------------------------------
237 {
238 UIFont *font;
239
240 fontSize = inSize;
241
242 font = [UIFont systemFontOfSize:[self fontSize]];
243
244 [self setCaptionHeight: ([font capHeight]/72) + (.35 * ([font capHeight]/72))];
245 }
246 // -----------------------------------------------------------------------------------
247 -(float) fontSize
248 // -----------------------------------------------------------------------------------
249 {
250 return fontSize;
251 }
252 // -----------------------------------------------------------------------------------
253 -(int)digitsToLeft
254 // -----------------------------------------------------------------------------------
255 {
256 return 0;
257 }
258 // -----------------------------------------------------------------------------------
259 -(int)digitsToRight
260 // -----------------------------------------------------------------------------------
261 {
262 return 0;
263 }
264 // -----------------------------------------------------------------------------------
265 -(NSString *)leftCaption
266 // -----------------------------------------------------------------------------------
267 {
268 return [content substringWithRange:NSMakeRange(0, [self digitsToLeft])];
269 }
270 // -----------------------------------------------------------------------------------
271 -(NSString *)rightCaption
272 // -----------------------------------------------------------------------------------
273 {
274 return [content substringWithRange:NSMakeRange([content length] - [self digitsToRight], [self digitsToRight])];
275 }
276 // -----------------------------------------------------------------------------------
277 -(NSString *)caption
278 // -----------------------------------------------------------------------------------
279 {
280 if ( ([self digitsToRight] == 0) && ([self digitsToLeft] == 0))
281 return content;
282 else
283 return [content substringWithRange:NSMakeRange([self digitsToLeft],
284 [content length] - [self digitsToLeft] - [self digitsToRight])];
285 }
286 // -----------------------------------------------------------------------------------
287 -(float)barBottom:(int)index
288 // -----------------------------------------------------------------------------------
289 {
290 if ([self printsCaption])
291 return captionHeight * kScreenResolution;
292 else
293 return 0.0;
294 }
295 // -----------------------------------------------------------------------------------
296 -(float)barTop:(int)index
297 // -----------------------------------------------------------------------------------
298 {
299 return [self height];
300 }
301 // -----------------------------------------------------------------------------------
302 -(float)firstBar
303 // -----------------------------------------------------------------------------------
304 {
305 return 0;
306 }
307 // -----------------------------------------------------------------------------------
308 -(float)lastBar
309 // -----------------------------------------------------------------------------------
310 {
311 return [self width] - [self barWidth];
312 }
313 // -----------------------------------------------------------------------------------
314 -(NSString *)description
315 // -----------------------------------------------------------------------------------
316 {
317 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",
318 [self class],
319 [self content],
320 [self checkDigit],
321 [self width],
322 [self height],
323 [self barWidth],
324 [self fontSize],
325 [self captionHeight]];
326 }
327 // -----------------------------------------------------------------------------------
328 - (BOOL)isEqual:(id)anObject
329 // -----------------------------------------------------------------------------------
330 {
331 NKDBarcode *compare;
332
333 compare = anObject;
334
335 return ([[self content] isEqual:[compare content]]);
336 }
337 // -----------------------------------------------------------------------------------
338 - (id)copyWithZone:(NSZone *)zone
339 // -----------------------------------------------------------------------------------
340 {
341 NKDBarcode *ret = [[[self class] alloc] initWithContent: [self content]
342 printsCaption: [self printsCaption]
343 andBarWidth: [self barWidth]
344 andHeight: [self height]
345 andFontSize: [self fontSize]
346 andCheckDigit: [self checkDigit]];
347 [ret calculateWidth];
348 return ret;
349 }
350 // -----------------------------------------------------------------------------------
351 - (id)initWithCoder:(NSCoder *)coder
352 // -----------------------------------------------------------------------------------
353 {
354
355
356 // Since we subclass NSObject, this call to super
357 // init is not necessary, but it's good form to
358 // include it, as it is possible that someday
359 // NSObject's -init method will do something
360 // -----------------------------------------------
361 self = [super init];
362
363 [self setContent:[coder decodeObject]];
364 [coder decodeValueOfObjCType:@encode(BOOL) at:&printsCaption];
365 [coder decodeValueOfObjCType:@encode(float) at:&barWidth];
366 [coder decodeValueOfObjCType:@encode(float) at:&width];
367 [coder decodeValueOfObjCType:@encode(float) at:&height];
368 [coder decodeValueOfObjCType:@encode(float) at:&fontSize];
369 [coder decodeValueOfObjCType:@encode(float) at:&captionHeight];
370 [coder decodeValueOfObjCType:@encode(char) at:&checkDigit];
371
372 return self;
373 }
374 //----------------------------------------------------------------------
375 - (void) encodeWithCoder: (NSCoder *)coder
376 //----------------------------------------------------------------------
377 {
378 [coder encodeObject:[self content]];
379 [coder encodeValueOfObjCType:@encode(BOOL) at:&printsCaption];
380 [coder encodeValueOfObjCType:@encode(float) at:&barWidth];
381 [coder encodeValueOfObjCType:@encode(float) at:&width];
382 [coder encodeValueOfObjCType:@encode(float) at:&height];
383 [coder encodeValueOfObjCType:@encode(float) at:&fontSize];
384 [coder encodeValueOfObjCType:@encode(float) at:&captionHeight];
385 [coder encodeValueOfObjCType:@encode(char) at:&checkDigit];
386
387 }
388 // -----------------------------------------------------------------------------------
389 -(void)dealloc
390 // -----------------------------------------------------------------------------------
391 {
392 [content release];
393 [super dealloc];
394 }
395 @end
Something went wrong with that request. Please try again.