OLD | NEW |
1 // Copyright 2016 PDFium Authors. All rights reserved. | 1 // Copyright 2016 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 /* | 6 /* |
7 * Copyright 2011 ZXing authors | 7 * Copyright 2011 ZXing authors |
8 * | 8 * |
9 * Licensed under the Apache License, Version 2.0 (the "License"); | 9 * Licensed under the Apache License, Version 2.0 (the "License"); |
10 * you may not use this file except in compliance with the License. | 10 * you may not use this file except in compliance with the License. |
11 * You may obtain a copy of the License at | 11 * You may obtain a copy of the License at |
12 * | 12 * |
13 * http://www.apache.org/licenses/LICENSE-2.0 | 13 * http://www.apache.org/licenses/LICENSE-2.0 |
14 * | 14 * |
15 * Unless required by applicable law or agreed to in writing, software | 15 * Unless required by applicable law or agreed to in writing, software |
16 * distributed under the License is distributed on an "AS IS" BASIS, | 16 * distributed under the License is distributed on an "AS IS" BASIS, |
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 * See the License for the specific language governing permissions and | 18 * See the License for the specific language governing permissions and |
19 * limitations under the License. | 19 * limitations under the License. |
20 */ | 20 */ |
21 | 21 |
22 #include "xfa/fxbarcode/cbc_ean13.h" | 22 #include "xfa/fxbarcode/cbc_ean13.h" |
23 | 23 |
24 #include "xfa/fxbarcode/BC_BinaryBitmap.h" | 24 #include "xfa/fxbarcode/BC_BinaryBitmap.h" |
25 #include "xfa/fxbarcode/BC_BufferedImageLuminanceSource.h" | 25 #include "xfa/fxbarcode/BC_BufferedImageLuminanceSource.h" |
26 #include "xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h" | 26 #include "xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h" |
27 #include "xfa/fxbarcode/oned/BC_OnedEAN13Reader.h" | 27 #include "xfa/fxbarcode/oned/BC_OnedEAN13Reader.h" |
28 #include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h" | 28 #include "xfa/fxbarcode/oned/BC_OnedEAN13Writer.h" |
29 | 29 |
30 CBC_EAN13::CBC_EAN13() { | 30 CBC_EAN13::CBC_EAN13() |
31 m_pBCReader = (CBC_Reader*)new (CBC_OnedEAN13Reader); | 31 : CBC_OneCode(new CBC_OnedEAN13Reader, new CBC_OnedEAN13Writer) {} |
32 m_pBCWriter = (CBC_Writer*)new (CBC_OnedEAN13Writer); | |
33 } | |
34 | 32 |
35 CBC_EAN13::~CBC_EAN13() { | 33 CBC_EAN13::~CBC_EAN13() {} |
36 delete (m_pBCReader); | |
37 delete (m_pBCWriter); | |
38 } | |
39 | 34 |
40 CFX_WideString CBC_EAN13::Preprocess(const CFX_WideStringC& contents) { | 35 CFX_WideString CBC_EAN13::Preprocess(const CFX_WideStringC& contents) { |
41 CFX_WideString encodeContents = | 36 CFX_WideString encodeContents = |
42 ((CBC_OnedEAN13Writer*)m_pBCWriter)->FilterContents(contents); | 37 static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter.get()) |
| 38 ->FilterContents(contents); |
43 int32_t length = encodeContents.GetLength(); | 39 int32_t length = encodeContents.GetLength(); |
44 if (length <= 12) { | 40 if (length <= 12) { |
45 for (int32_t i = 0; i < 12 - length; i++) | 41 for (int32_t i = 0; i < 12 - length; i++) |
46 encodeContents = FX_WCHAR('0') + encodeContents; | 42 encodeContents = FX_WCHAR('0') + encodeContents; |
47 | 43 |
48 CFX_ByteString byteString = encodeContents.UTF8Encode(); | 44 CFX_ByteString byteString = encodeContents.UTF8Encode(); |
49 int32_t checksum = | 45 int32_t checksum = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter.get()) |
50 ((CBC_OnedEAN13Writer*)m_pBCWriter)->CalcChecksum(byteString); | 46 ->CalcChecksum(byteString); |
51 byteString += checksum - 0 + '0'; | 47 byteString += checksum - 0 + '0'; |
52 encodeContents = byteString.UTF8Decode(); | 48 encodeContents = byteString.UTF8Decode(); |
53 } | 49 } |
54 if (length > 13) | 50 if (length > 13) |
55 encodeContents = encodeContents.Mid(0, 13); | 51 encodeContents = encodeContents.Mid(0, 13); |
56 | 52 |
57 return encodeContents; | 53 return encodeContents; |
58 } | 54 } |
59 | 55 |
60 FX_BOOL CBC_EAN13::Encode(const CFX_WideStringC& contents, | 56 FX_BOOL CBC_EAN13::Encode(const CFX_WideStringC& contents, |
61 FX_BOOL isDevice, | 57 FX_BOOL isDevice, |
62 int32_t& e) { | 58 int32_t& e) { |
63 if (contents.IsEmpty()) { | 59 if (contents.IsEmpty()) { |
64 e = BCExceptionNoContents; | 60 e = BCExceptionNoContents; |
65 return FALSE; | 61 return FALSE; |
66 } | 62 } |
67 BCFORMAT format = BCFORMAT_EAN_13; | 63 BCFORMAT format = BCFORMAT_EAN_13; |
68 int32_t outWidth = 0; | 64 int32_t outWidth = 0; |
69 int32_t outHeight = 0; | 65 int32_t outHeight = 0; |
70 CFX_WideString encodeContents = Preprocess(contents); | 66 CFX_WideString encodeContents = Preprocess(contents); |
71 CFX_ByteString byteString = encodeContents.UTF8Encode(); | 67 CFX_ByteString byteString = encodeContents.UTF8Encode(); |
72 m_renderContents = encodeContents; | 68 m_renderContents = encodeContents; |
73 uint8_t* data = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter) | 69 uint8_t* data = static_cast<CBC_OnedEAN13Writer*>(m_pBCWriter.get()) |
74 ->Encode(byteString, format, outWidth, outHeight, e); | 70 ->Encode(byteString, format, outWidth, outHeight, e); |
75 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | 71 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
76 ((CBC_OneDimWriter*)m_pBCWriter) | 72 static_cast<CBC_OneDimWriter*>(m_pBCWriter.get()) |
77 ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e); | 73 ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e); |
78 FX_Free(data); | 74 FX_Free(data); |
79 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | 75 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
80 return TRUE; | 76 return TRUE; |
81 } | 77 } |
82 | 78 |
83 FX_BOOL CBC_EAN13::RenderDevice(CFX_RenderDevice* device, | 79 FX_BOOL CBC_EAN13::RenderDevice(CFX_RenderDevice* device, |
84 const CFX_Matrix* matrix, | 80 const CFX_Matrix* matrix, |
85 int32_t& e) { | 81 int32_t& e) { |
86 ((CBC_OneDimWriter*)m_pBCWriter) | 82 static_cast<CBC_OneDimWriter*>(m_pBCWriter.get()) |
87 ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e); | 83 ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e); |
88 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | 84 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
89 return TRUE; | 85 return TRUE; |
90 } | 86 } |
91 | 87 |
92 FX_BOOL CBC_EAN13::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { | 88 FX_BOOL CBC_EAN13::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { |
93 ((CBC_OneDimWriter*)m_pBCWriter) | 89 static_cast<CBC_OneDimWriter*>(m_pBCWriter.get()) |
94 ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e); | 90 ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e); |
95 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); | 91 BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
96 return TRUE; | 92 return TRUE; |
97 } | 93 } |
98 | 94 |
99 CFX_WideString CBC_EAN13::Decode(uint8_t* buf, | 95 CFX_WideString CBC_EAN13::Decode(uint8_t* buf, |
100 int32_t width, | 96 int32_t width, |
101 int32_t height, | 97 int32_t height, |
102 int32_t& e) { | 98 int32_t& e) { |
103 CFX_WideString str; | 99 CFX_WideString str; |
104 return str; | 100 return str; |
105 } | 101 } |
106 | 102 |
107 CFX_WideString CBC_EAN13::Decode(CFX_DIBitmap* pBitmap, int32_t& e) { | 103 CFX_WideString CBC_EAN13::Decode(CFX_DIBitmap* pBitmap, int32_t& e) { |
108 CBC_BufferedImageLuminanceSource source(pBitmap); | 104 CBC_BufferedImageLuminanceSource source(pBitmap); |
109 CBC_GlobalHistogramBinarizer binarizer(&source); | 105 CBC_GlobalHistogramBinarizer binarizer(&source); |
110 CBC_BinaryBitmap bitmap(&binarizer); | 106 CBC_BinaryBitmap bitmap(&binarizer); |
111 CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e); | 107 CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e); |
112 BC_EXCEPTION_CHECK_ReturnValue(e, CFX_WideString()); | 108 BC_EXCEPTION_CHECK_ReturnValue(e, CFX_WideString()); |
113 return CFX_WideString::FromUTF8(str.AsStringC()); | 109 return CFX_WideString::FromUTF8(str.AsStringC()); |
114 } | 110 } |
OLD | NEW |