blob: 30ec1eef745e9e2f4a3d928e74cc5fddc9f2123a [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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 Sinclair1770c022016-03-14 14:14:16 -040023#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
45CBC_DataMatrixWriter::CBC_DataMatrixWriter() {}
46CBC_DataMatrixWriter::~CBC_DataMatrixWriter() {}
tsepezd19e9122016-11-02 15:43:18 -070047bool CBC_DataMatrixWriter::SetErrorCorrectionLevel(int32_t level) {
Dan Sinclair1770c022016-03-14 14:14:16 -040048 m_iCorrectLevel = level;
tsepezd19e9122016-11-02 15:43:18 -070049 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -040050}
51uint8_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 Sepezc8017b22017-01-31 13:02:10 -080057 if (e != BCExceptionNO)
58 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040059 }
60 CBC_SymbolShapeHint::SymbolShapeHint shape =
61 CBC_SymbolShapeHint::FORCE_SQUARE;
thestig6dc1d772016-06-09 18:39:33 -070062 CBC_Dimension* minSize = nullptr;
63 CBC_Dimension* maxSize = nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040064 CFX_WideString ecLevel;
65 CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel(
66 contents, ecLevel, shape, minSize, maxSize, e);
Tom Sepezc8017b22017-01-31 13:02:10 -080067 if (e != BCExceptionNO)
68 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040069 CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup(
tsepezd19e9122016-11-02 15:43:18 -070070 encoded.GetLength(), shape, minSize, maxSize, true, e);
Tom Sepezc8017b22017-01-31 13:02:10 -080071 if (e != BCExceptionNO)
72 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040073 CFX_WideString codewords =
74 CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e);
Tom Sepezc8017b22017-01-31 13:02:10 -080075 if (e != BCExceptionNO)
76 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040077 CBC_DefaultPlacement* placement =
78 new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e),
79 symbolInfo->getSymbolDataHeight(e));
Tom Sepezc8017b22017-01-31 13:02:10 -080080 if (e != BCExceptionNO)
81 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040082 placement->place();
83 CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e);
Tom Sepezc8017b22017-01-31 13:02:10 -080084 if (e != BCExceptionNO)
85 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -040086 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}
94CBC_CommonByteMatrix* CBC_DataMatrixWriter::encodeLowLevel(
95 CBC_DefaultPlacement* placement,
96 CBC_SymbolInfo* symbolInfo,
97 int32_t& e) {
98 int32_t symbolWidth = symbolInfo->getSymbolDataWidth(e);
Tom Sepezc8017b22017-01-31 13:02:10 -080099 if (e != BCExceptionNO)
100 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400101 int32_t symbolHeight = symbolInfo->getSymbolDataHeight(e);
Tom Sepezc8017b22017-01-31 13:02:10 -0800102 if (e != BCExceptionNO)
103 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400104 CBC_CommonByteMatrix* matrix = new CBC_CommonByteMatrix(
105 symbolInfo->getSymbolWidth(e), symbolInfo->getSymbolHeight(e));
Tom Sepezc8017b22017-01-31 13:02:10 -0800106 if (e != BCExceptionNO)
107 return nullptr;
Dan Sinclair1770c022016-03-14 14:14:16 -0400108 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) {
tsepezd19e9122016-11-02 15:43:18 -0700123 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -0400124 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++) {
tsepezd19e9122016-11-02 15:43:18 -0700137 matrix->Set(matrixX, matrixY, true);
Dan Sinclair1770c022016-03-14 14:14:16 -0400138 matrixX++;
139 }
140 matrixY++;
141 }
142 }
143 return matrix;
144}