Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
BC_EdifactEncoder.cpp
Go to the documentation of this file.
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
33namespace {
34
35WideString 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
53bool 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) {
95 context->m_pos -= restChars;
96 } else {
97 context->writeCodewords(encoded);
98 }
100 return true;
101}
102
103bool 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
119CBC_EdifactEncoder::CBC_EdifactEncoder() = default;
120
121CBC_EdifactEncoder::~CBC_EdifactEncoder() = default;
122
126
127bool 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);
144 CBC_HighLevelEncoder::LookAheadTest(context->m_msg, context->m_pos,
146 if (newMode != GetEncodingMode()) {
148 break;
149 }
150 }
151 }
152 buffer += static_cast<wchar_t>(31);
153 return HandleEOD(context, buffer);
154}
~CBC_EdifactEncoder() override
CBC_HighLevelEncoder::Encoding GetEncodingMode() override
bool Encode(CBC_EncoderContext *context) override
void writeCodewords(const WideString &codewords)
void SignalEncoderChange(CBC_HighLevelEncoder::Encoding encoding)
WideString & operator+=(wchar_t ch)
CharType operator[](const size_t index) const
Definition widestring.h:146
bool IsEmpty() const
Definition widestring.h:118