Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 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 2008 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 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | #include "xfa/fxbarcode/BC_Dimension.h" |
| 24 | #include "xfa/fxbarcode/BC_TwoDimWriter.h" |
| 25 | #include "xfa/fxbarcode/BC_UtilCodingConvert.h" |
| 26 | #include "xfa/fxbarcode/BC_Writer.h" |
| 27 | #include "xfa/fxbarcode/common/BC_CommonBitMatrix.h" |
| 28 | #include "xfa/fxbarcode/common/BC_CommonByteMatrix.h" |
| 29 | #include "xfa/fxbarcode/datamatrix/BC_ASCIIEncoder.h" |
| 30 | #include "xfa/fxbarcode/datamatrix/BC_Base256Encoder.h" |
| 31 | #include "xfa/fxbarcode/datamatrix/BC_C40Encoder.h" |
| 32 | #include "xfa/fxbarcode/datamatrix/BC_DataMatrixSymbolInfo144.h" |
| 33 | #include "xfa/fxbarcode/datamatrix/BC_DataMatrixWriter.h" |
| 34 | #include "xfa/fxbarcode/datamatrix/BC_DefaultPlacement.h" |
| 35 | #include "xfa/fxbarcode/datamatrix/BC_EdifactEncoder.h" |
| 36 | #include "xfa/fxbarcode/datamatrix/BC_Encoder.h" |
| 37 | #include "xfa/fxbarcode/datamatrix/BC_EncoderContext.h" |
| 38 | #include "xfa/fxbarcode/datamatrix/BC_ErrorCorrection.h" |
| 39 | #include "xfa/fxbarcode/datamatrix/BC_HighLevelEncoder.h" |
| 40 | #include "xfa/fxbarcode/datamatrix/BC_SymbolInfo.h" |
| 41 | #include "xfa/fxbarcode/datamatrix/BC_SymbolShapeHint.h" |
| 42 | #include "xfa/fxbarcode/datamatrix/BC_TextEncoder.h" |
| 43 | #include "xfa/fxbarcode/datamatrix/BC_X12Encoder.h" |
| 44 | |
| 45 | CBC_DataMatrixWriter::CBC_DataMatrixWriter() {} |
| 46 | CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {} |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 47 | bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 48 | m_iCorrectLevel = level; |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 49 | return true; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 50 | } |
| 51 | uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents, |
| 52 | int32_t& outWidth, |
| 53 | int32_t& outHeight, |
| 54 | int32_t& e) { |
| 55 | if (outWidth < 0 || outHeight < 0) { |
| 56 | e = BCExceptionHeightAndWidthMustBeAtLeast1; |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 57 | if (e != BCExceptionNO) |
| 58 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 59 | } |
| 60 | CBC_SymbolShapeHint::SymbolShapeHint shape = |
| 61 | CBC_SymbolShapeHint::FORCE_SQUARE; |
thestig | 6dc1d77 | 2016-06-09 18:39:33 -0700 | [diff] [blame] | 62 | CBC_Dimension* minSize = nullptr; |
| 63 | CBC_Dimension* maxSize = nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 64 | CFX_WideString ecLevel; |
| 65 | CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel( |
| 66 | contents, ecLevel, shape, minSize, maxSize, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 67 | if (e != BCExceptionNO) |
| 68 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 69 | CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup( |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 70 | encoded.GetLength(), shape, minSize, maxSize, true, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 71 | if (e != BCExceptionNO) |
| 72 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 73 | CFX_WideString codewords = |
| 74 | CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 75 | if (e != BCExceptionNO) |
| 76 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 77 | CBC_DefaultPlacement* placement = |
| 78 | new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e), |
| 79 | symbolInfo->getSymbolDataHeight(e)); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 80 | if (e != BCExceptionNO) |
| 81 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 82 | placement->place(); |
| 83 | CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 84 | if (e != BCExceptionNO) |
| 85 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 86 | outWidth = bytematrix->GetWidth(); |
| 87 | outHeight = bytematrix->GetHeight(); |
| 88 | uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); |
| 89 | FXSYS_memcpy(result, bytematrix->GetArray(), outWidth * outHeight); |
| 90 | delete bytematrix; |
| 91 | delete placement; |
| 92 | return result; |
| 93 | } |
| 94 | CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel( |
| 95 | CBC_DefaultPlacement* placement, |
| 96 | CBC_SymbolInfo* symbolInfo, |
| 97 | int32_t& e) { |
| 98 | int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 99 | if (e != BCExceptionNO) |
| 100 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 101 | int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 102 | if (e != BCExceptionNO) |
| 103 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 104 | CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix( |
| 105 | symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e)); |
Tom Sepez | c8017b2 | 2017-01-31 13:02:10 -0800 | [diff] [blame^] | 106 | if (e != BCExceptionNO) |
| 107 | return nullptr; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 108 | matrix->Init(); |
| 109 | int32_t matrixY = 0; |
| 110 | for (int32_t y = 0; y < symbolHeight; y++) { |
| 111 | int32_t matrixX; |
| 112 | if ((y % symbolInfo->m_matrixHeight) == 0) { |
| 113 | matrixX = 0; |
| 114 | for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) { |
| 115 | matrix->Set(matrixX, matrixY, (x % 2) == 0); |
| 116 | matrixX++; |
| 117 | } |
| 118 | matrixY++; |
| 119 | } |
| 120 | matrixX = 0; |
| 121 | for (int32_t x = 0; x < symbolWidth; x++) { |
| 122 | if ((x % symbolInfo->m_matrixWidth) == 0) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 123 | matrix->Set(matrixX, matrixY, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 124 | matrixX++; |
| 125 | } |
| 126 | matrix->Set(matrixX, matrixY, placement->getBit(x, y)); |
| 127 | matrixX++; |
| 128 | if ((x % symbolInfo->m_matrixWidth) == symbolInfo->m_matrixWidth - 1) { |
| 129 | matrix->Set(matrixX, matrixY, (y % 2) == 0); |
| 130 | matrixX++; |
| 131 | } |
| 132 | } |
| 133 | matrixY++; |
| 134 | if ((y % symbolInfo->m_matrixHeight) == symbolInfo->m_matrixHeight - 1) { |
| 135 | matrixX = 0; |
| 136 | for (int32_t x = 0; x < symbolInfo->getSymbolWidth(e); x++) { |
tsepez | d19e912 | 2016-11-02 15:43:18 -0700 | [diff] [blame] | 137 | matrix->Set(matrixX, matrixY, true); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 138 | matrixX++; |
| 139 | } |
| 140 | matrixY++; |
| 141 | } |
| 142 | } |
| 143 | return matrix; |
| 144 | } |