1 // Copyright 2014 PDFium Authors. All rights reserved.
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_OnedEAN8Writer.h"
24
25 #include <algorithm>
26 #include <cwctype>
27 #include <memory>
28 #include <vector>
29
30 #include "core/fxcrt/fx_extension.h"
31 #include "core/fxge/cfx_defaultrenderdevice.h"
32 #include "fxbarcode/BC_Writer.h"
33 #include "fxbarcode/common/BC_CommonBitMatrix.h"
34 #include "fxbarcode/oned/BC_OneDimWriter.h"
35 #include "fxbarcode/oned/BC_OnedEANChecksum.h"
36
37 namespace {
38
39 const int8_t kOnedEAN8StartPattern[3] = {1, 1, 1};
40 const int8_t kOnedEAN8MiddlePattern[5] = {1, 1, 1, 1, 1};
41 const int8_t kOnedEAN8LPattern[10][4] = {
42 {3, 2, 1, 1}, {2, 2, 2, 1}, {2, 1, 2, 2}, {1, 4, 1, 1}, {1, 1, 3, 2},
43 {1, 2, 3, 1}, {1, 1, 1, 4}, {1, 3, 1, 2}, {1, 2, 1, 3}, {3, 1, 1, 2}};
44
45 } // namespace
46
CBC_OnedEAN8Writer()47 CBC_OnedEAN8Writer::CBC_OnedEAN8Writer() {
48 m_iDataLenth = 8;
49 m_codeWidth = 3 + (7 * 4) + 5 + (7 * 4) + 3;
50 }
51
~CBC_OnedEAN8Writer()52 CBC_OnedEAN8Writer::~CBC_OnedEAN8Writer() {}
53
SetDataLength(int32_t length)54 void CBC_OnedEAN8Writer::SetDataLength(int32_t length) {
55 m_iDataLenth = 8;
56 }
57
SetTextLocation(BC_TEXT_LOC location)58 bool CBC_OnedEAN8Writer::SetTextLocation(BC_TEXT_LOC location) {
59 if (location == BC_TEXT_LOC_BELOWEMBED) {
60 m_locTextLoc = location;
61 return true;
62 }
63 return false;
64 }
65
CheckContentValidity(const WideStringView & contents)66 bool CBC_OnedEAN8Writer::CheckContentValidity(const WideStringView& contents) {
67 return std::all_of(contents.begin(), contents.end(), std::iswdigit);
68 }
69
FilterContents(const WideStringView & contents)70 WideString CBC_OnedEAN8Writer::FilterContents(const WideStringView& contents) {
71 WideString filtercontents;
72 wchar_t ch;
73 for (size_t i = 0; i < contents.GetLength(); i++) {
74 ch = contents[i];
75 if (ch > 175) {
76 i++;
77 continue;
78 }
79 if (ch >= '0' && ch <= '9') {
80 filtercontents += ch;
81 }
82 }
83 return filtercontents;
84 }
85
CalcChecksum(const ByteString & contents)86 int32_t CBC_OnedEAN8Writer::CalcChecksum(const ByteString& contents) {
87 return EANCalcChecksum(contents);
88 }
89
EncodeWithHint(const ByteString & contents,BCFORMAT format,int32_t & outWidth,int32_t & outHeight,int32_t hints)90 uint8_t* CBC_OnedEAN8Writer::EncodeWithHint(const ByteString& contents,
91 BCFORMAT format,
92 int32_t& outWidth,
93 int32_t& outHeight,
94 int32_t hints) {
95 if (format != BCFORMAT_EAN_8)
96 return nullptr;
97 return CBC_OneDimWriter::EncodeWithHint(contents, format, outWidth, outHeight,
98 hints);
99 }
100
EncodeImpl(const ByteString & contents,int32_t & outLength)101 uint8_t* CBC_OnedEAN8Writer::EncodeImpl(const ByteString& contents,
102 int32_t& outLength) {
103 if (contents.GetLength() != 8)
104 return nullptr;
105
106 outLength = m_codeWidth;
107 std::unique_ptr<uint8_t, FxFreeDeleter> result(
108 FX_Alloc(uint8_t, m_codeWidth));
109 int32_t pos = 0;
110 int32_t e = BCExceptionNO;
111 pos += AppendPattern(result.get(), pos, kOnedEAN8StartPattern, 3, 1, e);
112 if (e != BCExceptionNO)
113 return nullptr;
114
115 int32_t i = 0;
116 for (i = 0; i <= 3; i++) {
117 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
118 pos += AppendPattern(result.get(), pos, kOnedEAN8LPattern[digit], 4, 0, e);
119 if (e != BCExceptionNO)
120 return nullptr;
121 }
122 pos += AppendPattern(result.get(), pos, kOnedEAN8MiddlePattern, 5, 0, e);
123 if (e != BCExceptionNO)
124 return nullptr;
125
126 for (i = 4; i <= 7; i++) {
127 int32_t digit = FXSYS_DecimalCharToInt(contents[i]);
128 pos += AppendPattern(result.get(), pos, kOnedEAN8LPattern[digit], 4, 1, e);
129 if (e != BCExceptionNO)
130 return nullptr;
131 }
132 pos += AppendPattern(result.get(), pos, kOnedEAN8StartPattern, 3, 1, e);
133 if (e != BCExceptionNO)
134 return nullptr;
135 return result.release();
136 }
137
ShowChars(const WideStringView & contents,CFX_RenderDevice * device,const CFX_Matrix * matrix,int32_t barWidth,int32_t multiple)138 bool CBC_OnedEAN8Writer::ShowChars(const WideStringView& contents,
139 CFX_RenderDevice* device,
140 const CFX_Matrix* matrix,
141 int32_t barWidth,
142 int32_t multiple) {
143 if (!device)
144 return false;
145
146 int32_t leftPosition = 3 * multiple;
147 ByteString str = FX_UTF8Encode(contents);
148 size_t iLength = str.GetLength();
149 std::vector<FXTEXT_CHARPOS> charpos(iLength);
150 ByteString tempStr = str.Left(4);
151 size_t iLen = tempStr.GetLength();
152 int32_t strWidth = 7 * multiple * 4;
153 float blank = 0.0;
154
155 int32_t iFontSize = (int32_t)fabs(m_fFontSize);
156 int32_t iTextHeight = iFontSize + 1;
157
158 CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
159 CFX_FloatRect rect((float)leftPosition, (float)(m_Height - iTextHeight),
160 (float)(leftPosition + strWidth - 0.5), (float)m_Height);
161 matr.Concat(*matrix);
162 FX_RECT re = matr.TransformRect(rect).GetOuterRect();
163 device->FillRect(&re, m_backgroundColor);
164 CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
165 CFX_FloatRect rect1(
166 (float)(leftPosition + 33 * multiple), (float)(m_Height - iTextHeight),
167 (float)(leftPosition + 33 * multiple + strWidth - 0.5), (float)m_Height);
168 matr1.Concat(*matrix);
169 re = matr1.TransformRect(rect1).GetOuterRect();
170 device->FillRect(&re, m_backgroundColor);
171 strWidth = (int32_t)(strWidth * m_outputHScale);
172
173 CalcTextInfo(tempStr, charpos.data(), m_pFont.Get(), (float)strWidth,
174 iFontSize, blank);
175 {
176 CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
177 (float)leftPosition * m_outputHScale,
178 (float)(m_Height - iTextHeight + iFontSize));
179 affine_matrix1.Concat(*matrix);
180 device->DrawNormalText(iLen, charpos.data(), m_pFont.Get(),
181 static_cast<float>(iFontSize), &affine_matrix1,
182 m_fontColor, FXTEXT_CLEARTYPE);
183 }
184 tempStr = str.Mid(4, 4);
185 iLen = tempStr.GetLength();
186 CalcTextInfo(tempStr, &charpos[4], m_pFont.Get(), (float)strWidth, iFontSize,
187 blank);
188 {
189 CFX_Matrix affine_matrix1(
190 1.0, 0.0, 0.0, -1.0,
191 (float)(leftPosition + 33 * multiple) * m_outputHScale,
192 (float)(m_Height - iTextHeight + iFontSize));
193 if (matrix)
194 affine_matrix1.Concat(*matrix);
195 device->DrawNormalText(iLen, &charpos[4], m_pFont.Get(),
196 static_cast<float>(iFontSize), &affine_matrix1,
197 m_fontColor, FXTEXT_CLEARTYPE);
198 }
199 return true;
200 }
201