OLD | NEW |
| (Empty) |
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 "xfa/src/fxbarcode/BC_Reader.h" | |
24 #include "xfa/src/fxbarcode/BC_Writer.h" | |
25 #include "xfa/src/fxbarcode/common/BC_CommonBitArray.h" | |
26 #include "xfa/src/fxbarcode/common/BC_CommonBitMatrix.h" | |
27 #include "xfa/src/fxbarcode/oned/BC_OneDReader.h" | |
28 #include "xfa/src/fxbarcode/oned/BC_OneDimWriter.h" | |
29 #include "xfa/src/fxbarcode/oned/BC_OnedCodaBarReader.h" | |
30 #include "xfa/src/fxbarcode/oned/BC_OnedCodaBarWriter.h" | |
31 | |
32 const FX_CHAR CBC_OnedCodaBarWriter::START_END_CHARS[] = { | |
33 'A', 'B', 'C', 'D', 'T', 'N', '*', 'E', 'a', 'b', 'c', 'd', 't', 'n', 'e'}; | |
34 const FX_CHAR CBC_OnedCodaBarWriter::CONTENT_CHARS[] = { | |
35 '0', '1', '2', '3', '4', '5', '6', '7', | |
36 '8', '9', '-', '$', '/', ':', '+', '.'}; | |
37 | |
38 CBC_OnedCodaBarWriter::CBC_OnedCodaBarWriter() { | |
39 m_chStart = 'A'; | |
40 m_chEnd = 'B'; | |
41 m_iWideNarrRatio = 2; | |
42 } | |
43 CBC_OnedCodaBarWriter::~CBC_OnedCodaBarWriter() {} | |
44 FX_BOOL CBC_OnedCodaBarWriter::SetStartChar(FX_CHAR start) { | |
45 for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) { | |
46 if (START_END_CHARS[i] == start) { | |
47 m_chStart = start; | |
48 return TRUE; | |
49 } | |
50 } | |
51 return FALSE; | |
52 } | |
53 | |
54 FX_BOOL CBC_OnedCodaBarWriter::SetEndChar(FX_CHAR end) { | |
55 for (size_t i = 0; i < FX_ArraySize(START_END_CHARS); ++i) { | |
56 if (START_END_CHARS[i] == end) { | |
57 m_chEnd = end; | |
58 return TRUE; | |
59 } | |
60 } | |
61 return FALSE; | |
62 } | |
63 void CBC_OnedCodaBarWriter::SetDataLength(int32_t length) { | |
64 m_iDataLenth = length + 2; | |
65 } | |
66 FX_BOOL CBC_OnedCodaBarWriter::SetTextLocation(BC_TEXT_LOC location) { | |
67 if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { | |
68 return FALSE; | |
69 } | |
70 m_locTextLoc = location; | |
71 return TRUE; | |
72 } | |
73 FX_BOOL CBC_OnedCodaBarWriter::SetWideNarrowRatio(int32_t ratio) { | |
74 if (ratio < 2 || ratio > 3) { | |
75 return FALSE; | |
76 } | |
77 m_iWideNarrRatio = ratio; | |
78 return TRUE; | |
79 } | |
80 FX_BOOL CBC_OnedCodaBarWriter::FindChar(FX_WCHAR ch, FX_BOOL isContent) { | |
81 if (isContent) { | |
82 for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) { | |
83 if (ch == (FX_WCHAR)CONTENT_CHARS[i]) { | |
84 return TRUE; | |
85 } | |
86 } | |
87 for (size_t j = 0; j < FX_ArraySize(START_END_CHARS); ++j) { | |
88 if (ch == (FX_WCHAR)START_END_CHARS[j]) { | |
89 return TRUE; | |
90 } | |
91 } | |
92 return FALSE; | |
93 } else { | |
94 for (size_t i = 0; i < FX_ArraySize(CONTENT_CHARS); ++i) { | |
95 if (ch == (FX_WCHAR)CONTENT_CHARS[i]) { | |
96 return TRUE; | |
97 } | |
98 } | |
99 return FALSE; | |
100 } | |
101 } | |
102 FX_BOOL CBC_OnedCodaBarWriter::CheckContentValidity( | |
103 const CFX_WideStringC& contents) { | |
104 FX_WCHAR ch; | |
105 int32_t index = 0; | |
106 for (index = 0; index < contents.GetLength(); index++) { | |
107 ch = contents.GetAt(index); | |
108 if (FindChar(ch, FALSE)) { | |
109 continue; | |
110 } else { | |
111 return FALSE; | |
112 } | |
113 } | |
114 return TRUE; | |
115 } | |
116 CFX_WideString CBC_OnedCodaBarWriter::FilterContents( | |
117 const CFX_WideStringC& contents) { | |
118 CFX_WideString filtercontents; | |
119 FX_WCHAR ch; | |
120 for (int32_t index = 0; index < contents.GetLength(); index++) { | |
121 ch = contents.GetAt(index); | |
122 if (ch > 175) { | |
123 index++; | |
124 continue; | |
125 } | |
126 if (FindChar(ch, TRUE)) { | |
127 filtercontents += ch; | |
128 } else { | |
129 continue; | |
130 } | |
131 } | |
132 return filtercontents; | |
133 } | |
134 uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents, | |
135 BCFORMAT format, | |
136 int32_t& outWidth, | |
137 int32_t& outHeight, | |
138 int32_t& e) { | |
139 uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); | |
140 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
141 return ret; | |
142 } | |
143 uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents, | |
144 BCFORMAT format, | |
145 int32_t& outWidth, | |
146 int32_t& outHeight, | |
147 int32_t hints, | |
148 int32_t& e) { | |
149 if (format != BCFORMAT_CODABAR) { | |
150 e = BCExceptionOnlyEncodeCODEBAR; | |
151 return NULL; | |
152 } | |
153 uint8_t* ret = | |
154 CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); | |
155 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
156 return ret; | |
157 } | |
158 uint8_t* CBC_OnedCodaBarWriter::Encode(const CFX_ByteString& contents, | |
159 int32_t& outLength, | |
160 int32_t& e) { | |
161 CBC_OnedCodaBarReader CodaBarR; | |
162 CFX_ByteString data = m_chStart + contents + m_chEnd; | |
163 m_iContentLen = data.GetLength(); | |
164 uint8_t* result = FX_Alloc2D(uint8_t, m_iWideNarrRatio * 7, data.GetLength()); | |
165 FX_CHAR ch; | |
166 int32_t position = 0; | |
167 for (int32_t index = 0; index < data.GetLength(); index++) { | |
168 ch = data.GetAt(index); | |
169 if (((ch >= 'a') && (ch <= 'z'))) { | |
170 ch = ch - 32; | |
171 } | |
172 switch (ch) { | |
173 case 'T': | |
174 ch = 'A'; | |
175 break; | |
176 case 'N': | |
177 ch = 'B'; | |
178 break; | |
179 case '*': | |
180 ch = 'C'; | |
181 break; | |
182 case 'E': | |
183 ch = 'D'; | |
184 break; | |
185 default: | |
186 break; | |
187 } | |
188 int32_t code = 0; | |
189 int32_t len = (int32_t)strlen(CodaBarR.ALPHABET_STRING); | |
190 for (int32_t i = 0; i < len; i++) { | |
191 if (ch == CodaBarR.ALPHABET_STRING[i]) { | |
192 code = CodaBarR.CHARACTER_ENCODINGS[i]; | |
193 break; | |
194 } | |
195 } | |
196 uint8_t color = 1; | |
197 int32_t counter = 0; | |
198 int32_t bit = 0; | |
199 while (bit < 7) { | |
200 result[position] = color; | |
201 position++; | |
202 if (((code >> (6 - bit)) & 1) == 0 || counter == m_iWideNarrRatio - 1) { | |
203 color = !color; | |
204 bit++; | |
205 counter = 0; | |
206 } else { | |
207 counter++; | |
208 } | |
209 } | |
210 if (index < data.GetLength() - 1) { | |
211 result[position] = 0; | |
212 position++; | |
213 } | |
214 } | |
215 outLength = position; | |
216 return result; | |
217 } | |
218 CFX_WideString CBC_OnedCodaBarWriter::encodedContents( | |
219 const CFX_WideStringC& contents) { | |
220 CFX_WideString strStart(m_chStart); | |
221 CFX_WideString strEnd(m_chEnd); | |
222 return strStart + contents + strEnd; | |
223 } | |
224 void CBC_OnedCodaBarWriter::RenderResult(const CFX_WideStringC& contents, | |
225 uint8_t* code, | |
226 int32_t codeLength, | |
227 FX_BOOL isDevice, | |
228 int32_t& e) { | |
229 CBC_OneDimWriter::RenderResult(encodedContents(contents), code, codeLength, | |
230 isDevice, e); | |
231 } | |
OLD | NEW |