Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 1 | // Copyright 2016 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 | /* |
| 7 | * Copyright 2011 ZXing authors |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (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 |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
| 21 | |
| 22 | #include "xfa/fxbarcode/cbc_upca.h" |
| 23 | |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 24 | #include "xfa/fxbarcode/oned/BC_OnedUPCAWriter.h" |
| 25 | |
dsinclair | a261534 | 2016-06-16 12:29:07 -0700 | [diff] [blame] | 26 | CBC_UPCA::CBC_UPCA() : CBC_OneCode(new CBC_OnedUPCAWriter) {} |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 27 | |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 28 | CBC_UPCA::~CBC_UPCA() {} |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 29 | |
| 30 | CFX_WideString CBC_UPCA::Preprocess(const CFX_WideStringC& contents) { |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 31 | CBC_OnedUPCAWriter* pWriter = |
| 32 | static_cast<CBC_OnedUPCAWriter*>(m_pBCWriter.get()); |
| 33 | CFX_WideString encodeContents = pWriter->FilterContents(contents); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 34 | int32_t length = encodeContents.GetLength(); |
| 35 | if (length <= 11) { |
| 36 | for (int32_t i = 0; i < 11 - length; i++) |
| 37 | encodeContents = FX_WCHAR('0') + encodeContents; |
| 38 | |
| 39 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 40 | int32_t checksum = pWriter->CalcChecksum(byteString); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 41 | byteString += checksum - 0 + '0'; |
| 42 | encodeContents = byteString.UTF8Decode(); |
| 43 | } |
| 44 | if (length > 12) |
| 45 | encodeContents = encodeContents.Mid(0, 12); |
| 46 | |
| 47 | return encodeContents; |
| 48 | } |
| 49 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 50 | bool CBC_UPCA::Encode(const CFX_WideStringC& contents, |
| 51 | bool isDevice, |
| 52 | int32_t& e) { |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 53 | if (contents.IsEmpty()) { |
| 54 | e = BCExceptionNoContents; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 55 | return false; |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 56 | } |
| 57 | BCFORMAT format = BCFORMAT_UPC_A; |
| 58 | int32_t outWidth = 0; |
| 59 | int32_t outHeight = 0; |
| 60 | CFX_WideString encodeContents = Preprocess(contents); |
| 61 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
| 62 | m_renderContents = encodeContents; |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 63 | |
| 64 | CBC_OnedUPCAWriter* pWriter = |
| 65 | static_cast<CBC_OnedUPCAWriter*>(m_pBCWriter.get()); |
| 66 | |
| 67 | pWriter->Init(); |
| 68 | uint8_t* data = pWriter->Encode(byteString, format, outWidth, outHeight, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 69 | if (e != BCExceptionNO) |
| 70 | return false; |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 71 | pWriter->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, |
| 72 | e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 73 | FX_Free(data); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 74 | if (e != BCExceptionNO) |
| 75 | return false; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 76 | return true; |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 77 | } |
| 78 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 79 | bool CBC_UPCA::RenderDevice(CFX_RenderDevice* device, |
| 80 | const CFX_Matrix* matrix, |
| 81 | int32_t& e) { |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 82 | static_cast<CBC_OneDimWriter*>(m_pBCWriter.get()) |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame] | 83 | ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 84 | if (e != BCExceptionNO) |
| 85 | return false; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 86 | return true; |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 87 | } |
| 88 | |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 89 | bool CBC_UPCA::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { |
tsepez | 52d7856 | 2016-06-06 12:57:44 -0700 | [diff] [blame] | 90 | static_cast<CBC_OneDimWriter*>(m_pBCWriter.get()) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 91 | ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame] | 92 | if (e != BCExceptionNO) |
| 93 | return false; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 94 | return true; |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 95 | } |
| 96 | |
weili | 29b8ad0 | 2016-06-14 18:20:04 -0700 | [diff] [blame] | 97 | BC_TYPE CBC_UPCA::GetType() { |
| 98 | return BC_UPCA; |
| 99 | } |