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_OnedEAN13Writer.h"
24
25 #include <math.h>
26
27 #include <algorithm>
28 #include <memory>
29 #include <vector>
30
31 #include "core/fxcrt/fx_extension.h"
32 #include "core/fxcrt/fx_memory_wrappers.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 int8_t kFirstDigitEncodings[10] = {0x00, 0x0B, 0x0D, 0xE, 0x13,
42 0x19, 0x1C, 0x15, 0x16, 0x1A};
43 const uint8_t kOnedEAN13StartPattern[3] = {1, 1, 1};
44 const uint8_t kOnedEAN13MiddlePattern[5] = {1, 1, 1, 1, 1};
45 const uint8_t kOnedEAN13LPattern[10][4] = {
46 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
47 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
48 const uint8_t kOnedEAN13LGPattern[20][4] = {
49 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
50 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2},
51 {1, 1, 2, 3}, {1, 2, 2, 2}, {2, 2, 1, 2}, {1, 1, 4, 1}, {2, 3, 1, 1},
52 {1, 3, 2, 1}, {4, 1, 1, 1}, {2, 1, 3, 1}, {3, 1, 2, 1}, {2, 1, 1, 3}};
53
54 } // namespace
55
CBC_OnedEAN13Writer()56 CBC_OnedEAN13Writer::CBC_OnedEAN13Writer() {
57 m_bLeftPadding = true;
58 m_codeWidth = 3 + (7 * 6) + 5 + (7 * 6) + 3;
59 }
60 CBC_OnedEAN13Writer::~CBC_OnedEAN13Writer() = default;
61
CheckContentValidity(WideStringView contents)62 bool CBC_OnedEAN13Writer::CheckContentValidity(WideStringView contents) {
63 return HasValidContentSize(contents) &&
64 std::all_of(contents.begin(), contents.end(),
65 [](wchar_t c) { return FXSYS_IsDecimalDigit(c); });
66 }
67
FilterContents(WideStringView contents)68 WideString CBC_OnedEAN13Writer::FilterContents(WideStringView contents) {
69 WideString filtercontents;
70 filtercontents.Reserve(contents.GetLength());
71 wchar_t ch;
72 for (size_t i = 0; i < contents.GetLength(); i++) {
73 ch = contents[i];
74 if (ch > 175) {
75 i++;
76 continue;
77 }
78 if (FXSYS_IsDecimalDigit(ch))
79 filtercontents += ch;
80 }
81 return filtercontents;
82 }
83
CalcChecksum(const ByteString & contents)84 int32_t CBC_OnedEAN13Writer::CalcChecksum(const ByteString& contents) {
85 return EANCalcChecksum(contents);
86 }
87
Encode(const ByteString & contents)88 DataVector<uint8_t> CBC_OnedEAN13Writer::Encode(const ByteString& contents) {
89 if (contents.GetLength() != 13)
90 return DataVector<uint8_t>();
91
92 m_iDataLenth = 13;
93 int32_t firstDigit = FXSYS_DecimalCharToInt(contents.Front());
94 int32_t parities = kFirstDigitEncodings[firstDigit];
95 DataVector<uint8_t> result(m_codeWidth);
96 auto result_span = pdfium::make_span(result);
97 result_span = AppendPattern(result_span, kOnedEAN13StartPattern, true);
98
99 for (int i = 1; i <= 6; i++) {
100 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
101 if ((parities >> (6 - i) & 1) == 1) {
102 digit += 10;
103 }
104 result_span = AppendPattern(result_span, kOnedEAN13LGPattern[digit], false);
105 }
106 result_span = AppendPattern(result_span, kOnedEAN13MiddlePattern, false);
107
108 for (int i = 7; i <= 12; i++) {
109 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
110 result_span = AppendPattern(result_span, kOnedEAN13LPattern[digit], true);
111 }
112 AppendPattern(result_span, kOnedEAN13StartPattern, true);
113 return result;
114 }
115
ShowChars(WideStringView contents,CFX_RenderDevice * device,const CFX_Matrix & matrix,int32_t barWidth)116 bool CBC_OnedEAN13Writer::ShowChars(WideStringView contents,
117 CFX_RenderDevice* device,
118 const CFX_Matrix& matrix,
119 int32_t barWidth) {
120 if (!device)
121 return false;
122
123 constexpr float kLeftPosition = 10.0f;
124 ByteString str = FX_UTF8Encode(contents);
125 size_t length = str.GetLength();
126 std::vector<TextCharPos> charpos(length);
127 int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize));
128 int32_t iTextHeight = iFontSize + 1;
129 ByteString tempStr = str.Substr(1, 6);
130 constexpr int32_t kWidth = 42;
131
132 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
133 CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight),
134 kLeftPosition + kWidth - 0.5, (float)m_Height);
135 matr.Concat(matrix);
136 FX_RECT re = matr.TransformRect(rect).GetOuterRect();
137 device->FillRect(re, kBackgroundColor);
138 CFX_FloatRect rect1(kLeftPosition + 47, (float)(m_Height - iTextHeight),
139 kLeftPosition + 47 + kWidth - 0.5, (float)m_Height);
140 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
141 matr1.Concat(matrix);
142 re = matr1.TransformRect(rect1).GetOuterRect();
143 device->FillRect(re, kBackgroundColor);
144 CFX_Matrix matr2(m_outputHScale, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f);
145 CFX_FloatRect rect2(0.0f, (float)(m_Height - iTextHeight), 6.5f,
146 (float)m_Height);
147 matr2.Concat(matrix);
148 re = matr2.TransformRect(rect2).GetOuterRect();
149 device->FillRect(re, kBackgroundColor);
150
151 float blank = 0.0f;
152 length = tempStr.GetLength();
153 int32_t strWidth = static_cast<int32_t>(kWidth * m_outputHScale);
154
155 CalcTextInfo(tempStr, &charpos[1], m_pFont, (float)strWidth, iFontSize,
156 blank);
157 {
158 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
159 kLeftPosition * m_outputHScale,
160 (float)(m_Height - iTextHeight) + iFontSize);
161 affine_matrix1.Concat(matrix);
162 device->DrawNormalText(pdfium::make_span(charpos).subspan(1, length),
163 m_pFont, static_cast<float>(iFontSize),
164 affine_matrix1, m_fontColor, GetTextRenderOptions());
165 }
166 tempStr = str.Substr(7, 6);
167 length = tempStr.GetLength();
168 CalcTextInfo(tempStr, &charpos[7], m_pFont, (float)strWidth, iFontSize,
169 blank);
170 {
171 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
172 (kLeftPosition + 47) * m_outputHScale,
173 (float)(m_Height - iTextHeight + iFontSize));
174 affine_matrix1.Concat(matrix);
175 device->DrawNormalText(pdfium::make_span(charpos).subspan(7, length),
176 m_pFont, static_cast<float>(iFontSize),
177 affine_matrix1, m_fontColor, GetTextRenderOptions());
178 }
179 tempStr = str.First(1);
180 length = tempStr.GetLength();
181 strWidth = 7 * static_cast<int32_t>(strWidth * m_outputHScale);
182
183 CalcTextInfo(tempStr, charpos.data(), m_pFont, (float)strWidth, iFontSize,
184 blank);
185 {
186 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0.0,
187 (float)(m_Height - iTextHeight + iFontSize));
188 affine_matrix1.Concat(matrix);
189 device->DrawNormalText(pdfium::make_span(charpos).first(length), m_pFont,
190 static_cast<float>(iFontSize), affine_matrix1,
191 m_fontColor, GetTextRenderOptions());
192 }
193 return true;
194 }
195