Home | History | Annotate | Download | only in oned
      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 // Original code is licensed as follows:
      7 /*
      8  * Copyright 2011 ZXing authors
      9  *
     10  * Licensed under the Apache License, Version 2.0 (the "License");
     11  * you may not use this file except in compliance with the License.
     12  * You may obtain a copy of the License at
     13  *
     14  *      http://www.apache.org/licenses/LICENSE-2.0
     15  *
     16  * Unless required by applicable law or agreed to in writing, software
     17  * distributed under the License is distributed on an "AS IS" BASIS,
     18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     19  * See the License for the specific language governing permissions and
     20  * limitations under the License.
     21  */
     22 
     23 #include "xfa/fxbarcode/BC_Writer.h"
     24 #include "xfa/fxbarcode/common/BC_CommonBitArray.h"
     25 #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h"
     26 #include "xfa/fxbarcode/oned/BC_OneDimWriter.h"
     27 #include "xfa/fxbarcode/oned/BC_OnedCodaBarWriter.h"
     28 
     29 namespace {
     30 
     31 const FX_CHAR ALPHABET_STRING[] = "0123456789-$:/.+ABCDTN";
     32 
     33 const int32_t CHARACTER_ENCODINGS[22] = {
     34     0x003, 0x006, 0x009, 0x060, 0x012, 0x042, 0x021, 0x024,
     35     0x030, 0x048, 0x00c, 0x018, 0x045, 0x051, 0x054, 0x015,
     36     0x01A, 0x029, 0x00B, 0x00E, 0x01A, 0x029};
     37 
     38 const FX_CHAR START_END_CHARS[] = {'A', 'B', 'C', 'D', 'T', 'N', '*', 'E',
     39                                    'a', 'b', 'c', 'd', 't', 'n', 'e'};
     40 const FX_CHAR CONTENT_CHARS[] = {'0', '1', '2', '3', '4', '5', '6', '7',
     41                                  '8', '9', '-', '$', '/', ':', '+', '.'};
     42 
     43 }  // namespace
     44 
     45 CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() {
     46   m_chStart = 'A';
     47   m_chEnd = 'B';
     48   m_iWideNarrRatio = 2;
     49 }
     50 CBC_OnedCodaBarWriter::~CBC_OnedCodaBarWriter() {}
     51 bool CBC_OnedCodaBarWriter::SetStartChar(FX_CHAR start) {
     52   for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) {
     53     if (START_END_CHARS[i] == start) {
     54       m_chStart = start;
     55       return true;
     56     }
     57   }
     58   return false;
     59 }
     60 
     61 bool CBC_OnedCodaBarWriter::SetEndChar(FX_CHAR end) {
     62   for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) {
     63     if (START_END_CHARS[i] == end) {
     64       m_chEnd = end;
     65       return true;
     66     }
     67   }
     68   return false;
     69 }
     70 void CBC_OnedCodaBarWriter::SetDataLength(int32_t length) {
     71   m_iDataLenth = length + 2;
     72 }
     73 bool CBC_OnedCodaBarWriter::SetTextLocation(BC_TEXT_LOC location) {
     74   if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) {
     75     return false;
     76   }
     77   m_locTextLoc = location;
     78   return true;
     79 }
     80 bool CBC_OnedCodaBarWriter::SetWideNarrowRatio(int32_t ratio) {
     81   if (ratio < 2 || ratio > 3) {
     82     return false;
     83   }
     84   m_iWideNarrRatio = ratio;
     85   return true;
     86 }
     87 bool CBC_OnedCodaBarWriter::FindChar(FX_WCHAR ch, bool isContent) {
     88   if (isContent) {
     89     for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) {
     90       if (ch == (FX_WCHAR)CONTENT_CHARS[i]) {
     91         return true;
     92       }
     93     }
     94     for (size_t j = 0; j < FX_ArraySize(START_END_CHARS); ++j) {
     95       if (ch == (FX_WCHAR)START_END_CHARS[j]) {
     96         return true;
     97       }
     98     }
     99     return false;
    100   } else {
    101     for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) {
    102       if (ch == (FX_WCHAR)CONTENT_CHARS[i]) {
    103         return true;
    104       }
    105     }
    106     return false;
    107   }
    108 }
    109 bool CBC_OnedCodaBarWriter::CheckContentValidity(
    110     const CFX_WideStringC& contents) {
    111   FX_WCHAR ch;
    112   int32_t index = 0;
    113   for (index = 0; index < contents.GetLength(); index++) {
    114     ch = contents.GetAt(index);
    115     if (FindChar(ch, false)) {
    116       continue;
    117     } else {
    118       return false;
    119     }
    120   }
    121   return true;
    122 }
    123 CFX_WideString CBC_OnedCodaBarWriter::FilterContents(
    124     const CFX_WideStringC& contents) {
    125   CFX_WideString filtercontents;
    126   FX_WCHAR ch;
    127   for (int32_t index = 0; index < contents.GetLength(); index++) {
    128     ch = contents.GetAt(index);
    129     if (ch > 175) {
    130       index++;
    131       continue;
    132     }
    133     if (FindChar(ch, true)) {
    134       filtercontents += ch;
    135     } else {
    136       continue;
    137     }
    138   }
    139   return filtercontents;
    140 }
    141 uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents,
    142                                        BCFORMAT format,
    143                                        int32_t& outWidth,
    144                                        int32_t& outHeight,
    145                                        int32_t& e) {
    146   uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e);
    147   if (e != BCExceptionNO)
    148     return nullptr;
    149   return ret;
    150 }
    151 uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents,
    152                                        BCFORMAT format,
    153                                        int32_t& outWidth,
    154                                        int32_t& outHeight,
    155                                        int32_t hints,
    156                                        int32_t& e) {
    157   if (format != BCFORMAT_CODABAR) {
    158     e = BCExceptionOnlyEncodeCODEBAR;
    159     return nullptr;
    160   }
    161   uint8_t* ret =
    162       CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e);
    163   if (e != BCExceptionNO)
    164     return nullptr;
    165   return ret;
    166 }
    167 uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents,
    168                                        int32_t& outLength,
    169                                        int32_t& e) {
    170   CFX_ByteString data = m_chStart + contents + m_chEnd;
    171   m_iContentLen = data.GetLength();
    172   uint8_t* result = FX_Alloc2D(uint8_t, m_iWideNarrRatio * 7, data.GetLength());
    173   FX_CHAR ch;
    174   int32_t position = 0;
    175   for (int32_t index = 0; index < data.GetLength(); index++) {
    176     ch = data.GetAt(index);
    177     if (((ch >= 'a') && (ch <= 'z'))) {
    178       ch = ch - 32;
    179     }
    180     switch (ch) {
    181       case 'T':
    182         ch = 'A';
    183         break;
    184       case 'N':
    185         ch = 'B';
    186         break;
    187       case '*':
    188         ch = 'C';
    189         break;
    190       case 'E':
    191         ch = 'D';
    192         break;
    193       default:
    194         break;
    195     }
    196     int32_t code = 0;
    197     int32_t len = (int32_t)strlen(ALPHABET_STRING);
    198     for (int32_t i = 0; i < len; i++) {
    199       if (ch == ALPHABET_STRING[i]) {
    200         code = CHARACTER_ENCODINGS[i];
    201         break;
    202       }
    203     }
    204     uint8_t color = 1;
    205     int32_t counter = 0;
    206     int32_t bit = 0;
    207     while (bit < 7) {
    208       result[position] = color;
    209       position++;
    210       if (((code >> (6 - bit)) & 1) == 0 || counter == m_iWideNarrRatio - 1) {
    211         color = !color;
    212         bit++;
    213         counter = 0;
    214       } else {
    215         counter++;
    216       }
    217     }
    218     if (index < data.GetLength() - 1) {
    219       result[position] = 0;
    220       position++;
    221     }
    222   }
    223   outLength = position;
    224   return result;
    225 }
    226 CFX_WideString CBC_OnedCodaBarWriter::encodedContents(
    227     const CFX_WideStringC& contents) {
    228   CFX_WideString strStart(m_chStart);
    229   CFX_WideString strEnd(m_chEnd);
    230   return strStart + contents + strEnd;
    231 }
    232 void CBC_OnedCodaBarWriter::RenderResult(const CFX_WideStringC& contents,
    233                                          uint8_t* code,
    234                                          int32_t codeLength,
    235                                          bool isDevice,
    236                                          int32_t& e) {
    237   CBC_OneDimWriter::RenderResult(encodedContents(contents).AsStringC(), code,
    238                                  codeLength, isDevice, e);
    239 }
    240