Home | History | Annotate | Download | only in fxbarcode
      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_ean13.h"
     23 
     24 #include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h"
     25 
     26 CBC_EAN13::CBC_EAN13() : CBC_OneCode(new CBC_OnedEAN13Writer) {}
     27 
     28 CBC_EAN13::~CBC_EAN13() {}
     29 
     30 CFX_WideString CBC_EAN13::Preprocess(const CFX_WideStringC& contents) {
     31   CFX_WideString encodeContents =
     32       static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter.get())
     33           ->FilterContents(contents);
     34   int32_t length = encodeContents.GetLength();
     35   if (length <= 12) {
     36     for (int32_t i = 0; i < 12 - length; i++)
     37       encodeContents = FX_WCHAR('0') + encodeContents;
     38 
     39     CFX_ByteString byteString = encodeContents.UTF8Encode();
     40     int32_t checksum = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter.get())
     41                            ->CalcChecksum(byteString);
     42     byteString += checksum - 0 + '0';
     43     encodeContents = byteString.UTF8Decode();
     44   }
     45   if (length > 13)
     46     encodeContents = encodeContents.Mid(0, 13);
     47 
     48   return encodeContents;
     49 }
     50 
     51 bool CBC_EAN13::Encode(const CFX_WideStringC& contents,
     52                        bool isDevice,
     53                        int32_t& e) {
     54   if (contents.IsEmpty()) {
     55     e = BCExceptionNoContents;
     56     return false;
     57   }
     58   BCFORMAT format = BCFORMAT_EAN_13;
     59   int32_t outWidth = 0;
     60   int32_t outHeight = 0;
     61   CFX_WideString encodeContents = Preprocess(contents);
     62   CFX_ByteString byteString = encodeContents.UTF8Encode();
     63   m_renderContents = encodeContents;
     64   uint8_t* data = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter.get())
     65                       ->Encode(byteString, format, outWidth, outHeight, e);
     66   if (e != BCExceptionNO)
     67     return false;
     68   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
     69       ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e);
     70   FX_Free(data);
     71   if (e != BCExceptionNO)
     72     return false;
     73   return true;
     74 }
     75 
     76 bool CBC_EAN13::RenderDevice(CFX_RenderDevice* device,
     77                              const CFX_Matrix* matrix,
     78                              int32_t& e) {
     79   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
     80       ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e);
     81   if (e != BCExceptionNO)
     82     return false;
     83   return true;
     84 }
     85 
     86 bool CBC_EAN13::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) {
     87   static_cast<CBC_OneDimWriter*>(m_pBCWriter.get())
     88       ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e);
     89   if (e != BCExceptionNO)
     90     return false;
     91   return true;
     92 }
     93 
     94 BC_TYPE CBC_EAN13::GetType() {
     95   return BC_EAN13;
     96 }
     97