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
/
NKDEAN8Barcode.m
122 lines (110 loc) · 5.08 KB
/
NKDEAN8Barcode.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
// -----------------------------------------------------------------------------------
// NKDEAN8Barcode.m
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Wed May 29 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 "NKDEAN8Barcode.h"
@implementation NKDEAN8Barcode
// -----------------------------------------------------------------------------------
-(id)initWithContent: (NSString *)inContent
printsCaption: (BOOL)inPrints
// -----------------------------------------------------------------------------------
{
self = [super initWithContent:inContent printsCaption:inPrints];
// If 7 digits provided, calculate check-digit, if 8 provided, trust them
if ([[self content] length] == 7)
{
[self setContent:inContent];
[self generateChecksum];
}
else if ([[self content] length] == 8)
{
[self setCheckDigit:[[self content] characterAtIndex:7]];
[self setContent:[[self content] substringWithRange:NSMakeRange(0,7)]];
}
[self calculateWidth];
return self;
}
// -----------------------------------------------------------------------------------
-(NSString *)barcode
// -----------------------------------------------------------------------------------
{
/*
2. Two number system characters, encoded as left-hand odd-parity characters.
3. First two message characters, encoded as left-hand odd-parity characters.
4. Center guard bars, encoded as 01010.
5. Last three message characters, encoded as right-hand characters.
6. Check digit, encoded as right-hand character.
*/
NSMutableString *theReturn = [NSMutableString stringWithString:@""];
char *contentString;
char numSystem;
contentString = (char *)[[self content] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
numSystem = contentString[0];
[theReturn appendString:[self _encodeChar:contentString[0]]];
[theReturn appendString:[self _encodeChar:contentString[1]]];
[theReturn appendString:[self _encodeChar:contentString[2]]];
[theReturn appendString:[self _encodeChar:contentString[3]]];
[theReturn appendString:@"01010"];
[theReturn appendString:[[self _encodeChar:contentString[4]] swapHandedness]];
[theReturn appendString:[[self _encodeChar:contentString[5]] swapHandedness]];
[theReturn appendString:[[self _encodeChar:contentString[6]] swapHandedness]];
[theReturn appendString:[[self _encodeChar:checkDigit] swapHandedness]];
return theReturn;
}
// -----------------------------------------------------------------------------------
-(NSString *)caption
// -----------------------------------------------------------------------------------
{
return [NSString stringWithFormat:@" %@\t%@%c", [content substringWithRange:NSMakeRange(0,4)],
[content substringWithRange:NSMakeRange(4,3)],
[self checkDigit]];
}
// -----------------------------------------------------------------------------------
-(BOOL) isContentValid
// -----------------------------------------------------------------------------------
{
return ([[self content] length] ==7);
}
// -----------------------------------------------------------------------------------
-(void)generateChecksum
// -----------------------------------------------------------------------------------
{
int oddSum = 0;
int evenSum = 0;
int checkInt;
char * code = (char *) [content cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
oddSum += (code[0] - '0') + (code[2] - '0') + (code[4] - '0') + (code[6] - '0');
evenSum += (code[1] - '0') + (code[3] - '0') + (code[5] - '0');
checkInt = 10 - (((oddSum * 3) + evenSum) % 10);
if (checkInt == 10)
checkInt = 0;
checkDigit = checkInt + '0';
}
// -----------------------------------------------------------------------------------
-(float)barBottom:(int)index
// -----------------------------------------------------------------------------------
{
if ( (index < 4) || (index > ([[self completeBarcode] length] - 4)) || (index == 33) || (index == 35))
return 0.05*kScreenResolution;
else
return [self captionHeight] * kScreenResolution;
}
// -----------------------------------------------------------------------------------
-(int)digitsToRight
// -----------------------------------------------------------------------------------
{
return 0;
}
// -----------------------------------------------------------------------------------
-(int)digitsToLeft
// -----------------------------------------------------------------------------------
{
return 0;
}
@end