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_code39.h"
23 
24 #include "xfa/fxbarcode/oned/BC_OnedCode39Writer.h"
25 
CBC_Code39()26 CBC_Code39::CBC_Code39() : CBC_OneCode(new CBC_OnedCode39Writer) {}
27 
~CBC_Code39()28 CBC_Code39::~CBC_Code39() {}
29 
Encode(const CFX_WideStringC & contents,FX_BOOL isDevice,int32_t & e)30 FX_BOOL CBC_Code39::Encode(const CFX_WideStringC& contents,
31                            FX_BOOL isDevice,
32                            int32_t& e) {
33   if (contents.IsEmpty()) {
34     e = BCExceptionNoContents;
35     return FALSE;
36   }
37   BCFORMAT format = BCFORMAT_CODE_39;
38   int32_t outWidth = 0;
39   int32_t outHeight = 0;
40   CFX_WideString filtercontents =
41       static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
42           ->FilterContents(contents);
43   CFX_WideString renderContents =
44       static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
45           ->RenderTextContents(contents);
46   m_renderContents = renderContents;
47   CFX_ByteString byteString = filtercontents.UTF8Encode();
48   uint8_t* data = static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
49                       ->Encode(byteString, format, outWidth, outHeight, e);
50   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
51   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
52       ->RenderResult(renderContents.AsStringC(), data, outWidth, isDevice, e);
53   FX_Free(data);
54   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
55   return TRUE;
56 }
57 
RenderDevice(CFX_RenderDevice * device,const CFX_Matrix * matrix,int32_t & e)58 FX_BOOL CBC_Code39::RenderDevice(CFX_RenderDevice* device,
59                                  const CFX_Matrix* matrix,
60                                  int32_t& e) {
61   CFX_WideString renderCon =
62       static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
63           ->encodedContents(m_renderContents.AsStringC(), e);
64   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
65       ->RenderDeviceResult(device, matrix, renderCon.AsStringC(), e);
66   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
67   return TRUE;
68 }
69 
RenderBitmap(CFX_DIBitmap * & pOutBitmap,int32_t & e)70 FX_BOOL CBC_Code39::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
71   CFX_WideString renderCon =
72       static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
73           ->encodedContents(m_renderContents.AsStringC(), e);
74   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
75       ->RenderBitmapResult(pOutBitmap, renderCon.AsStringC(), e);
76   BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);
77   return TRUE;
78 }
79 
GetType()80 BC_TYPE CBC_Code39::GetType() {
81   return BC_CODE39;
82 }
83 
SetTextLocation(BC_TEXT_LOC location)84 FX_BOOL CBC_Code39::SetTextLocation(BC_TEXT_LOC location) {
85   if (m_pBCWriter)
86     return static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
87         ->SetTextLocation(location);
88   return FALSE;
89 }
90 
SetWideNarrowRatio(int32_t ratio)91 FX_BOOL CBC_Code39::SetWideNarrowRatio(int32_t ratio) {
92   if (m_pBCWriter)
93     return static_cast<CBC_OnedCode39Writer*>(m_pBCWriter.get())
94         ->SetWideNarrowRatio(ratio);
95   return FALSE;
96 }
97