RSCode128Generator.swift 13.3 KB
Newer Older
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
//  RSCode128Generator.swift
//  RSBarcodesSample
//
//  Created by R0CKSTAR on 6/11/14.
//  Copyright (c) 2014 P.D.Q. All rights reserved.
//

import UIKit

public enum RSCode128GeneratorCodeTable: Int {
    case auto = 0
    case a, b, c
}

// http://www.barcodeisland.com/code128.phtml
// http://courses.cs.washington.edu/courses/cse370/01au/minirproject/BarcodeBattlers/barcodes.html
open class RSCode128Generator: RSAbstractCodeGenerator, RSCheckDigitGenerator {
    class RSCode128GeneratorAutoCodeTable {
        var startCodeTable = RSCode128GeneratorCodeTable.auto
        var sequence:Array<Int> = []
    }
    
    var codeTable: RSCode128GeneratorCodeTable
    var codeTableSize: Int
    var autoCodeTable: RSCode128GeneratorAutoCodeTable
    
    public init(codeTable:RSCode128GeneratorCodeTable) {
        self.codeTable = codeTable
        self.codeTableSize = CODE128_CHARACTER_ENCODINGS.count
        self.autoCodeTable = RSCode128GeneratorAutoCodeTable()
    }
    
    public convenience override init() {
        self.init(codeTable: .auto)
    }
    
    func startCodeTableValue(_ startCodeTable: RSCode128GeneratorCodeTable) -> Int {
        switch self.autoCodeTable.startCodeTable {
        case .a:
            return self.codeTableSize - 4
        case .b:
            return self.codeTableSize - 3
        case .c:
            return self.codeTableSize - 2
        default:
            switch startCodeTable {
            case .a:
                return self.codeTableSize - 4
            case .b:
                return self.codeTableSize - 3
            case .c:
                return self.codeTableSize - 2
            default:
                return 0
            }
        }
    }
    
    func middleCodeTableValue(_ codeTable:RSCode128GeneratorCodeTable) -> Int {
        switch codeTable {
        case .a:
            return self.codeTableSize - 6
        case .b:
            return self.codeTableSize - 7
        case .c:
            return self.codeTableSize - 8
        default:
            return 0
        }
    }
    
    func calculateContinousDigits(_ contents:String, defaultCodeTable:RSCode128GeneratorCodeTable, range:Range<Int>) {
        var isFinished = false
        if range.upperBound == contents.length() {
            isFinished = true
        }
        
        let length = range.upperBound - range.lowerBound
        if (range.lowerBound == 0 && length >= 4)
            || (range.lowerBound > 0 && length >= 6) {
            var isOrphanDigitUsed = false
            // Use START C when continous digits are found from range.location == 0
            if range.lowerBound == 0 {
                self.autoCodeTable.startCodeTable = .c
            } else {
                if length % 2 == 1 {
                    let digitValue = CODE128_ALPHABET_STRING.location(contents[range.lowerBound])
                    self.autoCodeTable.sequence.append(digitValue)
                    isOrphanDigitUsed = true
                }
                self.autoCodeTable.sequence.append(self.middleCodeTableValue(.c))
            }
            
            // Insert all xx combinations
            for i in 0..<length / 2 {
                let startIndex = range.lowerBound + i * 2
                let digitValue = Int(contents.substring(isOrphanDigitUsed ? startIndex + 1 : startIndex, length: 2))!
                self.autoCodeTable.sequence.append(digitValue)
            }
            
            if (length % 2 == 1 && !isOrphanDigitUsed) || !isFinished {
                self.autoCodeTable.sequence.append(self.middleCodeTableValue(defaultCodeTable))
            }
            
            if length % 2 == 1 && !isOrphanDigitUsed {
                let digitValue = CODE128_ALPHABET_STRING.location(contents[range.upperBound - 1])
                self.autoCodeTable.sequence.append(digitValue)
            }
            
            if !isFinished {
                let characterValue = CODE128_ALPHABET_STRING.location(contents[range.upperBound])
                self.autoCodeTable.sequence.append(characterValue)
            }
        } else {
            for i in range.lowerBound...(isFinished ? range.upperBound - 1 : range.upperBound) {
                let characterValue = CODE128_ALPHABET_STRING.location(contents[i])
                self.autoCodeTable.sequence.append(characterValue)
            }
        }
    }
    
    func calculateAutoCodeTable(_ contents:String) {
        if self.codeTable == .auto {
            // Select the short code table A as default code table
            var defaultCodeTable: RSCode128GeneratorCodeTable = .a
            
            // Determine whether to use code table B
            if let CODE128_ALPHABET_STRING_A = CODE128_ALPHABET_STRING.substring(0, length: 64) {
                for i in 0..<contents.length() {
                    if CODE128_ALPHABET_STRING_A.location(contents[i]) == NSNotFound
                        && defaultCodeTable == .a {
                        defaultCodeTable = .b
                        break
                    }
                }
            }
            
            var continousDigitsStartIndex:Int = NSNotFound
            for i in 0..<contents.length() {
                var continousDigitsRange:Range<Int> = 0..<0
                if let character = contents[i] {
                    if DIGITS_STRING.location(character) == NSNotFound {
                        // Non digit found
                        if continousDigitsStartIndex != NSNotFound {
                            continousDigitsRange = continousDigitsStartIndex..<i
                        } else {
                            let characterValue = CODE128_ALPHABET_STRING.location(character)
                            self.autoCodeTable.sequence.append(characterValue)
                        }
                    } else {
                        // Digit found
                        if continousDigitsStartIndex == NSNotFound {
                            continousDigitsStartIndex = i
                        }
                        if continousDigitsStartIndex != NSNotFound && i == contents.length() - 1 {
                            continousDigitsRange = continousDigitsStartIndex..<(i + 1)
                        }
                    }
                    
                    if continousDigitsRange.upperBound - continousDigitsRange.lowerBound != 0 {
                        self.calculateContinousDigits(contents, defaultCodeTable: defaultCodeTable, range: continousDigitsRange)
                        continousDigitsStartIndex = NSNotFound
                    }
                }
            }
            
            if self.autoCodeTable.startCodeTable == .auto {
                self.autoCodeTable.startCodeTable = defaultCodeTable
            }
        }
    }
    
    func encodeCharacterString(_ characterString:String) -> String {
        return CODE128_CHARACTER_ENCODINGS[CODE128_ALPHABET_STRING.location(characterString)]
    }
    
    override open func initiator() -> String {
        switch self.codeTable {
        case .auto:
            return CODE128_CHARACTER_ENCODINGS[self.startCodeTableValue(self.autoCodeTable.startCodeTable)]
        default:
            return CODE128_CHARACTER_ENCODINGS[self.startCodeTableValue(self.codeTable)]
        }
    }
    
    override open func terminator() -> String {
        return CODE128_CHARACTER_ENCODINGS[self.codeTableSize - 1] + "11"
    }
    
    override open func isValid(_ contents: String) -> Bool {
        if contents.length() > 0 {
            for i in 0..<contents.length() {
                if CODE128_ALPHABET_STRING.location(contents[i]) == NSNotFound {
                    return false
                }
            }
            
            switch self.codeTable {
            case .auto:
                self.calculateAutoCodeTable(contents)
                fallthrough
            case .b:
                return true
            case .a:
                if let CODE128_ALPHABET_STRING_A = CODE128_ALPHABET_STRING.substring(0, length: 64) {
                    for i in 0..<contents.length() {
                        if CODE128_ALPHABET_STRING_A.location(contents[i]) == NSNotFound {
                            return false
                        }
                    }
                }
                return true
            case .c:
                if contents.length() % 2 == 0 && contents.isNumeric() {
                    return true
                }
                return false
            }
        }
        return false
    }
    
    override open func barcode(_ contents: String) -> String {
        var barcode = ""
        switch self.codeTable {
        case .auto:
            for i in 0..<self.autoCodeTable.sequence.count {
                barcode += CODE128_CHARACTER_ENCODINGS[self.autoCodeTable.sequence[i]]
            }
        case .a, .b:
            for i in 0..<contents.length() {
                barcode += self.encodeCharacterString(contents[i])
            }
        case .c:
            for i in 0..<contents.length() {
                if i % 2 == 1 {
                    continue
                } else {
                    let value = Int(contents.substring(i, length: 2))!
                    barcode += CODE128_CHARACTER_ENCODINGS[value]
                }
            }
        }
        
        barcode += self.checkDigit(contents)
        return barcode
    }
    
    // MARK: RSCheckDigitGenerator
    
    open func checkDigit(_ contents: String) -> String {
        var sum = 0
        switch self.codeTable {
        case .auto:
            sum += self.startCodeTableValue(self.autoCodeTable.startCodeTable)
            for i in 0..<self.autoCodeTable.sequence.count {
                sum += self.autoCodeTable.sequence[i] * (i + 1)
            }
        case .a:
            sum = -1 // START A = self.codeTableSize - 4 = START B - 1
            fallthrough
        case .b:
            sum += self.codeTableSize - 3 // START B
            for i in 0..<contents.length() {
                let characterValue = CODE128_ALPHABET_STRING.location(contents[i])
                sum += characterValue * (i + 1)
            }
        case .c:
            sum += self.codeTableSize - 2 // START C
            for i in 0..<contents.length() {
                if i % 2 == 1 {
                    continue
                } else {
                    let value = Int(contents.substring(i, length: 2))!
                    sum += value * (i / 2 + 1)
                }
            }
        }
        return CODE128_CHARACTER_ENCODINGS[sum % 103]
    }
    
    let CODE128_ALPHABET_STRING = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'abcdefghijklmnopqrstuvwxyz{|}~"
    
    let CODE128_CHARACTER_ENCODINGS = [
        "11011001100",
        "11001101100",
        "11001100110",
        "10010011000",
        "10010001100",
        "10001001100",
        "10011001000",
        "10011000100",
        "10001100100",
        "11001001000",
        "11001000100",
        "11000100100",
        "10110011100",
        "10011011100",
        "10011001110",
        "10111001100",
        "10011101100",
        "10011100110",
        "11001110010",
        "11001011100",
        "11001001110",
        "11011100100",
        "11001110100",
        "11101101110",
        "11101001100",
        "11100101100",
        "11100100110",
        "11101100100",
        "11100110100",
        "11100110010",
        "11011011000",
        "11011000110",
        "11000110110",
        "10100011000",
        "10001011000",
        "10001000110",
        "10110001000",
        "10001101000",
        "10001100010",
        "11010001000",
        "11000101000",
        "11000100010",
        "10110111000",
        "10110001110",
        "10001101110",
        "10111011000",
        "10111000110",
        "10001110110",
        "11101110110",
        "11010001110",
        "11000101110",
        "11011101000",
        "11011100010",
        "11011101110",
        "11101011000",
        "11101000110",
        "11100010110",
        "11101101000",
        "11101100010",
        "11100011010",
        "11101111010",
        "11001000010",
        "11110001010",
        "10100110000", // 64
        // Visible character encoding for code table A ended.
        "10100001100",
        "10010110000",
        "10010000110",
        "10000101100",
        "10000100110",
        "10110010000",
        "10110000100",
        "10011010000",
        "10011000010",
        "10000110100",
        "10000110010",
        "11000010010",
        "11001010000",
        "11110111010",
        "11000010100",
        "10001111010",
        "10100111100",
        "10010111100",
        "10010011110",
        "10111100100",
        "10011110100",
        "10011110010",
        "11110100100",
        "11110010100",
        "11110010010",
        "11011011110",
        "11011110110",
        "11110110110",
        "10101111000",
        "10100011110",
        "10001011110",
        // Visible character encoding for code table B ended.
        "10111101000",
        "10111100010",
        "11110101000",
        "11110100010",
        "10111011110", // to C from A, B (size - 8)
        "10111101110", // to B from A, C (size - 7)
        "11101011110", // to A from B, C (size - 6)
        "11110101110",
        "11010000100", // START A (size - 4)
        "11010010000", // START B (size - 3)
        "11010011100", // START C (size - 2)
        "11000111010"  // STOP    (size - 1)
    ]
}