This repository has been archived by the owner on Oct 12, 2024. It is now read-only.
forked from Coledunsby/CDCodabarView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDCodabarEncoder.swift
119 lines (99 loc) · 4.23 KB
/
CDCodabarEncoder.swift
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
//
// CDCodabarEncoder.swift
// CDCodabarViewExample
//
// Created by Emil Wojtaszek on 10/04/2017.
// Copyright © 2017 CocoaPods. All rights reserved.
//
import Foundation
/*********************************************************************
CODABAR ENCODING TABLE (http://www.barcodeisland.com/codabar.phtml)
0 => space
1 => bar
-------------------------------------------------------
| ASCII | WIDTH | BARCODE |
| CHARACTER | ENCODING | ENCODING |
-------------------------------------------------------
| 0 | 0000011 | 101010011 |
| 1 | 0000110 | 101011001 |
| 2 | 0001001 | 101001011 |
| 3 | 1100000 | 110010101 |
| 4 | 0010010 | 101101001 |
| 5 | 1000010 | 110101001 |
| 6 | 0100001 | 100101011 |
| 7 | 0100100 | 100101101 |
| 8 | 0110000 | 100110101 |
| 9 | 1001000 | 110100101 |
| - (Dash) | 0001100 | 101001101 |
| $ | 0011000 | 101100101 |
| : (Colon) | 1000101 | 1101011011 |
| / (Slash) | 1010001 | 1101101011 |
| . (Point) | 1010100 | 1101101101 |
| + (Plus) | 0011111 | 101100110011 |
| Start/Stop A | 0011010 | 1011001001 |
| Start/Stop B | 0001011 | 1010010011 |
| Start/Stop C | 0101001 | 1001001011 |
| Start/Stop D | 0001110 | 1010011001 |
-------------------------------------------------------
*********************************************************************/
public struct CDCodabarEncoder {
private struct Constants {
static let minLength = 3
static let maxLength = 16
static let encodings: [Character: [Int]] = [
"0": [1, 0, 1, 0, 1, 0, 0, 1, 1],
"1": [1, 0, 1, 0, 1, 1, 0, 0, 1],
"2": [1, 0, 1, 0, 0, 1, 0, 1, 1],
"3": [1, 1, 0, 0, 1, 0, 1, 0, 1],
"4": [1, 0, 1, 1, 0, 1, 0, 0, 1],
"5": [1, 1, 0, 1, 0, 1, 0, 0, 1],
"6": [1, 0, 0, 1, 0, 1, 0, 1, 1],
"7": [1, 0, 0, 1, 0, 1, 1, 0, 1],
"8": [1, 0, 0, 1, 1, 0, 1, 0, 1],
"9": [1, 1, 0, 1, 0, 0, 1, 0, 1],
"-": [1, 0, 1, 0, 0, 1, 1, 0, 1],
"$": [1, 0, 1, 1, 0, 0, 1, 0, 1],
":": [1, 1, 0, 1, 0, 1, 1, 0, 1, 1],
"/": [1, 1, 0, 1, 1, 0, 1, 0, 1, 1],
".": [1, 1, 0, 1, 1, 0, 1, 1, 0, 1],
"+": [1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
"A": [1, 0, 1, 1, 0, 0, 1, 0, 0, 1],
"B": [1, 0, 1, 0, 0, 1, 0, 0, 1, 1],
"C": [1, 0, 0, 1, 0, 0, 1, 0, 1, 1],
"D": [1, 0, 1, 0, 0, 1, 1, 0, 0, 1]
]
}
enum Error: Swift.Error {
case invalidLength
case invalidStartCharacter
case invalidEndCharacter
case invalidIntermediateCharacter
}
private let code: String
public init(code: String) throws {
let code = code.uppercased()
guard (Constants.minLength ... Constants.maxLength) ~= code.characters.count else {
throw Error.invalidLength
}
guard let startChar = code.characters.first, ("A" ... "D") ~= startChar else {
throw Error.invalidStartCharacter
}
guard let stopChar = code.characters.last, ("A" ... "D") ~= stopChar else {
throw Error.invalidEndCharacter
}
guard code.characters.filter({ !Constants.encodings.keys.contains($0) }).isEmpty else {
throw Error.invalidIntermediateCharacter
}
self.code = code
}
/// Return sequence of bits representing Codabar
/// Each character is separated by `0`.
///
/// - Returns: Returns array of integer representing bits
public func sequence() -> [Int] {
return code.characters
.map { Constants.encodings[$0]! }
.joined(separator: [0])
.flatMap { $0 }
}
}