• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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_X12Encoder.h"
24 
25 #include "core/fxcrt/fx_extension.h"
26 #include "fxbarcode/datamatrix/BC_C40Encoder.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 CBC_X12Encoder::CBC_X12Encoder() = default;
33 
34 CBC_X12Encoder::~CBC_X12Encoder() = default;
35 
GetEncodingMode()36 CBC_HighLevelEncoder::Encoding CBC_X12Encoder::GetEncodingMode() {
37   return CBC_HighLevelEncoder::Encoding::X12;
38 }
39 
Encode(CBC_EncoderContext * context)40 bool CBC_X12Encoder::Encode(CBC_EncoderContext* context) {
41   WideString buffer;
42   while (context->hasMoreCharacters()) {
43     wchar_t c = context->getCurrentChar();
44     context->m_pos++;
45     if (EncodeChar(c, &buffer) <= 0)
46       return false;
47 
48     size_t count = buffer.GetLength();
49     if ((count % 3) == 0) {
50       WriteNextTriplet(context, &buffer);
51       CBC_HighLevelEncoder::Encoding newMode =
52           CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
53                                               GetEncodingMode());
54       if (newMode != GetEncodingMode()) {
55         context->SignalEncoderChange(newMode);
56         break;
57       }
58     }
59   }
60   return HandleEOD(context, &buffer);
61 }
62 
HandleEOD(CBC_EncoderContext * context,WideString * buffer)63 bool CBC_X12Encoder::HandleEOD(CBC_EncoderContext* context,
64                                WideString* buffer) {
65   if (!context->UpdateSymbolInfo())
66     return false;
67 
68   int32_t available =
69       context->m_symbolInfo->data_capacity() - context->getCodewordCount();
70   size_t count = buffer->GetLength();
71   if (count == 2) {
72     context->writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH);
73     context->m_pos -= 2;
74     context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
75   } else if (count == 1) {
76     context->m_pos--;
77     if (available > 1) {
78       context->writeCodeword(CBC_HighLevelEncoder::X12_UNLATCH);
79     }
80     context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
81   }
82   return true;
83 }
84 
EncodeChar(wchar_t c,WideString * sb)85 int32_t CBC_X12Encoder::EncodeChar(wchar_t c, WideString* sb) {
86   if (c == '\r')
87     *sb += (wchar_t)'\0';
88   else if (c == '*')
89     *sb += (wchar_t)'\1';
90   else if (c == '>')
91     *sb += (wchar_t)'\2';
92   else if (c == ' ')
93     *sb += (wchar_t)'\3';
94   else if (FXSYS_IsDecimalDigit(c))
95     *sb += (wchar_t)(c - 48 + 4);
96   else if (FXSYS_IsUpperASCII(c))
97     *sb += (wchar_t)(c - 65 + 14);
98   else
99     return 0;
100   return 1;
101 }
102