Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 123 lines (110 sloc) 5.2 kB
297ae85 initial commit
Chris authored
1 // -----------------------------------------------------------------------------------
2 // NKDEAN8Barcode.m
3 // -----------------------------------------------------------------------------------
4 // Created by Jeff LaMarche on Wed May 29 2002.
23ca566 @dalewking Add XCode4 directories to .gitignore
dalewking authored
5 // �2002 Naked Software. All rights reserved.
297ae85 initial commit
Chris authored
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 "NKDEAN8Barcode.h"
12
13
14 @implementation NKDEAN8Barcode
15
16 // -----------------------------------------------------------------------------------
17 -(id)initWithContent: (NSString *)inContent
18 printsCaption: (BOOL)inPrints
19 // -----------------------------------------------------------------------------------
20 {
21 self = [super initWithContent:inContent printsCaption:inPrints];
22
23 // If 7 digits provided, calculate check-digit, if 8 provided, trust them
24 if ([[self content] length] == 7)
25 {
26 [self setContent:inContent];
27 [self generateChecksum];
28 }
29 else if ([[self content] length] == 8)
30 {
31 [self setCheckDigit:[[self content] characterAtIndex:7]];
32 [self setContent:[[self content] substringWithRange:NSMakeRange(0,7)]];
33 }
34
35 [self calculateWidth];
36 return self;
37 }
38 // -----------------------------------------------------------------------------------
39 -(NSString *)barcode
40 // -----------------------------------------------------------------------------------
41 {
42 /*
43 2. Two number system characters, encoded as left-hand odd-parity characters.
44 3. First two message characters, encoded as left-hand odd-parity characters.
45 4. Center guard bars, encoded as 01010.
46 5. Last three message characters, encoded as right-hand characters.
47 6. Check digit, encoded as right-hand character.
48 */
49 NSMutableString *theReturn = [NSMutableString stringWithString:@""];
50 char *contentString;
51 char numSystem;
52
23ca566 @dalewking Add XCode4 directories to .gitignore
dalewking authored
53 contentString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
297ae85 initial commit
Chris authored
54 numSystem = contentString[0];
55
56 [theReturn appendString:[self _encodeChar:contentString[0]]];
57 [theReturn appendString:[self _encodeChar:contentString[1]]];
58 [theReturn appendString:[self _encodeChar:contentString[2]]];
59 [theReturn appendString:[self _encodeChar:contentString[3]]];
60 [theReturn appendString:@"01010"];
61 [theReturn appendString:[[self _encodeChar:contentString[4]] swapHandedness]];
62 [theReturn appendString:[[self _encodeChar:contentString[5]] swapHandedness]];
63 [theReturn appendString:[[self _encodeChar:contentString[6]] swapHandedness]];
64 [theReturn appendString:[[self _encodeChar:checkDigit] swapHandedness]];
65
66 return theReturn;
67 }
68 // -----------------------------------------------------------------------------------
69 -(NSString *)caption
70 // -----------------------------------------------------------------------------------
71 {
72 return [NSString stringWithFormat:@" %@\t%@%c", [content substringWithRange:NSMakeRange(0,4)],
73 [content substringWithRange:NSMakeRange(4,3)],
74 [self checkDigit]];
23ca566 @dalewking Add XCode4 directories to .gitignore
dalewking authored
75
297ae85 initial commit
Chris authored
76 }
77 // -----------------------------------------------------------------------------------
78 -(BOOL) isContentValid
79 // -----------------------------------------------------------------------------------
80 {
81 return ([[self content] length] ==7);
82 }
83 // -----------------------------------------------------------------------------------
84 -(void)generateChecksum
85 // -----------------------------------------------------------------------------------
86 {
87 int oddSum = 0;
88 int evenSum = 0;
89 int checkInt;
23ca566 @dalewking Add XCode4 directories to .gitignore
dalewking authored
90 char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
91
297ae85 initial commit
Chris authored
92 oddSum += (code[0] - '0') + (code[2] - '0') + (code[4] - '0') + (code[6] - '0');
93 evenSum += (code[1] - '0') + (code[3] - '0') + (code[5] - '0');
94 checkInt = 10 - (((oddSum * 3) + evenSum) % 10);
23ca566 @dalewking Add XCode4 directories to .gitignore
dalewking authored
95
297ae85 initial commit
Chris authored
96 if (checkInt == 10)
97 checkInt = 0;
98
99 checkDigit = checkInt + '0';
100 }
101 // -----------------------------------------------------------------------------------
102 -(float)barBottom:(int)index
103 // -----------------------------------------------------------------------------------
104 {
105 if ( (index < 4) || (index > ([[self completeBarcode] length] - 4)) || (index == 33) || (index == 35))
106 return 0.05*kScreenResolution;
107 else
108 return [self captionHeight] * kScreenResolution;
109 }
110 // -----------------------------------------------------------------------------------
111 -(int)digitsToRight
112 // -----------------------------------------------------------------------------------
113 {
114 return 0;
115 }
116 // -----------------------------------------------------------------------------------
117 -(int)digitsToLeft
118 // -----------------------------------------------------------------------------------
119 {
120 return 0;
121 }
122 @end
Something went wrong with that request. Please try again.