• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fxbarcode/datamatrix/BC_C40Encoder.h"
24 
25 #include "core/fxcrt/fx_extension.h"
26 #include "fxbarcode/common/BC_CommonBitMatrix.h"
27 #include "fxbarcode/datamatrix/BC_Encoder.h"
28 #include "fxbarcode/datamatrix/BC_EncoderContext.h"
29 #include "fxbarcode/datamatrix/BC_HighLevelEncoder.h"
30 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
31 
32 namespace {
33 
EncodeToC40Codewords(const WideString & sb)34 WideString EncodeToC40Codewords(const WideString& sb) {
35   wchar_t c1 = sb[0];
36   wchar_t c2 = sb[1];
37   wchar_t c3 = sb[2];
38   int32_t v = (1600 * c1) + (40 * c2) + c3 + 1;
39   wchar_t cw[2];
40   cw[0] = static_cast<wchar_t>(v / 256);
41   cw[1] = static_cast<wchar_t>(v % 256);
42   return WideString(cw, FX_ArraySize(cw));
43 }
44 
45 }  // namespace
46 
47 CBC_C40Encoder::CBC_C40Encoder() = default;
48 
49 CBC_C40Encoder::~CBC_C40Encoder() = default;
50 
GetEncodingMode()51 CBC_HighLevelEncoder::Encoding CBC_C40Encoder::GetEncodingMode() {
52   return CBC_HighLevelEncoder::Encoding::C40;
53 }
54 
Encode(CBC_EncoderContext * context)55 bool CBC_C40Encoder::Encode(CBC_EncoderContext* context) {
56   WideString buffer;
57   while (context->hasMoreCharacters()) {
58     wchar_t c = context->getCurrentChar();
59     context->m_pos++;
60     int32_t lastCharSize = EncodeChar(c, &buffer);
61     if (lastCharSize <= 0)
62       return false;
63 
64     size_t unwritten = (buffer.GetLength() / 3) * 2;
65     int32_t curCodewordCount = context->getCodewordCount() + unwritten;
66     if (!context->UpdateSymbolInfo(curCodewordCount))
67       return false;
68 
69     int32_t available =
70         context->m_symbolInfo->dataCapacity() - curCodewordCount;
71     if (!context->hasMoreCharacters()) {
72       if ((buffer.GetLength() % 3) == 2) {
73         if (available < 2 || available > 2) {
74           lastCharSize = BacktrackOneCharacter(context, &buffer, lastCharSize);
75           if (lastCharSize < 0)
76             return false;
77         }
78       }
79       while ((buffer.GetLength() % 3) == 1 &&
80              ((lastCharSize <= 3 && available != 1) || lastCharSize > 3)) {
81         lastCharSize = BacktrackOneCharacter(context, &buffer, lastCharSize);
82         if (lastCharSize < 0)
83           return false;
84       }
85       break;
86     }
87     size_t count = buffer.GetLength();
88     if ((count % 3) == 0) {
89       CBC_HighLevelEncoder::Encoding newMode =
90           CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
91                                               GetEncodingMode());
92       if (newMode != GetEncodingMode()) {
93         context->SignalEncoderChange(newMode);
94         break;
95       }
96     }
97   }
98   return HandleEOD(context, &buffer);
99 }
100 
WriteNextTriplet(CBC_EncoderContext * context,WideString * buffer)101 void CBC_C40Encoder::WriteNextTriplet(CBC_EncoderContext* context,
102                                       WideString* buffer) {
103   context->writeCodewords(EncodeToC40Codewords(*buffer));
104   buffer->Delete(0, 3);
105 }
106 
HandleEOD(CBC_EncoderContext * context,WideString * buffer)107 bool CBC_C40Encoder::HandleEOD(CBC_EncoderContext* context,
108                                WideString* buffer) {
109   size_t unwritten = (buffer->GetLength() / 3) * 2;
110   size_t rest = buffer->GetLength() % 3;
111   int32_t curCodewordCount = context->getCodewordCount() + unwritten;
112   if (!context->UpdateSymbolInfo(curCodewordCount))
113     return false;
114 
115   int32_t available = context->m_symbolInfo->dataCapacity() - curCodewordCount;
116   if (rest == 2) {
117     *buffer += (wchar_t)'\0';
118     while (buffer->GetLength() >= 3)
119       WriteNextTriplet(context, buffer);
120     if (context->hasMoreCharacters()) {
121       context->writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
122     }
123   } else if (available == 1 && rest == 1) {
124     while (buffer->GetLength() >= 3)
125       WriteNextTriplet(context, buffer);
126     if (context->hasMoreCharacters()) {
127       context->writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
128     }
129     context->m_pos--;
130   } else if (rest == 0) {
131     while (buffer->GetLength() >= 3)
132       WriteNextTriplet(context, buffer);
133     if (available > 0 || context->hasMoreCharacters()) {
134       context->writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH);
135     }
136   } else {
137     return false;
138   }
139   context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
140   return true;
141 }
142 
EncodeChar(wchar_t c,WideString * sb)143 int32_t CBC_C40Encoder::EncodeChar(wchar_t c, WideString* sb) {
144   if (c == ' ') {
145     *sb += (wchar_t)'\3';
146     return 1;
147   }
148   if (FXSYS_IsDecimalDigit(c)) {
149     *sb += (wchar_t)(c - 48 + 4);
150     return 1;
151   }
152   if ((c >= 'A') && (c <= 'Z')) {
153     *sb += (wchar_t)(c - 65 + 14);
154     return 1;
155   }
156   if (c <= 0x1f) {
157     *sb += (wchar_t)'\0';
158     *sb += c;
159     return 2;
160   }
161   if ((c >= '!') && (c <= '/')) {
162     *sb += (wchar_t)'\1';
163     *sb += (wchar_t)(c - 33);
164     return 2;
165   }
166   if ((c >= ':') && (c <= '@')) {
167     *sb += (wchar_t)'\1';
168     *sb += (wchar_t)(c - 58 + 15);
169     return 2;
170   }
171   if ((c >= '[') && (c <= '_')) {
172     *sb += (wchar_t)'\1';
173     *sb += (wchar_t)(c - 91 + 22);
174     return 2;
175   }
176   if ((c >= 60) && (c <= 0x7f)) {
177     *sb += (wchar_t)'\2';
178     *sb += (wchar_t)(c - 96);
179     return 2;
180   }
181   if (c >= 80) {
182     *sb += (wchar_t)'\1';
183     *sb += (wchar_t)0x001e;
184     int32_t encode_result = EncodeChar(c - 128, sb);
185     return encode_result > 0 ? encode_result + 2 : 0;
186   }
187   return 0;
188 }
189 
BacktrackOneCharacter(CBC_EncoderContext * context,WideString * buffer,int32_t lastCharSize)190 int32_t CBC_C40Encoder::BacktrackOneCharacter(CBC_EncoderContext* context,
191                                               WideString* buffer,
192                                               int32_t lastCharSize) {
193   ASSERT(lastCharSize >= 0);
194 
195   if (context->m_pos < 1)
196     return -1;
197 
198   size_t count = buffer->GetLength();
199   if (count < static_cast<size_t>(lastCharSize))
200     return -1;
201 
202   buffer->Delete(count - lastCharSize, lastCharSize);
203   context->m_pos--;
204   wchar_t c = context->getCurrentChar();
205   WideString removed;
206   int32_t len = EncodeChar(c, &removed);
207   if (len <= 0)
208     return -1;
209 
210   context->resetSymbolInfo();
211   return len;
212 }
213