Merge XFA to PDFium master at 4dc95e7 on 10/28/2014
[pdfium.git] / xfa / src / fxbarcode / src / BC_BarCode.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4 \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #include "barcode.h"\r
8 #include "include/BC_Reader.h"\r
9 #include "include/BC_OneDReader.h"\r
10 #include "include/BC_OneDimReader.h"\r
11 #include "include/BC_Writer.h"\r
12 #include "include/BC_OneDimWriter.h"\r
13 #include "include/BC_OnedCode39Reader.h"\r
14 #include "include/BC_OnedCode39Writer.h"\r
15 #include "include/BC_OnedCodaBarReader.h"\r
16 #include "include/BC_OnedCodaBarWriter.h"\r
17 #include "include/BC_OnedCode128Reader.h"\r
18 #include "include/BC_OnedCode128Writer.h"\r
19 #include "include/BC_OnedEAN8Reader.h"\r
20 #include "include/BC_OnedEAN8Writer.h"\r
21 #include "include/BC_OnedEAN13Reader.h"\r
22 #include "include/BC_OnedEAN13Writer.h"\r
23 #include "include/BC_OnedUPCAReader.h"\r
24 #include "include/BC_OnedUPCAWriter.h"\r
25 #include "include/BC_QRCodeReader.h"\r
26 #include "include/BC_QRCodeWriter.h"\r
27 #include "include/BC_PDF417Compaction.h"\r
28 #include "include/BC_PDF417.h"\r
29 #include "include/BC_PDF417Writer.h"\r
30 #include "include/BC_DataMatrixReader.h"\r
31 #include "include/BC_DataMatrixWriter.h"\r
32 #include "include/BC_LuminanceSource.h"\r
33 #include "include/BC_BufferedImageLuminanceSource.h"\r
34 #include "include/BC_Binarizer.h"\r
35 #include "include/BC_GlobalHistogramBinarizer.h"\r
36 #include "include/BC_BinaryBitmap.h"\r
37 #include "include/BC_UtilCodingConvert.h"\r
38 #include "include/BC_PDF417HighLevelEncoder.h"\r
39 #include "include/BC_ResultPoint.h"\r
40 #include "include/BC_PDF417DetectorResult.h"\r
41 #include "include/BC_BinaryBitmap.h"\r
42 #include "include/BC_CommonBitMatrix.h"\r
43 #include "include/BC_CommonBitArray.h"\r
44 #include "include/BC_PDF417Detector.h"\r
45 #include "include/BC_PDF417DetectorResult.h"\r
46 #include "include/BC_DecoderResult.h"\r
47 #include "include/BC_PDF417Codeword.h"\r
48 #include "include/BC_CommonBitMatrix.h"\r
49 #include "include/BC_PDF417Common.h"\r
50 #include "include/BC_PDF417BarcodeValue.h"\r
51 #include "include/BC_PDF417BarcodeMetadata.h"\r
52 #include "include/BC_PDF417BoundingBox.h"\r
53 #include "include/BC_PDF417DetectionResultColumn.h"\r
54 #include "include/BC_PDF417DetectionResultRowIndicatorColumn.h"\r
55 #include "include/BC_PDF417DetectionResult.h"\r
56 #include "include/BC_PDF417DecodedBitStreamParser.h"\r
57 #include "include/BC_PDF417CodewordDecoder.h"\r
58 #include "include/BC_PDF417DecodedBitStreamParser.h"\r
59 #include "include/BC_PDF417ECModulusPoly.h"\r
60 #include "include/BC_PDF417ECModulusGF.h"\r
61 #include "include/BC_PDF417ECErrorCorrection.h"\r
62 #include "include/BC_PDF417DecodedBitStreamParser.h"\r
63 #include "include/BC_CommonDecoderResult.h"\r
64 #include "include/BC_PDF417ScanningDecoder.h"\r
65 #include "include/BC_PDF417Reader.h"\r
66 CBC_CodeBase :: CBC_CodeBase()\r
67 {\r
68 }\r
69 CBC_CodeBase :: ~CBC_CodeBase()\r
70 {\r
71 }\r
72 FX_BOOL CBC_CodeBase::SetCharEncoding(FX_INT32 encoding)\r
73 {\r
74     if (m_pBCWriter) {\r
75         return m_pBCWriter->SetCharEncoding(encoding);\r
76     }\r
77     return FALSE;\r
78 }\r
79 FX_BOOL CBC_CodeBase::SetModuleHeight(FX_INT32 moduleHeight)\r
80 {\r
81     if (m_pBCWriter) {\r
82         return m_pBCWriter->SetModuleHeight(moduleHeight);\r
83     }\r
84     return FALSE;\r
85 }\r
86 FX_BOOL CBC_CodeBase::SetModuleWidth(FX_INT32 moduleWidth)\r
87 {\r
88     if (m_pBCWriter) {\r
89         return m_pBCWriter->SetModuleWidth(moduleWidth);\r
90     }\r
91     return FALSE;\r
92 }\r
93 FX_BOOL CBC_CodeBase::SetHeight(FX_INT32 height)\r
94 {\r
95     if (m_pBCWriter) {\r
96         return m_pBCWriter->SetHeight(height);\r
97     }\r
98     return FALSE;\r
99 }\r
100 FX_BOOL CBC_CodeBase::SetWidth(FX_INT32 width)\r
101 {\r
102     if (m_pBCWriter) {\r
103         return m_pBCWriter->SetWidth(width);\r
104     }\r
105     return FALSE;\r
106 }\r
107 void CBC_CodeBase::SetBackgroundColor(FX_ARGB backgroundColor)\r
108 {\r
109     if (m_pBCWriter) {\r
110         m_pBCWriter->SetBackgroundColor(backgroundColor);\r
111     }\r
112 }\r
113 void CBC_CodeBase::SetBarcodeColor(FX_ARGB foregroundColor)\r
114 {\r
115     if (m_pBCWriter) {\r
116         m_pBCWriter->SetBarcodeColor(foregroundColor);\r
117     }\r
118 }\r
119 CBC_OneCode::CBC_OneCode()\r
120 {\r
121 };\r
122 CBC_OneCode::~CBC_OneCode()\r
123 {\r
124 }\r
125 FX_BOOL CBC_OneCode::CheckContentValidity(FX_WSTR contents)\r
126 {\r
127     if (m_pBCWriter) {\r
128         return ((CBC_OneDimWriter*)m_pBCWriter)->CheckContentValidity(contents);\r
129     }\r
130     return FALSE;\r
131 }\r
132 CFX_WideString CBC_OneCode::FilterContents(FX_WSTR contents)\r
133 {\r
134     CFX_WideString tmp;\r
135     if (m_pBCWriter == NULL) {\r
136         return tmp;\r
137     }\r
138     return ((CBC_OneDimWriter*)m_pBCWriter)->FilterContents(contents);\r
139 }\r
140 void CBC_OneCode::SetPrintChecksum(FX_BOOL checksum)\r
141 {\r
142     if (m_pBCWriter) {\r
143         ((CBC_OneDimWriter*)m_pBCWriter)->SetPrintChecksum(checksum);\r
144     }\r
145 }\r
146 void CBC_OneCode::SetDataLength(FX_INT32 length)\r
147 {\r
148     if (m_pBCWriter) {\r
149         ((CBC_OneDimWriter*)m_pBCWriter)->SetDataLength(length);\r
150     }\r
151 }\r
152 void CBC_OneCode::SetCalChecksum(FX_BOOL calc)\r
153 {\r
154     if (m_pBCWriter) {\r
155         ((CBC_OneDimWriter*)m_pBCWriter)->SetCalcChecksum(calc);\r
156     }\r
157 }\r
158 FX_BOOL CBC_OneCode::SetFont(CFX_Font* cFont)\r
159 {\r
160     if (m_pBCWriter) {\r
161         return ((CBC_OneDimWriter*)m_pBCWriter)->SetFont(cFont);\r
162     }\r
163     return FALSE;\r
164 }\r
165 void CBC_OneCode::SetFontSize(FX_FLOAT size)\r
166 {\r
167     if (m_pBCWriter) {\r
168         ((CBC_OneDimWriter*)m_pBCWriter)->SetFontSize(size);\r
169     }\r
170 }\r
171 void CBC_OneCode::SetFontStyle(FX_INT32 style)\r
172 {\r
173     if (m_pBCWriter) {\r
174         ((CBC_OneDimWriter*)m_pBCWriter)->SetFontStyle(style);\r
175     }\r
176 }\r
177 void CBC_OneCode::SetFontColor(FX_ARGB color)\r
178 {\r
179     if (m_pBCWriter) {\r
180         ((CBC_OneDimWriter*)m_pBCWriter)->SetFontColor(color);\r
181     }\r
182 }\r
183 CBC_Code39::CBC_Code39()\r
184 {\r
185     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_OnedCode39Reader);\r
186     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_OnedCode39Writer);\r
187 }\r
188 CBC_Code39::CBC_Code39(FX_BOOL usingCheckDigit)\r
189 {\r
190     m_pBCReader = (CBC_Reader*) FX_NEW CBC_OnedCode39Reader(usingCheckDigit);\r
191     m_pBCWriter = (CBC_Writer*) FX_NEW CBC_OnedCode39Writer;\r
192 }\r
193 CBC_Code39::CBC_Code39(FX_BOOL usingCheckDigit, FX_BOOL extendedMode)\r
194 {\r
195     m_pBCReader = (CBC_Reader*) FX_NEW CBC_OnedCode39Reader(usingCheckDigit, extendedMode);\r
196     m_pBCWriter = (CBC_Writer*) FX_NEW CBC_OnedCode39Writer(extendedMode);\r
197 }\r
198 CBC_Code39::~CBC_Code39()\r
199 {\r
200     if(m_pBCReader) {\r
201         delete(m_pBCReader);\r
202         m_pBCReader = NULL;\r
203     }\r
204     if(m_pBCWriter) {\r
205         delete(m_pBCWriter);\r
206         m_pBCWriter = NULL;\r
207     }\r
208 }\r
209 FX_BOOL CBC_Code39::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
210 {\r
211     if(contents.IsEmpty()) {\r
212         e = BCExceptionNoContents;\r
213         return FALSE;\r
214     }\r
215     BCFORMAT format     = BCFORMAT_CODE_39;\r
216     FX_INT32 outWidth = 0;\r
217     FX_INT32 outHeight = 0;\r
218     CFX_WideString filtercontents = ((CBC_OnedCode39Writer*)m_pBCWriter)->FilterContents(contents);\r
219     CFX_WideString renderContents = ((CBC_OnedCode39Writer*)m_pBCWriter)->RenderTextContents(contents);\r
220     m_renderContents = renderContents;\r
221     CFX_ByteString byteString =  filtercontents.UTF8Encode();\r
222     FX_BYTE *data = m_pBCWriter->Encode(byteString, format, outWidth, outHeight, e);\r
223     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
224     ((CBC_OneDimWriter*)m_pBCWriter)->RenderResult(renderContents, data, outWidth, isDevice, e);\r
225     FX_Free(data);\r
226     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
227     return TRUE;\r
228 }\r
229 FX_BOOL CBC_Code39::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
230 {\r
231     CFX_WideString renderCon = ((CBC_OnedCode39Writer*)m_pBCWriter)->encodedContents(m_renderContents, e);\r
232     ((CBC_OneDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx, renderCon, e);\r
233     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
234     return TRUE;\r
235 }\r
236 FX_BOOL CBC_Code39::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
237 {\r
238     CFX_WideString renderCon = ((CBC_OnedCode39Writer*)m_pBCWriter)->encodedContents(m_renderContents, e);\r
239     ((CBC_OneDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, renderCon, e);\r
240     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
241     return TRUE;\r
242 }\r
243 CFX_WideString CBC_Code39::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
244 {\r
245     CFX_WideString str;\r
246     return str;\r
247 }\r
248 CFX_WideString CBC_Code39::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
249 {\r
250     CBC_BufferedImageLuminanceSource source(pBitmap);\r
251     CBC_GlobalHistogramBinarizer binarizer(&source);\r
252     CBC_BinaryBitmap  bitmap(&binarizer);\r
253     CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);\r
254     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
255     return CFX_WideString::FromUTF8(str, str.GetLength());\r
256 }\r
257 FX_BOOL CBC_Code39::SetTextLocation(BC_TEXT_LOC location)\r
258 {\r
259     if (m_pBCWriter) {\r
260         return ((CBC_OnedCode39Writer*)m_pBCWriter)->SetTextLocation(location);\r
261     }\r
262     return FALSE;\r
263 }\r
264 FX_BOOL CBC_Code39::SetWideNarrowRatio(FX_INT32 ratio)\r
265 {\r
266     if (m_pBCWriter) {\r
267         return ((CBC_OnedCode39Writer*)m_pBCWriter)->SetWideNarrowRatio(ratio);\r
268     }\r
269     return FALSE;\r
270 }\r
271 CBC_Codabar::CBC_Codabar()\r
272 {\r
273     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_OnedCodaBarReader);\r
274     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_OnedCodaBarWriter);\r
275 }\r
276 CBC_Codabar::~CBC_Codabar()\r
277 {\r
278     if(m_pBCReader) {\r
279         delete(m_pBCReader);\r
280         m_pBCReader = NULL;\r
281     }\r
282     if(m_pBCWriter) {\r
283         delete(m_pBCWriter);\r
284         m_pBCWriter = NULL;\r
285     }\r
286 }\r
287 FX_BOOL CBC_Codabar::SetStartChar(FX_CHAR start)\r
288 {\r
289     if (m_pBCWriter) {\r
290         return ((CBC_OnedCodaBarWriter*)m_pBCWriter)->SetStartChar(start);\r
291     }\r
292     return FALSE;\r
293 }\r
294 FX_BOOL CBC_Codabar::SetEndChar(FX_CHAR end)\r
295 {\r
296     if (m_pBCWriter) {\r
297         return ((CBC_OnedCodaBarWriter*)m_pBCWriter)->SetEndChar(end);\r
298     }\r
299     return FALSE;\r
300 }\r
301 FX_BOOL CBC_Codabar::SetTextLocation(BC_TEXT_LOC location)\r
302 {\r
303     return ((CBC_OnedCodaBarWriter*) m_pBCWriter)->SetTextLocation(location);\r
304 }\r
305 FX_BOOL CBC_Codabar::SetWideNarrowRatio(FX_INT32 ratio)\r
306 {\r
307     if (m_pBCWriter) {\r
308         return ((CBC_OnedCodaBarWriter*) m_pBCWriter)->SetWideNarrowRatio(ratio);\r
309     }\r
310     return FALSE;\r
311 }\r
312 FX_BOOL CBC_Codabar::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
313 {\r
314     if(contents.IsEmpty()) {\r
315         e = BCExceptionNoContents;\r
316         return FALSE;\r
317     }\r
318     BCFORMAT format     = BCFORMAT_CODABAR;\r
319     FX_INT32 outWidth = 0;\r
320     FX_INT32 outHeight = 0;\r
321     CFX_WideString filtercontents = ((CBC_OneDimWriter*)m_pBCWriter)->FilterContents(contents);\r
322     CFX_ByteString byteString = filtercontents.UTF8Encode();\r
323     m_renderContents = filtercontents;\r
324     FX_BYTE *data = m_pBCWriter->Encode(byteString, format, outWidth, outHeight, e);\r
325     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
326     ((CBC_OneDimWriter*)m_pBCWriter)->RenderResult(filtercontents, data, outWidth, isDevice, e);\r
327     FX_Free(data);\r
328     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
329     return TRUE;\r
330 }\r
331 FX_BOOL CBC_Codabar::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
332 {\r
333     CFX_WideString renderCon = ((CBC_OnedCodaBarWriter*)m_pBCWriter)->encodedContents(m_renderContents);\r
334     ((CBC_OneDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx, renderCon, e);\r
335     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
336     return TRUE;\r
337 }\r
338 FX_BOOL CBC_Codabar::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
339 {\r
340     CFX_WideString renderCon = ((CBC_OnedCodaBarWriter*)m_pBCWriter)->encodedContents(m_renderContents);\r
341     ((CBC_OneDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, renderCon, e);\r
342     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
343     return TRUE;\r
344 }\r
345 CFX_WideString CBC_Codabar::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
346 {\r
347     CFX_WideString str;\r
348     return str;\r
349 }\r
350 CFX_WideString CBC_Codabar::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
351 {\r
352     CBC_BufferedImageLuminanceSource source(pBitmap);\r
353     CBC_GlobalHistogramBinarizer binarizer(&source);\r
354     CBC_BinaryBitmap  bitmap(&binarizer);\r
355     CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);\r
356     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
357     return CFX_WideString::FromUTF8(str, str.GetLength());\r
358 }\r
359 CBC_Code128::CBC_Code128(BC_TYPE type)\r
360 {\r
361     m_pBCReader = (CBC_Reader *) FX_NEW (CBC_OnedCode128Reader);\r
362     m_pBCWriter = (CBC_Writer *) FX_NEW CBC_OnedCode128Writer(type);\r
363 }\r
364 CBC_Code128::~CBC_Code128()\r
365 {\r
366     if(m_pBCReader) {\r
367         delete(m_pBCReader);\r
368         m_pBCReader = NULL;\r
369     }\r
370     if(m_pBCWriter) {\r
371         delete(m_pBCWriter);\r
372         m_pBCWriter = NULL;\r
373     }\r
374 }\r
375 FX_BOOL CBC_Code128::SetTextLocation(BC_TEXT_LOC location)\r
376 {\r
377     if (m_pBCWriter) {\r
378         return (( CBC_OnedCode128Writer*)m_pBCWriter)->SetTextLocation(location);\r
379     }\r
380     return FALSE;\r
381 }\r
382 FX_BOOL CBC_Code128::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
383 {\r
384     if(contents.IsEmpty()) {\r
385         e = BCExceptionNoContents;\r
386         return FALSE;\r
387     }\r
388     BCFORMAT format     = BCFORMAT_CODE_128;\r
389     FX_INT32 outWidth = 0;\r
390     FX_INT32 outHeight = 0;\r
391     CFX_WideString content = contents;\r
392     if (contents.GetLength() % 2 && ((CBC_OnedCode128Writer *)m_pBCWriter)->GetType() == BC_CODE128_C) {\r
393         content += '0';\r
394     }\r
395     CFX_WideString encodeContents = ((CBC_OnedCode128Writer*)m_pBCWriter)->FilterContents(content);\r
396     m_renderContents = encodeContents;\r
397     CFX_ByteString byteString = encodeContents.UTF8Encode();\r
398     FX_BYTE *data = m_pBCWriter->Encode(byteString, format, outWidth, outHeight, e);\r
399     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
400     ((CBC_OneDimWriter*)m_pBCWriter)->RenderResult(encodeContents, data, outWidth, isDevice, e);\r
401     FX_Free(data);\r
402     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
403     return TRUE;\r
404 }\r
405 FX_BOOL CBC_Code128::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
406 {\r
407     ((CBC_OneDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx, m_renderContents, e);\r
408     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
409     return TRUE;\r
410 }\r
411 FX_BOOL CBC_Code128::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
412 {\r
413     ((CBC_OneDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, m_renderContents, e);\r
414     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
415     return TRUE;\r
416 }\r
417 CFX_WideString CBC_Code128::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
418 {\r
419     CFX_WideString str;\r
420     return str;\r
421 }\r
422 CFX_WideString CBC_Code128::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
423 {\r
424     CBC_BufferedImageLuminanceSource source(pBitmap);\r
425     CBC_GlobalHistogramBinarizer binarizer(&source);\r
426     CBC_BinaryBitmap  bitmap(&binarizer);\r
427     CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);\r
428     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
429     return CFX_WideString::FromUTF8(str, str.GetLength());\r
430 }\r
431 CBC_EAN8::CBC_EAN8()\r
432 {\r
433     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_OnedEAN8Reader);\r
434     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_OnedEAN8Writer);\r
435 }\r
436 CBC_EAN8::~CBC_EAN8()\r
437 {\r
438     if(m_pBCReader) {\r
439         delete(m_pBCReader);\r
440         m_pBCReader = NULL;\r
441     }\r
442     if(m_pBCWriter) {\r
443         delete(m_pBCWriter);\r
444         m_pBCWriter = NULL;\r
445     }\r
446 }\r
447 CFX_WideString  CBC_EAN8::Preprocess(FX_WSTR contents)\r
448 {\r
449     CFX_WideString encodeContents = ((CBC_OnedEAN8Writer*)m_pBCWriter)->FilterContents(contents);\r
450     FX_INT32 length = encodeContents.GetLength();\r
451     if (length <= 7) {\r
452         for (FX_INT32 i = 0; i < 7 - length; i++) {\r
453             encodeContents = FX_WCHAR('0') + encodeContents;\r
454         }\r
455         CFX_ByteString byteString = encodeContents.UTF8Encode();\r
456         FX_INT32 checksum = ((CBC_OnedEAN8Writer *)m_pBCWriter)->CalcChecksum(byteString);\r
457         encodeContents += FX_WCHAR(checksum - 0 + '0');\r
458     }\r
459     if (length > 8) {\r
460         encodeContents = encodeContents.Mid(0, 8);\r
461     }\r
462     return encodeContents;\r
463 }\r
464 FX_BOOL CBC_EAN8::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
465 {\r
466     if(contents.IsEmpty()) {\r
467         e = BCExceptionNoContents;\r
468         return FALSE;\r
469     }\r
470     BCFORMAT format     = BCFORMAT_EAN_8;\r
471     FX_INT32 outWidth = 0;\r
472     FX_INT32 outHeight = 0;\r
473     CFX_WideString encodeContents = Preprocess(contents);\r
474     CFX_ByteString byteString = encodeContents.UTF8Encode();\r
475     m_renderContents = encodeContents;\r
476     FX_BYTE *data = m_pBCWriter->Encode(byteString, format, outWidth, outHeight, e);\r
477     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
478     ((CBC_OneDimWriter*)m_pBCWriter)->RenderResult(encodeContents, data, outWidth, isDevice, e);\r
479     FX_Free(data);\r
480     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
481     return TRUE;\r
482 }\r
483 FX_BOOL CBC_EAN8::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
484 {\r
485     ((CBC_OneDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx, m_renderContents, e);\r
486     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
487     return TRUE;\r
488 }\r
489 FX_BOOL CBC_EAN8::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
490 {\r
491     ((CBC_OneDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, m_renderContents, e);\r
492     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
493     return TRUE;\r
494 }\r
495 CFX_WideString CBC_EAN8::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
496 {\r
497     CFX_WideString str;\r
498     return str;\r
499 }\r
500 CFX_WideString CBC_EAN8::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
501 {\r
502     CBC_BufferedImageLuminanceSource source(pBitmap);\r
503     CBC_GlobalHistogramBinarizer binarizer(&source);\r
504     CBC_BinaryBitmap  bitmap(&binarizer);\r
505     CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);\r
506     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
507     return CFX_WideString::FromUTF8(str, str.GetLength());\r
508 }\r
509 CBC_EAN13::CBC_EAN13()\r
510 {\r
511     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_OnedEAN13Reader);\r
512     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_OnedEAN13Writer);\r
513 }\r
514 CBC_EAN13::~CBC_EAN13()\r
515 {\r
516     if(m_pBCReader) {\r
517         delete(m_pBCReader);\r
518         m_pBCReader = NULL;\r
519     }\r
520     if(m_pBCWriter) {\r
521         delete(m_pBCWriter);\r
522         m_pBCWriter = NULL;\r
523     }\r
524 }\r
525 CFX_WideString CBC_EAN13::Preprocess(FX_WSTR contents)\r
526 {\r
527     CFX_WideString encodeContents = ((CBC_OnedEAN8Writer*)m_pBCWriter)->FilterContents(contents);\r
528     FX_INT32 length = encodeContents.GetLength();\r
529     if (length <= 12) {\r
530         for (FX_INT32 i = 0; i < 12 - length; i++) {\r
531             encodeContents = FX_WCHAR('0') + encodeContents;\r
532         }\r
533         CFX_ByteString byteString = encodeContents.UTF8Encode();\r
534         FX_INT32 checksum = ((CBC_OnedEAN13Writer *)m_pBCWriter)->CalcChecksum(byteString);\r
535         byteString += checksum - 0 + '0';\r
536         encodeContents = byteString.UTF8Decode();\r
537     }\r
538     if (length > 13) {\r
539         encodeContents = encodeContents.Mid(0, 13);\r
540     }\r
541     return encodeContents;\r
542 }\r
543 FX_BOOL CBC_EAN13::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
544 {\r
545     if(contents.IsEmpty()) {\r
546         e = BCExceptionNoContents;\r
547         return FALSE;\r
548     }\r
549     BCFORMAT format     = BCFORMAT_EAN_13;\r
550     FX_INT32 outWidth = 0;\r
551     FX_INT32 outHeight = 0;\r
552     CFX_WideString encodeContents = Preprocess(contents);\r
553     CFX_ByteString byteString = encodeContents.UTF8Encode();\r
554     m_renderContents = encodeContents;\r
555     FX_BYTE *data = m_pBCWriter->Encode(byteString, format, outWidth, outHeight, e);\r
556     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
557     ((CBC_OneDimWriter*)m_pBCWriter)->RenderResult(encodeContents, data, outWidth, isDevice, e);\r
558     FX_Free(data);\r
559     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
560     return TRUE;\r
561 }\r
562 FX_BOOL CBC_EAN13::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
563 {\r
564     ((CBC_OneDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx, m_renderContents, e);\r
565     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
566     return TRUE;\r
567 }\r
568 FX_BOOL CBC_EAN13::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
569 {\r
570     ((CBC_OneDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, m_renderContents, e);\r
571     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
572     return TRUE;\r
573 }\r
574 CFX_WideString CBC_EAN13::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
575 {\r
576     CFX_WideString str;\r
577     return str;\r
578 }\r
579 CFX_WideString CBC_EAN13::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
580 {\r
581     CBC_BufferedImageLuminanceSource source(pBitmap);\r
582     CBC_GlobalHistogramBinarizer binarizer(&source);\r
583     CBC_BinaryBitmap  bitmap(&binarizer);\r
584     CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);\r
585     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
586     return CFX_WideString::FromUTF8(str, str.GetLength());\r
587 }\r
588 CBC_UPCA::CBC_UPCA()\r
589 {\r
590     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_OnedUPCAReader);\r
591     ((CBC_OnedUPCAReader *)m_pBCReader)->Init();\r
592     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_OnedUPCAWriter);\r
593 }\r
594 CBC_UPCA::~CBC_UPCA()\r
595 {\r
596     if(m_pBCReader) {\r
597         delete(m_pBCReader);\r
598         m_pBCReader = NULL;\r
599     }\r
600     if(m_pBCWriter) {\r
601         delete(m_pBCWriter);\r
602         m_pBCWriter = NULL;\r
603     }\r
604 }\r
605 CFX_WideString  CBC_UPCA::Preprocess(FX_WSTR contents)\r
606 {\r
607     CFX_WideString encodeContents = ((CBC_OnedEAN8Writer*)m_pBCWriter)->FilterContents(contents);\r
608     FX_INT32 length = encodeContents.GetLength();\r
609     if (length <= 11) {\r
610         for (FX_INT32 i = 0; i < 11 - length; i++) {\r
611             encodeContents = FX_WCHAR('0') + encodeContents;\r
612         }\r
613         CFX_ByteString byteString = encodeContents.UTF8Encode();\r
614         FX_INT32 checksum = ((CBC_OnedUPCAWriter *)m_pBCWriter)->CalcChecksum(byteString);\r
615         byteString += checksum - 0 + '0';\r
616         encodeContents = byteString.UTF8Decode();\r
617     }\r
618     if (length > 12) {\r
619         encodeContents = encodeContents.Mid(0, 12);\r
620     }\r
621     return encodeContents;\r
622 }\r
623 FX_BOOL CBC_UPCA::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
624 {\r
625     if(contents.IsEmpty()) {\r
626         e = BCExceptionNoContents;\r
627         return FALSE;\r
628     }\r
629     BCFORMAT format     = BCFORMAT_UPC_A;\r
630     FX_INT32 outWidth = 0;\r
631     FX_INT32 outHeight = 0;\r
632     CFX_WideString encodeContents = Preprocess(contents);\r
633     CFX_ByteString byteString = encodeContents.UTF8Encode();\r
634     m_renderContents = encodeContents;\r
635     ((CBC_OnedUPCAWriter*)m_pBCWriter)->Init();\r
636     FX_BYTE *data = m_pBCWriter->Encode(byteString, format, outWidth, outHeight, e);\r
637     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
638     ((CBC_OneDimWriter*)m_pBCWriter)->RenderResult(encodeContents, data, outWidth, isDevice, e);\r
639     FX_Free(data);\r
640     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
641     return TRUE;\r
642 }\r
643 FX_BOOL CBC_UPCA::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
644 {\r
645     ((CBC_OneDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx, m_renderContents, e);\r
646     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
647     return TRUE;\r
648 }\r
649 FX_BOOL CBC_UPCA::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
650 {\r
651     ((CBC_OneDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, m_renderContents, e);\r
652     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
653     return TRUE;\r
654 }\r
655 CFX_WideString CBC_UPCA::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
656 {\r
657     CFX_WideString str;\r
658     return str;\r
659 }\r
660 CFX_WideString CBC_UPCA::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
661 {\r
662     CBC_BufferedImageLuminanceSource source(pBitmap);\r
663     CBC_GlobalHistogramBinarizer binarizer(&source);\r
664     CBC_BinaryBitmap  bitmap(&binarizer);\r
665     CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e);\r
666     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
667     return CFX_WideString::FromUTF8(str, str.GetLength());\r
668 }\r
669 CBC_QRCode::CBC_QRCode()\r
670 {\r
671     m_pBCReader = (CBC_Reader *) FX_NEW (CBC_QRCodeReader);\r
672     ((CBC_QRCodeReader*)m_pBCReader)->Init();\r
673     m_pBCWriter = (CBC_Writer *) FX_NEW (CBC_QRCodeWriter);\r
674 }\r
675 CBC_QRCode ::~CBC_QRCode()\r
676 {\r
677     if(m_pBCReader) {\r
678         delete(m_pBCReader);\r
679         m_pBCReader = NULL;\r
680     }\r
681     if(m_pBCWriter) {\r
682         delete(m_pBCWriter);\r
683         m_pBCWriter = NULL;\r
684     }\r
685 }\r
686 FX_BOOL CBC_QRCode::SetVersion(FX_INT32 version)\r
687 {\r
688     if (version < 0 || version > 40) {\r
689         return FALSE;\r
690     }\r
691     if (m_pBCWriter == NULL) {\r
692         return FALSE;\r
693     }\r
694     return((CBC_QRCodeWriter*)m_pBCWriter)->SetVersion(version);\r
695 }\r
696 FX_BOOL CBC_QRCode::SetErrorCorrectionLevel (FX_INT32 level)\r
697 {\r
698     if (level < 0 || level > 3) {\r
699         return FALSE;\r
700     }\r
701     if (m_pBCWriter == NULL) {\r
702         return FALSE;\r
703     }\r
704     return ((CBC_TwoDimWriter*)m_pBCWriter)->SetErrorCorrectionLevel(level);\r
705 }\r
706 FX_BOOL CBC_QRCode::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
707 {\r
708     FX_INT32 outWidth = 0;\r
709     FX_INT32 outHeight = 0;\r
710     FX_BYTE *data = ((CBC_QRCodeWriter*)m_pBCWriter)->Encode(contents, ((CBC_QRCodeWriter*)m_pBCWriter)->GetErrorCorrectionLevel(), outWidth, outHeight, e);\r
711     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
712     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderResult(data, outWidth, outHeight, e);\r
713     FX_Free(data);\r
714     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
715     return TRUE;\r
716 }\r
717 FX_BOOL CBC_QRCode::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
718 {\r
719     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx);\r
720     return TRUE;\r
721 }\r
722 FX_BOOL CBC_QRCode::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
723 {\r
724     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, e);\r
725     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
726     return TRUE;\r
727 }\r
728 CFX_WideString CBC_QRCode::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
729 {\r
730     CFX_WideString str;\r
731     return str;\r
732 }\r
733 CFX_WideString CBC_QRCode::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
734 {\r
735     CBC_BufferedImageLuminanceSource source(pBitmap);\r
736     CBC_GlobalHistogramBinarizer binarizer(&source);\r
737     CBC_BinaryBitmap  bitmap(&binarizer);\r
738     CFX_ByteString retStr = m_pBCReader->Decode(&bitmap, 0, e);\r
739     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
740     return CFX_WideString::FromUTF8(retStr, retStr.GetLength());\r
741 }\r
742 CBC_PDF417I::CBC_PDF417I()\r
743 {\r
744     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_PDF417Reader);\r
745     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_PDF417Writer);\r
746 }\r
747 CBC_PDF417I::~CBC_PDF417I()\r
748 {\r
749     if(m_pBCReader) {\r
750         delete(m_pBCReader);\r
751         m_pBCReader = NULL;\r
752     }\r
753     if(m_pBCWriter) {\r
754         delete(m_pBCWriter);\r
755         m_pBCWriter = NULL;\r
756     }\r
757 }\r
758 FX_BOOL CBC_PDF417I::SetErrorCorrectionLevel (FX_INT32 level)\r
759 {\r
760     ((CBC_PDF417Writer*)m_pBCWriter)->SetErrorCorrectionLevel(level);\r
761     return TRUE;\r
762 }\r
763 void CBC_PDF417I::SetTruncated(FX_BOOL truncated)\r
764 {\r
765     ((CBC_PDF417Writer*)m_pBCWriter)->SetTruncated(truncated);\r
766 }\r
767 FX_BOOL CBC_PDF417I::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
768 {\r
769     BCFORMAT format     = BCFORMAT_PDF_417;\r
770     FX_INT32 outWidth = 0;\r
771     FX_INT32 outHeight = 0;\r
772     FX_BYTE *data = ((CBC_PDF417Writer*)m_pBCWriter)->Encode(contents, outWidth, outHeight, e);\r
773     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
774     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderResult(data, outWidth, outHeight, e);\r
775     FX_Free(data);\r
776     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
777     return TRUE;\r
778 }\r
779 FX_BOOL CBC_PDF417I::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
780 {\r
781     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx);\r
782     return TRUE;\r
783 }\r
784 FX_BOOL CBC_PDF417I::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
785 {\r
786     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, e);\r
787     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
788     return TRUE;\r
789 }\r
790 CFX_WideString CBC_PDF417I::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
791 {\r
792     CFX_WideString str;\r
793     return str;\r
794 }\r
795 CFX_WideString CBC_PDF417I::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
796 {\r
797     CBC_BufferedImageLuminanceSource source(pBitmap);\r
798     CBC_GlobalHistogramBinarizer binarizer(&source);\r
799     CBC_BinaryBitmap  bitmap(&binarizer);\r
800     CFX_ByteString bytestring = m_pBCReader->Decode(&bitmap, 0, e);\r
801     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
802     return CFX_WideString::FromUTF8(bytestring, bytestring.GetLength());\r
803 }\r
804 CBC_DataMatrix::CBC_DataMatrix()\r
805 {\r
806     m_pBCReader = (CBC_Reader*) FX_NEW (CBC_DataMatrixReader);\r
807     ((CBC_DataMatrixReader*)m_pBCReader)->Init();\r
808     m_pBCWriter = (CBC_Writer*) FX_NEW (CBC_DataMatrixWriter);\r
809 }\r
810 CBC_DataMatrix::~CBC_DataMatrix()\r
811 {\r
812     if(m_pBCReader) {\r
813         delete(m_pBCReader);\r
814         m_pBCReader = NULL;\r
815     }\r
816     if(m_pBCWriter) {\r
817         delete(m_pBCWriter);\r
818         m_pBCWriter = NULL;\r
819     }\r
820 }\r
821 FX_BOOL CBC_DataMatrix::Encode(FX_WSTR contents, FX_BOOL isDevice, FX_INT32 &e)\r
822 {\r
823     FX_INT32 outWidth = 0;\r
824     FX_INT32 outHeight = 0;\r
825     FX_BYTE *data = ((CBC_DataMatrixWriter *)m_pBCWriter)->Encode(contents, outWidth, outHeight, e);\r
826     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
827     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderResult(data, outWidth, outHeight, e);\r
828     FX_Free(data);\r
829     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
830     return TRUE;\r
831 }\r
832 FX_BOOL CBC_DataMatrix::RenderDevice(CFX_RenderDevice* device, const CFX_Matrix* matirx, FX_INT32 &e)\r
833 {\r
834     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderDeviceResult(device, matirx);\r
835     return TRUE;\r
836 }\r
837 FX_BOOL CBC_DataMatrix::RenderBitmap(CFX_DIBitmap *&pOutBitmap, FX_INT32 &e)\r
838 {\r
839     ((CBC_TwoDimWriter*)m_pBCWriter)->RenderBitmapResult(pOutBitmap, e);\r
840     BC_EXCEPTION_CHECK_ReturnValue(e, FALSE);\r
841     return TRUE;\r
842 }\r
843 CFX_WideString CBC_DataMatrix::Decode(FX_BYTE* buf, FX_INT32 width, FX_INT32 hight, FX_INT32 &e)\r
844 {\r
845     CFX_WideString str;\r
846     return str;\r
847 }\r
848 CFX_WideString CBC_DataMatrix::Decode(CFX_DIBitmap *pBitmap, FX_INT32 &e)\r
849 {\r
850     CBC_BufferedImageLuminanceSource source(pBitmap);\r
851     CBC_GlobalHistogramBinarizer binarizer(&source);\r
852     CBC_BinaryBitmap  bitmap(&binarizer);\r
853     CFX_ByteString retStr = m_pBCReader->Decode(&bitmap, 0, e);\r
854     BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L""));\r
855     return CFX_WideString::FromUTF8(retStr, retStr.GetLength());\r
856 }\r