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 "xfa/src/fxbarcode/barcode.h"
24 #include "xfa/src/fxbarcode/BC_Dimension.h"
25 #include "xfa/src/fxbarcode/BC_UtilCodingConvert.h"
26 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h"
27 #include "BC_Encoder.h"
28 #include "BC_SymbolShapeHint.h"
29 #include "BC_SymbolInfo.h"
30 #include "BC_EncoderContext.h"
CBC_EncoderContext(const CFX_WideString msg,CFX_WideString ecLevel,int32_t & e)31 CBC_EncoderContext::CBC_EncoderContext(const CFX_WideString msg,
32                                        CFX_WideString ecLevel,
33                                        int32_t& e) {
34   CFX_ByteString dststr;
35   CBC_UtilCodingConvert::UnicodeToUTF8(msg, dststr);
36   CFX_WideString sb;
37   int32_t c = dststr.GetLength();
38   for (int32_t i = 0; i < c; i++) {
39     FX_WCHAR ch = (FX_WCHAR)(dststr.GetAt(i) & 0xff);
40     if (ch == '?' && dststr.GetAt(i) != '?') {
41       e = BCExceptionCharactersOutsideISO88591Encoding;
42     }
43     sb += ch;
44   }
45   m_msg = sb;
46   m_shape = FORCE_NONE;
47   m_newEncoding = -1;
48   m_pos = 0;
49   m_symbolInfo = NULL;
50   m_skipAtEnd = 0;
51   m_maxSize = NULL;
52   m_minSize = NULL;
53 }
~CBC_EncoderContext()54 CBC_EncoderContext::~CBC_EncoderContext() {}
setSymbolShape(SymbolShapeHint shape)55 void CBC_EncoderContext::setSymbolShape(SymbolShapeHint shape) {
56   m_shape = shape;
57 }
setSizeConstraints(CBC_Dimension * minSize,CBC_Dimension * maxSize)58 void CBC_EncoderContext::setSizeConstraints(CBC_Dimension* minSize,
59                                             CBC_Dimension* maxSize) {
60   m_maxSize = maxSize;
61   m_minSize = minSize;
62 }
getMessage()63 CFX_WideString CBC_EncoderContext::getMessage() {
64   return m_msg;
65 }
setSkipAtEnd(int32_t count)66 void CBC_EncoderContext::setSkipAtEnd(int32_t count) {
67   m_skipAtEnd = count;
68 }
getCurrentChar()69 FX_WCHAR CBC_EncoderContext::getCurrentChar() {
70   return m_msg.GetAt(m_pos);
71 }
getCurrent()72 FX_WCHAR CBC_EncoderContext::getCurrent() {
73   return m_msg.GetAt(m_pos);
74 }
writeCodewords(CFX_WideString codewords)75 void CBC_EncoderContext::writeCodewords(CFX_WideString codewords) {
76   m_codewords += codewords;
77 }
writeCodeword(FX_WCHAR codeword)78 void CBC_EncoderContext::writeCodeword(FX_WCHAR codeword) {
79   m_codewords += codeword;
80 }
getCodewordCount()81 int32_t CBC_EncoderContext::getCodewordCount() {
82   return m_codewords.GetLength();
83 }
signalEncoderChange(int32_t encoding)84 void CBC_EncoderContext::signalEncoderChange(int32_t encoding) {
85   m_newEncoding = encoding;
86 }
resetEncoderSignal()87 void CBC_EncoderContext::resetEncoderSignal() {
88   m_newEncoding = -1;
89 }
hasMoreCharacters()90 FX_BOOL CBC_EncoderContext::hasMoreCharacters() {
91   return m_pos < getTotalMessageCharCount();
92 }
getRemainingCharacters()93 int32_t CBC_EncoderContext::getRemainingCharacters() {
94   return getTotalMessageCharCount() - m_pos;
95 }
updateSymbolInfo(int32_t & e)96 void CBC_EncoderContext::updateSymbolInfo(int32_t& e) {
97   updateSymbolInfo(getCodewordCount(), e);
98 }
updateSymbolInfo(int32_t len,int32_t & e)99 void CBC_EncoderContext::updateSymbolInfo(int32_t len, int32_t& e) {
100   if (m_symbolInfo == NULL || len > m_symbolInfo->m_dataCapacity) {
101     m_symbolInfo =
102         CBC_SymbolInfo::lookup(len, m_shape, m_minSize, m_maxSize, true, e);
103     BC_EXCEPTION_CHECK_ReturnVoid(e);
104   }
105 }
resetSymbolInfo()106 void CBC_EncoderContext::resetSymbolInfo() {
107   m_shape = FORCE_NONE;
108 }
getTotalMessageCharCount()109 int32_t CBC_EncoderContext::getTotalMessageCharCount() {
110   return m_msg.GetLength() - m_skipAtEnd;
111 }
112