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_code128.h" |
| 23 | |
| 24 | #include "xfa/fxbarcode/BC_BinaryBitmap.h" |
| 25 | #include "xfa/fxbarcode/BC_BufferedImageLuminanceSource.h" |
| 26 | #include "xfa/fxbarcode/common/BC_GlobalHistogramBinarizer.h" |
| 27 | #include "xfa/fxbarcode/oned/BC_OnedCode128Reader.h" |
| 28 | #include "xfa/fxbarcode/oned/BC_OnedCode128Writer.h" |
| 29 | |
| 30 | CBC_Code128::CBC_Code128(BC_TYPE type) { |
| 31 | m_pBCReader = (CBC_Reader*)new (CBC_OnedCode128Reader); |
| 32 | m_pBCWriter = (CBC_Writer*)new CBC_OnedCode128Writer(type); |
| 33 | } |
| 34 | |
| 35 | CBC_Code128::~CBC_Code128() { |
| 36 | delete (m_pBCReader); |
| 37 | delete (m_pBCWriter); |
| 38 | } |
| 39 | |
| 40 | FX_BOOL CBC_Code128::SetTextLocation(BC_TEXT_LOC location) { |
| 41 | if (m_pBCWriter) |
| 42 | return ((CBC_OnedCode128Writer*)m_pBCWriter)->SetTextLocation(location); |
| 43 | return FALSE; |
| 44 | } |
| 45 | |
| 46 | FX_BOOL CBC_Code128::Encode(const CFX_WideStringC& contents, |
| 47 | FX_BOOL isDevice, |
| 48 | int32_t& e) { |
| 49 | if (contents.IsEmpty()) { |
| 50 | e = BCExceptionNoContents; |
| 51 | return FALSE; |
| 52 | } |
| 53 | BCFORMAT format = BCFORMAT_CODE_128; |
| 54 | int32_t outWidth = 0; |
| 55 | int32_t outHeight = 0; |
| 56 | CFX_WideString content = contents; |
| 57 | if (contents.GetLength() % 2 && |
| 58 | ((CBC_OnedCode128Writer*)m_pBCWriter)->GetType() == BC_CODE128_C) { |
| 59 | content += '0'; |
| 60 | } |
tsepez | fc58ad1 | 2016-04-05 12:22:15 -0700 | [diff] [blame] | 61 | CFX_WideString encodeContents = ((CBC_OnedCode128Writer*)m_pBCWriter) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 62 | ->FilterContents(content.AsStringC()); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 63 | m_renderContents = encodeContents; |
| 64 | CFX_ByteString byteString = encodeContents.UTF8Encode(); |
| 65 | uint8_t* data = static_cast<CBC_OnedCode128Writer*>(m_pBCWriter) |
| 66 | ->Encode(byteString, format, outWidth, outHeight, e); |
| 67 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 68 | ((CBC_OneDimWriter*)m_pBCWriter) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 69 | ->RenderResult(encodeContents.AsStringC(), data, outWidth, isDevice, e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 70 | FX_Free(data); |
| 71 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 72 | return TRUE; |
| 73 | } |
| 74 | |
| 75 | FX_BOOL CBC_Code128::RenderDevice(CFX_RenderDevice* device, |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame^] | 76 | const CFX_Matrix* matrix, |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 77 | int32_t& e) { |
| 78 | ((CBC_OneDimWriter*)m_pBCWriter) |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame^] | 79 | ->RenderDeviceResult(device, matrix, m_renderContents.AsStringC(), e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 80 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 81 | return TRUE; |
| 82 | } |
| 83 | |
| 84 | FX_BOOL CBC_Code128::RenderBitmap(CFX_DIBitmap*& pOutBitmap, int32_t& e) { |
| 85 | ((CBC_OneDimWriter*)m_pBCWriter) |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 86 | ->RenderBitmapResult(pOutBitmap, m_renderContents.AsStringC(), e); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 87 | BC_EXCEPTION_CHECK_ReturnValue(e, FALSE); |
| 88 | return TRUE; |
| 89 | } |
| 90 | |
| 91 | CFX_WideString CBC_Code128::Decode(uint8_t* buf, |
| 92 | int32_t width, |
thestig | fbe14b9 | 2016-05-02 13:31:10 -0700 | [diff] [blame^] | 93 | int32_t height, |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 94 | int32_t& e) { |
| 95 | CFX_WideString str; |
| 96 | return str; |
| 97 | } |
| 98 | |
| 99 | CFX_WideString CBC_Code128::Decode(CFX_DIBitmap* pBitmap, int32_t& e) { |
| 100 | CBC_BufferedImageLuminanceSource source(pBitmap); |
| 101 | CBC_GlobalHistogramBinarizer binarizer(&source); |
| 102 | CBC_BinaryBitmap bitmap(&binarizer); |
| 103 | CFX_ByteString str = m_pBCReader->Decode(&bitmap, 0, e); |
| 104 | BC_EXCEPTION_CHECK_ReturnValue(e, FX_WSTRC(L"")); |
tsepez | 4c3debb | 2016-04-08 12:20:38 -0700 | [diff] [blame] | 105 | return CFX_WideString::FromUTF8(str.AsStringC()); |
Dan Sinclair | a98600a | 2016-03-21 15:15:56 -0400 | [diff] [blame] | 106 | } |