1 // Copyright 2016 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 /*
7  * Copyright 2011 ZXing authors
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #include "xfa/fxbarcode/cbc_code128.h"
23 
24 #include "xfa/fxbarcode/oned/BC_OnedCode128Writer.h"
25 
CBC_Code128(BC_TYPE type)26 CBC_Code128::CBC_Code128(BC_TYPE type)
27     : CBC_OneCode(new CBC_OnedCode128Writer(type)) {}
28 
~CBC_Code128()29 CBC_Code128::~CBC_Code128() {}
30 
SetTextLocation(BC_TEXT_LOC location)31 FX_BOOL CBC_Code128::SetTextLocation(BC_TEXT_LOC location) {
32   if (m_pBCWriter)
33     return static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())
34         ->SetTextLocation(location);
35   return FALSE;
36 }
37 
Encode(const CFX_WideStringC & contents,FX_BOOL isDevice,int32_t & e)38 FX_BOOL CBC_Code128::Encode(const CFX_WideStringC& contents,
39                             FX_BOOL isDevice,
40                             int32_t& e) {
41   if (contents.IsEmpty()) {
42     e = BCExceptionNoContents;
43     return FALSE;
44   }
45   BCFORMAT format = BCFORMAT_CODE_128;
46   int32_t outWidth = 0;
47   int32_t outHeight = 0;
48   CFX_WideString content(contents);
49   if (contents.GetLength() % 2 &&
50       static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())->GetType() ==
51           BC_CODE128_C) {
52     content += '0';
53   }
54   CFX_WideString encodeContents =
55       static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())
56           ->FilterContents(content.AsStringC());
57   m_renderContents = encodeContents;
58   CFX_ByteString byteString = encodeContents.UTF8Encode();
59   uint8_t* data = static_cast<CBC_OnedCode128Writer*>(m_pBCWriter.get())
60                       ->Encode(byteString, format, outWidth, outHeight, e);
61   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
62   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
63       ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e);
64   FX_Free(data);
65   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
66   return TRUE;
67 }
68 
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix * matrix,int32_t & e)69 FX_BOOL CBC_Code128::RenderDevice(CFX_RenderDevice* device,
70                                   const CFX_Matrix* matrix,
71                                   int32_t& e) {
72   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
73       ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e);
74   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
75   return TRUE;
76 }
77 
RenderBitmap(CFX_DIBitmap * & pOutBitmap,int32_t & e)78 FX_BOOL CBC_Code128::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
79   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
80       ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e);
81   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
82   return TRUE;
83 }
84 
GetType()85 BC_TYPE CBC_Code128::GetType() {
86   return BC_CODE128;
87 }
88 
89