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 2009 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 "core/fxge/cfx_fxgedevice.h"
     24 #include "core/fxge/cfx_gemodule.h"
     25 #include "xfa/fxbarcode/BC_Writer.h"
     26 #include "xfa/fxbarcode/oned/BC_OneDimWriter.h"
     27 #include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h"
     28 
     29 namespace {
     30 
     31 const int32_t FIRST_DIGIT_ENCODINGS[10] = {0x00, 0x0B, 0x0D, 0xE,  0x13,
     32                                            0x19, 0x1C, 0x15, 0x16, 0x1A};
     33 const int32_t START_END_PATTERN[3] = {1, 1, 1};
     34 const int32_t MIDDLE_PATTERN[5] = {1, 1, 1, 1, 1};
     35 const int32_t L_PATTERNS[10][4] = {
     36     {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
     37     {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
     38 const int32_t L_AND_G_PATTERNS[20][4] = {
     39     {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
     40     {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2},
     41     {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1},
     42     {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}};
     43 
     44 }  // namespace
     45 
     46 CBC_OnedEAN13Writer::CBC_OnedEAN13Writer() {
     47   m_bLeftPadding = true;
     48   m_codeWidth = 3 + (7 * 6) + 5 + (7 * 6) + 3;
     49 }
     50 CBC_OnedEAN13Writer::~CBC_OnedEAN13Writer() {}
     51 bool CBC_OnedEAN13Writer::CheckContentValidity(
     52     const CFX_WideStringC& contents) {
     53   for (int32_t i = 0; i < contents.GetLength(); i++) {
     54     if (contents.GetAt(i) >= '0' && contents.GetAt(i) <= '9') {
     55       continue;
     56     } else {
     57       return false;
     58     }
     59   }
     60   return true;
     61 }
     62 CFX_WideString CBC_OnedEAN13Writer::FilterContents(
     63     const CFX_WideStringC& contents) {
     64   CFX_WideString filtercontents;
     65   FX_WCHAR ch;
     66   for (int32_t i = 0; i < contents.GetLength(); i++) {
     67     ch = contents.GetAt(i);
     68     if (ch > 175) {
     69       i++;
     70       continue;
     71     }
     72     if (ch >= '0' && ch <= '9') {
     73       filtercontents += ch;
     74     }
     75   }
     76   return filtercontents;
     77 }
     78 int32_t CBC_OnedEAN13Writer::CalcChecksum(const CFX_ByteString& contents) {
     79   int32_t odd = 0;
     80   int32_t even = 0;
     81   int32_t j = 1;
     82   for (int32_t i = contents.GetLength() - 1; i >= 0; i--) {
     83     if (j % 2) {
     84       odd += FXSYS_atoi(contents.Mid(i, 1).c_str());
     85     } else {
     86       even += FXSYS_atoi(contents.Mid(i, 1).c_str());
     87     }
     88     j++;
     89   }
     90   int32_t checksum = (odd * 3 + even) % 10;
     91   checksum = (10 - checksum) % 10;
     92   return (checksum);
     93 }
     94 uint8_t* CBC_OnedEAN13Writer::Encode(const CFX_ByteString& contents,
     95                                      BCFORMAT format,
     96                                      int32_t& outWidth,
     97                                      int32_t& outHeight,
     98                                      int32_t& e) {
     99   uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e);
    100   if (e != BCExceptionNO)
    101     return nullptr;
    102   return ret;
    103 }
    104 uint8_t* CBC_OnedEAN13Writer::Encode(const CFX_ByteString& contents,
    105                                      BCFORMAT format,
    106                                      int32_t& outWidth,
    107                                      int32_t& outHeight,
    108                                      int32_t hints,
    109                                      int32_t& e) {
    110   if (format != BCFORMAT_EAN_13) {
    111     e = BCExceptionOnlyEncodeEAN_13;
    112   }
    113   uint8_t* ret =
    114       CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e);
    115   if (e != BCExceptionNO)
    116     return nullptr;
    117   return ret;
    118 }
    119 uint8_t* CBC_OnedEAN13Writer::Encode(const CFX_ByteString& contents,
    120                                      int32_t& outLength,
    121                                      int32_t& e) {
    122   if (contents.GetLength() != 13) {
    123     e = BCExceptionDigitLengthShould13;
    124     return nullptr;
    125   }
    126   m_iDataLenth = 13;
    127   int32_t firstDigit = FXSYS_atoi(contents.Mid(0, 1).c_str());
    128   int32_t parities = FIRST_DIGIT_ENCODINGS[firstDigit];
    129   outLength = m_codeWidth;
    130   uint8_t* result = FX_Alloc(uint8_t, m_codeWidth);
    131   int32_t pos = 0;
    132   pos += AppendPattern(result, pos, START_END_PATTERN, 3, 1, e);
    133   if (e != BCExceptionNO) {
    134     FX_Free(result);
    135     return nullptr;
    136   }
    137   int32_t i = 0;
    138   for (i = 1; i <= 6; i++) {
    139     int32_t digit = FXSYS_atoi(contents.Mid(i, 1).c_str());
    140     if ((parities >> (6 - i) & 1) == 1) {
    141       digit += 10;
    142     }
    143     pos += AppendPattern(result, pos, L_AND_G_PATTERNS[digit], 4, 0, e);
    144     if (e != BCExceptionNO) {
    145       FX_Free(result);
    146       return nullptr;
    147     }
    148   }
    149   pos += AppendPattern(result, pos, MIDDLE_PATTERN, 5, 0, e);
    150   if (e != BCExceptionNO) {
    151     FX_Free(result);
    152     return nullptr;
    153   }
    154   for (i = 7; i <= 12; i++) {
    155     int32_t digit = FXSYS_atoi(contents.Mid(i, 1).c_str());
    156     pos += AppendPattern(result, pos, L_PATTERNS[digit], 4, 1, e);
    157     if (e != BCExceptionNO) {
    158       FX_Free(result);
    159       return nullptr;
    160     }
    161   }
    162   pos += AppendPattern(result, pos, START_END_PATTERN, 3, 1, e);
    163   if (e != BCExceptionNO) {
    164     FX_Free(result);
    165     return nullptr;
    166   }
    167   return result;
    168 }
    169 
    170 void CBC_OnedEAN13Writer::ShowChars(const CFX_WideStringC& contents,
    171                                     CFX_DIBitmap* pOutBitmap,
    172                                     CFX_RenderDevice* device,
    173                                     const CFX_Matrix* matrix,
    174                                     int32_t barWidth,
    175                                     int32_t multiple,
    176                                     int32_t& e) {
    177   if (!device && !pOutBitmap) {
    178     e = BCExceptionIllegalArgument;
    179     return;
    180   }
    181   int32_t leftPadding = 7 * multiple;
    182   int32_t leftPosition = 3 * multiple + leftPadding;
    183   CFX_ByteString str = FX_UTF8Encode(contents);
    184   int32_t iLen = str.GetLength();
    185   FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen);
    186   FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen);
    187   CFX_FxgeDevice geBitmap;
    188   if (pOutBitmap)
    189     geBitmap.Attach(pOutBitmap, false, nullptr, false);
    190 
    191   int32_t iFontSize = (int32_t)fabs(m_fFontSize);
    192   int32_t iTextHeight = iFontSize + 1;
    193   CFX_ByteString tempStr = str.Mid(1, 6);
    194   int32_t strWidth = multiple * 42;
    195   if (!pOutBitmap) {
    196     CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
    197     CFX_FloatRect rect(
    198         (FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHeight),
    199         (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height);
    200     matr.Concat(*matrix);
    201     matr.TransformRect(rect);
    202     FX_RECT re = rect.GetOuterRect();
    203     device->FillRect(&re, m_backgroundColor);
    204     CFX_FloatRect rect1(
    205         (FX_FLOAT)(leftPosition + 47 * multiple),
    206         (FX_FLOAT)(m_Height - iTextHeight),
    207         (FX_FLOAT)(leftPosition + 47 * multiple + strWidth - 0.5),
    208         (FX_FLOAT)m_Height);
    209     CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
    210     matr1.Concat(*matrix);
    211     matr1.TransformRect(rect1);
    212     re = rect1.GetOuterRect();
    213     device->FillRect(&re, m_backgroundColor);
    214     int32_t strWidth1 = multiple * 7;
    215     CFX_Matrix matr2(m_outputHScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
    216     CFX_FloatRect rect2(0.0f, (FX_FLOAT)(m_Height - iTextHeight),
    217                         (FX_FLOAT)strWidth1 - 0.5f, (FX_FLOAT)m_Height);
    218     matr2.Concat(*matrix);
    219     matr2.TransformRect(rect2);
    220     re = rect2.GetOuterRect();
    221     device->FillRect(&re, m_backgroundColor);
    222   }
    223   FX_FLOAT blank = 0.0;
    224   iLen = tempStr.GetLength();
    225   if (!pOutBitmap) {
    226     strWidth = (int32_t)(strWidth * m_outputHScale);
    227   }
    228   CalcTextInfo(tempStr, pCharPos + 1, m_pFont, (FX_FLOAT)strWidth, iFontSize,
    229                blank);
    230   CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize);
    231   CFX_FxgeDevice ge;
    232   if (pOutBitmap) {
    233     ge.Create(strWidth, iTextHeight, FXDIB_Argb, nullptr);
    234     FX_RECT rect(0, 0, strWidth, iTextHeight);
    235     ge.FillRect(&rect, m_backgroundColor);
    236     ge.DrawNormalText(iLen, pCharPos + 1, m_pFont,
    237                       static_cast<FX_FLOAT>(iFontSize), &affine_matrix,
    238                       m_fontColor, FXTEXT_CLEARTYPE);
    239     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight);
    240   } else {
    241     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
    242                               (FX_FLOAT)leftPosition * m_outputHScale,
    243                               (FX_FLOAT)(m_Height - iTextHeight) + iFontSize);
    244     if (matrix) {
    245       affine_matrix1.Concat(*matrix);
    246     }
    247     device->DrawNormalText(iLen, pCharPos + 1, m_pFont,
    248                            static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
    249                            m_fontColor, FXTEXT_CLEARTYPE);
    250   }
    251   tempStr = str.Mid(7, 6);
    252   iLen = tempStr.GetLength();
    253   CalcTextInfo(tempStr, pCharPos + 7, m_pFont, (FX_FLOAT)strWidth, iFontSize,
    254                blank);
    255   if (pOutBitmap) {
    256     FX_RECT rect1(0, 0, strWidth, iTextHeight);
    257     ge.FillRect(&rect1, m_backgroundColor);
    258     ge.DrawNormalText(iLen, pCharPos + 7, m_pFont,
    259                       static_cast<FX_FLOAT>(iFontSize), &affine_matrix,
    260                       m_fontColor, FXTEXT_CLEARTYPE);
    261     geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 47 * multiple,
    262                        m_Height - iTextHeight);
    263   } else {
    264     CFX_Matrix affine_matrix1(
    265         1.0, 0.0, 0.0, -1.0,
    266         (FX_FLOAT)(leftPosition + 47 * multiple) * m_outputHScale,
    267         (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
    268     if (matrix) {
    269       affine_matrix1.Concat(*matrix);
    270     }
    271     device->DrawNormalText(iLen, pCharPos + 7, m_pFont,
    272                            static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
    273                            m_fontColor, FXTEXT_CLEARTYPE);
    274   }
    275   tempStr = str.Mid(0, 1);
    276   iLen = tempStr.GetLength();
    277   strWidth = multiple * 7;
    278   if (!pOutBitmap)
    279     strWidth = (int32_t)(strWidth * m_outputHScale);
    280 
    281   CalcTextInfo(tempStr, pCharPos, m_pFont, (FX_FLOAT)strWidth, iFontSize,
    282                blank);
    283   if (pOutBitmap) {
    284     delete ge.GetBitmap();
    285     ge.Create(strWidth, iTextHeight, FXDIB_Argb, nullptr);
    286     ge.GetBitmap()->Clear(m_backgroundColor);
    287     ge.DrawNormalText(iLen, pCharPos, m_pFont, static_cast<FX_FLOAT>(iFontSize),
    288                       &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
    289     geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight);
    290   } else {
    291     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0.0,
    292                               (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
    293     if (matrix) {
    294       affine_matrix1.Concat(*matrix);
    295     }
    296     device->DrawNormalText(iLen, pCharPos, m_pFont,
    297                            static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
    298                            m_fontColor, FXTEXT_CLEARTYPE);
    299   }
    300   FX_Free(pCharPos);
    301 }
    302 
    303 void CBC_OnedEAN13Writer::RenderResult(const CFX_WideStringC& contents,
    304                                        uint8_t* code,
    305                                        int32_t codeLength,
    306                                        bool isDevice,
    307                                        int32_t& e) {
    308   CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e);
    309 }
    310