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_EncoderContext.h"
24 
25 #include <utility>
26 
27 #include "fxbarcode/common/BC_CommonBitMatrix.h"
28 #include "fxbarcode/datamatrix/BC_Encoder.h"
29 #include "fxbarcode/datamatrix/BC_SymbolInfo.h"
30 
CBC_EncoderContext(const WideString & msg)31 CBC_EncoderContext::CBC_EncoderContext(const WideString& msg) {
32   ByteString dststr = msg.ToUTF8();
33   size_t c = dststr.GetLength();
34   WideString sb;
35   sb.Reserve(c);
36   for (size_t i = 0; i < c; i++) {
37     wchar_t ch = static_cast<wchar_t>(dststr[i] & 0xff);
38     if (ch == '?' && dststr[i] != '?') {
39       m_bHasCharactersOutsideISO88591Encoding = true;
40     }
41     sb += ch;
42   }
43   m_msg = std::move(sb);
44   m_codewords.Reserve(m_msg.GetLength());
45 }
46 
47 CBC_EncoderContext::~CBC_EncoderContext() = default;
48 
setSkipAtEnd(int32_t count)49 void CBC_EncoderContext::setSkipAtEnd(int32_t count) {
50   m_skipAtEnd = count;
51 }
getCurrentChar()52 wchar_t CBC_EncoderContext::getCurrentChar() {
53   return m_msg[m_pos];
54 }
getCurrent()55 wchar_t CBC_EncoderContext::getCurrent() {
56   return m_msg[m_pos];
57 }
58 
writeCodewords(const WideString & codewords)59 void CBC_EncoderContext::writeCodewords(const WideString& codewords) {
60   m_codewords += codewords;
61 }
62 
writeCodeword(wchar_t codeword)63 void CBC_EncoderContext::writeCodeword(wchar_t codeword) {
64   m_codewords += codeword;
65 }
66 
getCodewordCount()67 size_t CBC_EncoderContext::getCodewordCount() {
68   return m_codewords.GetLength();
69 }
70 
SignalEncoderChange(CBC_HighLevelEncoder::Encoding encoding)71 void CBC_EncoderContext::SignalEncoderChange(
72     CBC_HighLevelEncoder::Encoding encoding) {
73   m_newEncoding = encoding;
74 }
75 
ResetEncoderSignal()76 void CBC_EncoderContext::ResetEncoderSignal() {
77   m_newEncoding = CBC_HighLevelEncoder::Encoding::UNKNOWN;
78 }
79 
hasMoreCharacters()80 bool CBC_EncoderContext::hasMoreCharacters() {
81   return m_pos < getTotalMessageCharCount();
82 }
83 
getRemainingCharacters()84 size_t CBC_EncoderContext::getRemainingCharacters() {
85   return getTotalMessageCharCount() - m_pos;
86 }
87 
UpdateSymbolInfo()88 bool CBC_EncoderContext::UpdateSymbolInfo() {
89   return UpdateSymbolInfo(getCodewordCount());
90 }
91 
UpdateSymbolInfo(size_t len)92 bool CBC_EncoderContext::UpdateSymbolInfo(size_t len) {
93   if (!m_symbolInfo || len > m_symbolInfo->dataCapacity()) {
94     m_symbolInfo = CBC_SymbolInfo::Lookup(len, m_bAllowRectangular);
95     if (!m_symbolInfo)
96       return false;
97   }
98   return true;
99 }
100 
resetSymbolInfo()101 void CBC_EncoderContext::resetSymbolInfo() {
102   m_bAllowRectangular = true;
103 }
104 
getTotalMessageCharCount()105 size_t CBC_EncoderContext::getTotalMessageCharCount() {
106   return m_msg.GetLength() - m_skipAtEnd;
107 }
108