• 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_OnedEAN8Writer.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 const uint8_t kOnedEAN8StartPattern[3] = {1, 1, 1};
42 const uint8_t kOnedEAN8MiddlePattern[5] = {1, 1, 1, 1, 1};
43 
44 using LPatternRow = std::array<uint8_t, 4>;
45 constexpr std::array<const LPatternRow, 10> kOnedEAN8LPatternTable = {
46     {{3, 2, 1, 1},
47      {2, 2, 2, 1},
48      {2, 1, 2, 2},
49      {1, 4, 1, 1},
50      {1, 1, 3, 2},
51      {1, 2, 3, 1},
52      {1, 1, 1, 4},
53      {1, 3, 1, 2},
54      {1, 2, 1, 3},
55      {3, 1, 1, 2}}};
56 
57 }  // namespace
58 
CBC_OnedEAN8Writer()59 CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() {
60   m_iDataLenth = 8;
61 }
62 
63 CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() = default;
64 
SetDataLength(int32_t length)65 void CBC_OnedEAN8Writer::SetDataLength(int32_t length) {
66   m_iDataLenth = 8;
67 }
68 
SetTextLocation(BC_TEXT_LOC location)69 void CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
70   if (location == BC_TEXT_LOC::kBelowEmbed)
71     m_locTextLoc = location;
72 }
73 
CheckContentValidity(WideStringView contents)74 bool CBC_OnedEAN8Writer::CheckContentValidity(WideStringView contents) {
75   return HasValidContentSize(contents) &&
76          std::all_of(contents.begin(), contents.end(),
77                      [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
78 }
79 
FilterContents(WideStringView contents)80 WideString CBC_OnedEAN8Writer::FilterContents(WideStringView contents) {
81   WideString filtercontents;
82   filtercontents.Reserve(contents.GetLength());
83   wchar_t ch;
84   for (size_t i = 0; i < contents.GetLength(); i++) {
85     ch = contents[i];
86     if (ch > 175) {
87       i++;
88       continue;
89     }
90     if (FXSYS_IsDecimalDigit(ch))
91       filtercontents += ch;
92   }
93   return filtercontents;
94 }
95 
CalcChecksum(const ByteString & contents)96 int32_t CBC_OnedEAN8Writer::CalcChecksum(const ByteString& contents) {
97   return EANCalcChecksum(contents);
98 }
99 
Encode(const ByteString & contents)100 DataVector<uint8_t> CBC_OnedEAN8Writer::Encode(const ByteString& contents) {
101   if (contents.GetLength() != 8)
102     return {};
103 
104   DataVector<uint8_t> result(m_codeWidth);
105   auto result_span = pdfium::make_span(result);
106   result_span = AppendPattern(result_span, kOnedEAN8StartPattern, true);
107 
108   for (int i = 0; i <= 3; i++) {
109     int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
110     result_span =
111         AppendPattern(result_span, kOnedEAN8LPatternTable[digit], false);
112   }
113   result_span = AppendPattern(result_span, kOnedEAN8MiddlePattern, false);
114 
115   for (int i = 4; i <= 7; i++) {
116     int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
117     result_span =
118         AppendPattern(result_span, kOnedEAN8LPatternTable[digit], true);
119   }
120   AppendPattern(result_span, kOnedEAN8StartPattern, true);
121   return result;
122 }
123 
ShowChars(WideStringView contents,CFX_RenderDevice * device,const CFX_Matrix & matrix,int32_t barWidth)124 bool CBC_OnedEAN8Writer::ShowChars(WideStringView contents,
125                                    CFX_RenderDevice* device,
126                                    const CFX_Matrix& matrix,
127                                    int32_t barWidth) {
128   if (!device)
129     return false;
130 
131   constexpr float kLeftPosition = 3.0f;
132   ByteString str = FX_UTF8Encode(contents);
133   size_t iLength = str.GetLength();
134   std::vector<TextCharPos> charpos(iLength);
135   ByteString tempStr = str.First(4);
136   size_t iLen = tempStr.GetLength();
137   constexpr int32_t kWidth = 28;
138   float blank = 0.0f;
139 
140   int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
141   int32_t iTextHeight = iFontSize + 1;
142 
143   CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
144   CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
145                      kLeftPosition + kWidth - 0.5, (float)m_Height);
146   matr.Concat(matrix);
147   FX_RECT re = matr.TransformRect(rect).GetOuterRect();
148   device->FillRect(re, kBackgroundColor);
149   CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
150   CFX_FloatRect rect1(kLeftPosition + 33, (float)(m_Height - iTextHeight),
151                       kLeftPosition + 33 + kWidth - 0.5, (float)m_Height);
152   matr1.Concat(matrix);
153   re = matr1.TransformRect(rect1).GetOuterRect();
154   device->FillRect(re, kBackgroundColor);
155   int32_t strWidth = static_cast<int32_t>(kWidth * m_outputHScale);
156 
157   pdfium::span<TextCharPos> charpos_span = pdfium::make_span(charpos);
158   CalcTextInfo(tempStr, charpos, m_pFont, (float)strWidth, iFontSize, blank);
159   {
160     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
161                               kLeftPosition * m_outputHScale,
162                               (float)(m_Height - iTextHeight + iFontSize));
163     affine_matrix1.Concat(matrix);
164     device->DrawNormalText(charpos_span.first(iLen), m_pFont,
165                            static_cast<float>(iFontSize), affine_matrix1,
166                            m_fontColor, GetTextRenderOptions());
167   }
168   tempStr = str.Substr(4, 4);
169   iLen = tempStr.GetLength();
170   CalcTextInfo(tempStr, charpos_span.subspan(4), m_pFont, (float)strWidth,
171                iFontSize, blank);
172   {
173     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
174                               (kLeftPosition + 33) * m_outputHScale,
175                               (float)(m_Height - iTextHeight + iFontSize));
176     affine_matrix1.Concat(matrix);
177     device->DrawNormalText(charpos_span.subspan(4, iLen), m_pFont,
178                            static_cast<float>(iFontSize), affine_matrix1,
179                            m_fontColor, GetTextRenderOptions());
180   }
181   return true;
182 }
183