• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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_OnedEAN13Writer.h"
24 
25 #include <math.h>
26 
27 #include <algorithm>
28 #include <array>
29 #include <memory>
30 #include <vector>
31 
32 #include "core/fxcrt/fx_extension.h"
33 #include "core/fxge/cfx_defaultrenderdevice.h"
34 #include "core/fxge/text_char_pos.h"
35 #include "fxbarcode/BC_Writer.h"
36 #include "fxbarcode/oned/BC_OneDimWriter.h"
37 #include "fxbarcode/oned/BC_OnedEANChecksum.h"
38 
39 namespace {
40 
41 constexpr std::array<const int8_t, 10> kFirstDigitEncodings = {
42     {0x00, 0x0B, 0x0D, 0xE, 0x13, 0x19, 0x1C, 0x15, 0x16, 0x1A}};
43 
44 const uint8_t kOnedEAN13StartPattern[3] = {1, 1, 1};
45 const uint8_t kOnedEAN13MiddlePattern[5] = {1, 1, 1, 1, 1};
46 
47 using LPatternRow = std::array<uint8_t, 4>;
48 constexpr std::array<const LPatternRow, 10> kOnedEAN13LPatternTable = {
49     {{3, 2, 1, 1},
50      {2, 2, 2, 1},
51      {2, 1, 2, 2},
52      {1, 4, 1, 1},
53      {1, 1, 3, 2},
54      {1, 2, 3, 1},
55      {1, 1, 1, 4},
56      {1, 3, 1, 2},
57      {1, 2, 1, 3},
58      {3, 1, 1, 2}}};
59 
60 using LGPatternRow = std::array<uint8_t, 4>;
61 constexpr std::array<const LGPatternRow, 20> kOnedEAN13LGPatternTable = {
62     {{3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
63      {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2},
64      {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1},
65      {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}}};
66 
67 }  // namespace
68 
CBC_OnedEAN13Writer()69 CBC_OnedEAN13Writer::CBC_OnedEAN13Writer() {
70   m_bLeftPadding = true;
71   m_codeWidth = 3 + (7 * 6) + 5 + (7 * 6) + 3;
72 }
73 CBC_OnedEAN13Writer::~CBC_OnedEAN13Writer() = default;
74 
CheckContentValidity(WideStringView contents)75 bool CBC_OnedEAN13Writer::CheckContentValidity(WideStringView contents) {
76   return HasValidContentSize(contents) &&
77          std::all_of(contents.begin(), contents.end(),
78                      [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
79 }
80 
FilterContents(WideStringView contents)81 WideString CBC_OnedEAN13Writer::FilterContents(WideStringView contents) {
82   WideString filtercontents;
83   filtercontents.Reserve(contents.GetLength());
84   wchar_t ch;
85   for (size_t i = 0; i < contents.GetLength(); i++) {
86     ch = contents[i];
87     if (ch > 175) {
88       i++;
89       continue;
90     }
91     if (FXSYS_IsDecimalDigit(ch))
92       filtercontents += ch;
93   }
94   return filtercontents;
95 }
96 
CalcChecksum(const ByteString & contents)97 int32_t CBC_OnedEAN13Writer::CalcChecksum(const ByteString& contents) {
98   return EANCalcChecksum(contents);
99 }
100 
Encode(const ByteString & contents)101 DataVector<uint8_t> CBC_OnedEAN13Writer::Encode(const ByteString& contents) {
102   if (contents.GetLength() != 13)
103     return DataVector<uint8_t>();
104 
105   m_iDataLenth = 13;
106   int32_t firstDigit = FXSYS_DecimalCharToInt(contents.Front());
107   int32_t parities = kFirstDigitEncodings[firstDigit];
108   DataVector<uint8_t> result(m_codeWidth);
109   auto result_span = pdfium::make_span(result);
110   result_span = AppendPattern(result_span, kOnedEAN13StartPattern, true);
111 
112   for (int i = 1; i <= 6; i++) {
113     int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
114     if ((parities >> (6 - i) & 1) == 1) {
115       digit += 10;
116     }
117     result_span =
118         AppendPattern(result_span, kOnedEAN13LGPatternTable[digit], false);
119   }
120   result_span = AppendPattern(result_span, kOnedEAN13MiddlePattern, false);
121 
122   for (int i = 7; i <= 12; i++) {
123     int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
124     result_span =
125         AppendPattern(result_span, kOnedEAN13LPatternTable[digit], true);
126   }
127   AppendPattern(result_span, kOnedEAN13StartPattern, true);
128   return result;
129 }
130 
ShowChars(WideStringView contents,CFX_RenderDevice * device,const CFX_Matrix & matrix,int32_t barWidth)131 bool CBC_OnedEAN13Writer::ShowChars(WideStringView contents,
132                                     CFX_RenderDevice* device,
133                                     const CFX_Matrix& matrix,
134                                     int32_t barWidth) {
135   if (!device)
136     return false;
137 
138   constexpr float kLeftPosition = 10.0f;
139   ByteString str = FX_UTF8Encode(contents);
140   size_t length = str.GetLength();
141   std::vector<TextCharPos> charpos(length);
142   int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
143   int32_t iTextHeight = iFontSize + 1;
144   ByteString tempStr = str.Substr(1, 6);
145   constexpr int32_t kWidth = 42;
146 
147   CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
148   CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
149                      kLeftPosition + kWidth - 0.5, (float)m_Height);
150   matr.Concat(matrix);
151   FX_RECT re = matr.TransformRect(rect).GetOuterRect();
152   device->FillRect(re, kBackgroundColor);
153   CFX_FloatRect rect1(kLeftPosition + 47, (float)(m_Height - iTextHeight),
154                       kLeftPosition + 47 + kWidth - 0.5, (float)m_Height);
155   CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
156   matr1.Concat(matrix);
157   re = matr1.TransformRect(rect1).GetOuterRect();
158   device->FillRect(re, kBackgroundColor);
159   CFX_Matrix matr2(m_outputHScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
160   CFX_FloatRect rect2(0.0f, (float)(m_Height - iTextHeight), 6.5f,
161                       (float)m_Height);
162   matr2.Concat(matrix);
163   re = matr2.TransformRect(rect2).GetOuterRect();
164   device->FillRect(re, kBackgroundColor);
165 
166   float blank = 0.0f;
167   length = tempStr.GetLength();
168   int32_t strWidth = static_cast<int32_t>(kWidth * m_outputHScale);
169 
170   pdfium::span<TextCharPos> charpos_span = pdfium::make_span(charpos);
171   CalcTextInfo(tempStr, charpos_span.subspan(1), m_pFont, (float)strWidth,
172                iFontSize, blank);
173   {
174     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
175                               kLeftPosition * m_outputHScale,
176                               (float)(m_Height - iTextHeight) + iFontSize);
177     affine_matrix1.Concat(matrix);
178     device->DrawNormalText(charpos_span.subspan(1, length), m_pFont,
179                            static_cast<float>(iFontSize), affine_matrix1,
180                            m_fontColor, GetTextRenderOptions());
181   }
182   tempStr = str.Substr(7, 6);
183   length = tempStr.GetLength();
184   CalcTextInfo(tempStr, charpos_span.subspan(7), m_pFont, (float)strWidth,
185                iFontSize, blank);
186   {
187     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
188                               (kLeftPosition + 47) * m_outputHScale,
189                               (float)(m_Height - iTextHeight + iFontSize));
190     affine_matrix1.Concat(matrix);
191     device->DrawNormalText(charpos_span.subspan(7, length), m_pFont,
192                            static_cast<float>(iFontSize), affine_matrix1,
193                            m_fontColor, GetTextRenderOptions());
194   }
195   tempStr = str.First(1);
196   length = tempStr.GetLength();
197   strWidth = 7 * static_cast<int32_t>(strWidth * m_outputHScale);
198 
199   CalcTextInfo(tempStr, charpos, m_pFont, (float)strWidth, iFontSize, blank);
200   {
201     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0.0,
202                               (float)(m_Height - iTextHeight + iFontSize));
203     affine_matrix1.Concat(matrix);
204     device->DrawNormalText(charpos_span.first(length), m_pFont,
205                            static_cast<float>(iFontSize), affine_matrix1,
206                            m_fontColor, GetTextRenderOptions());
207   }
208   return true;
209 }
210