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 "xfa/fxbarcode/BC_Writer.h" 24 #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" 25 #include "xfa/fxbarcode/oned/BC_OneDimWriter.h" 26 #include "xfa/fxbarcode/oned/BC_OnedCode39Writer.h" 27 28 namespace { 29 30 const FX_CHAR ALPHABET_STRING[] = 31 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%"; 32 33 const FX_CHAR CHECKSUM_STRING[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%"; 34 35 const int32_t CHARACTER_ENCODINGS[44] = { 36 0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124, 37 0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C, 38 0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007, 39 0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0, 40 0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A}; 41 42 } // namespace 43 44 CBC_OnedCode39Writer::CBC_OnedCode39Writer() { 45 m_iWideNarrRatio = 3; 46 } 47 CBC_OnedCode39Writer::~CBC_OnedCode39Writer() {} 48 bool CBC_OnedCode39Writer::CheckContentValidity( 49 const CFX_WideStringC& contents) { 50 for (int32_t i = 0; i < contents.GetLength(); i++) { 51 FX_WCHAR ch = contents.GetAt(i); 52 if ((ch >= (FX_WCHAR)'0' && ch <= (FX_WCHAR)'9') || 53 (ch >= (FX_WCHAR)'A' && ch <= (FX_WCHAR)'Z') || ch == (FX_WCHAR)'-' || 54 ch == (FX_WCHAR)'.' || ch == (FX_WCHAR)' ' || ch == (FX_WCHAR)'*' || 55 ch == (FX_WCHAR)'$' || ch == (FX_WCHAR)'/' || ch == (FX_WCHAR)'+' || 56 ch == (FX_WCHAR)'%') { 57 continue; 58 } 59 return false; 60 } 61 return true; 62 } 63 64 CFX_WideString CBC_OnedCode39Writer::FilterContents( 65 const CFX_WideStringC& contents) { 66 CFX_WideString filtercontents; 67 for (int32_t i = 0; i < contents.GetLength(); i++) { 68 FX_WCHAR ch = contents.GetAt(i); 69 if (ch == (FX_WCHAR)'*' && (i == 0 || i == contents.GetLength() - 1)) { 70 continue; 71 } 72 if (ch > 175) { 73 i++; 74 continue; 75 } else { 76 ch = Upper(ch); 77 } 78 if ((ch >= (FX_WCHAR)'0' && ch <= (FX_WCHAR)'9') || 79 (ch >= (FX_WCHAR)'A' && ch <= (FX_WCHAR)'Z') || ch == (FX_WCHAR)'-' || 80 ch == (FX_WCHAR)'.' || ch == (FX_WCHAR)' ' || ch == (FX_WCHAR)'*' || 81 ch == (FX_WCHAR)'$' || ch == (FX_WCHAR)'/' || ch == (FX_WCHAR)'+' || 82 ch == (FX_WCHAR)'%') { 83 filtercontents += ch; 84 } 85 } 86 return filtercontents; 87 } 88 89 CFX_WideString CBC_OnedCode39Writer::RenderTextContents( 90 const CFX_WideStringC& contents) { 91 CFX_WideString renderContents; 92 for (int32_t i = 0; i < contents.GetLength(); i++) { 93 FX_WCHAR ch = contents.GetAt(i); 94 if (ch == (FX_WCHAR)'*' && (i == 0 || i == contents.GetLength() - 1)) { 95 continue; 96 } 97 if (ch > 175) { 98 i++; 99 continue; 100 } 101 if ((ch >= (FX_WCHAR)'0' && ch <= (FX_WCHAR)'9') || 102 (ch >= (FX_WCHAR)'A' && ch <= (FX_WCHAR)'Z') || 103 (ch >= (FX_WCHAR)'a' && ch <= (FX_WCHAR)'z') || ch == (FX_WCHAR)'-' || 104 ch == (FX_WCHAR)'.' || ch == (FX_WCHAR)' ' || ch == (FX_WCHAR)'*' || 105 ch == (FX_WCHAR)'$' || ch == (FX_WCHAR)'/' || ch == (FX_WCHAR)'+' || 106 ch == (FX_WCHAR)'%') { 107 renderContents += ch; 108 } 109 } 110 return renderContents; 111 } 112 113 bool CBC_OnedCode39Writer::SetTextLocation(BC_TEXT_LOC location) { 114 if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { 115 return false; 116 } 117 m_locTextLoc = location; 118 return true; 119 } 120 bool CBC_OnedCode39Writer::SetWideNarrowRatio(int32_t ratio) { 121 if (ratio < 2 || ratio > 3) { 122 return false; 123 } 124 m_iWideNarrRatio = ratio; 125 return true; 126 } 127 uint8_t* CBC_OnedCode39Writer::Encode(const CFX_ByteString& contents, 128 BCFORMAT format, 129 int32_t& outWidth, 130 int32_t& outHeight, 131 int32_t& e) { 132 uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); 133 if (e != BCExceptionNO) 134 return nullptr; 135 return ret; 136 } 137 uint8_t* CBC_OnedCode39Writer::Encode(const CFX_ByteString& contents, 138 BCFORMAT format, 139 int32_t& outWidth, 140 int32_t& outHeight, 141 int32_t hints, 142 int32_t& e) { 143 if (format != BCFORMAT_CODE_39) { 144 e = BCExceptionOnlyEncodeCODE_39; 145 return nullptr; 146 } 147 uint8_t* ret = 148 CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); 149 if (e != BCExceptionNO) 150 return nullptr; 151 return ret; 152 } 153 void CBC_OnedCode39Writer::ToIntArray(int32_t a, int32_t* toReturn) { 154 for (int32_t i = 0; i < 9; i++) { 155 toReturn[i] = (a & (1 << i)) == 0 ? 1 : m_iWideNarrRatio; 156 } 157 } 158 FX_CHAR CBC_OnedCode39Writer::CalcCheckSum(const CFX_ByteString& contents, 159 int32_t& e) { 160 int32_t length = contents.GetLength(); 161 if (length > 80) { 162 e = BCExceptionContentsLengthShouldBetween1and80; 163 return '*'; 164 } 165 int32_t checksum = 0; 166 int32_t len = (int32_t)strlen(ALPHABET_STRING); 167 for (int32_t i = 0; i < contents.GetLength(); i++) { 168 int32_t j = 0; 169 for (; j < len; j++) { 170 if (ALPHABET_STRING[j] == contents[i]) { 171 if (contents[i] != '*') { 172 checksum += j; 173 break; 174 } else { 175 break; 176 } 177 } 178 } 179 if (j >= len) { 180 e = BCExceptionUnSupportedString; 181 return '*'; 182 } 183 } 184 checksum = checksum % 43; 185 return CHECKSUM_STRING[checksum]; 186 } 187 uint8_t* CBC_OnedCode39Writer::Encode(const CFX_ByteString& contents, 188 int32_t& outlength, 189 int32_t& e) { 190 FX_CHAR checksum = CalcCheckSum(contents, e); 191 if (checksum == '*') { 192 return nullptr; 193 } 194 int32_t widths[9] = {0}; 195 int32_t wideStrideNum = 3; 196 int32_t narrStrideNum = 9 - wideStrideNum; 197 CFX_ByteString encodedContents = contents; 198 if (m_bCalcChecksum) { 199 encodedContents += checksum; 200 } 201 m_iContentLen = encodedContents.GetLength(); 202 int32_t codeWidth = (wideStrideNum * m_iWideNarrRatio + narrStrideNum) * 2 + 203 1 + m_iContentLen; 204 int32_t len = (int32_t)strlen(ALPHABET_STRING); 205 for (int32_t j = 0; j < m_iContentLen; j++) { 206 for (int32_t i = 0; i < len; i++) { 207 if (ALPHABET_STRING[i] == encodedContents[j]) { 208 ToIntArray(CHARACTER_ENCODINGS[i], widths); 209 for (int32_t k = 0; k < 9; k++) { 210 codeWidth += widths[k]; 211 } 212 } 213 } 214 } 215 outlength = codeWidth; 216 uint8_t* result = FX_Alloc(uint8_t, codeWidth); 217 ToIntArray(CHARACTER_ENCODINGS[39], widths); 218 int32_t pos = AppendPattern(result, 0, widths, 9, 1, e); 219 if (e != BCExceptionNO) { 220 FX_Free(result); 221 return nullptr; 222 } 223 int32_t narrowWhite[] = {1}; 224 pos += AppendPattern(result, pos, narrowWhite, 1, 0, e); 225 if (e != BCExceptionNO) { 226 FX_Free(result); 227 return nullptr; 228 } 229 for (int32_t l = m_iContentLen - 1; l >= 0; l--) { 230 for (int32_t i = 0; i < len; i++) { 231 if (ALPHABET_STRING[i] == encodedContents[l]) { 232 ToIntArray(CHARACTER_ENCODINGS[i], widths); 233 pos += AppendPattern(result, pos, widths, 9, 1, e); 234 if (e != BCExceptionNO) { 235 FX_Free(result); 236 return nullptr; 237 } 238 } 239 } 240 pos += AppendPattern(result, pos, narrowWhite, 1, 0, e); 241 if (e != BCExceptionNO) { 242 FX_Free(result); 243 return nullptr; 244 } 245 } 246 ToIntArray(CHARACTER_ENCODINGS[39], widths); 247 pos += AppendPattern(result, pos, widths, 9, 1, e); 248 if (e != BCExceptionNO) { 249 FX_Free(result); 250 return nullptr; 251 } 252 for (int32_t i = 0; i < codeWidth / 2; i++) { 253 result[i] ^= result[codeWidth - 1 - i]; 254 result[codeWidth - 1 - i] ^= result[i]; 255 result[i] ^= result[codeWidth - 1 - i]; 256 } 257 return result; 258 } 259 CFX_WideString CBC_OnedCode39Writer::encodedContents( 260 const CFX_WideStringC& contents, 261 int32_t& e) { 262 CFX_WideString encodedContents(contents); 263 if (m_bCalcChecksum && m_bPrintChecksum) { 264 CFX_WideString checksumContent = FilterContents(contents); 265 CFX_ByteString str = checksumContent.UTF8Encode(); 266 FX_CHAR checksum; 267 checksum = CalcCheckSum(str, e); 268 if (e != BCExceptionNO) 269 return CFX_WideString(); 270 str += checksum; 271 encodedContents += checksum; 272 } 273 return encodedContents; 274 } 275 void CBC_OnedCode39Writer::RenderResult(const CFX_WideStringC& contents, 276 uint8_t* code, 277 int32_t codeLength, 278 bool isDevice, 279 int32_t& e) { 280 CFX_WideString encodedCon = encodedContents(contents, e); 281 if (e != BCExceptionNO) 282 return; 283 CBC_OneDimWriter::RenderResult(encodedCon.AsStringC(), code, codeLength, 284 isDevice, e); 285 } 286