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 "fxbarcode/cbc_upca.h"
     23 
     24 #include <memory>
     25 
     26 #include "fxbarcode/oned/BC_OnedUPCAWriter.h"
     27 #include "third_party/base/ptr_util.h"
     28 
     29 CBC_UPCA::CBC_UPCA() : CBC_OneCode(pdfium::MakeUnique<CBC_OnedUPCAWriter>()) {}
     30 
     31 CBC_UPCA::~CBC_UPCA() {}
     32 
     33 WideString CBC_UPCA::Preprocess(const WideStringView& contents) {
     34   CBC_OnedUPCAWriter* pWriter = GetOnedUPCAWriter();
     35   WideString encodeContents = pWriter->FilterContents(contents);
     36   int32_t length = encodeContents.GetLength();
     37   if (length <= 11) {
     38     for (int32_t i = 0; i < 11 - length; i++)
     39       encodeContents = wchar_t('0') + encodeContents;
     40 
     41     ByteString byteString = encodeContents.UTF8Encode();
     42     int32_t checksum = pWriter->CalcChecksum(byteString);
     43     byteString += checksum - 0 + '0';
     44     encodeContents = byteString.UTF8Decode();
     45   }
     46   if (length > 12)
     47     encodeContents = encodeContents.Left(12);
     48 
     49   return encodeContents;
     50 }
     51 
     52 bool CBC_UPCA::Encode(const WideStringView& contents) {
     53   if (contents.IsEmpty())
     54     return false;
     55 
     56   BCFORMAT format = BCFORMAT_UPC_A;
     57   int32_t outWidth = 0;
     58   int32_t outHeight = 0;
     59   WideString encodeContents = Preprocess(contents);
     60   ByteString byteString = encodeContents.UTF8Encode();
     61   m_renderContents = encodeContents;
     62 
     63   CBC_OnedUPCAWriter* pWriter = GetOnedUPCAWriter();
     64   pWriter->Init();
     65   std::unique_ptr<uint8_t, FxFreeDeleter> data(
     66       pWriter->Encode(byteString, format, outWidth, outHeight));
     67   if (!data)
     68     return false;
     69   return pWriter->RenderResult(encodeContents.AsStringView(), data.get(),
     70                                outWidth);
     71 }
     72 
     73 bool CBC_UPCA::RenderDevice(CFX_RenderDevice* device,
     74                             const CFX_Matrix* matrix) {
     75   return GetOnedUPCAWriter()->RenderDeviceResult(
     76       device, matrix, m_renderContents.AsStringView());
     77 }
     78 
     79 BC_TYPE CBC_UPCA::GetType() {
     80   return BC_UPCA;
     81 }
     82 
     83 CBC_OnedUPCAWriter* CBC_UPCA::GetOnedUPCAWriter() {
     84   return static_cast<CBC_OnedUPCAWriter*>(m_pBCWriter.get());
     85 }
     86