xref: /aosp_15_r20/external/pdfium/fxbarcode/datamatrix/BC_EdifactEncoder.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_EdifactEncoder.h"
24 
25 #include <algorithm>
26 
27 #include "fxbarcode/common/BC_CommonBitMatrix.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 namespace {
34 
EncodeToEdifactCodewords(const WideString & sb)35 WideString EncodeToEdifactCodewords(const WideString& sb) {
36   size_t len = sb.GetLength();
37   if (len == 0)
38     return WideString();
39 
40   wchar_t c1 = sb[0];
41   wchar_t c2 = len >= 2 ? sb[1] : 0;
42   wchar_t c3 = len >= 3 ? sb[2] : 0;
43   wchar_t c4 = len >= 4 ? sb[3] : 0;
44   int32_t v = (c1 << 18) + (c2 << 12) + (c3 << 6) + c4;
45   constexpr size_t kBuflen = 3;
46   wchar_t cw[kBuflen];
47   cw[0] = static_cast<wchar_t>((v >> 16) & 255);
48   cw[1] = static_cast<wchar_t>((v >> 8) & 255);
49   cw[2] = static_cast<wchar_t>(v & 255);
50   return WideString(cw, std::min(len, kBuflen));
51 }
52 
HandleEOD(CBC_EncoderContext * context,const WideString & buffer)53 bool HandleEOD(CBC_EncoderContext* context, const WideString& buffer) {
54   size_t count = buffer.GetLength();
55   if (count == 0)
56     return true;
57   if (count > 4)
58     return false;
59 
60   if (count == 1) {
61     if (!context->UpdateSymbolInfo())
62       return false;
63 
64     int32_t available =
65         context->m_symbolInfo->data_capacity() - context->getCodewordCount();
66     int32_t remaining = context->getRemainingCharacters();
67     if (remaining == 0 && available <= 2)
68       return true;
69   }
70 
71   int32_t restChars = count - 1;
72   WideString encoded = EncodeToEdifactCodewords(buffer);
73   if (encoded.IsEmpty())
74     return false;
75 
76   bool endOfSymbolReached = !context->hasMoreCharacters();
77   bool restInAscii = endOfSymbolReached && restChars <= 2;
78   if (restChars <= 2) {
79     if (!context->UpdateSymbolInfo(context->getCodewordCount() + restChars))
80       return false;
81 
82     int32_t available =
83         context->m_symbolInfo->data_capacity() - context->getCodewordCount();
84     if (available >= 3) {
85       restInAscii = false;
86       if (!context->UpdateSymbolInfo(context->getCodewordCount() +
87                                      encoded.GetLength())) {
88         return false;
89       }
90     }
91   }
92 
93   if (restInAscii) {
94     context->resetSymbolInfo();
95     context->m_pos -= restChars;
96   } else {
97     context->writeCodewords(encoded);
98   }
99   context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
100   return true;
101 }
102 
AppendEncodedChar(wchar_t c,WideString * sb)103 bool AppendEncodedChar(wchar_t c, WideString* sb) {
104   if (c >= ' ' && c <= '?') {
105     *sb += c;
106     return true;
107   }
108 
109   if (c >= '@' && c <= '^') {
110     *sb += (c - 64);
111     return true;
112   }
113 
114   return false;
115 }
116 
117 }  // namespace
118 
119 CBC_EdifactEncoder::CBC_EdifactEncoder() = default;
120 
121 CBC_EdifactEncoder::~CBC_EdifactEncoder() = default;
122 
GetEncodingMode()123 CBC_HighLevelEncoder::Encoding CBC_EdifactEncoder::GetEncodingMode() {
124   return CBC_HighLevelEncoder::Encoding::EDIFACT;
125 }
126 
Encode(CBC_EncoderContext * context)127 bool CBC_EdifactEncoder::Encode(CBC_EncoderContext* context) {
128   WideString buffer;
129   while (context->hasMoreCharacters()) {
130     wchar_t c = context->getCurrentChar();
131     if (!AppendEncodedChar(c, &buffer))
132       return false;
133 
134     context->m_pos++;
135     size_t count = buffer.GetLength();
136     if (count >= 4) {
137       WideString encoded = EncodeToEdifactCodewords(buffer);
138       if (encoded.IsEmpty())
139         return false;
140 
141       context->writeCodewords(encoded);
142       buffer.Delete(0, 4);
143       CBC_HighLevelEncoder::Encoding newMode =
144           CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
145                                               GetEncodingMode());
146       if (newMode != GetEncodingMode()) {
147         context->SignalEncoderChange(CBC_HighLevelEncoder::Encoding::ASCII);
148         break;
149       }
150     }
151   }
152   buffer += static_cast<wchar_t>(31);
153   return HandleEOD(context, buffer);
154 }
155