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