blob: 6317c9baf0a2fa02b5bb8eae237bf7a1f6ea48a7 [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 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
kumarashishg826308d2023-06-23 13:21:22 +000025#include <math.h>
26
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070027#include <algorithm>
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070028#include <memory>
29#include <vector>
30
31#include "core/fxcrt/fx_extension.h"
Haibo Huang49cc9302020-04-27 16:14:24 -070032#include "core/fxcrt/fx_memory_wrappers.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070033#include "core/fxge/cfx_defaultrenderdevice.h"
Haibo Huang49cc9302020-04-27 16:14:24 -070034#include "core/fxge/text_char_pos.h"
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070035#include "fxbarcode/BC_Writer.h"
36#include "fxbarcode/common/BC_CommonBitMatrix.h"
37#include "fxbarcode/oned/BC_OneDimWriter.h"
38#include "fxbarcode/oned/BC_OnedEANChecksum.h"
39
40namespace {
41
kumarashishg826308d2023-06-23 13:21:22 +000042const uint8_t kOnedEAN8StartPattern[3] = {1, 1, 1};
43const uint8_t kOnedEAN8MiddlePattern[5] = {1, 1, 1, 1, 1};
44const uint8_t kOnedEAN8LPattern[10][4] = {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070045 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
46 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
47
48} // namespace
49
50CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() {
51 m_iDataLenth = 8;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070052}
53
Haibo Huang49cc9302020-04-27 16:14:24 -070054CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() = default;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070055
56void CBC_OnedEAN8Writer::SetDataLength(int32_t length) {
57 m_iDataLenth = 8;
58}
59
kumarashishg826308d2023-06-23 13:21:22 +000060void CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
61 if (location == BC_TEXT_LOC::kBelowEmbed)
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070062 m_locTextLoc = location;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070063}
64
Haibo Huang49cc9302020-04-27 16:14:24 -070065bool CBC_OnedEAN8Writer::CheckContentValidity(WideStringView contents) {
kumarashishg826308d2023-06-23 13:21:22 +000066 return HasValidContentSize(contents) &&
67 std::all_of(contents.begin(), contents.end(),
Haibo Huang49cc9302020-04-27 16:14:24 -070068 [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070069}
70
Haibo Huang49cc9302020-04-27 16:14:24 -070071WideString CBC_OnedEAN8Writer::FilterContents(WideStringView contents) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070072 WideString filtercontents;
Haibo Huang49cc9302020-04-27 16:14:24 -070073 filtercontents.Reserve(contents.GetLength());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070074 wchar_t ch;
75 for (size_t i = 0; i < contents.GetLength(); i++) {
76 ch = contents[i];
77 if (ch > 175) {
78 i++;
79 continue;
80 }
Haibo Huang49cc9302020-04-27 16:14:24 -070081 if (FXSYS_IsDecimalDigit(ch))
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070082 filtercontents += ch;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070083 }
84 return filtercontents;
85}
86
87int32_t CBC_OnedEAN8Writer::CalcChecksum(const ByteString& contents) {
88 return EANCalcChecksum(contents);
89}
90
kumarashishg826308d2023-06-23 13:21:22 +000091DataVector<uint8_t> CBC_OnedEAN8Writer::Encode(const ByteString& contents) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070092 if (contents.GetLength() != 8)
kumarashishg826308d2023-06-23 13:21:22 +000093 return {};
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070094
kumarashishg826308d2023-06-23 13:21:22 +000095 DataVector<uint8_t> result(m_codeWidth);
96 auto result_span = pdfium::make_span(result);
97 result_span = AppendPattern(result_span, kOnedEAN8StartPattern, true);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -070098
kumarashishg826308d2023-06-23 13:21:22 +000099 for (int i = 0; i <= 3; i++) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700100 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
kumarashishg826308d2023-06-23 13:21:22 +0000101 result_span = AppendPattern(result_span, kOnedEAN8LPattern[digit], false);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700102 }
kumarashishg826308d2023-06-23 13:21:22 +0000103 result_span = AppendPattern(result_span, kOnedEAN8MiddlePattern, false);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700104
kumarashishg826308d2023-06-23 13:21:22 +0000105 for (int i = 4; i <= 7; i++) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700106 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
kumarashishg826308d2023-06-23 13:21:22 +0000107 result_span = AppendPattern(result_span, kOnedEAN8LPattern[digit], true);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700108 }
kumarashishg826308d2023-06-23 13:21:22 +0000109 AppendPattern(result_span, kOnedEAN8StartPattern, true);
110 return result;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700111}
112
Haibo Huang49cc9302020-04-27 16:14:24 -0700113bool CBC_OnedEAN8Writer::ShowChars(WideStringView contents,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700114 CFX_RenderDevice* device,
kumarashishg826308d2023-06-23 13:21:22 +0000115 const CFX_Matrix& matrix,
116 int32_t barWidth) {
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700117 if (!device)
118 return false;
119
kumarashishg826308d2023-06-23 13:21:22 +0000120 constexpr float kLeftPosition = 3.0f;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700121 ByteString str = FX_UTF8Encode(contents);
122 size_t iLength = str.GetLength();
Haibo Huang49cc9302020-04-27 16:14:24 -0700123 std::vector<TextCharPos> charpos(iLength);
124 ByteString tempStr = str.First(4);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700125 size_t iLen = tempStr.GetLength();
kumarashishg826308d2023-06-23 13:21:22 +0000126 constexpr int32_t kWidth = 28;
127 float blank = 0.0f;
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700128
kumarashishg826308d2023-06-23 13:21:22 +0000129 int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700130 int32_t iTextHeight = iFontSize + 1;
131
132 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
kumarashishg826308d2023-06-23 13:21:22 +0000133 CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
134 kLeftPosition + kWidth - 0.5, (float)m_Height);
135 matr.Concat(matrix);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700136 FX_RECT re = matr.TransformRect(rect).GetOuterRect();
Haibo Huang49cc9302020-04-27 16:14:24 -0700137 device->FillRect(re, kBackgroundColor);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700138 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
kumarashishg826308d2023-06-23 13:21:22 +0000139 CFX_FloatRect rect1(kLeftPosition + 33, (float)(m_Height - iTextHeight),
140 kLeftPosition + 33 + kWidth - 0.5, (float)m_Height);
141 matr1.Concat(matrix);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700142 re = matr1.TransformRect(rect1).GetOuterRect();
Haibo Huang49cc9302020-04-27 16:14:24 -0700143 device->FillRect(re, kBackgroundColor);
kumarashishg826308d2023-06-23 13:21:22 +0000144 int32_t strWidth = static_cast<int32_t>(kWidth * m_outputHScale);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700145
kumarashishg826308d2023-06-23 13:21:22 +0000146 CalcTextInfo(tempStr, charpos.data(), m_pFont, (float)strWidth, iFontSize,
147 blank);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700148 {
149 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
kumarashishg826308d2023-06-23 13:21:22 +0000150 kLeftPosition * m_outputHScale,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700151 (float)(m_Height - iTextHeight + iFontSize));
kumarashishg826308d2023-06-23 13:21:22 +0000152 affine_matrix1.Concat(matrix);
153 device->DrawNormalText(pdfium::make_span(charpos).first(iLen), m_pFont,
Haibo Huang49cc9302020-04-27 16:14:24 -0700154 static_cast<float>(iFontSize), affine_matrix1,
kumarashishg826308d2023-06-23 13:21:22 +0000155 m_fontColor, GetTextRenderOptions());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700156 }
Haibo Huang49cc9302020-04-27 16:14:24 -0700157 tempStr = str.Substr(4, 4);
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700158 iLen = tempStr.GetLength();
kumarashishg826308d2023-06-23 13:21:22 +0000159 CalcTextInfo(tempStr, &charpos[4], m_pFont, (float)strWidth, iFontSize,
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700160 blank);
161 {
kumarashishg826308d2023-06-23 13:21:22 +0000162 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
163 (kLeftPosition + 33) * m_outputHScale,
164 (float)(m_Height - iTextHeight + iFontSize));
165 affine_matrix1.Concat(matrix);
166 device->DrawNormalText(pdfium::make_span(charpos).subspan(4, iLen), m_pFont,
Haibo Huang49cc9302020-04-27 16:14:24 -0700167 static_cast<float>(iFontSize), affine_matrix1,
kumarashishg826308d2023-06-23 13:21:22 +0000168 m_fontColor, GetTextRenderOptions());
Philip P. Moltmannd904c1e2018-03-19 09:26:45 -0700169 }
170 return true;
171}