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 CalcTextInfo(tempStr, &charpos[1], m_pFont, strWidth, iFontSize, blank);
140 {
141 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
142 kLeftPosition * m_outputHScale,
143 (float)(m_Height - iTextHeight + iFontSize));
144 affine_matrix1.Concat(matrix);
145 device->DrawNormalText(pdfium::make_span(charpos).subspan(1, length),
146 m_pFont, static_cast<float>(iFontSize),
147 affine_matrix1, m_fontColor, GetTextRenderOptions());
148 }
149 tempStr = str.Substr(6, 5);
150 length = tempStr.GetLength();
151 CalcTextInfo(tempStr, &charpos[6], m_pFont, strWidth, iFontSize, blank);
152 {
153 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
154 (kLeftPosition + 40) * m_outputHScale,
155 (float)(m_Height - iTextHeight + iFontSize));
156 affine_matrix1.Concat(matrix);
157 device->DrawNormalText(pdfium::make_span(charpos).subspan(6, length),
158 m_pFont, static_cast<float>(iFontSize),
159 affine_matrix1, m_fontColor, GetTextRenderOptions());
160 }
161 tempStr = str.First(1);
162 length = tempStr.GetLength();
163 strWidth = 7 * m_outputHScale;
164
165 CalcTextInfo(tempStr, charpos.data(), m_pFont, strWidth, iFontSize, blank);
166 {
167 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0,
168 (float)(m_Height - iTextHeight + iFontSize));
169 affine_matrix1.Concat(matrix);
170 device->DrawNormalText(pdfium::make_span(charpos).first(length), m_pFont,
171 static_cast<float>(iFontSize), affine_matrix1,
172 m_fontColor, GetTextRenderOptions());
173 }
174 tempStr = str.Substr(11, 1);
175 length = tempStr.GetLength();
176 CalcTextInfo(tempStr, &charpos[11], m_pFont, strWidth, iFontSize, blank);
177 {
178 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
179 (kLeftPosition + 85) * m_outputHScale,
180 (float)(m_Height - iTextHeight + iFontSize));
181 affine_matrix1.Concat(matrix);
182 device->DrawNormalText(pdfium::make_span(charpos).subspan(11, length),
183 m_pFont, static_cast<float>(iFontSize),
184 affine_matrix1, m_fontColor, GetTextRenderOptions());
185 }
186 return true;
187 }
188