summaryrefslogtreecommitdiff
path: root/fxbarcode/oned/BC_OnedCode39Writer.cpp
blob: 4f25eb1570798d20cfa75cdd13fc15be6a238b26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
// Original code is licensed as follows:
/*
 * Copyright 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "fxbarcode/oned/BC_OnedCode39Writer.h"

#include <memory>

#include "fxbarcode/BC_Writer.h"
#include "fxbarcode/common/BC_CommonBitMatrix.h"
#include "fxbarcode/oned/BC_OneDimWriter.h"

namespace {

const char ALPHABET_STRING[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";

const char CHECKSUM_STRING[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%";

const int32_t CHARACTER_ENCODINGS[44] = {
    0x034, 0x121, 0x061, 0x160, 0x031, 0x130, 0x070, 0x025, 0x124,
    0x064, 0x109, 0x049, 0x148, 0x019, 0x118, 0x058, 0x00D, 0x10C,
    0x04C, 0x01C, 0x103, 0x043, 0x142, 0x013, 0x112, 0x052, 0x007,
    0x106, 0x046, 0x016, 0x181, 0x0C1, 0x1C0, 0x091, 0x190, 0x0D0,
    0x085, 0x184, 0x0C4, 0x094, 0x0A8, 0x0A2, 0x08A, 0x02A};

}  // namespace

CBC_OnedCode39Writer::CBC_OnedCode39Writer() : m_iWideNarrRatio(3) {}

CBC_OnedCode39Writer::~CBC_OnedCode39Writer() {}

bool CBC_OnedCode39Writer::CheckContentValidity(
    const WideStringView& contents) {
  for (size_t i = 0; i < contents.GetLength(); i++) {
    wchar_t ch = contents[i];
    if ((ch >= L'0' && ch <= L'9') || (ch >= L'A' && ch <= L'Z') ||
        ch == L'-' || ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' ||
        ch == L'/' || ch == L'+' || ch == L'%') {
      continue;
    }
    return false;
  }
  return true;
}

WideString CBC_OnedCode39Writer::FilterContents(
    const WideStringView& contents) {
  WideString filtercontents;
  for (size_t i = 0; i < contents.GetLength(); i++) {
    wchar_t ch = contents[i];
    if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
      continue;
    }
    if (ch > 175) {
      i++;
      continue;
    }
    ch = Upper(ch);
    if ((ch >= L'0' && ch <= L'9') || (ch >= L'A' && ch <= L'Z') ||
        ch == L'-' || ch == L'.' || ch == L' ' || ch == L'*' || ch == L'$' ||
        ch == L'/' || ch == L'+' || ch == L'%') {
      filtercontents += ch;
    }
  }
  return filtercontents;
}

WideString CBC_OnedCode39Writer::RenderTextContents(
    const WideStringView& contents) {
  WideString renderContents;
  for (size_t i = 0; i < contents.GetLength(); i++) {
    wchar_t ch = contents[i];
    if (ch == L'*' && (i == 0 || i == contents.GetLength() - 1)) {
      continue;
    }
    if (ch > 175) {
      i++;
      continue;
    }
    if ((ch >= L'0' && ch <= L'9') || (ch >= L'A' && ch <= L'Z') ||
        (ch >= L'a' && ch <= L'z') || ch == L'-' || ch == L'.' || ch == L' ' ||
        ch == L'*' || ch == L'$' || ch == L'/' || ch == L'+' || ch == L'%') {
      renderContents += ch;
    }
  }
  return renderContents;
}

bool CBC_OnedCode39Writer::SetTextLocation(BC_TEXT_LOC location) {
  if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) {
    return false;
  }
  m_locTextLoc = location;
  return true;
}
bool CBC_OnedCode39Writer::SetWideNarrowRatio(int8_t ratio) {
  if (ratio < 2 || ratio > 3)
    return false;

  m_iWideNarrRatio = ratio;
  return true;
}

uint8_t* CBC_OnedCode39Writer::EncodeWithHint(const ByteString& contents,
                                              BCFORMAT format,
                                              int32_t& outWidth,
                                              int32_t& outHeight,
                                              int32_t hints) {
  if (format != BCFORMAT_CODE_39)
    return nullptr;
  return CBC_OneDimWriter::EncodeWithHint(contents, format, outWidth, outHeight,
                                          hints);
}

void CBC_OnedCode39Writer::ToIntArray(int32_t a, int8_t* toReturn) {
  for (int32_t i = 0; i < 9; i++) {
    toReturn[i] = (a & (1 << i)) == 0 ? 1 : m_iWideNarrRatio;
  }
}

char CBC_OnedCode39Writer::CalcCheckSum(const ByteString& contents) {
  if (contents.GetLength() > 80)
    return '*';

  int32_t checksum = 0;
  size_t len = strlen(ALPHABET_STRING);
  for (const auto& c : contents) {
    size_t j = 0;
    for (; j < len; j++) {
      if (ALPHABET_STRING[j] == c) {
        if (c != '*')
          checksum += j;
        break;
      }
    }
    if (j >= len)
      return '*';
  }
  checksum = checksum % 43;
  return CHECKSUM_STRING[checksum];
}

uint8_t* CBC_OnedCode39Writer::EncodeImpl(const ByteString& contents,
                                          int32_t& outlength) {
  char checksum = CalcCheckSum(contents);
  if (checksum == '*')
    return nullptr;

  int8_t widths[9] = {0};
  int32_t wideStrideNum = 3;
  int32_t narrStrideNum = 9 - wideStrideNum;
  ByteString encodedContents = contents;
  if (m_bCalcChecksum)
    encodedContents += checksum;
  m_iContentLen = encodedContents.GetLength();
  int32_t codeWidth = (wideStrideNum * m_iWideNarrRatio + narrStrideNum) * 2 +
                      1 + m_iContentLen;
  size_t len = strlen(ALPHABET_STRING);
  for (size_t j = 0; j < m_iContentLen; j++) {
    for (size_t i = 0; i < len; i++) {
      if (ALPHABET_STRING[i] != encodedContents[j])
        continue;

      ToIntArray(CHARACTER_ENCODINGS[i], widths);
      for (size_t k = 0; k < 9; k++)
        codeWidth += widths[k];
    }
  }
  outlength = codeWidth;
  std::unique_ptr<uint8_t, FxFreeDeleter> result(FX_Alloc(uint8_t, codeWidth));
  ToIntArray(CHARACTER_ENCODINGS[39], widths);
  int32_t e = BCExceptionNO;
  int32_t pos = AppendPattern(result.get(), 0, widths, 9, 1, e);
  if (e != BCExceptionNO)
    return nullptr;

  int8_t narrowWhite[] = {1};
  pos += AppendPattern(result.get(), pos, narrowWhite, 1, 0, e);
  if (e != BCExceptionNO)
    return nullptr;

  for (int32_t l = m_iContentLen - 1; l >= 0; l--) {
    for (size_t i = 0; i < len; i++) {
      if (ALPHABET_STRING[i] != encodedContents[l])
        continue;

      ToIntArray(CHARACTER_ENCODINGS[i], widths);
      pos += AppendPattern(result.get(), pos, widths, 9, 1, e);
      if (e != BCExceptionNO)
        return nullptr;
    }
    pos += AppendPattern(result.get(), pos, narrowWhite, 1, 0, e);
    if (e != BCExceptionNO)
      return nullptr;
  }
  ToIntArray(CHARACTER_ENCODINGS[39], widths);
  pos += AppendPattern(result.get(), pos, widths, 9, 1, e);
  if (e != BCExceptionNO)
    return nullptr;

  auto* result_ptr = result.get();
  for (int32_t i = 0; i < codeWidth / 2; i++) {
    result_ptr[i] ^= result_ptr[codeWidth - 1 - i];
    result_ptr[codeWidth - 1 - i] ^= result_ptr[i];
    result_ptr[i] ^= result_ptr[codeWidth - 1 - i];
  }
  return result.release();
}

bool CBC_OnedCode39Writer::encodedContents(const WideStringView& contents,
                                           WideString* result) {
  *result = WideString(contents);
  if (m_bCalcChecksum && m_bPrintChecksum) {
    WideString checksumContent = FilterContents(contents);
    ByteString str = checksumContent.UTF8Encode();
    char checksum;
    checksum = CalcCheckSum(str);
    if (checksum == '*')
      return false;
    str += checksum;
    *result += checksum;
  }
  return true;
}

bool CBC_OnedCode39Writer::RenderResult(const WideStringView& contents,
                                        uint8_t* code,
                                        int32_t codeLength) {
  WideString encodedCon;
  if (!encodedContents(contents, &encodedCon))
    return false;
  return CBC_OneDimWriter::RenderResult(encodedCon.AsStringView(), code,
                                        codeLength);
}