-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBCBarcodeLabelsHtmlFormatter.m
More file actions
214 lines (118 loc) · 5.73 KB
/
BCBarcodeLabelsHtmlFormatter.m
File metadata and controls
214 lines (118 loc) · 5.73 KB
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
//
// BCBarcodeLabelsHtmlFormatter.m
// Bartender
//
// Created by Tom Houpt on 12/7/15.
// Copyright 2012 Behavioral Cybernetics. All rights reserved.
//
#import "BCBarcodeLabelsHtmlFormatter.h"
#define kBarcodeFormat (@"*%@%02ld%@*")
#define kPlainTextFormat (@"%@ %02ld %@")
@implementation BCBarcodeLabelsHtmlFormatter
- (id) initWithExptCode:(NSString *)ec andItemCodes:(NSArray *)il forSubjectsCount:(NSUInteger)sc; {
self = [super init];
if (self) {
exptCode = ec;
itemCodes = il;
numSubjects = sc;
}
return self;
}
- (NSString *) htmlString; {
NSMutableString *htmlString = [[NSMutableString alloc] init];
[self appendHtmlToString:htmlString];
return htmlString;
}
-(void)appendCssToString:(NSMutableString *)buffer; {
NSMutableString *cssString = [[NSMutableString alloc] init];
[cssString appendString: @"<style type=\"text/css\">\r"];
// specify fixed layout and collapsed borders for barcode table
[cssString appendString:@"table { table-layout: fixed ; border-collapse: collapse; page-break-after: always; } \r"];
[cssString appendString:@"table:last-child { page-break-after: auto; } \r"];
// specify 1.8" wide table cells with dotted borders
[cssString appendString:@"td { width: 1.8in; border: 1px dotted; padding-bottom: 0px;padding-top: 0px;} \r"];
// specify barcode font "New" at 36pt with 0.5in height for barcode cells
[cssString appendString:@".barcode { font-family:\"New\"; font-size: 36pt; vertical-align: bottom; text-align: center; height: 0.5in; border-bottom:hidden; } \r"];
// specify times New Roman font "New" at 18pt with 0.2in height for plaintext cells
[cssString appendString:@".plaintext { font-family: \"Times New Roman\"; font-weight:bold; font-size: 18pt; vertical-align: top; text-align: center; height: 0.2in; border-top:hidden; padding-top: 0px;} \r"];
[cssString appendString: @"</style>\r"];
[buffer appendString:cssString];
}
- (void) appendHtmlToString:(NSMutableString *)buffer; {
NSUInteger itemIndex;
NSUInteger subjectPage, numPages;
NSUInteger subjectIndex;
[self appendCssToString:buffer];
// ten labels per page
numPages = (numSubjects/kNumSubjectsPerPage) + 1;
for (itemIndex = 0; itemIndex < [itemCodes count]; itemIndex++) {
for (subjectPage = 0; subjectPage < numPages; subjectPage++) {
NSString *theItemCode = (NSString *)[itemCodes objectAtIndex:itemIndex];
[buffer appendFormat: @"<TABLE>\r"];
for (subjectIndex=subjectPage * kNumSubjectsPerPage;subjectIndex < numSubjects && subjectIndex < ((subjectPage+1) * kNumSubjectsPerPage);subjectIndex++) {
[self appendRowAtIndex:subjectIndex withItemCode:theItemCode toString:buffer];
} // next subject
[buffer appendString: @"</TABLE>\r"];
} // next subject page
} // next item
// write html to a file for debugging
NSError *error;
NSString *testFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0] stringByAppendingPathComponent:@"barcodeLabelsHtmlTest.html"];
[buffer writeToFile:testFilePath atomically:YES encoding:NSUnicodeStringEncoding error:&error];
if (nil != error) {
NSLog(@"BCBarcodeLabelsdHtmlFormatter appendHtmlToString Error Desc: %@",[error localizedDescription]);
NSLog(@"BCBarcodeLabelsdHtmlFormatter appendHtmlToString Error Sugg: %@",[error localizedRecoverySuggestion]);
}
}
-(void)appendHeadersToString:(NSMutableString *)buffer; {
[buffer appendString: @"\t<TR frame=\"below\">\r"];
for (NSTableColumn *aTableColumn in [tableView tableColumns]) {
[buffer appendString: @"\t\t<TH>"];
[buffer appendString:[[aTableColumn headerCell] stringValue]];
[buffer appendString: @"</TH>\r"];
}
[buffer appendString: @"\t</TR>\r"];
}
-(void)appendRowAtIndex:(NSUInteger)rowIndex withItemCode:(NSString *)itemCode toString:(NSMutableString *)buffer; {
NSUInteger i;
// first barcode row...
[buffer appendString: @"\t<TR class=\"barcode\">\r"];
for (i=0; i<kNumColumns;i++) {
[buffer appendString: @"\t\t<TD>"];
// label text should be centered in the cell
// this should be in 3of9 barcode font 36 pt (FRE3OF9X.TTF is what we use on Macs)
NSString *barcodeString = [[NSString alloc] initWithFormat:kBarcodeFormat, exptCode, (rowIndex+1), itemCode];
[buffer appendString:barcodeString];
[buffer appendString: @"</TD>\r"];
}
[buffer appendString: @"\t</TR>\r"];
// then plaintext row...
[buffer appendString: @"\t<TR class=\"plaintext\">\r"];
for (i=0; i<kNumColumns;i++) {
[buffer appendString: @"\t\t<TD>"];
// this should be in Times Roman Bold 18 pt
NSString *plaintextString = [[NSString alloc] initWithFormat:kPlainTextFormat, exptCode, (rowIndex+1),itemCode];
[buffer appendString:plaintextString];
[buffer appendString: @"</TD>\r"];
}
[buffer appendString: @"\t</TR>\r"];
}
//- (NSString *)stringForObjectValue:(id)anObject; {
//
// // Textual representation of cell content
// // taken from "MyValueFormatter.m" of "QTMetadataEditor" sample code
//
// NSString *resultString;
//
// if ([anObject isMemberOfClass:[NSData class]]) {
// // resultString = [MyValueFormatter hexStringFromData:(NSData *)anObject];
// }
// else if ([anObject isMemberOfClass:[NSString class]]) {
// resultString = anObject;
// }
// else if ([anObject isMemberOfClass:[NSNumber class]]) {
// resultString = [anObject stringValue];
// }
// return resultString;
//}
@end