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 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 "core/fxge/cfx_fxgedevice.h"
24#include "core/fxge/cfx_gemodule.h"
25#include "third_party/base/ptr_util.h"
26#include "xfa/fxbarcode/BC_Writer.h"
27#include "xfa/fxbarcode/oned/BC_OneDimWriter.h"
28#include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h"
29#include "xfa/fxbarcode/oned/BC_OnedUPCAWriter.h"
30
31CBC_OnedUPCAWriter::CBC_OnedUPCAWriter() {
32  m_bLeftPadding = true;
33  m_bRightPadding = true;
34}
35
36void CBC_OnedUPCAWriter::Init() {
37  m_subWriter = pdfium::MakeUnique<CBC_OnedEAN13Writer>();
38}
39
40CBC_OnedUPCAWriter::~CBC_OnedUPCAWriter() {}
41
42bool CBC_OnedUPCAWriter::CheckContentValidity(const CFX_WideStringC& contents) {
43  for (FX_STRSIZE i = 0; i < contents.GetLength(); ++i) {
44    if (contents.GetAt(i) < '0' || contents.GetAt(i) > '9')
45      return false;
46  }
47  return true;
48}
49
50CFX_WideString CBC_OnedUPCAWriter::FilterContents(
51    const CFX_WideStringC& contents) {
52  CFX_WideString filtercontents;
53  FX_WCHAR ch;
54  for (int32_t i = 0; i < contents.GetLength(); i++) {
55    ch = contents.GetAt(i);
56    if (ch > 175) {
57      i++;
58      continue;
59    }
60    if (ch >= '0' && ch <= '9') {
61      filtercontents += ch;
62    }
63  }
64  return filtercontents;
65}
66
67int32_t CBC_OnedUPCAWriter::CalcChecksum(const CFX_ByteString& contents) {
68  int32_t odd = 0;
69  int32_t even = 0;
70  int32_t j = 1;
71  for (int32_t i = contents.GetLength() - 1; i >= 0; i--) {
72    if (j % 2) {
73      odd += FXSYS_atoi(contents.Mid(i, 1).c_str());
74    } else {
75      even += FXSYS_atoi(contents.Mid(i, 1).c_str());
76    }
77    j++;
78  }
79  int32_t checksum = (odd * 3 + even) % 10;
80  checksum = (10 - checksum) % 10;
81  return (checksum);
82}
83
84uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents,
85                                    BCFORMAT format,
86                                    int32_t& outWidth,
87                                    int32_t& outHeight,
88                                    int32_t& e) {
89  uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e);
90  if (e != BCExceptionNO)
91    return nullptr;
92  return ret;
93}
94
95uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents,
96                                    BCFORMAT format,
97                                    int32_t& outWidth,
98                                    int32_t& outHeight,
99                                    int32_t hints,
100                                    int32_t& e) {
101  if (format != BCFORMAT_UPC_A) {
102    e = BCExceptionOnlyEncodeUPC_A;
103    return nullptr;
104  }
105  CFX_ByteString toEAN13String = '0' + contents;
106  m_iDataLenth = 13;
107  uint8_t* ret = m_subWriter->Encode(toEAN13String, BCFORMAT_EAN_13, outWidth,
108                                     outHeight, hints, e);
109  if (e != BCExceptionNO)
110    return nullptr;
111  return ret;
112}
113
114uint8_t* CBC_OnedUPCAWriter::Encode(const CFX_ByteString& contents,
115                                    int32_t& outLength,
116                                    int32_t& e) {
117  return nullptr;
118}
119
120void CBC_OnedUPCAWriter::ShowChars(const CFX_WideStringC& contents,
121                                   CFX_DIBitmap* pOutBitmap,
122                                   CFX_RenderDevice* device,
123                                   const CFX_Matrix* matrix,
124                                   int32_t barWidth,
125                                   int32_t multiple,
126                                   int32_t& e) {
127  if (!device && !pOutBitmap) {
128    e = BCExceptionIllegalArgument;
129    return;
130  }
131
132  int32_t leftPadding = 7 * multiple;
133  int32_t leftPosition = 10 * multiple + leftPadding;
134  CFX_ByteString str = FX_UTF8Encode(contents);
135  int32_t iLen = str.GetLength();
136  FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen);
137  FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen);
138  CFX_ByteString tempStr = str.Mid(1, 5);
139  FX_FLOAT strWidth = (FX_FLOAT)35 * multiple;
140  FX_FLOAT blank = 0.0;
141  CFX_FxgeDevice geBitmap;
142  if (pOutBitmap)
143    geBitmap.Attach(pOutBitmap, false, nullptr, false);
144
145  iLen = tempStr.GetLength();
146  int32_t iFontSize = (int32_t)fabs(m_fFontSize);
147  int32_t iTextHeight = iFontSize + 1;
148  if (!pOutBitmap) {
149    CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
150    CFX_FloatRect rect(
151        (FX_FLOAT)leftPosition, (FX_FLOAT)(m_Height - iTextHeight),
152        (FX_FLOAT)(leftPosition + strWidth - 0.5), (FX_FLOAT)m_Height);
153    matr.Concat(*matrix);
154    matr.TransformRect(rect);
155    FX_RECT re = rect.GetOuterRect();
156    device->FillRect(&re, m_backgroundColor);
157    CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
158    CFX_FloatRect rect1(
159        (FX_FLOAT)(leftPosition + 40 * multiple),
160        (FX_FLOAT)(m_Height - iTextHeight),
161        (FX_FLOAT)((leftPosition + 40 * multiple) + strWidth - 0.5),
162        (FX_FLOAT)m_Height);
163    matr1.Concat(*matrix);
164    matr1.TransformRect(rect1);
165    re = rect1.GetOuterRect();
166    device->FillRect(&re, m_backgroundColor);
167    FX_FLOAT strWidth1 = (FX_FLOAT)multiple * 7;
168    CFX_Matrix matr2(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
169    CFX_FloatRect rect2(0.0, (FX_FLOAT)(m_Height - iTextHeight),
170                        (FX_FLOAT)strWidth1 - 1, (FX_FLOAT)m_Height);
171    matr2.Concat(*matrix);
172    matr2.TransformRect(rect2);
173    re = rect2.GetOuterRect();
174    device->FillRect(&re, m_backgroundColor);
175    CFX_Matrix matr3(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0);
176    CFX_FloatRect rect3(
177        (FX_FLOAT)(leftPosition + 85 * multiple),
178        (FX_FLOAT)(m_Height - iTextHeight),
179        (FX_FLOAT)((leftPosition + 85 * multiple) + strWidth1 - 0.5),
180        (FX_FLOAT)m_Height);
181    matr3.Concat(*matrix);
182    matr3.TransformRect(rect3);
183    re = rect3.GetOuterRect();
184    device->FillRect(&re, m_backgroundColor);
185  }
186  if (!pOutBitmap)
187    strWidth = strWidth * m_outputHScale;
188
189  CalcTextInfo(tempStr, pCharPos + 1, m_pFont, strWidth, iFontSize, blank);
190  CFX_Matrix affine_matrix(1.0, 0.0, 0.0, -1.0, 0.0, (FX_FLOAT)iFontSize);
191  CFX_FxgeDevice ge;
192  if (pOutBitmap) {
193    ge.Create((int)strWidth, iTextHeight, FXDIB_Argb, nullptr);
194    ge.GetBitmap()->Clear(m_backgroundColor);
195    ge.DrawNormalText(iLen, pCharPos + 1, m_pFont,
196                      static_cast<FX_FLOAT>(iFontSize), &affine_matrix,
197                      m_fontColor, FXTEXT_CLEARTYPE);
198    geBitmap.SetDIBits(ge.GetBitmap(), leftPosition, m_Height - iTextHeight);
199  } else {
200    CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0,
201                              (FX_FLOAT)leftPosition * m_outputHScale,
202                              (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
203    if (matrix) {
204      affine_matrix1.Concat(*matrix);
205    }
206    device->DrawNormalText(iLen, pCharPos + 1, m_pFont,
207                           static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
208                           m_fontColor, FXTEXT_CLEARTYPE);
209  }
210  tempStr = str.Mid(6, 5);
211  iLen = tempStr.GetLength();
212  CalcTextInfo(tempStr, pCharPos + 6, m_pFont, strWidth, iFontSize, blank);
213  if (pOutBitmap) {
214    FX_RECT rect2(0, 0, (int)strWidth, iTextHeight);
215    ge.FillRect(&rect2, m_backgroundColor);
216    ge.DrawNormalText(iLen, pCharPos + 6, m_pFont,
217                      static_cast<FX_FLOAT>(iFontSize), &affine_matrix,
218                      m_fontColor, FXTEXT_CLEARTYPE);
219    geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 40 * multiple,
220                       m_Height - iTextHeight);
221  } else {
222    CFX_Matrix affine_matrix1(
223        1.0, 0.0, 0.0, -1.0,
224        (FX_FLOAT)(leftPosition + 40 * multiple) * m_outputHScale,
225        (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
226    if (matrix) {
227      affine_matrix1.Concat(*matrix);
228    }
229    device->DrawNormalText(iLen, pCharPos + 6, m_pFont,
230                           static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
231                           m_fontColor, FXTEXT_CLEARTYPE);
232  }
233  tempStr = str.Mid(0, 1);
234  iLen = tempStr.GetLength();
235  strWidth = (FX_FLOAT)multiple * 7;
236  if (!pOutBitmap)
237    strWidth = strWidth * m_outputHScale;
238
239  CalcTextInfo(tempStr, pCharPos, m_pFont, strWidth, iFontSize, blank);
240  if (pOutBitmap) {
241    delete ge.GetBitmap();
242    ge.Create((int)strWidth, iTextHeight, FXDIB_Argb, nullptr);
243    ge.GetBitmap()->Clear(m_backgroundColor);
244    ge.DrawNormalText(iLen, pCharPos, m_pFont, static_cast<FX_FLOAT>(iFontSize),
245                      &affine_matrix, m_fontColor, FXTEXT_CLEARTYPE);
246    geBitmap.SetDIBits(ge.GetBitmap(), 0, m_Height - iTextHeight);
247  } else {
248    CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0,
249                              (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
250    if (matrix) {
251      affine_matrix1.Concat(*matrix);
252    }
253    device->DrawNormalText(iLen, pCharPos, m_pFont,
254                           static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
255                           m_fontColor, FXTEXT_CLEARTYPE);
256  }
257  tempStr = str.Mid(11, 1);
258  iLen = tempStr.GetLength();
259  CalcTextInfo(tempStr, pCharPos + 11, m_pFont, strWidth, iFontSize, blank);
260  if (pOutBitmap) {
261    delete ge.GetBitmap();
262    ge.Create((int)strWidth, iTextHeight, FXDIB_Argb, nullptr);
263    ge.GetBitmap()->Clear(m_backgroundColor);
264    ge.DrawNormalText(iLen, pCharPos + 11, m_pFont,
265                      static_cast<FX_FLOAT>(iFontSize), &affine_matrix,
266                      m_fontColor, FXTEXT_CLEARTYPE);
267    geBitmap.SetDIBits(ge.GetBitmap(), leftPosition + 85 * multiple,
268                       m_Height - iTextHeight);
269  } else {
270    CFX_Matrix affine_matrix1(
271        1.0, 0.0, 0.0, -1.0,
272        (FX_FLOAT)(leftPosition + 85 * multiple) * m_outputHScale,
273        (FX_FLOAT)(m_Height - iTextHeight + iFontSize));
274    if (matrix) {
275      affine_matrix1.Concat(*matrix);
276    }
277    device->DrawNormalText(iLen, pCharPos + 11, m_pFont,
278                           static_cast<FX_FLOAT>(iFontSize), &affine_matrix1,
279                           m_fontColor, FXTEXT_CLEARTYPE);
280  }
281  FX_Free(pCharPos);
282}
283
284void CBC_OnedUPCAWriter::RenderResult(const CFX_WideStringC& contents,
285                                      uint8_t* code,
286                                      int32_t codeLength,
287                                      bool isDevice,
288                                      int32_t& e) {
289  CBC_OneDimWriter::RenderResult(contents, code, codeLength, isDevice, e);
290}
291