• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 2011 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_OnedCodaBarWriter.h"
24 
25 #include <iterator>
26 
27 #include "core/fxcrt/fx_2d_size.h"
28 #include "core/fxcrt/fx_extension.h"
29 #include "fxbarcode/BC_Writer.h"
30 #include "fxbarcode/common/BC_CommonBitMatrix.h"
31 #include "fxbarcode/oned/BC_OneDimWriter.h"
32 #include "third_party/base/containers/contains.h"
33 
34 namespace {
35 
36 const char kOnedCodaAlphabet[] = {'0', '1', '2', '3', '4', '5', '6', '7',
37                                   '8', '9', '-', '$', ':', '/', '.', '+',
38                                   'A', 'B', 'C', 'D', 'T', 'N'};
39 
40 const int8_t kOnedCodaCharacterEncoding[] = {
41     0x03, 0x06, 0x09, 0x60, 0x12, 0x42, 0x21, 0x24, 0x30, 0x48, 0x0c,
42     0x18, 0x45, 0x51, 0x54, 0x15, 0x1A, 0x29, 0x0B, 0x0E, 0x1A, 0x29};
43 static_assert(std::size(kOnedCodaCharacterEncoding) == 22, "Wrong size");
44 static_assert(std::size(kOnedCodaCharacterEncoding) ==
45                   std::size(kOnedCodaAlphabet),
46               "Wrong size");
47 
48 const char kStartEndChars[] = {'A', 'B', 'C', 'D', 'T', 'N', '*', 'E',
49                                'a', 'b', 'c', 'd', 't', 'n', 'e'};
50 const char kContentChars[] = {'0', '1', '2', '3', '4', '5', '6', '7',
51                               '8', '9', '-', '$', '/', ':', '+', '.'};
52 
IsValidChar(wchar_t ch,bool isContent)53 bool IsValidChar(wchar_t ch, bool isContent) {
54   if (ch > 0x7F)
55     return false;
56 
57   char narrow_ch = static_cast<char>(ch);
58   return pdfium::Contains(kContentChars, narrow_ch) ||
59          (isContent && pdfium::Contains(kStartEndChars, narrow_ch));
60 }
61 
62 }  // namespace
63 
64 CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() = default;
65 
66 CBC_OnedCodaBarWriter::~CBC_OnedCodaBarWriter() = default;
67 
SetStartChar(char start)68 bool CBC_OnedCodaBarWriter::SetStartChar(char start) {
69   if (!pdfium::Contains(kStartEndChars, start))
70     return false;
71 
72   m_chStart = start;
73   return true;
74 }
75 
SetEndChar(char end)76 bool CBC_OnedCodaBarWriter::SetEndChar(char end) {
77   if (!pdfium::Contains(kStartEndChars, end))
78     return false;
79 
80   m_chEnd = end;
81   return true;
82 }
83 
SetDataLength(int32_t length)84 void CBC_OnedCodaBarWriter::SetDataLength(int32_t length) {
85   m_iDataLenth = length + 2;
86 }
87 
SetTextLocation(BC_TEXT_LOC location)88 void CBC_OnedCodaBarWriter::SetTextLocation(BC_TEXT_LOC location) {
89   m_locTextLoc = location;
90 }
91 
SetWideNarrowRatio(int8_t ratio)92 bool CBC_OnedCodaBarWriter::SetWideNarrowRatio(int8_t ratio) {
93   if (ratio < 2 || ratio > 3)
94     return false;
95 
96   m_iWideNarrRatio = ratio;
97   return true;
98 }
99 
CheckContentValidity(WideStringView contents)100 bool CBC_OnedCodaBarWriter::CheckContentValidity(WideStringView contents) {
101   return HasValidContentSize(contents) &&
102          std::all_of(contents.begin(), contents.end(),
103                      [](const wchar_t& ch) { return IsValidChar(ch, false); });
104 }
105 
FilterContents(WideStringView contents)106 WideString CBC_OnedCodaBarWriter::FilterContents(WideStringView contents) {
107   WideString filtercontents;
108   filtercontents.Reserve(contents.GetLength());
109   wchar_t ch;
110   for (size_t index = 0; index < contents.GetLength(); index++) {
111     ch = contents[index];
112     if (ch > 175) {
113       index++;
114       continue;
115     }
116     if (!IsValidChar(ch, true))
117       continue;
118     filtercontents += ch;
119   }
120   return filtercontents;
121 }
122 
Encode(const ByteString & contents)123 DataVector<uint8_t> CBC_OnedCodaBarWriter::Encode(const ByteString& contents) {
124   ByteString data = m_chStart + contents + m_chEnd;
125   m_iContentLen = data.GetLength();
126   DataVector<uint8_t> result(
127       Fx2DSizeOrDie(m_iWideNarrRatio * 7, data.GetLength()));
128   char ch;
129   int32_t position = 0;
130   for (size_t index = 0; index < data.GetLength(); index++) {
131     ch = FXSYS_ToUpperASCII(data[index]);
132     switch (ch) {
133       case 'T':
134         ch = 'A';
135         break;
136       case 'N':
137         ch = 'B';
138         break;
139       case '*':
140         ch = 'C';
141         break;
142       case 'E':
143         ch = 'D';
144         break;
145       default:
146         break;
147     }
148     int8_t code = 0;
149     for (size_t i = 0; i < std::size(kOnedCodaAlphabet); i++) {
150       if (ch == kOnedCodaAlphabet[i]) {
151         code = kOnedCodaCharacterEncoding[i];
152         break;
153       }
154     }
155     uint8_t color = 1;
156     int32_t counter = 0;
157     int32_t bit = 0;
158     while (bit < 7) {
159       result[position] = color;
160       position++;
161       if (((code >> (6 - bit)) & 1) == 0 || counter == m_iWideNarrRatio - 1) {
162         color = !color;
163         bit++;
164         counter = 0;
165       } else {
166         counter++;
167       }
168     }
169     if (index < data.GetLength() - 1) {
170       result[position] = 0;
171       position++;
172     }
173   }
174   result.resize(position);
175   return result;
176 }
177 
encodedContents(WideStringView contents)178 WideString CBC_OnedCodaBarWriter::encodedContents(WideStringView contents) {
179   WideString strStart(static_cast<wchar_t>(m_chStart));
180   WideString strEnd(static_cast<wchar_t>(m_chEnd));
181   return strStart + contents + strEnd;
182 }
183 
RenderResult(WideStringView contents,pdfium::span<const uint8_t> code)184 bool CBC_OnedCodaBarWriter::RenderResult(WideStringView contents,
185                                          pdfium::span<const uint8_t> code) {
186   return CBC_OneDimWriter::RenderResult(
187       encodedContents(contents).AsStringView(), code);
188 }
189