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