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