• 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 2010 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_OnedUPCAWriter.h"
24 
25 #include <math.h>
26 
27 #include <algorithm>
28 #include <vector>
29 
30 #include "core/fxcrt/fx_extension.h"
31 #include "core/fxge/cfx_defaultrenderdevice.h"
32 #include "core/fxge/text_char_pos.h"
33 #include "fxbarcode/BC_Writer.h"
34 #include "fxbarcode/oned/BC_OneDimWriter.h"
35 #include "fxbarcode/oned/BC_OnedEAN13Writer.h"
36 
CBC_OnedUPCAWriter()37 CBC_OnedUPCAWriter::CBC_OnedUPCAWriter() {
38   m_bLeftPadding = true;
39   m_bRightPadding = true;
40 }
41 
42 CBC_OnedUPCAWriter::~CBC_OnedUPCAWriter() = default;
43 
CheckContentValidity(WideStringView contents)44 bool CBC_OnedUPCAWriter::CheckContentValidity(WideStringView contents) {
45   return HasValidContentSize(contents) &&
46          std::all_of(contents.begin(), contents.end(),
47                      [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
48 }
49 
FilterContents(WideStringView contents)50 WideString CBC_OnedUPCAWriter::FilterContents(WideStringView contents) {
51   WideString filtercontents;
52   filtercontents.Reserve(contents.GetLength());
53   wchar_t ch;
54   for (size_t i = 0; i < contents.GetLength(); i++) {
55     ch = contents[i];
56     if (ch > 175) {
57       i++;
58       continue;
59     }
60     if (FXSYS_IsDecimalDigit(ch))
61       filtercontents += ch;
62   }
63   return filtercontents;
64 }
65 
InitEANWriter()66 void CBC_OnedUPCAWriter::InitEANWriter() {
67   m_subWriter = std::make_unique<CBC_OnedEAN13Writer>();
68 }
69 
CalcChecksum(const ByteString & contents)70 int32_t CBC_OnedUPCAWriter::CalcChecksum(const ByteString& contents) {
71   int32_t odd = 0;
72   int32_t even = 0;
73   size_t j = 1;
74   for (size_t i = contents.GetLength(); i > 0; i--) {
75     if (j % 2) {
76       odd += FXSYS_DecimalCharToInt(contents[i - 1]);
77     } else {
78       even += FXSYS_DecimalCharToInt(contents[i - 1]);
79     }
80     j++;
81   }
82   int32_t checksum = (odd * 3 + even) % 10;
83   checksum = (10 - checksum) % 10;
84   return checksum;
85 }
86 
Encode(const ByteString & contents)87 DataVector<uint8_t> CBC_OnedUPCAWriter::Encode(const ByteString& contents) {
88   ByteString toEAN13String = '0' + contents;
89   m_iDataLenth = 13;
90   return m_subWriter->Encode(toEAN13String);
91 }
92 
ShowChars(WideStringView contents,CFX_RenderDevice * device,const CFX_Matrix & matrix,int32_t barWidth)93 bool CBC_OnedUPCAWriter::ShowChars(WideStringView contents,
94                                    CFX_RenderDevice* device,
95                                    const CFX_Matrix& matrix,
96                                    int32_t barWidth) {
97   if (!device)
98     return false;
99 
100   constexpr float kLeftPosition = 17.0f;
101   ByteString str = FX_UTF8Encode(contents);
102   size_t length = str.GetLength();
103   std::vector<TextCharPos> charpos(length);
104   ByteString tempStr = str.Substr(1, 5);
105   constexpr float kWidth = 35.0f;
106   float blank = 0.0f;
107 
108   length = tempStr.GetLength();
109   int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
110   int32_t iTextHeight = iFontSize + 1;
111 
112   CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
113   CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
114                      kLeftPosition + kWidth - 0.5, (float)m_Height);
115   matr.Concat(matrix);
116   FX_RECT re = matr.TransformRect(rect).GetOuterRect();
117   device->FillRect(re, kBackgroundColor);
118   CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
119   CFX_FloatRect rect1(kLeftPosition + 40, (float)(m_Height - iTextHeight),
120                       kLeftPosition + 40 + kWidth - 0.5, (float)m_Height);
121   matr1.Concat(matrix);
122   re = matr1.TransformRect(rect1).GetOuterRect();
123   device->FillRect(re, kBackgroundColor);
124   constexpr float kWidth1 = 7.0f;
125   CFX_Matrix matr2(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
126   CFX_FloatRect rect2(0.0, (float)(m_Height - iTextHeight), kWidth1 - 1,
127                       (float)m_Height);
128   matr2.Concat(matrix);
129   re = matr2.TransformRect(rect2).GetOuterRect();
130   device->FillRect(re, kBackgroundColor);
131   CFX_Matrix matr3(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
132   CFX_FloatRect rect3(kLeftPosition + 85, (float)(m_Height - iTextHeight),
133                       kLeftPosition + 85 + kWidth1 - 0.5, (float)m_Height);
134   matr3.Concat(matrix);
135   re = matr3.TransformRect(rect3).GetOuterRect();
136   device->FillRect(re, kBackgroundColor);
137   float strWidth = kWidth * m_outputHScale;
138 
139   pdfium::span<TextCharPos> charpos_span = pdfium::make_span(charpos);
140   CalcTextInfo(tempStr, charpos_span.subspan(1), m_pFont, strWidth, iFontSize,
141                blank);
142   {
143     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
144                               kLeftPosition * m_outputHScale,
145                               (float)(m_Height - iTextHeight + iFontSize));
146     affine_matrix1.Concat(matrix);
147     device->DrawNormalText(charpos_span.subspan(1, length), m_pFont,
148                            static_cast<float>(iFontSize), affine_matrix1,
149                            m_fontColor, GetTextRenderOptions());
150   }
151   tempStr = str.Substr(6, 5);
152   length = tempStr.GetLength();
153   CalcTextInfo(tempStr, charpos_span.subspan(6), m_pFont, strWidth, iFontSize,
154                blank);
155   {
156     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
157                               (kLeftPosition + 40) * m_outputHScale,
158                               (float)(m_Height - iTextHeight + iFontSize));
159     affine_matrix1.Concat(matrix);
160     device->DrawNormalText(charpos_span.subspan(6, length), m_pFont,
161                            static_cast<float>(iFontSize), affine_matrix1,
162                            m_fontColor, GetTextRenderOptions());
163   }
164   tempStr = str.First(1);
165   length = tempStr.GetLength();
166   strWidth = 7 * m_outputHScale;
167 
168   CalcTextInfo(tempStr, charpos, m_pFont, strWidth, iFontSize, blank);
169   {
170     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0,
171                               (float)(m_Height - iTextHeight + iFontSize));
172     affine_matrix1.Concat(matrix);
173     device->DrawNormalText(charpos_span.first(length), m_pFont,
174                            static_cast<float>(iFontSize), affine_matrix1,
175                            m_fontColor, GetTextRenderOptions());
176   }
177   tempStr = str.Substr(11, 1);
178   length = tempStr.GetLength();
179   CalcTextInfo(tempStr, charpos_span.subspan(11), m_pFont, strWidth, iFontSize,
180                blank);
181   {
182     CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
183                               (kLeftPosition + 85) * m_outputHScale,
184                               (float)(m_Height - iTextHeight + iFontSize));
185     affine_matrix1.Concat(matrix);
186     device->DrawNormalText(charpos_span.subspan(11, length), m_pFont,
187                            static_cast<float>(iFontSize), affine_matrix1,
188                            m_fontColor, GetTextRenderOptions());
189   }
190   return true;
191 }
192