OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 // Original code is licensed as follows: | |
7 /* | |
8 * Copyright 2006-2007 Jeremias Maerki. | |
9 * | |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | |
11 * you may not use this file except in compliance with the License. | |
12 * You may obtain a copy of the License at | |
13 * | |
14 * http://www.apache.org/licenses/LICENSE-2.0 | |
15 * | |
16 * Unless required by applicable law or agreed to in writing, software | |
17 * distributed under the License is distributed on an "AS IS" BASIS, | |
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
19 * See the License for the specific language governing permissions and | |
20 * limitations under the License. | |
21 */ | |
22 | |
23 #include "barcode.h" | |
24 #include "include/BC_Encoder.h" | |
25 #include "include/BC_Dimension.h" | |
26 #include "include/BC_CommonBitMatrix.h" | |
27 #include "include/BC_SymbolShapeHint.h" | |
28 #include "include/BC_SymbolInfo.h" | |
29 #include "include/BC_EncoderContext.h" | |
30 #include "include/BC_HighLevelEncoder.h" | |
31 #include "include/BC_C40Encoder.h" | |
32 CBC_C40Encoder::CBC_C40Encoder() | |
33 { | |
34 } | |
35 CBC_C40Encoder::~CBC_C40Encoder() | |
36 { | |
37 } | |
38 FX_INT32 CBC_C40Encoder::getEncodingMode() | |
39 { | |
40 return C40_ENCODATION; | |
41 } | |
42 void CBC_C40Encoder::Encode(CBC_EncoderContext &context, FX_INT32 &e) | |
43 { | |
44 CFX_WideString buffer; | |
45 while (context.hasMoreCharacters()) { | |
46 FX_WCHAR c = context.getCurrentChar(); | |
47 context.m_pos++; | |
48 FX_INT32 lastCharSize = encodeChar(c, buffer, e); | |
49 if (e != BCExceptionNO) { | |
50 return; | |
51 } | |
52 FX_INT32 unwritten = (buffer.GetLength() / 3) * 2; | |
53 FX_INT32 curCodewordCount = context.getCodewordCount() + unwritten; | |
54 context.updateSymbolInfo(curCodewordCount, e); | |
55 if (e != BCExceptionNO) { | |
56 return; | |
57 } | |
58 FX_INT32 available = context.m_symbolInfo->m_dataCapacity - curCodewordC
ount; | |
59 if (!context.hasMoreCharacters()) { | |
60 CFX_WideString removed; | |
61 if ((buffer.GetLength() % 3) == 2) { | |
62 if (available < 2 || available > 2) { | |
63 lastCharSize = backtrackOneCharacter(context, buffer, remove
d, lastCharSize, e); | |
64 if (e != BCExceptionNO) { | |
65 return; | |
66 } | |
67 } | |
68 } | |
69 while ((buffer.GetLength() % 3) == 1 | |
70 && ((lastCharSize <= 3 && available != 1) || lastCharSize >
3)) { | |
71 lastCharSize = backtrackOneCharacter(context, buffer, removed, l
astCharSize, e); | |
72 if (e != BCExceptionNO) { | |
73 return; | |
74 } | |
75 } | |
76 break; | |
77 } | |
78 FX_INT32 count = buffer.GetLength(); | |
79 if ((count % 3) == 0) { | |
80 FX_INT32 newMode = CBC_HighLevelEncoder::lookAheadTest(context.m_msg
, context.m_pos, getEncodingMode()); | |
81 if (newMode != getEncodingMode()) { | |
82 context.signalEncoderChange(newMode); | |
83 break; | |
84 } | |
85 } | |
86 } | |
87 handleEOD(context, buffer, e); | |
88 } | |
89 void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext &context, CFX_WideStrin
g &buffer) | |
90 { | |
91 context.writeCodewords(encodeToCodewords(buffer, 0)); | |
92 buffer.Delete(0, 3); | |
93 } | |
94 void CBC_C40Encoder::handleEOD(CBC_EncoderContext &context, CFX_WideString &buff
er, FX_INT32 &e) | |
95 { | |
96 FX_INT32 unwritten = (buffer.GetLength() / 3) * 2; | |
97 FX_INT32 rest = buffer.GetLength() % 3; | |
98 FX_INT32 curCodewordCount = context.getCodewordCount() + unwritten; | |
99 context.updateSymbolInfo(curCodewordCount, e); | |
100 if (e != BCExceptionNO) { | |
101 return; | |
102 } | |
103 FX_INT32 available = context.m_symbolInfo->m_dataCapacity - curCodewordCount
; | |
104 if (rest == 2) { | |
105 buffer += (FX_WCHAR)'\0'; | |
106 while (buffer.GetLength() >= 3) { | |
107 writeNextTriplet(context, buffer); | |
108 } | |
109 if (context.hasMoreCharacters()) { | |
110 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); | |
111 } | |
112 } else if (available == 1 && rest == 1) { | |
113 while (buffer.GetLength() >= 3) { | |
114 writeNextTriplet(context, buffer); | |
115 } | |
116 if (context.hasMoreCharacters()) { | |
117 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); | |
118 } | |
119 context.m_pos--; | |
120 } else if (rest == 0) { | |
121 while (buffer.GetLength() >= 3) { | |
122 writeNextTriplet(context, buffer); | |
123 } | |
124 if (available > 0 || context.hasMoreCharacters()) { | |
125 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); | |
126 } | |
127 } else { | |
128 e = BCExceptionIllegalStateUnexpectedCase; | |
129 return; | |
130 } | |
131 context.signalEncoderChange(ASCII_ENCODATION); | |
132 } | |
133 FX_INT32 CBC_C40Encoder::encodeChar(FX_WCHAR c, CFX_WideString &sb, FX_INT32 &e) | |
134 { | |
135 if (c == ' ') { | |
136 sb += (FX_WCHAR)'\3'; | |
137 return 1; | |
138 } else if ((c >= '0') && (c <= '9')) { | |
139 sb += (FX_WCHAR)(c - 48 + 4); | |
140 return 1; | |
141 } else if ((c >= 'A') && (c <= 'Z')) { | |
142 sb += (FX_WCHAR)(c - 65 + 14); | |
143 return 1; | |
144 } else if ((c >= '\0') && (c <= 0x1f)) { | |
145 sb += (FX_WCHAR)'\0'; | |
146 sb += c; | |
147 return 2; | |
148 } else if ((c >= '!') && (c <= '/')) { | |
149 sb += (FX_WCHAR)'\1'; | |
150 sb += (FX_WCHAR)(c - 33); | |
151 return 2; | |
152 } else if ((c >= ':') && (c <= '@')) { | |
153 sb += (FX_WCHAR)'\1'; | |
154 sb += (FX_WCHAR)(c - 58 + 15); | |
155 return 2; | |
156 } else if ((c >= '[') && (c <= '_')) { | |
157 sb += (FX_WCHAR)'\1'; | |
158 sb += (FX_WCHAR)(c - 91 + 22); | |
159 return 2; | |
160 } else if ((c >= 60) && (c <= 0x7f)) { | |
161 sb += (FX_WCHAR)'\2'; | |
162 sb += (FX_WCHAR)(c - 96); | |
163 return 2; | |
164 } else if (c >= 80) { | |
165 sb += (FX_WCHAR)'\1'; | |
166 sb += (FX_WCHAR)0x001e; | |
167 FX_INT32 len = 2; | |
168 len += encodeChar((c - 128), sb, e); | |
169 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | |
170 return len; | |
171 } else { | |
172 e = BCExceptionIllegalArgument; | |
173 return 0; | |
174 } | |
175 } | |
176 FX_INT32 CBC_C40Encoder::backtrackOneCharacter(CBC_EncoderContext &context, CFX_
WideString &buffer, CFX_WideString &removed, FX_INT32 lastCharSize, FX_INT32 &e) | |
177 { | |
178 FX_INT32 count = buffer.GetLength(); | |
179 buffer.Delete(count - lastCharSize, count); | |
180 context.m_pos--; | |
181 FX_WCHAR c = context.getCurrentChar(); | |
182 lastCharSize = encodeChar(c, removed, e); | |
183 BC_EXCEPTION_CHECK_ReturnValue(e, -1); | |
184 context.resetSymbolInfo(); | |
185 return lastCharSize; | |
186 } | |
187 CFX_WideString CBC_C40Encoder::encodeToCodewords(CFX_WideString sb, FX_INT32 sta
rtPos) | |
188 { | |
189 FX_WCHAR c1 = sb.GetAt(startPos); | |
190 FX_WCHAR c2 = sb.GetAt(startPos + 1); | |
191 FX_WCHAR c3 = sb.GetAt(startPos + 2); | |
192 FX_INT32 v = (1600 * c1) + (40 * c2) + c3 + 1; | |
193 FX_WCHAR cw1 = (FX_WCHAR) (v / 256); | |
194 FX_WCHAR cw2 = (FX_WCHAR) (v % 256); | |
195 CFX_WideString b1(cw1); | |
196 CFX_WideString b2(cw2); | |
197 return b1 + b2; | |
198 } | |
OLD | NEW |