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
/
NKDBarcodeOffscreenView.m
249 lines (228 loc) · 10.9 KB
/
NKDBarcodeOffscreenView.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
// -----------------------------------------------------------------------------------
// NKDBarcodeOffscreenView.m
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Mon May 06 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 "NKDBarcodeOffscreenView.h"
@implementation NKDBarcodeOffscreenView
- (id)initWithBarcode:(NKDBarcode *)inBarcode
{
CGRect frame = CGRectMake(0,0,[inBarcode width], [inBarcode height]);
// Calculate frame and then...
self = [super initWithFrame:frame];
if (self)
{
[self setBarcode:inBarcode];
}
return self;
}
// A more elegant solution offered by Sato Akira (arigoto!)
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetAllowsAntialiasing(context, false);
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
int i, barCount=0;
float curPos = [barcode firstBar];
NSString *codeString = [barcode completeBarcode];
BOOL started = NO;
int lastBarIndex = -1;
for (i = 0; i < [codeString length]; i++)
{
if ([codeString characterAtIndex:i] == '1')
{
if (!started)
started = YES;
barCount++;
lastBarIndex = i;
// If last character is a bar, it needs to be printed here.
if (i == [codeString length]-1)
{
CGContextSetLineWidth(context, 0.0);
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, CGRectMake(curPos,
[barcode barBottom:lastBarIndex],
[barcode barWidth] * barCount,
[barcode barTop:lastBarIndex] - [barcode barBottom:lastBarIndex]));
/* CGContextFillRect(context, CGRectMake(curPos,
[barcode barBottom:lastBarIndex],
[barcode barWidth] * barCount,
[barcode barTop:lastBarIndex] - [barcode barBottom:lastBarIndex])); */
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathFill);
CGPathRelease(path);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
}
}
else
{
if (started)
{
CGContextSetLineWidth(context, 0.0);
CGMutablePathRef path = CGPathCreateMutable();
/*
CGContextFillRect(context, CGRectMake(curPos,
[barcode barBottom:lastBarIndex],
[barcode barWidth] * barCount,
[barcode barTop:lastBarIndex] - [barcode barBottom:lastBarIndex] ));
*/
CGPathAddRect(path, NULL, CGRectMake(curPos,
[barcode barBottom:lastBarIndex],
[barcode barWidth] * barCount,
[barcode barTop:lastBarIndex] - [barcode barBottom:lastBarIndex] ));
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextAddPath(context, path);
CGContextDrawPath(context, kCGPathFill);
CGPathRelease(path);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
}
curPos += [barcode barWidth] * (barCount + 1);
barCount = 0;
started = NO;
}
}
if ([barcode printsCaption])
{
NSLog(@":::::::NOTE:::::: DOES NOT CURRENTLY PRINT CAPTION");
NSLog(@":::::::NOTE:::::: FIX PROGRAMMER");
/*
NSMutableAttributedString *sAttr;
float kerning;
CGSize captionSize;
CGRect bBounds = [self bounds];
float heightSpace = 0.0;
float widthSpace = 0.0;
float barWidth = [barcode barWidth];
NSString *leftCaption = [barcode leftCaption];
NSString *caption = [barcode caption];
NSString *rightCaption = [barcode rightCaption];
float yPos = bBounds.origin.y + heightSpace;
//NSFont *font = [NSFont fontWithName:@"Lucida Grande" size:[barcode fontSize]];
UIFont *font = [UIFont systemFontOfSize:[barcode fontSize]];
NSMutableParagraphStyle *leftAligmentStyle = [[NSMutableParagraphStyle allocWithZone:[self zone]] init];
[leftAligmentStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
// Left caption for UPC / EAN
if ((nil != leftCaption) && (NO == [leftCaption isEqualToString:@""])) {
kerning = 0.0;
sAttr = [[NSMutableAttributedString allocWithZone:[self zone]] initWithString:leftCaption attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,leftAligmentStyle,NSParagraphStyleAttributeName,[NSNumber numberWithFloat:kerning],NSKernAttributeName,nil]];
captionSize = [sAttr size];
[sAttr drawAtPoint:NSMakePoint(bBounds.origin.x + widthSpace + 0.25 * [barcode firstBar],yPos + 3.0)];
[sAttr release];
}
// Draw the main caption under the barcode
if ((nil != caption) && (NO == [caption isEqualToString:@""])) {
unsigned int cCount,captionLength;
NSRange range;
float container;
NSArray *captions = [caption componentsSeparatedByString:@"\t"];
if (((nil != leftCaption) && (NO == [leftCaption isEqualToString:@""])) || (1 < [captions count])) {
if (1 == [captions count]) // for UPCE //
container = [barcode width] / (float)[captions count] - 2.0 * [[barcode initiator] length] * barWidth - 2.0 * [barcode firstBar];
else // for UPCA,EAN8,EAN13 //
container = [barcode width] / (float)[captions count] - 1.0 * [[barcode initiator] length] * barWidth - [barcode firstBar]; // [[barcode initiator] length] == [[barcode terminator] length]; && [barcode firstBar] == [barcode width] - [barcode lastBar]; //
for (cCount = 0; cCount < [captions count]; cCount++) {
kerning = 0.0;
sAttr = [[NSMutableAttributedString allocWithZone:[self zone]] initWithString:[captions objectAtIndex:cCount] attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,leftAligmentStyle,NSParagraphStyleAttributeName,[NSNumber numberWithFloat:kerning],NSKernAttributeName,nil]];
captionSize = [sAttr size];
captionLength = [(NSString *)[captions objectAtIndex:cCount] length];
kerning = (container - captionSize.width) / (float)(2 * captionLength);
range = NSMakeRange(0,captionLength);
[sAttr setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,leftAligmentStyle,NSParagraphStyleAttributeName,[NSNumber numberWithFloat:1.3 * kerning],NSKernAttributeName,nil] range:range];
[sAttr drawAtPoint:NSMakePoint(bBounds.origin.x + widthSpace + kerning * 3 + [barcode firstBar] + (float)(cCount + 1) * [[barcode initiator] length] * barWidth + (float)cCount * container,yPos)];
[sAttr release];
}
}
else {
cCount = 0;
kerning = 0.0;
sAttr = [[NSMutableAttributedString allocWithZone:[self zone]] initWithString:[captions objectAtIndex:cCount] attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,leftAligmentStyle,NSParagraphStyleAttributeName,[NSNumber numberWithFloat:kerning],NSKernAttributeName,nil]];
captionSize = [sAttr size];
captionLength = [(NSString *)[captions objectAtIndex:cCount] length];
container = [barcode width];
kerning = (container - captionSize.width) / (float)(2 * captionLength);
range = NSMakeRange(0,captionLength);
[sAttr setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,leftAligmentStyle,NSParagraphStyleAttributeName,[NSNumber numberWithFloat:2.0 * kerning],NSKernAttributeName,nil] range:range];
[sAttr drawAtPoint:NSMakePoint(bBounds.origin.x + widthSpace + kerning,yPos)];
[sAttr release];
}
}
// Right caption for UPC / EAN
if ((nil != rightCaption) && (NO == [rightCaption isEqualToString:@""])) {
kerning = 0.0;
sAttr = [[NSMutableAttributedString allocWithZone:[self zone]] initWithString:rightCaption attributes:[NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,leftAligmentStyle,NSParagraphStyleAttributeName,[NSNumber numberWithFloat:kerning],NSKernAttributeName,nil]];
[sAttr drawAtPoint:NSMakePoint(bBounds.origin.x + widthSpace + [barcode lastBar] + ([barcode width] * 0.07),yPos + 3.0)];
[sAttr release];
}
[leftAligmentStyle release];
*/
}
}
// -----------------------------------------------------------------------------------
-(NKDBarcode *)barcode
// -----------------------------------------------------------------------------------
{
return barcode;
}
// -----------------------------------------------------------------------------------
-(void)setBarcode:(NKDBarcode *)inBarcode
// -----------------------------------------------------------------------------------
{
[barcode autorelease];
barcode = inBarcode;
}
// -----------------------------------------------------------------------------------
-(BOOL)knowsPageRange:(NSRange *)rptr
// -----------------------------------------------------------------------------------
{
rptr->location = 1;
rptr->length = 1;
return YES;
}
// -----------------------------------------------------------------------------------
-(CGRect)rectForPage:(int)pageNum
// -----------------------------------------------------------------------------------
{
return CGRectMake(0,0,[[self barcode] width], [[self barcode] height]);
}
- (UIImage *)_renderViewAsImage:(CGRect) rect
/* this method is not intended to be called directly */
/* used by dataWithPDFInsideRect: when multithreading is required */
{
UIGraphicsBeginImageContext(CGSizeMake(rect.size.width, rect.size.height));
[self drawRect:rect];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
- (UIImage *)imageInsideRect:(CGRect)rect
/* This method overrides the default NSView version to allow the view
to be rendered to PDF within another print operation. If there is
already a print operation running on the current thread, this method
spawns a new thread to render the PDF, otherwise the default NSView
version of dataWithPDFInsideRect: is called... */
{
UIImage * result = [self _renderViewAsImage:rect];
return result;
}
- (NSData *)pdfInsideRect:(CGRect)rect {
NSMutableData * data = [NSMutableData data];
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef)data);
CGRect media = CGRectMake(0.0, 0.0, rect.size.width, rect.size.height);
CGContextRef context = CGPDFContextCreate(consumer, &media, (CFDictionaryRef)[NSDictionary dictionary]);
UIGraphicsPushContext(context);
CGPDFContextBeginPage(context, (CFDictionaryRef)[NSDictionary dictionary]);
[self drawRect:rect];
UIGraphicsPopContext();
CGPDFContextEndPage(context);
CGPDFContextClose(context);
CGDataConsumerRelease(consumer);
CGContextRelease(context);
return data;
}
@end