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