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