Debian Codegraph @

text_encoder.go

raw symbols SCIP
1 package pdf417
2
3 // Code word used to switch to Text mode.
4 const TEXT_SWITCH_CODE_WORD int = 900
5
6 // Since each code word consists of 2 characters, a padding value is
7 // needed when encoding a single character. 29 is used as padding because
8 // it's a switch in all 4 submodes, and doesn't add any data.
9 const PADDING_VALUE = 29
10
11 // Uppercase submode.
12 const SUBMODE_UPPER = "SUBMODE_UPPER"
13
14 // Lowercase submode.
15 const SUBMODE_LOWER = "SUBMODE_LOWER"
16
17 // mixed submode (numbers and some punctuation).
18 const SUBMODE_MIXED = "SUBMODE_MIXED"
19
20 // Punctuation submode.
21 const SUBMODE_PUNCT = "SUBMODE_PUNCT"
22
23 // Switch to uppercase submode.
24 const SWITCH_UPPER = "SWITCH_UPPER"
25
26 // Switch to uppercase submode for a single character.
27 const SWITCH_UPPER_SINGLE = "SWITCH_UPPER_SINGLE"
28
29 // Switch to lowercase submode.
30 const SWITCH_LOWER = "SWITCH_LOWER"
31
32 // Switch to mixed submode.
33 const SWITCH_MIXED = "SWITCH_MIXED"
34
35 // Switch to punctuation submode.
36 const SWITCH_PUNCT = "SWITCH_PUNCT"
37
38 // Switch to punctuation submode for a single character.
39 const SWITCH_PUNCT_SINGLE = "SWITCH_PUNCT_SINGLE"
40
41 type TextEncoder struct {
42 CharacterTables map[string][]string
43 Switching map[string]map[string][]string
44 SwitchSubmode map[string]string
45 ReverseLookup map[string]map[string]int
46 }
47
48 func CreateTextEncoder() *TextEncoder {
49 encoder := new(TextEncoder)
50 encoder.CharacterTables = map[string][]string{
51 SUBMODE_UPPER: []string{
52 "A", "B", "C", "D", "E", "F", "G", "H", "I",
53 "J", "K", "L", "M", "N", "O", "P", "Q", "R",
54 "S", "T", "U", "V", "W", "X", "Y", "Z", " ",
55 SWITCH_LOWER,
56 SWITCH_MIXED,
57 SWITCH_PUNCT_SINGLE,
58 },
59
60 SUBMODE_LOWER: []string{
61 "a", "b", "c", "d", "e", "f", "g", "h", "i",
62 "j", "k", "l", "m", "n", "o", "p", "q", "r",
63 "s", "t", "u", "v", "w", "x", "y", "z", " ",
64 SWITCH_UPPER_SINGLE,
65 SWITCH_MIXED,
66 SWITCH_PUNCT_SINGLE,
67 },
68
69 SUBMODE_MIXED: []string{
70 "0", "1", "2", "3", "4", "5", "6", "7", "8",
71 "9", "&", "\r", "\t", ",", ":", "#", "-", ".",
72 "$", "/", "+", "%", "*", "=", "^",
73 SWITCH_PUNCT, " ",
74 SWITCH_LOWER,
75 SWITCH_UPPER,
76 SWITCH_PUNCT_SINGLE,
77 },
78
79 SUBMODE_PUNCT: []string{
80 ";", "<", ">", "@", "[", "\\", "]", "_", "`",
81 "~", "!", "\r", "\t", ",", ":", "\n", "-", ".",
82 "$", "/", "\"", "|", "*", "(", ")", "?", "{", "}", "'",
83 SWITCH_UPPER,
84 },
85 }
86
87 encoder.Switching = map[string]map[string][]string{
88 SUBMODE_UPPER: map[string][]string{
89 SUBMODE_LOWER: []string{SWITCH_LOWER},
90 SUBMODE_MIXED: []string{SWITCH_MIXED},
91 SUBMODE_PUNCT: []string{SWITCH_MIXED, SWITCH_PUNCT},
92 },
93
94 SUBMODE_LOWER: map[string][]string{
95 SUBMODE_UPPER: []string{SWITCH_MIXED, SWITCH_UPPER},
96 SUBMODE_MIXED: []string{SWITCH_MIXED},
97 SUBMODE_PUNCT: []string{SWITCH_MIXED, SWITCH_PUNCT},
98 },
99
100 SUBMODE_MIXED: map[string][]string{
101 SUBMODE_UPPER: []string{SWITCH_UPPER},
102 SUBMODE_LOWER: []string{SWITCH_LOWER},
103 SUBMODE_PUNCT: []string{SWITCH_PUNCT},
104 },
105
106 SUBMODE_PUNCT: map[string][]string{
107 SUBMODE_UPPER: []string{SWITCH_UPPER},
108 SUBMODE_LOWER: []string{SWITCH_UPPER, SWITCH_LOWER},
109 SUBMODE_MIXED: []string{SWITCH_UPPER, SWITCH_MIXED},
110 },
111 }
112
113 encoder.SwitchSubmode = map[string]string{
114 SWITCH_UPPER: SUBMODE_UPPER,
115 SWITCH_LOWER: SUBMODE_LOWER,
116 SWITCH_PUNCT: SUBMODE_PUNCT,
117 SWITCH_MIXED: SUBMODE_MIXED,
118 }
119
120 encoder.ReverseLookup = make(map[string]map[string]int)
121 for submode, codes := range encoder.CharacterTables {
122 for row, char := range codes {
123 if encoder.ReverseLookup[char] == nil {
124 encoder.ReverseLookup[char] = make(map[string]int)
125 }
126
127 encoder.ReverseLookup[char][submode] = int(row)
128 }
129 }
130
131 return encoder
132 }
133
134 func (encoder TextEncoder) GetName() string {
135 return "text"
136 }
137
138 func (encoder TextEncoder) CanEncode(char string) bool {
139 return encoder.ReverseLookup[char] != nil
140 }
141
142 func (TextEncoder) GetSwitchCode(data string) int {
143 return TEXT_SWITCH_CODE_WORD
144 }
145
146 func (encoder TextEncoder) Encode(data string, addSwitchCode bool) []int {
147 interim := encodeinterim(encoder, data);
148
149 return encodeFinal(interim, addSwitchCode)
150 }
151
152 func encodeinterim(encoder TextEncoder, data string) []int {
153 submode := SUBMODE_UPPER
154
155 codes := []int{}
156
157 for i := 0; i < len(data); i++ {
158 char := string(data[i])
159
160 if (existsInSubmode(encoder, char, submode) == false) {
161 prevSubmode := submode
162
163 submode = getSubmode(encoder, char)
164
165 switchCodes := getSwitchCodes(encoder, prevSubmode, submode)
166
167 codes = append(codes, switchCodes...)
168 }
169
170 codes = append(
171 codes,
172 getCharacterCode(encoder, char, submode),
173 )
174 }
175
176 return codes
177 }
178
179 func getSubmode(encoder TextEncoder, char string) string {
180 _, ok := encoder.ReverseLookup[char]
181
182 if ! ok {
183 panic("Weird, not found")
184 }
185
186 for key := range encoder.ReverseLookup[char] {
187 return key
188 }
189
190 return "INVALID"
191 }
192
193 func getSwitchCodes(encoder TextEncoder, from string, to string) []int {
194 switches := encoder.Switching[from][to]
195
196 codes := []int{}
197
198 for _, switcher := range switches {
199 codes = append(codes, getCharacterCode(encoder, switcher, from))
200
201 from = encoder.SwitchSubmode[switcher]
202 }
203
204 return codes
205 }
206
207 func encodeFinal(codes []int, addSwitchCode bool) []int {
208 codeWords := []int{}
209
210 if addSwitchCode {
211 codeWords = append(codeWords, TEXT_SWITCH_CODE_WORD)
212 }
213
214 chunks := [][]int{}
215 chunkPart := []int{}
216 i := 0
217 for _, code := range codes {
218 chunkPart = append(chunkPart, code)
219
220 i++
221
222 if i % 2 == 0 {
223 chunks = append(chunks, chunkPart)
224
225 chunkPart = []int{}
226 }
227 }
228
229 if len(chunkPart) > 0 {
230 chunks = append(chunks, chunkPart)
231 }
232
233 for _, chunk := range chunks {
234 if len(chunk) == 1 {
235 chunk = append(chunk, PADDING_VALUE)
236 }
237
238 codeWords = append(
239 codeWords,
240 30 * chunk[0] + chunk[1],
241 )
242 }
243
244 return codeWords
245 }
246
247 func getCharacterCode(encoder TextEncoder, char string, submode string) int {
248 cw, ok := encoder.ReverseLookup[char][submode]
249
250 if ! ok {
251 panic("This is not possible")
252 }
253
254 return cw
255 }
256
257 func existsInSubmode(encoder TextEncoder, char string, submode string) bool {
258 _, ok := encoder.ReverseLookup[char][submode]
259
260 return ok
261 }