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