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