• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fxbarcode/oned/BC_OnedEAN8Writer.h"
24 
25 #include <algorithm>
26 #include <cwctype>
27 #include <memory>
28 #include <vector>
29 
30 #include "core/fxcrt/fx_extension.h"
31 #include "core/fxcrt/fx_memory_wrappers.h"
32 #include "core/fxge/cfx_defaultrenderdevice.h"
33 #include "core/fxge/text_char_pos.h"
34 #include "fxbarcode/BC_Writer.h"
35 #include "fxbarcode/common/BC_CommonBitMatrix.h"
36 #include "fxbarcode/oned/BC_OneDimWriter.h"
37 #include "fxbarcode/oned/BC_OnedEANChecksum.h"
38 
39 namespace {
40 
41 const int8_t kOnedEAN8StartPattern[3] = {1, 1, 1};
42 const int8_t kOnedEAN8MiddlePattern[5] = {1, 1, 1, 1, 1};
43 const int8_t kOnedEAN8LPattern[10][4] = {
44     {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
45     {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
46 
47 }  // namespace
48 
CBC_OnedEAN8Writer()49 CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() {
50   m_iDataLenth = 8;
51 }
52 
53 CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() = default;
54 
SetDataLength(int32_t length)55 void CBC_OnedEAN8Writer::SetDataLength(int32_t length) {
56   m_iDataLenth = 8;
57 }
58 
SetTextLocation(BC_TEXT_LOC location)59 bool CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
60   if (location == BC_TEXT_LOC_BELOWEMBED) {
61     m_locTextLoc = location;
62     return true;
63   }
64   return false;
65 }
66 
CheckContentValidity(WideStringView contents)67 bool CBC_OnedEAN8Writer::CheckContentValidity(WideStringView contents) {
68   return std::all_of(contents.begin(), contents.end(),
69                      [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
70 }
71 
FilterContents(WideStringView contents)72 WideString CBC_OnedEAN8Writer::FilterContents(WideStringView contents) {
73   WideString filtercontents;
74   filtercontents.Reserve(contents.GetLength());
75   wchar_t ch;
76   for (size_t i = 0; i < contents.GetLength(); i++) {
77     ch = contents[i];
78     if (ch > 175) {
79       i++;
80       continue;
81     }
82     if (FXSYS_IsDecimalDigit(ch))
83       filtercontents += ch;
84   }
85   return filtercontents;
86 }
87 
CalcChecksum(const ByteString & contents)88 int32_t CBC_OnedEAN8Writer::CalcChecksum(const ByteString& contents) {
89   return EANCalcChecksum(contents);
90 }
91 
EncodeWithHint(const ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t hints)92 uint8_t* CBC_OnedEAN8Writer::EncodeWithHint(const ByteString& contents,
93                                             BCFORMAT format,
94                                             int32_t& outWidth,
95                                             int32_t& outHeight,
96                                             int32_t hints) {
97   if (format != BCFORMAT_EAN_8)
98     return nullptr;
99   return CBC_OneDimWriter::EncodeWithHint(contents, format, outWidth, outHeight,
100                                           hints);
101 }
102 
EncodeImpl(const ByteString & contents,int32_t & outLength)103 uint8_t* CBC_OnedEAN8Writer::EncodeImpl(const ByteString& contents,
104                                         int32_t& outLength) {
105   if (contents.GetLength() != 8)
106     return nullptr;
107 
108   outLength = m_codeWidth;
109   std::unique_ptr<uint8_t, FxFreeDeleter> result(
110       FX_Alloc(uint8_t, m_codeWidth));
111   int32_t pos = 0;
112   pos += AppendPattern(result.get(), pos, kOnedEAN8StartPattern, 3, true);
113 
114   int32_t i = 0;
115   for (i = 0; i <= 3; i++) {
116     int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
117     pos += AppendPattern(result.get(), pos, kOnedEAN8LPattern[digit], 4, false);
118   }
119   pos += AppendPattern(result.get(), pos, kOnedEAN8MiddlePattern, 5, false);
120 
121   for (i = 4; i <= 7; i++) {
122     int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
123     pos += AppendPattern(result.get(), pos, kOnedEAN8LPattern[digit], 4, true);
124   }
125   pos += AppendPattern(result.get(), pos, kOnedEAN8StartPattern, 3, true);
126   return result.release();
127 }
128 
ShowChars(WideStringView contents,CFX_RenderDevice * device,const CFX_Matrix * matrix,int32_t barWidth,int32_t multiple)129 bool CBC_OnedEAN8Writer::ShowChars(WideStringView contents,
130                                    CFX_RenderDevice* device,
131                                    const CFX_Matrix* matrix,
132                                    int32_t barWidth,
133                                    int32_t multiple) {
134   if (!device)
135     return false;
136 
137   int32_t leftPosition = 3 * multiple;
138   ByteString str = FX_UTF8Encode(contents);
139   size_t iLength = str.GetLength();
140   std::vector<TextCharPos> charpos(iLength);
141   ByteString tempStr = str.First(4);
142   size_t iLen = tempStr.GetLength();
143   int32_t strWidth = 7 * multiple * 4;
144   float blank = 0.0;
145 
146   int32_t iFontSize = (int32_t)fabs(m_fFontSize);
147   int32_t iTextHeight = iFontSize + 1;
148 
149   CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
150   CFX_FloatRect rect((float)leftPosition, (float)(m_Height - iTextHeight),
151                      (float)(leftPosition + strWidth - 0.5), (float)m_Height);
152   matr.Concat(*matrix);
153   FX_RECT re = matr.TransformRect(rect).GetOuterRect();
154   device->FillRect(re, kBackgroundColor);
155   CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
156   CFX_FloatRect rect1(
157       (float)(leftPosition + 33 * multiple), (float)(m_Height - iTextHeight),
158       (float)(leftPosition + 33 * multiple + strWidth - 0.5), (float)m_Height);
159   matr1.Concat(*matrix);
160   re = matr1.TransformRect(rect1).GetOuterRect();
161   device->FillRect(re, kBackgroundColor);
162   strWidth = (int32_t)(strWidth * m_outputHScale);
163 
164   CalcTextInfo(tempStr, charpos.data(), m_pFont.Get(), (float)strWidth,
165                iFontSize, blank);
166   {
167     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
168                               (float)leftPosition * m_outputHScale,
169                               (float)(m_Height - iTextHeight + iFontSize));
170     affine_matrix1.Concat(*matrix);
171     device->DrawNormalText(iLen, charpos.data(), m_pFont.Get(),
172                            static_cast<float>(iFontSize), affine_matrix1,
173                            m_fontColor, FXTEXT_CLEARTYPE);
174   }
175   tempStr = str.Substr(4, 4);
176   iLen = tempStr.GetLength();
177   CalcTextInfo(tempStr, &charpos[4], m_pFont.Get(), (float)strWidth, iFontSize,
178                blank);
179   {
180     CFX_Matrix affine_matrix1(
181         1.0, 0.0, 0.0, -1.0,
182         (float)(leftPosition + 33 * multiple) * m_outputHScale,
183         (float)(m_Height - iTextHeight + iFontSize));
184     if (matrix)
185       affine_matrix1.Concat(*matrix);
186     device->DrawNormalText(iLen, &charpos[4], m_pFont.Get(),
187                            static_cast<float>(iFontSize), affine_matrix1,
188                            m_fontColor, FXTEXT_CLEARTYPE);
189   }
190   return true;
191 }
192