Home | History | Annotate | Download | only in fwl
      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 
      7 #include "xfa/fwl/cfx_barcode.h"
      8 
      9 #include "xfa/fxbarcode/cbc_codabar.h"
     10 #include "xfa/fxbarcode/cbc_code128.h"
     11 #include "xfa/fxbarcode/cbc_code39.h"
     12 #include "xfa/fxbarcode/cbc_codebase.h"
     13 #include "xfa/fxbarcode/cbc_datamatrix.h"
     14 #include "xfa/fxbarcode/cbc_ean13.h"
     15 #include "xfa/fxbarcode/cbc_ean8.h"
     16 #include "xfa/fxbarcode/cbc_pdf417i.h"
     17 #include "xfa/fxbarcode/cbc_qrcode.h"
     18 #include "xfa/fxbarcode/cbc_upca.h"
     19 #include "xfa/fxbarcode/utils.h"
     20 
     21 namespace {
     22 
     23 CBC_CodeBase* CreateBarCodeEngineObject(BC_TYPE type) {
     24   switch (type) {
     25     case BC_CODE39:
     26       return new CBC_Code39();
     27     case BC_CODABAR:
     28       return new CBC_Codabar();
     29     case BC_CODE128:
     30       return new CBC_Code128(BC_CODE128_B);
     31     case BC_CODE128_B:
     32       return new CBC_Code128(BC_CODE128_B);
     33     case BC_CODE128_C:
     34       return new CBC_Code128(BC_CODE128_C);
     35     case BC_EAN8:
     36       return new CBC_EAN8();
     37     case BC_UPCA:
     38       return new CBC_UPCA();
     39     case BC_EAN13:
     40       return new CBC_EAN13();
     41     case BC_QR_CODE:
     42       return new CBC_QRCode();
     43     case BC_PDF417:
     44       return new CBC_PDF417I();
     45     case BC_DATAMATRIX:
     46       return new CBC_DataMatrix();
     47     case BC_UNKNOWN:
     48     default:
     49       return nullptr;
     50   }
     51 }
     52 
     53 }  // namespace
     54 
     55 CFX_Barcode::CFX_Barcode() {}
     56 
     57 CFX_Barcode::~CFX_Barcode() {}
     58 
     59 bool CFX_Barcode::Create(BC_TYPE type) {
     60   m_pBCEngine.reset(CreateBarCodeEngineObject(type));
     61   return !!m_pBCEngine;
     62 }
     63 
     64 BC_TYPE CFX_Barcode::GetType() {
     65   return m_pBCEngine ? m_pBCEngine->GetType() : BC_UNKNOWN;
     66 }
     67 
     68 bool CFX_Barcode::SetCharEncoding(BC_CHAR_ENCODING encoding) {
     69   return m_pBCEngine ? m_pBCEngine->SetCharEncoding(encoding) : false;
     70 }
     71 
     72 bool CFX_Barcode::SetModuleHeight(int32_t moduleHeight) {
     73   return m_pBCEngine ? m_pBCEngine->SetModuleHeight(moduleHeight) : false;
     74 }
     75 
     76 bool CFX_Barcode::SetModuleWidth(int32_t moduleWidth) {
     77   return m_pBCEngine ? m_pBCEngine->SetModuleWidth(moduleWidth) : false;
     78 }
     79 
     80 bool CFX_Barcode::SetHeight(int32_t height) {
     81   return m_pBCEngine ? m_pBCEngine->SetHeight(height) : false;
     82 }
     83 
     84 bool CFX_Barcode::SetWidth(int32_t width) {
     85   return m_pBCEngine ? m_pBCEngine->SetWidth(width) : false;
     86 }
     87 
     88 bool CFX_Barcode::SetPrintChecksum(bool checksum) {
     89   switch (GetType()) {
     90     case BC_CODE39:
     91     case BC_CODABAR:
     92     case BC_CODE128:
     93     case BC_CODE128_B:
     94     case BC_CODE128_C:
     95     case BC_EAN8:
     96     case BC_EAN13:
     97     case BC_UPCA:
     98       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
     99                                 ->SetPrintChecksum(checksum),
    100                             true)
    101                          : false;
    102     default:
    103       return false;
    104   }
    105 }
    106 
    107 bool CFX_Barcode::SetDataLength(int32_t length) {
    108   switch (GetType()) {
    109     case BC_CODE39:
    110     case BC_CODABAR:
    111     case BC_CODE128:
    112     case BC_CODE128_B:
    113     case BC_CODE128_C:
    114     case BC_EAN8:
    115     case BC_EAN13:
    116     case BC_UPCA:
    117       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
    118                                 ->SetDataLength(length),
    119                             true)
    120                          : false;
    121     default:
    122       return false;
    123   }
    124 }
    125 
    126 bool CFX_Barcode::SetCalChecksum(bool state) {
    127   switch (GetType()) {
    128     case BC_CODE39:
    129     case BC_CODABAR:
    130     case BC_CODE128:
    131     case BC_CODE128_B:
    132     case BC_CODE128_C:
    133     case BC_EAN8:
    134     case BC_EAN13:
    135     case BC_UPCA:
    136       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
    137                                 ->SetCalChecksum(state),
    138                             true)
    139                          : false;
    140     default:
    141       return false;
    142   }
    143 }
    144 
    145 bool CFX_Barcode::SetFont(CFX_Font* pFont) {
    146   switch (GetType()) {
    147     case BC_CODE39:
    148     case BC_CODABAR:
    149     case BC_CODE128:
    150     case BC_CODE128_B:
    151     case BC_CODE128_C:
    152     case BC_EAN8:
    153     case BC_EAN13:
    154     case BC_UPCA:
    155       return m_pBCEngine
    156                  ? static_cast<CBC_OneCode*>(m_pBCEngine.get())->SetFont(pFont)
    157                  : false;
    158     default:
    159       return false;
    160   }
    161 }
    162 
    163 bool CFX_Barcode::SetFontSize(FX_FLOAT size) {
    164   switch (GetType()) {
    165     case BC_CODE39:
    166     case BC_CODABAR:
    167     case BC_CODE128:
    168     case BC_CODE128_B:
    169     case BC_CODE128_C:
    170     case BC_EAN8:
    171     case BC_EAN13:
    172     case BC_UPCA:
    173       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
    174                                 ->SetFontSize(size),
    175                             true)
    176                          : false;
    177     default:
    178       return false;
    179   }
    180 }
    181 
    182 bool CFX_Barcode::SetFontColor(FX_ARGB color) {
    183   switch (GetType()) {
    184     case BC_CODE39:
    185     case BC_CODABAR:
    186     case BC_CODE128:
    187     case BC_CODE128_B:
    188     case BC_CODE128_C:
    189     case BC_EAN8:
    190     case BC_EAN13:
    191     case BC_UPCA:
    192       return m_pBCEngine ? (static_cast<CBC_OneCode*>(m_pBCEngine.get())
    193                                 ->SetFontColor(color),
    194                             true)
    195                          : false;
    196     default:
    197       return false;
    198   }
    199 }
    200 
    201 bool CFX_Barcode::SetTextLocation(BC_TEXT_LOC location) {
    202   typedef bool (CBC_CodeBase::*memptrtype)(BC_TEXT_LOC);
    203   memptrtype memptr = nullptr;
    204   switch (GetType()) {
    205     case BC_CODE39:
    206       memptr = (memptrtype)&CBC_Code39::SetTextLocation;
    207       break;
    208     case BC_CODABAR:
    209       memptr = (memptrtype)&CBC_Codabar::SetTextLocation;
    210       break;
    211     case BC_CODE128:
    212     case BC_CODE128_B:
    213     case BC_CODE128_C:
    214       memptr = (memptrtype)&CBC_Code128::SetTextLocation;
    215       break;
    216     default:
    217       break;
    218   }
    219   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(location) : false;
    220 }
    221 
    222 bool CFX_Barcode::SetWideNarrowRatio(int32_t ratio) {
    223   typedef bool (CBC_CodeBase::*memptrtype)(int32_t);
    224   memptrtype memptr = nullptr;
    225   switch (GetType()) {
    226     case BC_CODE39:
    227       memptr = (memptrtype)&CBC_Code39::SetWideNarrowRatio;
    228       break;
    229     case BC_CODABAR:
    230       memptr = (memptrtype)&CBC_Codabar::SetWideNarrowRatio;
    231       break;
    232     default:
    233       break;
    234   }
    235   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(ratio) : false;
    236 }
    237 
    238 bool CFX_Barcode::SetStartChar(FX_CHAR start) {
    239   typedef bool (CBC_CodeBase::*memptrtype)(FX_CHAR);
    240   memptrtype memptr = nullptr;
    241   switch (GetType()) {
    242     case BC_CODABAR:
    243       memptr = (memptrtype)&CBC_Codabar::SetStartChar;
    244       break;
    245     default:
    246       break;
    247   }
    248   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(start) : false;
    249 }
    250 
    251 bool CFX_Barcode::SetEndChar(FX_CHAR end) {
    252   typedef bool (CBC_CodeBase::*memptrtype)(FX_CHAR);
    253   memptrtype memptr = nullptr;
    254   switch (GetType()) {
    255     case BC_CODABAR:
    256       memptr = (memptrtype)&CBC_Codabar::SetEndChar;
    257       break;
    258     default:
    259       break;
    260   }
    261   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(end) : false;
    262 }
    263 
    264 bool CFX_Barcode::SetVersion(int32_t version) {
    265   typedef bool (CBC_CodeBase::*memptrtype)(int32_t);
    266   memptrtype memptr = nullptr;
    267   switch (GetType()) {
    268     case BC_QR_CODE:
    269       memptr = (memptrtype)&CBC_QRCode::SetVersion;
    270       break;
    271     default:
    272       break;
    273   }
    274   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(version) : false;
    275 }
    276 
    277 bool CFX_Barcode::SetErrorCorrectionLevel(int32_t level) {
    278   typedef bool (CBC_CodeBase::*memptrtype)(int32_t);
    279   memptrtype memptr = nullptr;
    280   switch (GetType()) {
    281     case BC_QR_CODE:
    282       memptr = (memptrtype)&CBC_QRCode::SetErrorCorrectionLevel;
    283       break;
    284     case BC_PDF417:
    285       memptr = (memptrtype)&CBC_PDF417I::SetErrorCorrectionLevel;
    286       break;
    287     default:
    288       return false;
    289   }
    290   return m_pBCEngine && memptr ? (m_pBCEngine.get()->*memptr)(level) : false;
    291 }
    292 bool CFX_Barcode::SetTruncated(bool truncated) {
    293   typedef void (CBC_CodeBase::*memptrtype)(bool);
    294   memptrtype memptr = nullptr;
    295   switch (GetType()) {
    296     case BC_PDF417:
    297       memptr = (memptrtype)&CBC_PDF417I::SetTruncated;
    298       break;
    299     default:
    300       break;
    301   }
    302   return m_pBCEngine && memptr ? ((m_pBCEngine.get()->*memptr)(truncated), true)
    303                                : false;
    304 }
    305 
    306 bool CFX_Barcode::Encode(const CFX_WideStringC& contents,
    307                          bool isDevice,
    308                          int32_t& e) {
    309   return m_pBCEngine && m_pBCEngine->Encode(contents, isDevice, e);
    310 }
    311 
    312 bool CFX_Barcode::RenderDevice(CFX_RenderDevice* device,
    313                                const CFX_Matrix* matrix,
    314                                int32_t& e) {
    315   return m_pBCEngine && m_pBCEngine->RenderDevice(device, matrix, e);
    316 }
    317