blob: 55a6a02333781508776e7ee1b76912710558d7cd [file] [log] [blame]
kumarashishg826308d2023-06-23 13:21:22 +00001// Copyright 2014 The PDFium Authors
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -07002// 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
kumarashishg826308d2023-06-23 13:21:22 +000025#include <math.h>
26
27#include <algorithm>
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070028#include <vector>
29
30#include "core/fxcrt/fx_extension.h"
31#include "core/fxge/cfx_defaultrenderdevice.h"
Haibo Huang49cc9302020-04-27 16:14:24 -070032#include "core/fxge/text_char_pos.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070033#include "fxbarcode/BC_Writer.h"
34#include "fxbarcode/oned/BC_OneDimWriter.h"
35#include "fxbarcode/oned/BC_OnedEAN13Writer.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070036
37CBC_OnedUPCAWriter::CBC_OnedUPCAWriter() {
38 m_bLeftPadding = true;
39 m_bRightPadding = true;
40}
41
kumarashishg826308d2023-06-23 13:21:22 +000042CBC_OnedUPCAWriter::~CBC_OnedUPCAWriter() = default;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070043
Haibo Huang49cc9302020-04-27 16:14:24 -070044bool CBC_OnedUPCAWriter::CheckContentValidity(WideStringView contents) {
kumarashishg826308d2023-06-23 13:21:22 +000045 return HasValidContentSize(contents) &&
46 std::all_of(contents.begin(), contents.end(),
47 [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070048}
49
Haibo Huang49cc9302020-04-27 16:14:24 -070050WideString CBC_OnedUPCAWriter::FilterContents(WideStringView contents) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070051 WideString filtercontents;
Haibo Huang49cc9302020-04-27 16:14:24 -070052 filtercontents.Reserve(contents.GetLength());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070053 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 }
Haibo Huang49cc9302020-04-27 16:14:24 -070060 if (FXSYS_IsDecimalDigit(ch))
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070061 filtercontents += ch;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070062 }
63 return filtercontents;
64}
65
Haibo Huang49cc9302020-04-27 16:14:24 -070066void CBC_OnedUPCAWriter::InitEANWriter() {
kumarashishg826308d2023-06-23 13:21:22 +000067 m_subWriter = std::make_unique<CBC_OnedEAN13Writer>();
Haibo Huang49cc9302020-04-27 16:14:24 -070068}
69
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070070int32_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
kumarashishg826308d2023-06-23 13:21:22 +000087DataVector<uint8_t> CBC_OnedUPCAWriter::Encode(const ByteString& contents) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070088 ByteString toEAN13String = '0' + contents;
89 m_iDataLenth = 13;
kumarashishg826308d2023-06-23 13:21:22 +000090 return m_subWriter->Encode(toEAN13String);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070091}
92
Haibo Huang49cc9302020-04-27 16:14:24 -070093bool CBC_OnedUPCAWriter::ShowChars(WideStringView contents,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070094 CFX_RenderDevice* device,
kumarashishg826308d2023-06-23 13:21:22 +000095 const CFX_Matrix& matrix,
96 int32_t barWidth) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070097 if (!device)
98 return false;
99
kumarashishg826308d2023-06-23 13:21:22 +0000100 constexpr float kLeftPosition = 17.0f;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700101 ByteString str = FX_UTF8Encode(contents);
Haibo Huang49cc9302020-04-27 16:14:24 -0700102 size_t length = str.GetLength();
103 std::vector<TextCharPos> charpos(length);
104 ByteString tempStr = str.Substr(1, 5);
kumarashishg826308d2023-06-23 13:21:22 +0000105 constexpr float kWidth = 35.0f;
106 float blank = 0.0f;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700107
Haibo Huang49cc9302020-04-27 16:14:24 -0700108 length = tempStr.GetLength();
kumarashishg826308d2023-06-23 13:21:22 +0000109 int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700110 int32_t iTextHeight = iFontSize + 1;
111
112 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
kumarashishg826308d2023-06-23 13:21:22 +0000113 CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
114 kLeftPosition + kWidth - 0.5, (float)m_Height);
115 matr.Concat(matrix);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700116 FX_RECT re = matr.TransformRect(rect).GetOuterRect();
Haibo Huang49cc9302020-04-27 16:14:24 -0700117 device->FillRect(re, kBackgroundColor);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700118 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
kumarashishg826308d2023-06-23 13:21:22 +0000119 CFX_FloatRect rect1(kLeftPosition + 40, (float)(m_Height - iTextHeight),
120 kLeftPosition + 40 + kWidth - 0.5, (float)m_Height);
121 matr1.Concat(matrix);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700122 re = matr1.TransformRect(rect1).GetOuterRect();
Haibo Huang49cc9302020-04-27 16:14:24 -0700123 device->FillRect(re, kBackgroundColor);
kumarashishg826308d2023-06-23 13:21:22 +0000124 constexpr float kWidth1 = 7.0f;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700125 CFX_Matrix matr2(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
kumarashishg826308d2023-06-23 13:21:22 +0000126 CFX_FloatRect rect2(0.0, (float)(m_Height - iTextHeight), kWidth1 - 1,
127 (float)m_Height);
128 matr2.Concat(matrix);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700129 re = matr2.TransformRect(rect2).GetOuterRect();
Haibo Huang49cc9302020-04-27 16:14:24 -0700130 device->FillRect(re, kBackgroundColor);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700131 CFX_Matrix matr3(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
kumarashishg826308d2023-06-23 13:21:22 +0000132 CFX_FloatRect rect3(kLeftPosition + 85, (float)(m_Height - iTextHeight),
133 kLeftPosition + 85 + kWidth1 - 0.5, (float)m_Height);
134 matr3.Concat(matrix);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700135 re = matr3.TransformRect(rect3).GetOuterRect();
Haibo Huang49cc9302020-04-27 16:14:24 -0700136 device->FillRect(re, kBackgroundColor);
kumarashishg826308d2023-06-23 13:21:22 +0000137 float strWidth = kWidth * m_outputHScale;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700138
kumarashishg826308d2023-06-23 13:21:22 +0000139 CalcTextInfo(tempStr, &charpos[1], m_pFont, strWidth, iFontSize, blank);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700140 {
141 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
kumarashishg826308d2023-06-23 13:21:22 +0000142 kLeftPosition * m_outputHScale,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700143 (float)(m_Height - iTextHeight + iFontSize));
kumarashishg826308d2023-06-23 13:21:22 +0000144 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());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700148 }
Haibo Huang49cc9302020-04-27 16:14:24 -0700149 tempStr = str.Substr(6, 5);
150 length = tempStr.GetLength();
kumarashishg826308d2023-06-23 13:21:22 +0000151 CalcTextInfo(tempStr, &charpos[6], m_pFont, strWidth, iFontSize, blank);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700152 {
kumarashishg826308d2023-06-23 13:21:22 +0000153 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());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700160 }
Haibo Huang49cc9302020-04-27 16:14:24 -0700161 tempStr = str.First(1);
162 length = tempStr.GetLength();
kumarashishg826308d2023-06-23 13:21:22 +0000163 strWidth = 7 * m_outputHScale;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700164
kumarashishg826308d2023-06-23 13:21:22 +0000165 CalcTextInfo(tempStr, charpos.data(), m_pFont, strWidth, iFontSize, blank);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700166 {
167 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0,
168 (float)(m_Height - iTextHeight + iFontSize));
kumarashishg826308d2023-06-23 13:21:22 +0000169 affine_matrix1.Concat(matrix);
170 device->DrawNormalText(pdfium::make_span(charpos).first(length), m_pFont,
Haibo Huang49cc9302020-04-27 16:14:24 -0700171 static_cast<float>(iFontSize), affine_matrix1,
kumarashishg826308d2023-06-23 13:21:22 +0000172 m_fontColor, GetTextRenderOptions());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700173 }
Haibo Huang49cc9302020-04-27 16:14:24 -0700174 tempStr = str.Substr(11, 1);
175 length = tempStr.GetLength();
kumarashishg826308d2023-06-23 13:21:22 +0000176 CalcTextInfo(tempStr, &charpos[11], m_pFont, strWidth, iFontSize, blank);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700177 {
kumarashishg826308d2023-06-23 13:21:22 +0000178 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());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700185 }
186 return true;
187}