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 2010 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/oned/BC_OneDReader.h" | |
26 #include "xfa/src/fxbarcode/oned/BC_OneDimWriter.h" | |
27 #include "xfa/src/fxbarcode/oned/BC_OnedCode128Reader.h" | |
28 #include "xfa/src/fxbarcode/oned/BC_OnedCode128Writer.h" | |
29 | |
30 const int32_t CBC_OnedCode128Writer::CODE_CODE_B = 100; | |
31 const int32_t CBC_OnedCode128Writer::CODE_CODE_C = 99; | |
32 const int32_t CBC_OnedCode128Writer::CODE_START_B = 104; | |
33 const int32_t CBC_OnedCode128Writer::CODE_START_C = 105; | |
34 const int32_t CBC_OnedCode128Writer::CODE_STOP = 106; | |
35 | |
36 CBC_OnedCode128Writer::CBC_OnedCode128Writer() { | |
37 m_codeFormat = BC_CODE128_B; | |
38 } | |
39 CBC_OnedCode128Writer::CBC_OnedCode128Writer(BC_TYPE type) { | |
40 m_codeFormat = type; | |
41 } | |
42 CBC_OnedCode128Writer::~CBC_OnedCode128Writer() {} | |
43 BC_TYPE CBC_OnedCode128Writer::GetType() { | |
44 return m_codeFormat; | |
45 } | |
46 FX_BOOL CBC_OnedCode128Writer::CheckContentValidity( | |
47 const CFX_WideStringC& contents) { | |
48 FX_BOOL ret = TRUE; | |
49 int32_t position = 0; | |
50 int32_t patternIndex = -1; | |
51 if (m_codeFormat == BC_CODE128_B || m_codeFormat == BC_CODE128_C) { | |
52 while (position < contents.GetLength()) { | |
53 patternIndex = (int32_t)contents.GetAt(position); | |
54 if (patternIndex >= 32 && patternIndex <= 126 && patternIndex != 34) { | |
55 position++; | |
56 continue; | |
57 } else { | |
58 ret = FALSE; | |
59 break; | |
60 } | |
61 position++; | |
62 } | |
63 } else { | |
64 ret = FALSE; | |
65 } | |
66 return ret; | |
67 } | |
68 CFX_WideString CBC_OnedCode128Writer::FilterContents( | |
69 const CFX_WideStringC& contents) { | |
70 CFX_WideString filterChineseChar; | |
71 FX_WCHAR ch; | |
72 for (int32_t i = 0; i < contents.GetLength(); i++) { | |
73 ch = contents.GetAt(i); | |
74 if (ch > 175) { | |
75 i++; | |
76 continue; | |
77 } | |
78 filterChineseChar += ch; | |
79 } | |
80 CFX_WideString filtercontents; | |
81 if (m_codeFormat == BC_CODE128_B) { | |
82 for (int32_t i = 0; i < filterChineseChar.GetLength(); i++) { | |
83 ch = filterChineseChar.GetAt(i); | |
84 if (ch >= 32 && ch <= 126) { | |
85 filtercontents += ch; | |
86 } else { | |
87 continue; | |
88 } | |
89 } | |
90 } else if (m_codeFormat == BC_CODE128_C) { | |
91 for (int32_t i = 0; i < filterChineseChar.GetLength(); i++) { | |
92 ch = filterChineseChar.GetAt(i); | |
93 if (ch >= 32 && ch <= 106) { | |
94 filtercontents += ch; | |
95 } else { | |
96 continue; | |
97 } | |
98 } | |
99 } else { | |
100 filtercontents = contents; | |
101 } | |
102 return filtercontents; | |
103 } | |
104 FX_BOOL CBC_OnedCode128Writer::SetTextLocation(BC_TEXT_LOC location) { | |
105 if (location < BC_TEXT_LOC_NONE || location > BC_TEXT_LOC_BELOWEMBED) { | |
106 return FALSE; | |
107 } | |
108 m_locTextLoc = location; | |
109 return TRUE; | |
110 } | |
111 uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, | |
112 BCFORMAT format, | |
113 int32_t& outWidth, | |
114 int32_t& outHeight, | |
115 int32_t hints, | |
116 int32_t& e) { | |
117 if (format != BCFORMAT_CODE_128) { | |
118 e = BCExceptionOnlyEncodeCODE_128; | |
119 return NULL; | |
120 } | |
121 uint8_t* ret = | |
122 CBC_OneDimWriter::Encode(contents, format, outWidth, outHeight, hints, e); | |
123 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
124 return ret; | |
125 } | |
126 uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, | |
127 BCFORMAT format, | |
128 int32_t& outWidth, | |
129 int32_t& outHeight, | |
130 int32_t& e) { | |
131 uint8_t* ret = Encode(contents, format, outWidth, outHeight, 0, e); | |
132 BC_EXCEPTION_CHECK_ReturnValue(e, NULL); | |
133 return ret; | |
134 } | |
135 FX_BOOL CBC_OnedCode128Writer::IsDigits(const CFX_ByteString& contents, | |
136 int32_t start, | |
137 int32_t length) { | |
138 int32_t end = start + length; | |
139 for (int32_t i = start; i < end; i++) { | |
140 if (contents[i] < '0' || contents[i] > '9') { | |
141 return FALSE; | |
142 } | |
143 } | |
144 return TRUE; | |
145 } | |
146 uint8_t* CBC_OnedCode128Writer::Encode(const CFX_ByteString& contents, | |
147 int32_t& outLength, | |
148 int32_t& e) { | |
149 if (contents.GetLength() < 1 || contents.GetLength() > 80) { | |
150 e = BCExceptionContentsLengthShouldBetween1and80; | |
151 return NULL; | |
152 } | |
153 CFX_PtrArray patterns; | |
154 int32_t checkSum = 0; | |
155 if (m_codeFormat == BC_CODE128_B) { | |
156 checkSum = Encode128B(contents, patterns); | |
157 } else if (m_codeFormat == BC_CODE128_C) { | |
158 checkSum = Encode128C(contents, patterns); | |
159 } else { | |
160 e = BCExceptionFormatException; | |
161 return NULL; | |
162 } | |
163 checkSum %= 103; | |
164 patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[checkSum]); | |
165 patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_STOP]); | |
166 m_iContentLen = contents.GetLength() + 3; | |
167 int32_t codeWidth = 0; | |
168 for (int32_t k = 0; k < patterns.GetSize(); k++) { | |
169 int32_t* pattern = (int32_t*)patterns[k]; | |
170 for (int32_t j = 0; j < 7; j++) { | |
171 codeWidth += pattern[j]; | |
172 } | |
173 } | |
174 outLength = codeWidth; | |
175 uint8_t* result = FX_Alloc(uint8_t, outLength); | |
176 int32_t pos = 0; | |
177 for (int32_t j = 0; j < patterns.GetSize(); j++) { | |
178 int32_t* pattern = (int32_t*)patterns[j]; | |
179 pos += AppendPattern(result, pos, pattern, 7, 1, e); | |
180 if (e != BCExceptionNO) { | |
181 FX_Free(result); | |
182 return NULL; | |
183 } | |
184 } | |
185 return result; | |
186 } | |
187 int32_t CBC_OnedCode128Writer::Encode128B(const CFX_ByteString& contents, | |
188 CFX_PtrArray& patterns) { | |
189 int32_t checkSum = 0; | |
190 int32_t checkWeight = 1; | |
191 int32_t position = 0; | |
192 patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_START_B]); | |
193 checkSum += CODE_START_B * checkWeight; | |
194 while (position < contents.GetLength()) { | |
195 int32_t patternIndex = 0; | |
196 patternIndex = contents[position] - ' '; | |
197 position += 1; | |
198 patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[patternIndex]); | |
199 checkSum += patternIndex * checkWeight; | |
200 if (position != 0) { | |
201 checkWeight++; | |
202 } | |
203 } | |
204 return checkSum; | |
205 } | |
206 int32_t CBC_OnedCode128Writer::Encode128C(const CFX_ByteString& contents, | |
207 CFX_PtrArray& patterns) { | |
208 int32_t checkSum = 0; | |
209 int32_t checkWeight = 1; | |
210 int32_t position = 0; | |
211 patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[CODE_START_C]); | |
212 checkSum += CODE_START_C * checkWeight; | |
213 while (position < contents.GetLength()) { | |
214 int32_t patternIndex = 0; | |
215 FX_CHAR ch = contents.GetAt(position); | |
216 if (ch < '0' || ch > '9') { | |
217 patternIndex = (int32_t)ch; | |
218 position++; | |
219 } else { | |
220 patternIndex = FXSYS_atoi(contents.Mid(position, 2)); | |
221 if (contents.GetAt(position + 1) < '0' || | |
222 contents.GetAt(position + 1) > '9') { | |
223 position += 1; | |
224 } else { | |
225 position += 2; | |
226 } | |
227 } | |
228 patterns.Add((int32_t*)CBC_OnedCode128Reader::CODE_PATTERNS[patternIndex]); | |
229 checkSum += patternIndex * checkWeight; | |
230 if (position != 0) { | |
231 checkWeight++; | |
232 } | |
233 } | |
234 return checkSum; | |
235 } | |
OLD | NEW |