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