kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 1 | // Copyright 2014 The PDFium Authors |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 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 "fxbarcode/oned/BC_OnedUPCAWriter.h" |
| 24 | |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 25 | #include <math.h> |
| 26 | |
| 27 | #include <algorithm> |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | #include "core/fxcrt/fx_extension.h" |
| 31 | #include "core/fxge/cfx_defaultrenderdevice.h" |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 32 | #include "core/fxge/text_char_pos.h" |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 33 | #include "fxbarcode/BC_Writer.h" |
| 34 | #include "fxbarcode/oned/BC_OneDimWriter.h" |
| 35 | #include "fxbarcode/oned/BC_OnedEAN13Writer.h" |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 36 | |
| 37 | CBC_OnedUPCAWriter::CBC_OnedUPCAWriter() { |
| 38 | m_bLeftPadding = true; |
| 39 | m_bRightPadding = true; |
| 40 | } |
| 41 | |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 42 | CBC_OnedUPCAWriter::~CBC_OnedUPCAWriter() = default; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 43 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 44 | bool CBC_OnedUPCAWriter::CheckContentValidity(WideStringView contents) { |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 45 | return HasValidContentSize(contents) && |
| 46 | std::all_of(contents.begin(), contents.end(), |
| 47 | [](wchar_t c) { return FXSYS_IsDecimalDigit(c); }); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 50 | WideString CBC_OnedUPCAWriter::FilterContents(WideStringView contents) { |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 51 | WideString filtercontents; |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 52 | filtercontents.Reserve(contents.GetLength()); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 53 | 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 Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 60 | if (FXSYS_IsDecimalDigit(ch)) |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 61 | filtercontents += ch; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 62 | } |
| 63 | return filtercontents; |
| 64 | } |
| 65 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 66 | void CBC_OnedUPCAWriter::InitEANWriter() { |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 67 | m_subWriter = std::make_unique<CBC_OnedEAN13Writer>(); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 70 | int32_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 | |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 87 | DataVector<uint8_t> CBC_OnedUPCAWriter::Encode(const ByteString& contents) { |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 88 | ByteString toEAN13String = '0' + contents; |
| 89 | m_iDataLenth = 13; |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 90 | return m_subWriter->Encode(toEAN13String); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 93 | bool CBC_OnedUPCAWriter::ShowChars(WideStringView contents, |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 94 | CFX_RenderDevice* device, |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 95 | const CFX_Matrix& matrix, |
| 96 | int32_t barWidth) { |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 97 | if (!device) |
| 98 | return false; |
| 99 | |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 100 | constexpr float kLeftPosition = 17.0f; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 101 | ByteString str = FX_UTF8Encode(contents); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 102 | size_t length = str.GetLength(); |
| 103 | std::vector<TextCharPos> charpos(length); |
| 104 | ByteString tempStr = str.Substr(1, 5); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 105 | constexpr float kWidth = 35.0f; |
| 106 | float blank = 0.0f; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 107 | |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 108 | length = tempStr.GetLength(); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 109 | int32_t iFontSize = static_cast<int32_t>(fabs(m_fFontSize)); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 110 | int32_t iTextHeight = iFontSize + 1; |
| 111 | |
| 112 | CFX_Matrix matr(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 113 | CFX_FloatRect rect(kLeftPosition, (float)(m_Height - iTextHeight), |
| 114 | kLeftPosition + kWidth - 0.5, (float)m_Height); |
| 115 | matr.Concat(matrix); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 116 | FX_RECT re = matr.TransformRect(rect).GetOuterRect(); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 117 | device->FillRect(re, kBackgroundColor); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 118 | CFX_Matrix matr1(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 119 | CFX_FloatRect rect1(kLeftPosition + 40, (float)(m_Height - iTextHeight), |
| 120 | kLeftPosition + 40 + kWidth - 0.5, (float)m_Height); |
| 121 | matr1.Concat(matrix); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 122 | re = matr1.TransformRect(rect1).GetOuterRect(); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 123 | device->FillRect(re, kBackgroundColor); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 124 | constexpr float kWidth1 = 7.0f; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 125 | CFX_Matrix matr2(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 126 | CFX_FloatRect rect2(0.0, (float)(m_Height - iTextHeight), kWidth1 - 1, |
| 127 | (float)m_Height); |
| 128 | matr2.Concat(matrix); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 129 | re = matr2.TransformRect(rect2).GetOuterRect(); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 130 | device->FillRect(re, kBackgroundColor); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 131 | CFX_Matrix matr3(m_outputHScale, 0.0, 0.0, 1.0, 0.0, 0.0); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 132 | CFX_FloatRect rect3(kLeftPosition + 85, (float)(m_Height - iTextHeight), |
| 133 | kLeftPosition + 85 + kWidth1 - 0.5, (float)m_Height); |
| 134 | matr3.Concat(matrix); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 135 | re = matr3.TransformRect(rect3).GetOuterRect(); |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 136 | device->FillRect(re, kBackgroundColor); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 137 | float strWidth = kWidth * m_outputHScale; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 138 | |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 139 | CalcTextInfo(tempStr, &charpos[1], m_pFont, strWidth, iFontSize, blank); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 140 | { |
| 141 | CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 142 | kLeftPosition * m_outputHScale, |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 143 | (float)(m_Height - iTextHeight + iFontSize)); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 144 | 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. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 148 | } |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 149 | tempStr = str.Substr(6, 5); |
| 150 | length = tempStr.GetLength(); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 151 | CalcTextInfo(tempStr, &charpos[6], m_pFont, strWidth, iFontSize, blank); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 152 | { |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 153 | 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. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 160 | } |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 161 | tempStr = str.First(1); |
| 162 | length = tempStr.GetLength(); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 163 | strWidth = 7 * m_outputHScale; |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 164 | |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 165 | CalcTextInfo(tempStr, charpos.data(), m_pFont, strWidth, iFontSize, blank); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 166 | { |
| 167 | CFX_Matrix affine_matrix1(1.0, 0.0, 0.0, -1.0, 0, |
| 168 | (float)(m_Height - iTextHeight + iFontSize)); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 169 | affine_matrix1.Concat(matrix); |
| 170 | device->DrawNormalText(pdfium::make_span(charpos).first(length), m_pFont, |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 171 | static_cast<float>(iFontSize), affine_matrix1, |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 172 | m_fontColor, GetTextRenderOptions()); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 173 | } |
Haibo Huang | 49cc930 | 2020-04-27 16:14:24 -0700 | [diff] [blame] | 174 | tempStr = str.Substr(11, 1); |
| 175 | length = tempStr.GetLength(); |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 176 | CalcTextInfo(tempStr, &charpos[11], m_pFont, strWidth, iFontSize, blank); |
Philip P. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 177 | { |
kumarashishg | 826308d | 2023-06-23 13:21:22 +0000 | [diff] [blame] | 178 | 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. Moltmann | d904c1e | 2018-03-19 09:26:45 -0700 | [diff] [blame] | 185 | } |
| 186 | return true; |
| 187 | } |